| 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::AlexNet; | ||
| 19 | use strict; | ||
| 20 | use warnings; | ||
| 21 | use AI::MXNet::Function::Parameters; | ||
| 22 | use AI::MXNet::Gluon::Mouse; | ||
| 23 | extends 'AI::MXNet::Gluon::HybridBlock'; | ||
| 24 | |||
| 25 | =head1 NAME | ||
| 26 | |||
| 27 | AI::MXNet::Gluon::ModelZoo::Vision::AlexNet - AlexNet model from the `"One weird trick..." | ||
| 28 | =cut | ||
| 29 | |||
| 30 | =head1 DESCRIPTION | ||
| 31 | |||
| 32 | AlexNet model from the "One weird trick..." <https://arxiv.org/abs/1404.5997> paper. | ||
| 33 | |||
| 34 | Parameters | ||
| 35 | ---------- | ||
| 36 | classes : Int, default 1000 | ||
| 37 | Number of classes for the output layer. | ||
| 38 | =cut | ||
| 39 | has 'classes' => (is => 'ro', isa => 'Int', default => 1000); | ||
| 40 | method python_constructor_arguments() { ['classes'] } | ||
| 41 | |||
| 42 | sub BUILD | ||
| 43 | { | ||
| 44 | my $self = shift; | ||
| 45 | $self->name_scope(sub { | ||
| 46 | $self->features(nn->HybridSequential(prefix=>'')); | ||
| 47 | $self->features->name_scope(sub { | ||
| 48 | $self->features->add(nn->Conv2D(64, kernel_size=>11, strides=>4, | ||
| 49 | padding=>2, activation=>'relu')); | ||
| 50 | $self->features->add(nn->MaxPool2D(pool_size=>3, strides=>2)); | ||
| 51 | $self->features->add(nn->Conv2D(192, kernel_size=>5, padding=>2, | ||
| 52 | activation=>'relu')); | ||
| 53 | $self->features->add(nn->MaxPool2D(pool_size=>3, strides=>2)); | ||
| 54 | $self->features->add(nn->Conv2D(384, kernel_size=>3, padding=>1, | ||
| 55 | activation=>'relu')); | ||
| 56 | $self->features->add(nn->Conv2D(256, kernel_size=>3, padding=>1, | ||
| 57 | activation=>'relu')); | ||
| 58 | $self->features->add(nn->Conv2D(256, kernel_size=>3, padding=>1, | ||
| 59 | activation=>'relu')); | ||
| 60 | $self->features->add(nn->MaxPool2D(pool_size=>3, strides=>2)); | ||
| 61 | $self->features->add(nn->Flatten()); | ||
| 62 | $self->features->add(nn->Dense(4096, activation=>'relu')); | ||
| 63 | $self->features->add(nn->Dropout(0.5)); | ||
| 64 | $self->features->add(nn->Dense(4096, activation=>'relu')); | ||
| 65 | $self->features->add(nn->Dropout(0.5)); | ||
| 66 | }); | ||
| 67 | $self->output(nn->Dense($self->classes)); | ||
| 68 | }); | ||
| 69 | } | ||
| 70 | |||
| 71 | method hybrid_forward(GluonClass $F, GluonInput $x) | ||
| 72 | { | ||
| 73 | $x = $self->features->($x); | ||
| 74 | $x = $self->output->($x); | ||
| 75 | return $x; | ||
| 76 | } | ||
| 77 | |||
| 78 | package AI::MXNet::Gluon::ModelZoo::Vision; | ||
| 79 | |||
| 80 | =head2 alexnet | ||
| 81 | |||
| 82 | AlexNet model from the `"One weird trick..." <https://arxiv.org/abs/1404.5997> paper. | ||
| 83 | |||
| 84 | Parameters | ||
| 85 | ---------- | ||
| 86 | :$pretrained : Bool, default 0 | ||
| 87 | Whether to load the pretrained weights for model. | ||
| 88 | :$ctx : AI::MXNet::Context, default AI::MXNet::Context->cpu | ||
| 89 | The context in which to load the pretrained weights. | ||
| 90 | :$root : Str, default '~/.mxnet/models' | ||
| 91 | Location for keeping the model parameters. | ||
| 92 | =cut | ||
| 93 | |||
| 94 | method alexnet( | ||
| 95 | Bool :$pretrained=0, | ||
| 96 | AI::MXNet::Context :$ctx=AI::MXNet::Context->cpu(), | ||
| 97 | Str :$root='~/.mxnet/models', | ||
| 98 | Int :$classes=1000 | ||
| 99 | ) | ||
| 100 | { | ||
| 101 | my $net = AI::MXNet::Gluon::ModelZoo::Vision::AlexNet->new($classes); | ||
| 102 | if($pretrained) | ||
| 103 | { | ||
| 104 | $net->load_parameters( | ||
| 105 | AI::MXNet::Gluon::ModelZoo::ModelStore->get_model_file( | ||
| 106 | 'alexnet', | ||
| 107 | root=>$root | ||
| 108 | ), | ||
| 109 | ctx=>$ctx | ||
| 110 | ); | ||
| 111 | } | ||
| 112 | return $net; | ||
| 113 | } | ||
| 114 | |||
| 115 | 1; |