深度有趣 | 09 Inception-v3图片分类

本文介绍了如何利用预训练的Inception-v3模型进行图片分类,包括加载预训练模型、定制分类任务以及迁移学习的过程。通过实例展示了在五种花卉分类任务上的应用,详细解释了重训练过程和相关参数设置。
摘要由CSDN通过智能技术生成

简介

Inception-v3是由Google提出,用于实现ImageNet大规模视觉识别任务(ImageNet Large Visual Recognition Challenge)的一种神经网络

Inception模型结构

Inception-v3反复使用了Inception Block,涉及大量的卷积和池化,而ImageNet包括1400多万张图片,类别数超过1000

因此手动在ImageNet上训练Inception-v3,需要耗费大量的资源和时间

这里我们选择加载pre-trained的Inception-v3模型,来完成一些图片分类任务

准备

预训练好的模型共包括三个部分

  • classify_image_graph_def.pb:Inception-v3模型结构和参数
  • imagenet_2012_challenge_label_map_proto.pbtxt:从类别编号到类别字符串的对应关系
  • imagenet_synset_to_human_label_map.txt:从类别字符串到类别名的对应关系

例如,169对应n02510455,对应giant panda, panda, panda bear, coon bear, Ailuropoda melanoleuca

图片分类

加载库

# -*- coding: utf-8 -*-

import tensorflow as tf
import numpy as np

整理两个映射文件,得到从类别编号到类别名的对应关系

uid_to_human = {}
for line in tf.gfile.GFile('imagenet_synset_to_human_label_map.txt').readlines():
	items = line.strip().split('\t')
	uid_to_human[items[0]] = items[1]

node_id_to_uid = {}
for line in tf.gfile.GFile('imagenet_2012_challenge_label_map_proto.pbtxt').readlines():
	if line.startswith('  target_class:'):
		target_class = int(line.split(': ')[1])
	if line.startswith('  target_class_string:'):
		target_class_string = line.split(': ')[1].strip('\n').strip('\"')
		node_id_to_uid[target_class] = target_class_string

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

加载模型

def create_graph():
	with tf.gfile.FastGFile('classify_image_graph_def.pb', 'rb') as f:
		graph_def = tf.GraphDef()
		graph_def.ParseFromString(f.read())
		_ = tf.import_graph_def(graph_def, name='')

定义一个分类图片的函数</

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值