ResNeXt介绍及keras实现

编程入门 行业动态 更新时间:2024-10-28 11:24:39

<a href=https://www.elefans.com/category/jswz/34/1735425.html style=ResNeXt介绍及keras实现"/>

ResNeXt介绍及keras实现

一,ResneXt与 ResNet 相比,相同的参数个数,结果更好:一个 101 层的 ResNeXt 网络,和 200 层的 ResNet 准确度差不多,但是计算量只有后者的一半。

二,ResneXt与 ResNet 网络block结构比较,大的结构类似,都采用了残差恒等,ResneXt增加了cardinality维度,默认参数是32。此处借鉴了 GoogLeNet 的 split-transform-merge架构。

三,ResneXt与 ResNet 网络结构及参数比较,ResneXt50与 ResNet50参数及浮点运算量差不多,但性能却高i出不少。


四,ResneXt等价模式的介绍,最右侧是 AlexNet 中提出的分组卷积,相同层的 width 分组卷积,最终作者使用的是下图最右边的模型,更加简洁并且训练更快。
五,keras的实现

def block3(x, filters, kernel_size=3, stride=1, groups=32,conv_shortcut=True, name=None):"""A residual block.# Argumentsx: input tensor.filters: integer, filters of the bottleneck layer.kernel_size: default 3, kernel size of the bottleneck layer.stride: default 1, stride of the first layer.groups: default 32, group size for grouped convolution.conv_shortcut: default True, use convolution shortcut if True,otherwise identity shortcut.name: string, block label.# ReturnsOutput tensor for the residual block."""bn_axis = 3 if backend.image_data_format() == 'channels_last' else 1if conv_shortcut is True:shortcut = layers.Conv2D((64 // groups) * filters, 1, strides=stride,use_bias=False, name=name + '_0_conv')(x)shortcut = layers.BatchNormalization(axis=bn_axis, epsilon=1.001e-5,name=name + '_0_bn')(shortcut)else:shortcut = xx = layers.Conv2D(filters, 1, use_bias=False, name=name + '_1_conv')(x)x = layers.BatchNormalization(axis=bn_axis, epsilon=1.001e-5,name=name + '_1_bn')(x)x = layers.Activation('relu', name=name + '_1_relu')(x)c = filters // groupsx = layers.ZeroPadding2D(padding=((1, 1), (1, 1)), name=name + '_2_pad')(x)x = layers.DepthwiseConv2D(kernel_size, strides=stride, depth_multiplier=c,use_bias=False, name=name + '_2_conv')(x)kernel = np.zeros((1, 1, filters * c, filters), dtype=np.float32)for i in range(filters):start = (i // c) * c * c + i % cend = start + c * ckernel[:, :, start:end:c, i] = 1.x = layers.Conv2D(filters, 1, use_bias=False, trainable=False,kernel_initializer={'class_name': 'Constant','config': {'value': kernel}},name=name + '_2_gconv')(x)x = layers.BatchNormalization(axis=bn_axis, epsilon=1.001e-5,name=name + '_2_bn')(x)x = layers.Activation('relu', name=name + '_2_relu')(x)x = layers.Conv2D((64 // groups) * filters, 1,use_bias=False, name=name + '_3_conv')(x)x = layers.BatchNormalization(axis=bn_axis, epsilon=1.001e-5,name=name + '_3_bn')(x)x = layers.Add(name=name + '_add')([shortcut, x])x = layers.Activation('relu', name=name + '_out')(x)return xdef stack3(x, filters, blocks, stride1=2, groups=32, name=None):"""A set of stacked residual blocks.# Argumentsx: input tensor.filters: integer, filters of the bottleneck layer in a block.blocks: integer, blocks in the stacked blocks.stride1: default 2, stride of the first layer in the first block.groups: default 32, group size for grouped convolution.name: string, stack label.# ReturnsOutput tensor for the stacked blocks."""x = block3(x, filters, stride=stride1, groups=groups, name=name + '_block1')for i in range(2, blocks + 1):x = block3(x, filters, groups=groups, conv_shortcut=False,name=name + '_block' + str(i))return xdef ResNet(stack_fn,preact,use_bias,model_name='resnet',include_top=True,weights='imagenet',input_tensor=None,input_shape=None,pooling=None,classes=1000,**kwargs):"""Instantiates the ResNet, ResNetV2, and ResNeXt architecture.Optionally loads weights pre-trained on ImageNet.Note that the data format convention used by the model isthe one specified in your Keras config at `~/.keras/keras.json`.# Argumentsstack_fn: a function that returns output tensor for thestacked residual blocks.preact: whether to use pre-activation or not(True for ResNetV2, False for ResNet and ResNeXt).use_bias: whether to use biases for convolutional layers or not(True for ResNet and ResNetV2, False for ResNeXt).model_name: string, model name.include_top: whether to include the fully-connectedlayer at the top of the network.weights: one of `None` (random initialization),'imagenet' (pre-training on ImageNet),or the path to the weights file to be loaded.input_tensor: optional Keras tensor(i.e. output of `layers.Input()`)to use as image input for the model.input_shape: optional shape tuple, only to be specifiedif `include_top` is False (otherwise the input shapehas to be `(224, 224, 3)` (with `channels_last` data format)or `(3, 224, 224)` (with `channels_first` data format).It should have exactly 3 inputs channels.pooling: optional pooling mode for feature extractionwhen `include_top` is `False`.- `None` means that the output of the model will bethe 4D tensor output of thelast convolutional layer.- `avg` means that global average poolingwill be applied to the output of thelast convolutional layer, and thusthe output of the model will be a 2D tensor.- `max` means that global max pooling willbe applied.classes: optional number of classes to classify imagesinto, only to be specified if `include_top` is True, andif no `weights` argument is specified.# ReturnsA Keras model instance.# RaisesValueError: in case of invalid argument for `weights`,or invalid input shape."""global backend, layers, models, keras_utilsbackend, layers, models, keras_utils = get_submodules_from_kwargs(kwargs)if not (weights in {'imagenet', None} or os.path.exists(weights)):raise ValueError('The `weights` argument should be either ''`None` (random initialization), `imagenet` ''(pre-training on ImageNet), ''or the path to the weights file to be loaded.')if weights == 'imagenet' and include_top and classes != 1000:raise ValueError('If using `weights` as `"imagenet"` with `include_top`'' as true, `classes` should be 1000')# Determine proper input shapeinput_shape = _obtain_input_shape(input_shape,default_size=224,min_size=32,data_format=backend.image_data_format(),require_flatten=include_top,weights=weights)if input_tensor is None:img_input = layers.Input(shape=input_shape)else:if not backend.is_keras_tensor(input_tensor):img_input = layers.Input(tensor=input_tensor, shape=input_shape)else:img_input = input_tensorbn_axis = 3 if backend.image_data_format() == 'channels_last' else 1x = layers.ZeroPadding2D(padding=((3, 3), (3, 3)), name='conv1_pad')(img_input)x = layers.Conv2D(64, 7, strides=2, use_bias=use_bias, name='conv1_conv')(x)if preact is False:x = layers.BatchNormalization(axis=bn_axis, epsilon=1.001e-5,name='conv1_bn')(x)x = layers.Activation('relu', name='conv1_relu')(x)x = layers.ZeroPadding2D(padding=((1, 1), (1, 1)), name='pool1_pad')(x)x = layers.MaxPooling2D(3, strides=2, name='pool1_pool')(x)x = stack_fn(x)if preact is True:x = layers.BatchNormalization(axis=bn_axis, epsilon=1.001e-5,name='post_bn')(x)x = layers.Activation('relu', name='post_relu')(x)if include_top:x = layers.GlobalAveragePooling2D(name='avg_pool')(x)x = layers.Dense(classes, activation='softmax', name='probs')(x)else:if pooling == 'avg':x = layers.GlobalAveragePooling2D(name='avg_pool')(x)elif pooling == 'max':x = layers.GlobalMaxPooling2D(name='max_pool')(x)# Ensure that the model takes into account# any potential predecessors of `input_tensor`.if input_tensor is not None:inputs = keras_utils.get_source_inputs(input_tensor)else:inputs = img_input# Create model.model = models.Model(inputs, x, name=model_name)# Load weights.if (weights == 'imagenet') and (model_name in WEIGHTS_HASHES):if include_top:file_name = model_name + '_weights_tf_dim_ordering_tf_kernels.h5'file_hash = WEIGHTS_HASHES[model_name][0]else:file_name = model_name + '_weights_tf_dim_ordering_tf_kernels_notop.h5'file_hash = WEIGHTS_HASHES[model_name][1]weights_path = keras_utils.get_file(file_name,BASE_WEIGHTS_PATH + file_name,cache_subdir='models',file_hash=file_hash)by_name = True if 'resnext' in model_name else Falsemodel.load_weights(weights_path, by_name=by_name)elif weights is not None:model.load_weights(weights)return modeldef ResNeXt50(include_top=True,weights='imagenet',input_tensor=None,input_shape=None,pooling=None,classes=1000,**kwargs):def stack_fn(x):x = stack3(x, 128, 3, stride1=1, name='conv2')x = stack3(x, 256, 4, name='conv3')x = stack3(x, 512, 6, name='conv4')x = stack3(x, 1024, 3, name='conv5')return xreturn ResNet(stack_fn, False, False, 'resnext50',include_top, weights,input_tensor, input_shape,pooling, classes,**kwargs)

更多推荐

ResNeXt介绍及keras实现

本文发布于:2023-07-28 16:02:15,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1243433.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:ResNeXt   keras

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!