TensorFlow Keras dimension error for input layer - python

I've searched through all the solutions related to this, and I still can't figure out how to shape my training data so Tensorflow accepts it.
My training data is a numpy array of shape (21005, 48, 48), where the 21005 is number of elements and the 48,48 is a 48x48 grayscale image.
model.add(tf.keras.layers.Conv2D(64, kernel_size=3,activation='relu',input_shape=(48,48,1)))
model.add(tf.keras.layers.Conv2D(32, kernel_size=3,activation='relu'))
model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(7, activation='softmax'))
model.compile(optimizer='adam',
loss = 'sparse_categorical_crossentropy',
metrics=['accuracy'])
model.fit(image_train, emotion_train,batch_size=BATCH_SIZE,epochs=EPOCHS, verbose=1)
When I run the fit function, however, it returns an error stating:
ValueError: Error when checking input: expected conv2d_input to have 4 dimensions, but got array with shape (21005, 48, 48)
This leads me to think I'm formatting the input data incorrectly, or missing something regarding how Keras and TF actually pass the input image into the input layer. I've tried adding the extra dimension to the input shape to allow for a channel in a 2d Conv layer, as well as reshaping the images themselves to no avail. Any advice?

Reshape your training data to have 4-dimensions before calling model.fit() such as:
image_train = np.reshape(image_train, (21005, 48, 48, 1))
This is needed because the first Conv2D layer expects an image to have an input_shape of (48,48,1)

When you made your preprocessing, you might have read the image in grayscale mode with a library OpenCV/PIL.
When you read them, your library considers a grayscale image of size (48,48), not a (48,48,1), hence the issue that you have.
Solve the issue as soon as possible, not before feeding to your model; in your code, wherever you read those images, before appending to your list/arrays, ensure the right shape of the array is picked. You can see down below an OpenCV example:
image = cv2.imread(filepath, 0)
#Before this np_expand_dims, image has shape (48,48)
image = np.expand_dims(image , axis=2)
#After this step, image has shape (48,48,1)

Related

Dimension value error to load model with keras

I am trying to denoise an image with a pre-trained model I loaded as "model". I am getting an error as a result of the dimensions being different. Here is the code I have:
path_clean = r"clean.png"
clean = load_img(path_clean)
path_noisy = r"noise.png"
noisy = load_img(path_noisy)
model.compile(
optimizer=tf.keras.optimizers.Adam(learning_rate=3e-4),
loss=tf.keras.losses.mean_squared_error,
metrics=[tf.keras.metrics.mean_absolute_error])
history = model.fit(img_to_array(noisy), img_to_array(clean), epochs=50)
Here is the error I get, calling from the "history" line:
ValueError: Exception encountered when calling layer "concatenate" (type Concatenate).
Dimension 1 in both shapes must be equal, but are 113 and 114. Shapes are [?,113,1] and [?,114,2]. for '{{node model/concatenate/concat}} = ConcatV2[N=2, T=DT_FLOAT, Tidx=DT_INT32](model/conv2d_6/Relu, model/up_sampling2d/resize/ResizeNearestNeighbor, model/concatenate/concat/axis)' with input shapes: [?,113,1,128], [?,114,2,128], [] and with computed input tensors: input[2] = <3>.
Call arguments received:
• inputs=['tf.Tensor(shape=(None, 113, 1, 128), dtype=float32)', 'tf.Tensor(shape=(None, 114, 2, 128), dtype=float32)']
What does it mean that one is 113 and one is 114? When I print the shapes of each image using this:
print(img_to_array(clean).shape)
print(img_to_array(noisy).shape)
I get this:
(500, 500, 3)
(500, 500, 3)
So the dimensions should be the same, right? Thanks for your help.
The error has to do with a certain layer within the network not managing to align the inputs that you give it. The number you see are different because the input data undergoes a series of transformations and then it arrives at this layer and everything breaks down.
Try reading the documentation for this pre-trained model to understand what its constraints are - maybe it performs some reshaping magic and expects a certain shape as input.
When you load the model, you should also be able to inspect the graph structure to understand what happens to the input up until this concatenation.
The issue is that your model relies on a certain image input size (e.g., most likely a multiple of 32). So make sure that the input width and height of the images are divisible by 32.

Using U-net in Python with 3-channel input images for image segmentation

I am using unet for image segmentation, using the code outlined herein.
My input images are 256x256x3. while the corresponding segmentation masks are 256x256.
I have changed the size for the input to Unet:
def unet(pretrained_weights = None,input_size = (256,256,3)):
and get a network with a 256x256x1 layer for the output
conv2d_144 (Conv2D) (None, 256, 256, 1) 2 conv2d_143[0][0]
See the full architecture here.
When I try and run using .fit_generator, I get the following error:
ValueError: Error when checking target: expected conv2d_144 to have shape (256, 256, 1) but got array with shape (256, 256, 3)
What can I do to fix this? Please let me know what extra information I can give!
Thank you!
PS: I have three classes in the outputs, could that be the reason?
You'll have to decide if want an RGB or grayscale input for your images:
Either convert your images to grayscale or change the conv layer. Another option would be to flatten the 256x256x3 input to a one dimension and use that as input.
I've actually fixed it by one-hot encoding my segmentation masks and changing the activation function of the last layer to softmax, with a filtersize to match the number of classes!
https://github.com/MKeel1ng/MULTI-CHANNEL-UNET

