1- function lgraph = convMixerLayers(opts )
2- % convMixerLayers Build ConvMixer architecture.
3- %
4- % lgraph = convMixerLayers() returns a LayerGraph object with a ConvMixer
5- % architecture with default options as proposed in
6- % https://openreview.net/forum?id=TVHS5Y4dNvM.
7- %
8- % lgraph = convMixerLayers(PARAM1=VAL1,PARAM2=VAL2,...) specifies optional
9- % parameter name/value pairs for creating the layer graph:
10- %
11- % 'InputSize' - Size of the input images.
12- %
13- % 'NumClasses' - Number of classes the network predicts.
14- %
15- % 'KernelSize' - Size of the kernel for the depthwise
16- % convolution.
17- %
18- % 'PatchSize' - Size of the pathes for the patch embedding
19- % layer.
20- %
21- % 'Depth' - Number of repeated fully-convolutional
22- % blocks.
23- %
24- % 'HiddenDimension' - Number of channels output by the patch
25- % embedding.
26- %
27- % 'ConnectOutputLayer' - Determines whether to append a softmax and
28- % classification output layers to the
29- % returned LayerGraph object.
30- %
31- % Example:
32- %
33- % lgraph = convMixerLayers(InputSize=[28 28 1], Depth=5, NumClasses=10)
34-
35- % Copyright 2021 The MathWorks, Inc.
36-
37- arguments
38- opts.InputSize = [227 227 3 ]
39- opts.NumClasses = 1000
40- opts.KernelSize = 9
41- opts.PatchSize = 7
42- opts.Depth = 20
43- opts.HiddenDimension = 1536
44- opts.ConnectOutputLayer logical = false
45- end
46-
47- input_size = opts .InputSize ;
48- num_classes = opts .NumClasses ;
49-
50- kernel_size = opts .KernelSize ;
51- patch_size = opts .PatchSize ;
52- depth = opts .Depth ;
53- hidden_dim = opts .HiddenDimension ;
54- connectOutputLayers = opts .ConnectOutputLayer ;
55-
56- % First layer is a "path embedding". Seems to be this:
57- patchEmbedding = convolution2dLayer(patch_size , hidden_dim , ...
58- Stride= patch_size , ...
59- Name= " patchEmbedding" , ...
60- WeightsInitializer= " glorot" );
61-
62- % Make Layer Graph
63- lgraph = layerGraph();
64-
65- start = [
66- imageInputLayer(input_size ,Normalization= " none" )
67- patchEmbedding
68- geluLayer(Name = " gelu_0" )
69- batchNormalizationLayer(Name = " batchnorm_0" )
70- ];
71- lgraph = addLayers(lgraph ,start );
72-
73- for i = 1 : depth
74- convMixer = [
75- groupedConvolution2dLayer(kernel_size ,1 ," channel-wise" ,Name= " depthwiseConv_" +i ,Padding= " same" ,WeightsInitializer= " glorot" )
76- geluLayer(Name = " gelu_" +(2 * i - 1 ))
77- batchNormalizationLayer(Name = " batchnorm_" +(2 * i - 1 ))
78- additionLayer(2 ,Name= " addition_" +i )
79- convolution2dLayer([1 1 ],hidden_dim ,Name= " pointwiseConv_" +i ,WeightsInitializer= " glorot" )
80- geluLayer(Name = " gelu_" +2 * i )
81- batchNormalizationLayer(Name = " batchnorm_" +2 * i )
82- ];
83- lgraph = addLayers(lgraph ,convMixer );
84- lgraph = connectLayers(lgraph ," batchnorm_" +2 *(i - 1 )," depthwiseConv_" +i );
85- lgraph = connectLayers(lgraph ," batchnorm_" +2 *(i - 1 )," addition_" +i +" /in2" );
86- end
87-
88- gapFc = [
89- globalAveragePooling2dLayer(Name = " GAP" )
90- fullyConnectedLayer(num_classes )
91- ];
92- lgraph = addLayers(lgraph ,gapFc );
93- lgraph = connectLayers(lgraph ," batchnorm_" +2 * depth ," GAP" );
94-
95- if connectOutputLayers
96- lgraph = addLayers(lgraph , softmaxLayer(' Name' ,' softmax' ));
97- lgraph = addLayers(lgraph , classificationLayer(' Name' ,' classification' ));
98- lgraph = connectLayers(lgraph ,' fc' ,' softmax' );
99- lgraph = connectLayers(lgraph ,' softmax' ,' classification' );
100- end
101- end
1+ function lgraph = convMixerLayers(opts )
2+ % convMixerLayers Build ConvMixer architecture.
3+ %
4+ % lgraph = convMixerLayers() returns a LayerGraph object with a ConvMixer
5+ % architecture with default options as proposed in
6+ % https://openreview.net/forum?id=TVHS5Y4dNvM.
7+ %
8+ % lgraph = convMixerLayers(PARAM1=VAL1,PARAM2=VAL2,...) specifies optional
9+ % parameter name/value pairs for creating the layer graph:
10+ %
11+ % 'InputSize' - Size of the input images.
12+ %
13+ % 'NumClasses' - Number of classes the network predicts.
14+ %
15+ % 'KernelSize' - Size of the kernel for the depthwise
16+ % convolution.
17+ %
18+ % 'PatchSize' - Size of the pathes for the patch embedding
19+ % layer.
20+ %
21+ % 'Depth' - Number of repeated fully-convolutional
22+ % blocks.
23+ %
24+ % 'HiddenDimension' - Number of channels output by the patch
25+ % embedding.
26+ %
27+ % 'ConnectOutputLayer' - Determines whether to append a softmax and
28+ % classification output layers to the
29+ % returned LayerGraph object.
30+ %
31+ % Example:
32+ %
33+ % lgraph = convMixerLayers(InputSize=[28 28 1], Depth=5, NumClasses=10)
34+
35+ % Copyright 2021 The MathWorks, Inc.
36+
37+ arguments
38+ opts.InputSize = [227 227 3 ]
39+ opts.NumClasses = 1000
40+ opts.KernelSize = 9
41+ opts.PatchSize = 7
42+ opts.Depth = 20
43+ opts.HiddenDimension = 1536
44+ opts.ConnectOutputLayer logical = false
45+ end
46+
47+ input_size = opts .InputSize ;
48+ num_classes = opts .NumClasses ;
49+
50+ kernel_size = opts .KernelSize ;
51+ patch_size = opts .PatchSize ;
52+ depth = opts .Depth ;
53+ hidden_dim = opts .HiddenDimension ;
54+ connectOutputLayers = opts .ConnectOutputLayer ;
55+
56+ % First layer is a "path embedding". Seems to be this:
57+ patchEmbedding = convolution2dLayer(patch_size , hidden_dim , ...
58+ Stride= patch_size , ...
59+ Name= " patchEmbedding" , ...
60+ WeightsInitializer= " glorot" );
61+
62+ % Make Layer Graph
63+ lgraph = layerGraph();
64+
65+ start = [
66+ imageInputLayer(input_size ,Normalization= " none" )
67+ patchEmbedding
68+ geluLayer(Name = " gelu_0" )
69+ batchNormalizationLayer(Name = " batchnorm_0" )
70+ ];
71+ lgraph = addLayers(lgraph ,start );
72+
73+ for i = 1 : depth
74+ convMixer = [
75+ groupedConvolution2dLayer(kernel_size ,1 ," channel-wise" ,Name= " depthwiseConv_" +i ,Padding= " same" ,WeightsInitializer= " glorot" )
76+ geluLayer(Name = " gelu_" +(2 * i - 1 ))
77+ batchNormalizationLayer(Name = " batchnorm_" +(2 * i - 1 ))
78+ additionLayer(2 ,Name= " addition_" +i )
79+ convolution2dLayer([1 1 ],hidden_dim ,Name= " pointwiseConv_" +i ,WeightsInitializer= " glorot" )
80+ geluLayer(Name = " gelu_" +2 * i )
81+ batchNormalizationLayer(Name = " batchnorm_" +2 * i )
82+ ];
83+ lgraph = addLayers(lgraph ,convMixer );
84+ lgraph = connectLayers(lgraph ," batchnorm_" +2 *(i - 1 )," depthwiseConv_" +i );
85+ lgraph = connectLayers(lgraph ," batchnorm_" +2 *(i - 1 )," addition_" +i +" /in2" );
86+ end
87+
88+ gapFc = [
89+ globalAveragePooling2dLayer(Name = " GAP" )
90+ fullyConnectedLayer(num_classes )
91+ ];
92+ lgraph = addLayers(lgraph ,gapFc );
93+ lgraph = connectLayers(lgraph ," batchnorm_" +2 * depth ," GAP" );
94+
95+ if connectOutputLayers
96+ lgraph = addLayers(lgraph , softmaxLayer(' Name' ,' softmax' ));
97+ lgraph = addLayers(lgraph , classificationLayer(' Name' ,' classification' ));
98+ lgraph = connectLayers(lgraph ,' fc' ,' softmax' );
99+ lgraph = connectLayers(lgraph ,' softmax' ,' classification' );
100+ end
101+ end
0 commit comments