forked from TensorLayer/tensorlayer3
153 lines
6.7 KiB
Python
153 lines
6.7 KiB
Python
from __future__ import print_function
|
|
|
|
import tensorflow as tf
|
|
import tensorlayer as tl
|
|
from tensorlayer import logging
|
|
from tensorlayer.files import (assign_weights, maybe_download_and_extract)
|
|
from tensorlayer.layers import (BatchNorm, Conv2d, Dense, Elementwise, GlobalMeanPool2d, Input, MaxPool2d)
|
|
from tensorlayer.layers import Module, SequentialLayer,ZeroPad2d,AdaptiveMaxPool2d,TimeDistributedLayer,LambdaLayer
|
|
def identity_block(input_tensor, kernel_size, filters, stage, block):
|
|
filters1, filters2, filters3 = filters
|
|
conv_name_base = 'res' + str(stage) + block + '_branch'
|
|
bn_name_base = 'bn' + str(stage) + block + '_branch'
|
|
|
|
x = Conv2d(filters1, (1, 1),name=conv_name_base+"2a")(input_tensor)
|
|
x = BatchNorm(is_train=False,name=bn_name_base+'2a',act="relu",num_features=filters1)(x)
|
|
|
|
x = Conv2d(filters2, (1, 1), name=conv_name_base + "2b")(x)
|
|
x = BatchNorm(is_train=False, name=bn_name_base + '2b', act="relu",num_features=filters2)(x)
|
|
|
|
x = Conv2d(filters3, (1, 1), name=conv_name_base + "2c")(x)
|
|
x = BatchNorm(is_train=False, name=bn_name_base + '2c',num_features=filters3)(x)
|
|
|
|
x = Elementwise(tl.add,act="relu")([input_tensor,x])
|
|
|
|
return x
|
|
|
|
|
|
def conv_block(input_tensor, kernel_size, filters, stage, block, strides=(2, 2)):
|
|
|
|
filters1, filters2, filters3 = filters
|
|
|
|
conv_name_base = 'res' + str(stage) + block + '_branch'
|
|
bn_name_base = 'bn' + str(stage) + block + '_branch'
|
|
|
|
x = Conv2d(filters1, (1, 1), strides=strides,name=conv_name_base + '2a')(input_tensor)
|
|
x = BatchNorm(is_train=False, name=bn_name_base + '2a',num_features=filters1,act="relu")(x)
|
|
|
|
x = Conv2d(filters2, (kernel_size,kernel_size), padding='same', name=conv_name_base + '2b')(x)
|
|
x = BatchNorm(is_train=False, name=bn_name_base + '2b',num_features=filters2,act="relu")(x)
|
|
|
|
|
|
x = Conv2d(filters3, (1, 1), name=conv_name_base + '2c')(x)
|
|
x = BatchNorm(is_train=False, name=bn_name_base + '2c',num_features=filters3)(x)
|
|
|
|
shortcut = Conv2d(filters3, (1, 1), strides=strides, name=conv_name_base + '1')(input_tensor)
|
|
shortcut = BatchNorm(is_train=False, name=bn_name_base + '1')(shortcut)
|
|
|
|
x = Elementwise(tl.add,act="relu")([x, shortcut])
|
|
return x
|
|
|
|
|
|
def ResNet50(inputs):
|
|
#-----------------------------------#
|
|
# 假设输入进来的图片是600,600,3
|
|
#-----------------------------------#
|
|
img_input = inputs
|
|
|
|
# 600,600,3 -> 300,300,64
|
|
x = ZeroPad2d((3, 3))(img_input)
|
|
x = Conv2d(64, (7, 7), strides=(2, 2), name='conv1')(x)
|
|
x = BatchNorm(is_train=False, name='bn_conv1',act="relu",num_features=64)(x)
|
|
|
|
|
|
# 300,300,64 -> 150,150,64
|
|
x = MaxPool2d((3, 3), strides=(2, 2), padding="same")(x)
|
|
|
|
# 150,150,64 -> 150,150,256
|
|
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')
|
|
|
|
# 150,150,256 -> 75,75,512
|
|
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')
|
|
|
|
# 75,75,512 -> 38,38,1024
|
|
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')
|
|
x = identity_block(x, 3, [256, 256, 1024], stage=4, block='f')
|
|
|
|
# 最终获得一个38,38,1024的共享特征层
|
|
return x
|
|
|
|
def identity_block_td(input_tensor, kernel_size, filters, stage, block):
|
|
nb_filter1, nb_filter2, nb_filter3 = filters
|
|
conv_name_base = 'res' + str(stage) + block + '_branch'
|
|
bn_name_base = 'bn' + str(stage) + block + '_branch'
|
|
|
|
x = TimeDistributedLayer(Conv2d(nb_filter1, (1, 1),) ,name=conv_name_base + '2a')(input_tensor)
|
|
x = TimeDistributedLayer(BatchNorm(is_train=False, act="relu"),name=bn_name_base+ '2a')(x)
|
|
|
|
|
|
x = TimeDistributedLayer(Conv2d(nb_filter2, (kernel_size, kernel_size),padding='same'), name=conv_name_base + '2b')(x)
|
|
x = TimeDistributedLayer(BatchNorm(is_train=False,act="relu"),name=bn_name_base+ '2b')(x)
|
|
|
|
|
|
x = TimeDistributedLayer(Conv2d(nb_filter3, (1, 1)) ,name=conv_name_base + '2c')(x)
|
|
x = TimeDistributedLayer(BatchNorm(is_train=False),name=bn_name_base+ '2c')(x)
|
|
|
|
x = Elementwise(tl.add,act="relu")([x,input_tensor])
|
|
|
|
|
|
return x
|
|
|
|
def conv_block_td(input_tensor, kernel_size, filters, stage, block, strides=(2, 2)):
|
|
nb_filter1, nb_filter2, nb_filter3 = filters
|
|
conv_name_base = 'res' + str(stage) + block + '_branch'
|
|
bn_name_base = 'bn' + str(stage) + block + '_branch'
|
|
|
|
x = TimeDistributedLayer(Conv2d(nb_filter1, (1, 1), strides=strides), name=conv_name_base + '2a')(input_tensor)
|
|
x = TimeDistributedLayer(BatchNorm(is_train=False,act="relu"), name=bn_name_base + '2a')(x)
|
|
|
|
x = TimeDistributedLayer(Conv2d(nb_filter2, (kernel_size, kernel_size), padding='same'), name=conv_name_base + '2b')(x)
|
|
x = TimeDistributedLayer(BatchNorm(is_train=False,act="relu"),name=bn_name_base + '2b')(x)
|
|
|
|
|
|
x = TimeDistributedLayer(Conv2d(nb_filter3, (1, 1)), name=conv_name_base + '2c')(x)
|
|
x = TimeDistributedLayer(BatchNorm(is_train=False), name=bn_name_base + '2c')(x)
|
|
|
|
shortcut = TimeDistributedLayer(Conv2d(nb_filter3, (1, 1), strides=strides), name=conv_name_base + '1')(input_tensor)
|
|
shortcut = TimeDistributedLayer(BatchNorm(is_train=False),name=bn_name_base + '1')(shortcut)
|
|
|
|
x = Elementwise(tl.add,act="relu")([x,shortcut])
|
|
return x
|
|
|
|
|
|
def classifier_layers(x):
|
|
# num_rois, 14, 14, 1024 -> num_rois, 7, 7, 2048
|
|
x = conv_block_td(x, 3, [512, 512, 2048], stage=5, block='a', strides=(2, 2))
|
|
# num_rois, 7, 7, 2048 -> num_rois, 7, 7, 2048
|
|
x = identity_block_td(x, 3, [512, 512, 2048], stage=5, block='b')
|
|
# num_rois, 7, 7, 2048 -> num_rois, 7, 7, 2048
|
|
x = identity_block_td(x, 3, [512, 512, 2048], stage=5, block='c')
|
|
# num_rois, 7, 7, 2048 -> num_rois, 1, 1, 2048
|
|
x = AdaptiveMaxPool2d((7, 7), name='avg_pool')(x)
|
|
|
|
return x
|
|
|
|
if __name__=="__main__":
|
|
import numpy as np
|
|
intput=Input(shape=(1,600,600,3))
|
|
model=ResNet50(intput)
|
|
image = (np.random.rand(1,224, 224, 3)).astype(np.float32)
|
|
transform = tl.vision.transforms.Resize(size=(600, 600), interpolation='bilinear')
|
|
image = transform(image)
|
|
bb=ResNet50(image)
|
|
print(image.shape)
|
|
print(bb.shape) |