<jupyter_start><jupyter_text>Using opencv load image<jupyter_code>img = cv2.imread('../images/dog.jpeg', 3)
img = cv2.resize(img, (224, 224))  # Resize the iamge to [224,244,3]
plt.imshow(img)<jupyter_output><empty_output><jupyter_text>We have a 3 channel image with size 224x224<jupyter_code>img.shape<jupyter_output><empty_output><jupyter_text>Building Tensorflow Computation GraphThe placehold has shape as (number of samples, height, width, number of channels).We use (None, 224, 224, 3) is because we are using a pre-trained VGG16 model.<jupyter_code>tf.reset_default_graph() # Reset computation graph
x = tf.placeholder(dtype=tf.float32, shape=[None, 224, 224, 3],name="image_input") # Placeholder with shape [None, 224, 224, 3]<jupyter_output><empty_output><jupyter_text>Using tensorlayer to build pre-defined VGG16 computation graph![a](../images/vgg6.png)<jupyter_code>vgg = tl.models.VGG16(x)<jupyter_output>[TL] InputLayer  vgg16/input: (?, 224, 224, 3)
[TL] Conv2d vgg16/conv1_1: n_filter: 64 filter_size: (3, 3) strides: (1, 1) pad: SAME act: relu
[TL] Conv2d vgg16/conv1_2: n_filter: 64 filter_size: (3, 3) strides: (1, 1) pad: SAME act: relu
[TL] MaxPool2d vgg16/pool1: filter_size: (2, 2) strides: (2, 2) padding: SAME
[TL] Conv2d vgg16/conv2_1: n_filter: 128 filter_size: (3, 3) strides: (1, 1) pad: SAME act: relu
[TL] Conv2d vgg16/conv2_2: n_filter: 128 filter_size: (3, 3) strides: (1, 1) pad: SAME act: relu
[TL] MaxPool2d vgg16/pool2: filter_size: (2, 2) strides: (2, 2) padding: SAME
[TL] Conv2d vgg16/conv3_1: n_filter: 256 filter_size: (3, 3) strides: (1, 1) pad: SAME act: relu
[TL] Conv2d vgg16/conv3_2: n_filter: 256 filter_size: (3, 3) strides: (1, 1) pad: SAME act: relu
[TL] Conv2d vgg16/conv3_3: n_filter: 256 filter_size: (3, 3) strides: (1, 1) pad: SAME act: relu
[TL] MaxPool2d vgg16/pool3: filter_size: (2, 2) strides: (2, 2) padding: SAME
[TL] Conv2d vgg16/conv4_1: n_filter: 512 f[...]<jupyter_text>Intialize the Session<jupyter_code>sess = tf.InteractiveSession()<jupyter_output><empty_output><jupyter_text>Restore pre-trained VGG parameters<jupyter_code>vgg.restore_params(sess)<jupyter_output>[TL] Restore pre-trained parameters
[TL]   Loading params (3, 3, 3, 64)
[TL]   Loading params (64,)
[TL]   Loading params (3, 3, 64, 64)
[TL]   Loading params (64,)
[TL]   Loading params (3, 3, 64, 128)
[TL]   Loading params (128,)
[TL]   Loading params (3, 3, 128, 128)
[TL]   Loading params (128,)
[TL]   Loading params (3, 3, 128, 256)
[TL]   Loading params (256,)
[TL]   Loading params (3, 3, 256, 256)
[TL]   Loading params (256,)
[TL]   Loading params (3, 3, 256, 256)
[TL]   Loading params (256,)
[TL]   Loading params (3, 3, 256, 512)
[TL]   Loading params (512,)
[TL]   Loading params (3, 3, 512, 512)
[TL]   Loading params (512,)
[TL]   Loading params (3, 3, 512, 512)
[TL]   Loading params (512,)
[TL]   Loading params (3, 3, 512, 512)
[TL]   Loading params (512,)
[TL]   Loading params (3, 3, 512, 512)
[TL]   Loading params (512,)
[TL]   Loading params (3, 3, 512, 512)
[TL]   Loading params (512,)
[TL]   Loading params (25088, 4096)
[TL]   Loading params (4096,)
[TL]   Loading params [...]<jupyter_text>Softmax layer for outputing probabiliy distribution<jupyter_code>probs = tf.nn.softmax(vgg.outputs)<jupyter_output><empty_output><jupyter_text>Tnesorlayer API for printing the network structure<jupyter_code>vgg.print_params(False)
vgg.print_layers()<jupyter_output>[TL]   layer   0: image_input:0        (?, 224, 224, 3)    float32
[TL]   layer   1: vgg16/conv1_1/Relu:0 (?, 224, 224, 64)    float32
[TL]   layer   2: vgg16/conv1_2/Relu:0 (?, 224, 224, 64)    float32
[TL]   layer   3: vgg16/pool1/MaxPool:0 (?, 112, 112, 64)    float32
[TL]   layer   4: vgg16/conv2_1/Relu:0 (?, 112, 112, 128)    float32
[TL]   layer   5: vgg16/conv2_2/Relu:0 (?, 112, 112, 128)    float32
[TL]   layer   6: vgg16/pool2/MaxPool:0 (?, 56, 56, 128)    float32
[TL]   layer   7: vgg16/conv3_1/Relu:0 (?, 56, 56, 256)    float32
[TL]   layer   8: vgg16/conv3_2/Relu:0 (?, 56, 56, 256)    float32
[TL]   layer   9: vgg16/conv3_3/Relu:0 (?, 56, 56, 256)    float32
[TL]   layer  10: vgg16/pool3/MaxPool:0 (?, 28, 28, 256)    float32
[TL]   layer  11: vgg16/conv4_1/Relu:0 (?, 28, 28, 512)    float32
[TL]   layer  12: vgg16/conv4_2/Relu:0 (?, 28, 28, 512)    float32
[TL]   layer  13: vgg16/conv4_3/Relu:0 (?, 28, 28, 512)    float32
[TL]   layer  14: vgg16/pool4/MaxPool:0 (?, 14, 14, [...]<jupyter_text>Session run inference<jupyter_code>img = np.expand_dims(img,0) #[224,224,3] -> [1,224,224,3]
img.shape
prob = sess.run(probs, feed_dict={x: img})
prob.shape
prob = prob[0]
p = np.argsort(prob)[0:5]
for p in preds:
    print(class_names[p], prob[p])<jupyter_output>Samoyed, Samoyede 0.16822036
Eskimo dog, husky 0.16267341
golden retriever 0.1160161
collie 0.083168924
white wolf, Arctic wolf, Canis lupus tundrarum 0.07951109
{'fraction_non_alphanumeric': 0.15384615384615385, 'fraction_numerical': 0.1258741258741259, 'mean_word_length': 4.707746478873239, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 34, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '158325', 'license': 'Apache-2.0', 'n_tokens_mistral': 2492, 'n_tokens_neox': 2010, 'n_words': 562, 'path': 'scripts/.ipynb_checkpoints/vgg16_sample-checkpoint.ipynb', 'repo_name': 'Lucklyric/tf_image_recongnition_tutorial', 'size': '4862'}