Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Also, add learning rate as RunOption parameter * Remove fcn.py, since fcn_resnet.py supersedes it.
- Loading branch information
Showing
8 changed files
with
207 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
""" | ||
ResNet based FCN. | ||
""" | ||
from keras.models import Model | ||
from keras.layers import (Input, | ||
Activation, | ||
Convolution2D, | ||
Reshape, | ||
Lambda, | ||
merge) | ||
|
||
from .resnet import ResNet | ||
|
||
|
||
def make_fcn_resnet(input_shape, nb_labels): | ||
input_shape = tuple(input_shape) | ||
nb_rows, nb_cols, _ = input_shape | ||
nb_labels = nb_labels | ||
|
||
input_tensor = Input(shape=input_shape) | ||
model = ResNet(input_tensor=input_tensor) | ||
|
||
x = model.output | ||
|
||
x64 = model.get_layer('activation_10').output | ||
x32 = model.get_layer('activation_22').output | ||
x16 = model.get_layer('activation_37').output | ||
|
||
def resize_bilinear(images): | ||
# Workaround for | ||
# https://github.com/fchollet/keras/issues/4609 | ||
import tensorflow as tf | ||
nb_rows = 512 | ||
nb_cols = 512 | ||
return tf.image.resize_bilinear(images, [nb_rows, nb_cols]) | ||
|
||
c64 = Convolution2D(nb_labels, 1, 1)(x64) | ||
c32 = Convolution2D(nb_labels, 1, 1)(x32) | ||
c16 = Convolution2D(nb_labels, 1, 1)(x16) | ||
|
||
b64 = Lambda(resize_bilinear)(c64) | ||
b32 = Lambda(resize_bilinear)(c32) | ||
b16 = Lambda(resize_bilinear)(c16) | ||
|
||
x = merge([b64, b32, b16], mode='sum') | ||
|
||
x = Reshape((nb_rows * nb_cols, nb_labels))(x) | ||
x = Activation('softmax')(x) | ||
x = Reshape((nb_rows, nb_cols, nb_labels))(x) | ||
|
||
model = Model(input=input_tensor, output=x) | ||
|
||
return model |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
# -*- coding: utf-8 -*- | ||
# flake8: noqa | ||
'''ResNet50 model for Keras. | ||
# Reference: | ||
- [Deep Residual Learning for Image Recognition](https://arxiv.org/abs/1512.03385) | ||
Adapted from code contributed by BigMoyan. | ||
Adapted from code from | ||
https://github.com/fchollet/deep-learning-models/blob/master/resnet50.py | ||
''' | ||
from __future__ import print_function | ||
|
||
import numpy as np | ||
import warnings | ||
|
||
from keras.layers import merge, Input | ||
from keras.layers import Dense, Activation, Flatten | ||
from keras.layers import Convolution2D, MaxPooling2D, ZeroPadding2D, AveragePooling2D | ||
from keras.layers import BatchNormalization | ||
from keras.models import Model | ||
from keras.preprocessing import image | ||
import keras.backend as K | ||
from keras.utils.layer_utils import convert_all_kernels_in_model | ||
from keras.utils.data_utils import get_file | ||
|
||
|
||
def identity_block(input_tensor, kernel_size, filters, stage, block): | ||
'''The identity_block is the block that has no conv layer at shortcut | ||
# Arguments | ||
input_tensor: input tensor | ||
kernel_size: defualt 3, the kernel size of middle conv layer at main path | ||
filters: list of integers, the nb_filters of 3 conv layer at main path | ||
stage: integer, current stage label, used for generating layer names | ||
block: 'a','b'..., current block label, used for generating layer names | ||
''' | ||
nb_filter1, nb_filter2, nb_filter3 = filters | ||
if K.image_dim_ordering() == 'tf': | ||
bn_axis = 3 | ||
else: | ||
bn_axis = 1 | ||
conv_name_base = 'res' + str(stage) + block + '_branch' | ||
bn_name_base = 'bn' + str(stage) + block + '_branch' | ||
|
||
x = Convolution2D(nb_filter1, 1, 1, name=conv_name_base + '2a')(input_tensor) | ||
x = BatchNormalization(axis=bn_axis, name=bn_name_base + '2a')(x) | ||
x = Activation('relu')(x) | ||
|
||
x = Convolution2D(nb_filter2, kernel_size, kernel_size, | ||
border_mode='same', name=conv_name_base + '2b')(x) | ||
x = BatchNormalization(axis=bn_axis, name=bn_name_base + '2b')(x) | ||
x = Activation('relu')(x) | ||
|
||
x = Convolution2D(nb_filter3, 1, 1, name=conv_name_base + '2c')(x) | ||
x = BatchNormalization(axis=bn_axis, name=bn_name_base + '2c')(x) | ||
|
||
x = merge([x, input_tensor], mode='sum') | ||
x = Activation('relu')(x) | ||
return x | ||
|
||
|
||
def conv_block(input_tensor, kernel_size, filters, stage, block, strides=(2, 2)): | ||
'''conv_block is the block that has a conv layer at shortcut | ||
# Arguments | ||
input_tensor: input tensor | ||
kernel_size: defualt 3, the kernel size of middle conv layer at main path | ||
filters: list of integers, the nb_filters of 3 conv layer at main path | ||
stage: integer, current stage label, used for generating layer names | ||
block: 'a','b'..., current block label, used for generating layer names | ||
Note that from stage 3, the first conv layer at main path is with subsample=(2,2) | ||
And the shortcut should have subsample=(2,2) as well | ||
''' | ||
nb_filter1, nb_filter2, nb_filter3 = filters | ||
if K.image_dim_ordering() == 'tf': | ||
bn_axis = 3 | ||
else: | ||
bn_axis = 1 | ||
conv_name_base = 'res' + str(stage) + block + '_branch' | ||
bn_name_base = 'bn' + str(stage) + block + '_branch' | ||
|
||
x = Convolution2D(nb_filter1, 1, 1, subsample=strides, | ||
name=conv_name_base + '2a')(input_tensor) | ||
x = BatchNormalization(axis=bn_axis, name=bn_name_base + '2a')(x) | ||
x = Activation('relu')(x) | ||
|
||
x = Convolution2D(nb_filter2, kernel_size, kernel_size, border_mode='same', | ||
name=conv_name_base + '2b')(x) | ||
x = BatchNormalization(axis=bn_axis, name=bn_name_base + '2b')(x) | ||
x = Activation('relu')(x) | ||
|
||
x = Convolution2D(nb_filter3, 1, 1, name=conv_name_base + '2c')(x) | ||
x = BatchNormalization(axis=bn_axis, name=bn_name_base + '2c')(x) | ||
|
||
shortcut = Convolution2D(nb_filter3, 1, 1, subsample=strides, | ||
name=conv_name_base + '1')(input_tensor) | ||
shortcut = BatchNormalization(axis=bn_axis, name=bn_name_base + '1')(shortcut) | ||
|
||
x = merge([x, shortcut], mode='sum') | ||
x = Activation('relu')(x) | ||
return x | ||
|
||
|
||
def ResNet(input_tensor=None): | ||
img_input = input_tensor | ||
if K.image_dim_ordering() == 'tf': | ||
bn_axis = 3 | ||
else: | ||
bn_axis = 1 | ||
|
||
x = ZeroPadding2D((3, 3))(img_input) | ||
x = Convolution2D(64, 7, 7, subsample=(2, 2), name='conv1')(x) | ||
x = BatchNormalization(axis=bn_axis, name='bn_conv1')(x) | ||
x = Activation('relu')(x) | ||
x = MaxPooling2D((3, 3), strides=(2, 2))(x) | ||
|
||
x = conv_block(x, 3, [64, 64, 256], stage=2, block='a', strides=(1, 1)) | ||
x = identity_block(x, 3, [64, 64, 256], stage=2, block='b') | ||
x = identity_block(x, 3, [64, 64, 256], stage=2, block='c') | ||
|
||
x = conv_block(x, 3, [128, 128, 512], stage=3, block='a') | ||
x = identity_block(x, 3, [128, 128, 512], stage=3, block='b') | ||
x = identity_block(x, 3, [128, 128, 512], stage=3, block='c') | ||
x = identity_block(x, 3, [128, 128, 512], stage=3, block='d') | ||
|
||
x = conv_block(x, 3, [256, 256, 1024], stage=4, block='a') | ||
x = identity_block(x, 3, [256, 256, 1024], stage=4, block='b') | ||
x = identity_block(x, 3, [256, 256, 1024], stage=4, block='c') | ||
x = identity_block(x, 3, [256, 256, 1024], stage=4, block='d') | ||
x = identity_block(x, 3, [256, 256, 1024], stage=4, block='e') | ||
|
||
model = Model(img_input, x) | ||
|
||
return model |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters