python - ValueError ResNet Keras

标签 python image tensorflow machine-learning keras

我尝试使用 Keras ResNet 50 应用程序模型来解决此代码的问题:

#Tensorflow and tf.keras
import tensorflow as tf
from tensorflow import keras
#tf.enable_eager_execution()

#Helper libraries
import numpy as np
import matplotlib.pyplot as plt

import Muenzbetragserkennung_input_ResNet

#print(tf.__version__)

#Dataset
#Training and test data
(train_images, train_labels), (test_images, test_labels) = 
Muenzbetragserkennung_input_ResNet.read_input_shuffle()

batch_size, height, width, channels = train_images.shape
train_images = train_images / 255.0
test_images = test_images / 255.0
print(train_images.shape)

#Build the model
model = tf.keras.applications.resnet50.ResNet50(include_top=False, 
weights=None, input_tensor=None, input_shape=(height, width, channels), 
pooling='max')

model.compile(optimizer=tf.train.AdamOptimizer(),
          loss='mean_squared_error',
          metrics=['accuracy'])

#model.summary()

#Train
model.fit(train_images, train_labels, epochs=10)
#model.save_weights('models/muenzen.h5')

#Evaluate
loss, accuracy = model.evaluate(test_images, test_labels)
print('Accuracy', accuracy)

#Prediction
prediction = model.predict(test_images[0:1])
print(prediction)

但得到以下输出/错误:

Shape train images: (3865, 240, 320, 3)

Shape train labels: (3865,)

Shape test images: (967, 240, 320, 3)

Shape test labels: (967,)

(3865, 240, 320, 3)

Traceback (most recent call last):
File"C:/Users/Christian/PycharmProjects/MuenzbetragserkennungResNet/Muenzbetragserkennung_ResNet.py", line 34, in model.fit(train_images, train_labels, epochs=10)

File "C:\Users\Christian\AppData\Roaming\Python\Python36\site-packages\tensorflow\python\keras\engine\training.py", line 1278, in fit validation_split=validation_split)

File "C:\Users\Christian\AppData\Roaming\Python\Python36\site-packages\tensorflow\python\keras\engine\training.py", line 917, in _standardize_user_data exception_prefix='target')

File "C:\Users\Christian\AppData\Roaming\Python\Python36\site-packages\tensorflow\python\keras\engine\training_utils.py", line 191, in standardize_input_data ' but got array with shape ' + str(data_shape))

ValueError: Error when checking target: expected global_max_pooling2d to have shape (2048,) but got array with shape (1,)

Process finished with exit code 1

我已经尝试了不同的池版本,但只得到了其他 ValueErrors。 该模型应输出一个值(图像中硬币的值(value))。

预先感谢您的帮助。

最佳答案

问题在于您的标签是一维的,但模型的输出是 2048 维向量。这是很自然的,因为您没有添加任何层来产生正确的输出。这可以这样做:

resnet_model = tf.keras.applications.resnet50.ResNet50(include_top=False, 
weights=None, input_tensor=None, input_shape=(height, width, channels), 
pooling='max')

x = Dense(128, activation='relu')(resnet_model.output)
x = Dense(1, activation='relu')(x)

model = Model(resnet_model.input, x)

请注意,最后一个密集层输出一个标量,该标量现在与您的目标兼容。

关于python - ValueError ResNet Keras,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52136423/

相关文章:

html - 如何将背景图像放在div内的img前面

python - 属性错误 : module 'keras.backend' has no attribute 'image_dim_ordering'

python - 使用模拟测试构造函数

python - 如何从 .yaml 文件访问变量到机器人框架脚本?

python - 类型错误:+ 不支持的操作数类型: 'int' 和 'NoneType'

python - lightGBM 中的分类特征是如何编码的?

html - <img> 元素是 block 级还是行内级?

javascript - 在鼠标轨迹中显示图像

python - ValueError : Input 0 of layer conv2d_10 is incompatible with the layer: expected ndim=4, 发现 ndim=3。收到完整形状 : [None, 100, 100]

machine-learning - 深度学习 - 如何为大型分类集准备训练数据?