VggNet Mnist训练

#coding:utf-8
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
import numpy as np
import time

#paras
n_classes = 10
# Training Parameters
learning_rate = 0.001
num_steps = 20000
batch_size = 128
display_step = 10

X = tf.placeholder(tf.float32,[None,28*28])
y = tf.placeholder(tf.float32,[None,n_classes])

#build vgg16 model
x = tf.reshape(X,[-1,28,28,1])
tf.summary.image('x',x)
#conv_1
with tf.name_scope('conv1_1') as scope:
    kernel = tf.Variable(tf.truncated_normal([3,3,1,16],dtype=tf.float32,stddev=1e-1),name='weights')
    conv = tf.nn.conv2d(x,kernel,[1,1,1,1],padding='SAME')
    biases = tf.Variable(tf.constant(0.0,shape=[16],dtype=tf.float32),trainable=True,name='biases')
    out = tf.nn.bias_add(conv,biases)
    conv1_1 = tf.nn.relu(out, name='scope')
with tf.name_scope('conv1_2') as scope:
    kernel = tf.Variable(tf.truncated_normal([3,3,16,16],dtype=tf.float32,stddev=1e-1),name='weights')
    conv = tf.nn.conv2d(conv1_1,kernel,[1,1,1,1],padding='SAME')
    biases = tf.Variable(tf.constant(0.0,shape=[16],dtype=tf.float32),trainable=True,name='biases')
    out = tf.nn.bias_add(conv,biases)
    conv1_2 = tf.nn.relu(out,name='scope')
#pool1
pool_1 = tf.nn.max_pool(conv1_2,ksize=[1,2,2,1],strides=[1,2,2,1],padding='SAME',name='pool_1')

#conv_2
with tf.name_scope('conv2_1') as scope:
    kernel = tf.Variable(tf.truncated_normal([3,3,16,32],dtype=tf.float32,stddev=1e-1),name='weights')
    conv = tf.nn.conv2d(pool_1,kernel,[1,1,1,1],padding='SAME')
    biases = tf.Variable(tf.constant(0.0,shape=[32],dtype=tf.float32),trainable=True,name='biases')
    out = tf.nn.bias_add(conv,biases)
    conv2_1 = tf.nn.relu(out,name='scope')
with tf.name_scope('conv2_2') as scope:
    kernel = tf.Variable(tf.truncated_normal([3,3,32,32],dtype=tf.float32,stddev=1e-1),name='weights')
    conv = tf.nn.conv2d(conv2_1,kernel,[1,1,1,1],padding='SAME')
    biases = tf.Variable(tf.constant(0.0,shape=[32],dtype=tf.float32),trainable=True,name='biases')
    out = tf.nn.bias_add(conv,biases)
    conv2_2 = tf.nn.relu(out,name='scope')

#pool2
pool_2 = tf.nn.max_pool(conv2_2,ksize=[1,2,2,1],strides=[1,2,2,1],padding='SAME',name='pool_1')

#conv_3
with tf.name_scope('conv3_1') as scope:
    kernel = tf.Variable(tf.truncated_normal([3, 3, 32, 64], dtype=tf.float32, stddev=1e-1), name='weights')
    conv = tf.nn.conv2d(pool_2, kernel, [1, 1, 1, 1], padding='SAME')
    biases = tf.Variable(tf.constant(0.0, shape=[64], dtype=tf.float32), trainable=True, name='biases')
    out = tf.nn.bias_add(conv, biases)
    conv3_1 = tf.nn.relu(out, name='scope')
with tf.name_scope('conv3_2') as scope:
    kernel = tf.Variable(tf.truncated_normal([3, 3, 64, 64], dtype=tf.float32, stddev=1e-1), name='weights')
    conv = tf.nn.conv2d(conv3_1, kernel, [1, 1, 1, 1], padding='SAME')
    biases = tf.Variable(tf.constant(0.0, shape=[64], dtype=tf.float32), trainable=True, name='biases')
    out = tf.nn.bias_add(conv, biases)
    conv3_2 = tf.nn.relu(out, name='scope')
