<jupyter_start><jupyter_text>MXNet/Gluon Inference<jupyter_code>import os
import sys
import numpy as np
import mxnet as mx
from mxnet import gluon, nd
from collections import namedtuple
from common.params_inf import *
from common.utils import *
# Force one-gpu
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
print("OS: ", sys.platform)
print("Python: ", sys.version)
print("Numpy: ", np.__version__)
print("GPU: ", get_gpu_name())
print(get_cuda_version())
print("CuDNN Version ", get_cudnn_version())
Batch = namedtuple('Batch', ['data'])
# Create batches of fake data
fake_input_data_cl, fake_input_data_cf = give_fake_data(BATCH_SIZE*BATCHES_GPU)
print(fake_input_data_cl.shape, fake_input_data_cf.shape)
# Download Resnet weights
path='http://data.mxnet.io/models/imagenet/'
mx.test_utils.download(path+'resnet/50-layers/resnet-50-symbol.json')
mx.test_utils.download(path+'resnet/50-layers/resnet-50-0000.params')
print("Downloaded")
# Load model
sym, arg_params, aux_params = mx.model.load_checkpoint('resnet-50', 0)
# List the last 10 layers
all_layers = sym.get_internals()
# Get last layer
flatten_layer = all_layers['flatten0_output']
# We create the network
net = gluon.nn.SymbolBlock(outputs=flatten_layer, inputs=mx.sym.var('data'))
ctx = mx.gpu()
# We assign the weights
net_params = net.collect_params()
for param in arg_params:
    if param in net_params:
        net_params[param]._load_init(arg_params[param], ctx=ctx)
for param in aux_params:
    if param in net_params:
        net_params[param]._load_init(aux_params[param], ctx=ctx)
# We hybridize the network
net.hybridize()
def predict_fn(classifier, data, batchsize):
    """ Return features from classifier """
    out = nd.zeros((len(data), RESNET_FEATURES), dtype=np.float32)
    for idx, dta in yield_mb_X(data, batchsize):
        outputs = classifier(mx.nd.array(dta, ctx=ctx))
        out[idx*batchsize:(idx+1)*batchsize] = outputs
    nd.waitall()
    return out.asnumpy()
cold_start = predict_fn(net, fake_input_data_cf, BATCH_SIZE)
%%time
features = predict_fn(net, fake_input_data_cf, BATCH_SIZE)
print("Images per second {}".format((BATCH_SIZE*BATCHES_GPU)/2.2))<jupyter_output>Images per second 581.8181818181818
{'fraction_non_alphanumeric': 0.12716499544211485, 'fraction_numerical': 0.018231540565177756, 'mean_word_length': 6.012779552715655, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '383987', 'license': 'MIT', 'n_tokens_mistral': 803, 'n_tokens_neox': 772, 'n_words': 165, 'path': 'notebooks/Gluon_Inference.ipynb', 'repo_name': 'ThomasDelteil/DeepLearningFrameworks', 'size': '2194'}