def distorted_inputs(): """Construct distorted input for CIFAR training using the Reader ops. Returns: images: Images. 4D tensor of [batch_size, IMAGE_SIZE, IMAGE_SIZE, 3] size. labels: Labels. 1D tensor of [batch_size] size. """ images, labels = cifar10_input.distorted_inputs( batch_size=FLAGS.batch_size) if FLAGS.use_fp16: images = tf.cast(images, tf.float16) labels = tf.cast(labels, tf.float16) return images, labels
def build_model(): print('build model') g = tf.Graph() with g.as_default(), tf.device( tf.train.replica_device_setter(FLAGS.ps_tasks)): # inputs, labels = imagenet_input(is_training=True) inputs, labels = cifar10_input.distorted_inputs( '../cifar10/cifar-10-batches-bin', 100) with slim.arg_scope( mobilenet_v1.mobilenet_v1_arg_scope(is_training=True)): print('default layers') logits, _ = mobilenet_v1.mobilenet_v1( inputs, is_training=True, depth_multiplier=FLAGS.depth_multiplier, num_classes=FLAGS.num_classes, ) print('start train') one_hot_labels = tf.one_hot(tf.cast(labels, dtype=tf.uint8), FLAGS.num_classes, dtype=tf.float32) slim.losses.softmax_cross_entropy(one_hot_labels, logits) if FLAGS.quantize: tf.contrib.quantize.create_training_graph( quant_delay=get_quant_delay()) total_loss = tf.losses.get_total_loss(name='total_loss') # configure the learning rate using an exponential decay num_epochs_per_decay = 2.5 imagenet_size = 1271167 decay_steps = int(imagenet_size / FLAGS.batch_size * num_epochs_per_decay) learning_rate = tf.train.exponential_decay( get_learning_rate(), tf.train.get_or_create_global_step(), decay_steps, _LEARNING_RATE_DECAY_FACTOR, staircase=True) opt = tf.train.GradientDescentOptimizer(learning_rate) train_tensor = slim.learning.create_train_op(total_loss, optimizer=opt) slim.summaries.add_scalar_summary(total_loss, 'total_loss', 'losses') slim.summaries.add_scalar_summary(learning_rate, 'learning_rate', 'traing') return g, train_tensor
def distorted_inputs(): """Construct distorted input for CIFAR training using the Reader ops. Returns: images: Images. 4D tensor of [batch_size, IMAGE_SIZE, IMAGE_SIZE, 3] size. labels: Labels. 1D tensor of [batch_size] size. Raises: ValueError: If no data_dir """ if not FLAGS.data_dir: raise ValueError('Please supply a data_dir') data_dir = os.path.join(FLAGS.data_dir, 'cifar-10-batches-bin') images, labels = cifar10_input.distorted_inputs( data_dir=data_dir, batch_size=FLAGS.batch_size) if FLAGS.use_fp16: images = tf.cast(images, tf.float16) labels = tf.cast(labels, tf.float16) return images, labels
import tensorflow as tf from cifar10 import cifar10_input, cifar10 import os os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' batch_size = 100 # data_dir='D:\\学习笔记\\ai\\dataSets\\cifar10\\cifar-10-binary.tar' data_dir = 'D:\\学习笔记\\ai\\dataSets\\cifar10\\cifar-10-batches-bin' # data_dir='D:\\学习笔记\\ai\\dataSets\\cifar10' cifar10.maybe_download_and_extract() images_train, label_train = cifar10_input.distorted_inputs( data_dir=data_dir, batch_size=batch_size) images_test, label_test = cifar10_input.inputs(eval_data=True, data_dir=data_dir, batch_size=batch_size) X = tf.placeholder(shape=images_train.get_shape(), dtype=tf.float32) y = tf.placeholder(shape=label_train.get_shape(), dtype=tf.int32) w_cnn = tf.Variable(tf.truncated_normal(shape=(4, 4, 3, 24), stddev=0.1)) b_cnn = tf.Variable(tf.ones(shape=24)) cnn = tf.nn.conv2d(X, w_cnn, strides=(1, 1, 1, 1), padding='SAME', name='cnn') cnn = tf.reshape(cnn, shape=(batch_size, -1)) cnn_shape = cnn.get_shape() relu_cnn = tf.nn.relu(cnn) print('cnn shape', cnn_shape) w_fc = tf.Variable(tf.truncated_normal(shape=(13824, 10), stddev=0.1)) b_fc = tf.Variable(tf.ones(shape=10))
cross_entropy_mean = tf.reduce_mean(tf.reduce_sum(cross_entropy)) tf.add_to_collection("losses", cross_entropy_mean) return tf.add_n(tf.get_collection("losses"), name="total_loss") if __name__ == "__main__": #设置最大迭代次数 max_steps = 10000 #设置每次训练的数据大小 batch_size = 128 #下载解压数据 cifar10.maybe_download_and_extract() # 设置数据的存放目录 cifar10_dir = "D:/dataset/cifar10_data/cifar-10-batches-bin" #获取数据增强后的训练集数据 images_train, labels_train = cifar10_input.distorted_inputs( cifar10_dir, batch_size) #获取裁剪后的测试数据 images_test, labels_test = cifar10_input.inputs(eval_data=True, data_dir=cifar10_dir, batch_size=batch_size) #定义模型的输入和输出数据 image_holder = tf.placeholder(dtype=tf.float32, shape=[batch_size, 24, 24, 3]) label_holder = tf.placeholder(dtype=tf.int32, shape=[batch_size]) #设计第一层卷积 weight1 = variable_with_weight_loss(shape=[5, 5, 3, 64], std=5e-2, w1=0) kernel1 = tf.nn.conv2d(image_holder, weight1, [1, 1, 1, 1], padding="SAME") bais1 = tf.Variable(tf.constant(0.0, dtype=tf.float32, shape=[64])) conv1 = tf.nn.relu(tf.nn.bias_add(kernel1, bais1)) pool1 = tf.nn.max_pool(conv1, [1, 3, 3, 1], [1, 2, 2, 1], padding="SAME")
def variable_with_weight_loss(shape, stddev, wl): var = tf.Variable(tf.truncated_normal(shape, stddev=stddev)) if wl is not None: weight_loss = tf.multiply(tf.nn.l2_loss(var), wl, name='weight_loss') tf.add_to_collection('losses', weight_loss) return var # 下载数据集到指定位置 cifar10.maybe_download_and_extract() # 提取训练集和测试集 # distort_inputs函数包含了一些数据增强操作 images_train, labels_train = cifar10_input.distorted_inputs(eval_data=True, data_dir=data_dir, batch_size=batch_size) # 测试集数据正常读取,不做增强处理 images_test, labels_test = cifar10_input.inputs(eval_data=True, data_dir=data_dir, batch_size=batch_size) # 设置输入数据承接 image_holder = tf.placeholder(tf.float32, [batch_size, 24, 24, 3]) label_holder = tf.placeholder(tf.float32, [batch_size]) # 第一层卷积操作 # 初始化卷积核,第一层卷积核不进行L2正则化,大小为5*5,3通道,64个卷积核,标准差为0.05 weight1 = variable_with_weight_loss(shape=[5, 5, 3, 64], stddev=5e-2, wl=0.0) # 第一次卷积两个步长都为1,padding值为SAME kernel1 = tf.nn.conv2d(image_holder, weight1, [1, 1, 1, 1], padding='SAME') # 偏值为64个,初始化为0 bias1 = tf.Variable(tf.constant(0.0, shape=[64])) # 激活函数进行非线性化
batch_size = 128 data_dir = 'F:\project\MachineLearning\DeepLearning\Data\cifar10' def variable_with_weight_loss(shape, stddev, w1): var = tf.Variable(tf.truncated_normal(shape, stddev=stddev)) if w1 is not None: weight_loss = tf.multiply(tf.nn.l2_loss(var), w1, name='weight_loss') tf.add_to_collection('losses', weight_loss) return var maybe_download_and_extract() # 下载解压数据 # 使用distorted_inputs 产生训练需要的数据,包括特征及其对应的label, 该方法做了数据增强 images_train, labels_train = distorted_inputs(data_dir=data_dir, batch_size=batch_size) # cifar10_input.inputs() 生成测试数据 image_test, labels_test = inputs(eval_data=True, data_dir=data_dir, batch_size=batch_size) image_holder = tf.placeholder(tf.float32, [batch_size, 24, 24, 3]) # 3表示有RGB三种通道 label_holder = tf.placeholder(tf.int32, [batch_size]) # 创建卷积层,注意LRN的理解 # 使用 5x5的卷积核大小,3个颜色通道,64个卷积核 # ============== # 卷积层对数据进行特征提取, 全连接层进行组合匹配
def debug(): g = tf.Graph() inputs = tf.placeholder(shape=[None, 28, 28, 3], dtype=tf.float32) labels = tf.placeholder(shape=[ None, ], dtype=tf.float32) with g.as_default(), tf.device( tf.train.replica_device_setter(FLAGS.ps_tasks)): # inputs, labels = imagenet_input(is_training=True) tf_inputs, tf_labels = cifar10_input.distorted_inputs( '../cifar10/cifar-10-batches-bin', 100) with slim.arg_scope( mobilenet_v1.mobilenet_v1_arg_scope(is_training=True)): logits, _ = mobilenet_v1.mobilenet_v1( inputs, is_training=True, depth_multiplier=FLAGS.depth_multiplier, num_classes=FLAGS.num_classes, ) one_hot_labels = tf.one_hot(tf.cast(labels, dtype=tf.uint8), FLAGS.num_classes, dtype=tf.float32) slim.losses.softmax_cross_entropy(one_hot_labels, logits) if FLAGS.quantize: tf.contrib.quantize.create_training_graph( quant_delay=get_quant_delay()) total_loss = tf.losses.get_total_loss(name='total_loss') # configure the learning rate using an exponential decay num_epochs_per_decay = 2.5 imagenet_size = 1271167 decay_steps = int(imagenet_size / FLAGS.batch_size * num_epochs_per_decay) learning_rate = tf.train.exponential_decay( get_learning_rate(), tf.train.get_or_create_global_step(), decay_steps, _LEARNING_RATE_DECAY_FACTOR, staircase=True) opt = tf.train.GradientDescentOptimizer(learning_rate) train_tensor = slim.learning.create_train_op(total_loss, optimizer=opt) slim.summaries.add_scalar_summary(total_loss, 'total_loss', 'losses') slim.summaries.add_scalar_summary(learning_rate, 'learning_rate', 'traing') init_op = [ tf.global_variables_initializer(), tf.local_variables_initializer() ] # accuracy test_inputs, test_labels = cifar10_input.distorted_inputs( '../cifar10/cifar-10-batches-bin', 1000, is_validdata=True) accuracy = tf.metrics.accuracy(tf.argmax(logits, axis=1), test_labels) with tf.Session(graph=g) as session: session.run(init_op) for step in range(10000): _loss, _ = session.run([total_loss, train_tensor], feed_dict={ inputs: tf_inputs, labels: tf_labels, }) if step % 100 == 0: _accuracy = session.run(accuracy, feed_dict={ inputs: test_inputs, labels: test_labels }) print('step: %d, loss: %s, accuracy: %s' % (step, _loss, _accuracy))