2016-09-28 2 views
0

Ich versuche, diesen CodeTensorflow Inception RESNET v2 Eingang Tensor

import os 
import tensorflow as tf 
from datasets import imagenet 
from nets import inception_resnet_v2 
from preprocessing import inception_preprocessing 

checkpoints_dir = 'model' 

slim = tf.contrib.slim 

batch_size = 3 
image_size = 299 

with tf.Graph().as_default(): 

with slim.arg_scope(inception_resnet_v2.inception_resnet_v2_arg_scope()): 
    logits, _ = inception_resnet_v2.inception_resnet_v2([1, 299, 299, 3], num_classes=1001, is_training=False) 
    probabilities = tf.nn.softmax(logits) 

    init_fn = slim.assign_from_checkpoint_fn(
    os.path.join(checkpoints_dir, 'inception_resnet_v2_2016_08_30.ckpt'), 
    slim.get_model_variables('InceptionResnetV2')) 

    with tf.Session() as sess: 
     init_fn(sess) 

     imgPath = '.../image_3.jpeg' 
     testImage_string = tf.gfile.FastGFile(imgPath, 'rb').read() 
     testImage = tf.image.decode_jpeg(testImage_string, channels=3) 

     np_image, probabilities = sess.run([testImage, probabilities]) 
     probabilities = probabilities[0, 0:] 
     sorted_inds = [i[0] for i in sorted(enumerate(-probabilities), key=lambda x:x[1])] 

     names = imagenet.create_readable_names_for_imagenet_labels() 
     for i in range(15): 
      index = sorted_inds[i] 
      print((probabilities[index], names[index])) 

Aber TF zeigt einen Fehler auszuführen: ValueError: rank of shape must be at least 4 not: 1

Ich glaube, dass Problem ist in Eingangs Tensor [1, 299, 299, 3] Form. Wie man Tensor für 3-Kanal-JPEG-Bild eingeben ???

gibt es auch eine ähnliche Frage (Using pre-trained inception_resnet_v2 with Tensorflow). Ich sah im Code input_tensor - leider gibt es eine Erklärung, was input_tensor ist. Vielleicht frage ich etwas Selbstverständliches, aber ich steckte fest! Vielen Dank im Voraus für einen Rat!

+0

können Sie überprüfen, dass Ihr testImage Vektor ein Array von 4 Dimensionen ist – Julius

+0

ja, testimage ist 4D Tensor. Wenn ich "imgPath, testImage_string und test_image" kurz nach 'mit tf.Graph(). As_default()' schreibe und statt '[1, 299, 299, 3]' write 'test_image' alles gut geht. Meine Absicht ist es, den Input-Tensor von 4D manuell zu setzen, und dann möchte ich das Model auf vielen verschiedenen Bildern testen. TF – Saddam

Antwort

3

Sie müssen Ihr Bild vorverarbeiten. Hier ist ein Code:

import os 
import tensorflow as tf 
from datasets import imagenet 
from nets import inception_resnet_v2 
from preprocessing import inception_preprocessing 

checkpoints_dir = 'model' 

slim = tf.contrib.slim 

batch_size = 3 
image_size = 299 

with tf.Graph().as_default(): 
    with slim.arg_scope(inception_resnet_v2.inception_resnet_v2_arg_scope()): 

     imgPath = '.../cat.jpg' 
     testImage_string = tf.gfile.FastGFile(imgPath, 'rb').read() 
     testImage = tf.image.decode_jpeg(testImage_string, channels=3) 
     processed_image = inception_preprocessing.preprocess_image(testImage, image_size, image_size, is_training=False) 
     processed_images = tf.expand_dims(processed_image, 0) 

     logits, _ = inception_resnet_v2.inception_resnet_v2(processed_images, num_classes=1001, is_training=False) 
     probabilities = tf.nn.softmax(logits) 

     init_fn = slim.assign_from_checkpoint_fn(
     os.path.join(checkpoints_dir, 'inception_resnet_v2_2016_08_30.ckpt'), slim.get_model_variables('InceptionResnetV2')) 

     with tf.Session() as sess: 
      init_fn(sess) 

      np_image, probabilities = sess.run([processed_images, probabilities]) 
      probabilities = probabilities[0, 0:] 
      sorted_inds = [i[0] for i in sorted(enumerate(-probabilities), key=lambda x: x[1])] 

      names = imagenet.create_readable_names_for_imagenet_labels() 
      for i in range(15): 
       index = sorted_inds[i] 
       print((probabilities[index], names[index])) 

Die Antwort lautet:

(0.1131034, 'tiger cat') 
(0.079478227, 'tabby, tabby cat') 
(0.052777905, 'Cardigan, Cardigan Welsh corgi') 
(0.030195976, 'laptop, laptop computer') 
(0.027841948, 'bathtub, bathing tub, bath, tub') 
(0.026694898, 'television, television system') 
(0.024981709, 'carton') 
(0.024039172, 'Egyptian cat') 
(0.018425584, 'tub, vat') 
(0.018221909, 'Pembroke, Pembroke Welsh corgi') 
(0.015066789, 'skunk, polecat, wood pussy') 
(0.01377619, 'screen, CRT screen') 
(0.012509955, 'monitor') 
(0.012224807, 'mouse, computer mouse') 
(0.012188354, 'refrigerator, icebox') 
-1

Sie tf.expand_dims(your_tensor_3channel, axis=0) verwenden könnte es in Batch-Format zu erweitern.

Verwandte Themen