Skip to content

Commit d84d0ba

Browse files
committed
Revert "Revert "Fix a bug in resnet; add performance test in static model""
This reverts commit dff4fe2.
1 parent dff4fe2 commit d84d0ba

File tree

3 files changed

+173
-7
lines changed

3 files changed

+173
-7
lines changed

tensorlayer/models/resnet.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -150,21 +150,21 @@ def ResNet50(pretrained=False, end_with='fc1000', n_classes=1000, name=None):
150150
n = BatchNorm(name='bn_conv1', act='relu')(n)
151151
n = MaxPool2d((3, 3), strides=(2, 2), name='max_pool1')(n)
152152

153-
for i, name in enumerate(block_names):
154-
if len(name) == 2:
155-
stage = int(name[0])
156-
block = name[1]
153+
for i, block_name in enumerate(block_names):
154+
if len(block_name) == 2:
155+
stage = int(block_name[0])
156+
block = block_name[1]
157157
if block == 'a':
158158
strides = (1, 1) if stage == 2 else (2, 2)
159159
n = conv_block(n, 3, block_filters[stage - 2], stage=stage, block=block, strides=strides)
160160
else:
161161
n = identity_block(n, 3, block_filters[stage - 2], stage=stage, block=block)
162-
elif name == 'avg_pool':
162+
elif block_name == 'avg_pool':
163163
n = GlobalMeanPool2d(name='avg_pool')(n)
164-
elif name == 'fc1000':
164+
elif block_name == 'fc1000':
165165
n = Dense(n_classes, name='fc1000')(n)
166166

167-
if name == end_with:
167+
if block_name == end_with:
168168
break
169169

170170
network = Model(inputs=ni, outputs=n, name=name)
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
2+
import sys
3+
sys.path.append("/home/ruihai/tensorlayer/")
4+
5+
import time
6+
import os
7+
import psutil
8+
import tensorflow as tf
9+
import tensorlayer as tl
10+
from exp_config import random_input_generator, MONITOR_INTERVAL, NUM_ITERS, BATCH_SIZE, LERANING_RATE
11+
12+
gpus = tf.config.experimental.list_physical_devices('GPU')
13+
if gpus:
14+
for gpu in gpus:
15+
tf.config.experimental.set_memory_growth(gpu, True)
16+
17+
tl.logging.set_verbosity(tl.logging.DEBUG)
18+
19+
# get the whole model
20+
vgg = tl.models.vgg16(mode='static')
21+
22+
# system monitor
23+
info = psutil.virtual_memory()
24+
monitor_interval = MONITOR_INTERVAL
25+
avg_mem_usage = 0
26+
max_mem_usage = 0
27+
count = 0
28+
total_time = 0
29+
30+
# training setting
31+
num_iter = NUM_ITERS
32+
batch_size = BATCH_SIZE
33+
train_weights = vgg.trainable_weights
34+
optimizer = tf.optimizers.Adam(learning_rate=LERANING_RATE)
35+
loss_object = tl.cost.cross_entropy
36+
37+
# data generator
38+
gen = random_input_generator(num_iter, batch_size)
39+
40+
41+
# training function
42+
@tf.function
43+
def train_step(x_batch, y_batch):
44+
# forward + backward
45+
with tf.GradientTape() as tape:
46+
## compute outputs
47+
_logits = vgg(x_batch)
48+
## compute loss and update model
49+
_loss = loss_object(_logits, y_batch)
50+
51+
grad = tape.gradient(_loss, train_weights)
52+
optimizer.apply_gradients(zip(grad, train_weights))
53+
54+
55+
# begin training
56+
vgg.train()
57+
58+
for idx, data in enumerate(gen):
59+
start_time = time.time()
60+
61+
train_step(data[0], data[1])
62+
63+
end_time = time.time()
64+
consume_time = end_time - start_time
65+
total_time += consume_time
66+
67+
if idx % monitor_interval == 0:
68+
cur_usage = psutil.Process(os.getpid()).memory_info().rss
69+
max_mem_usage = max(cur_usage, max_mem_usage)
70+
avg_mem_usage += cur_usage
71+
count += 1
72+
tl.logging.info(
73+
"[*] {} iteration: memory usage {:.2f}MB, consume time {:.4f}s".format(
74+
idx, cur_usage / (1024 * 1024), consume_time
75+
)
76+
)
77+
78+
print('consumed time:', total_time)
79+
80+
avg_mem_usage = avg_mem_usage / count / (1024 * 1024)
81+
max_mem_usage = max_mem_usage / (1024 * 1024)
82+
print('average memory usage: {:.2f}MB'.format(avg_mem_usage))
83+
print('maximum memory usage: {:.2f}MB'.format(max_mem_usage))
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
2+
import sys
3+
sys.path.append("/home/ruihai/tensorlayer/")
4+
5+
import time
6+
import os
7+
import psutil
8+
import tensorflow as tf
9+
import tensorlayer as tl
10+
from exp_config import random_input_generator, MONITOR_INTERVAL, NUM_ITERS, BATCH_SIZE, LERANING_RATE
11+
12+
gpus = tf.config.experimental.list_physical_devices('GPU')
13+
if gpus:
14+
for gpu in gpus:
15+
tf.config.experimental.set_memory_growth(gpu, True)
16+
17+
tl.logging.set_verbosity(tl.logging.DEBUG)
18+
19+
# get the whole model
20+
vgg = tl.models.vgg16(mode='static')
21+
22+
# system monitor
23+
info = psutil.virtual_memory()
24+
monitor_interval = MONITOR_INTERVAL
25+
avg_mem_usage = 0
26+
max_mem_usage = 0
27+
count = 0
28+
total_time = 0
29+
30+
# training setting
31+
num_iter = NUM_ITERS
32+
batch_size = BATCH_SIZE
33+
train_weights = vgg.trainable_weights
34+
optimizer = tf.optimizers.Adam(learning_rate=LERANING_RATE)
35+
loss_object = tl.cost.cross_entropy
36+
37+
# data generator
38+
gen = random_input_generator(num_iter, batch_size)
39+
40+
41+
# training function
42+
def train_step(x_batch, y_batch):
43+
# forward + backward
44+
with tf.GradientTape() as tape:
45+
## compute outputs
46+
_logits = vgg(x_batch)
47+
## compute loss and update model
48+
_loss = loss_object(_logits, y_batch)
49+
50+
grad = tape.gradient(_loss, train_weights)
51+
optimizer.apply_gradients(zip(grad, train_weights))
52+
return _loss
53+
54+
55+
# begin training
56+
vgg.train()
57+
58+
for idx, data in enumerate(gen):
59+
start_time = time.time()
60+
61+
loss = train_step(data[0], data[1])
62+
63+
end_time = time.time()
64+
consume_time = end_time - start_time
65+
total_time += consume_time
66+
67+
if idx % monitor_interval == 0:
68+
cur_usage = psutil.Process(os.getpid()).memory_info().rss
69+
max_mem_usage = max(cur_usage, max_mem_usage)
70+
avg_mem_usage += cur_usage
71+
count += 1
72+
tl.logging.info(
73+
"[*] {} iteration: memory usage {:.2f}MB, consume time {:.4f}s, loss {:.4f}".format(
74+
idx, cur_usage / (1024 * 1024), consume_time, loss
75+
)
76+
)
77+
78+
print('consumed time:', total_time)
79+
80+
avg_mem_usage = avg_mem_usage / count / (1024 * 1024)
81+
max_mem_usage = max_mem_usage / (1024 * 1024)
82+
print('average memory usage: {:.2f}MB'.format(avg_mem_usage))
83+
print('maximum memory usage: {:.2f}MB'.format(max_mem_usage))

0 commit comments

Comments
 (0)