Advertisement
Guest User

Untitled

a guest
Mar 24th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. import tensorflow as tf
  2. from tensorflow.python.platform import gfile
  3. import numpy as np
  4.  
  5. from imagenet_classes import class_names
  6. from scipy.misc import imread, imresize
  7.  
  8. dir_name = 'mobilenet_v1_1.0_224'
  9. with tf.Graph().as_default() as graph:
  10. with tf.Session() as sess:
  11. with gfile.FastGFile(dir_name + "/mobilenet_v1_1.0_224_frozen.pb") as f:
  12. file = 'file1.jpg'
  13. input = imread(file, mode='RGB')
  14. input = imresize(input, (224, 224)).reshape(1, 224, 224, 3).astype(float)
  15. input/=127.5
  16. input-=1.
  17.  
  18. graph_def = tf.GraphDef()
  19. graph_def.ParseFromString(f.read())
  20. sess.graph.as_default()
  21.  
  22. tf.import_graph_def(graph_def, input_map=None, return_elements=None,
  23. name="", op_dict=None, producer_op_list=None)
  24. for op in graph.get_operations():
  25. print("Operation Name :" + op.name)
  26. print("Tensor Stats :" + str(op.values()))
  27. l_input = graph.get_tensor_by_name('input:0')
  28. intermediate = graph.get_tensor_by_name('MobilenetV1/MobilenetV1/Conv2d_0/Relu6:0')
  29. l_output = graph.get_tensor_by_name('MobilenetV1/Predictions/Reshape_1:0')
  30. tf.global_variables_initializer()
  31. inter_out = sess.run(intermediate, feed_dict = {l_input : input})
  32. print(inter_out)
  33. op_prob = sess.run(l_output, feed_dict = {l_input : input})
  34. preds = (np.argsort(op_prob[0])[::-1])[0:5]
  35. for p in preds:
  36. print(class_names[p-1], op_prob[0][p])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement