利用 Inception-v3 进行图像识别

2018-09-07  本文已影响0人  起个名费劲儿

使用 TensorFlow 你可以完成很多有意的事情,TensorFlow 是 Google 开源的一套机器学习框架。Inception-v3 是一个新的模型,是 GoogleNet 的第三个版本,目前最新的 Inception 模型已经更新到第四个版本。

这篇文章中,我将使用 Inception-v3 模型来做简单的各种图像识别。如果你想直接跳到代码,可以在文末的 GitHub 链接上找到它;或者你也可以利用 Google Colab 进行零设置运行模型。

数据集:图像识别来源的数据

我们将使用 Google 训练好的数据集做图像识别。

下载地址:https://storage.googleapis.com/download.tensorflow.org/models/inception_dec_2015.zip

代码编写

模型中的 imagenet_2012_challenge_label_map_proto.pbtxt 描述文件中包含着大量名值匹配的数据。其中 target_class 有着 1000 个数据分类。

entry {
  target_class: 449
  target_class_string: “n01440764”
}

target_class_string 对应编号:

n01440764 tench, Tinca tinca

首先,导入所有必要模块。

import tensorflow as tf
import os
import numpy as np
import re
from PIL import Image
import matplotlib.pyplot as plt

接下来,将下载好的数据加载进行分类。

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(label_lookup_path, uid_lookup_path)
…
proto_as_ascii_lines = tf.gfile.GFile(uid_lookup_path).readlines()
for line in proto_as_ascii_lines:
…
proto_as_ascii = tf.gfile.GFile(label_lookup_path).readlines()
for line in proto_as_ascii:

给 1000 个数据建立映射关系。

node_id_to_name = {}
for key, val in node_id_to_uid.items():
name = uid_to_human[val]
node_id_to_name[key] = name
return node_id_to_name

创建 Graph 训练模型。

with tf.gfile.FastGFile(‘inception_model/classify_image_graph_def.pb’, ‘rb’) as f:
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
tf.import_graph_def(graph_def, name = ”)

运行结果

lakeside, lakeshore (score = 0.86106)
lakeside, lakeshore (score = 0.86106)
canoe (score = 0.05234)
boathouse (score = 0.04097)
corn (score = 0.00564)
rapeseed (score = 0.00400)

应用结果给出 86% 的 lakeside, lakeshore.

运用 Inception-v3 进行图像识别,你还可以做更多简单的实例,比如利用它进行实物翻译,或者制作模型做些细化的图像分类等。

GIthub

https://github.com/Timeframes/inception-v3

上一篇下一篇

猜你喜欢

热点阅读