--------------------------------------------------------------------------- ValueError…
- User
ValueError Traceback (most recent call last) Cell In[7], line 5 3 iEM.checkTypeMLP( iEM.getImageElement('zeichen',2) ) 4 print('Keras Test Infos') ----> 5 iEM.checkTypeKeras( iEM.getImageElement('zeichen',1) ) 6 iEM.checkTypeKeras( iEM.getImageElement('zeichen',2) )
Cell In[4], line 385, in ImageElementManager.checkTypeKeras(self, imageElement) 383 failures = 0 384 for image in images: --> 385 predictedId = iEM.predictKeras(image) 386 if isinstance(predictedId, (collections.abc.Sequence, np.ndarray)) and len(predictedId) == 1: 387 predictedId = predictedId[0]
Cell In[4], line 303, in ImageElementManager.predictKeras(self, image, verbose) 300 print('Es kann keine Vorhersage getroffen werden, da Keras nicht init ist!') 301 return None --> 303 predictedVirtualId = np.argmax(self.keras.predict( image.astype('float32') ), axis=1) 305 predictedElement = iEM.getImageElementFromVirtualId( predictedVirtualId ) 306 if verbose:
File ~\AppData\Roaming\Python\Python311\site-packages\keras\src\utils\traceback_utils.py:122, in filter_traceback..error_handler(*args, **kwargs) 119 filtered_tb = _process_traceback_frames(e.traceback) 120 # To get the full stack trace, call: 121 #
keras.config.disable_traceback_filtering()--> 122 raise e.with_traceback(filtered_tb) from None 123 finally: 124 del filtered_tbFile ~\AppData\Roaming\Python\Python311\site-packages\keras\src\models\functional.py:280, in Functional._adjust_input_rank(self, flat_inputs) 278 adjusted.append(ops.expand_dims(x, axis=-1)) 279 continue --> 280 raise ValueError( 281 f"Invalid input shape for input {x}. Expected shape " 282 f"{ref_shape}, but input has incompatible shape {x.shape}" 283 ) 284 # Add back metadata. 285 for i in range(len(flat_inputs)):
ValueError: Exception encountered when calling Sequential.call().
Invalid input shape for input Tensor("data:0", shape=(28, 28), dtype=float32). Expected shape (None, 28, 28, 1), but input has incompatible shape (28, 28)
Arguments received by Sequential.call(): • inputs=tf.Tensor(shape=(28, 28), dtype=float32) • training=False • mask=None
- Assistant
The error you're encountering indicates a mismatch in the expected input shape of the Keras model compared to the actual shape of the data you provided. Your Keras model expects input data to be in the shape
(None, 28, 28, 1), but the data you are providing is in the shape(28, 28). The expected shape suggests that the model is designed to take images with height and width both equal to 28 pixels and with a single channel (e.g., grayscale images), but your input lacks the channel dimension.To fix this issue, you need to reshape your input data to include the channel dimension. Here is how you can do it:
import numpy as np # Assuming `image` is your input image with shape (28, 28) # You need to reshape it to (1, 28, 28, 1) before feeding it to the model image_reshaped = np.expand_dims(image, axis=0) # This adds the batch dimension: (1, 28, 28) image_reshaped = np.expand_dims(image_reshaped, axis=-1) # This adds the channel dimension: (1, 28, 28, 1) # Now you can provide this `image_reshaped` to your Keras model predictedVirtualId = np.argmax(self.keras.predict(image_reshaped), axis=1) # Continue with your processing... - User
ValueError: Exception encountered when calling Conv2D.call().
Negative dimension size caused by subtracting 3 from 1 for '{{node sequential_1/conv2d_1/convolution}} = Conv2D[T=DT_FLOAT, data_format="NHWC", dilations=[1, 1, 1, 1], explicit_paddings=[], padding="VALID", strides=[1, 1, 1, 1], use_cudnn_on_gpu=true](sequential_1/ExpandDims, sequential_1/conv2d_1/convolution/ReadVariableOp)' with input shapes: [1,784,1,1], [3,3,1,32].
Arguments received by Conv2D.call(): • inputs=tf.Tensor(shape=(1, 784, 1, 1), dtype=float32)
- Assistant
The error message you're seeing,
Negative dimension size caused by subtracting 3 from 1 for '{{node sequential_1/conv2d_1/convolution}} = Conv2D..., indicates that there's an issue with how the data is being fed into theConv2Dlayer of your neural network model. Specifically, it reveals an incorrect input shape being used - in your case.