Skip to content

Instantly share code, notes, and snippets.

@HTLife
Last active August 8, 2017 04:59
Show Gist options
  • Save HTLife/0e26f321b8f5711ee048b8e32a9c1303 to your computer and use it in GitHub Desktop.
Save HTLife/0e26f321b8f5711ee048b8e32a9c1303 to your computer and use it in GitHub Desktop.
import numpy as np
import os
import tensorflow as tf
#import urllib2
#from datasets import imagenet
#from nets import inception
#from preprocessing import inception_preprocessing
from PIL import Image, ImageDraw, ImageFont
import imghdr
import numpy as np
import inception_preprocessing
from inception_resnet_v2 import inception_resnet_v2, inception_resnet_v2_arg_scope
#State your log directory where you can retrieve your model
log_dir = './log'
dataset_dir = './data/test/teeth/1'
#Get the latest checkpoint file
checkpoint_file = tf.train.latest_checkpoint(log_dir)
slim = tf.contrib.slim
batch_size = 3
image_size = 299
checkpoints_filename = checkpoint_file#'inception_resnet_v2_2016_08_30.ckpt'
model_name = 'InceptionResnetV2'
sess = tf.InteractiveSession()
graph = tf.Graph()
graph.as_default()
def main():
for image_file in os.listdir(dataset_dir):
try:
image_type = imghdr.what(os.path.join(dataset_dir, image_file))
if not image_type:
continue
except IsADirectoryError:
continue
filepath = os.path.join(dataset_dir, image_file)
print(filepath)
imgPath = filepath#'./data/test/teeth/1/7005.jpg'
testImage_string = tf.gfile.FastGFile(imgPath, 'rb').read()
testImage = tf.image.decode_jpeg(testImage_string, channels=3)
processed_image = inception_preprocessing.preprocess_image(testImage, image_size, image_size, is_training=False)
processed_images = tf.expand_dims(processed_image, 0)
with slim.arg_scope(inception_resnet_v2_arg_scope()):
logits, _ = inception_resnet_v2(processed_images, num_classes=16, is_training=False)
probabilities = tf.nn.softmax(logits)
init_fn = slim.assign_from_checkpoint_fn(
checkpoint_file,
slim.get_model_variables(model_name))
init_fn(sess)
np_image, probabilities = sess.run([processed_images, probabilities])
probabilities = probabilities[0, 0:]
sorted_inds = [i[0] for i in sorted(enumerate(-probabilities), key=lambda x: x[1])]
#print(probabilities)
print(probabilities.argmax(axis=0))
#names = imagenet.create_readable_names_for_imagenet_labels()
#for i in range(15):
# index = sorted_inds[i]
# print((probabilities[index], names[index]))
if __name__ == '__main__':
main()
@himsR
Copy link

himsR commented Aug 8, 2017

Hey , were you able to solve the ValueError: When attempting to re-use a scope by suppling adictionary, kwargs must be empty.?
I am stuck at the same error and will really appreciate any help . Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment