Advertisement
Guest User

Untitled

a guest
Jul 26th, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.84 KB | None | 0 0
  1. def oneFile(filepath):
  2. imgPath = filepath
  3. testImage_string = tf.gfile.FastGFile(imgPath, 'rb').read()
  4. testImage = tf.image.decode_jpeg(testImage_string, channels=3)
  5. processed_image = inception_preprocessing.preprocess_image(testImage, image_size, image_size, is_training=False)
  6. processed_images = tf.expand_dims(processed_image, 0)
  7.  
  8.  
  9. # Create the model, use the default arg scope to configure the batch norm parameters.
  10. with slim.arg_scope(inception_resnet_v2_arg_scope()):
  11. #logits, end_points = inception_resnet_v2(images, num_classes = dataset.num_classes, is_training = False)
  12. logits, _ = inception_resnet_v2(processed_images, num_classes=16, is_training=False)
  13.  
  14. probabilities = tf.nn.softmax(logits)
  15.  
  16. init_fn = slim.assign_from_checkpoint_fn(
  17. checkpoint_file,
  18. slim.get_model_variables(model_name))
  19.  
  20.  
  21. with tf.Session() as sess:
  22. init_fn(sess)
  23.  
  24. np_image, probabilities = sess.run([processed_images, probabilities])
  25. probabilities = probabilities[0, 0:]
  26. sorted_inds = [i[0] for i in sorted(enumerate(-probabilities), key=lambda x: x[1])]
  27. #print(probabilities)
  28. print(probabilities.argmax(axis=0))
  29. #names = imagenet.create_readable_names_for_imagenet_labels()
  30. #for i in range(15):
  31. # index = sorted_inds[i]
  32. # print((probabilities[index], names[index]))
  33.  
  34. def main():
  35. for image_file in os.listdir(dataset_dir):
  36. try:
  37. image_type = imghdr.what(os.path.join(dataset_dir, image_file))
  38. if not image_type:
  39. continue
  40. except IsADirectoryError:
  41. continue
  42.  
  43. #image = Image.open(os.path.join(dataset_dir, image_file))
  44. filepath = os.path.join(dataset_dir, image_file)
  45.  
  46. oneFile(filepath)
  47.  
  48. def inception_resnet_v2_arg_scope(weight_decay=0.00004,
  49. batch_norm_decay=0.9997,
  50. batch_norm_epsilon=0.001):
  51. """Yields the scope with the default parameters for inception_resnet_v2.
  52.  
  53. Args:
  54. weight_decay: the weight decay for weights variables.
  55. batch_norm_decay: decay for the moving average of batch_norm momentums.
  56. batch_norm_epsilon: small float added to variance to avoid dividing by zero.
  57.  
  58. Returns:
  59. a arg_scope with the parameters needed for inception_resnet_v2.
  60. """
  61. # Set weight_decay for weights in conv2d and fully_connected layers.
  62. with slim.arg_scope([slim.conv2d, slim.fully_connected],
  63. weights_regularizer=slim.l2_regularizer(weight_decay),
  64. biases_regularizer=slim.l2_regularizer(weight_decay)):
  65.  
  66. batch_norm_params = {
  67. 'decay': batch_norm_decay,
  68. 'epsilon': batch_norm_epsilon,
  69. }
  70. # Set activation_fn and parameters for batch_norm.
  71. with slim.arg_scope([slim.conv2d], activation_fn=tf.nn.relu,
  72. normalizer_fn=slim.batch_norm,
  73. normalizer_params=batch_norm_params) as scope:
  74.  
  75. return scope
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement