我正在尝试创建一个稀疏类别的keras模型,但出现以下错误:ValueError:设置具有序列的数组元素。
我在这里浏览了关于同一错误的许多主题,并尝试了提供的一些解决方案,但是都没有运气。
一种解决方案是检查以确保输入数组的大小相同(solution here)。我检查了以下代码:
for i in range(len(pre_x)):
shape_x = pre_x[0].shape
if pre_x[i].shape != shape_x:
print(pre_x[i].shape)
print(i)
pre-x是输入x。不,我没有打印出来,所以这不是错误。
我也曾在声明数组时尝试指定数据类型,例如,当我将x数据中的某些列表转换为数组时,我使用了以下代码:
pre_x = np.array(pre_x, dtype=object)
同样,错误仍然存在,因此这不是错误。另外,我尝试对y变量进行一次热编码,使之仅是分类熵,而不是稀疏的分类熵,但是还是有同样的错误。
这是我的简化代码:
import matplotlib.pyplot as plt
import numpy as np
import scipy.io.wavfile
import librosa
import librosa.display
from scipy import misc
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix
from keras.utils.np_utils import to_categorical
from keras.models import Sequential
from keras.layers import Dense, Dropout, Conv2D, MaxPool2D
from keras.preprocessing.image import ImageDataGenerator
def image_to_numpy_array(img_location):
img = (misc.imread(img_location, flatten=True))/255
img = img.tolist()
for i in range(49,126):
for n in range(len(img[0])):
img[i][n] = [img[i][n]]
img = np.array(img, dtype=object)
return img[50:125]
def genres_to_sectionlist(genres):
for i in range(len(genres)):
fail_count = 0
song_count = 0
print("Currently uploading genre: " + genres[i])
while fail_count < 2:
section_count = 0
Temp_sections = []
#fail_count = 0
while True:
try:
section = [i, image_to_numpy_array('TempImage\\' + genres[i] + str(song_count) + '_' + str(section_count) + '.jpg')]
Temp_sections.append(section)
fail_count = 0
except:
fail_count += 1
break
section_count += 1
if len(Temp_sections) > 1:
All_sections.append(Temp_sections)
print("Song number: %i" % song_count)
song_count += 1
return All_sections
All_sections = []
Temp_sections = []
genres = ["Classic_Doom", "2W_Black_Metal", "Cavern_Death"]
# genres = ["Cavern_Death", "2W_Black_Metal"]
All_sections = genres_to_sectionlist(genres)
for i in range(len(All_sections)):
print(len(All_sections[i]))
for i in range(len(All_sections)):
for j in range(len(All_sections[i])):
All_sections[i][j][1] = np.expand_dims(All_sections[i][j][1], axis=2)
for i in range(len(All_sections)):
full_data = []
Test_sections = All_sections[i]
for n in range(len(All_sections)):
if i != n:
full_data += All_sections[n]
print("Training data length: " + str(len(full_data)))
print("Test data length: " + str(len(Test_sections)))
pre_x = []
pre_y = []
test_x = []
test_y = []
for j in range(len(full_data)):
pre_x.append(full_data[j][1])
pre_y.append(full_data[j][0])
for j in range(len(Test_sections)):
test_x.append(Test_sections[j][1])
test_y.append(Test_sections[j][0])
pre_x = np.array(pre_x, dtype=object)
test_x = np.array(test_x, dtype=object)
model = Sequential()
model.add(Conv2D(32, (3, 3), activation='relu', input_shape=(75, 192, 1)))
model.add(MaxPool2D((2, 2)))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPool2D((2, 2)))
model.add(Conv2D(128, (3, 3), activation='relu'))
model.add(MaxPool2D((2, 2)))
model.add(Conv2D(128, (3, 3), activation='relu'))
model.add(MaxPool2D((2, 2)))
model.add(Flatten())
model.add(Dropout(0.5))
model.add(Dense(512, activation='relu'))
model.add(Dense(1, activation='softmax'))
print("TESTING...")
for i in range(len(pre_y)):
shape_x = pre_y[0].shape
if pre_y[i].shape != shape_x:
print(pre_y[i].shape)
print(i)
datagen = ImageDataGenerator()
model.compile(loss='sparse_categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
hist = model.fit_generator(datagen.flow(pre_x, pre_y, batch_size=8), steps_per_epoch=500, epochs=10, verbose=2, validation_data=(test_x, test_y))
有人怀疑为什么会发生此错误吗?有什么我可以尝试的吗?
编辑:错误输出如下:
Traceback (most recent call last):
File "Analyzer_Keras.py", line 158, in <module>
hist = model.fit_generator(datagen.flow(pre_x, pre_y, batch_size=8), steps_per_epoch=500, epochs=10, verbose=2, validation_data=(test_x, test_y))
File "C:\Users\aibow\AppData\Local\Programs\Python\Python37\lib\site-packages\keras_preprocessing\image\image_data_generator.py", line 430, in flow
subset=subset
File "C:\Users\aibow\AppData\Local\Programs\Python\Python37\lib\site-packages\keras_preprocessing\image\numpy_array_iterator.py", line 112, in __init__
self.x = np.asarray(x, dtype=self.dtype)
File "C:\Users\aibow\AppData\Local\Programs\Python\Python37\lib\site-packages\numpy\core\numeric.py", line 538, in asarray
return array(a, dtype, copy=False, order=order)
ValueError: setting an array element with a sequence.
编辑2:我已经检查了Github上的numpy asarray源代码(发生错误的地方),我唯一能真正推断出的就是它正在尝试从x数据创建数组,这已经是数组,因此它不应引发错误。有什么我要忽略的吗?