引擎搜索器,网站建设和seo讲师要求,wordpress 中文优化版,金华网站建设团队Residual net概念
概念#xff1a;
Residual net(残差网络)#xff1a;将靠前若干层的某一层数据输出直接跳过多层引入到后面数据层的输入 部分。 残差神经单元#xff1a;假定某段神经网络的输入是x#xff0c;期望输出是H(x)#xff0c;如果我们直接将输入x传到输出作…Residual net概念
概念
Residual net(残差网络)将靠前若干层的某一层数据输出直接跳过多层引入到后面数据层的输入 部分。 残差神经单元假定某段神经网络的输入是x期望输出是H(x)如果我们直接将输入x传到输出作 为初始结果那么我们需要学习的目标就是F(x) H(x) - x这就是一个残差神经单元相当于将 学习目标改变了不再是学习一个完整的输出H(x)只是输出和输入的差别 H(x) - x 即残差。
细节
• 普通的直连的卷积神经网络和ResNet的最大区别在 于ResNet有很多旁路的支线将输入直接连到后面 的层使得后面的层可以直接学习残差这种结构也 被称为shortcut或skip connections。 • 传统的卷积层或全连接层在信息传递时或多或少会 存在信息丢失、损耗等问题。ResNet在某种程度上 解决了这个问题通过直接将输入信息绕道传到输出 保护信息的完整性整个网络只需要学习输入、输出 差别的那一部分简化了学习目标和难度。
思路
ResNet50有两个基本的块分别名为Conv Block和Identity Block其中Conv Block输入和输出的维度 是不一样的所以不能连续串联它的作用是改变网络的维度Identity Block输入维度和输出维度相 同可以串联用于加深网络的。
resent50代码实现
网络主体部分
#-------------------------------------------------------------#
# ResNet50的网络部分
#-------------------------------------------------------------#
from __future__ import print_functionimport numpy as np
from keras import layersfrom keras.layers import Input
from keras.layers import Dense,Conv2D,MaxPooling2D,ZeroPadding2D,AveragePooling2D
from keras.layers import Activation,BatchNormalization,Flatten
from keras.models import Modelfrom keras.preprocessing import image
import keras.backend as K
from keras.utils.data_utils import get_file
from keras_applications.imagenet_utils import decode_predictions
from keras_applications.imagenet_utils import preprocess_inputdef identity_block(input_tensor, kernel_size, filters, stage, block):filters1, filters2, filters3 filtersconv_name_base res str(stage) block _branchbn_name_base bn str(stage) block _branchx Conv2D(filters1, (1, 1), nameconv_name_base 2a)(input_tensor)x BatchNormalization(namebn_name_base 2a)(x)x Activation(relu)(x)x Conv2D(filters2, kernel_size,paddingsame, nameconv_name_base 2b)(x)x BatchNormalization(namebn_name_base 2b)(x)x Activation(relu)(x)x Conv2D(filters3, (1, 1), nameconv_name_base 2c)(x)x BatchNormalization(namebn_name_base 2c)(x)x layers.add([x, input_tensor])x Activation(relu)(x)return xdef conv_block(input_tensor, kernel_size, filters, stage, block, strides(2, 2)):filters1, filters2, filters3 filtersconv_name_base res str(stage) block _branchbn_name_base bn str(stage) block _branchx Conv2D(filters1, (1, 1), stridesstrides,nameconv_name_base 2a)(input_tensor)x BatchNormalization(namebn_name_base 2a)(x)x Activation(relu)(x)x Conv2D(filters2, kernel_size, paddingsame,nameconv_name_base 2b)(x)x BatchNormalization(namebn_name_base 2b)(x)x Activation(relu)(x)x Conv2D(filters3, (1, 1), nameconv_name_base 2c)(x)x BatchNormalization(namebn_name_base 2c)(x)shortcut Conv2D(filters3, (1, 1), stridesstrides,nameconv_name_base 1)(input_tensor)shortcut BatchNormalization(namebn_name_base 1)(shortcut)x layers.add([x, shortcut])x Activation(relu)(x)return xdef ResNet50(input_shape[224,224,3],classes1000):img_input Input(shapeinput_shape)x ZeroPadding2D((3, 3))(img_input)x Conv2D(64, (7, 7), strides(2, 2), nameconv1)(x)x BatchNormalization(namebn_conv1)(x)x Activation(relu)(x)x MaxPooling2D((3, 3), strides(2, 2))(x)x conv_block(x, 3, [64, 64, 256], stage2, blocka, strides(1, 1))x identity_block(x, 3, [64, 64, 256], stage2, blockb)x identity_block(x, 3, [64, 64, 256], stage2, blockc)x conv_block(x, 3, [128, 128, 512], stage3, blocka)x identity_block(x, 3, [128, 128, 512], stage3, blockb)x identity_block(x, 3, [128, 128, 512], stage3, blockc)x identity_block(x, 3, [128, 128, 512], stage3, blockd)x conv_block(x, 3, [256, 256, 1024], stage4, blocka)x identity_block(x, 3, [256, 256, 1024], stage4, blockb)x identity_block(x, 3, [256, 256, 1024], stage4, blockc)x identity_block(x, 3, [256, 256, 1024], stage4, blockd)x identity_block(x, 3, [256, 256, 1024], stage4, blocke)x identity_block(x, 3, [256, 256, 1024], stage4, blockf)x conv_block(x, 3, [512, 512, 2048], stage5, blocka)x identity_block(x, 3, [512, 512, 2048], stage5, blockb)x identity_block(x, 3, [512, 512, 2048], stage5, blockc)x AveragePooling2D((7, 7), nameavg_pool)(x)x Flatten()(x)x Dense(classes, activationsoftmax, namefc1000)(x)model Model(img_input, x, nameresnet50)model.load_weights(resnet50_weights_tf_dim_ordering_tf_kernels.h5)return modelif __name__ __main__:model ResNet50()model.summary()img_path elephant.jpg# img_path bike.jpgimg image.load_img(img_path, target_size(224, 224))x image.img_to_array(img)x np.expand_dims(x, axis0)x preprocess_input(x)print(Input image shape:, x.shape)preds model.predict(x)print(Predicted:, decode_predictions(preds))