记录:使用InceptionV3模型进行迁移学习,并训练自己的数据集

训练:

首先把自己的图片放在文件夹目录下,文件夹名字很重要,这就是到时候生成的label名字。
在这里插入图片描述
使用到了tensorflow的retrain文件,这里直接贴出来:
由于太长了贴不出来,上传到资源,附上地址。
https://download.csdn.net/download/qq_45128278/12511012

我使用的是tensorflow1.12,没有报错。

在cmd中进入retrain.py文件的位置,输入:
python retrain.py --image_dir C:\Users\83543\Desktop\bytrain --how_many_training_steps 400 --model_dir C:\Users\83543\Desktop\inception_dec_2015 --output_graph C:\Users\83543\Desktop\output_graph.pd --output_labels C:\Users\83543\Desktop\output_labels.txt

#–image_dir flower_photos 要分类的图片地址
#–how_many_training_steps 设置训练周期
–model_dir 使用的inceptionV3模型的位置,直接是tar.gz格式的位置就行。
–output_graph output_graph.pd 输出的训练好的我自己的分类模型。
–output_labels output_labels.txt 输出标签的位置,输出后是连续的。

训练过程会显示出准确率:
在这里插入图片描述

测试:

res中是输出的类别,要与输出的标签txt的顺序对应。

# coding: UTF-8
import tensorflow as tf
import os
import numpy as np
import matplotlib.pyplot as plt

# 创建一个图来存放google调整好的模型 inception_pretrain\classify_image_graph_def.pb
# 结果数组与C:\Users\admin\PycharmProjects\TensorFlowTestNew\TensorFlow\inception利用\output_labels.txt文件中的顺序要一致
res = ['beerbottle', 'glass', 'metal', 'waterbottle']

with tf.gfile.FastGFile('C:/Users/83543/Desktop/output_graph.pd', 'rb') as f:
    graph_def = tf.GraphDef()
    graph_def.ParseFromString(f.read())
    tf.import_graph_def(graph_def, name='')

with tf.Session() as sess:
    softmax_tensor = sess.graph.get_tensor_by_name(
        'final_result:0')  # 获取新模型最后的输出节点叫做final_result,可以从tensorboard中的graph中看到,其中名字后面的’:’之后接数字为EndPoints索引值(An operation allocates memory for its outputs, which are available on endpoints :0, :1, etc, and you can think of each of these endpoints as a Tensor.),通常情况下为0,因为大部分operation都只有一个输出。
    # 遍历目录
    for root, dirs, files in os.walk('C:/Users/83543/Desktop/test'):  # 预测图片的位置
        for file in files:
            image_data = tf.gfile.FastGFile(os.path.join(root, file),
                                            'rb').read()  # Returns the contents of a file as a string.
            predictions = sess.run(softmax_tensor, {
                'DecodeJpeg/contents:0': image_data})  # tensorboard中的graph中可以看到DecodeJpeg/contents是模型的输入变量名字
            predictions = np.squeeze(predictions)

            image_path = os.path.join(root, file)
            print(image_path)
            # 展示图片
            # img = plt.imread(image_path)#只能读png图,所以不能显示其他图片,训练非png图时把这段注释掉,他只是一个显示作用
            # plt.imshow(img)
            # plt.axis('off')
            # plt.show()

            top_k = predictions.argsort()[-2:][::-1]  # 概率最高的后2个,然后在倒排一下
            for node_id in top_k:
                score = predictions[node_id]
                print('%s (score=%.5f)' % (res[node_id], score))
            print()

部分识别结果:

在这里插入图片描述

参考链接:https://blog.csdn.net/qq_16320025/article/details/89154488?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522159177380819724845058082%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=159177380819724845058082&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2allfirst_rank_v2~rank_v25-1-89154488.pc_search_back_js3&utm_term=使用inception进行迁移学习训练自己的分类器

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值