简体   繁体   中英

Tensorflow: use pretrained inception model

I would like to use one of these pretrained tensorflow models: https://github.com/tensorflow/models/tree/master/slim

After loading the inceptionv4 model, I've had problems with some test predictions. There is a similar question: Using pre-trained inception_resnet_v2 with Tensorflow

In that question, the solution was to fix the image preprocessing. I tried using ranges for the color channels from 0 to 1 and from -1 to 1.

Here is my code (I've imported everything from the inceptionv4 source file):

checkpoint_file = '..\checkpoints\inception_resnet_v2_2016_08_30.ckpt'
sample_images = ['horse.jpg', 'hound.jpg']
sess = tf.Session()

im_size = 299
inception_v4.default_image_size = im_size

arg_scope = inception_utils.inception_arg_scope()
inputs = tf.placeholder(tf.float32, (None, im_size, im_size, 3))

with slim.arg_scope(arg_scope):
    net, logits, end_points = inception_v4(inputs)

saver = tf.train.Saver()

saver.restore(sess,'..\checkpoints\inception_v4.ckpt')

for image in sample_images:
    im = Image.open(image)
    im = im.resize((299, 299))
    im = np.array(im)
    im = im.reshape(-1, 299, 299, 3)
    im = 2. * (im / 255.) - 1.
    logit_values = sess.run(logits, feed_dict={inputs: im})
    print(np.max(logit_values))
    print(np.argmax(logit_values))

In the code, I'm testing the network with a horse. This is the picture. 在此处输入图片说明

With the current preprocessing, color channels from -1 to 1, the network thinks that this horse is a bathing cap. For scaling from 0 to 1, it becomes a bittern, apparently a small bird. I've used this table to find out the predicted classes: https://gist.github.com/aaronpolhamus/964a4411c0906315deb9f4a3723aac57

I've also checked more than one image. The network is consistently off.

What is going wrong ?

I think you used the wrong synsets for Imagenet. To be specific, the one you used is 2012 version. You may try these two: imagenet_lsvrc_2015_synsets.txt and imagenet_metadata .

For example, if your output is 340, then 340->n02389026-> sorrel

I would agree for the wrong synset, it can be automatically downloaded with the imagenet file, that way your are sure to have the correct one:

from datasets import imagenet
names = imagenet.create_readable_names_for_imagenet_labels()

print(names[0])

when you import the inceptions,

net, logits, end_points = inception_v4(inputs)

should be

logits, end_points = inception_v4(inputs, is_training=False, dropout_keep_prob=1.0) 

for inference

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