caffe finetune predict and classify the lung nodule( 肺结节的分类)

引言--做了什么?

通过对caffe已有模型进行finetune 实现医学图像CT肺 结节的预测与检测,并实现可视化!caffe finetune predict and classify the lung nodule( 肺结节的分类)

思路--出发点是?

如果将CNN应用于医学图像,首要面对的问题是训练数据的缺乏。因为CNN的训练数据都需要有类别标号,这通常需要专家来手工标记。要是标记像ImageNet这样大规模的上百万张的训练图像,简直是不可想象的。
对于医学图像而言,得到大规模的训练数据是比较不容易的,那么可否使用finetune利用现成的ImageNet的图像来帮助医学图像的识别呢?ImageNet里面的图像(二维,彩色)没有医学图像,包含一些诸如鸟类、猫、狗、直升机等物体的识别,与医学图像(二维或三维,非彩色)相差很大。如果回答是肯定的话,是一件令人非常振奋的事情。

方案--怎么做?

首先是医学图像彩色化:http://blog.csdn.net/dcxhun3/article/details/51777794 其实这也是得到大师的指点的
然后是对样本的data augmentation,方可获得较多样本
再就是对彩色化的图像进行finetune训练

预测

利用finetune后的模型进行结节预测与检测。

代码和结果

借助 jupyter 也就是ipython notebook  现在还不知道怎么将imagenet_classify.ipynb源码上传 等学会了上传。你可以在相应路径下输入ipython notebook 启动 然后逐一将下面的路径复制运行
[python] view plain copy
 caffe finetune predict and classify the lung nodule( 肺结节的分类)caffe finetune predict and classify the lung nodule( 肺结节的分类)
  1. import os  
  2. import numpy as np  
  3. import matplotlib.pyplot as plt  
  4. %matplotlib inline  
  5.   
  6. #设置caffe源码所在的路径    
  7. caffe_root = '/usr/local/caffe/'  
  8. import sys  
  9. sys.path.insert(0, caffe_root + 'python')  
  10. import caffe  
  11.   
  12. plt.rcParams['figure.figsize'] = (1010)  
  13. plt.rcParams['image.interpolation'] = 'nearest'  
  14. plt.rcParams['image.cmap'] = 'gray'  
  15.   
  16. #加载均值文件   
  17. mean_filename='./imagenet_mean.binaryproto'  
  18. proto_data = open(mean_filename, "rb").read()  
  19. a = caffe.io.caffe_pb2.BlobProto.FromString(proto_data)  
  20. mean  = caffe.io.blobproto_to_array(a)[0]  
  21.   
  22. #创建网络,并加载已经训练好的模型文件    
  23. nodule_net_pretrained='./mytask_train_iter_8000.caffemodel'  
  24. nodule_net_model_file='./deploy.prototxt'  
  25. nodule_net = caffe.Classifier(nodule_net_model_file, nodule_net_pretrained,  
  26.                        mean=mean,  
  27.                        channel_swap=(2,1,0),#RGB通道与BGR   
  28.                        raw_scale=255,#把图片归一化到0~1之间  
  29.                        image_dims=(256256))#设置输入图片的大小  
(10, 3, 227, 227)

[python] view plain copy
 caffe finetune predict and classify the lung nodule( 肺结节的分类)caffe finetune predict and classify the lung nodule( 肺结节的分类)
  1. #预测分类及其可特征视化    
  2. nodule_list=['bg','nodule'#类别表  
  3. example_image = './2.bmp'  
  4. input_image = caffe.io.load_image(example_image)#读取图片  
  5. _ = plt.imshow(input_image)#显示原图片  
  6.   
  7. #预测结果  
  8. prediction = nodule_net.predict([input_image])  
  9. print 'predict bg or nodule:', nodule_list[prediction[0].argmax()]  
predict bg or nodule: bg
caffe finetune predict and classify the lung nodule( 肺结节的分类)

[python] view plain copy
 caffe finetune predict and classify the lung nodule( 肺结节的分类)caffe finetune predict and classify the lung nodule( 肺结节的分类)
  1. #预测分类及其可特征视化    
  2. nodule_list=['bg','nodule'#类别表  
  3. example_image = './136.bmp'  
  4. input_image = caffe.io.load_image(example_image)#读取图片  
  5. _ = plt.imshow(input_image)#显示原图片  
  6.   
  7. #预测结果  
  8. prediction = nodule_net.predict([input_image])   
  9. print 'predict bg or nodule:', nodule_list[prediction[0].argmax()]  

predict bg or nodule: nodule
caffe finetune predict and classify the lung nodule( 肺结节的分类)

[python] view plain copy
 caffe finetune predict and classify the lung nodule( 肺结节的分类)caffe finetune predict and classify the lung nodule( 肺结节的分类)
  1. def showimage(im):  
  2.     if im.ndim == 3:  
  3.         im = im[:, :, ::-1]  
  4.     plt.set_cmap('jet')  
  5.     plt.imshow(im)  
  6.       
  7.   
  8. def vis_square(data, padsize=1, padval=0):  
  9.     data -= data.min()  
  10.     data /= data.max()  
  11.       
  12.     # force the number of filters to be square  
  13.     n = int(np.ceil(np.sqrt(data.shape[0])))  
  14.     padding = ((0, n ** 2 - data.shape[0]), (0, padsize), (0, padsize)) + ((00),) * (data.ndim - 3)  
  15.     data = np.pad(data, padding, mode='constant', constant_values=(padval, padval))  
  16.       
  17.     # tile the filters into an image  
  18.     data = data.reshape((n, n) + data.shape[1:]).transpose((0213) + tuple(range(4, data.ndim + 1)))  
  19.     data = data.reshape((n * data.shape[1], n * data.shape[3]) + data.shape[4:])  
  20.       
  21.     showimage(data)  
  22. age_net = caffe.Classifier(nodule_net_model_file, nodule_net_pretrained,  
  23.                        channel_swap=(2,1,0),  
  24.                        raw_scale=255,  
  25.                        image_dims=(256256))  
  26.   
  27. prediction = nodule_net.predict([input_image])  
[python] view plain copy
 caffe finetune predict and classify the lung nodule( 肺结节的分类)caffe finetune predict and classify the lung nodule( 肺结节的分类)
  1. _ = plt.imshow(input_image)  


caffe finetune predict and classify the lung nodule( 肺结节的分类)

[python] view plain copy
 caffe finetune predict and classify the lung nodule( 肺结节的分类)caffe finetune predict and classify the lung nodule( 肺结节的分类)
  1. filters = nodule_net.params['conv1'][0].data[:49#conv1滤波器可视化  
  2. vis_square(filters.transpose(0231))  
caffe finetune predict and classify the lung nodule( 肺结节的分类)

[python] view plain copy
 caffe finetune predict and classify the lung nodule( 肺结节的分类)caffe finetune predict and classify the lung nodule( 肺结节的分类)
  1. feat = nodule_net.blobs['conv1'].data[4, :49]  #The first Conv layer output, conv1 (rectified responses of the filters above)  
  2. vis_square(feat, padval=1)  

caffe finetune predict and classify the lung nodule( 肺结节的分类)

哇塞,可视化成功~不得不佩服Python的强大