迁移学习的出现是为了解决图形标注与训练时间的问题,将一个问题上训练好的模型通过简单的调整使其适用于一个新的问题。
模型:Google提供的基于ImageNet数据集训练好的Inception V3模型
修改:对于训练好的Inception V3模型,保留卷积层的所有参数,只是替换掉最后一层的全连层,并连接Softmax层输出分类结果。在全连层之前有一层bottlenecks层,该层主要输出图像经Inception V3模型处理后得到的特征向量。
数据集:https://storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz
模型下载:https://storage.googleapis.com/download.tensorflow.org/models/inception_dec_2015.zip
两个py文件:flower_photos_display.py 和 Inception V3.py
- flower_photos_display.py主要完成图片处理相关内容,将图片整理成一个字典,获得并返回图片路径、计算得到的特征向量。其中定义的函数如下:
- def create_image_dict():读取所有的图片名并组成列表的形式,随后按训练集、测试集、验证集分开,返回值为result;
- def get_random_bottlenecks(sess, num_classes, image_lists, batch_size, data_category, jpeg_data_tensor,bottleneck_tensor):用来随机产生一个batch的特征向量及其对应的label标签,返回bottlenecks,labels
- def create_bottleneck(sess, image_lists, flower_category, image_index,data_category, jpeg_data_tensor, bottleneck_tensor):在get_random_bottlenecks()函数中被调用,能够获取一张经Inception V3模型处理后的特征向量,返回值bottleneck_values;
- def get_image_path(image_lists(=result.dir), image_dir(input_data或CACHE_DIR), flower_category, image_index(由get_random_bottlenecks产生), data_category(Inception V3.py传递)):在create_bottleneck()函数中被使用,根据传递进来的参数返回一个带路径的图片名,返回值full_path;
- def get_test_bottlenecks(sess, image_lists, num_classes, jpeg_data_tensor, bottleneck_tensor):获取全部的测试数据,与get_random_bottlenecks()不同的是,没有划分batch,没有随机抽样过程,返回值bottlenecks,labels。
- Inception V3.py实现了模型文件的读取,增加一层全连接层并进行训练、验证和测试。
- 调用create_image_lists()函数获得该函数返回的字典
- 读取已经训练好的Inception-v3模型。
- 定义一层全连接层
- 定义交叉熵损失函数以及train_step使用的随机梯度下降优化器
- 定义计算正确率的操作
函数文件下载:函数文件上传到我上传的资源中。https://download.csdn.net/download/icecreamdinner/28492287



