forked from NVIDIA/TensorRT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
95 lines (80 loc) · 2.87 KB
/
utils.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
#
# SPDX-FileCopyrightText: Copyright (c) 1993-2022 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import os
import tensorflow as tf
from examples.data.data_loader import _NUM_CLASSES, _DEFAULT_IMAGE_SIZE, _NUM_CHANNELS
from typing import Tuple
MODELS_CLASSES_DICT = {
"resnet_50v1": tf.keras.applications.ResNet50,
"resnet_101v1": tf.keras.applications.ResNet101,
"resnet_152v1": tf.keras.applications.ResNet152,
"resnet_50v2": tf.keras.applications.ResNet50V2,
"resnet_101v2": tf.keras.applications.ResNet101V2,
"resnet_152v2": tf.keras.applications.ResNet152V2,
"mobilenet_v1": tf.keras.applications.MobileNet,
"mobilenet_v2": tf.keras.applications.MobileNetV2,
"inception_v3": tf.keras.applications.InceptionV3,
}
def get_tfkeras_model(model_name: str = "mobilenet_v1", shape: Tuple = None) -> tf.keras.Model:
"""
Creates a native tf.keras.applications model.
Args:
model_name (str): Options={model_name_options}.
Returns:
model (tf.keras.Model): model corresponding to 'model_name'.
Raises:
ValueError: raised when 'model_name' is not supported.
""".format(
model_name_options=list(MODELS_CLASSES_DICT.keys())
)
try:
model_class = MODELS_CLASSES_DICT[model_name]
except ValueError:
raise ValueError("Model {} was not found!".format(model_name))
print("Loading model as {}".format(model_class))
if shape is None:
shape = (
_DEFAULT_IMAGE_SIZE[model_name],
_DEFAULT_IMAGE_SIZE[model_name],
_NUM_CHANNELS,
)
input_img = tf.keras.layers.Input(shape=shape, name="input_1")
model = model_class(
include_top=True,
weights="imagenet",
input_tensor=input_img,
input_shape=None,
pooling=None,
classes=_NUM_CLASSES,
classifier_activation="softmax",
)
return model
def print_model_weights_shapes(model):
"""
Print shape of each layer weight.
Args:
model: Keras model
"""
print([model.get_weights()[i].shape for i in range(len(model.get_weights()))])
def ensure_dir(dirname):
"""
Create directory is doesn't exist already.
Args:
dirname: Name of the directory to create.
"""
if not os.path.exists(dirname):
os.makedirs(dirname)