How to feed Cifar10 trained model with my own image and get label as output?

后端 未结 2 1338
被撕碎了的回忆
被撕碎了的回忆 2021-01-13 21:44

I am trying to use the trained model based on the Cifar10 tutorial and would like to feed it with an external image 32x32 (jpg or png).
My goal is to be able to get

2条回答
  •  灰色年华
    2021-01-13 22:31

    Some basics first:

    1. First you define your graph: image queue, image preprocessing, inference of the convnet, top-k accuracy
    2. Then you create a tf.Session() and work inside it: starting the queue runners, and calls to sess.run()

    Here is what your code should look like

    # 1. GRAPH CREATION 
    filename_queue = tf.train.string_input_producer(['/home/tensor/.../inputImage.jpg'])
    ...  # NO CREATION of a tf.Session here
    float_image = ...
    images = tf.expand_dims(float_image, 0)  # create a fake batch of images (batch_size=1)
    logits = faultnet.inference(images)
    _, top_k_pred = tf.nn.top_k(logits, k=5)
    
    # 2. TENSORFLOW SESSION
    with tf.Session() as sess:
        sess.run(init_op)
    
        coord = tf.train.Coordinator()
        threads = tf.train.start_queue_runners(coord=coord)
    
        top_indices = sess.run([top_k_pred])
        print ("Predicted ", top_indices[0], " for your input image.")
    

    EDIT:

    As @mrry suggests, if you only need to work on a single image, you can remove the queue runners:

    # 1. GRAPH CREATION
    input_img = tf.image.decode_jpeg(tf.read_file("/home/.../your_image.jpg"), channels=3)
    reshaped_image = tf.image.resize_image_with_crop_or_pad(tf.cast(input_img, width, height), tf.float32)
    float_image = tf.image.per_image_withening(reshaped_image)
    images = tf.expand_dims(float_image, 0)  # create a fake batch of images (batch_size = 1)
    logits = faultnet.inference(images)
    _, top_k_pred = tf.nn.top_k(logits, k=5)
    
    # 2. TENSORFLOW SESSION
    with tf.Session() as sess:
      sess.run(init_op)
    
      top_indices = sess.run([top_k_pred])
      print ("Predicted ", top_indices[0], " for your input image.")
    

提交回复
热议问题