| 1 | # Licensed to the Apache Software Foundation (ASF) under one | ||
| 2 | # or more contributor license agreements. See the NOTICE file | ||
| 3 | # distributed with this work for additional information | ||
| 4 | # regarding copyright ownership. The ASF licenses this file | ||
| 5 | # to you under the Apache License, Version 2.0 (the | ||
| 6 | # "License"); you may not use this file except in compliance | ||
| 7 | # with the License. You may obtain a copy of the License at | ||
| 8 | # | ||
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 | ||
| 10 | # | ||
| 11 | # Unless required by applicable law or agreed to in writing, | ||
| 12 | # software distributed under the License is distributed on an | ||
| 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| 14 | # KIND, either express or implied. See the License for the | ||
| 15 | # specific language governing permissions and limitations | ||
| 16 | # under the License. | ||
| 17 | |||
| 18 | package AI::MXNet::Gluon::ModelZoo::Vision::Inception::V3; | ||
| 19 | use strict; | ||
| 20 | use warnings; | ||
| 21 | use AI::MXNet::Base; | ||
| 22 | use AI::MXNet::Function::Parameters; | ||
| 23 | use AI::MXNet::Gluon::Mouse; | ||
| 24 | extends 'AI::MXNet::Gluon::HybridBlock'; | ||
| 25 | |||
| 26 | func _make_basic_conv(%kwargs) | ||
| 27 | { | ||
| 28 | my $out = nn->HybridSequential(prefix=>''); | ||
| 29 | $out->add(nn->Conv2D(use_bias=>0, %kwargs)); | ||
| 30 | $out->add(nn->BatchNorm(epsilon=>0.001)); | ||
| 31 | $out->add(nn->Activation('relu')); | ||
| 32 | return $out; | ||
| 33 | } | ||
| 34 | |||
| 35 | func _make_branch($use_pool, @conv_settings) | ||
| 36 | { | ||
| 37 | my $out = nn->HybridSequential(prefix=>''); | ||
| 38 | if($use_pool eq 'avg') | ||
| 39 | { | ||
| 40 | $out->add(nn->AvgPool2D(pool_size=>3, strides=>1, padding=>1)); | ||
| 41 | } | ||
| 42 | elsif($use_pool eq 'max') | ||
| 43 | { | ||
| 44 | $out->add(nn->MaxPool2D(pool_size=>3, strides=>2)); | ||
| 45 | } | ||
| 46 | my @setting_names = ('channels', 'kernel_size', 'strides', 'padding'); | ||
| 47 | for my $setting (@conv_settings) | ||
| 48 | { | ||
| 49 | my %kwargs; | ||
| 50 | for(enumerate($setting)) | ||
| 51 | { | ||
| 52 | my ($i, $value) = @$_; | ||
| 53 | if(defined $value) | ||
| 54 | { | ||
| 55 | $kwargs{ $setting_names[$i] } = $value; | ||
| 56 | } | ||
| 57 | } | ||
| 58 | $out->add(_make_basic_conv(%kwargs)); | ||
| 59 | } | ||
| 60 | return $out; | ||
| 61 | } | ||
| 62 | |||
| 63 | func _make_A($pool_features, $prefix) | ||
| 64 | { | ||
| 65 | my $out = nn->HybridConcurrent(axis=>1, prefix=>$prefix); | ||
| 66 | $out->name_scope(sub { | ||
| 67 | $out->add(_make_branch('', [64, 1, undef, undef])); | ||
| 68 | $out->add(_make_branch( | ||
| 69 | '', | ||
| 70 | [48, 1, undef, undef], | ||
| 71 | [64, 5, undef, 2] | ||
| 72 | )); | ||
| 73 | $out->add(_make_branch( | ||
| 74 | '', | ||
| 75 | [64, 1, undef, undef], | ||
| 76 | [96, 3, undef, 1], | ||
| 77 | [96, 3, undef, 1] | ||
| 78 | )); | ||
| 79 | $out->add(_make_branch('avg', [$pool_features, 1, undef, undef])); | ||
| 80 | }); | ||
| 81 | return $out; | ||
| 82 | } | ||
| 83 | |||
| 84 | func _make_B($prefix) | ||
| 85 | { | ||
| 86 | my $out = nn->HybridConcurrent(axis=>1, prefix=>$prefix); | ||
| 87 | $out->name_scope(sub { | ||
| 88 | $out->add(_make_branch('', [384, 3, 2, undef])); | ||
| 89 | $out->add(_make_branch( | ||
| 90 | '', | ||
| 91 | [64, 1, undef, undef], | ||
| 92 | [96, 3, undef, 1], | ||
| 93 | [96, 3, 2, undef] | ||
| 94 | )); | ||
| 95 | $out->add(_make_branch('max')); | ||
| 96 | }); | ||
| 97 | return $out; | ||
| 98 | } | ||
| 99 | |||
| 100 | func _make_C($channels_7x7, $prefix) | ||
| 101 | { | ||
| 102 | my $out = nn->HybridConcurrent(axis=>1, prefix=>$prefix); | ||
| 103 | $out->name_scope(sub { | ||
| 104 | $out->add(_make_branch('', [192, 1, undef, undef])); | ||
| 105 | $out->add(_make_branch( | ||
| 106 | '', | ||
| 107 | [$channels_7x7, 1, undef, undef], | ||
| 108 | [$channels_7x7, [1, 7], undef, [0, 3]], | ||
| 109 | [192, [7, 1], undef, [3, 0]] | ||
| 110 | )); | ||
| 111 | $out->add(_make_branch( | ||
| 112 | '', | ||
| 113 | [$channels_7x7, 1, undef, undef], | ||
| 114 | [$channels_7x7, [7, 1], undef, [3, 0]], | ||
| 115 | [$channels_7x7, [1, 7], undef, [0, 3]], | ||
| 116 | [$channels_7x7, [7, 1], undef, [3, 0]], | ||
| 117 | [192, [1, 7], undef, [0, 3]] | ||
| 118 | )); | ||
| 119 | $out->add(_make_branch( | ||
| 120 | 'avg', | ||
| 121 | [192, 1, undef, undef] | ||
| 122 | )); | ||
| 123 | }); | ||
| 124 | return $out; | ||
| 125 | } | ||
| 126 | |||
| 127 | func _make_D($prefix) | ||
| 128 | { | ||
| 129 | my $out = nn->HybridConcurrent(axis=>1, prefix=>$prefix); | ||
| 130 | $out->name_scope(sub { | ||
| 131 | $out->add(_make_branch( | ||
| 132 | '', | ||
| 133 | [192, 1, undef, undef], | ||
| 134 | [320, 3, 2, undef] | ||
| 135 | )); | ||
| 136 | $out->add(_make_branch( | ||
| 137 | '', | ||
| 138 | [192, 1, undef, undef], | ||
| 139 | [192, [1, 7], undef, [0, 3]], | ||
| 140 | [192, [7, 1], undef, [3, 0]], | ||
| 141 | [192, 3, 2, undef] | ||
| 142 | )); | ||
| 143 | $out->add(_make_branch('max')); | ||
| 144 | }); | ||
| 145 | return $out; | ||
| 146 | } | ||
| 147 | |||
| 148 | func _make_E($prefix) | ||
| 149 | { | ||
| 150 | my $out = nn->HybridConcurrent(axis=>1, prefix=>$prefix); | ||
| 151 | $out->name_scope(sub { | ||
| 152 | $out->add(_make_branch('', [320, 1, undef, undef])); | ||
| 153 | |||
| 154 | my $branch_3x3 = nn->HybridSequential(prefix=>''); | ||
| 155 | $out->add($branch_3x3); | ||
| 156 | $branch_3x3->add(_make_branch( | ||
| 157 | '', | ||
| 158 | [384, 1, undef, undef] | ||
| 159 | )); | ||
| 160 | my $branch_3x3_split = nn->HybridConcurrent(axis=>1, prefix=>''); | ||
| 161 | $branch_3x3_split->add(_make_branch('', [384, [1, 3], undef, [0, 1]])); | ||
| 162 | $branch_3x3_split->add(_make_branch('', [384, [3, 1], undef, [1, 0]])); | ||
| 163 | $branch_3x3->add($branch_3x3_split); | ||
| 164 | |||
| 165 | my $branch_3x3dbl = nn->HybridSequential(prefix=>''); | ||
| 166 | $out->add($branch_3x3dbl); | ||
| 167 | $branch_3x3dbl->add(_make_branch( | ||
| 168 | '', | ||
| 169 | [448, 1, undef, undef], | ||
| 170 | [384, 3, undef, 1] | ||
| 171 | )); | ||
| 172 | my $branch_3x3dbl_split = nn->HybridConcurrent(axis=>1, prefix=>''); | ||
| 173 | $branch_3x3dbl->add($branch_3x3dbl_split); | ||
| 174 | $branch_3x3dbl_split->add(_make_branch('', [384, [1, 3], undef, [0, 1]])); | ||
| 175 | $branch_3x3dbl_split->add(_make_branch('', [384, [3, 1], undef, [1, 0]])); | ||
| 176 | |||
| 177 | $out->add(_make_branch('avg', [192, 1, undef, undef])); | ||
| 178 | }); | ||
| 179 | return $out; | ||
| 180 | } | ||
| 181 | |||
| 182 | func make_aux($classes) | ||
| 183 | { | ||
| 184 | my $out = nn->HybridSequential(prefix=>''); | ||
| 185 | $out->add(nn->AvgPool2D(pool_size=>5, strides=>3)); | ||
| 186 | $out->add(_make_basic_conv(channels=>128, kernel_size=>1)); | ||
| 187 | $out->add(_make_basic_conv(channels=>768, kernel_size=>5)); | ||
| 188 | $out->add(nn->Flatten()); | ||
| 189 | $out->add(nn->Dense($classes)); | ||
| 190 | return $out; | ||
| 191 | } | ||
| 192 | |||
| 193 | =head1 NAME | ||
| 194 | |||
| 195 | AI::MXNet::Gluon::ModelZoo::Vision::Inception::V3 - Inception v3 model. | ||
| 196 | =cut | ||
| 197 | |||
| 198 | =head1 DESCRIPTION | ||
| 199 | |||
| 200 | Inception v3 model from | ||
| 201 | "Rethinking the Inception Architecture for Computer Vision" | ||
| 202 | <http://arxiv.org/abs/1512.00567> paper. | ||
| 203 | |||
| 204 | Parameters | ||
| 205 | ---------- | ||
| 206 | classes : Int, default 1000 | ||
| 207 | Number of classification classes. | ||
| 208 | =cut | ||
| 209 | |||
| 210 | has 'classes' => (is => 'ro', isa => 'Int', default => 1000); | ||
| 211 | method python_constructor_arguments(){ ['classes'] } | ||
| 212 | |||
| 213 | sub BUILD | ||
| 214 | { | ||
| 215 | my $self = shift; | ||
| 216 | $self->name_scope(sub { | ||
| 217 | $self->features(nn->HybridSequential(prefix=>'')); | ||
| 218 | $self->features->add(_make_basic_conv(channels=>32, kernel_size=>3, strides=>2)); | ||
| 219 | $self->features->add(_make_basic_conv(channels=>32, kernel_size=>3)); | ||
| 220 | $self->features->add(_make_basic_conv(channels=>64, kernel_size=>3, padding=>1)); | ||
| 221 | $self->features->add(nn->MaxPool2D(pool_size=>3, strides=>2)); | ||
| 222 | $self->features->add(_make_basic_conv(channels=>80, kernel_size=>1)); | ||
| 223 | $self->features->add(_make_basic_conv(channels=>192, kernel_size=>3)); | ||
| 224 | $self->features->add(nn->MaxPool2D(pool_size=>3, strides=>2)); | ||
| 225 | $self->features->add(_make_A(32, 'A1_')); | ||
| 226 | $self->features->add(_make_A(64, 'A2_')); | ||
| 227 | $self->features->add(_make_A(64, 'A3_')); | ||
| 228 | $self->features->add(_make_B('B_')); | ||
| 229 | $self->features->add(_make_C(128, 'C1_')); | ||
| 230 | $self->features->add(_make_C(160, 'C2_')); | ||
| 231 | $self->features->add(_make_C(160, 'C3_')); | ||
| 232 | $self->features->add(_make_C(192, 'C4_')); | ||
| 233 | $self->features->add(_make_D('D_')); | ||
| 234 | $self->features->add(_make_E('E1_')); | ||
| 235 | $self->features->add(_make_E('E2_')); | ||
| 236 | $self->features->add(nn->AvgPool2D(pool_size=>8)); | ||
| 237 | $self->features->add(nn->Dropout(0.5)); | ||
| 238 | |||
| 239 | $self->output(nn->Dense($self->classes)); | ||
| 240 | }); | ||
| 241 | } | ||
| 242 | |||
| 243 | method hybrid_forward(GluonClass $F, GluonInput $x) | ||
| 244 | { | ||
| 245 | $x = $self->features->($x); | ||
| 246 | $x = $self->output->($x); | ||
| 247 | return $x; | ||
| 248 | } | ||
| 249 | |||
| 250 | package AI::MXNet::Gluon::ModelZoo::Vision; | ||
| 251 | |||
| 252 | =head2 inception_v3 | ||
| 253 | |||
| 254 | Inception v3 model from | ||
| 255 | "Rethinking the Inception Architecture for Computer Vision" | ||
| 256 | <http://arxiv.org/abs/1512.00567> paper. | ||
| 257 | |||
| 258 | Parameters | ||
| 259 | ---------- | ||
| 260 | :$pretrained : Bool, default 0 | ||
| 261 | Whether to load the pretrained weights for model. | ||
| 262 | :$ctx : AI::MXNet::Context, default CPU | ||
| 263 | The context in which to load the pretrained weights. | ||
| 264 | :$root : Str, default '~/.mxnet/models' | ||
| 265 | Location for keeping the model parameters. | ||
| 266 | =cut | ||
| 267 | |||
| 268 | method inception_v3( | ||
| 269 | Bool :$pretrained=0, AI::MXNet::Context :$ctx=AI::MXNet::Context->cpu(), | ||
| 270 | Str :$root='~/.mxnet/models', Int :$classes=1000 | ||
| 271 | ) | ||
| 272 | { | ||
| 273 | my $net = AI::MXNet::Gluon::ModelZoo::Vision::Inception::V3->new($classes); | ||
| 274 | if($pretrained) | ||
| 275 | { | ||
| 276 | $net->load_parameters( | ||
| 277 | AI::MXNet::Gluon::ModelZoo::ModelStore->get_model_file( | ||
| 278 | "inceptionv3", | ||
| 279 | root=>$root | ||
| 280 | ), | ||
| 281 | ctx=>$ctx | ||
| 282 | ); | ||
| 283 | } | ||
| 284 | return $net; | ||
| 285 | } | ||
| 286 | |||
| 287 | 1; |