tensorflow实现Alexnet resnetv1 mobilenetv1 mobilenetv2的分类训练

174 阅读2分钟

Alexnet、resenetv1、mobilenetv2网络实现,基本的架构(backbone)使用slim的实现,slim是tensorflow的一个库,这个库实现了一些经典的网络架构,如Alexnet,resnet,inception系列等等。

在slim库中实现的Alexnet不同的是,没有使用LRN,并且把全连接层替换成卷积层。

  1. 代码实现多GPU训练
  2. 读入数据采用多进程的方式
  3. 损失函数采用交叉熵加L2正则
  4. 训练样本采用的是猫狗大战读入数据采用多进程的方式

数据集链接:pan.baidu.com/s/1GoFCwLs8… 密码:pxkx
Alexnet代码实现

`#coding:utf-8 import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data mnist = input_data.read_data_sets('MNIST_data',one_hot=True) sess = tf.InteractiveSession() #在运行之前先定义一些操作,Session必须在所有操作全部定义完才能是运行

#初始化权值和偏重 def weight_variabe(shape): initial = tf.truncated_normal(shape, stddev=0.1)#产生截断的正态分布张量,shape表示产生的张量维度,mean为均值,stddev为标准差 return tf.Variable(initial)

def bias_variable(shape): initial = tf.constant(0.1, shape = shape) return tf.Variable(initial)

#卷积和池化层实现 def conv2d(x,W): return tf.nn.conv2d(x,W, strides = [1,1,1,1], padding = 'SAME')

def max_pool_3x3(x): return tf.nn.max_pool(x,ksize = [1,3,3,1],strides = [1,2,2,1], padding = 'SAME')

#LRN 局部响应归一化层 def norm(x,lsize = 4): return tf.nn.lrn(x,lsize,bias = 1.0,aplha = 0.001/9.0 ,beta = 0.75)

#构建卷积层 x = tf.placeholder(tf.float32, [None,784]) y_ = tf.placeholder(tf.float32,[None,10]) keep_prob = tf.placeholder(tf.float32) x_image = tf.reshape(x,[-1,28,28,1])

W_conv1 = weight_variabe([3,3,1,64]) b_conv1 = bias_variable([64]) h_conv1 = tf.nn.relu(conv2d(x_image,W_conv1) + b_conv1) h_pool1 = max_pool_3x3(h_conv1) h_norm1 = norm(h_pool1, lsize=4)

W_conv2 = weight_variabe([3,3,64,128]) b_conv2 = bias_variable([128]) h_conv2 = tf.nn.relu(conv2d(h_norm1,W_conv2) + b_conv2) h_pool2 = max_pool_3x3(h_conv2) h_norm2 = norm(h_pool2, lsize=4)

W_conv3 = weight_variabe([3,3,128,256]) b_conv3 = bias_variable([256]) h_conv3 = tf.nn.relu(conv2d(h_pool2,W_conv3) + b_conv3) h_pool3 = max_pool_3x3(h_conv3) h_norm3 = norm(h_pool3, lsize=4)

#实现全连接和Dropout W_fc1 = weight_variabe([3,3,128,256]) b_fc1 = bias_variable([1024]) h_norm3_flat = tf.reshape(h_norm3,[-1,44256]) h_fc1 = tf.nn.relu(tf.matmul(h_norm3_flat,W_fc1) + b_fc1) h_fc1_drop = tf.nn.dropout(h_fc1,keep_prob)

W_fc2 = weight_variabe([1024,1024]) b_fc2 = bias_variable([1024]) h_fc2 = tf.nn.relu(tf.matmul(h_fc1_drop,W_fc2) + b_fc2) h_fc2_drop = tf.nn.dropout(h_fc2 , keep_prob)

#实现Readout层 W_fc3 = weight_variabe([1024,0]) b_fc3 = bias_variable([10]) y_conv = tf.matmul(h_fc2_drop,W_fc3) +b_fc3

#参数训练与模型评估 cross_entropy = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits(labes = y_,logits=y_conv) ) train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy) correct_prediction = tf.equal(tf.argmax(y_conv,1),tf.argmax(y_,1)) accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))

sess.run(tf.global_variables_initializer()) for i in range(20000): batch = mnist.next_batch(50) if i%100 == 0: train_accuracy = accuracy.eval(feed_dict={ x:batch[0] , y_:batch[1] , keep_prob:1.0 }) print('step %d,training accuracy %g'%(i,train_accuracy)) train_step.run(feed_dict = {x:batch[0], y_:batch[1] , keep_prob : 0.5})

print('test accuracy %g'% accuracy.eval(feed_dict = { x:mnist.test.images, y_:mnist.test.labels , keep_prob:1.0 })) sess.close()`

参考:

www.jianshu.com/p/4c261534a…

github.com/liuheng92/A…

blog.csdn.net/matrix_spac…

www.jianshu.com/p/1cf3b543a…

blog.csdn.net/zj360202/ar…

blog.csdn.net/weixin_4180…