Home > front end >  Different results in tensorflow prediction
Different results in tensorflow prediction

Time:11-08

I cannot understand why the following codes gives different results. I'm printing the first 3 components of the prediction array to compare results. my_features and feat have totally different results, but they should be the same, given the model and the data are the same. There should be something wrong in the loading and image preprocessing, but I cannot find it. Any help will be appreciated.

import tensorflow as tf
import os
import numpy as np
from tensorflow.keras.preprocessing import image
from tensorflow.keras.applications import MobileNetV3Small
from tensorflow.keras.applications.imagenet_utils import preprocess_input

model= MobileNetV3Small(weights='imagenet', include_top=False, pooling='avg')

DatasetPath= "DB"
imagePathList= sorted(os.listdir(DatasetPath))
imagePathList= [os.path.join(DatasetPath, imagePath) for imagePath in imagePathList]

def read_image(filename):
    image_string = tf.io.read_file(filename)
    image = tf.image.decode_jpeg(image_string, channels=3)
    image = tf.image.convert_image_dtype(image, tf.float32)
    image = tf.image.resize(image, [224,224])
    image = tf.keras.applications.mobilenet_v3.preprocess_input(image)
    return image


ds_imagePathList= tf.data.Dataset.from_tensor_slices(imagePathList)
dataset = ds_imagePathList.map(read_image, num_parallel_calls=tf.data.AUTOTUNE)
dataset = dataset.batch(32, drop_remainder=False)
dataset = dataset.prefetch(tf.data.AUTOTUNE)
my_features = model.predict(dataset)

my_features[0][:3]

Second snippet

def loadProcessedImage(path):
    #img = image.load_img(path, target_size=model.input_shape[1:3])
    img = image.load_img(path, target_size= (224,224,3))
    imgP = image.img_to_array(img)
    imgP = np.expand_dims(imgP, axis=0)
    imgP = preprocess_input(imgP)
    return img, imgP

img, x = loadProcessedImage(imagePathList[0])
feat = model.predict(x)

feat = feat.flatten()
feat[:3]

CodePudding user response:

The problem is related to the image resize. In the second snippet there is a call to load_img which internally uses pillow to load and resize the image. The problem is that tf.image.resize is not correct see here, and even this a 2018 blog post, the problem is still there

  • Related