with tf.name_scope('conv3_3') as scope:
    kernel = tf.Variable(tf.truncated_normal([3, 3, 64, 64], dtype=tf.float32, stddev=1e-1), name='weights')
    conv = tf.nn.conv2d(conv3_2, kernel, [1, 1, 1, 1], padding='SAME')
    biases = tf.Variable(tf.constant(0.0, shape=[64], dtype=tf.float32), trainable=True, name='biases')
    out = tf.nn.bias_add(conv, biases)
    conv3_3 = tf.nn.relu(out, name='scope')
#pool_3
pool_3 = tf.nn.max_pool(conv3_3,ksize=[1,2,2,1],strides=[1,2,2,1],padding='SAME',name='pool_3')

# conv_4
with tf.name_scope('conv4_1') as scope:
   kernel = tf.Variable(tf.truncated_normal([3, 3, 64, 128], dtype=tf.float32, stddev=1e-1), name='weights')
   conv = tf.nn.conv2d(pool_3, kernel, [1, 1, 1, 1], padding='SAME')
   biases = tf.Variable(tf.constant(0.0, shape=[128], dtype=tf.float32), trainable=True, name='biases')
   out = tf.nn.bias_add(conv, biases)
   conv4_1 = tf.nn.relu(out, name='scope')
with tf.name_scope('conv4_2') as scope:
    kernel = tf.Variable(tf.truncated_normal([3, 3, 128, 128], dtype=tf.float32, stddev=1e-1), name='weights')
    conv = tf.nn.conv2d(conv4_1, kernel, [1, 1, 1, 1], padding='SAME')
    biases = tf.Variable(tf.constant(0.0, shape=[128], dtype=tf.float32), trainable=True, name='biases')
    out = tf.nn.bias_add(conv, biases)
    conv4_2 = tf.nn.relu(out, name='scope')
with tf.name_scope('conv4_3') as scope:
    kernel = tf.Variable(tf.truncated_normal([3, 3, 128, 128], dtype=tf.float32, stddev=1e-1), name='weights')
    conv = tf.nn.conv2d(conv4_2, kernel, [1, 1, 1, 1], padding='SAME')
    biases = tf.Variable(tf.constant(0.0, shape=[128], dtype=tf.float32), trainable=True, name='biases')
    out = tf.nn.bias_add(conv, biases)
    conv4_3 = tf.nn.relu(out, name='scope')
