[Transfer learning] ALpaca model

Hello !

I am trying to train a classifier that says if there is a flower on the image or not.

We create two datasets using image_dataset_from_directory.

I want to make a separate block of code below in case I want to test on a separate image, neither from train or validation dataset.

In which form should I provide that image in order to feed that to my NN and get the prediction ?

I tried this block of code but getting unexpected output:
img_path = 'flower_testing/cover.jpg'
img = load_img(img_path, target_size=(128, 128))
x = img_to_array(img)
x = np.expand_dims(x, axis=0)
x = x/255.0
print('Input image shape:', x.shape)
plt.imshow(img)
prediction = model2.predict(x)

print("Class prediction vector:", prediction)
print("Class:", np.argmax(prediction))

Getting the following output:

Input image shape: (1, 128, 128, 3)
Class prediction vector: [[7.3119264]]
Class: 0

Any suggestions will be welcomed !!

Thanks!

Based on the assignment, the preprocessing is done inside the model by preprocess_input. There’s no need to rescale image manually to input to the model. x / 255 is required for plt.imgshow.

The prediction is logit. So, convert the logit to probability.

img_path = 'flower_testing/cover.jpg'
img = load_img(img_path, target_size=(160, 160))
img = img_to_array(img)
img = np.expand_dims(img, axis=0)
print('Input image shape:', img.shape)
logit = model2.predict(img)
# Need to convert a logit to probability scale
probabiliy = tf.nn.sigmoid(logit)
# in the assignment, 1 means not alpaca. 
print(tf.where(probability > .5, 1, 0))
1 Like