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


相关推荐

  • 版本号/缓存刷新 laravel mix函数

    版本号/缓存刷新 laravel mix函数

    2021年10月23日
    44
  • php unicode编码_js unicode转中文

    php unicode编码_js unicode转中文php实现汉字转unicode编码的方法:首先创建一个PHP示例文件;然后通过“functionUnicodeEncode($str){…}”方法将指定汉字转换为unicode字符串即可。本文操作环境:Windows7系统、PHP7.1、DellG3电脑。下面来看PHPUnicode编码方法,将中文转为Unicode字符,例如将新浪微博转换为unicode字符串,代码如下:functio…

    2025年7月20日
    2
  • pycharm怎么打开工具栏_pycharm菜单栏介绍

    pycharm怎么打开工具栏_pycharm菜单栏介绍  

    2022年8月26日
    6
  • ACT初代奥特曼_ac自动机上dp

    ACT初代奥特曼_ac自动机上dp上帝手中有 N 种世界元素,每种元素可以限制另外 1 种元素,把第 i 种世界元素能够限制的那种世界元素记为 A[i]。现在,上帝要把它们中的一部分投放到一个新的空间中去建造世界。为了世界的和平与安宁,上帝希望所有被投放的世界元素都有至少一个没有被投放的世界元素限制它。上帝希望知道,在此前提下,他最多可以投放多少种世界元素?输入格式第一行是一个整数 N,表示世界元素的数目。第二行有 N 个整数 A[1],A[2],…,A[N]。A[i] 表示第 i 个世界元素能够限制的世界元素的编号。输出格式

    2022年8月9日
    6
  • 广告平台精准推送系统解决方案架构「建议收藏」

    广告平台精准推送系统解决方案架构「建议收藏」以上就是广告精准推送的一个架构图。广告联盟是由多家广告提供商提供形成的一个组织,提供了多个平台的收集到的数据进行整合,数据的分析、清理,计算、统计等,提供向需要投放广告的广告主提供了一个投放系统平台。当用户进入门户网站或者app时,不同的用户看到的是不同的广告,广告联盟的系统计算出了不同用户或者用户群体的不同需求,通过广告推荐引擎系统和数据仓库中的统计数据以及用户的需求,展示给对应需求的用户观看,…

    2022年10月5日
    1
  • Android学习(简单使用Bottom Navigation Activity来实现底部导航栏)

    Android学习(简单使用Bottom Navigation Activity来实现底部导航栏)在我们实际编写程序时,不必每一个activity都要从零开始,利用好系统自带的模板往往可以起到事半功倍的效果。下面我们就来看看如何使用BottomNavigationActivity来完成简单的底部导航栏功能。先来看一下效果图吧:创建activity首先在创建面板,我们选择然后next,finish就OK了。创建成功以后我们来运行一下,发现已经基本实现了底部导航栏的功能了!但是还没有结…

    2025年6月16日
    2

发表回复

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

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