import glob
import os.path
import random
import numpy as np
import tensorflow as tf
from tensorflow.python.platform import gfile
from tensorflow.python.framework import graph_util
import inceptionV3.dirDef as gDir
def create_inception_graph():
with tf.Graph().as_default() as graph:
model_filename = os.path.join(
gDir.MODEL_DIR, gDir.MODEL_FILE_ori)
with gfile.FastGFile(model_filename, 'rb') as f:
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
相关功能包导入
import glob import os.path import random import numpy as np import tensorflow as tf from tensorflow.python.platform import gfile from tensorflow.python.framework import graph_util import inceptionV3.dirDef as gDir
inception-v3 模型瓶颈层的节点个数
BOTTLENECK_TENSOR_SIZE = 2048
inception-v3 模型中代表瓶颈层结果的张量名称
BOTTLENECK_TENSOR_NAME = 'pool_3/_reshape:0'
图像输入张量所对应的名称
JPEG_DATA_TENSOR_NAME = 'DecodeJpeg/contents:0'
验证的数据百分比
VALIDATION_PERCENTAGE = 10
测试的数据百分比
TEST_PERCENTACE = 10
定义神经网路的设置
LEARNING_RATE = 0.01 STEPS = 1000 BATCH = 100
这个函数把数据集分成训练,验证,测试三部分
def create_image_lists(testing_percentage, validation_percentage): """ 这个函数把数据集分成训练,验证,测试三部分 :param testing_percentage:测试的数据百分比 10 :param validation_percentage:验证的数据百分比 10 :return: """ result = {}
获取目录下所有子目录
获取当前目录下所有的有效图片文件
通过目录名获取类别名称
初始化当前类别的训练数据集、测试数据集、验证数据集
这个函数通过类别名称、所属数据集和图片编号获取一张图片的地址
def get_image_path(image_lists, image_dir, label_name, index, category): """ :param image_lists:所有图片信息 :param image_dir:根目录 ( 图片特征向量根目录 CACHE_DIR | 图片原始路径根目录 INPUT_DATA ) :param label_name:类别的名称( daisy|dandelion|roses|sunflowers|tulips ) :param index:编号 :param category:所属的数据集( training|testing|validation ) :return: 一张图片的地址 """
获取给定类别的图片集合
图片的特征向量的文件地址
def get_bottleneck_path(image_lists, label_name, index, category): return get_image_path(image_lists, gDir.CACHE_DIR, label_name, index, category) + '.txt' # CACHE_DIR 特征向量的根地址
计算特征向量
def run_bottleneck_on_image(sess, image_data, image_data_tensor, bottleneck_tensor): bottleneck_values = sess.run(bottleneck_tensor, {image_data_tensor: image_data}) bottleneck_values = np.squeeze(bottleneck_values) return bottleneck_values
获取一张图片对应的特征向量的路径
def get_or_create_bottleneck(sess, image_lists, label_name, index, category, jpeg_data_tensor, bottleneck_tensor): label_lists = image_lists[label_name] sub_dir = label_lists['dir'] sub_dir_path = os.path.join(gDir.CACHE_DIR, sub_dir) # 到类别的文件夹 if not os.path.exists(sub_dir_path): os.makedirs(sub_dir_path) bottleneck_path = get_bottleneck_path(image_lists, label_name, index, category) # 获取图片特征向量的路径 if not os.path.exists(bottleneck_path): # 如果不存在
获取图片原始路径
随机获取一个batch的图片作为训练数据(特征向量,类别)
def get_random_cached_bottlenecks(sess, n_classes, image_lists, how_many, category, jpeg_data_tensor, bottleneck_tensor): """ :param sess: :param n_classes: 类别数量 :param image_lists: :param how_many: 一个batch的数量 :param category: 所属的数据集 :param jpeg_data_tensor: :param bottleneck_tensor: :return: 特征向量列表,类别列表 """ bottlenecks = [] groundtruths = [] for in range(how_many):
随机一个类别和图片编号加入当前的训练数据
获取全部的测试数据
def get_test_bottlenecks(sess, image_lists, n_classes, jpeg_data_tensor, bottleneck_tensor): bottlenecks = [] ground_truths = [] label_name_list = list(image_lists.keys()) # ['dandelion', 'daisy', 'sunflowers', 'roses', 'tulips'] for label_index, label_name in enumerate(label_name_list): # 枚举每个类别,如:0 sunflowers category = 'testing' for index, unused_base_name in enumerate(image_lists[label_name][category]): # 枚举此类别中的测试数据集中的每张图片
创建网络
def create_inception_graph(): with tf.Graph().as_default() as graph: model_filename = os.path.join( gDir.MODEL_DIR, gDir.MODEL_FILE_ori) with gfile.FastGFile(model_filename, 'rb') as f: graph_def = tf.GraphDef() graph_def.ParseFromString(f.read())
在原有网络上新增新的softmax层
def add_final_training_ops(class_count, bottleneck_tensor):
输入
按照batch 执行梯度下降训练网络
def train(_): image_lists = create_image_lists(TEST_PERCENTACE, VALIDATION_PERCENTAGE) n_classes = len(image_lists.keys()) print('n_classes:', n_classes)
执行总入口
if name == 'main': tf.app.run(train)