mobilenet_psp_u

#1.encoder_mobilenet
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 ]
#2.decoder_pspnet
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):#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_factor=[1,2,3,6],#strides=[18,18],[9,9],[6,6],[3,3]or other conditions
    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
    #对f5进行不同因子的池化
    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])#与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])#与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))#扩大2倍
    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
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我是小z呀

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值