Predicting a single PNG image using a trained TensorFlow model

import tensorflow as tf
model = tf.keras.models.Sequential([
tf.keras.layers.Flatten(input_shape = (28,28)),
tf.keras.layers.Dense(128, activation = 'relu'),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(10)
])
This is the code for the model, which I have trained using the mnist dataset. What I want to do is to then pass a 28x28 png image to the predict() method, which is not working. The code for the prediction is:
img = imageio.imread('image_0.png')
prediction = model.predict(img, batch_size = 1)
which produces the error
ValueError: Error when checking input: expected flatten_input to have shape (28, 28) but got array with shape (28, 3)
I have been stuck on this problem for a few days, but I can't find the correct way to pass an image into the predict method. Any help?
Predict function makes predictions over a batch of image. You should include batch dimension (first dimension) to your img, even to predict a single example.
You need something like this:
img = imageio.imread('image_0.png')
img = np.expand_dims(img, axis=0)
prediction = model.predict(img)
As #desertnaut says, seems you are using a RGB image, so your first layer should use input_shape = (28,28,3). Therefore, img parameter of predict function should have (1,28,28,3) shape.
In your case, img parameter of predict function has (28,28,3) shape, thus predict function took the first dimension as number of images, and could not match the other two dimensions to the input_shape of the first layer.

Shape mismatch, 2D Input & 2D Labels

I want to create a neural network, that -easy speaking- creates an image out of an image (greyscale)
I have successfully created a dataset of 3200 examples of input and output (label) images.
(I know the dataset should be larger but that is not the problem right now)
The input [Xin] has the size (3200, 50, 30), since it is 50*30 pixels
The output [yout] has the size of (3200, 30, 20) since it is 30*20 pixels
I want to try out a fully connected network (later on a CNN)
The built of the fully connected model looks like that:
# 5 Create Model
model = tf.keras.models.Sequential()
model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(256, activation=tf.nn.relu))
model.add(tf.keras.layers.Dense(30*20, activation=tf.nn.relu))
#compile the model
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
# 6 Train the model
model.fit(Xin, yout, epochs=1) #train the model
After that I get the following error:
ValueError: Shape mismatch: The shape of labels (received (19200,)) should equal the shape of logits except for the last dimension (received (32, 600)).
I already tried to flatten yout:
youtflat = yout.transpose(1,0,2).reshape(-1,yout.shape[1]*yout.shape[2])
but this resulted in the same error
It appears you're flattening your labels (yout) completely, i.e., you're losing batch dimension. If your original yout has a shape of (3200, 30, 20) you should reshape it to have a shape of (3200, 30*20) which equals (3200, 600):
yout = numpy.reshape((3200, 600))
Then it should work
NOTE
The suggested fix however only removes the error. I see many problems with your method though. For the task you're trying to perform (getting an image as output), you cannot use sparse_categorical_crossentropy as loss and accuracy as metrics. You should use 'mse' or 'mae' instead.

How to apply a previously unseen image to a previously saved model?

I wanted to know how to apply a previously unseen image to a previously saved CNN model and see how it classifies it ?
Code (My attempt)
from keras.models import load_model
from keras.preprocessing import image
import numpy as np
img_path = '/Users/eoind/code/1.jpg'
model = load_model('food.h5')
model.summary()
img = image.load_img(img_path, target_size=(100,100))
image = image.img_to_array(img)
image = np.expand_dims(image, axis=0)
print(image.shape)
images = np.vstack([image])
print("classifying images..")
image_class = model.predict_classes(images)
print(image_class)
iPython Console Error
ValueError: Error when checking : expected dense_1_input to have 2 dimensions, but got array with shape (1, 100, 100, 3)
The error says that the input shape of your network does not match the shape of your image. It seems like the first layer of your network is a dense layer, which is a fully-connected layer and it expects the input to be of shape (batch_size, num_of_neurons_in_the_bottom), but your are giving it an image with shape (batch_size, height, width, channels). Here is a checklist for troubleshooting your problem:
Has the model at least loaded? If the above error happens during loading, then your model is broken (perhaps not properly saved). If not, continue through the checklist...
In programming, always output the values of your variables for debugging! What is the value of model.summary()? Are you sure that the input shape of your network is (100, 100, 3)? Is the first layer a convolutional one?
If the first layer is dense (which is fully-connected), check the training code on how you feed images to the model - maybe your image should be re-shaped or somehow pre-processed?

Categories