# pool_4
pool_4 = tf.nn.max_pool(conv4_3, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME', name='pool_4')

# conv_5
with tf.name_scope('conv5_1') as scope:
    kernel = tf.Variable(tf.truncated_normal([3, 3, 128, 256], dtype=tf.float32, stddev=1e-1), name='weights')
    conv = tf.nn.conv2d(pool_4, kernel, [1, 1, 1, 1], padding='SAME')
    biases = tf.Variable(tf.constant(0.0, shape=[256], dtype=tf.float32), trainable=True, name='biases')
    out = tf.nn.bias_add(conv, biases)
    conv5_1 = tf.nn.relu(out, name='scope')
with tf.name_scope('conv5_2') as scope:
    kernel = tf.Variable(tf.truncated_normal([3, 3, 256, 256], dtype=tf.float32, stddev=1e-1), name='weights')
    conv = tf.nn.conv2d(conv5_1, kernel, [1, 1, 1, 1], padding='SAME')
    biases = tf.Variable(tf.constant(0.0, shape=[256], dtype=tf.float32), trainable=True, name='biases')
    out = tf.nn.bias_add(conv, biases)
    conv5_2 = tf.nn.relu(out, name='scope')
with tf.name_scope('conv5_3') as scope:
    kernel = tf.Variable(tf.truncated_normal([3, 3, 256, 256], dtype=tf.float32, stddev=1e-1), name='weights')
    conv = tf.nn.conv2d(conv5_2, kernel, [1, 1, 1, 1], padding='SAME')
    biases = tf.Variable(tf.constant(0.0, shape=[256], dtype=tf.float32), trainable=True, name='biases')
    out = tf.nn.bias_add(conv, biases)
    conv5_3 = tf.nn.relu(out, name='scope')
# pool_5
pool_5 = tf.nn.max_pool(conv5_3, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME', name='pool_5')

#fc1
with tf.name_scope('fc1') as scope:
    shape = int(np.prod(pool_5.get_shape()[1:]))
    fc1w = tf.Variable(tf.truncated_normal([shape,100],dtype=tf.float32,stddev=1e-1),name='weights')
    fc1b = tf.Variable(tf.constant(1.0,shape=[100],dtype=tf.float32),trainable=True,name='biases')
    pool5_flat = tf.reshape(pool_5,[-1,shape])
    fc11 = tf.nn.bias_add(tf.matmul(pool5_flat,fc1w),fc1b)
    fc1 = tf.nn.relu(fc11,name='scope')
#fc2
with tf.name_scope('fc2') as scope:
    fc2w = tf.Variable(tf.truncated_normal([100,100],dtype=tf.float32,stddev=1e-1),name='weights')
    fc2b = tf.Variable(tf.constant(1.0,shape=[100],dtype=tf.float32),trainable=True,name='biases')
    fc21 = tf.nn.bias_add(tf.matmul(fc1,fc2w),fc2b)
    fc2 = tf.nn.relu(fc21,name='scope')
#fc3
with tf.name_scope('fc3') as scope:
    fc3w = tf.Variable(tf.truncated_normal([100, 10], dtype=tf.float32, stddev=1e-1), name='weights')
    fc3b = tf.Variable(tf.constant(1.0, shape=[10], dtype=tf.float32), trainable=True, name='biases')
    fc31 = tf.nn.bias_add(tf.matmul(fc2, fc3w), fc3b,name='scope')

mnist = input_data.read_data_sets('../data/',one_hot=True)


prediction = tf.nn.softmax(fc31)

#tensorboard事件保存地址
log_dir = './tensorboard/'
# Define loss and optimizer
with tf.name_scope('loss'):
    loss_op = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=fc31, labels=y))
    optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate)
    train_op = optimizer.minimize(loss_op)

#evaluate model
with tf.name_scope('accuracy'):
    correct_pred = tf.equal(tf.argmax(prediction,1),tf.argmax(y,1))
    accuracy = tf.reduce_mean(tf.cast(correct_pred,tf.float32))
#tensorboard 
tf.summary.scalar('loss',loss_op)
tf.summary.scalar('accuracy',accuracy)
merged_summary_op = tf.summary.merge_all()
summary_writer = tf.summary.FileWriter(log_dir,graph=tf.get_default_graph())
# initialize the variables
init = tf.global_variables_initializer()

# Start training
with tf.Session() as sess:

    # Run the initializer
    sess.run(init)

    for step in range(1, num_steps+1):
        batch_x, batch_y = mnist.train.next_batch(batch_size)
        # Run optimization op (backprop)
        sess.run(train_op, feed_dict={X: batch_x, y: batch_y})
        if step % display_step == 0 or step == 1:
            # Calculate batch loss and accuracy
            loss, acc,summary = sess.run([loss_op, accuracy,merged_summary_op], feed_dict={X: batch_x, y: batch_y})
            summary_writer.add_summary(summary,step)
            print("Step " + str(step) + ", Minibatch Loss= " + \
                  "{:.4f}".format(loss) + ", Training Accuracy= " + \
                  "{:.3f}".format(acc))
    print("Optimization Finished!")

    # Calculate accuracy for 256 MNIST test images
    print("Testing Accuracy:", \
        sess.run(accuracy, feed_dict={X: mnist.test.images[:256],
                                      y: mnist.test.labels[:256]}))
