Image preprocessing in TensorFlow for pre-trained VGG16
We define a function for the preprocessing steps in TensorFlow as follows:
def tf_preprocess(filelist): images=[] for filename in filelist: image_string = tf.read_file(filename) image_decoded = tf.image.decode_jpeg(image_string, channels=3) image_float = tf.cast(image_decoded, tf.float32) resize_fn = tf.image.resize_image_with_crop_or_pad image_resized = resize_fn(image_float, image_height, image_width) means = tf.reshape(tf.constant([123.68, 116.78, 103.94]), [1, 1, 3]) image = image_resized - means images.append(image) images = tf.stack(images) return images
Here, we create the images variable instead of a placeholder:
images=tf_preprocess([x for x in x_test])
We follow the same process as before to define the VGG16 model, restore the variables and then run the predictions:
with slim.arg_scope(vgg.vgg_arg_scope()):...