When feeding an image to a pretrained InceptionResNetV2 network, I have the following results.
from keras.applications.inception_resnet_v2 import InceptionResNetV2
INPUT_SHAPE = (200, 250, 3)
img = load_img() # loads a 200x250 rgb image into a (200, 250, 3) numpy array
assert img.shape == INPUT_SHAPE # just fine
model = InceptionResNetV2(include_top=False, input_shape=INPUT_SHAPE)
model.predict(img)
ValueError: Error when checking input: expected input_1 to have 4 dimensions, but got array with shape (200, 150, 3)
I don't understand why and how the model expects a 4 dimension input. What must be done to adapt the (200, 250, 3) image so that it can be processed by the model?
try reshape your input with shapes (1, 200, 150, 3) or (200, 150, 3, 1).
You can use image = np.expand_dims(image, axis=0)) or
image = input_data.reshape((-1, image_side1, image_side2, channels))
You need to feed a batch of images. If your batch has one image, it should also have the same format.
try img.reshape((1, 200, 150, 3))
Related
I'm decoding a base64 image with the following code:
def string_to_image(base64_string):
decoded = base64.b64decode(base64_string)
np_data = np.frombuffer(decoded, np.uint8)
img = cv2.imdecode(np_data, cv2.IMREAD_UNCHANGED)
return img
The goal is to receive an image from the request body, decode it, resize it with tensorflow, predict it with a model, and return a response saying what is that image:
image_base64 = request.json['image']
decoded_image = string_to_image(image_base64)
image_resized = tf.image.resize(decoded_image, (256, 256))
model = load_model('src/models/mymodel.h5')
result = model.predict(np.expand_dims(image_resized/255, 0))
However, I'm getting the error ValueError: Input 0 of layer "sequential_2" is incompatible with the layer: expected shape=(None, 256, 256, 3), found shape=(None, 256, 256, 4).
I don't know how to change the Shape value from '4' to '3'.
I tried the following:
image_resized = tf.image.resize(decoded_image, (256, 256, 3))
But I get 'size' must be a 1-D Tensor of 2 elements: new_height, new_width.
I also tried:
image_resized = cv2.resize(decoded_image, (256,256,3))
But I get OpenCV(4.6.0) :-1: error: (-5:Bad argument) in function 'resize'
Overload resolution failed:
- Can't parse 'dsize'. Expected sequence length 2, got 3
- Can't parse 'dsize'. Expected sequence length 2, got 3
Please help :(
You could reshape the array by using tf.squeeze after reshaping the tensor. According to documentation, tf.squeeze will remove axis with dimensions 1.
image_resized = tf.reshape(decoded_image, (-1, 256, 256, 3, 1))
image_resized = tf.squeeze(image_resized)
With vijayachandran mariappan comment and AndreaYolo answer I figured out a solution. First, change the channels of the image and then resize its dimensions:
decoded_image = string_to_image(image_base64)
decoded_image = decoded_image[:,:,:3]
image_resized = tf.image.resize(decoded_image, (256, 256))
My model then was able to predict perfectly!
I want to predict from image url. In the past, I use ImageDatagenerator().flow_from_directory() methods, but now I have only one image. so I want to predict from this single image.
I have tried the below code, but failed. (Dimension error)
url = "http://3.36.149.28/uploads/WEBUPLOADprofile.png"
img = Image.open(requests.get(url, stream=True).raw)
img = img_to_array(img)
img = img/255.
#Predict
pred = model.predict(img)
so I tried reshape & retrying, but failed (cannot reshape array of size 1048576 into shape (28,28,1))
img = img.reshape(-1, 28, 28, 1)
img = img/255.
#Predict
pred = model.predict(img)
for getting reshape & get colored predict image, what can I do ? please help..
Additional : I trained srcnn model, and inputs :
inputs = Input((None, None, 3), dtype='float')
I resolved this problem.
First, my url image shape is (None, None, 4), but my trained shape is (None, None, 3).
So I tried another jpg image (None, None, 3) and expand dimension via np,
and result shape = (1, None, None, 3)
image = np.expand_dims(image, axis=0)
model.predict(image)
from link
and now I get predict image successfully.
I have trained my ResNet101V2 model (keras) and have saved the model. On loading the model and trying to classify a new image, I keep getting the error: ValueError: Input 0 is incompatible with layer model_7: expected shape=(None, 255, 255, 3), found shape=(None, 255, 3)
Here's my code:
load_path = 'path to my model'
model = keras.models.load_model(load_path)
image_path = 'path to my image'
img_np = cv2.imread(image_path, cv2.IMREAD_COLOR)
resized_img_np = cv2.resize(img_np, (255, 255))
print(resized_img_np.shape) # <============= PRINTS (255, 255, 3)
prediction = model.predict(resized_img_np) # <========= ERROR
You need to add an extra dimension to match with batch size. Add a dimension using np.expand_dims to the resized image and pass to model for predictionion.
resized_img_np = np.expand_dims(resized_img_np,axis=0)
prediction = model.predict(resized_img_np)
As the model was trained on batches you have to add a batch value of 1 for a single sample,
the error indicated that the size should be:
(None, 255, 255, 3)
Where the None shows the varying batchsize.
You can simply solve this by adding a "1" as the first dimension of your input image, showing that you are going to classify only one image.
Where the shape instead of (255, 255, 3) would be:
import numpy as np
resized_img_np = cv2.resize(np.array(img_np), (255, 255))
resized_img_np = np.expand_dims(resized_img_np, axis=0)
I've just started to learn Python. I'm using Numpy, and this is on of the things that I don't understand.
I have a Numpy array with Shape (960, 200, 200, 1). I use it to store images of size 200x200x1 (200 height x 200 width x 1 channel). So, I think it means that I have 960 images.
Now, I have to get one image to test my CNN network, to do it, I did it:
D = ... # My Dataset with shape (960, 2, 200, 200, 1)
features = D[:,0,:] # Shape (960, 200, 200, 1)
labels = D[:,1,:] # Shape (960, 200, 200, 1)
print("D shape: ", D.shape)
print("Features shape: ", features.shape)
print("Labels shape: ", labels.shape)
print(features[0, :].shape) # Shape (200, 200, 1)
print(labels[0,:].shape) # Shape (200, 200, 1)
This code outputs:
D shape: (960, 2, 200, 200, 1)
Features shape: (960, 200, 200, 1)
Labels shape: (960, 200, 200, 1)
(200, 200, 1)
(200, 200, 1)
I want to get one image from features and one from labels. I have tried these two commands, getting and image with the same shape (200, 200, 1):
features[0, :]
features[0]
But I need to get an image with shape (1, 200, 200, 1).
How can I do it to get an image with that shape?
Maybe creating a 4 dimensions Numpy Array, and adding the image.
A = np.random.random((200,200,3))
A.shape
gives
(200, 200, 3)
You can use
B = np.expand_dims(A,0)
and then
B.shape
(1, 200, 200, 3)
my model input shape is (50,50,1)
I am getting the frame by:
cv2.VideoCapture(0).read()
When I'm using np.reshape() function it does not reshape it the desired shape.
sized_frame = (cv2.resize(frame, (50,50)))
cv2.waitKey(0)
img_data = np.array(photo)
data = tf.reshape(img_data, (1,50,50,3))
model_out = model.predict([img_data])[0]
print(model_out)
if np.argmax(model_out) == 1:
str_label = 'Dog'
else:
str_label = 'Cat'
return str_label
This is the error I'm getting:
ValueError: Cannot feed value of shape (1, 50, 50, 3) for Tensor 'input/X:0', which has shape '(?, 50, 50, 1)'
The following code should solve your error
gray = cv2.cvtColor(photo, cv2.COLOR_BGR2GRAY)
img_data = np.array(gray)
data = tf.reshape(img_data, (1,50,50,1))
model_out = model.predict(img_data)[0]
Under the assumption that you trained on Grayscale images