单通道灰度图片fine-tune训练网络与caffe批量分类测试

1. 转imdb灰度图数据

一定要加上--gray,否则训练时报如下错误:

GLOG_logtostderr=1 $TOOLS/convert_imageset \
    --resize_height=$RESIZE_HEIGHT \
    --resize_width=$RESIZE_WIDTH \
    --shuffle \
    --gray \
    $TRAIN_DATA_ROOT \
    $DATA/train.txt \
    $EXAMPLE/train_lmdb

echo "Creating val lmdb..."

GLOG_logtostderr=1 $TOOLS/convert_imageset \
    --resize_height=$RESIZE_HEIGHT \
    --resize_width=$RESIZE_WIDTH \
    --shuffle \
    --gray \
    $VAL_DATA_ROOT \
    $DATA/val.txt \
    $EXAMPLE/val_lmdb

2. fine-tune训练网络

在上一步生成lmdb的时候有一个参数是--gray,这样生成的lmdb就是单通道了,然后就是需要修改一下第一个卷基层的名字,这一层会被随机初始化,通过finetune的方式进行学习

layer {
    bottom: "data"
    top: "conv1"
    name: "myconv1" #修改该层名字
    type: "Convolution"
    convolution_param {
        num_output: 64
        kernel_size: 7
        pad: 3
        stride: 2
        weight_filler {
            type: "msra"
        }
    }
}

3.更改ResNet_50_deploy.prototxt输入数据为单通道

name: "ResNet-50"
input: "data"
input_dim: 1
input_dim: 1
input_dim: 224
input_dim: 896

(1,1,224,896)分别对应一张图片,单通道灰度图,图像高度和图像宽度

4.mean.binaryproto转换为mean.npy文件

#!/usr/bin/env python
import numpy as np
import sys
import caffe
root='/home/yangjian/caffe-master/myfile_jian/data/'
mean_proto_path=root+'imagenet_mean.binaryproto'   
mean_npy_path=root+'mean.npy'              

blob=caffe.proto.caffe_pb2.BlobProto()    
data=open(mean_proto_path,'rb').read()     
blob.ParseFromString(data)                 

array=np.array(caffe.io.blobproto_to_array(blob))  
mean_npy=array[0]                          
np.save(mean_npy_path,mean_npy)

5.批量测试灰度图

  • #单通道灰度图,不用设置该项
    transformer.set_channel_swap('data', (2,1,0))# caffe中图片是BGR格式,而原始格式是RGB,所以将图片由RGB变成BGR
  • img=caffe.io.load_image(imgpath,color=False) ,此句读入图像并转化为单通道灰度图,值为float型,范围是0~1.0,因为读入时图像时单通道
#!/usr/bin/env python
#coding=utf-8
import os
import sys
import numpy as np
import cv2

caffe_root='/home/yangjian/caffe-master/' 
sys.path.insert(0, caffe_root + 'python')
import caffe
#使用GPU模式
caffe.set_device(0)  
caffe.set_mode_gpu()
#caffe.set_mode_cpu()
os.chdir(caffe_root)
#保存分类结果路径
path_0= "/home/yangjian/caffe-master/myfile_jian/test_result/test_image_result/0_liangshiche"
path_1= "/home/yangjian/caffe-master/myfile_jian/test_result/test_image_result/1_pengche"
path_2= "/home/yangjian/caffe-master/myfile_jian/test_result/test_image_result/2_changche"
path_3= "/home/yangjian/caffe-master/myfile_jian/test_result/test_image_result/3_guangche"
path_4= "/home/yangjian/caffe-master/myfile_jian/test_result/test_image_result/4_keche"
path_5= "/home/yangjian/caffe-master/myfile_jian/test_result/test_image_result/5_pingche"

deploy=caffe_root+'myfile_jian/ResNet_50_deploy.prototxt'     
caffe_model=caffe_root+'myfile_jian/models/ResNet_50_iter_1000.caffemodel'  
mean_file=caffe_root+'myfile_jian/data/mean.npy'    
labels_filename=caffe_root+'myfile_jian/data/word.txt'
 
