Default Code from: How can I make the inception-v3 model pre-trained from Imagenet (classify_image.py) in the Tensorflow tutorial importable as a module?
I use this code to classify my images with tensorflow:
import tensorflow as tf
import os, time
slim = tf.contrib.slim
import PIL as pillow
from PIL import Image
#import Image
from testd import *
import numpy as np
import glob
with open('imagenet_synset_to_human_label_map.txt','r') as inf:
imagenet_classes = eval(inf.read())
def get_human_readable(id):
id = id - 1
label = imagenet_classes[id]
return label
checkpoint_file = 'inception_resnet_v2_2016_08_30.ckpt'
#Load the model
sess = tf.Session()
arg_scope = inception_resnet_v2_arg_scope()
input_tensor = tf.placeholder(tf.float32, [None, 299, 299, 3])
with slim.arg_scope(arg_scope):
logits, end_points = inception_resnet_v2(input_tensor, is_training=False)
saver = tf.train.Saver()
saver.restore(sess, checkpoint_file)
print('oui5')
def classify_image():
classifications = []
while True:
image = glob.glob('*png')
for i in image:
im = Image.open(i).resize((299,299))
im = np.array(im)
im = im.reshape(-1,299,299,3)
im = 2*(im/255.0)-1.0
predict_values, logit_values = sess.run([end_points['Predictions'], logits], feed_dict={input_tensor: im})
#print (np.max(predict_values), np.max(logit_values))
#print (np.argmax(predict_values), np.argmax(logit_values))
label = get_human_readable(np.argmax(predict_values))
predict_value = np.max(predict_values)
classifications.append({"label":label, "predict_value":predict_value})
print (i, label, predict_value)
return classifications
classify_image()
I modified the default code very slightly so that it can host several images. However, I have a problem. Indeed, I find that the more I realize classification, the more incorrect it is. So, the only solution available to me is to stop the script and restart it, so that it shows me a better result ...
After many searches, I do not find a track that can answer my problem. Did you have a solution? :)
Thank you in advance!