How to change Batchsize during training
10 views (last 30 days)
Show older comments
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 Comments
yanqi liu
on 28 Dec 2021
yes,sir,why not use reshape to change data dimension in prepare step,may be upload your data mat file to do some analysis
sheyda Ghanbaralizadeh
on 28 Dec 2021
Edited: sheyda Ghanbaralizadeh
on 28 Dec 2021
Answers (1)
Srivardhan Gadila
on 31 Dec 2021
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 Comments
See Also
Categories
Find more on Image Data Workflows in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!