gpt4 book ai didi

python - ValueError ResNet Keras

转载 作者:行者123 更新时间:2023-11-30 09:05:53 24 4
gpt4 key购买 nike

我尝试使用 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com