forked from intel/neural-compressor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprepare_model.py
99 lines (81 loc) · 3.46 KB
/
prepare_model.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import os
import argparse
import enum
import tarfile
import abc
class SupportedModels(enum.Enum):
"""
Enumeration containing supported models
"""
ssd_resnet50_v1 = 'ssd_resnet50_v1'
ssd_mobilnet_v1 = 'ssd_mobilenet_v1'
class Model(abc.ABC):
"""
Base model class used to obtain the model (and perform any necessary operations to make it usable)
"""
@abc.abstractmethod
def get_pretrained_model(self, destination):
"""
Base method for obtaining a ready to use model
Args:
destination: path to where the file should be stored
"""
pass
class SsdMobilenetV1(Model):
""" Concrete implementation of the Model base class for ssd_mobilenet_v1"""
def get_pretrained_model(self, destination):
"""
Obtains a ready to use ssd_mobilenet_v1 model file.
Args:
destination: path to where the file should be stored
"""
url = 'http://download.tensorflow.org/models/object_detection/ssd_mobilenet_v1_coco_2018_01_28.tar.gz'
os.system("curl -o ssd_mobilenet_v1_coco_2018_01_28.tar.gz {0}".format(url))
with tarfile.open("ssd_mobilenet_v1_coco_2018_01_28.tar.gz") as tar:
if not os.path.exists(destination):
os.makedirs(destination)
tar.extractall(destination)
class SsdResnet50(Model):
""" Concrete implementation of the Model base class for ssd_resnet_50"""
def get_pretrained_model(self, destination):
"""
Obtains a ready to use ssd_resnet_50 model file.
Args:
destination: path to where the file should be stored
"""
url = "http://download.tensorflow.org/models/object_detection/" \
"ssd_resnet50_v1_fpn_shared_box_predictor_640x640_coco14_sync_2018_07_03.tar.gz"
os.system("curl -o ssd_resnet50_v1.tar.gz {0}".format(url))
with tarfile.open("ssd_resnet50_v1.tar.gz") as tar:
if not os.path.exists(destination):
os.makedirs(destination)
tar.extractall(destination)
def get_model(model: SupportedModels) -> Model:
"""
Factory method that returns the requested model object
Args:
model: model from SupportedModels enumeration
Returns: Concrete object inheriting the Model base class
"""
if model == SupportedModels.ssd_resnet50_v1:
return SsdResnet50()
if model == SupportedModels.ssd_mobilnet_v1:
return SsdMobilenetV1()
else:
raise AttributeError("The model {0} is not supported. Supported models: {1}"
.format(model_name, SupportedModels.__members__.keys()))
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Prepare pre-trained model for COCO object detection')
parser.add_argument('--model_name', type=str, default='ssd_resnet50_v1',
help='model to download, default is ssd_resnet50_v1',
choices=["ssd_resnet50_v1", "ssd_mobilenet_v1"])
parser.add_argument('--model_path', type=str, default='./model', help='directory to put models, default is ./model')
args = parser.parse_args()
model_name = args.model_name
model_path = args.model_path
try:
model = get_model(SupportedModels(model_name))
model.get_pretrained_model(model_path)
except AttributeError:
print("The model {0} is not supported. Supported models: {1}"
.format(model_name, SupportedModels.__members__.keys()))