基于python实现resnet_【Tensorflow系列】使用Inception_resnet_v2训练自己的数据集并用Tensorboard监控...

本文介绍了如何利用Tensorflow的Inception_resnet_v2模型训练自己的蔬菜图像数据集,包括数据准备、模型训练、使用Tensorboard监控和验证模型。针对Tensorboard版本问题给出了解决方案。
摘要由CSDN通过智能技术生成

【写在前面】

用Tensorflow(TF)已实现好的卷积神经网络(CNN)模型来训练自己的数据集,验证目前较成熟模型在不同数据集上的准确度,如Inception_V3, VGG16,Inception_resnet_v2等模型。本文验证Inception_resnet_v2基于菜场实拍数据的准确性,测试数据为芹菜、鸡毛菜、青菜,各类别样本约600张,多个菜场拍摄,不同数据源。

补充:自己当初的计划是用别人预训练好的模型来再训练自己的数据集已使可以完成新的分类任务,但必须要修改代码改网络结构,并使用迁移学习(Fine-tune)

本文记录了其间的工作过程 ,  相信也会有一些帮助的  : )

测试环境:Centos7.3-64位  python3.5.4(Anaconda)

目录

一.准备

1.安装python

2.安装tensorflow

3.下载TF-slim图像库

4.准备数据

5.下载模型

二.训练

1.读入数据

2.构建模型

3.开始训练

4.执行脚本,训练自己的数据

5.可视化log

【问题】 tensorboard版本已更新,找不到对应包

三.验证

四.测试

一.准备

1.安装python

推荐Anaconda,可创建虚拟环境,用conda命令易实现虚拟环境管理、包管理,安装包时会查出所有依赖包并一共一键安装, 链接:https://www.anaconda.com/download/

2.安装tensorflow

进入当下Anaconda的运行环境,我安装的是python2.7版,并创建3.5虚拟环境

conda create -n py35 python=3.5    【py35是虚拟环境的名称; 输入y 安装】

source activate py35  【激活py35环境】

conda install tensorflow  【安装tensorflow-cpu版,有GPU可安装cpu版】

3.下载TF-slim代码库

cd $WORKSPACE 【目录跳转到自己的工作目录下】

git clone https://github.com/tensorflow/models/

4.准备数据

对所有训练样本按不同样本类别存在不同文件夹下

zsy_train|---jimaocai|--- 0.jpg|---...|---qc|---qingcai

下面的代码是为了生成list.txt , 把不同文件夹下的图片和 数字label对应起来

1 importos2 class_names_to_ids = {'jimaocai': 0, 'qc': 1, 'qingcai': 2}3 data_dir = 'flower_photos/'

4 output_path = 'list.txt'

5 fd = open(output_path, 'w')6 for class_name inclass_names_to_ids.keys():7 images_list = os.listdir(data_dir +class_name)8 for image_name inimages_list:9 fd.write('{}/{} {}\n'.format(class_name, image_name, class_names_to_ids[class_name]))10 fd.close()

为了方便后期查看label标签,也可定义labels.txt

jimaocai

qc

qingcai

随机生成训练集和验证集(在总量中随机选取350个样本作为验证集)

1 importrandom2 _NUM_VALIDATION = 350

3 _RANDOM_SEED =04 list_path = 'list.txt'

5 train_list_path = 'list_train.txt'

6 val_list_path = 'list_val.txt'

7 fd =open(list_path)8 lines =fd.readlines()9 fd.close()10 random.seed(_RANDOM_SEED)11 random.shuffle(lines)12 fd = open(train_list_path, 'w')13 for line inlines[_NUM_VALIDATION:]:14 fd.write(line)15 fd.close()16 fd = open(val_list_path, 'w')17 for line inlines[:_NUM_VALIDATION]:18 fd.write(line)19 fd.close()

生成TFRecord数据

importsys#sys.path.insert(0, '../models/slim/') models-master research

sys.path.insert(0, './models/research/slim/') #把后面的路径插入到系统路径中 idx=0

from datasets importdataset_utilsimportmathimportosimporttensorflow as tf#根据list路径 把数据转化为TFRecord#def convert_dataset(list_path, data_dir, output_dir, _NUM_SHARDS=5):

def convert_dataset(list_path, data_dir, output_dir, _NUM_SHARDS=3):

fd=open(list_path)

lines= [line.split() for line infd]

fd.close()

num_per_shard= int(math.ceil(len(lines) /float(_NUM_SHARDS)))

with tf.Graph().as_default():

decode_jpeg_data= tf.placeholder(dtype=tf.string)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
TensorFlow 2.3.0中,tf_slim库已被弃用,因此无法直接使用`from tf_slim.nets import inception_resnet_v2`来引用inception_resnet_v2。但是,您可以使用TensorFlow官方的模型库(tensorflow/models)中的相应模型来代替。 首先,您需要从GitHub上克隆tensorflow/models仓库到本地: ``` git clone https://github.com/tensorflow/models.git ``` 然后,将models/research/slim目录添加到您的Python路径中。您可以通过以下方式实现: ```python import sys sys.path.append('/path/to/models/research/slim') ``` 现在,您可以使用官方模型库中的inception_resnet_v2模型了。示例代码如下: ```python import tensorflow as tf from official.vision.image_classification import imagenet_preprocessing from official.vision.image_classification import resnet_preprocessing # 导入inception_resnet_v2模型 from official.vision.image_classification.resnet import inception_resnet_v2 # 创建模型实例 model = inception_resnet_v2.InceptionResNetV2(weights=None) # 加载预训练权重(如果有的话) model.load_weights('path/to/pretrained/weights.h5') # 预处理输入图像 image_path = 'path/to/image.jpg' image = tf.io.read_file(image_path) image = tf.image.decode_jpeg(image, channels=3) image = resnet_preprocessing.preprocess_image(image, model.input_shape[1], model.input_shape[2]) image = tf.expand_dims(image, axis=0) # 进行推理 predictions = model.predict(image) # 打印预测结果 print(predictions) ``` 请确保您已经安装了所需的依赖项,并将路径替换为适当的路径。这样,您就可以在TensorFlow 2.3.0中使用inception_resnet_v2模型了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值