如果

时间:2017-08-01 13:39:47

标签: python tensorflow deep-learning

  

默认代码来自:How can I make the inception-v3 model pre-trained from Imagenet (classify_image.py) in the Tensorflow tutorial importable as a module?

我使用此代码使用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()

我稍微修改了默认代码,以便它可以托管多个图像。 但是,我有一个问题。实际上,我发现越是意识到分类,它就越不正确。 因此,我可以使用的唯一解决方案是停止脚本并重新启动它,以便它向我显示更好的结果......

经过多次搜索后,我找不到可以解答问题的曲目。 你有解决方案吗? :)

提前谢谢!

0 个答案:

没有答案