大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。
Jetbrains全系列IDE稳定放心使用
keras里面tensorflow版ResNet101源码分析
"""
Adapted from https://gist.github.com/flyyufelix/65018873f8cb2bbe95f429c474aa1294
改编自 flyyufelix
注意:keras支持的Tensorflow----Using TensorFlow backend(需要修改相应的配置文件)
keras其实只是再把tensorflow封装一次,除此以外还可以接Theano以及CNTK后端,
你每次import keras后,都会显示这样的:Using TensorFlow backend,
这就是你用的tensorflow做后端的意思,后端是可以改的,具体方法你们自己百度
一般是先把图片转换成HDF5格式储存的,优点是读取快速方便
conv_block和identity_block其实就是ResNet的基本模块,
它们的区别是conv_block的旁路是直接一条线,identity_block的旁路有一个卷积层。
之所以有的基本模块旁路一条线,有的基础模块旁路会有卷积层,是为了保证旁路出来的featuremap和主路的featuremap尺寸一致,这样它们才能相加
"""
import sys
from keras.layers import Input, Dense, Conv2D, MaxPooling2D, AveragePooling2D, ZeroPadding2D, Flatten, Activation, add
from keras.layers.normalization import BatchNormalization
from keras.models import Model
from keras import initializers
from keras.engine import Layer, InputSpec
from keras import backend as K
from keras.regularizers import l2 # L2正则化
# 设置最大递归层数
sys.setrecursionlimit(3000)
class Scale(Layer):
'''Learns a set of weights and biases used for scaling the input data.
the output consists simply in an element-wise multiplication of the input and a sum of a set of constants:
学习一组用于缩放输入数据的权重和偏差。
输出仅由输入元素的乘法和一组常量的和组成。
out = in * gamma + beta,
where 'gamma' and 'beta' are the weights and biases larned.
# Arguments
axis: integer, axis along which to normalize in mode 0. For instance,
if your input tensor has shape (samples, channels, rows, cols),
set axis to 1 to normalize per feature map (channels axis).
momentum: momentum in the computation of the
exponential average of the mean and standard deviation
of the data, for feature-wise normalization.
weights: Initialization weights.
List of 2 Numpy arrays, with shapes:
`[(input_shape,), (input_shape,)]`
beta_init: name of initialization function for shift parameter
(see [initializations](../initializations.md)), or alternatively,
Theano/TensorFlow function to use for weights initialization.
This parameter is only relevant if you don't pass a `weights` argument.
gamma_init: name of initialization function for scale parameter (see
[initializations](../initializations.md)), or alternatively,
Theano/TensorFlow function to use for weights initialization.
This parameter is only relevant if you don't pass a `weights` argument.
gamma_init: name of initialization function for scale parameter (see
[initializations](../initializations.md)), or alternatively,
Theano/TensorFlow function to use for weights initialization.
This parameter is only relevant if you don't pass a `weights` argument.
'''
def __init__(self, weights=None, axis=-1, momentum=0.9, beta_init='zero', gamma_init='one', **kwargs):
self.momentum = momentum
self.axis = axis
self.beta_init = initializers.get(beta_init)
self.gamma_init = initializers.get(gamma_init)
self.initial_weights = weights
super(Scale, self).__init__(**kwargs)
def build(self, input_shape):
self.input_spec = [InputSpec(shape=input_shape)]
shape = (int(input_shape[self.axis]),)
self.gamma = K.variable(self.gamma_init(shape), name='{}_gamma'.format(self.name))
self.beta = K.variable(self.beta_init(shape), name='{}_beta'.format(self.name))
self.trainable_weights = [self.gamma, self.beta]
if self.initial_weights is not None:
self.set_weights(self.initial_weights)
del self.initial_weights
def call(self, x, mask=None):
input_shape = self.input_spec[0].shape
broadcast_shape = [1] * len(input_shape)
broadcast_shape[self.axis] = input_shape[self.axis]
out = K.reshape(self.gamma, broadcast_shape) * x + K.reshape(self.beta, broadcast_shape)
return out
def get_config(self):
config = {"momentum": self.momentum, "axis": self.axis}
base_config = super(Scale, self).get_config()
return dict(list(base_config.items()) + list(config.items()))
# identity_block是在shortcut(旁路)方式下没有conv层的block
# identity_block的旁路是直接一条线
def identity_block(input_tensor, kernel_size, filters, stage, block, weight_decay=0):
'''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 # 整数列表,主路径上3个conv层的nb_filters
stage: integer, current stage label, used for generating layer names # 整数,当前阶段标签,用于生成层名称
block: 'a','b'..., current block label, used for generating layer names
'''
eps = 1.1e-5
nb_filter1, nb_filter2, nb_filter3 = filters # 例如: [64, 64, 256]
conv_name_base = 'res' + str(stage) + block + '_branch'
bn_name_base = 'bn' + str(stage) + block + '_branch'
scale_name_base = 'scale' + str(stage) + block + '_branch'
# identity_block(x, 3, [64, 64, 256], stage=2, block='b', weight_decay=weight_decay)
# 1x1x64的卷积
x = Conv2D(nb_filter1, (1, 1), name=conv_name_base + '2a', use_bias=False, kernel_regularizer=l2(weight_decay))(input_tensor)
x = BatchNormalization(epsilon=eps, axis=bn_axis, name=bn_name_base + '2a', gamma_regularizer=l2(weight_decay),
beta_regularizer=l2(weight_decay))(x)
x = Scale(axis=bn_axis, name=scale_name_base + '2a')(x)
x = Activation('relu', name=conv_name_base + '2a_relu')(x)
x = ZeroPadding2D((1, 1), name=conv_name_base + '2b_zeropadding')(x)
# 3x3x64的卷积
x = Conv2D(nb_filter2, (kernel_size, kernel_size),
name=conv_name_base + '2b', use_bias=False, kernel_regularizer=l2(weight_decay))(x)
x = BatchNormalization(epsilon=eps, axis=bn_axis, name=bn_name_base + '2b', gamma_regularizer=l2(weight_decay),
beta_regularizer=l2(weight_decay))(x)
x = Scale(axis=bn_axis, name=scale_name_base + '2b')(x)
x = Activation('relu', name=conv_name_base + '2b_relu')(x)
# 1x1x256的卷积
x = Conv2D(nb_filter3, (1, 1), name=conv_name_base + '2c', use_bias=False, kernel_regularizer=l2(weight_decay))(x)
x = BatchNormalization(epsilon=eps, axis=bn_axis, name=bn_name_base + '2c', gamma_regularizer=l2(weight_decay),
beta_regularizer=l2(weight_decay))(x)
x = Scale(axis=bn_axis, name=scale_name_base + '2c')(x)
x = add([x, input_tensor], name='res' + str(stage) + block)
x = Activation('relu', name='res' + str(stage) + block + '_relu')(x)
return x
# conv_block 是在shortcut 方式下具有conv层的块
# conv_block的旁路有一个卷积层
def conv_block(input_tensor, kernel_size, filters, stage, block, strides=(2, 2), weight_decay=0):
'''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
'''
eps = 1.1e-5
nb_filter1, nb_filter2, nb_filter3 = filters # [64, 64, 256]
conv_name_base = 'res' + str(stage) + block + '_branch'
bn_name_base = 'bn' + str(stage) + block + '_branch'
scale_name_base = 'scale' + str(stage) + block + '_branch'
# conv_block(x, 3, [64, 64, 256], stage=2, block='a', strides=(1, 1), weight_decay=weight_decay)
# 1x1x64的卷积
x = Conv2D(nb_filter1, (1, 1), strides=strides,
name=conv_name_base + '2a', use_bias=False, kernel_regularizer=l2(weight_decay))(input_tensor)
x = BatchNormalization(epsilon=eps, axis=bn_axis, name=bn_name_base + '2a', gamma_regularizer=l2(weight_decay),
beta_regularizer=l2(weight_decay))(x)
x = Scale(axis=bn_axis, name=scale_name_base + '2a')(x)
x = Activation('relu', name=conv_name_base + '2a_relu')(x)
x = ZeroPadding2D((1, 1), name=conv_name_base + '2b_zeropadding')(x)
# 3x3x64的卷积
x = Conv2D(nb_filter2, (kernel_size, kernel_size),
name=conv_name_base + '2b', use_bias=False, kernel_regularizer=l2(weight_decay))(x)
x = BatchNormalization(epsilon=eps, axis=bn_axis, name=bn_name_base + '2b', gamma_regularizer=l2(weight_decay),
beta_regularizer=l2(weight_decay))(x)
x = Scale(axis=bn_axis, name=scale_name_base + '2b')(x)
x = Activation('relu', name=conv_name_base + '2b_relu')(x)
# 1x1x256的卷积
x = Conv2D(nb_filter3, (1, 1), name=conv_name_base + '2c', use_bias=False, kernel_regularizer=l2(weight_decay))(x)
x = BatchNormalization(epsilon=eps, axis=bn_axis, name=bn_name_base + '2c', gamma_regularizer=l2(weight_decay),
beta_regularizer=l2(weight_decay))(x)
x = Scale(axis=bn_axis, name=scale_name_base + '2c')(x)
shortcut = Conv2D(nb_filter3, (1, 1), strides=strides,
name=conv_name_base + '1', use_bias=False, kernel_regularizer=l2(weight_decay))(input_tensor)
shortcut = BatchNormalization(epsilon=eps, axis=bn_axis, name=bn_name_base + '1', gamma_regularizer=l2(weight_decay),
beta_regularizer=l2(weight_decay))(shortcut)
shortcut = Scale(axis=bn_axis, name=scale_name_base + '1')(shortcut)
x = add([x, shortcut], name='res' + str(stage) + block)
x = Activation('relu', name='res' + str(stage) + block + '_relu')(x)
return x
def resnet101(no_classes, initialization='imagenet', weight_decay=0, final_activation=None):
# no_classes:数据集
# initialization: 初始化方式
# weight_decay: 权重衰减量
'''Instantiate the ResNet101 architecture,
# Arguments
weights_path: path to pretrained weight file
# Returns
A Keras model instance.
'''
if initialization == 'imagenet': # 使用imagenet预训练的权重做初始化
weights_path = 'pre_model_weight/resnet101_weights_tf.h5'
else:
weights_path = None
eps = 1.1e-5
# Handle Dimension Ordering for different backends
# 不同后端的处理 维度排序
global bn_axis
if K.image_dim_ordering() == 'tf':
bn_axis = 3 # “channels_last”对应原本的“tf”
img_input = Input(shape=(224, 224, 3), name='data') # 224x224x3
else:
bn_axis = 1 # “channels_first”对应原本的“th”
img_input = Input(shape=(3, 224, 224), name='data')
# keras.layers.convolutional.ZeroPadding2D(padding=(1, 1), dim_ordering='default')
# padding:整数tuple,表示在要填充的轴的起始和结束处填充0的数目,这里要填充的轴 是轴3和轴4(即在'th'模式下图像的行和列,在‘channels_last’模式下要填充的则是轴2,3)
# data_format:字符串,“channels_first”或“channels_last”之一,代表图像的通道维的位置。
# “channels_last”对应原本的“tf”,“channels_first”对应原本的“th”。
# 以128x128的RGB图像为例,“channels_first”应将数据组织为(3,128,128),而“channels_last”应将数据组织为(128,128,3)。
# 该参数的默认值是~/.keras/keras.json中设置的值,若从未设置过,则为“channels_last”。
# 对2D输入(如图片)的边界填充0,以控制卷积以后特征图的大小
# padding= (1,0),会在行的最前和最后都增加一行0 比方说,原来的尺寸为(None,20,11,1),padding之后就会变成(None,22,11,1).
# padding= (1,1),会在行和列的最前和最后都增加一行0 比方说,原来的尺寸为(None,20,11,1),padding之后就会变成(None,22,13,1).
x = ZeroPadding2D((3, 3), name='conv1_zeropadding')(img_input) # 图像用0填充
# conv2d(x, kernel, strides=(1, 1), border_mode='valid', dim_ordering='th', image_shape=None, filter_shape=None)
# 参数:
# kernel:卷积核张量
# strides:步长,长为2的tuple
# border_mode:“same”,“valid”之一的字符串
# dim_ordering:“tf”和“th”之一,维度排列顺序
# kernel_regularizer:施加在权重上的正则项,为keras.regularizer.Regularizer对象
# bias_regularizer:施加在偏置向量上的正则项,为keras.regularizer.Regularizer对象
# activity_regularizer:施加在输出上的正则项,为keras.regularizer.Regularizer对象
x = Conv2D(64, (7, 7), strides=(2, 2), name='conv1', use_bias=False, kernel_regularizer=l2(weight_decay))(x)
# keras.layers.normalization.BatchNormalization(axis=-1, momentum=0.99, epsilon=0.001, center=True, scale=True, beta_initializer='zeros',
# gamma_initializer='ones', moving_mean_initializer='zeros', moving_variance_initializer='ones',
# beta_regularizer=None, gamma_regularizer=None, beta_constraint=None, gamma_constraint=None)
# epsilon:大于0的小浮点数,用于防止除0错误
# axis: 整数,指定要规范化的轴,通常为特征轴。例如在进行data_format="channels_first的2D卷积后,一般会设axis=1
# gamma_regularizer: 可选的gamma正则
# beta_regularizer: 可选的beta正则
x = BatchNormalization(epsilon=eps, axis=bn_axis, name='bn_conv1', gamma_regularizer=l2(weight_decay), beta_regularizer=l2(weight_decay))(x)
x = Scale(axis=bn_axis, name='scale_conv1')(x)
x = Activation('relu', name='conv1_relu')(x)
x = MaxPooling2D((3, 3), strides=(2, 2), name='pool1')(x)
""" conv_2 阶段"""
# conv_block(input_tensor, kernel_size, filters, stage, block, strides=(2, 2), weight_decay=0)
x = conv_block(x, 3, [64, 64, 256], stage=2, block='a', strides=(1, 1), weight_decay=weight_decay)
# identity_block(input_tensor, kernel_size, filters, stage, block, weight_decay=0)
x = identity_block(x, 3, [64, 64, 256], stage=2, block='b', weight_decay=weight_decay)
x = identity_block(x, 3, [64, 64, 256], stage=2, block='c', weight_decay=weight_decay)
""" conv_3 阶段"""
x = conv_block(x, 3, [128, 128, 512], stage=3, block='a', weight_decay=weight_decay)
for i in list(range(1, 3)):
x = identity_block(x, 3, [128, 128, 512], stage=3, block='b'+str(i), weight_decay=weight_decay)
""" conv_4 阶段"""
x = conv_block(x, 3, [256, 256, 1024], stage=4, block='a', weight_decay=weight_decay)
for i in list(range(1, 23)):
x = identity_block(x, 3, [256, 256, 1024], stage=4, block='b'+str(i), weight_decay=weight_decay)
""" conv_5 阶段"""
x = conv_block(x, 3, [512, 512, 2048], stage=5, block='a', weight_decay=weight_decay)
x = identity_block(x, 3, [512, 512, 2048], stage=5, block='b', weight_decay=weight_decay)
x = identity_block(x, 3, [512, 512, 2048], stage=5, block='c', weight_decay=weight_decay)
# 平均池化
x_fc = AveragePooling2D((7, 7), name='avg_pool')(x)
# 维度变换
x_fc = Flatten()(x_fc)
# 全连接层
# keras.layers.core.Dense(units, activation=None, use_bias=True, kernel_initializer='glorot_uniform', bias_initializer='zeros',
# kernel_regularizer=None, bias_regularizer=None, activity_regularizer=None, kernel_constraint=None, bias_constraint=None)
# units:大于0的整数,代表该层的输出维度
# use_bias: 布尔值,是否使用偏置项
# activation:激活函数,为预定义的激活函数名(参考激活函数),或逐元素(element-wise)的Theano函数。如果不指定该参数,将不会使用任何激活函数(即使用线性激活函数:a(x)=x)
# kernel_regularizer:施加在权重上的正则项,为Regularizer对象
x_fc = Dense(no_classes, activation=final_activation, name='fc_final' + str(no_classes), kernel_regularizer=l2(weight_decay))(x_fc)
# Model(inputs=image_input,outputs= out)
# 旧版本中:model=Model( input=layer1,output=layer2 )
# 新版本中:model=Model( inputs=layer1,outputs=layer2 )
model = Model(img_input, x_fc)
# load weights
# weights_path为权重的具体位置--pre_model_weight/resnet101_weights_tf.h5
if weights_path:
model.load_weights(weights_path, by_name=True) # 加载权重
return model
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/188989.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...