import os
import random
from keras import layers
from keras import backend as K
from keras.layers import Input, Convolution3D, MaxPooling3D, Flatten, Dropout,\
AveragePooling3D, BatchNormalization,Activation
from keras.metrics import binary_accuracy, binary_crossentropy
from keras.models import Model
from keras.optimizers import Adam
from keras.callbacks import ModelCheckpoint, History, EarlyStopping
import matplotlib.pyplot as plt
import numpy
import cv2
K.set_image_dim_ordering("tf")
CUBE_SIZE =32
MEAN_PIXEL_VALUE =41
BATCH_SIZE =8
实现3dcnn的网络结构,并加载预训练好的权重——优化模型
defget_3dnnnet(input_shape=(CUBE_SIZE, CUBE_SIZE, CUBE_SIZE,1),
load_weight_path=None, USE_DROPOUT=True)-> Model:
inputs = Input(shape=input_shape, name="input_1")
x = inputs
#-------------------------------------------------------------
X_shortcut = x #保存输入值,后面将需要添加回主路径#-------------------------------------------------------------
x = AveragePooling3D(pool_size=(2,1,1), strides=(2,1,1), border_mode="same")(x)
x = Convolution3D(64,3,3,3, border_mode='same', name='conv1', subsample=(1,1,1))(x)
x = BatchNormalization()(x)
x = Activation('relu')(x)
x = MaxPooling3D(pool_size=(1,2,2), strides=(1,2,2), border_mode='valid', name='pool1')(x)if USE_DROPOUT:
x = Dropout(p=0.3)(x)#空间金字塔池化###dropout = SpatialDropout3D(rate=dropout_rate, data_format=data_format)(convolution1)# 2nd layer group
x = Convolution3D(128,3,3,3, border_mode='same', name='conv2', subsample=(1,1,1))(x)
x = BatchNormalization()(x)
x = Activation('relu')(x)
x = MaxPooling3D(pool_size=(2,2,2), strides=(2,2,2), border_mode='valid', name='pool2')(x)if USE_DROPOUT:
x = Dropout(p=0.3)(x)# 3rd layer group
x = Convolution3D(256,3,3,3, activation='relu', border_mode='same', name='conv3a', subsample=(1,1,1))(x)
x = Convolution3D(256,3,3,3, activation='relu', border_mode='same', name='conv3b', subsample=(1,1,1))(x)
x = MaxPooling3D(pool_size=(2,2,2), strides=(2,2,2), border_mode='valid', name='pool3')(x)if USE_DROPOUT:
x = Dropout(p=0.4)(x)# 4th layer group
x = Convolution3D(512,3,3,3, activation='relu', border_mode='same', name='conv4a', subsample=(1,1,1))(x)
x = Convolution3D(512,3,3,3, activation='relu', border_mode='same', name='conv4b', subsample=(1,1,1),)(x)
x = MaxPooling3D(pool_size=(2,2,2), strides=(2,2,2), border_mode='valid', name='pool4')(x)if USE_DROPOUT:
x = Dropout(p=0.5)(x)# shortcut路径--------------------------------------------
X_shortcut = Convolution3D(512,3,3,3, activation='relu', border_mode='same', name='conv4a_X', subsample=(1,1,1))(x)
X_shortcut = Convolution3D(512,3,3,3, activation='relu', border_mode='same', name='conv4b_X', subsample=(1,1,1))(X_shortcut)
X_shortcut = MaxPooling3D(pool_size=(2,2,2), strides=(2,2,2), border_mode='valid', name='pool5')(X_shortcut)
X_shortcut = BatchNormalization()(X_shortcut)# 主路径最后部分,为主路径添加shortcut并通过relu激活
X = layers.add([x, X_shortcut])#--------------------------------------------
last64 = Convolution3D(64,2,2,2, activation="relu", name="last_64")(X)
out_class = Convolution3D(1,1,1,1, activation="sigmoid", name="out_class_last")(last64)
out_class = Flatten(name="out_class")(out_class)
model = Model(input=inputs, output=[out_class])
model.load_weights(load_weight_path, by_name=False)
model.compile(optimizer=Adam(lr=0.0001, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.0),
loss={"out_class":"binary_crossentropy"},
metrics={"out_class":[binary_accuracy, binary_crossentropy]})return model