| 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::SqueezeNet; | ||
| 19 | use strict; | ||
| 20 | use warnings; | ||
| 21 | use AI::MXNet::Function::Parameters; | ||
| 22 | use AI::MXNet::Gluon::Mouse; | ||
| 23 | use AI::MXNet::Types; | ||
| 24 | extends 'AI::MXNet::Gluon::HybridBlock'; | ||
| 25 | |||
| 26 | func _make_fire($squeeze_channels, $expand1x1_channels, $expand3x3_channels) | ||
| 27 | { | ||
| 28 | my $out = nn->HybridSequential(prefix=>''); | ||
| 29 | $out->add(_make_fire_conv($squeeze_channels, 1)); | ||
| 30 | |||
| 31 | my $paths = nn->HybridConcurrent(axis=>1, prefix=>''); | ||
| 32 | $paths->add(_make_fire_conv($expand1x1_channels, 1)); | ||
| 33 | $paths->add(_make_fire_conv($expand3x3_channels, 3, 1)); | ||
| 34 | $out->add($paths); | ||
| 35 | |||
| 36 | return $out; | ||
| 37 | } | ||
| 38 | |||
| 39 | func _make_fire_conv($channels, $kernel_size, $padding=0) | ||
| 40 | { | ||
| 41 | my $out = nn->HybridSequential(prefix=>''); | ||
| 42 | $out->add(nn->Conv2D($channels, $kernel_size, padding=>$padding)); | ||
| 43 | $out->add(nn->Activation('relu')); | ||
| 44 | return $out; | ||
| 45 | } | ||
| 46 | |||
| 47 | =head1 NAME | ||
| 48 | |||
| 49 | AI::MXNet::Gluon::ModelZoo::Vision::SqueezeNet - SqueezeNet model from the "SqueezeNet: AlexNet-level accuracy with 50x fewer parameters and <0.5MB model size" | ||
| 50 | =cut | ||
| 51 | |||
| 52 | =head1 DESCRIPTION | ||
| 53 | |||
| 54 | SqueezeNet model from the "SqueezeNet: AlexNet-level accuracy with 50x fewer parameters | ||
| 55 | and <0.5MB model size" <https://arxiv.org/abs/1602.07360> paper. | ||
| 56 | SqueezeNet 1.1 model from the official SqueezeNet repo | ||
| 57 | <https://github.com/DeepScale/SqueezeNet/tree/master/SqueezeNet_v1.1>. | ||
| 58 | SqueezeNet 1.1 has 2.4x less computation and slightly fewer parameters | ||
| 59 | than SqueezeNet 1.0, without sacrificing accuracy. | ||
| 60 | |||
| 61 | Parameters | ||
| 62 | ---------- | ||
| 63 | version : Str | ||
| 64 | Version of squeezenet. Options are '1.0', '1.1'. | ||
| 65 | classes : Int, default 1000 | ||
| 66 | Number of classification classes. | ||
| 67 | =cut | ||
| 68 | |||
| 69 | has 'version' => (is => 'ro', isa => enum([qw[1.0 1.1]]), required => 1); | ||
| 70 | has 'classes' => (is => 'ro', isa => 'Int', default => 1000); | ||
| 71 | method python_constructor_arguments() { [qw/version classes/] } | ||
| 72 | |||
| 73 | sub BUILD | ||
| 74 | { | ||
| 75 | my $self = shift; | ||
| 76 | $self->name_scope(sub { | ||
| 77 | $self->features(nn->HybridSequential(prefix=>'')); | ||
| 78 | if($self->version eq '1.0') | ||
| 79 | { | ||
| 80 | $self->features->add(nn->Conv2D(96, kernel_size=>7, strides=>2)); | ||
| 81 | $self->features->add(nn->Activation('relu')); | ||
| 82 | $self->features->add(nn->MaxPool2D(pool_size=>3, strides=>2, ceil_mode=>1)); | ||
| 83 | $self->features->add(_make_fire(16, 64, 64)); | ||
| 84 | $self->features->add(_make_fire(16, 64, 64)); | ||
| 85 | $self->features->add(_make_fire(32, 128, 128)); | ||
| 86 | $self->features->add(nn->MaxPool2D(pool_size=>3, strides=>2, ceil_mode=>1)); | ||
| 87 | $self->features->add(_make_fire(32, 128, 128)); | ||
| 88 | $self->features->add(_make_fire(48, 192, 192)); | ||
| 89 | $self->features->add(_make_fire(48, 192, 192)); | ||
| 90 | $self->features->add(_make_fire(64, 256, 256)); | ||
| 91 | $self->features->add(nn->MaxPool2D(pool_size=>3, strides=>2, ceil_mode=>1)); | ||
| 92 | $self->features->add(_make_fire(64, 256, 256)); | ||
| 93 | } | ||
| 94 | else | ||
| 95 | { | ||
| 96 | $self->features->add(nn->Conv2D(64, kernel_size=>3, strides=>2)); | ||
| 97 | $self->features->add(nn->Activation('relu')); | ||
| 98 | $self->features->add(nn->MaxPool2D(pool_size=>3, strides=>2, ceil_mode=>1)); | ||
| 99 | $self->features->add(_make_fire(16, 64, 64)); | ||
| 100 | $self->features->add(_make_fire(16, 64, 64)); | ||
| 101 | $self->features->add(nn->MaxPool2D(pool_size=>3, strides=>2, ceil_mode=>1)); | ||
| 102 | $self->features->add(_make_fire(32, 128, 128)); | ||
| 103 | $self->features->add(_make_fire(32, 128, 128)); | ||
| 104 | $self->features->add(nn->MaxPool2D(pool_size=>3, strides=>2, ceil_mode=>1)); | ||
| 105 | $self->features->add(_make_fire(48, 192, 192)); | ||
| 106 | $self->features->add(_make_fire(48, 192, 192)); | ||
| 107 | $self->features->add(_make_fire(64, 256, 256)); | ||
| 108 | $self->features->add(_make_fire(64, 256, 256)); | ||
| 109 | } | ||
| 110 | $self->features->add(nn->Dropout(0.5)); | ||
| 111 | |||
| 112 | $self->output(nn->HybridSequential(prefix=>'')); | ||
| 113 | $self->output->add(nn->Conv2D($self->classes, kernel_size=>1)); | ||
| 114 | $self->output->add(nn->Activation('relu')); | ||
| 115 | $self->output->add(nn->AvgPool2D(13)); | ||
| 116 | $self->output->add(nn->Flatten()); | ||
| 117 | }); | ||
| 118 | } | ||
| 119 | |||
| 120 | method hybrid_forward(GluonClass $F, GluonInput $x) | ||
| 121 | { | ||
| 122 | $x = $self->features->($x); | ||
| 123 | $x = $self->output->($x); | ||
| 124 | return $x; | ||
| 125 | } | ||
| 126 | |||
| 127 | |||
| 128 | package AI::MXNet::Gluon::ModelZoo::Vision; | ||
| 129 | |||
| 130 | =head2 get_squeezenet | ||
| 131 | |||
| 132 | SqueezeNet model from the "SqueezeNet: AlexNet-level accuracy with 50x fewer parameters | ||
| 133 | and <0.5MB model size" <https://arxiv.org/abs/1602.07360> paper. | ||
| 134 | SqueezeNet 1.1 model from the official SqueezeNet repo | ||
| 135 | <https://github.com/DeepScale/SqueezeNet/tree/master/SqueezeNet_v1.1>. | ||
| 136 | SqueezeNet 1.1 has 2.4x less computation and slightly fewer parameters | ||
| 137 | than SqueezeNet 1.0, without sacrificing accuracy. | ||
| 138 | |||
| 139 | Parameters | ||
| 140 | ---------- | ||
| 141 | $version : Str | ||
| 142 | Version of squeezenet. Options are '1.0', '1.1'. | ||
| 143 | :$pretrained : Bool, default 0 | ||
| 144 | Whether to load the pretrained weights for model. | ||
| 145 | :$ctx : AI::MXNet::Context, default CPU | ||
| 146 | The context in which to load the pretrained weights. | ||
| 147 | :$root : Str, default '~/.mxnet/models' | ||
| 148 | Location for keeping the model parameters. | ||
| 149 | =cut | ||
| 150 | |||
| 151 | method get_squeezenet( | ||
| 152 | Str $version, Bool :$pretrained=0, AI::MXNet::Context :$ctx=AI::MXNet::Context->cpu(), | ||
| 153 | Str :$root='~/.mxnet/models', Int :$classes=1000 | ||
| 154 | ) | ||
| 155 | { | ||
| 156 | my $net = AI::MXNet::Gluon::ModelZoo::Vision::SqueezeNet->new($version, $classes); | ||
| 157 | if($pretrained) | ||
| 158 | { | ||
| 159 | $net->load_parameters( | ||
| 160 | AI::MXNet::Gluon::ModelZoo::ModelStore->get_model_file( | ||
| 161 | "squeezenet$version", | ||
| 162 | root=>$root | ||
| 163 | ), | ||
| 164 | ctx=>$ctx | ||
| 165 | ); | ||
| 166 | } | ||
| 167 | return $net; | ||
| 168 | } | ||
| 169 | |||
| 170 | =head2 squeezenet1_0 | ||
| 171 | |||
| 172 | SqueezeNet 1.0 model from the "SqueezeNet: AlexNet-level accuracy with 50x fewer parameters | ||
| 173 | and <0.5MB model size" <https://arxiv.org/abs/1602.07360> paper. | ||
| 174 | |||
| 175 | Parameters | ||
| 176 | ---------- | ||
| 177 | :$pretrained : Bool, default 0 | ||
| 178 | Whether to load the pretrained weights for model. | ||
| 179 | :$ctx : AI::MXNet::Context, default CPU | ||
| 180 | The context in which to load the pretrained weights. | ||
| 181 | :$root : Str, default '~/.mxnet/models' | ||
| 182 | Location for keeping the model parameters. | ||
| 183 | =cut | ||
| 184 | |||
| 185 | method squeezenet1_0(%kwargs) | ||
| 186 | { | ||
| 187 | return __PACKAGE__->get_squeezenet('1.0', %kwargs); | ||
| 188 | } | ||
| 189 | |||
| 190 | =head2 squeezenet1_1 | ||
| 191 | |||
| 192 | SqueezeNet 1.1 model from the official SqueezeNet repo | ||
| 193 | <https://github.com/DeepScale/SqueezeNet/tree/master/SqueezeNet_v1.1>. | ||
| 194 | SqueezeNet 1.1 has 2.4x less computation and slightly fewer parameters | ||
| 195 | than SqueezeNet 1.0, without sacrificing accuracy. | ||
| 196 | |||
| 197 | Parameters | ||
| 198 | ---------- | ||
| 199 | :$pretrained : Bool, default 0 | ||
| 200 | Whether to load the pretrained weights for model. | ||
| 201 | :$ctx : AI::MXNet::Context, default CPU | ||
| 202 | The context in which to load the pretrained weights. | ||
| 203 | :$root : Str, default '~/.mxnet/models' | ||
| 204 | Location for keeping the model parameters. | ||
| 205 | =cut | ||
| 206 | |||
| 207 | method squeezenet1_1(%kwargs) | ||
| 208 | { | ||
| 209 | return __PACKAGE__->get_squeezenet('1.1', %kwargs); | ||
| 210 | } | ||
| 211 | |||
| 212 | 1; |