from keras.models import *
from keras.layers import *
import keras.backend as K
import keras
IMAGE_ORDERING='channels_last'
channel_axis=1 if IMAGE_ORDERING=='channels_first' else -1
def relu6(x):
return K.relu(x,max_value=6)
def _conv_block(inputs,filters,kernel=(3,3),strides=(1,1)):
x=ZeroPadding2D(padding=(1,1),name='conv1_pad',data_format=IMAGE_ORDERING)(inputs)
x=Conv2D(filters,kernel,padding='valid',use_bias=False,strides=strides,name='conv1')(x)
x=BatchNormalization(axis=channel_axis,name='conv1_bn')(x)
return Activation(relu6,name='conv1_relu')(x)
def _depthwise_conv_block(inputs,filters,depth_multiplier,strides=(1,1),block_id=1):
x=ZeroPadding2D((1,1),name='conv_pad_%d'%block_id)(inputs)
x=DepthwiseConv2D((3,3),padding='valid',depth_multiplier=depth_multiplier,strides=strides,use_bias=False,name='conv_dw_%d'%block_id)(x)
x=BatchNormalization(axis=channel_axis,name='conv_dw_%d_bn'%block_id)(x)
x=Activation(relu6,name='conv_dw_%d_relu'%block_id)(x)
x=Conv2D(filters,(1,1),padding='same',use_bias=False,strides=(1,1),name='conv_pw_%d'%block_id)(x)
x=BatchNormalization(axis=channel_axis,name='conv_pw_%d_bn'%block_id)(x)
return Activation(relu6,name='conv_pw_%d_relu'%block_id)(x)
def get_mobilenet_encoder(input_height=224,input_width=224,pretrained='imagenet'):
depth_multiplier=1
dropout=1e-3
img_input=Input(shape=(input_height,input_width,3))
x=_conv_block(img_input,32,strides=(2,2))
x=_depthwise_conv_block(x,64,depth_multiplier,block_id=1)
f1=x
x=_depthwise_conv_block(x,128,depth_multiplier,strides=(2,2),block_id=2)
x=_depthwise_conv_block(x,128,depth_multiplier,block_id=3)
f2=x
x=_depthwise_conv_block(x,256,depth_multiplier,strides=(2,2),block_id=4)
x=_depthwise_conv_block(x,256,depth_multiplier,block_id=5)
f3=x
x=_depthwise_conv_block(x,512,depth_multiplier,strides=(2,2),block_id=6)
x = _depthwise_conv_block(x, 512,depth_multiplier, block_id=7)
x = _depthwise_conv_block(x, 512, depth_multiplier, block_id=8)
x = _depthwise_conv_block(x, 512, depth_multiplier, block_id=9)
x = _depthwise_conv_block(x, 512,depth_multiplier, block_id=10)
x = _depthwise_conv_block(x, 512,depth_multiplier, block_id=11)
f4 = x
x = _depthwise_conv_block(x, 1024,depth_multiplier,strides=(2, 2), block_id=12)
x = _depthwise_conv_block(x, 1024,depth_multiplier, block_id=13)
f5 = x
return img_input , [f1 , f2 , f3 , f4 , f5 ]
import tensorflow as tf
def resize_image(inp,s):
return Lambda(lambda x:tf.image.resize_images(x,(K.int_shape(x)[1]*s[0],K.int_shape(x)[2]*s[1])))(inp)
def pool_block(feat,pool_factor):
if IMAGE_ORDERING=='channels_first':
h=K.int_shape(feat)[2]
w=K.int_shape(feat)[3]
elif IMAGE_ORDERING=='channels_last':
h=K.int_shape(feat)[1]
w=K.int_shape(feat)[2]
pool_size=strides=[int(np.round(float(h)/pool_factor)),int(np.round(float(w)/pool_factor))]
x=AveragePooling2D(pool_size=pool_size,strides=strides,padding='same')(feat)
x=Conv2D(512,(1,1),padding='same',use_bias=False)(x)
x=BatchNormalization()(x)
x=Activation('relu')(x)
x=resize_image(x,strides)
return x
def _pspnet(n_classes,encoder,input_height=384,input_width=576):
assert input_height%192==0
assert input_width%192==0
img_input,levels=encoder(input_height=input_height,input_width=input_width)
[f1,f2,f3,f4,f5]=levels
pool_outs=[f5]
pool_factors=[1,2,3,6]
for p in pool_factors:
pooled=pool_block(f5,p)
pool_outs.append(pooled)
x=Concatenate(axis=channel_axis)(pool_outs)
x=Conv2D(512,(1,1),padding='valid')(x)
x=BatchNormalization()(x)
x=resize_image(x,(2,2))
x=Concatenate(axis=channel_axis)([x,f4])
x=Conv2D(512,(1,1),padding='valid')(x)
x=BatchNormalization()(x)
x=Activation('relu')(x)
pool_outs=[f4]
for p in pool_factors:
pooled=pool_block(x,p)
pool_outs.append(pooled)
x=Concatenate(axis=channel_axis)(pool_outs)
x=Conv2D(512,(1,1),padding='valid')(x)
x=BatchNormalization()(x)
x=resize_image(x,(2,2))
x=Concatenate(axis=channel_axis)([x,f3])
x=Conv2D(512,(1,1),padding='valid')(x)
x=BatchNormalization()(x)
x=Activation('relu')(x)
pool_outs=[f3]
for p in pool_factors:
pooled=pool_block(x,p)
pool_outs.append(pooled)
x=Concatenate(axis=channel_axis)(pool_outs)
x=Conv2D(512,(1,1),padding='valid')(x)
x=BatchNormalization()(x)
x=resize_image(x,(4,4))
x=Conv2D(n_classes,(3,3),padding='same')(x)
x=resize_image(x,(2,2))
x=Reshape((-1,n_classes))(x)
x=Softmax()(x)
model=Model(img_input,x)
return model