-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add SlimNetsLayer and Inception V3 example / Merge TF-Slim into Tenso…
…rLayer
- Loading branch information
Showing
5 changed files
with
217 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
#! /usr/bin/python | ||
# -*- coding: utf8 -*- | ||
|
||
|
||
import tensorflow as tf | ||
import tensorlayer as tl | ||
slim = tf.contrib.slim | ||
from tensorflow.contrib.slim.python.slim.nets.alexnet import alexnet_v2 | ||
from tensorflow.contrib.slim.python.slim.nets.inception_v3 import inception_v3_base, inception_v3, inception_v3_arg_scope | ||
# from tensorflow.contrib.slim.python.slim.nets.resnet_v2 import resnet_v2_152 | ||
# from tensorflow.contrib.slim.python.slim.nets.vgg import vgg_16 | ||
import skimage | ||
import skimage.io | ||
import skimage.transform | ||
import time | ||
from data.imagenet_classes import * | ||
import numpy as np | ||
""" | ||
You will learn: | ||
1. What is TF-Slim ? | ||
1. How to combine TensorLayer and TF-Slim ? | ||
Introduction of Slim : https://github.com/tensorflow/tensorflow/tree/master/tensorflow/contrib/slim | ||
Slim Pre-trained Models : https://github.com/tensorflow/models/tree/master/slim | ||
With the help of SlimNetsLayer, all Slim Model can be combined into TensorLayer. | ||
All models in the following link, end with `return net, end_points`` are available. | ||
https://github.com/tensorflow/tensorflow/tree/master/tensorflow/contrib/slim/python/slim/nets | ||
Bugs | ||
----- | ||
tf.variable_scope : | ||
https://groups.google.com/a/tensorflow.org/forum/#!topic/discuss/RoxrU3UnbFA | ||
load inception_v3 for prediction: | ||
http://stackoverflow.com/questions/39357454/restore-checkpoint-in-tensorflow-tensor-name-not-found | ||
""" | ||
def load_image(path): | ||
# load image | ||
img = skimage.io.imread(path) | ||
img = img / 255.0 | ||
assert (0 <= img).all() and (img <= 1.0).all() | ||
# print "Original Image Shape: ", img.shape | ||
# we crop image from center | ||
short_edge = min(img.shape[:2]) | ||
yy = int((img.shape[0] - short_edge) / 2) | ||
xx = int((img.shape[1] - short_edge) / 2) | ||
crop_img = img[yy: yy + short_edge, xx: xx + short_edge] | ||
# resize to 224, 224 | ||
resized_img = skimage.transform.resize(crop_img, (299, 299)) | ||
return resized_img | ||
|
||
|
||
def print_prob(prob): | ||
synset = class_names | ||
# print prob | ||
pred = np.argsort(prob)[::-1] | ||
# Get top1 label | ||
top1 = synset[pred[0]] | ||
print("Top1: ", top1, prob[pred[0]]) | ||
# Get top5 label | ||
top5 = [(synset[pred[i]], prob[pred[i]]) for i in range(5)] | ||
print("Top5: ", top5) | ||
return top1 | ||
|
||
|
||
## Alexnet_v2 / All Slim nets can be merged into TensorLayer | ||
# x = tf.placeholder(tf.float32, shape=[None, 299, 299, 3]) | ||
# net_in = tl.layers.InputLayer(x, name='input_layer') | ||
# network = tl.layers.SlimNetsLayer(layer=net_in, slim_layer=alexnet_v2, | ||
# slim_args= { | ||
# 'num_classes' : 1000, | ||
# 'is_training' : True, | ||
# 'dropout_keep_prob' : 0.5, | ||
# 'spatial_squeeze' : True, | ||
# 'scope' : 'alexnet_v2' | ||
# } | ||
# ) | ||
# sess = tf.InteractiveSession() | ||
# sess.run(tf.initialize_all_variables()) | ||
# network.print_params() | ||
# exit() | ||
|
||
# InceptionV3 | ||
x = tf.placeholder(tf.float32, shape=[None, 299, 299, 3]) | ||
net_in = tl.layers.InputLayer(x, name='input_layer') # DH | ||
with slim.arg_scope(inception_v3_arg_scope()): | ||
# logits, end_points = inception_v3(X, num_classes=1001, | ||
# is_training=False) | ||
network = tl.layers.SlimNetsLayer(layer=net_in, slim_layer=inception_v3, | ||
slim_args= { | ||
'num_classes' : 1001, | ||
'is_training' : False, | ||
# 'dropout_keep_prob' : 0.8, # for training | ||
# 'min_depth' : 16, | ||
# 'depth_multiplier' : 1.0, | ||
# 'prediction_fn' : slim.softmax, | ||
# 'spatial_squeeze' : True, | ||
# 'reuse' : None, | ||
# 'scope' : 'InceptionV3' | ||
}, | ||
name='' | ||
) | ||
saver = tf.train.Saver() | ||
|
||
sess = tf.InteractiveSession() | ||
sess.run(tf.initialize_all_variables()) | ||
|
||
# with tf.Session() as sess: | ||
saver.restore(sess, "inception_v3.ckpt") # download from https://github.com/tensorflow/models/tree/master/slim#Install | ||
print("Model Restored") | ||
network.print_params(False) | ||
|
||
|
||
from scipy.misc import imread, imresize | ||
y = network.outputs | ||
probs = tf.nn.softmax(y) | ||
img1 = load_image("data/puzzle.jpeg") | ||
img1 = img1.reshape((1, 299, 299, 3)) | ||
|
||
start_time = time.time() | ||
prob = sess.run(probs, feed_dict= {x : img1}) | ||
print("End time : %.5ss" % (time.time() - start_time)) | ||
print_prob(prob[0][1:]) # Note : as it have 1001 outputs, the 1st output is nothing | ||
|
||
|
||
|
||
|
||
|
||
|
||
# |