Guest User

Cv-tricks.com_example_multi_batch

a guest
Jan 18th, 2018
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.96 KB | None | 0 0
  1. import tensorflow as tf
  2. import numpy as np
  3. import os,glob,cv2
  4. import sys,argparse
  5.  
  6.  
  7. # First, pass the path of the image
  8. dir_path = os.path.dirname(os.path.realpath(__file__))
  9. image_path=sys.argv[1]
  10. filename = dir_path +'/' +image_path
  11. image_size=128
  12. num_channels=3
  13. ims = ["cat.1110.jpg","cat.1111.jpg"]
  14. images=[]
  15. # Reading the image using OpenCV
  16. for filename in ims:
  17. image = cv2.imread(filename)
  18. # Resizing the image to our desired size and preprocessing will be done exactly as done during training
  19. image = cv2.resize(image, (image_size, image_size),0,0, cv2.INTER_LINEAR)
  20. images.append(image)
  21. images = np.array(images, dtype=np.uint8)
  22. images = images.astype('float32')
  23. images = np.multiply(images, 1.0/255.0)
  24. #The input to the network is of shape [None image_size image_size num_channels]. Hence we reshape.
  25. x_batch = images.reshape(2, image_size,image_size,num_channels)
  26.  
  27. ## Let us restore the saved model
  28. sess = tf.Session()
  29. # Step-1: Recreate the network graph. At this step only graph is created.
  30. saver = tf.train.import_meta_graph('./trained_model/dogs-cats-model.meta')
  31. # Step-2: Now let's load the weights saved using the restore method.
  32. saver.restore(sess, tf.train.latest_checkpoint('./trained_model/'))
  33.  
  34. # Accessing the default graph which we have restored
  35. graph = tf.get_default_graph()
  36.  
  37. # Now, let's get hold of the op that we can be processed to get the output.
  38. # In the original network y_pred is the tensor that is the prediction of the network
  39. y_pred = graph.get_tensor_by_name("y_pred:0")
  40.  
  41. ## Let's feed the images to the input placeholders
  42. x= graph.get_tensor_by_name("x:0")
  43. y_true = graph.get_tensor_by_name("y_true:0")
  44. y_test_images = np.zeros((1, 2))
  45.  
  46.  
  47. ### Creating the feed_dict that is required to be fed to calculate y_pred
  48. feed_dict_testing = {x: x_batch, y_true: y_test_images}
  49. result=sess.run(y_pred, feed_dict=feed_dict_testing)
  50. # result is of this format [probabiliy_of_rose probability_of_sunflower]
  51. print(result)
Add Comment
Please, Sign In to add comment