Output in transfer learning

av1 = GlobalAveragePooling2D()(model.output)
fc1 = Dense(256,activation=‘relu’)(av1)
d1 = Dropout(0.5)(fc1)
fc2 = Dense(4,activation=‘softmax’)(d1)

model_new = Model(inputs=model.input, outputs= fc2)
model_new.summary()

adam = Adam(lr=0.00003)
model_new.compile(loss=‘categorical_crossentropy’, optimizer=adam, metrics=[‘accuracy’])

hist = model_new.fit(X_train,Y_train,
shuffle = True,
batch_size = 16,
epochs = 5,
validation_split=0.20
)

from keras.applications.resnet50 import ResNet50
from keras.preprocessing import image
from keras.applications.resnet50 import preprocess_input, decode_predictions
import numpy as np
import matplotlib.pyplot as plt

img_path = ‘elephant.png’
img = image.load_img(img_path, target_size=(224, 224))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
plt.imshow(x[0])
preds = model_new.predict(x[0])

decode the results into a list of tuples (class, description, probability)

(one such list for each sample in the batch)

print(‘Predicted:’, decode_predictions(preds, top=3)[0])

why i am getting error shown below while predicting?
what should i do for predicting single image?

error:
ValueError: Error when checking input: expected input_1 to have 4 dimensions, but got array with shape (224, 224, 3)

Hey @jhashantanu52, use preds = model_new.predict(x).

Hope this resolved your doubt.
Plz mark the doubt as resolved in my doubts section. :blush:

I tried but that’s not working

Hey @jhaprashant5222, could you share the screenshot of the error.

image
image
image
![image|602x265]
image (upload://mzVUi4ThFnkyR0zThm5humBB70E.png)
image

here the model is always predicting human as output why?

Hey @jhaprashant5222, this is due to the reason our model was not able to classify it properly.

so what should i do?

Hey @jhaprashant5222, use train_test_split on training data, and than check you model’s accuracy using validation data. If its not good you may need to change the model.

thanks
using train_test_split on data along with image augmentation works

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.