keras TensorFlow_tensorflow 安装

keras TensorFlow_tensorflow 安装keras里面tensorflow版ResNet101源码分析”””Adaptedfromhttps://gist.github.com/flyyufelix/65018873f8cb2bbe95f429c474aa1294改编自flyyufelix注意:keras支持的Tensorflow—-UsingTensorFlowbackend(需要修改相应的配置文件)ker…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新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

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/188989.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 2021年Android面试题及答案收集(不断更新中)

    2021年Android面试题及答案(适合中高级)找工作、招人必备之良品。后期不断完善中……

    2022年4月11日
    46
  • Python设置环境变量

    Python设置环境变量文章目录一、我们安装Python。二、设置环境变量。1、找到计算机属性。2、设置PATH。一、我们安装Python。点击链接下载:python下载你也可以无脑安装,对计算机的影响不打,不过建议别无脑我们选择自己安装,然后选择想要安装的目录,不然我们能看见在InstallNow下面有一个目录那个就是默认的安装C盘位置,当然若你觉得这样好找python也可以直接点第一个安装这里如果看不懂可以不管,也可以根据自我需求来点,可以用微信的拍照翻译二、设置环境变量。1、找到计算机属性。我们右击桌

    2022年10月22日
    0
  • drf 教程_drm限制

    drf 教程_drm限制一、drf的安装1djangorestframework:django的app,只能再django上使用-djangorestframework是django的一个app,更快速在django框

    2022年8月6日
    1
  • C# 解析 sln 文件

    C# 解析 sln 文件我的项目,编码工具需要检测打开一个工程,获取所有项目。但是发现原来的方法,如果存在文件夹,把项目放在文件夹中,那么是无法获得项目,于是我就找了一个方法去获得sln文件的所有项目。

    2022年4月28日
    38
  • 宽度学习系统:一种不需要深度结构的高效增量学习系统「建议收藏」

    宽度学习系统:一种不需要深度结构的高效增量学习系统「建议收藏」宽度学习系统:一种不需要深度结构的高效增量学习系统本文是对陈俊龙教授团队“BroadLearningSystem:AnEffectiveandEfficientIncrementalLearningSystemWithouttheNeedforDeepArchitecture”的中文综述,原文将在IEEETransactionsonNeuralNetwor…

    2022年5月21日
    32
  • Java cast_java concat方法

    Java cast_java concat方法在说ClassCastException之前,先介绍下引用类型转换;引用类型转换分为向上转型和向下转型两种;向上转型:多态本身是子类类型向父类类型向上转换的过程,这个过程是默认的;当父类引用指向一个子类对象时,便是向上转换;使用格式:父类类型变量名=new子类类型();向下转型:父类类型向子类类型向下转换的过程,这个过程时强制;一个已经向上转型的子类对象,将父类引用转为子类引用,可以使用强…

    2022年9月4日
    3

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号