keras TensorFlow_tensorflow 安装

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


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)
全栈程序员-站长的头像全栈程序员-站长

相关推荐

  • 搭建rsyslog日志服务器_syslog服务器

    搭建rsyslog日志服务器_syslog服务器文章目录1.rsyslog介绍2.实验目的3.实验环境4.配置服务端5.配置客户端6.在服务端验证效果1.rsyslog介绍  rsyslog是一个快速处理收集系统日志的开源程序,提供了高性能、安全功能和模块化设计。rsyslog是syslog的升级版,它将多种来源输入输出转换结果到目的地,rsyslog被广泛用于Linux系统以通过TCP/UDP协议转发或接收日志消息。  rsyslog守护进程可以被配置成两种环境,一种是配置成日志收集服务器,rsysl

    2022年9月25日
  • HTML embed 标签「建议收藏」

    HTML embed 标签「建议收藏」HTMLembed标签embed标签–定义网页中嵌入除图片外的多媒体不符合标准网页设计的理念,不赞成使用.embed标签是单独出现的,以开始,结束使用embed标签可以在网页中嵌入Flash,Mid,MP3等嵌入式内容embed标签已经被符合标准的object标签代替。属性Common–一般属性align–对齐方式autostart–是否

    2022年10月21日
  • 2012服务器系统密钥,WINDOWS SERVER 2012标准版密钥[通俗易懂]

    2012服务器系统密钥,WINDOWS SERVER 2012标准版密钥[通俗易懂]js获取浏览器内核、类型、版本以及系统类型正则表达式:varrsys=/\b(windows|win32|macintosh|macosx|adobeair|linux|unix)\b/;varrkn=/\b(opera|…docker组件(c/s)Docker组件1.dockerclient:docker…

    2022年8月31日
  • 记1024

    记1024等我写一个重磅级的博客。。。

    2022年6月24日
  • jdk1.8下载与安装教程(win10)超详细[通俗易懂]

    jdk1.8下载与安装教程(win10)超详细[通俗易懂]一、前言虽然现在jdk已经升级到JDK11版本了,但是JDK1.8(JDK8)仍然有很多小伙伴在使用,这里也记录一下jdk1.8的下载及安装过程,对于刚学习java的小伙伴可以参考,熟手可忽略,呵呵~二、下载安装步骤一、首先是去jdk官网下载,如果大家不怕麻烦的话可以去官网下载,需要注册账号,此外下载速度也是龟速,我也是花了好长时间才下载下来,大家可以可以点击我的网盘目录jdk目录下载…

    2022年6月14日
  • pandas’_pandas常用方法

    pandas’_pandas常用方法文章目录1.pd.Series.fillna官方案例2.pd.DataFrame.fillna官方案例1.pd.Series.fillnaSeries.fillna(value=None,method=None,axis=None,inplace=False,limit=None,downcast=None)描述按照指定的方法填充NA/NaN值参数value:scalar,dict,Series,orDataFrame标量值或字典对象用于填充缺失值要填充的值,该

    2022年8月12日

发表回复

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

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