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


相关推荐

  • vue中import作用「建议收藏」

    vue中import作用「建议收藏」html文件中,通过script src = ‘xxx’标签引入js文件。而vue中,通过import xxx from xxx路径 的方式导入文件,不光可以导入js文件。“xxx”指的是为导入的文件起一个名称,不是指导入的文件的名称,相当于变量名。“xxx路径”指的是文件的相对路径.例如:在App.vue中导入index 和 content分别可以写:import…

    2022年6月13日
    46
  • 阿里矢量图标库的使用

    阿里矢量图标库的使用1、注册登录账号网站地址:https://www.iconfont.cn/该网站可通过GitHub或新浪账号进行登录下面是网站的首页2、查找图标在中间输入框输入想要查找的图标,。以‘home

    2022年8月6日
    4
  • python 之免费ip代理池[通俗易懂]

    python 之免费ip代理池[通俗易懂]基于proxy_pool,部署了一个开放的免费ip代理池,提供出来供大家使用。数据有效性每2分钟更新一次。地址:http://proxy.linuxdba.ltd/all/开源项目地址:https://github.com/jhao104/proxy_pool

    2022年5月29日
    57
  • 【MySQL】使用Visio绘制数据库关系模型图

    【MySQL】使用Visio绘制数据库关系模型图使用Visio绘制数据库关系模型图1新建项目文件—新建–软件和数据库—数据库模型图点击后,出现如下界面:2绘制左侧“实体关系”中将“实体”形状拖放到绘制界面,如下图3编辑实体名称,如下图:4编辑列点击“列”如下图:完成实体:客人信息表GuestRecord,如下截图完成实体:客房表Room(同上操作),如下图5关系绑定5.1添加列RoomID到客人信息表5.2将“实体关系”中的关系工具拖放到某个实体上(鼠标不松开),直到该实体边框变红色,松开;箭头指

    2022年7月16日
    13
  • data gateway_交通运输部监测调度

    data gateway_交通运输部监测调度对于大数据场景,计算的业务逻辑基本都在数据处理过程中完成,最后往往会持久化下来计算的结果,存储在mysql,es,hbase等适合提供查询的地方。而对于在不同数据库上的数据,需要开发接口,管理接口也是一个不小的工作量。Dataway的出现,正好解决了这个场景下的接口开发,管理等问题。只需要通过SQL,Dataql等配置就能完成一个接口的上线,大大提高了开发效率,以及管理成本。对于数据中台建设中,Dataway可以使用在统一服务层,对接各种数据源,管理数据出口。一、安装引入依赖Da…

    2025年6月1日
    0
  • Java酒店管理系统_java酒店管理系统报告

    Java酒店管理系统_java酒店管理系统报告基于jsp+servlet+pojo+mysql实现一个javaee/javaweb的小型酒店管理系统,该项目可用各类java课程设计大作业中,小型酒店管理系统的系统架构分为前后台两部分,最终实现在线上进行小型酒店管理系统各项功能,实现了诸如用户管理,登录注册,权限管理等功能,并实现对各类小型酒店管理系统相关的实体进行管理。该小型酒店管理系统为一个采用mvc设计模式进行开发B/S架构项…

    2022年9月24日
    0

发表回复

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

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