3D肺結節分類網路
阿新 • • 發佈:2018-12-11
import keras
from keras.callbacks import EarlyStopping
from keras.models import load_model
from keras.layers import Conv2D, MaxPooling2D, MaxPooling2D,merge,MaxPooling3D,Conv3D,concatenate
from keras.layers import Activation, Dropout, Flatten, Dense,Merge,Input, Dense,Reshape,BatchNormalization
from keras import backend as K
from keras.models import Model
from keras import regularizers
import pprint
import random
import numpy as np
import os
import cv2
import math
from keras.layers.normalization import BatchNormalization as bn
from scipy.misc import imsave
import numpy as np
import time
from sklearn import metrics
l2_lambda = 0.0002
DropP = 0.3
kernel_size=3
inputthree=Input(shape=(100,100,10,1), dtype='float32',name='inputthree')
prepool= MaxPooling3D(pool_size=(2,2,2))(inputthree)
#分支1開始
#殘差塊1
conv1a = Conv3D( 12, (kernel_size, kernel_size, kernel_size), activation='relu', padding='same',
kernel_regularizer= regularizers.l2(l2_lambda) )(prepool)
conv1a = bn()(conv1a)
conv1b = Conv3D(12, (kernel_size, kernel_size, kernel_size), activation='relu', padding='same',
kernel_regularizer=regularizers.l2(l2_lambda) )(conv1a)
conv1b = bn()(conv1b)
merge1=concatenate([conv1a,conv1b])
conv1c = Conv3D(12, (kernel_size, kernel_size, kernel_size), activation='relu', padding='same',
kernel_regularizer=regularizers.l2(l2_lambda) )(merge1)
conv1c = bn()(conv1c)
merge2=concatenate([conv1a,conv1b,conv1c])
conv1d = Conv3D(12, (kernel_size, kernel_size, kernel_size), activation='relu', padding='same',
kernel_regularizer=regularizers.l2(l2_lambda) )(merge2)
conv1d = bn()(conv1d)
pool1 = MaxPooling3D(pool_size=(2, 2, 1))(conv1d)
pool1 = Dropout(DropP)(pool1)
flatten1=Flatten()(pool1)
output1=Dense(1,activation='sigmoid')(flatten1)
#--------------------------------------------------------------------------------------------------
#殘差塊2
conv2a = Conv3D(12, (kernel_size, kernel_size, kernel_size), activation='relu', padding='same',
kernel_regularizer=regularizers.l2(l2_lambda) )(pool1)
conv2a = bn()(conv2a)
conv2b = Conv3D(12, (kernel_size, kernel_size, kernel_size), activation='relu', padding='same',
kernel_regularizer=regularizers.l2(l2_lambda) )(conv2a)
conv2b = bn()(conv2b)
merge1=concatenate([conv2a,conv2b])
conv2c = Conv3D(12, (kernel_size, kernel_size, kernel_size), activation='relu', padding='same',
kernel_regularizer=regularizers.l2(l2_lambda) )(merge1)
conv2c = bn()(conv2c)
merge2=concatenate([conv2a,conv2b,conv2c])
conv2d = Conv3D(12, (kernel_size, kernel_size, kernel_size), activation='relu', padding='same',
kernel_regularizer=regularizers.l2(l2_lambda) )(merge2)
conv2d = bn()(conv2d)
merge3=concatenate([conv2a,conv2b,conv2c,conv2d])
conv2e = Conv3D(12, (kernel_size, kernel_size, kernel_size), activation='relu', padding='same',
kernel_regularizer=regularizers.l2(l2_lambda) )(merge3)
conv2e = bn()(conv2e)
merge4=concatenate([conv2a,conv2b,conv2c,conv2d,conv2e])
conv2f = Conv3D(12, (kernel_size, kernel_size, kernel_size), activation='relu', padding='same',
kernel_regularizer=regularizers.l2(l2_lambda) )(merge4)
conv2f = bn()(conv2f)
merge5=concatenate([conv2a,conv2b,conv2c,conv2d,conv2e,conv2f])
conv2g = Conv3D(12, (kernel_size, kernel_size, kernel_size), activation='relu', padding='same',
kernel_regularizer=regularizers.l2(l2_lambda) )(merge5)
conv2g = bn()(conv2g)
merge6=concatenate([conv2a,conv2b,conv2c,conv2d,conv2e,conv2f,conv2g])
conv2h = Conv3D(12, (kernel_size, kernel_size, kernel_size), activation='relu', padding='same',
kernel_regularizer=regularizers.l2(l2_lambda) )(merge6)
conv2h = bn()(conv2h)
merge7=concatenate([conv2a,conv2b,conv2c,conv2d,conv2e,conv2f,conv2g,conv2h])
conv2i = Conv3D(12, (kernel_size, kernel_size, kernel_size), activation='relu', padding='same',
kernel_regularizer=regularizers.l2(l2_lambda) )(merge7)
conv2i = bn()(conv2g)
merge8=concatenate([conv2a,conv2b,conv2c,conv2d,conv2e,conv2f,conv2g,conv2h,conv2i])
conv2j = Conv3D(12, (kernel_size, kernel_size, kernel_size), activation='relu', padding='same',
kernel_regularizer=regularizers.l2(l2_lambda) )(merge8)
conv2j = bn()(conv2g)
pool2 = MaxPooling3D(pool_size=(2, 2, 1))(conv2j)
pool2 = Dropout(DropP)(pool2)
flatten2=Flatten()(pool2)
output2=Dense(1,activation='sigmoid')(flatten2)
#--------------------------------------------------------------------------------------------------
#殘差塊3
conv3a = Conv3D(12, (kernel_size, kernel_size, kernel_size), activation='relu', padding='same',
kernel_regularizer=regularizers.l2(l2_lambda) )(pool2)
conv3a = bn()(conv3a)
conv3b = Conv3D(12, (kernel_size, kernel_size, kernel_size), activation='relu', padding='same',
kernel_regularizer=regularizers.l2(l2_lambda) )(conv3a)
conv3b = bn()(conv3b)
merge1=concatenate([conv3a,conv3b])
conv3c = Conv3D(12, (kernel_size, kernel_size, kernel_size), activation='relu', padding='same',
kernel_regularizer=regularizers.l2(l2_lambda) )(merge1)
conv3c = bn()(conv3c)
merge2=concatenate([conv3a,conv3b,conv3c])
conv3d = Conv3D(12, (kernel_size, kernel_size, kernel_size), activation='relu', padding='same',
kernel_regularizer=regularizers.l2(l2_lambda) )(merge2)
conv3d = bn()(conv3d)
merge3=concatenate([conv3a,conv3b,conv3c,conv3d])
conv3e = Conv3D(12, (kernel_size, kernel_size, kernel_size), activation='relu', padding='same',
kernel_regularizer=regularizers.l2(l2_lambda) )(merge3)
conv3e = bn()(conv3e)
merge4=concatenate([conv3a,conv3b,conv3c,conv3d,conv3e])
conv3f = Conv3D(12, (kernel_size, kernel_size, kernel_size), activation='relu', padding='same',
kernel_regularizer=regularizers.l2(l2_lambda) )(merge4)
conv3f = bn()(conv3f)
merge5=concatenate([conv3a,conv3b,conv3c,conv3d,conv3e,conv3f])
conv3g = Conv3D(12, (kernel_size, kernel_size, kernel_size), activation='relu', padding='same',
kernel_regularizer=regularizers.l2(l2_lambda) )(merge5)
conv3g = bn()(conv3g)
merge6=concatenate([conv3a,conv3b,conv3c,conv3d,conv3e,conv3f,conv3g])
conv3h = Conv3D(12, (kernel_size, kernel_size, kernel_size), activation='relu', padding='same',
kernel_regularizer=regularizers.l2(l2_lambda) )(merge6)
conv3h = bn()(conv3h)
merge7=concatenate([conv3a,conv3b,conv3c,conv3d,conv3e,conv3f,conv3g,conv3h])
conv3i = Conv3D(12, (kernel_size, kernel_size, kernel_size), activation='relu', padding='same',
kernel_regularizer=regularizers.l2(l2_lambda) )(merge7)
conv3i = bn()(conv3i)
merge8=concatenate([conv3a,conv3b,conv3c,conv3d,conv3e,conv3f,conv3g,conv3h,conv3i])
conv3j = Conv3D(12, (kernel_size, kernel_size, kernel_size), activation='relu', padding='same',
kernel_regularizer=regularizers.l2(l2_lambda) )(merge8)
conv3j = bn()(conv3j)
merge9=concatenate([conv3a,conv3b,conv3c,conv3d,conv3e,conv3f,conv3g,conv3h,conv3i,conv3j])
conv3k = Conv3D(12, (kernel_size, kernel_size, kernel_size), activation='relu', padding='same',
kernel_regularizer=regularizers.l2(l2_lambda) )(merge9)
conv3k = bn()(conv3k)
merge10=concatenate([conv3a,conv3b,conv3c,conv3d,conv3e,conv3f,conv3g,conv3h,conv3i,conv3j,conv3k])
conv3l=Conv3D(12, (kernel_size, kernel_size, kernel_size), activation='relu', padding='same',
kernel_regularizer=regularizers.l2(l2_lambda) )(merge10)
conv3l = bn()(conv3l)
merge11=concatenate([conv3a,conv3b,conv3c,conv3d,conv3e,conv3f,conv3g,conv3h,conv3i,conv3j,conv3k,conv3l])
conv3m=Conv3D(12, (kernel_size, kernel_size, kernel_size), activation='relu', padding='same',
kernel_regularizer=regularizers.l2(l2_lambda) )(merge11)
conv3m = bn()(conv3m)
merge12=concatenate([conv3a,conv3b,conv3c,conv3d,conv3e,conv3f,conv3g,conv3h,conv3i,conv3j,conv3k,conv3l,conv3m])
conv3n=Conv3D(12, (kernel_size, kernel_size, kernel_size), activation='relu', padding='same',
kernel_regularizer=regularizers.l2(l2_lambda) )(merge12)
conv3n = bn()(conv3n)
merge13=concatenate([conv3a,conv3b,conv3c,conv3d,conv3e,conv3f,conv3g,conv3h,conv3i,conv3j,conv3k,conv3l,conv3m,conv3n])
conv3o=Conv3D(12, (kernel_size, kernel_size, kernel_size), activation='relu', padding='same',
kernel_regularizer=regularizers.l2(l2_lambda) )(merge13)
conv3o = bn()(conv3o)
merge14=concatenate([conv3a,conv3b,conv3c,conv3d,conv3e,conv3f,conv3g,conv3h,conv3i,conv3j,conv3k,conv3l,conv3m,conv3n,conv3o])
conv3p=Conv3D(12, (kernel_size, kernel_size, kernel_size), activation='relu', padding='same',
kernel_regularizer=regularizers.l2(l2_lambda) )(merge14)
conv3p = bn()(conv3p)
merge15=concatenate([conv3a,conv3b,conv3c,conv3d,conv3e,conv3f,conv3g,conv3h,conv3i,conv3j,conv3k,conv3l,conv3m,conv3n,conv3o,conv3p])
conv3q=Conv3D(12, (kernel_size, kernel_size, kernel_size), activation='relu', padding='same',
kernel_regularizer=regularizers.l2(l2_lambda) )(merge15)
conv3q = bn()(conv3q)
merge16=concatenate([conv3a,conv3b,conv3c,conv3d,conv3e,conv3f,conv3g,conv3h,conv3i,conv3j,conv3k,conv3l,conv3m,conv3n,conv3o,conv3p,conv3q])
conv3r=Conv3D(12, (kernel_size, kernel_size, kernel_size), activation='relu', padding='same',
kernel_regularizer=regularizers.l2(l2_lambda) )(merge16)
conv3r = bn()(conv3r)
merge17=concatenate([conv3a,conv3b,conv3c,conv3d,conv3e,conv3f,conv3g,conv3h,conv3i,conv3j,conv3k,conv3l,conv3m,conv3n,conv3o,conv3p,conv3q,conv3r])
conv3s=Conv3D(12, (kernel_size, kernel_size, kernel_size), activation='relu', padding='same',
kernel_regularizer=regularizers.l2(l2_lambda) )(merge17)
conv3s = bn()(conv3s)
merge18=concatenate([conv3a,conv3b,conv3c,conv3d,conv3e,conv3f,conv3g,conv3h,conv3i,conv3j,conv3k,conv3l,conv3m,conv3n,conv3o,conv3p,conv3q,conv3r,conv3s])
conv3t=Conv3D(12, (kernel_size, kernel_size, kernel_size), activation='relu', padding='same',
kernel_regularizer=regularizers.l2(l2_lambda) )(merge18)
conv3t = bn()(conv3t)
merge19=concatenate([conv3a,conv3b,conv3c,conv3d,conv3e,conv3f,conv3g,conv3h,conv3i,conv3j,conv3k,conv3l,conv3m,conv3n,conv3o,conv3p,conv3q