简体   繁体   中英

Tensorflow pretrained CNN: predicts the same class of the image

I am using MobilenetV2 pretrained model for classifying plant images having four classes (fresh plant, diseased plant, fresh leaves, diseased leaves).

I have tried using the following set of codes for augmentation and flow from directory method, I made sure I disabled shuffle = False for val_generator.

    train_datagen = ImageDataGenerator(
        rescale=1./255,
        shear_range=0.2,
        zoom_range=0.2,
        horizontal_flip=True)

#Rescaling or normalising the pixels in the image
test_datagen = ImageDataGenerator(
        rescale=1./255)

val_datagen = ImageDataGenerator(
        rescale=1./255)

#Using the flow from directory method to read the images directly from the google drive
train_generator = train_datagen.flow_from_directory(
    directory='/content/drive/My Drive/data/train',
    target_size=(224, 224),
    batch_size=32,
    class_mode="categorical"
)

val_generator = val_datagen.flow_from_directory(
    directory='/content/drive/My Drive/data/val',
    target_size=(224, 224),
    batch_size=32,
    class_mode="categorical",shuffle = False
)
test_generator = test_datagen.flow_from_directory(
    directory='/content/drive/My Drive/data/test',
    target_size=(224, 224),
    batch_size=32,
    class_mode="categorical", shuffle = False
)

The issue arises when I try predicting the class for a random image from the test data,

img = image.load_img('/content/drive/My Drive/data/test/diseased cotton leaf/dis_leaf (248).jpg',target_size=(224,224))
img = image.img_to_array(img)/255

img.shape #(224, 224, 3)
x=np.expand_dims(img,axis=0)
img_data=preprocess_input(x)
img_data.shape#(1, 224, 224, 3)
pred = model1.predict_generator(img_data)
pred #array([[0.28619877, 0.00713898, 0.3015607 , 0.40510157]], dtype=float32)
np.argmax(pred,axis=1)

it returns the same class for different image paths. How can I resolve this issue?

Your pre process might do the /255 (very likely). Try removing it or feeding it unmodified image.

you need to use the MobileNet preprocess image function in your train, validation and test generators. This function scales the pixel value to be between -1 and +1. Remove the rescale parameter in the generators. See code below for train generator

train_gen=ImageDataGenerator(preprocessing_function=keras.applications.mobilenet.preprocess_input, etc.......

if you input an image to predict makes sure you preprocess in the same way using the function. If you don't want to use the mobilenet preprocess function you can write your own which does the same thing. see code below

def pre_processor (img):
    img=img/127.5-1
    return img
train_gen=ImageDataGenerator(preprocessing_function=pre_processor etc......

Use this same function on images you read in before you do predictions

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM