tensorflow学习使用路线
扫描二维码
随时随地手机看文章
一、学习路线
个人感觉对于任何一个深度学习库,如mxnet、tensorflow、theano、caffe等,基本上我都采用同样的一个学习流程,大体流程如下:
(1)训练阶段:数据打包-》网络构建、训练-》模型保存-》可视化查看损失函数、验证精度
(2)测试阶段:模型加载-》测试图片读取-》预测显示结果
(3)移植阶段:量化、压缩加速-》微调-》C++移植打包-》上线
这边我就以tensorflow为例子,讲解整个流程的大体架构,完成一个深度学习项目所需要熟悉的过程代码。
二、训练、测试阶段
1、tensorflow打包数据
这一步对于tensorflow来说,也可以直接自己在线读取:.jpg图片、标签文件等,然后通过phaceholder变量,把数据送入网络中,进行计算。
不过这种效率比较低,对于大规模训练数据来说,我们需要一个比较高效的方式,tensorflow建议我们采用tfrecoder进行高效数据读取。学习tensorflow一定要学会tfrecoder文件写入、读取,具体示例代码如下:
[python]view plaincopy#coding=utf-8 #tensorflow高效数据读取训练 importtensorflowastf importcv2 #把train.txt文件格式,每一行:图片路径名类别标签 #奖数据打包,转换成tfrecords格式,以便后续高效读取 defencode_to_tfrecords(lable_file,data_root,new_name='data.tfrecords',resize=None): writer=tf.python_io.TFRecordWriter(data_root+'/'+new_name) num_example=0 withopen(lable_file,'r')asf: forlinf.readlines(): l=l.split() image=cv2.imread(data_root+"/"+l[0]) ifresizeisnotNone: image=cv2.resize(image,resize)#为了 height,width,nchannel=image.shape label=int(l[1]) example=tf.train.Example(features=tf.train.Features(feature={ 'height':tf.train.Feature(int64_list=tf.train.Int64List(value=[height])), 'width':tf.train.Feature(int64_list=tf.train.Int64List(value=[width])), 'nchannel':tf.train.Feature(int64_list=tf.train.Int64List(value=[nchannel])), 'image':tf.train.Feature(bytes_list=tf.train.BytesList(value=[image.tobytes()])), 'label':tf.train.Feature(int64_list=tf.train.Int64List(value=[label])) })) serialized=example.SerializeToString() writer.write(serialized) num_example+=1 printlable_file,"样本数据量:",num_example writer.close() #读取tfrecords文件 defdecode_from_tfrecords(filename,num_epoch=None): filename_queue=tf.train.string_input_producer([filename],num_epochs=num_epoch)#因为有的训练数据过于庞大,被分成了很多个文件,所以第一个参数就是文件列表名参数 reader=tf.TFRecordReader() _,serialized=reader.read(filename_queue) example=tf.parse_single_example(serialized,features={ 'height':tf.FixedLenFeature([],tf.int64), 'width':tf.FixedLenFeature([],tf.int64), 'nchannel':tf.FixedLenFeature([],tf.int64), 'image':tf.FixedLenFeature([],tf.string), 'label':tf.FixedLenFeature([],tf.int64) }) label=tf.cast(example['label'],tf.int32) image=tf.decode_raw(example['image'],tf.uint8) image=tf.reshape(image,tf.pack([ tf.cast(example['height'],tf.int32), tf.cast(example['width'],tf.int32), tf.cast(example['nchannel'],tf.int32)])) #label=example['label'] returnimage,label #根据队列流数据格式,解压出一张图片后,输入一张图片,对其做预处理、及样本随机扩充 defget_batch(image,label,batch_size,crop_size): #数据扩充变换 distorted_image=tf.random_crop(image,[crop_size,crop_size,3])#随机裁剪 distorted_image=tf.image.random_flip_up_down(distorted_image)#上下随机翻转 #distorted_image=tf.image.random_brightness(distorted_image,max_delta=63)#亮度变化 #distorted_image=tf.image.random_contrast(distorted_image,lower=0.2,upper=1.8)#对比度变化 #生成batch #shuffle_batch的参数:capacity用于定义shuttle的范围,如果是对整个训练数据集,获取batch,那么capacity就应该够大 #保证数据打的足够乱 images,label_batch=tf.train.shuffle_batch([distorted_image,label],batch_size=batch_size, num_threads=16,capacity=50000,min_after_dequeue=10000) #images,label_batch=tf.train.batch([distorted_image,label],batch_size=batch_size) #调试显示 #tf.image_summary('images',images) returnimages,tf.reshape(label_batch,[batch_size]) #这个是用于测试阶段,使用的get_batch函数 defget_test_batch(image,label,batch_size,crop_size): #数据扩充变换 distorted_image=tf.image.central_crop(image,39./45.) distorted_image=tf.random_crop(distorted_image,[crop_size,crop_size,3])#随机裁剪 images,label_batch=tf.train.batch([distorted_image,label],batch_size=batch_size) returnimages,tf.reshape(label_batch,[batch_size]) #测试上面的压缩、解压代码 deftest(): encode_to_tfrecords("data/train.txt","data",(100,100)) image,label=decode_from_tfrecords('data/data.tfrecords') batch_image,batch_label=get_batch(image,label,3)#batch生成测试 init=tf.initialize_all_variables() withtf.Session()assession: session.run(init) coord=tf.train.Coordinator() threads=tf.train.start_queue_runners(coord=coord) forlinrange(100000):#每run一次,就会指向下一个样本,一直循环 #image_np,label_np=session.run([image,label])#每调用run一次,那么 '''''cv2.imshow("temp",image_np) cv2.waitKey()''' #printlabel_np #printimage_np.shape batch_image_np,batch_label_np=session.run([batch_image,batch_label]) printbatch_image_np.shape printbatch_label_np.shape coord.request_stop()#queue需要关闭,否则报错 coord.join(threads) #test()
2、网络架构与训练
经过上面的数据格式处理,接着我们只要写一写网络结构、网络优化方法,把数据搞进网络中就可以了,具体示例代码如下:
[python]view
plaincopy#coding=utf-8
importtensorflowastf
fromdata_encoder_decoederimportencode_to_tfrecords,decode_from_tfrecords,get_batch,get_test_batch
importcv2
importos
classnetwork(object):
def__init__(self):
withtf.variable_scope("weights"):
self.weights={
#39*39*3->36*36*20->18*18*20
'conv1':tf.get_variable('conv1',[4,4,3,20],initializer=tf.contrib.layers.xavier_initializer_conv2d()),
#18*18*20->16*16*40->8*8*40
'conv2':tf.get_variable('conv2',[3,3,20,40],initializer=tf.contrib.layers.xavier_initializer_conv2d()),
#8*8*40->6*6*60->3*3*60
'conv3':tf.get_variable('conv3',[3,3,40,60],initializer=tf.contrib.layers.xavier_initializer_conv2d()),
#3*3*60->120
'fc1':tf.get_variable('fc1',[3*3*60,120],initializer=tf.contrib.layers.xavier_initializer()),
#120->6
'fc2':tf.get_variable('fc2',[120,6],initializer=tf.contrib.layers.xavier_initializer()),
}
withtf.variable_scope("biases"):
self.biases={
'conv1':tf.get_variable('conv1',[20,],initializer=tf.constant_initializer(value=0.0,dtype=tf.float32)),
'conv2':tf.get_variable('conv2',[40,],initializer=tf.constant_initializer(value=0.0,dtype=tf.float32)),
'conv3':tf.get_variable('conv3',[60,],initializer=tf.constant_initializer(value=0.0,dtype=tf.float32)),
'fc1':tf.get_variable('fc1',[120,],initializer=tf.constant_initializer(value=0.0,dtype=tf.float32)),
'fc2':tf.get_variable('fc2',[6,],initializer=tf.constant_initializer(value=0.0,dtype=tf.float32))
}
definference(self,images):
#向量转为矩阵
images=tf.reshape(images,shape=[-1,39,39,3])#[batch,in_height,in_width,in_channels]
images=(tf.cast(images,tf.float32)/255.-0.5)*2#归一化处理
#第一层
conv1=tf.nn.bias_add(tf.nn.conv2d(images,self.weights['conv1'],strides=[1,1,1,1],padding='VALID'),
self.biases['conv1'])
relu1=tf.nn.relu(conv1)
pool1=tf.nn.max_pool(relu1,ksize=[1,2,2,1],strides=[1,2,2,1],padding='VALID')
#第二层
conv2=tf.nn.bias_add(tf.nn.conv2d(pool1,self.weights['conv2'],strides=[1,1,1,1],padding='VALID'),
self.biases['conv2'])
relu2=tf.nn.relu(conv2)
pool2=tf.nn.max_pool(relu2,ksize=[1,2,2,1],strides=[1,2,2,1],padding='VALID')
#第三层
conv3=tf.nn.bias_add(tf.nn.conv2d(pool2,self.weights['conv3'],strides=[1,1,1,1],padding='VALID'),
self.biases['conv3'])
relu3=tf.nn.relu(conv3)
pool3=tf.nn.max_pool(relu3,ksize=[1,2,2,1],strides=[1,2,2,1],padding='VALID')
#全连接层1,先把特征图转为向量
flatten=tf.reshape(pool3,[-1,self.weights['fc1'].get_shape().as_list()[0]])
drop1=tf.nn.dropout(flatten,0.5)
fc1=tf.matmul(drop1,self.weights['fc1'])+self.biases['fc1']
fc_relu1=tf.nn.relu(fc1)
fc2=tf.matmul(fc_relu1,self.weights['fc2'])+self.biases['fc2']
returnfc2
definference_test(self,images):
#向量转为矩阵
images=tf.reshape(images,shape=[-1,39,39,3])#[batch,in_height,in_width,in_channels]
images=(tf.cast(images,tf.float32)/255.-0.5)*2#归一化处理
#第一层
conv1=tf.nn.bias_add(tf.nn.conv2d(images,self.weights['conv1'],strides=[1,1,1,1],padding='VALID'),
self.biases['conv1'])
relu1=tf.nn.relu(conv1)
pool1=tf.nn.max_pool(relu1,ksize=[1,2,2,1],strides=[1,2,2,1],padding='VALID')
#第二层
conv2=tf.nn.bias_add(tf.nn.conv2d(pool1,self.weights['conv2'],strides=[1,1,1,1],padding='VALID'),
self.biases['conv2'])
relu2=tf.nn.relu(conv2)
pool2=tf.nn.max_pool(relu2,ksize=[1,2,2,1],strides=[1,2,2,1],padding='VALID')
#第三层
conv3=tf.nn.bias_add(tf.nn.conv2d(pool2,self.weights['conv3'],strides=[1,1,1,1],padding='VALID'),
self.biases['conv3'])
relu3=tf.nn.relu(conv3)
pool3=tf.nn.max_pool(relu3,ksize=[1,2,2,1],strides=[1,2,2,1],padding='VALID')
#全连接层1,先把特征图转为向量
flatten=tf.reshape(pool3,[-1,self.weights['fc1'].get_shape().as_list()[0]])
fc1=tf.matmul(flatten,self.weights['fc1'])+self.biases['fc1']
fc_relu1=tf.nn.relu(fc1)
fc2=tf.matmul(fc_relu1,self.weights['fc2'])+self.biases['fc2']
returnfc2
#计算softmax交叉熵损失函数
defsorfmax_loss(self,predicts,labels):
predicts=tf.nn.softmax(predicts)
labels=tf.one_hot(labels,self.weights['fc2'].get_shape().as_list()[1])
loss=-tf.reduce_mean(labels*tf.log(predicts))#tf.nn.softmax_cross_entropy_with_logits(predicts,labels)
self.cost=loss
returnself.cost
#梯度下降
defoptimer(self,loss,lr=0.001):
train_optimizer=tf.train.GradientDescentOptimizer(lr).minimize(loss)
returntrain_optimizer
deftrain():
encode_to_tfrecords("data/train.txt","data",'train.tfrecords',(45,45))
image,label=decode_from_tfrecords('data/train.tfrecords')
batch_image,batch_label=get_batch(image,label,batch_size=50,crop_size=39)#batch生成测试
#网络链接,训练所用
net=network()
inf=net.inference(batch_image)
loss=net.sorfmax_loss(inf,batch_label)
opti=net.optimer(loss)
#验证集所用
encode_to_tfrecords("data/val.txt","data",'val.tfrecords',(45,45))
test_image,test_label=decode_from_tfrecords('data/val.tfrecords',num_epoch=None)
test_images,test_labels=get_test_batch(test_image,test_label,batch_size=120,crop_size=39)#batch生成测试
test_inf=net.inference_test(test_images)
correct_prediction=tf.equal(tf.cast(tf.argmax(test_inf,1),tf.int32),test_labels)
accuracy=tf.reduce_mean(tf.cast(correct_prediction,tf.float32))
init=tf.initialize_all_variables()
withtf.Session()assession:
session.run(init)
coord=tf.train.Coordinator()
threads=tf.train.start_queue_runners(coord=coord)
max_iter=100000
iter=0
ifos.path.exists(os.path.join("model",'model.ckpt'))isTrue:
tf.train.Saver(max_to_keep=None).restore(session,os.path.join("model",'model.ckpt'))
whileiter<max_iter:
loss_np,_,label_np,image_np,inf_np=session.run([loss,opti,batch_label,batch_image,inf])
#printimage_np.shape
#cv2.imshow(str(label_np[0]),image_np[0])
#printlabel_np[0]
#cv2.waitKey()
#printlabel_np
ifiter%50==0:
print'trainloss:',loss_np
ifiter%500==0:
accuracy_np=session.run([accuracy])
print'***************testaccruacy:',accuracy_np,'*******************'
tf.train.Saver(max_to_keep=None).save(session,os.path.join('model','model.ckpt'))
iter+=1
coord.request_stop()#queue需要关闭,否则报错
coord.join(threads)
train()
3、可视化显示
(1)首先再源码中加入需要跟踪的变量:
[python]view plaincopy