# 测试图片存放文件夹
#dir=caffe_root+'myfile_jian/test_image/test_image1'
#dir=caffe_root+'myfile_jian/test_image/image1_50'
dir=caffe_root+'myfile_jian/test_image/11/'

filelist=[]
filenames=os.listdir(dir)
for fn in filenames:
    fullfilename=os.path.join(dir,fn)
    filelist.append(fullfilename)
    
net=caffe.Net(deploy,caffe_model,caffe.TEST)
print net.blobs['data'].data.shape #(1,1,224,896)
#图片预处理设置
transformer = caffe.io.Transformer({'data': net.blobs['data'].data.shape})
transformer.set_transpose('data', (2,0,1))#python读取的图片文件格式为H×W×C,需转化为C×H×W
transformer.set_mean('data', np.load(mean_file).mean(1).mean(1))
transformer.set_raw_scale('data', 255) # python中将图片存储为[0, 1],而caffe中将图片存储为[0, 255]
#单通道灰度图,不用该项
#transformer.set_channel_swap('data', (2,1,0))# caffe中图片是BGR格式,而原始格式是RGB,所以将图片由RGB变成BGR

for i in range(0,len(filelist)):
    img=filelist[i]
    img_jpg=os.path.basename(img) 
    result_img= cv2.imread(img)
    print '********** Prediction the picture:',filenames[i],'**********'
    #im=caffe.io.load_image(img)#加载RGB图
    im=caffe.io.load_image(img,color=False)  #加载灰度图片, 读取的图片文件格式为H×W×C,需注意
    net.blobs['data'].data[...]=transformer.preprocess('data',im) #执行上面的预处理操作,并将图片载入到blob中 
    out=net.forward()    
    labels=np.loadtxt(labels_filename,str,delimiter='/t')#读取类别名称文件
    prob=net.blobs['prob'].data[0].flatten() #取出最后一层(prob)属于某个类标的概率值,'prob'为最后一层的名称   
    #print prob
    index1=prob.argsort()[-1] #获取最大概率值对应的index 
    index2=prob.argsort()[-2] 
    index3=prob.argsort()[-3]  
    index4=prob.argsort()[-4]
    index5=prob.argsort()[-5] 
    index6=prob.argsort()[-6] 
    
    print labels[index1],'--',prob[index1]
    print labels[index2],'--',prob[index2]
    print labels[index3],'--',prob[index3]
    print labels[index4],'--',prob[index4]
    print labels[index5],'--',prob[index5]
    print labels[index6],'--',prob[index6]
    print '**********the final classification results:',labels[index1],'**********'

    if labels[index1]=='0 liangshiche':
       cv2.imwrite(os.path.join(path_0,img_jpg),result_img)
    if labels[index1]=='1 pengche':
       cv2.imwrite(os.path.join(path_1,img_jpg),result_img)
    if labels[index1]=='2 changche':
       cv2.imwrite(os.path.join(path_2,img_jpg),result_img)
    if labels[index1]=='3 guangche':
       cv2.imwrite(os.path.join(path_3,img_jpg),result_img)
    if labels[index1]=='4 keche':
       cv2.imwrite(os.path.join(path_4,img_jpg),result_img)
    if labels[index1]=='5 pingche':
       cv2.imwrite(os.path.join(path_5,img_jpg),result_img)
    

参考:

(1)单通道灰度图片怎么fine-tune

http://www.caffecn.cn/?/question/1269

(2)Caffe中把数据转换成灰度图

https://blog.csdn.net/Sunshine_in_Moon/article/details/50981760?locationNum=9

(3)使用caffe的convert_imageset生成lmdb文件 –gray

https://blog.csdn.net/Losteng/article/details/51170394

(4)训练图像及测试图像都是灰度图,图像通道为单通道

https://blog.csdn.net/qq_36524525/article/details/78117097

(5)Caffe学习——Imagenet分类

https://blog.csdn.net/shadow_guo/article/details/50359532

(6)caffemodel进行批量测试

https://blog.csdn.net/qq_27923041/article/details/75308248

你可能感兴趣的:(deep,learning学习笔记)