How to change Batchsize during training
조회 수: 10 (최근 30일)
이전 댓글 표시
Hi,
I have data with size of (224,224,3,4) in 'SSCB' format. During traing, data size is required to change to ( 7,7,3,4*1024). data is divided in smaller chuncks ( with help of a 7*7 window size ) and added to Batchsize dimension.
I have tested resize2dLayer and also designed a custom layer for reshaping the data but it seems that MATLAB layers don't include Batchsize dimension as dimension of data
i.e, size of input data X in DimChangeLayer is (224,224,3) not (224,224,3,4). how can I solve it and have a output of size ( 7,7,3,4*1024). Thanks
my code:
Input_name = 'MSA_input';
input_size= [224 , 224 , 3];
C = input_size(3) ;
H = input_size(1);
W = input_size(2);
win_s=7;
n_win1 = round(H/win_s);
n_win2 = round(W/win_s);
num_Win = n_win1*n_win2;
l = [imageInputLayer(input_size, 'Name',Input_name, 'Normalization', 'none', 'NormalizationDimension', 'auto', 'DataAugmentation', 'none')];
l = [l DimChangeLayer( 'dimchanger4' , [win_s,win_s , C,num_Win )]; %
net =dlnetwork(l);
and this is my DimChangeLayer code :
classdef DimChangeLayer < nnet.layer.Layer
properties
Output_size
end
methods
function layer = DimChangeLayer(name , out_size)
% layer = DimChangeLayer
% Set layer name.
layer.Name = name;
% Set layer description.
layer.Description = "change dim";
% layer otputsize
layer.Output_size = out_size;
end
function [Z] = predict(layer,X)
Z = reshape( X , layer.Output_size);
end
function [Z] = forward(layer,X)
Z = reshape( X , layer.Output_size);
end
end
end
댓글 수: 2
yanqi liu
2021년 12월 28일
yes,sir,why not use reshape to change data dimension in prepare step,may be upload your data mat file to do some analysis
답변 (1개)
Srivardhan Gadila
2021년 12월 31일
When we call dlnetwork to create a dlnetwork object, it validates if all the layers in the layers array are valid or not and during this process some sample inputs are passed which include, few random inputs with single batch size and few inputs with batch size greater than one. Hence your custom layer fails this check as you have one constant (4 in this case) multiplied with a variable and this should be changed to as follows:
classdef DimChangeLayer < nnet.layer.Layer
properties
Output_size
end
methods
function layer = DimChangeLayer(name , out_size)
% layer = DimChangeLayer
% Set layer name.
layer.Name = name;
% Set layer description.
layer.Description = "change dim";
% layer otputsize
layer.Output_size = out_size;
end
function [Z] = predict(layer,X)
sz = layer.Output_size;
Z = reshape( X ,sz(1), sz(2), sz(3), []);
end
end
end
You can refer to this example more information: Define Custom Deep Learning Layer with Formatted Inputs. Although in the example they also inherit from nnet.layer.Formattable super class, it is not needed in this case.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Image Data Workflows에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!