关键词

使用Keras构建CNN神经网络在医学图像CT上的应用

个人博客,欢迎来撩 fangzengye.com

1.基础设置

1.1.加载包

import numpy as np # matrix tools
import matplotlib.pyplot as plt # for basic plots
import seaborn as sns # for nicer plots
import pandas as pd
from glob import glob
import re
from skimage.io import imread

import keras

1.2.显示文件夹的文件

matplotlib inline
import os
for dirname, _, filenames in os.walk(\'/kaggle/input\'):
    print(dirname,"______")
    for filename in filenames:
        print(os.path.join(dirname, filename))

2数据集分析

2.1预览数据集信息

BASE_IMG_PATH=os.path.join(\'..\',\'kaggle/input\')
path= os.path.join(BASE_IMG_PATH,\'overview.csv\')
overview = pd.read_csv(path)
overview.head()

 

overview[\'Contrast\'] = overview[\'Contrast\'].map(lambda x: 1 if x else 0)

2.2画出Age属性特征可视化

plt.figure(figsize=(10,5))
sns.distplot(overview[\'Age\'])

 

g = sns.FacetGrid(overview, hue="Contrast",size=6, legend_out=True)
g = g.map(sns.distplot, "Age").add_legend()

3.读取图像文件

3.1查看文件夹的文件信息

BASE_IMG_PATH=os.path.join(\'..\',\'kaggle/input\')
print(os.path.join(BASE_IMG_PATH,\'tiff_images\',\'*.tif\'))
all_images_list = glob(os.path.join(BASE_IMG_PATH,\'tiff_images\',\'*.tif\'))
all_images_list[:5]
print(all_images_list)

 

jimread = lambda x: np.expand_dims(imread(x)[::2,::2],0)

 

test_image = jimread(all_images_list[0])
plt.imshow(test_image[0])

3.2设置label

check_contrast = re.compile(r\'data\\tiff_images\\ID_([\d]+)_AGE_[\d]+_CONTRAST_([\d]+)_CT.tif\')
label = []
id_list = []
for image in all_images_list:
    id_list.append(check_contrast.findall(image)[0][0])
    label.append(check_contrast.findall(image)[0][1])

 

label_list = pd.DataFrame(label,id_list)

 

images = np.stack([jimread(i) for i in all_images_list],0)

 

4.分割数据集为训练集和测试集

4.1倒入包

from sklearn.model_selection import train_test_split

4.2随机分割数据集

X_train, X_test, y_train, y_test = train_test_split(images, label_list, test_size=0.1, random_state=0)

 

5.重新设置图像size

5.1

n_train, depth, width, height = X_train.shape
n_test,_,_,_ = X_test.shape

 

5.2设置输出size

input_shape = (width,height,depth)

 

5.3训练集归一化

input_train = X_train.reshape((n_train, width,height,depth))
input_train.shape
input_train.astype(\'float32\')
input_train = input_train / np.max(input_train)
input_train.max()

5.4测试集归一化

input_test = X_test.reshape(n_test, *input_shape)
input_test.astype(\'float32\')
input_test = input_test / np.max(input_test)

5.5设置类别数

output_train = keras.utils.to_categorical(y_train, 2)
output_test = keras.utils.to_categorical(y_test, 2)
output_train[5]

6.构建神经网络

6.1导包

from keras.models import Sequential
from keras.layers import Dense, Flatten
from keras.optimizers import Adam
from keras.layers import Conv2D, MaxPooling2D

6.2设置每次训练样板大小,训练代数

batch_size = 20
epochs = 40

6.3构建网络

model2 = Sequential()
model2.add(Conv2D(50, (5, 5), activation=\'relu\', input_shape=input_shape))
 # 32개의 4x4 Filter 를 이용하여 Convolutional Network생성
model2.add(MaxPooling2D(pool_size=(3, 3))) # 3x3 Maxpooling 
model2.add(Conv2D(30, (4, 4), activation=\'relu\', input_shape=input_shape))
model2.add(MaxPooling2D(pool_size=(2, 2))) # 2x2 Maxpooling 
model2.add(Flatten()) # 쭉풀어서 Fully Connected Neural Network를 만든다. 
model2.add(Dense(2, activation=\'softmax\'))

6.4显示训练结果摘要

print(model2.summary())

6.5开始拟合、训练

history = model2.fit(input_train, output_train,
                    batch_size=batch_size,
                    epochs=epochs,
                    verbose=1,
                    validation_data=(input_test, output_test))

6.6评估训练效果

score = model2.evaluate(input_test, output_test, verbose=0)
print(score)

 

 

参考资料https://www.kaggle.com/azaemon/applyting-cnn-on-ct-images-with-keras

 

 

 

 

 

 

 

 

本文链接:http://task.lmcjl.com/news/12461.html

展开阅读全文