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)
data, or whycur_spectrohas that 0 dimension. The problem traces back to thepreprocess_soundstep.