TensorFlow练习11: 图像分类器 – retrain谷歌Inception模型(转)

原文地址:https://www.tuicool.com/articles/ieQZVfa

前一帖《 TensorFlow练习10: 实现谷歌Deep Dream 》使用到了谷歌训练的Inception模型,本帖就基于Inception模型retrain一个图像分类器。

图像分类器应用广泛,连农业都在使用,如判断黄瓜种类。

本帖使用的训练数据是《 TensorFlow练习9: 生成妹子图(PixelCNN) 》一文中使用的妹子图,最后训练出的分类器可以判断图片是不是妹子图。

首先下载tensorflow源代码:

$ gitclone https://github.com/tensorflow/tensorflow
$ gitcheckoutr0.11  # checkout对应已安装的Tensorflow版本

使用examples中的image_retraining。

训练:

$ pythontensorflow/tensorflow/examples/image_retraining/retrain.py --bottleneck_dirbottleneck --how_many_training_steps 4000 --model_dirmodel --output_graphoutput_graph.pb --output_labelsoutput_labels.txt --image_dirgirl_types/

参数解释参考retrain.py源文件。

大概训练了半个小时:

生成的模型文件和labels文件:

使用训练好的模型:

importtensorflowas tf
importsys
 
# 命令行参数,传入要判断的图片路径
image_file = sys.argv[1]
#print(image_file)
 
# 读取图像
image = tf.gfile.FastGFile(image_file, 'rb').read()
 
# 加载图像分类标签
labels = []
for labelin tf.gfile.GFile("output_labels.txt"):
 labels.append(label.rstrip())
 
# 加载Graph
withtf.gfile.FastGFile("output_graph.pb", 'rb') as f:
 graph_def = tf.GraphDef()
 graph_def.ParseFromString(f.read())
 tf.import_graph_def(graph_def, name='')
 
withtf.Session() as sess:
 softmax_tensor = sess.graph.get_tensor_by_name('final_result:0')
 predict = sess.run(softmax_tensor, {'DecodeJpeg/contents:0': image})
 
 # 根据分类概率进行排序
 top = predict[0].argsort()[-len(predict[0]):][::-1]
 for indexin top:
 human_string = labels[index]
 score = predict[0][index]
 print(human_string, score)

执行结果:

参考:

Share the post "TensorFlow练习11: 图像分类器 – retrain谷歌Inception模型"

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值