np.array creation shape differs from original - python

I'm trying test/predict my model, in tensorflow with keras.
For now, I'm using image from train dataset, I'll change once it's working
So I'm calling predict like this:
print(x[0].shape) # <- (128, 128, 3)
print(np.array(x[0])[0].shape) # <- (128, 3)
model.predict(np.array(x[0]))
But it gives me: layer model: expected shape=(None, 128, 128, 3), found shape=(32, 128, 3)
Should not it work? Why is the shape changing when creating array?

You need to add an extra dimension for batch size. For single image batch size would be 1. You can use np.expand_dims to add the extra dimension.
np.expand_dims(np.array(x[0]), axis=0)

model.predict always works in batches. Therefore you need to provide test data in batches, or in other words "provide data point in rows". If you just want to predict for one single data point, you have to either expand it like vivekpadia said or try this:
model.predict(x)

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.

Keras model is interpreting my input data as one image in 4D not lots of separate images

I have the network model_classifier, which is the RCNN/classifier component of Faster-RCNN in the middle of a training loop.
loss_class = model_classifier.train_on_batch([X_batch_for_classifier, X2_batch], [Y1_batch, Y2_batch])
Producing the error message:
InvalidArgumentError: Input to reshape is a tensor with 1605632 values, but the requested shape has 100352 [Op:Reshape]
The input tensor shape for model_classifier is (None, 300, 300, 3).
The shapes for the two sets of inputs and two sets of labels are as follows:
X2_for_classifier: (16, 300, 300, 3)
X2_batch: (16, 4, 4)
Y1_batch: (16, 4, 4)
Y2_batch: (16, 4, 24)
I am putting in 16 training examples in in the hope that it will treat all 16 as separate, but it is instead treating all as one training example as a 4D image. I get the same problem regardless of whether I input them as tensors or np.arrays.
I have also tried converting the inputs and targets into a tf.data.Dataset format but that just gives the same problem.
Ok I have fixed it now, the problem was with my network not my data entry.
Where you define the ROIPoolingConv class you have to swap this:
final_output = K.reshape(final_output, (1, self.num_rois, self.pool_size, self.pool_size, self.nb_channels))
for this:
final_output = K.reshape(final_output, (-1, self.num_rois, self.pool_size, self.pool_size, self.nb_channels))
Where the -1 creates a "None" dim to the output tensor to allow for a variable batch size.

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

TensorFlow Keras dimension error for input layer

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)

Do I understand batch_size correctly in Keras?

I'm using Keras' built-in inception_resnet_v2 to train a CNN to recognize images. When training the model, I have a numpy array of data as inputs, with input shape (1000, 299, 299, 3),
model.fit(x=X, y=Y, batch_size=16, ...) # Output shape `Y` is (1000, 6), for 6 classes
At first, When trying to predict, I passed in a single image of shape (299, 299, 3), but got the error
ValueError: Error when checking input: expected input_1 to have 4 dimensions, but got array with shape (299, 299, 3)
I reshaped my input with:
x = np.reshape(x, ((1, 299, 299, 3)))
Now, when I predict,
y = model.predict(x, batch_size=1, verbose=0)
I don't get an error.
I want to make sure I understand batch_size correctly in both training and predicting. My assumptions are:
1) With model.fit, Keras takes batch_size elements from the input array (in this case, it works through my 1000 examples 16 samples at a time)
2) With model.predict, I should reshape my input to be a single 3D array, and I should explicitly set batch_size to 1.
Are these correct assumptions?
Also, would it be better (possible even) to provide training data to the model so that this sort of reshape before prediction was not necessary? Thank you for helping me learn this.
No, you got the idea wrong. batch_size specifies how many data examples are "forwarded" through the network at once (using GPU usually).
By default, this value is set to 32 inside model.predict method, but you may specify otherwise (as you did with batch_size=1). Because of this default value you got an error:
ValueError: Error when checking input: expected input_1 to have 4
dimensions, but got array with shape (299, 299, 3)
You should not reshape your input this way, rather you would provide it with the correct batch size.
Say, for the default case you would pass an array of shape (32, 299, 299, 3), analogous for different batch_size, e.g. with batch_size=64 this function requires you to pass an input of shape (64, 299, 299, 3.
EDIT:
It seems you need to reshape your single sample into a batch. I would advise you to use np.expand_dims for improved readability and portability of your code, like this:
y = model.predict(np.expand_dims(x, axis=0), batch_size=1)

Categories