一、预备知识
(1)tensor : 符号化的句柄,指向操作的运算结果。
(2)operation : 符号化的运算过程,即图中的节点。
(3)graph : 模型结构的流程图,tensor 和 operation 都是图中的对象。
(4)tf.gfile.FastGFile(path,decodestyle)
函数功能:实现对图片的读取。
函数参数:(1)path:图片所在路径 (2)decodestyle:图片的解码方式。(‘r’:UTF-8编码; ‘rb’:非UTF-8编码)。
import matplotlib.pyplot as plt
import tensorflow as tf
#tf.gfileGFile()函数:读取图像
image_jpg = tf.gfile.FastGFile('dog.jpg','rb').read()
image_png = tf.gfile.FastGFile('lizard.png','rb').read()
with tf.Session() as sess:
image_jpg = tf.image.decode_jpeg(image_jpg) #图像解码
print(sess.run(image_jpg))#打印解码后的图像(即为一个三维矩阵[w,h,3])
image_jpg = tf.image.convert_image_dtype(image_jpg,dtype=tf.uint8) #改变图像数据类型
image_png = tf.image.decode_png(image_png)
print(sess.run(image_jpg))
image_png = tf.image.convert_image_dtype(image_png,dtype=tf.uint8)
plt.figure(1)
plt.imshow(image_jpg.eval())
plt.figure(2)
plt.imshow(image_png.eval())
二、使用 Inception V3 做图像分类
# coding: utf-8
import tensorflow as tf
import os
import numpy as np
import re
from PIL import Image
import matplotlib.pyplot as plt
class NodeLookup(object):
def __init__(self):
label_lookup_path = 'inception_model/imagenet_2012_challenge_label_map_proto.pbtxt'
uid_lookup_path = 'inception_model/imagenet_synset_to_human_label_map.txt'
self.node_lookup = self.load(labe