fully connected layer as 1x1 convolution

Quoting Lecun’s post:

In Convolutional Nets, there is no such thing as “fully-connected layers”. There are only convolution layers with 1x1 convolution kernels and a full connection table.

It’s a too-rarely-understood fact that ConvNets don’t need to have a fixed-size input. You can train them on inputs that happen to produce a single output vector (with no spatial extent), and then apply them to larger images. Instead of a single output vector, you then get a spatial map of output vectors. Each vector sees input windows at different locations on the input.

In that scenario, the “fully connected layers” really act as 1x1 convolutions.

References:

1、读取数据 digitDatasetPath=fullfile(matlabroot,'toolbox','nnet','nndemos','nndatasets','DigitDataset'); imds=imageDatastore(digitDatasetPath,'IncludeSubfolders',true,'LabelSource','foldernames'); numTrainingFiles=0.75; [imdsTrain,imdsTest]=splitEachLabel(imds,numTrainingFiles,'randomized'); 2、神经网络架构 layers=[... imageInputLayer([28 28 1]); convolution2dLayer(5,6,'Stride',1,'Padding','same'); reluLayer maxPooling2dLayer(2,'Stride',2,'Padding','same') convolution2dLayer(5,16,'Stride',1,'Padding','same'); reluLayer maxPooling2dLayer(2,'Stride',2,'Padding','same') fullyConnectedLayer(120) reluLayer fullyConnectedLayer(84) reluLayer fullyConnectedLayer(10) softmaxLayer classificationLayer ]; 3、超参数设置 options=trainingOptions('adam',... 'ExecutionEnvironment','auto','MaxEpochs',30,... 'InitialLearnRate',1e-3,'Verbose',false,'Plots','training-progress'); 4、神经网络训练 net=trainNetwork(imdsTrain,layers,options); 5、预测和输出 Ypred=classify(net, imdsTest); YTest=imdsTest.Labels; accuracy=sum(Ypred==YTest)/numel(YTest) fprintf('精确值为:%5.2f%%\n',accuracy*100); clear I=imread('风扇.png'); net = squeezenet; %net = resnet50('Weights','none') inputSize=net.Layers(1).InputSize; I_resize=imresize(I,inputSize(1:2)); label=classify(net,I_resize,'ExecutionEnvironment','cpu'); 6、输出图片 figure subplot(1,4,1),plot(layerGraph(net.Layers)); subplot(1,4,2),imshow(I); subplot(1,4,3),imshow(I_resize); subplot(1,4,4),imshow(I_resize);title(string(label))
06-07
% 构建cnn网络进行训练 trainingSetup = load("C:\Users\Administrator\Desktop\MATLAB基于卷积神经网络的手势识别\代码\cnn.mat"); imdsTrain = trainingSetup.imdsTrain; [imdsTrain, imdsValidation] = splitEachLabel(imdsTrain,0.7);% 70%用于训练 % 调整图像大小以匹配网络输入层。 augimdsTrain = augmentedImageDatastore([28 28 1],imdsTrain); augimdsValidation = augmentedImageDatastore([28 28 1],imdsValidation); layers = [ imageInputLayer([28 28 1],"Name","imageinput") convolution2dLayer([3 3],32,"Name","conv_1","Padding","same") batchNormalizationLayer("Name","batchnorm_1") reluLayer("Name","relu_1") maxPooling2dLayer([3 3],"Name","maxpool_1","Padding","same") convolution2dLayer([3 3],32,"Name","conv_2","Padding","same") batchNormalizationLayer("Name","batchnorm_2") reluLayer("Name","relu_2") maxPooling2dLayer([3 3],"Name","maxpool_2","Padding","same") fullyConnectedLayer(10,"Name","fc") softmaxLayer("Name","softmax") classificationLayer("Name","classoutput")]; figure('Visible','on'); plot(layerGraph(layers)) % 显示网络结构图 opts = trainingOptions("sgdm",... "ExecutionEnvironment","auto",... "InitialLearnRate",0.01,... "Shuffle","every-epoch",... "MaxEpochs",15, ... % 最大学习整个数据集的次数,训练15轮 "MiniBatchSize",130, ... % 一个batch有130个样本 "Plots","training-progress",... % 画出整个训练过程 "ValidationData",augimdsValidation); % MaxEpochsy:训练轮数;MiniBatchSize:每轮迭代次数 = 训练样本数 / MiniBatchSize [net, traininfo] = trainNetwork(augimdsTrain,layers,opts); % 保存当前网络 save('cnn.mat','net')
07-20
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值