Feeding image data in tensorflow for transfer learning

后端 未结 2 1276
北荒
北荒 2020-12-02 23:31

I am trying to use tensorflow for transfer learning. I downloaded the pre-trained model inception3 from the tutorial. In the code, for prediction:

predictio         


        
2条回答
  •  抹茶落季
    2020-12-03 00:09

    The following code should handle of both cases.

    import numpy as np
    from PIL import Image
    
    image_file = 'test.jpeg'
    with tf.Session() as sess:
    
        #     softmax_tensor = sess.graph.get_tensor_by_name('final_result:0')
        if image_file.lower().endswith('.jpeg'):
            image_data = tf.gfile.FastGFile(image_file, 'rb').read()
            prediction = sess.run('final_result:0', {'DecodeJpeg/contents:0': image_data})
        elif image_file.lower().endswith('.png'):
            image = Image.open(image_file)
            image_array = np.array(image)[:, :, 0:3]
            prediction = sess.run('final_result:0', {'DecodeJpeg:0': image_array})
    
        prediction = prediction[0]    
        print(prediction)
    

    or shorter version with direct strings:

    image_file = 'test.png' # or 'test.jpeg'
    image_data = tf.gfile.FastGFile(image_file, 'rb').read()
    ph = tf.placeholder(tf.string, shape=[])
    
    with tf.Session() as sess:        
        predictions = sess.run(output_layer_name, {ph: image_data} )
    

提交回复
热议问题