tensorflow迁移学习
tensorflow利用Inception-v3实现迁移学习。加载已有的模型,作为新任务提取特征的操作,实现分类迁移。包含完整的代码和数据集
代码片段和文件信息
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 88631107 2018-10-01 12:03 inception_dec_2015.zip
文件 10466 2018-10-02 15:54 迁移学习.py
----------- --------- ---------- ----- ----
88641573 2
#photo地址:
#http://download.tensorflow.org/example_images/flower_photos.tgz
#Inception-v3模型
#https://storage.googleapis.com/download.tensorflow.org/models/inception_dec_2015.zip
import glob
import os.path
import random
import numpy as np
import tensorflow as tf
from tensorflow.python.platform import gfile
#Inception-v3模型瓶颈层得节点个数
BOTTLENECK_TENSOR_SIZE=2048
BOTTLENECK_TENSOR_NAME=‘pool_3/_reshape:0‘
JPEG_DATA_TENSOR_NAME=‘DecodeJpeg/contents:0‘
MODEL_DIR=‘C:/Users/casgj/PycharmProjects/CNN/inception_dec_2015‘
MODEL_FILE=‘tensorflow_inception_graph.pb‘
CACHE_DIR=‘C:/Users/casgj/PycharmProjects/CNN/bottleneck‘
INPUT_DATA=‘C:/Users/casgj/PycharmProjects/CNN/flower_photos‘
#验证得数据百分比
VALIDATION_PERCENTAGE=10
#测试得数据百分比
TEST_PERCENTAGE=10
#定义神经网络得设置
LEARNING_RATE=0.01
STEPS=4000
BATCH=100
#从数据文件夹读取所有得图片列表并按训练、验证、测试数据分开
def create_image_lists(testing_percentage validation_percentage):
result = {}
sub_dirs = [x[0] for x in os.walk(INPUT_DATA)]
is_root_dir = True
for sub_dir in sub_dirs:
if is_root_dir:
is_root_dir = False
continue
extensions = [‘jpg‘ ‘jpeg‘ ‘JPG‘ ‘JPEG‘]
file_list = []
dir_name = os.path.basename(sub_dir)
for extension in extensions:
file_glob = os.path.join(INPUT_DATA dir_name ‘*.‘ + extension)
file_list.extend(glob.glob(file_glob))
if not file_list:
continue
label_name = dir_name.lower()
# 初始化
training_images = []
testing_images = []
validation_images = []
for file_name in file_list:
base_name = os.path.basename(file_name)
# 随机划分数据
chance = np.random.randint(100) #产生一个<100得正整数
if chance < validation_percentage:
validation_images.append(base_name)
elif chance < (testing_percentage + validation_percentage):
testing_images.append(base_name)
else:
training_images.append(base_name)
result[label_name] = {
‘dir‘: dir_name
‘training‘: training_images
‘testing‘: testing_images
‘validation‘: validation_images
}
return result
#获取给定类别category中index图片的地址
def get_image_path(image_listsimage_dirlabel_nameindexcategory):
label_lists=image_lists[label_name]
category_list=label_lists[category]
mod_index=index% len(category_list)
base_name=category_list[mod_index]
sub_dir=label_lists[‘dir‘]
full_path=os.path.join(image_dirsub_dirbase_name)
return full_path
def get_bottleneck_path(image_listslabel_nameindexcategory):
return get_image_path(image_listsCACHE_DIR
label_nameindexcategory)+‘.txt‘
def run_bottleneck_on_image(sessimage_dataimage_data_tensorbottleneck_tensor):
bottleneck_values=sess.run(bottleneck_te
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 88631107 2018-10-01 12:03 inception_dec_2015.zip
文件 10466 2018-10-02 15:54 迁移学习.py
----------- --------- ---------- ----- ----
88641573 2
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件举报,一经查实,本站将立刻删除。
评论列表(条)