ModelZoo/ModelStore.pm untested

filter: Statementstmt 0.0% 0 / 84 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 6 Podpod n/a totaltotal 0.0% 0 / 107 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
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::ModelStore;
19 use strict;
20 use warnings;
21 use AI::MXNet::Function::Parameters;
22  
23 =head1 NAME
24  
25 AI::MXNet::Gluon::ModelZoo::ModelStore - Model zoo for pre-trained models.
26 =cut
27  
28 use AI::MXNet::Gluon::Utils qw(download check_sha1);
29 use IO::Uncompress::Unzip qw(unzip);
30 use File::Path qw(make_path);
31  
32 my %_model_sha1 = map { $_->[1] => $_->[0] } (
33 ['44335d1f0046b328243b32a26a4fbd62d9057b45', 'alexnet'],
34 ['f27dbf2dbd5ce9a80b102d89c7483342cd33cb31', 'densenet121'],
35 ['b6c8a95717e3e761bd88d145f4d0a214aaa515dc', 'densenet161'],
36 ['2603f878403c6aa5a71a124c4a3307143d6820e9', 'densenet169'],
37 ['1cdbc116bc3a1b65832b18cf53e1cb8e7da017eb', 'densenet201'],
38 ['ed47ec45a937b656fcc94dabde85495bbef5ba1f', 'inceptionv3'],
39 ['9f83e440996887baf91a6aff1cccc1c903a64274', 'mobilenet0.25'],
40 ['8e9d539cc66aa5efa71c4b6af983b936ab8701c3', 'mobilenet0.5'],
41 ['529b2c7f4934e6cb851155b22c96c9ab0a7c4dc2', 'mobilenet0.75'],
42 ['6b8c5106c730e8750bcd82ceb75220a3351157cd', 'mobilenet1.0'],
43 ['36da4ff1867abccd32b29592d79fc753bca5a215', 'mobilenetv2_1.0'],
44 ['e2be7b72a79fe4a750d1dd415afedf01c3ea818d', 'mobilenetv2_0.75'],
45 ['aabd26cd335379fcb72ae6c8fac45a70eab11785', 'mobilenetv2_0.5'],
46 ['ae8f9392789b04822cbb1d98c27283fc5f8aa0a7', 'mobilenetv2_0.25'],
47 ['a0666292f0a30ff61f857b0b66efc0228eb6a54b', 'resnet18_v1'],
48 ['48216ba99a8b1005d75c0f3a0c422301a0473233', 'resnet34_v1'],
49 ['0aee57f96768c0a2d5b23a6ec91eb08dfb0a45ce', 'resnet50_v1'],
50 ['d988c13d6159779e907140a638c56f229634cb02', 'resnet101_v1'],
51 ['671c637a14387ab9e2654eafd0d493d86b1c8579', 'resnet152_v1'],
52 ['a81db45fd7b7a2d12ab97cd88ef0a5ac48b8f657', 'resnet18_v2'],
53 ['9d6b80bbc35169de6b6edecffdd6047c56fdd322', 'resnet34_v2'],
54 ['ecdde35339c1aadbec4f547857078e734a76fb49', 'resnet50_v2'],
55 ['18e93e4f48947e002547f50eabbcc9c83e516aa6', 'resnet101_v2'],
56 ['f2695542de38cf7e71ed58f02893d82bb409415e', 'resnet152_v2'],
57 ['264ba4970a0cc87a4f15c96e25246a1307caf523', 'squeezenet1.0'],
58 ['33ba0f93753c83d86e1eb397f38a667eaf2e9376', 'squeezenet1.1'],
59 ['dd221b160977f36a53f464cb54648d227c707a05', 'vgg11'],
60 ['ee79a8098a91fbe05b7a973fed2017a6117723a8', 'vgg11_bn'],
61 ['6bc5de58a05a5e2e7f493e2d75a580d83efde38c', 'vgg13'],
62 ['7d97a06c3c7a1aecc88b6e7385c2b373a249e95e', 'vgg13_bn'],
63 ['e660d4569ccb679ec68f1fd3cce07a387252a90a', 'vgg16'],
64 ['7f01cf050d357127a73826045c245041b0df7363', 'vgg16_bn'],
65 ['ad2f660d101905472b83590b59708b71ea22b2e5', 'vgg19'],
66 ['f360b758e856f1074a85abd5fd873ed1d98297c3', 'vgg19_bn']
67 );
68  
69 my $apache_repo_url = 'http://apache-mxnet.s3-accelerate.dualstack.amazonaws.com/';
70 my $_url_format = '%sgluon/models/%s.zip';
71  
72 func short_hash($name)
73 {
74 Carp::confess("model $name is not available in model zoo") unless exists $_model_sha1{$name};
75 return substr($_model_sha1{$name}, 0, 8);
76 }
77  
78 =head2 get_model_file
79  
80 Return location for the pretrained on local file system.
81  
82 This function will download from online model zoo when model cannot be found or has mismatch.
83 The root directory will be created if it doesn't exist.
84  
85 Parameters
86 ----------
87 $name : Str
88 Name of the model.
89 :$root : Str, default '~/.mxnet/models'
90 Location for keeping the model parameters.
91  
92 Returns
93 -------
94 $file_path
95 Path to the requested pretrained model file.
96 =cut
97  
98 method get_model_file(Str $name, Str :$root='~/.mxnet/models')
99 {
100 my $file_name = "$name-".short_hash($name);
101 $root =~ s/~/$ENV{HOME}/;
102 my $file_path = "$root/$file_name.params";
103 my $sha1_hash = $_model_sha1{$name};
104 if(-f $file_path)
105 {
106 if(check_sha1($file_path, $sha1_hash))
107 {
108 return $file_path;
109 }
110 else
111 {
112 warn("Mismatch in the content of model file detected. Downloading again.\n");
113 }
114 }
115 else
116 {
117 warn("Model file is not found. Downloading.\n");
118 }
119  
120 if(not -d $root)
121 {
122 make_path($root);
123 }
124  
125 my $zip_file_path = "$root/$file_name.zip";
126 my $repo_url = $ENV{MXNET_GLUON_REPO}//$apache_repo_url;
127 if($repo_url !~ /\/$/)
128 {
129 $repo_url .= '/';
130 }
131 download(
132 sprintf($_url_format, $repo_url, $file_name),
133 path=>$zip_file_path,
134 overwrite=>1
135 );
136 unzip($zip_file_path, $file_path);
137 unlink $zip_file_path;
138 if(check_sha1($file_path, $sha1_hash))
139 {
140 return $file_path;
141 }
142 else
143 {
144 Carp::confess("Downloaded file $file_path has different hash. Please try again.");
145 }
146 }
147  
148 =head2 purge
149  
150 Purge all pretrained model files in local file store.
151  
152 Parameters
153 ----------
154 root : str, default '~/.mxnet/models'
155 Location for keeping the model parameters.
156 =cut
157  
158 method purge(Str $root='~/.mxnet/models')
159 {
160 $root =~ s/~/$ENV{HOME}/;
161 map { unlink } glob("$root/*.params");
162 }
163  
164 1;