0

I am facing an issue with broadcasting the processed 4D NumPy array into a 4D array. I have compared the dimensions to check something wrong with the dimensions. I am not understanding what's wrong.

train_path = files
train_file_names = os.listdir(train_path)
train_file_names.sort(key=lambda x: int(x.partition('.')[0]))
seg_num = 60
seg_len = 2
sample_num = len(df)
data = np.zeros((seg_num*100, 496, 64, 1))
label = np.zeros((seg_num * sample_num,))

for i, file_name in enumerate(train_file_names):
        sr, sound_file = wavfile.read(train_path + file_name)
        # print(train_path+file_name)
        length = sr * seg_len           # 5s segment
        range_high = len(sound_file) - length
        print(range_high, length)
        random_start = np.random.randint(range_high, size=seg_num)
        print("i", i)
        for j in range(seg_num):
            cur_wav = sound_file[random_start[j]:random_start[j] + length]
            cur_wav = cur_wav / 32768.0
            cur_spectro = preprocess_sound(cur_wav, sr)
            #print(cur_spectro.shape)
            cur_spectro = np.copy(np.expand_dims(cur_spectro, 3))
            print("cur_spectro",cur_spectro.shape)
            print("data", data.shape)
            print(data[i * seg_num + j, :, :, :].shape)
            data[i * seg_num + j, :, :, :] = cur_spectro
            label[i * seg_num + j] = df['class'][i]

Output

88200 88200
i 0
cur_spectro (0, 496, 64, 1)
data (6000, 496, 64, 1)
(496, 64, 1)
<ipython-input-226-30eefc542ce4> in loading_data(files, df)
      29                 print("data", data.shape)
      30                 print(data[i * seg_num + j, :, :, :].shape)
 ---> 31                 data[i * seg_num + j, :, :, :] = cur_spectro
      32                 label[i * seg_num + j] = df['class'][i]
      33 ValueError: could not broadcast input array from shape (0,496,64,1) into shape (496,64,1)
2
  • What don't you understand? Why an array with a 0 dimension can't be put in a slice of data, or why cur_spectro has that 0 dimension. The problem traces back to the preprocess_sound step. Commented Oct 12, 2020 at 16:39
  • @hpaulj Thanks for the reply. Yes, the problem is with the preprocessing steps. Thanks for pointing out. Commented Oct 12, 2020 at 16:51

1 Answer 1

1

One of your printouts is cur_spectro (0, 496, 64, 1).

See What does a numpy shape starting with zero mean It contains a description how to understand the case when one of array dimensions is zero. It means actually that the array in question is empty.

So it looks like you attempted to copy an empty array into a non-empty array. As in this situation these arrays can not be aligned (broadcast), the exception is raised.

Just as you can read in one of comments, analyze why preprocess_sound function returns as empty array.

Maybe you should add to your code a check whether the first dimension of cur_spectro has size zero and in such case you should either skip the offending instruction or copy to the target array some "surrogate" content.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.