模型保存方法:
saver = tf.train.Saver(tf.global_variables())
下文中在cifar10数据中的模型保存以及测试:
训练以及模型保存代码:
#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Created on Tue Jan 9 21:46:26 2018 @author: lvedng """ # In[1]: import tensorflow as tf import matplotlib.pyplot as plt import numpy as np def read_and_decode(filename): # 读入dog_train.tfrecords # filename_queue = tf.train.string_input_producer([filename]) # 生成一个queue队列 filename_queue = tf.train.string_input_producer([filename],shuffle=True,num_epochs=100) # 生成一个queue队列 reader = tf.TFRecordReader() _, serialized_example = reader.read(filename_queue) # 返回文件名和文件 features = tf.parse_single_example(serialized_example, features={ 'label': tf.FixedLenFeature([], tf.int64), 'img_raw': tf.FixedLenFeature([], tf.string), }) # 将image数据和label取出来 img = tf.decode_raw(features['img_raw'], tf.uint8) img = tf.reshape(img, [32 * 32 * 3]) # reshape为128*128的3通道图片 img = tf.cast(img, tf.float32) * (1. / 255) label = tf.cast(features['label'], tf.int32) # 在流中抛出label张量 return img, label def createBatch(filename, batchsize): images, labels = read_and_decode(filename) min_after_dequeue = batchsize capacity = 10* batchsize image_batch, label_batch = tf.train.shuffle_batch([images, labels], batch_size=batchsize, capacity=capacity, min_after_dequeue=min_after_dequeue ) label_batch = tf.one_hot(label_batch, depth=10) return image_batch, label_batch # 实现Batch Normalization def bn_layer(x,name, is_training, moving_decay=0.9, eps=1e-5): # 获取输入维度并判断是否匹配卷积层(4)或者全连接层(2) shape = x.shape assert len(shape) in [2, 4] param_shape = shape[-1] with tf.variable_scope(name): # 声明BN中唯一需要学习的两个参数,y=gamma*x+beta gamma = tf.get_variable("gamma", param_shape, initializer=tf.constant_initializer(1)) beta = tf.get_variable("beta", param_shape, initializer=tf.constant_initializer(0)) # 计算当前整个batch的均值与方差 axes = list(range(len(shape) - 1)) # 特征的通道数 batch_mean, batch_var = tf.nn.moments(x, axes, name='moments') # 采用滑动平均更新均值与方差 ema = tf.train.ExponentialMovingAverage(moving_decay) def mean_var_with_update(): ema_apply_op = ema.apply([batch_mean, batch_var]) with tf.control_dependencies([ema_apply_op]): return tf.identity(batch_mean), tf.identity(batch_var) # 训练时,更新均值与方差,测试时使用之前最后一次保存的均值与方差 mean, var = tf.cond(tf.equal(is_training, True), mean_var_with_update, lambda: (ema.average(batch_mean), ema.average(batch_var))) # 最后执行batch normalization return tf.nn.batch_normalization(x, mean, var, beta, gamma, eps) # 初始化权值 def weight_variable(shape): initial = tf.truncated_normal(shape, stddev=0.1) # 生成一个截断的正态分布 return tf.Variable(initial) # 初始化偏置 def bias_variable(shape): initial = tf.constant(0.1, shape=shape) return tf.Variable(initial) def dwconv(name,input, filter_size, in_filters, mutil_filters, strides): regularizer = tf.contrib.layers.l2_regularizer(scale = 0.0001) n = filter_size * filter_size * mutil_filters with tf.variable_scope(name): weights = tf.get_variable('conv_weights', shape=[filter_size, filter_size, in_filters, mutil_filters], initializer=tf.random_normal_initializer(stddev=np.sqrt(2.0 / n)), regularizer=regularizer) # f2 = tf.Variable(tf.truncated_normal([filter_size, filter_size, in_filters, mutil_filters], stddev=0.1)) return tf.nn.depthwise_conv2d(input, weights, strides, padding="SAME", rate=None, name=None, data_format=None) # 池化层 def max_pool_2x2(x): return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') # 2D卷积 def _conv(name, x, filter_size, in_fliters, out_filters, strides): b = bias_variable([out_filters]) regularizer = tf.contrib.layers.l2_regularizer(scale=0.0001) n = filter_size * filter_size * out_filters with tf.variable_scope(name): # 获取或新建卷积核,正态随机初始化 weights = tf.get_variable('convws', shape=[filter_size, filter_size, in_fliters, out_filters], initializer=tf.random_normal_initializer(stddev=np.sqrt(2.0 / n)), regularizer=regularizer) # kernel = tf.Variable(tf.truncated_normal([filter_size, filter_size, in_fliters, out_filters], stddev=0.1)) out=tf.nn.conv2d(x, weights, strides, padding='SAME') + b # 计算卷积 return out # 把步长值转换成tf.nn.conv2d需要的步长数组 def stride_arr(stride): return [1, stride, stride, 1] def output(x_tensor, num_outputs): #全连接层 x_shape = x_tensor.get_shape().as_list() regularizer = tf.contrib.layers.l2_regularizer(scale = 0.0001) weights = tf.get_variable('weight', shape = [x_shape[1], num_outputs], initializer = tf.uniform_unit_scaling_initializer(factor = 1.0), regularizer = regularizer) bias = tf.Variable(tf.zeros([num_outputs])) out = tf.add(tf.matmul(x_tensor, weights), bias) return out def visualize_feature_map_sum(feature_batch): ''' 将每张子图进行相加 :param feature_batch: :return: ''' feature_map = feature_batch feature_map_combination = [] # 取出 featurn map 的数量 num_pic = feature_map.shape[2] # 将 每一层卷积的特征图,拼接层 5 × 5 for i in range(0, num_pic): feature_map_split = feature_map[:, :, i] feature_map_combination.append(feature_map_split) # 按照特征图 进行 叠加代码 feature_map_sum = sum(one for one in feature_map_combination) plt.imshow(feature_map_sum) plt.show() def visualize_feature_map(feature_batch): ''' 创建特征子图,创建叠加后的特征图 :param feature_batch: 一个卷积层所有特征图 :return: ''' feature_map = feature_batch feature_map_combination = [] plt.figure(figsize=(8, 8)) # 取出 featurn map 的数量,因为特征图数量很多,这里直接手动指定了。 #num_pic = feature_map.shape[2] row, col = get_row_col(64) # 将 每一层卷积的特征图,拼接层 5 × 5 for i in range(0, 64): feature_map_split = feature_map[:, :, i] feature_map_combination.append(feature_map_split) plt.subplot(row, col, i+1) plt.imshow(feature_map_split) plt.axis('off') def get_row_col(num_pic): ''' 计算行列的值 :param num_pic: 特征图的数量 :return: ''' squr = num_pic ** 0.5 row = round(squr) col = row + 1 if squr - row > 0 else row return row, col #plt.savefig('./mao_feature/feature_map2.png') # 保存图像到本地 plt.show() # 命名空间 lamda = 0.004 # 正则化系数,这里添加了正则化但是没有使用 # 定义两个placeholder x = tf.placeholder(tf.float32, [None, 32 * 32 * 3]) y = tf.placeholder(tf.float32, [None, 10]) # 改变x的格式转为4D的向量[batch, in_height, in_width, in_channels]` x_image = tf.reshape(x, [-1, 32, 32, 3]) is_training = tf.placeholder(tf.bool) learnrate = tf.placeholder(tf.float32) conv1 = _conv("conv1", x_image, 3, 3, 32, stride_arr(2)) # 16 conv1 = bn_layer(conv1, "BN1",is_training) conv1 = tf.nn.relu(conv1) conv2 = dwconv('dw2',conv1, 3, 32, 1, stride_arr(1)) conv2 = bn_layer(conv2, "BN2_1", is_training) conv2 = tf.nn.relu(conv2) conv2 = _conv("conv2", conv2, 1, 32, 64, stride_arr(1)) conv2 = bn_layer(conv2, "BN2_2", is_training) conv2 = tf.nn.relu(conv2) conv3 = dwconv('dw3',conv2, 3, 64, 1, stride_arr(2)) # 8 conv3 = bn_layer(conv3, "BN3_1", is_training) conv3 = tf.nn.relu(conv3) conv3 = _conv("conv3", conv3, 1, 64, 128, stride_arr(1)) conv3 = bn_layer(conv3, "BN3_2", is_training) conv3 = tf.nn.relu(conv3) conv4 = dwconv('dw4',conv3, 3, 128, 1, stride_arr(1)) conv4 = bn_layer(conv4, "BN4_1", is_training) conv4 = tf.nn.relu(conv4) conv4 = _conv("conv4", conv4, 1, 128, 128, stride_arr(1)) conv4 = bn_layer(conv4, "BN4_2", is_training) conv4 = tf.nn.relu(conv4) conv5 = dwconv('dw5',conv4, 3, 128, 1, stride_arr(2)) # 4 conv5 = bn_layer(conv5, "BN5_1", is_training) conv5 = tf.nn.relu(conv5) conv5 = _conv("conv5", conv5, 1, 128, 256, stride_arr(1)) conv5 = bn_layer(conv5, "BN5_2", is_training) conv5 = tf.nn.relu(conv5) conv6 = dwconv('dw6',conv5, 3, 256, 1, stride_arr(1)) conv6 = bn_layer(conv6, "BN6_1", is_training) conv6 = tf.nn.relu(conv6) conv6 = _conv("conv6", conv6, 1, 256, 256, stride_arr(1)) conv6 = bn_layer(conv6, "BN6_2", is_training) conv6 = tf.nn.relu(conv6) conv7 = dwconv('dw7',conv6, 3, 256, 1, stride_arr(2)) # 2 conv7 = bn_layer(conv7, "BN7_1", is_training) conv7 = tf.nn.relu(conv7) conv7 = _conv("conv7", conv7, 1, 256, 512, stride_arr(1)) conv7 = bn_layer(conv7, "BN7_2", is_training) conv7 = tf.nn.relu(conv7) #10个重复的层 conv8 = dwconv('dw8_1',conv7, 3, 512, 1, stride_arr(1)) conv8 = bn_layer(conv8, "BN8_1", is_training) conv8 = tf.nn.relu(conv8) conv8 = _conv("conv8_1", conv8, 1, 512, 512, stride_arr(1)) conv8 = bn_layer(conv8, "BN8_2", is_training) conv8 = tf.nn.relu(conv8) conv8 = dwconv('dw8_2',conv8, 3, 512, 1, stride_arr(1)) conv8 = bn_layer(conv8, "BN8_3", is_training) conv8 = tf.nn.relu(conv8) conv8 = _conv("conv8_2", conv8, 1, 512, 512, stride_arr(1)) conv8 = bn_layer(conv8, "BN8_4", is_training) conv8 = tf.nn.relu(conv8) conv8 = dwconv('dw8_3',conv8, 3, 512, 1, stride_arr(1)) conv8 = bn_layer(conv8, "BN8_5", is_training) conv8 = tf.nn.relu(conv8) conv8 = _conv("conv8_3", conv8, 1, 512, 512, stride_arr(1)) conv8 = bn_layer(conv8, "BN8_6", is_training) conv8 = tf.nn.relu(conv8) conv8 = dwconv('dw8_4',conv8, 3, 512, 1, stride_arr(1)) conv8 = bn_layer(conv8, "BN8_7", is_training) conv8 = tf.nn.relu(conv8) conv8 = _conv("conv8_4", conv8, 1, 512, 512, stride_arr(1)) conv8 = bn_layer(conv8, "BN8_8", is_training) conv8 = tf.nn.relu(conv8) conv8 = dwconv('dw8_5',conv8, 3, 512, 1, stride_arr(1)) conv8 = bn_layer(conv8, "BN8_9", is_training) conv8 = tf.nn.relu(conv8) conv8 = _conv("8_5", conv8, 1, 512, 512, stride_arr(1)) conv8 = bn_layer(conv8, "BN8_10", is_training) conv8 = tf.nn.relu(conv8) conv9 = dwconv('dw9',conv8, 3, 512, 1, stride_arr(2)) # 1 conv9 = bn_layer(conv9, "BN9_1", is_training) conv9 = tf.nn.relu(conv9) conv9 = _conv("conv9", conv9, 1, 512, 1024, stride_arr(1)) conv9 = bn_layer(conv9, "BN9_2", is_training) conv9 = tf.nn.relu(conv9) conv10 = dwconv('dw10',conv9, 3, 1024, 1, stride_arr(1)) # 1*1 conv10 = bn_layer(conv10, "BN10_1", is_training) conv10 = tf.nn.relu(conv10) conv10 = _conv("conv10", conv10, 1, 1024, 1024, stride_arr(1)) conv10 = bn_layer(conv10,"BN10_2", is_training) conv10 = tf.nn.relu(conv10) conv10=tf.reduce_mean(conv10,[1,2]) log = output(conv10,10) prediction = tf.nn.softmax(log) # 交叉熵损失 cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y, logits=log)) # 总损失 zloss = cross_entropy # 用AdamOptimizer优化器训练 update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS) with tf.control_dependencies(update_ops): train_step = tf.train.RMSPropOptimizer(learnrate).minimize(zloss) # 计算准确率 labely=tf.argmax(y, 1) labelp=tf.argmax(prediction, 1) correct_prediction = tf.equal(labely, labelp) accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) # tf.cast将数据转换成指定类型 # var_list = tf.trainable_variables() # # g_list = tf.global_variables() # # bn_moving_vars = [g for g in g_list if 'moving_mean' in g.name] # # bn_moving_vars += [g for g in g_list if 'moving_variance' in g.name] # # var_list += bn_moving_vars # saver = tf.train.Saver(var_list=var_list, max_to_keep=1) saver = tf.train.Saver(tf.global_variables()) tfrecord_path = 'train_shuffle.tfrecords' test_path = 'test_shuffle.tfrecords' image_batch, label_batch = createBatch(filename=tfrecord_path, batchsize=128) test_batch, testlabel_batch = createBatch(filename=test_path, batchsize=128) train_acc = [] train_loss = [] test_acc = [] test_loss = [] with tf.Session() as sess: sess.run(tf.group(tf.global_variables_initializer(),tf.local_variables_initializer())) coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(sess=sess, coord=coord) rate = 0.001 for i in range(100): if i<5000: rate=0.1 elif i>=5000 and i<7000: rate=0.01 else: rate=0.001 # 训练模型 img_xs, label_xs = sess.run([image_batch, label_batch]) _, trainacc, loss1 = sess.run([train_step, accuracy, cross_entropy], feed_dict={is_training: True, x: img_xs, y: label_xs, learnrate: rate, }) # _, trainacc, loss1,labely1,labelp1 = sess.run([train_step, accuracy, cross_entropy,labely,labelp], # feed_dict={is_training: True, x: img_xs, y: label_xs, learnrate: rate, # keep_prob: 0.5}) # print("输出数据标签的类型以及大小") # print("实际的标签") # print(labely1) # print("预测的标签") # print(labelp1) print("Itertrainbatch " + str(i) + ", train accuracy= " + str(trainacc) + ",loss=" + str(loss1)) if i != 0: if i % 10== 0: train_acc.append(trainacc) train_loss.append(loss1) img_txs, label_txs = sess.run([test_batch, testlabel_batch]) conv2s,accur, loss2 = sess.run([conv2,accuracy, cross_entropy], feed_dict={is_training: False, x: img_txs, y: label_txs, learnrate: rate, }) # 卷积1 # test_image = np.reshape(conv2s, (128, 16, 16, 64)) # 查看 每个 特征 子图 # visualize_feature_map(test_image[0]) # 查看 叠加后的 特征图 # visualize_feature_map_sum(test_image[0]) # fig2, ax2 = plt.subplots(nrows=1, ncols=64, figsize=(64, 1)) # for i in range(64): # ax2[i].imshow(np.reshape(test_image[0], (64, 1, 16, 16))[i][0]) # test_imagesum=test_image[0] # # plt.title('Conv2 32x14x14') # # plt.show() test_acc.append(accur) test_loss.append(loss2) # print('11111111111111111111111111111111111111111111111111111111111111111111111') # print("输出测试数据标签的类型以及大小") # print("实际测试的标签") # print(labely2) # print("预测测试的标签") # print(labelp2) print("Iter " + str(i) + ", Test accuracy= " + str(accur) + ",loss=" + str(loss2)) # # print('11111111111111111111111111111111111111111111111111111111111111111111111') coord.request_stop() coord.join(threads) # 保存模型 saver.save(sess, 'net/my_net.ckpt') fig = plt.figure() ax1 = fig.add_subplot(111) ax1.plot(test_acc, 'r') ax1.plot(train_acc, 'b') ax1.set_ylabel('accuracy') # ax1.set_title("Double Y axis") ax2 = ax1.twinx() # this is the important function ax2.plot(test_loss, 'r') ax2.plot(train_loss, 'b') ax2.set_ylabel('loss') plt.show() 测试代码:
#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Created on Tue Jan 9 21:46:26 2018 @author: lvedng """ # In[1]: import tensorflow as tf import matplotlib.pyplot as plt import numpy as np from PIL import Image #注意Image,后面会用到 def read_and_decode(filename): # 读入dog_train.tfrecords # filename_queue = tf.train.string_input_producer([filename]) # 生成一个queue队列 filename_queue = tf.train.string_input_producer([filename],shuffle=True,num_epochs=100) # 生成一个queue队列 reader = tf.TFRecordReader() _, serialized_example = reader.read(filename_queue) # 返回文件名和文件 features = tf.parse_single_example(serialized_example, features={ 'label': tf.FixedLenFeature([], tf.int64), 'img_raw': tf.FixedLenFeature([], tf.string), }) # 将image数据和label取出来 img = tf.decode_raw(features['img_raw'], tf.uint8) img = tf.reshape(img, [32 * 32 * 3]) # reshape为128*128的3通道图片 img = tf.cast(img, tf.float32) * (1. / 255) label = tf.cast(features['label'], tf.int32) # 在流中抛出label张量 return img, label def createBatch(filename, batchsize): images, labels = read_and_decode(filename) min_after_dequeue = batchsize capacity = 10* batchsize image_batch, label_batch = tf.train.shuffle_batch([images, labels], batch_size=batchsize, capacity=capacity, min_after_dequeue=min_after_dequeue ) label_batch = tf.one_hot(label_batch, depth=10) return image_batch, label_batch # 实现Batch Normalization def bn_layer(x,name, is_training, moving_decay=0.9, eps=1e-5): # 获取输入维度并判断是否匹配卷积层(4)或者全连接层(2) shape = x.shape assert len(shape) in [2, 4] param_shape = shape[-1] with tf.variable_scope(name): # 声明BN中唯一需要学习的两个参数,y=gamma*x+beta gamma = tf.get_variable("gamma", param_shape, initializer=tf.constant_initializer(1)) beta = tf.get_variable("beta", param_shape, initializer=tf.constant_initializer(0)) # 计算当前整个batch的均值与方差 axes = list(range(len(shape) - 1)) # 特征的通道数 batch_mean, batch_var = tf.nn.moments(x, axes, name='moments') # 采用滑动平均更新均值与方差 ema = tf.train.ExponentialMovingAverage(moving_decay) def mean_var_with_update(): ema_apply_op = ema.apply([batch_mean, batch_var]) with tf.control_dependencies([ema_apply_op]): return tf.identity(batch_mean), tf.identity(batch_var) # 训练时,更新均值与方差,测试时使用之前最后一次保存的均值与方差 mean, var = tf.cond(tf.equal(is_training, True), mean_var_with_update, lambda: (ema.average(batch_mean), ema.average(batch_var))) # 最后执行batch normalization return tf.nn.batch_normalization(x, mean, var, beta, gamma, eps) # 初始化权值 def weight_variable(shape): initial = tf.truncated_normal(shape, stddev=0.1) # 生成一个截断的正态分布 return tf.Variable(initial) # 初始化偏置 def bias_variable(shape): initial = tf.constant(0.1, shape=shape) return tf.Variable(initial) def dwconv(name,input, filter_size, in_filters, mutil_filters, strides): regularizer = tf.contrib.layers.l2_regularizer(scale = 0.0001) n = filter_size * filter_size * mutil_filters with tf.variable_scope(name): weights = tf.get_variable('conv_weights', shape=[filter_size, filter_size, in_filters, mutil_filters], initializer=tf.random_normal_initializer(stddev=np.sqrt(2.0 / n)), regularizer=regularizer) # f2 = tf.Variable(tf.truncated_normal([filter_size, filter_size, in_filters, mutil_filters], stddev=0.1)) return tf.nn.depthwise_conv2d(input, weights, strides, padding="SAME", rate=None, name=None, data_format=None) # 池化层 def max_pool_2x2(x): return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') # 2D卷积 def _conv(name, x, filter_size, in_fliters, out_filters, strides): b = bias_variable([out_filters]) regularizer = tf.contrib.layers.l2_regularizer(scale=0.0001) n = filter_size * filter_size * out_filters with tf.variable_scope(name): # 获取或新建卷积核,正态随机初始化 weights = tf.get_variable('convws', shape=[filter_size, filter_size, in_fliters, out_filters], initializer=tf.random_normal_initializer(stddev=np.sqrt(2.0 / n)), regularizer=regularizer) # kernel = tf.Variable(tf.truncated_normal([filter_size, filter_size, in_fliters, out_filters], stddev=0.1)) out=tf.nn.conv2d(x, weights, strides, padding='SAME') + b # 计算卷积 return out # 把步长值转换成tf.nn.conv2d需要的步长数组 def stride_arr(stride): return [1, stride, stride, 1] def output(x_tensor, num_outputs): #全连接层 x_shape = x_tensor.get_shape().as_list() regularizer = tf.contrib.layers.l2_regularizer(scale = 0.0001) weights = tf.get_variable('weight', shape = [x_shape[1], num_outputs], initializer = tf.uniform_unit_scaling_initializer(factor = 1.0), regularizer = regularizer) bias = tf.Variable(tf.zeros([num_outputs])) out = tf.add(tf.matmul(x_tensor, weights), bias) return out def get_one_image(img_dir): image = Image.open(img_dir) plt.imshow(image) image = image.resize([32,32]) image_arr = np.array(image) return image_arr # 命名空间 lamda = 0.004 # 正则化系数,这里添加了正则化但是没有使用 # 定义两个placeholder x = tf.placeholder(tf.float32, [1, 32 * 32 * 3]) y = tf.placeholder(tf.float32, [1, 10]) # 改变x的格式转为4D的向量[batch, in_height, in_width, in_channels]` x_image = tf.reshape(x, [1, 32, 32, 3]) is_training = tf.placeholder(tf.bool) learnrate = tf.placeholder(tf.float32) conv1 = _conv("conv1", x_image, 3, 3, 32, stride_arr(2)) # 16 conv1 = bn_layer(conv1, "BN1",is_training) conv1 = tf.nn.relu(conv1) conv2 = dwconv('dw2',conv1, 3, 32, 1, stride_arr(1)) conv2 = bn_layer(conv2, "BN2_1", is_training) conv2 = tf.nn.relu(conv2) conv2 = _conv("conv2", conv2, 1, 32, 64, stride_arr(1)) conv2 = bn_layer(conv2, "BN2_2", is_training) conv2 = tf.nn.relu(conv2) conv3 = dwconv('dw3',conv2, 3, 64, 1, stride_arr(2)) # 8 conv3 = bn_layer(conv3, "BN3_1", is_training) conv3 = tf.nn.relu(conv3) conv3 = _conv("conv3", conv3, 1, 64, 128, stride_arr(1)) conv3 = bn_layer(conv3, "BN3_2", is_training) conv3 = tf.nn.relu(conv3) conv4 = dwconv('dw4',conv3, 3, 128, 1, stride_arr(1)) conv4 = bn_layer(conv4, "BN4_1", is_training) conv4 = tf.nn.relu(conv4) conv4 = _conv("conv4", conv4, 1, 128, 128, stride_arr(1)) conv4 = bn_layer(conv4, "BN4_2", is_training) conv4 = tf.nn.relu(conv4) conv5 = dwconv('dw5',conv4, 3, 128, 1, stride_arr(2)) # 4 conv5 = bn_layer(conv5, "BN5_1", is_training) conv5 = tf.nn.relu(conv5) conv5 = _conv("conv5", conv5, 1, 128, 256, stride_arr(1)) conv5 = bn_layer(conv5, "BN5_2", is_training) conv5 = tf.nn.relu(conv5) conv6 = dwconv('dw6',conv5, 3, 256, 1, stride_arr(1)) conv6 = bn_layer(conv6, "BN6_1", is_training) conv6 = tf.nn.relu(conv6) conv6 = _conv("conv6", conv6, 1, 256, 256, stride_arr(1)) conv6 = bn_layer(conv6, "BN6_2", is_training) conv6 = tf.nn.relu(conv6) conv7 = dwconv('dw7',conv6, 3, 256, 1, stride_arr(2)) # 2 conv7 = bn_layer(conv7, "BN7_1", is_training) conv7 = tf.nn.relu(conv7) conv7 = _conv("conv7", conv7, 1, 256, 512, stride_arr(1)) conv7 = bn_layer(conv7, "BN7_2", is_training) conv7 = tf.nn.relu(conv7) #10个重复的层 conv8 = dwconv('dw8_1',conv7, 3, 512, 1, stride_arr(1)) conv8 = bn_layer(conv8, "BN8_1", is_training) conv8 = tf.nn.relu(conv8) conv8 = _conv("conv8_1", conv8, 1, 512, 512, stride_arr(1)) conv8 = bn_layer(conv8, "BN8_2", is_training) conv8 = tf.nn.relu(conv8) conv8 = dwconv('dw8_2',conv8, 3, 512, 1, stride_arr(1)) conv8 = bn_layer(conv8, "BN8_3", is_training) conv8 = tf.nn.relu(conv8) conv8 = _conv("conv8_2", conv8, 1, 512, 512, stride_arr(1)) conv8 = bn_layer(conv8, "BN8_4", is_training) conv8 = tf.nn.relu(conv8) conv8 = dwconv('dw8_3',conv8, 3, 512, 1, stride_arr(1)) conv8 = bn_layer(conv8, "BN8_5", is_training) conv8 = tf.nn.relu(conv8) conv8 = _conv("conv8_3", conv8, 1, 512, 512, stride_arr(1)) conv8 = bn_layer(conv8, "BN8_6", is_training) conv8 = tf.nn.relu(conv8) conv8 = dwconv('dw8_4',conv8, 3, 512, 1, stride_arr(1)) conv8 = bn_layer(conv8, "BN8_7", is_training) conv8 = tf.nn.relu(conv8) conv8 = _conv("conv8_4", conv8, 1, 512, 512, stride_arr(1)) conv8 = bn_layer(conv8, "BN8_8", is_training) conv8 = tf.nn.relu(conv8) conv8 = dwconv('dw8_5',conv8, 3, 512, 1, stride_arr(1)) conv8 = bn_layer(conv8, "BN8_9", is_training) conv8 = tf.nn.relu(conv8) conv8 = _conv("8_5", conv8, 1, 512, 512, stride_arr(1)) conv8 = bn_layer(conv8, "BN8_10", is_training) conv8 = tf.nn.relu(conv8) conv9 = dwconv('dw9',conv8, 3, 512, 1, stride_arr(2)) # 1 conv9 = bn_layer(conv9, "BN9_1", is_training) conv9 = tf.nn.relu(conv9) conv9 = _conv("conv9", conv9, 1, 512, 1024, stride_arr(1)) conv9 = bn_layer(conv9, "BN9_2", is_training) conv9 = tf.nn.relu(conv9) conv10 = dwconv('dw10',conv9, 3, 1024, 1, stride_arr(1)) # 1*1 conv10 = bn_layer(conv10, "BN10_1", is_training) conv10 = tf.nn.relu(conv10) conv10 = _conv("conv10", conv10, 1, 1024, 1024, stride_arr(1)) conv10 = bn_layer(conv10,"BN10_2", is_training) conv10 = tf.nn.relu(conv10) conv10=tf.reduce_mean(conv10,[1,2]) log = output(conv10,10) # 将8 * 8 * 64 三维向量拉直成一行向量 # h_pool2_flat = tf.reshape(conv10, [-1, 1 * 1 * 512]) # # 第一层全连接 # W_fc1 = weight_variable([1 * 1 * 512, 256]) # b_fc1 = bias_variable([256]) # h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1) # # # 对隐藏层使用dropout # # keep_prob = tf.placeholder(tf.float32) # W_fc2 = weight_variable([256, 10]) # b_fc2 = bias_variable([10]) # log = tf.matmul(h_fc1, W_fc2) + b_fc2 prediction = tf.nn.softmax(log) # w1_loss = lamda * tf.nn.l2_loss(W_fc1) # 对W_fc1使用L2正则化 # w2_loss = lamda * tf.nn.l2_loss(W_fc2) # 对W_fc2使用L2正则化 # 交叉熵损失 cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y, logits=log)) # 总损失 zloss = cross_entropy # 用AdamOptimizer优化器训练 update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS) with tf.control_dependencies(update_ops): train_step = tf.train.RMSPropOptimizer(learnrate).minimize(zloss) # 计算准确率 labely=tf.argmax(y, 1) labelp=tf.argmax(prediction, 1) correct_prediction = tf.equal(labely, labelp) accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) # tf.cast将数据转换成指定类型 saver = tf.train.Saver(max_to_keep=1) image_arr = get_one_image('G:\\data\\test\\00000\\3215.png') image_arr=image_arr.reshape([1,32 * 32 * 3]) with tf.Session() as sess: saver.restore(sess, 'net/my_net.ckpt') # 注意此处路径前添加"./" prediction1,labelp1, = sess.run([prediction,labelp], feed_dict={is_training: False,x: image_arr}) print(prediction1) print(labelp1)