1
votes

I was trying to use the Keras ResNet 50 application model on my problem with this code:

#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)

But got the following Ouput/Error:

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

I already tried different pooling versions, but only got other ValueErrors. The model should output one value (worth of coins in the image).

Thank you in advance for your help.

1
Irrelevant to your issue, but you are mixing a regression loss (MSE) with a classification metric (accuracy), which should not be the case.desertnaut
I know, the problem is: The input is an image with, for example, 20 coins. I want to know how much the 20 coins are worth in total. If I get the output of 16.10€ it is closer to the real value (e.g., 16.20€) than 7.50€. Nevertheless, I want to know for how many images the value is correct (e.g., for this image 16.20€).chris

1 Answers

1
votes

The problem is that your labels are one-dimensional, but the output of your model is a 2048-dimensional vector. This is natural as you didn't add any layers to produce the proper output. This can be done as:

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)

Note that the last Dense layer outputs a single scalar, which is now compatible with your targets.