Skip to content

Instantly share code, notes, and snippets.

@olga-gorun
Created June 4, 2018 15:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save olga-gorun/895bd09c39aae363121b3836764c0d4e to your computer and use it in GitHub Desktop.
Save olga-gorun/895bd09c39aae363121b3836764c0d4e to your computer and use it in GitHub Desktop.
mxnet - docker performance results
import argparse, os, cv2
import mxnet as mx, numpy as np
from collections import namedtuple
IMAGE_SIZE=224
def resize_min(img, msize = 224):
old_size = img.shape[:2] # old_size is in (height, width) format
ratio = float(msize)/max(old_size)
new_size = tuple([int(x*ratio) for x in old_size])
# new_size should be in (width, height) format
img = cv2.resize(img, (new_size[1], new_size[0]))
delta_w = msize - new_size[1]
delta_h = msize - new_size[0]
top, bottom = delta_h//2, delta_h-(delta_h//2)
left, right = delta_w//2, delta_w-(delta_w//2)
color = [0, 0, 0]
img = cv2.copyMakeBorder(img, top, bottom, left, right, cv2.BORDER_CONSTANT, value=color)
return img
def load_model_for_batch_prediction(prefix, checkpoint_epoch, shape, device):
sym, arg_params, aux_params = mx.model.load_checkpoint(prefix, checkpoint_epoch)
all_layers = sym.get_internals()
devs = mx.gpu() if device == 'gpu' else mx.cpu()
fe_sym = all_layers['flatten0_output']
mod = mx.mod.Module(symbol=fe_sym, label_names=None, context=devs)
mod.bind(for_training=False, data_shapes=[('data', shape)]) # (batch_size,3,224,224)
mod.set_params(arg_params, aux_params)
return mod
def prepare_nd_array(filename):
img = cv2.imread(filename)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img = resize_min(img, IMAGE_SIZE)
img = np.swapaxes(img, 0, 2)
img = np.swapaxes(img, 1, 2)
img = img[np.newaxis, :]
return img
def predict_mxnet(model, dimentions):
Batch = namedtuple('Batch', ['data'])
model.forward(Batch([dimentions]))
prob = model.get_outputs()[0].asnumpy()
return prob
#return np.squeeze(prob)
def get_minimal_files_list_size(batch_size, files_list_size):
size = batch_size
while size + batch_size <= files_list_size:
size += batch_size
if files_list_size > size:
size += batch_size
return size
def get_files_list(input_dir, files_num, batch_size):
files_list = os.listdir(input_dir)[:files_num]
min_list_size = get_minimal_files_list_size(batch_size, len(files_list))
last_image = files_list[len(files_list) - 1]
files_list += (min_list_size - len(files_list))*[last_image]
return files_list
parser = argparse.ArgumentParser(description='Files generator')
parser.add_argument('--input-dir')
parser.add_argument('--files-num', type=int)
parser.add_argument('--model-prefix')
parser.add_argument('--model-epoch', type=int, default=0)
parser.add_argument('--device', default='cpu')
parser.add_argument('--batch-size', type=int, default=32)
# python mxnet_bulk.py --input-dir=/media/olga/war_and_terror/war_and_terror_manual_filtered --files-num=200 \
# --model-prefix=/home/olga/IdeaProjects/video-data-science/mxnet_experiments/resnet-152 --model-epoch=0 \
# --device=cpu --batch-size=32
print("start")
args = parser.parse_args()
batch_size=args.batch_size
mxnet_obj = load_model_for_batch_prediction(args.model_prefix, args.model_epoch, (batch_size,3,IMAGE_SIZE,IMAGE_SIZE), args.device)
files_list = get_files_list(args.input_dir, args.files_num, batch_size)
batches_num = round(len(files_list)/batch_size)
Batch = namedtuple('Batch', ['data'])
#for file_name in files_list:
for i in range(0, batches_num):
print(i)
# tic = time.time()
idx = range(i*batch_size, (i+1)*batch_size)
batch_files_list = [files_list[j] for j in idx]
img_batch = [prepare_nd_array(os.path.join(args.input_dir, file)) for file in batch_files_list]
img = np.concatenate(img_batch)
mxnet_obj.forward(Batch([mx.nd.array(img)]))
prob = mxnet_obj.get_outputs()[0].asnumpy()
print("done")
import argparse, os, cv2
import mxnet as mx, numpy as np, multiprocessing as mproc
from collections import namedtuple
IMAGE_SIZE=224
def resize_min(img, msize = 224):
old_size = img.shape[:2] # old_size is in (height, width) format
ratio = float(msize)/max(old_size)
new_size = tuple([int(x*ratio) for x in old_size])
# new_size should be in (width, height) format
img = cv2.resize(img, (new_size[1], new_size[0]))
delta_w = msize - new_size[1]
delta_h = msize - new_size[0]
top, bottom = delta_h//2, delta_h-(delta_h//2)
left, right = delta_w//2, delta_w-(delta_w//2)
color = [0, 0, 0]
img = cv2.copyMakeBorder(img, top, bottom, left, right, cv2.BORDER_CONSTANT, value=color)
return img
def load_model_for_batch_prediction(prefix, checkpoint_epoch, shape, device):
sym, arg_params, aux_params = mx.model.load_checkpoint(prefix, checkpoint_epoch)
all_layers = sym.get_internals()
devs = mx.gpu() if device == 'gpu' else mx.cpu()
fe_sym = all_layers['flatten0_output']
mod = mx.mod.Module(symbol=fe_sym, label_names=None, context=devs)
mod.bind(for_training=False, data_shapes=[('data', shape)]) # (batch_size,3,224,224)
mod.set_params(arg_params, aux_params)
return mod
def prepare_nd_array(filename):
img = cv2.imread(filename)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img = resize_min(img, IMAGE_SIZE)
img = np.swapaxes(img, 0, 2)
img = np.swapaxes(img, 1, 2)
img = img[np.newaxis, :]
return img
def predict_mxnet(model, dimentions):
Batch = namedtuple('Batch', ['data'])
model.forward(Batch([dimentions]))
prob = model.get_outputs()[0].asnumpy()
return prob
#return np.squeeze(prob)
def get_minimal_files_list_size(batch_size, files_list_size):
size = batch_size
while size + batch_size <= files_list_size:
size += batch_size
if files_list_size > size:
size += batch_size
return size
def get_files_list(input_dir, files_num, batch_size):
files_list = os.listdir(input_dir)[:files_num]
min_list_size = get_minimal_files_list_size(batch_size, len(files_list))
last_image = files_list[len(files_list) - 1]
files_list += (min_list_size - len(files_list))*[last_image]
return files_list
def process_bulk(files_list, i, batch_size, input_dir):
print(i)
# tic = time.time()
idx = range(i*batch_size, (i+1)*batch_size)
batch_files_list = [files_list[j] for j in idx]
img_batch = [prepare_nd_array(os.path.join(input_dir, file)) for file in batch_files_list]
img = np.concatenate(img_batch)
mxnet_obj.forward(Batch([mx.nd.array(img)]))
prob = mxnet_obj.get_outputs()[0].asnumpy()
return prob
parser = argparse.ArgumentParser(description='Files generator')
parser.add_argument('--input-dir')
parser.add_argument('--files-num', type=int)
parser.add_argument('--model-prefix')
parser.add_argument('--model-epoch', type=int, default=0)
parser.add_argument('--device', default='cpu')
parser.add_argument('--batch-size', type=int, default=32)
parser.add_argument('--pool-size', type=int)
# python mxnet_bulk_parallel.py --input-dir=/media/olga/war_and_terror/war_and_terror_manual_filtered --files-num=200 \
# --model-prefix=/home/olga/IdeaProjects/video-data-science/mxnet_experiments/resnet-152 --model-epoch=0 \
# --device=cpu --batch-size=32
print("start")
args = parser.parse_args()
batch_size=args.batch_size
mxnet_obj = load_model_for_batch_prediction(args.model_prefix, args.model_epoch, (batch_size,3,IMAGE_SIZE,IMAGE_SIZE), args.device)
files_list = get_files_list(args.input_dir, args.files_num, batch_size)
batches_num = round(len(files_list)/batch_size)
Batch = namedtuple('Batch', ['data'])
#for file_name in files_list:
# for i in range(0, batches_num):
# print(i)
# # tic = time.time()
# idx = range(i*batch_size, (i+1)*batch_size)
# batch_files_list = [files_list[j] for j in idx]
# img_batch = [prepare_nd_array(os.path.join(args.input_dir, file)) for file in batch_files_list]
# img = np.concatenate(img_batch)
# mxnet_obj.forward(Batch([mx.nd.array(img)]))
# prob = mxnet_obj.get_outputs()[0].asnumpy()
#
# print("done")
if __name__ == '__main__':
print('started')
files_list = get_files_list(args.input_dir, args.files_num, batch_size)
iterator = [(files_list, i, batch_size, args.input_dir) for i in range(0, batches_num)]#os.path.join(args.input_dir, file_name) for file_name in files_list]
with mproc.Pool(processes=min(args.pool_size, len(iterator))) as pool:
res = pool.starmap(process_bulk, iterator)
print("done")
root@da4fe28faf8a:/# python3 diagnose.py
----------Python Info----------
Version : 3.4.3
Compiler : GCC 4.8.4
Build : ('default', 'Nov 28 2017 16:41:13')
Arch : ('64bit', 'ELF')
------------Pip Info-----------
Version : 9.0.1
Directory : /usr/local/lib/python3.4/dist-packages/pip
----------MXNet Info-----------
libdc1394 error: Failed to initialize libdc1394
Version : 1.1.0
Directory : /mxnet/python/mxnet
Hashtag not found. Not installed from pre-built package.
----------System Info----------
Platform : Linux-4.4.0-1055-aws-x86_64-with-Ubuntu-14.04-trusty
system : Linux
node : da4fe28faf8a
release : 4.4.0-1055-aws
version : #64-Ubuntu SMP Thu Apr 5 17:06:36 UTC 2018
----------Hardware Info----------
machine : x86_64
processor : x86_64
Architecture: x86_64
CPU op-mode(s): 32-bit, 64-bit
Byte Order: Little Endian
CPU(s): 4
On-line CPU(s) list: 0-3
Thread(s) per core: 2
Core(s) per socket: 2
Socket(s): 1
NUMA node(s): 1
Vendor ID: GenuineIntel
CPU family: 6
Model: 85
Stepping: 3
CPU MHz: 3000.000
BogoMIPS: 6000.00
Hypervisor vendor: KVM
Virtualization type: full
L1d cache: 32K
L1i cache: 32K
L2 cache: 1024K
L3 cache: 25344K
NUMA node0 CPU(s): 0-3
----------Network Test----------
Setting timeout: 10
Timing for Gluon Tutorial(cn): https://zh.gluon.ai, DNS: 0.0604 sec, LOAD: 0.9503 sec.
Timing for Conda: https://repo.continuum.io/pkgs/free/, DNS: 0.0036 sec, LOAD: 0.0553 sec.
Timing for PYPI: https://pypi.python.org/pypi/pip, DNS: 0.0011 sec, LOAD: 0.0956 sec.
Timing for FashionMNIST: https://apache-mxnet.s3-accelerate.dualstack.amazonaws.com/gluon/dataset/fashion-mnist/train-labels-idx1-ubyte.gz, DNS: 0.0039 sec, LOAD: 0.3542 sec.
Timing for Gluon Tutorial(en): http://gluon.mxnet.io, DNS: 0.0824 sec, LOAD: 0.1584 sec.
Timing for MXNet: https://github.com/apache/incubator-mxnet, DNS: 0.0011 sec, LOAD: 0.7186 sec.
import argparse, os, cv2
import mxnet as mx, numpy as np
from collections import namedtuple
IMAGE_SIZE=224
def resize_min(img, msize = 224):
old_size = img.shape[:2] # old_size is in (height, width) format
ratio = float(msize)/min(old_size)
new_size = tuple([int(x*ratio) for x in old_size])
# new_size should be in (width, height) format
img = cv2.resize(img, (new_size[1], new_size[0]))
return img
def load_model_for_one(prefix, checkpoint_epoch, device):
print('load_model_for_one ', prefix, checkpoint_epoch)
sym, arg_params, aux_params = mx.model.load_checkpoint(prefix, checkpoint_epoch)
all_layers = sym.get_internals()
devs = mx.gpu() if device == 'gpu' else mx.cpu()
fe_sym = all_layers['flatten0_output']
mod = mx.mod.Module(symbol=fe_sym, context=devs, label_names=None)
mod.bind(for_training=False, data_shapes=[('data', (1, 3, IMAGE_SIZE, IMAGE_SIZE))])
mod.set_params(arg_params, aux_params)
return mod
def prepare_nd_array(filename):
img = cv2.imread(filename)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img = resize_min(img, IMAGE_SIZE)
img = np.swapaxes(img, 0, 2)
img = np.swapaxes(img, 1, 2)
img = img[np.newaxis, :]
return mx.nd.array(img)
def predict_mxnet(model, dimentions):
Batch = namedtuple('Batch', ['data'])
model.forward(Batch([dimentions]))
prob = model.get_outputs()[0].asnumpy()
return prob
#return np.squeeze(prob)
parser = argparse.ArgumentParser(description='Files generator')
parser.add_argument('--input-dir')
parser.add_argument('--files-num', type=int)
parser.add_argument('--model-prefix')
parser.add_argument('--model-epoch', type=int, default=0)
parser.add_argument('--device', default='cpu')
# python mxnet_one_by_one.py --input-dir=/media/olga/war_and_terror/war_and_terror_manual_filtered --files-num=200 --model-prefix=/home/olga/IdeaProjects/video-data-science/mxnet_experiments/resnet-152 --model-epoch=0
print("start")
args = parser.parse_args()
mxnet_obj = load_model_for_one(args.model_prefix, args.model_epoch, args.device)
files_list = os.listdir(args.input_dir)[:args.files_num]
for file_name in files_list:
full_path = os.path.join(args.input_dir, file_name)
array = prepare_nd_array(full_path)
mxnet_results = predict_mxnet(mxnet_obj, array)
print("done")
import argparse, os, cv2
import mxnet as mx, numpy as np, multiprocessing as mproc
from collections import namedtuple
IMAGE_SIZE=224
def resize_min(img, msize = 224):
old_size = img.shape[:2] # old_size is in (height, width) format
ratio = float(msize)/min(old_size)
new_size = tuple([int(x*ratio) for x in old_size])
# new_size should be in (width, height) format
img = cv2.resize(img, (new_size[1], new_size[0]))
return img
def load_model_for_one(prefix, checkpoint_epoch, device):
print('load_model_for_one ', prefix, checkpoint_epoch)
sym, arg_params, aux_params = mx.model.load_checkpoint(prefix, checkpoint_epoch)
devs = mx.gpu() if device == 'gpu' else mx.cpu()
all_layers = sym.get_internals()
fe_sym = all_layers['flatten0_output']
mod = mx.mod.Module(symbol=fe_sym, context=devs, label_names=None)
mod.bind(for_training=False, data_shapes=[('data', (1, 3, IMAGE_SIZE, IMAGE_SIZE))])
mod.set_params(arg_params, aux_params)
return mod
def prepare_nd_array(filename):
img = cv2.imread(filename)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img = resize_min(img, IMAGE_SIZE)
img = np.swapaxes(img, 0, 2)
img = np.swapaxes(img, 1, 2)
img = img[np.newaxis, :]
return mx.nd.array(img)
def predict_mxnet(model, dimentions):
Batch = namedtuple('Batch', ['data'])
model.forward(Batch([dimentions]))
prob = model.get_outputs()[0].asnumpy()
return prob
#return np.squeeze(prob)
def process_file(full_path):
array = prepare_nd_array(full_path)
result = predict_mxnet(mxnet_obj, array)
# print(full_path, result)
return result
parser = argparse.ArgumentParser(description='Files generator')
parser.add_argument('--input-dir')
parser.add_argument('--files-num', type=int)
parser.add_argument('--model-prefix')
parser.add_argument('--model-epoch', type=int, default=0)
parser.add_argument('--pool-size', type=int)
parser.add_argument('--device', default='cpu')
# python mxnet_parallel.py --input-dir=/media/olga/war_and_terror/war_and_terror_manual_filtered --files-num=200 \
# --model-prefix=/home/olga/IdeaProjects/video-data-science/mxnet_experiments/resnet-152 --model-epoch=0 \
# --pool-size=7 --device=cpu
args = parser.parse_args()
mxnet_obj = load_model_for_one(args.model_prefix, args.model_epoch, args.device)
ncpu = mproc.cpu_count()
print('ncpu - ' + str(ncpu))
if __name__ == '__main__':
print('started')
files_list = os.listdir(args.input_dir)[:args.files_num]
iterator = [os.path.join(args.input_dir, file_name) for file_name in files_list]
with mproc.Pool(processes=min(args.pool_size, len(iterator))) as pool:
res = pool.map(process_file, iterator)
print("done")
ubuntu@ip-192-168-33-11:~$ python3 diagnose.py
----------Python Info----------
Version : 3.5.2
Compiler : GCC 5.4.0 20160609
Build : ('default', 'Nov 23 2017 16:37:01')
Arch : ('64bit', 'ELF')
------------Pip Info-----------
Version : 8.1.1
Directory : /usr/lib/python3/dist-packages/pip
----------MXNet Info-----------
Version : 1.1.0
Directory : /home/ubuntu/.local/lib/python3.5/site-packages/mxnet
Commit Hash : 07a83a0325a3d782513a04f47d711710972cb144
----------System Info----------
Platform : Linux-4.4.0-1055-aws-x86_64-with-Ubuntu-16.04-xenial
system : Linux
node : ip-192-168-33-11
release : 4.4.0-1055-aws
version : #64-Ubuntu SMP Thu Apr 5 17:06:36 UTC 2018
----------Hardware Info----------
machine : x86_64
processor : x86_64
Architecture: x86_64
CPU op-mode(s): 32-bit, 64-bit
Byte Order: Little Endian
CPU(s): 4
On-line CPU(s) list: 0-3
Thread(s) per core: 2
Core(s) per socket: 2
Socket(s): 1
NUMA node(s): 1
Vendor ID: GenuineIntel
CPU family: 6
Model: 85
Model name: Intel(R) Xeon(R) Platinum 8124M CPU @ 3.00GHz
Stepping: 3
CPU MHz: 3000.000
BogoMIPS: 6000.00
Hypervisor vendor: KVM
Virtualization type: full
L1d cache: 32K
L1i cache: 32K
L2 cache: 1024K
L3 cache: 25344K
NUMA node0 CPU(s): 0-3
Flags: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss ht syscall nx pdpe1gb rdtscp lm constant_tsc rep_good nopl xtopology nonstop_tsc aperfmperf eagerfpu pni pclmulqdq ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowprefetch invpcid_single rsb_ctxsw retpoline kaiser fsgsbase tsc_adjust bmi1 hle avx2 smep bmi2 erms invpcid rtm mpx avx512f rdseed adx smap clflushopt clwb avx512cd xsaveopt xsavec xgetbv1 ida arat
----------Network Test----------
Setting timeout: 10
Timing for MXNet: https://github.com/apache/incubator-mxnet, DNS: 0.0029 sec, LOAD: 0.8403 sec.
Timing for Conda: https://repo.continuum.io/pkgs/free/, DNS: 0.0034 sec, LOAD: 0.0303 sec.
Timing for Gluon Tutorial(en): http://gluon.mxnet.io, DNS: 0.0368 sec, LOAD: 0.6461 sec.
Timing for PYPI: https://pypi.python.org/pypi/pip, DNS: 0.0104 sec, LOAD: 0.0911 sec.
Timing for Gluon Tutorial(cn): https://zh.gluon.ai, DNS: 0.0485 sec, LOAD: 0.4284 sec.
Timing for FashionMNIST: https://apache-mxnet.s3-accelerate.dualstack.amazonaws.com/gluon/dataset/fashion-mnist/train-labels-idx1-ubyte.gz, DNS: 0.0125 sec, LOAD: 0.3838 sec.
@ThomasDelteil
Copy link

def prepare_nd_array(filename):
    img = mx.image.imread(filename)
    img = mx.image.resize_short(img, IMAGE_SIZE)
    return img.transpose((2, 0, 1)).expand_dims(axis=0)

is a bit more concise and does not have external dependencies

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment