'Running a fine-tune model for my CNN : Value Error
So I am trying to use a pre-trained model on my data set to then compare it to my own cnn model. However, I see an error as soon as I try to do model. fit so much that ((None, 4, 4, 1) vs (None,)). Where is this error coming from? Am I supposed to edit the pre-tune cnn.
The model that I am using is ResNET50 with no modification except the input layer changed to 128 and there are 2 outputs.
Any help is welcome,
CODE:
history = modelB.fit_generator(train_data,
validation_data = test_data,
epochs=5,
steps_per_epoch = 1714,)
ERROR:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-89-89a7f1c1eb60> in <module>()
2 validation_data = test_data,
3 epochs=5,
----> 4 steps_per_epoch = 1714,)
2 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/func_graph.py in autograph_handler(*args, **kwargs)
1145 except Exception as e: # pylint:disable=broad-except
1146 if hasattr(e, "ag_error_metadata"):
-> 1147 raise e.ag_error_metadata.to_exception(e)
1148 else:
1149 raise
ValueError: in user code:
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1021, in train_function *
return step_function(self, iterator)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1010, in step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1000, in run_step **
outputs = model.train_step(data)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 860, in train_step
loss = self.compute_loss(x, y, y_pred, sample_weight)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 919, in compute_loss
y, y_pred, sample_weight, regularization_losses=self.losses)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/compile_utils.py", line 201, in __call__
loss_value = loss_obj(y_t, y_p, sample_weight=sw)
File "/usr/local/lib/python3.7/dist-packages/keras/losses.py", line 141, in __call__
losses = call_fn(y_true, y_pred)
File "/usr/local/lib/python3.7/dist-packages/keras/losses.py", line 245, in call **
return ag_fn(y_true, y_pred, **self._fn_kwargs)
File "/usr/local/lib/python3.7/dist-packages/keras/losses.py", line 1932, in binary_crossentropy
backend.binary_crossentropy(y_true, y_pred, from_logits=from_logits),
File "/usr/local/lib/python3.7/dist-packages/keras/backend.py", line 5247, in binary_crossentropy
return tf.nn.sigmoid_cross_entropy_with_logits(labels=target, logits=output)
ValueError: `logits` and `labels` must have the same shape, received ((None, 4, 4, 1) vs (None,)).
Solution 1:[1]
The Issue is with the loss function used when you compile the model.
Replace the compile with below code:
model.compile(optimizer='adam',loss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True), metrics=['accuracy'])
Solution 2:[2]
Use tf.keras.utils.plot_model
to print out a graphic representation of the model. you have a mismatch between the number of input and output nodes.
Solution 3:[3]
Replace the loss function in compile with SparseCategoricalCrossentropy
then you can rectify the error.
model.compile(optimizer='adam',loss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True), metrics=['accuracy'])
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | user17651088 |
Solution 2 | Todd |
Solution 3 | Suraj Rao |