python - 构建图像分类的初始架构。喀拉斯。值错误

标签 python machine-learning keras neural-network conv-neural-network

我正在尝试构建用于图像分类的 googleNet Inception 架构。 我已经读取并保存了下面给出的图像数据和标签。

print(X_train.shape)
(16016, 224, 224, 3)
print(X_test.shape)
(16016, 1, 163)
print(y_train.shape)
(14939, 224, 224, 3)
print(y_test.shape)
(14939, 1, 163)

利用这些数据,我正在尝试训练我的分类器。我的代码如下。

IMG_SIZE = 224
input_image = Input(shape = (IMG_SIZE,IMG_SIZE,3))

tower_1 = Conv2D(64,(1,1),padding='same', activation='relu') (input_image)
tower_1 = Conv2D(64,(3,3), padding='same',activation='relu') (tower_1)

tower_2 = Conv2D(64,(1,1), padding='same',activation='relu')(input_image)
tower_2 = Conv2D(64,(5,5), padding='same', activation='relu')(tower_2)

tower_3 = MaxPooling2D((3,3),strides=(1,1),padding='same')(input_image)
tower_3 = Conv2D(64,(1,1), padding='same',activation='relu')(tower_3)

output = keras.layers.concatenate([tower_1,tower_2,tower_3],axis=3)
output = Flatten()(output)
out = Dense(163, activation='softmax')(output)

model = Model(inputs = input_image, outputs = out)
print(model.summary())

epochs = 30
lrate = 0.01
decay = lrate/epochs

sgd = SGD(lr=lrate, momentum=0.9, decay=decay, nesterov= False)
model.compile(loss='categorical_crossentropy',optimizer=sgd, metrics=['accuracy'])

history = model.fit(X_train,y_train,validation_data=(X_test,y_test), epochs=epochs, batch_size=32)

from keras.models import model_from_json

model_json = model.to_json()
with open("model.json", "w") as json_file:
    json_file.write(model_json)

model.save_weights(os.path.join(os.getcwd(),'model.h5'))

scores = model.evaluate(X_test,y_test, verbose=0)
print("Accuracy: %.2f%%" % (scores[1]*100))

但是每次我运行程序时,它都会给出值错误,这对我来说是无法理解的。我已经尝试过 'y_test= y_test.reshape(14939,IMG_SIZE,IMG_SIZE,3)' 但它仍然给我同样的错误。

错误

Traceback (most recent call last):
  File "c:/Users/zeele/OneDrive/Desktop/googleNet_Architecture.py", line 149, in <module>
    history = model.fit(X_train,y_train,validation_data=(X_test,y_test), epochs=epochs, batch_size=32)
  File "C:\Users\zeele\Miniconda3\lib\site-packages\keras\engine\training.py", line 1405, in fit
    batch_size=batch_size)
  File "C:\Users\zeele\Miniconda3\lib\site-packages\keras\engine\training.py", line 1299, in _standardize_user_data
    exception_prefix='model target')
  File "C:\Users\zeele\Miniconda3\lib\site-packages\keras\engine\training.py", line 121, in _standardize_input_data
    str(array.shape))
ValueError: Error when checking model target: expected dense_1 to have 2 dimensions, but got array with shape (14939, 224, 224, 3)

请帮我解决这个问题。

谢谢。

最佳答案

可以肯定的是,您的数据形状不正确/一致;自从

print(X_train.shape)
(16016, 224, 224, 3)

人们肯定会期望 X_test.shape质量上相似,仅在样本数量上有所不同,即形式为 (NUM_TEST_SAMPLES, 224, 224, 3); ;但你报告的是:

print(X_test.shape)
(16016, 1, 163)

它看起来更像标签的预期形状(即y_train.shape)。

另请注意,训练集和测试集的数据和标签的长度必须相同,但这里的情况并非如此:对于训练集和测试集,您报告了 16,016 个样本数据和只有 14,939 个标签。

我的猜测是,您在使用 scikit-learn 的 train_test_split 将数据拆分为训练集和测试集时很可能犯了一个(足够频繁的)错误(请参阅 docs ):

# WRONG ORDER:
X_train, y_train, X_test, y_test = train_test_split(X, y, test_size=0.33, random_state=42)

# CORRECT ORDER:
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42)

关于python - 构建图像分类的初始架构。喀拉斯。值错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54120757/

相关文章:

jquery - 使用 Ajax 和 jQuery 的 Django 表单!无法弄清楚如何在 URL 中将控制从 Ajax 传递到 Python

python - 在 Django 中将固定装置加载到数据库后如何延迟评估 ORM 调用?

r - 何时在 R 中的插入符包中使用 train() 的索引和种子参数

python - 当新数据到来时,如何重新训练 pyspark 中保存的线性回归 ML 模型

python-3.x - 如何使用 CLI 测试我自己的手写数字或 MNIST 数据集中的数据之一

python - 用于文本分类的预训练模型

python - 如何在 Microsoft Windows 上安装 Python 包 pyrouge?

python - session 未提交 - checkout 12b - Flask Web 开发

machine-learning - 我可以通过对类进行编码将分类问题转化为回归问题吗?

python - 如何 pickle Keras 自定义层?