Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def oneFile(filepath):
- imgPath = filepath
- 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)
- # Create the model, use the default arg scope to configure the batch norm parameters.
- with slim.arg_scope(inception_resnet_v2_arg_scope()):
- #logits, end_points = inception_resnet_v2(images, num_classes = dataset.num_classes, is_training = False)
- 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))
- with tf.Session() as sess:
- 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]))
- 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
- #image = Image.open(os.path.join(dataset_dir, image_file))
- filepath = os.path.join(dataset_dir, image_file)
- oneFile(filepath)
- def inception_resnet_v2_arg_scope(weight_decay=0.00004,
- batch_norm_decay=0.9997,
- batch_norm_epsilon=0.001):
- """Yields the scope with the default parameters for inception_resnet_v2.
- Args:
- weight_decay: the weight decay for weights variables.
- batch_norm_decay: decay for the moving average of batch_norm momentums.
- batch_norm_epsilon: small float added to variance to avoid dividing by zero.
- Returns:
- a arg_scope with the parameters needed for inception_resnet_v2.
- """
- # Set weight_decay for weights in conv2d and fully_connected layers.
- with slim.arg_scope([slim.conv2d, slim.fully_connected],
- weights_regularizer=slim.l2_regularizer(weight_decay),
- biases_regularizer=slim.l2_regularizer(weight_decay)):
- batch_norm_params = {
- 'decay': batch_norm_decay,
- 'epsilon': batch_norm_epsilon,
- }
- # Set activation_fn and parameters for batch_norm.
- with slim.arg_scope([slim.conv2d], activation_fn=tf.nn.relu,
- normalizer_fn=slim.batch_norm,
- normalizer_params=batch_norm_params) as scope:
- return scope
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement