加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
model_ResNet.py 2.06 KB
一键复制 编辑 原始数据 按行查看 历史
wikke 提交于 2017-11-17 16:07 . fine README.md and code refine
from keras.models import Model
from keras.layers import Input, Conv3D, Dense, BatchNormalization, Add, Flatten, Concatenate, AveragePooling3D, GlobalMaxPooling3D, Activation
from keras.optimizers import Adam
from config import *
def conv_bn_relu(x, filters, kernel_size=(3, 3, 3), strides=(1, 1, 1), padding='same', apply_relu=True):
x = Conv3D(filters, kernel_size=kernel_size, strides=strides, padding=padding)(x)
x = BatchNormalization()(x)
if apply_relu:
x = Activation('relu')(x)
return x
def bottleneck(x, shrinkage=False):
print('resnet block, shrinkage:{}'.format(shrinkage))
print(x.get_shape())
input_filters = x.get_shape()[4].value
keep_filters = input_filters // 2 if shrinkage else input_filters // 4
output_filters = input_filters * 2 if shrinkage else input_filters
first_strides = (2, 2, 2) if shrinkage else (1, 1, 1)
residual = conv_bn_relu(x, filters=keep_filters, kernel_size=(1, 1, 1), strides=first_strides)
residual = conv_bn_relu(residual, filters=keep_filters, kernel_size=(3, 3, 3))
residual = conv_bn_relu(residual, filters=output_filters, kernel_size=(1, 1, 1), apply_relu=False)
if shrinkage:
x = conv_bn_relu(x, filters=output_filters, kernel_size=(3, 3, 3), strides=(2, 2, 2), apply_relu=False)
print(residual.get_shape())
print(x.get_shape())
x = Add()([residual, x])
x = Activation('relu')(x)
return x
def get_ResNet_classifier():
inputs = Input((CLASSIFY_INPUT_WIDTH, CLASSIFY_INPUT_HEIGHT, CLASSIFY_INPUT_DEPTH, CLASSIFY_INPUT_CHANNEL))
x = conv_bn_relu(inputs, RESNET_INITIAL_FILTERS)
print('base')
print(x.get_shape())
for i in range(RESNET_BLOCKS):
x = bottleneck(x, shrinkage=(i % RESNET_SHRINKAGE_STEPS == 0))
print('top')
x = GlobalMaxPooling3D()(x)
print(x.get_shape())
x = Dense(2, activation='softmax')(x)
print(x.get_shape())
model = Model(inputs=inputs, outputs=x)
model.compile(optimizer=Adam(lr=TRAIN_CLASSIFY_LEARNING_RATE), loss='binary_crossentropy', metrics=['accuracy'])
return model
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化