-
Notifications
You must be signed in to change notification settings - Fork 2
/
tst_1D_classification_tfkeras_special_cases.py
137 lines (118 loc) · 4.07 KB
/
tst_1D_classification_tfkeras_special_cases.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# coding: utf-8
__author__ = 'ZFTurbo: https://kaggle.com/zfturbo'
if __name__ == '__main__':
import os
gpu_use = 4
print('GPU use: {}'.format(gpu_use))
os.environ["KERAS_BACKEND"] = "tensorflow"
os.environ["CUDA_VISIBLE_DEVICES"] = "{}".format(gpu_use)
def get_model_memory_usage(batch_size, model):
import numpy as np
from keras import backend as K
shapes_mem_count = 0
internal_model_mem_count = 0
for l in model.layers:
layer_type = l.__class__.__name__
if layer_type == 'Model':
internal_model_mem_count += get_model_memory_usage(batch_size, l)
single_layer_mem = 1
out_shape = l.output_shape
if type(out_shape) is list:
out_shape = out_shape[0]
for s in out_shape:
if s is None:
continue
single_layer_mem *= s
shapes_mem_count += single_layer_mem
trainable_count = np.sum([K.count_params(p) for p in model.trainable_weights])
non_trainable_count = np.sum([K.count_params(p) for p in model.non_trainable_weights])
number_size = 4.0
if K.floatx() == 'float16':
number_size = 2.0
if K.floatx() == 'float64':
number_size = 8.0
total_memory = number_size * (batch_size * shapes_mem_count + trainable_count + non_trainable_count)
gbytes = np.round(total_memory / (1024.0 ** 3), 3) + internal_model_mem_count
return gbytes
def tst_keras():
# for tensorflow.keras
from tensorflow import __version__
from tensorflow.compat.v1 import reset_default_graph
from classification_models_1D.tfkeras import Classifiers
print('Tensorflow version: {}'.format(__version__))
if 0:
type = 'resnet18'
print('Go for {}'.format(type))
modelPoint, preprocess_input = Classifiers.get(type)
model = modelPoint(
input_shape=(224*224, 2),
include_top=False,
weights=None,
stride_size=(2, 4, 4, 4, 2, 2, 2, 2),
kernel_size=3,
repetitions=(2, 2, 2, 2, 2, 2, 2),
init_filters=16,
)
print(model.summary())
reset_default_graph()
if 0:
type = 'resnext50'
print('Go for {}'.format(type))
modelPoint, preprocess_input = Classifiers.get(type)
model = modelPoint(
input_shape=(224*224, 2),
include_top=False,
weights=None,
stride_size=(4, 4, 4, 4, 2, 2),
kernel_size=3,
repetitions=(3, 4, 6, 3, 2),
init_filters=64,
)
print(model.summary())
reset_default_graph()
if 0:
type = 'densenet121'
print('Go for {}'.format(type))
modelPoint, preprocess_input = Classifiers.get(type)
model = modelPoint(
input_shape=(224*224, 2),
include_top=False,
weights=None,
stride_size=(1, 1, 2, 4, 4, 4, 4),
kernel_size=3,
repetitions=(6, 12, 24, 16, 8, 4),
init_filters=64,
)
print(model.summary())
print(get_model_memory_usage(1, model), 'GB')
reset_default_graph()
if 0:
type = 'EfficientNetB0'
print('Go for {}'.format(type))
modelPoint, preprocess_input = Classifiers.get(type)
model = modelPoint(
input_shape=(224 * 224, 2),
include_top=False,
weights=None,
stride_size=(2, 4, 8, 4, 4),
kernel_size=3,
)
print(model.summary())
print(get_model_memory_usage(1, model), 'GB')
reset_default_graph()
if 1:
type = 'EfficientNetV2B0'
print('Go for {}'.format(type))
modelPoint, preprocess_input = Classifiers.get(type)
model = modelPoint(
input_shape=(224 * 224, 2),
include_top=False,
weights=None,
stride_size=(2, 4, 8, 4, 4),
kernel_size=3,
)
print(model.summary())
print(get_model_memory_usage(1, model), 'GB')
reset_default_graph()
if __name__ == '__main__':
tst_keras()