print("run the tensorbord:tensorboard --logdir=./tensorboard")

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
这篇博客使用TensorFlow框架,使用预训练模型进行猫狗分类。代码在Github上已经公开,可以从[这里](https://github.com/jmhIcoding/dogsVScats)获取。 使用预训练模型进行微调的代码如下,其中包括了数据处理、模型构建和训练三个部分: 数据处理部分[^1]: ```python import tensorflow as tf slim = tf.contrib.slim ... def get_dataset(dataset_name, split_name, dataset_dir, file_pattern): """ 获取指定数据集和指定数据集中的数据切分 """ file_pattern = os.path.join(dataset_dir, file_pattern % split_name) if dataset_name == 'imagenet': return dataset.get_split(split_name, dataset_dir, file_pattern) elif dataset_name == 'flowers': return flowers.get_split(split_name, dataset_dir, file_pattern) elif dataset_name == 'cifar10': return cifar10.get_split(split_name, dataset_dir, file_pattern) elif dataset_name == 'mnist': return mnist.get_split(split_name, dataset_dir, file_pattern) elif dataset_name == 'cats_vs_dogs': return dogs.get_split(split_name, dataset_dir, file_pattern) else: raise ValueError('Invalid dataset name %s.' % dataset_name) def load_batch(dataset, batch_size, height, width, is_training=True): """ 加载一批数据 """ data_provider = slim.dataset_data_provider.DatasetDataProvider( dataset, shuffle=is_training, common_queue_capacity=2 * batch_size, common_queue_min=batch_size) image_raw, label = data_provider.get(['image', 'label']) image = inception_preprocessing.preprocess_image( image_raw, height, width, is_training=is_training) images, labels = tf.train.batch( [image, label], batch_size=batch_size, num_threads=4, capacity=5 * batch_size) return images, labels ``` 模型构建部分: ```python import tensorflow as tf slim = tf.contrib.slim ... def build_model(inputs, num_classes, is_training=True, scope='vgg_16'): """ 构建VGG16模型 """ with tf.variable_scope(scope, 'vgg_16', [inputs]) as sc: end_points_collection = sc.name + '_end_points' with slim.arg_scope([slim.conv2d, slim.fully_connected, slim.max_pool2d], outputs_collections=end_points_collection): net = slim.repeat(inputs, 2, slim.conv2d, 64, [3, 3], scope='conv1') net = slim.max_pool2d(net, [2, 2], scope='pool1') ... net = slim.repeat(net, 2, slim.conv2d, 512, [3, 3], scope='conv5') net = slim.max_pool2d(net, [2, 2], scope='pool5') net = slim.conv2d(net, 4096, [7, 7], padding='VALID', scope='fc6') net = slim.dropout(net, 0.5, is_training=is_training, scope='dropout6') net = slim.conv2d(net, 4096, [1, 1], scope='fc7') net = slim.dropout(net, 0.5, is_training=is_training, scope='dropout7') net = slim.conv2d(net, num_classes, [1, 1], activation_fn=None, normalizer_fn=None, scope='fc8') end_points = slim.utils.convert_collection_to_dict(end_points_collection) return net, end_points ``` 训练部分: ```python import tensorflow as tf slim = tf.contrib.slim ... def run_training(dataset_name, train_dir, dataset_dir, num_classes=2, batch_size=32, num_epochs=10, initial_learning_rate=0.0001): """ 训练模型 """ with tf.Graph().as_default(): tf.logging.set_verbosity(tf.logging.INFO) # 获取数据集 dataset = get_dataset(dataset_name, 'train', dataset_dir, '%s_*.tfrecord') images, labels = load_batch(dataset, batch_size=batch_size, height=224, width=224, is_training=True) # 构建网络 logits, end_points = build_model(images, num_classes=num_classes, is_training=True) # 定义损失函数 one_hot_labels = slim.one_hot_encoding(labels, num_classes) slim.losses.softmax_cross_entropy(logits=logits, onehot_labels=one_hot_labels) total_loss = slim.losses.get_total_loss() # 定义优化器 global_step = tf.train.get_or_create_global_step() learning_rate = tf.train.exponential_decay( initial_learning_rate, global_step, decay_steps=1000, decay_rate=0.96, staircase=True) optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate) train_op = slim.learning.create_train_op(total_loss, optimizer, global_step=global_step) # 进行训练 saver = tf.train.Saver(tf.global_variables()) slim.learning.train( train_op, train_dir, log_every_n_steps=1, save_summaries_secs=20, saver=saver, number_of_steps=num_epochs * dataset.num_samples // batch_size, save_interval_secs=120) if __name__ == '__main__': run_training('cats_vs_dogs', '/tmp/cats_vs_dogs', '/path/to/dataset') ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值