def initialize(self, context): self._context = context self._batch_size = context.system_properties["batch_size"] self.initialized = True properties = context.system_properties model_dir = properties.get("model_dir") n_data = 11 inputs_list = [] for i in range(n_data): inputs_list.append("data{}".format(i)) self.batch_size = 50 data_shape = [('data0', (self.batch_size, 23)), ('data1', (self.batch_size, 1)), ('data2', (self.batch_size, 1)), ('data3', (self.batch_size, 1)), ('data4', (self.batch_size, 1)), ('data5', (self.batch_size, 15)), ('data6', (self.batch_size, 15)), ('data7', (self.batch_size, 15)), ('data8', (self.batch_size, 15)), ('data9', (self.batch_size, 15)), ('data10', (self.batch_size, 15))] self.mxnet_ctx = mx.neuron() load_symbol, args, auxs = mx.model.load_checkpoint( os.path.join(model_dir, "compiled"), 0) self.mod = mx.mod.Module(symbol=load_symbol, context=mx.neuron(), data_names=inputs_list, label_names=None) self.mod.bind(for_training=False, data_shapes=data_shape, label_shapes=self.mod._label_shapes) self.mod.set_params(args, auxs)
def transform_fn(mod, payload, input_content_type, output_content_type): logging.info("Invoking user-defined transform_fn") Batch = namedtuple("Batch", ["data"]) ctx = mx.neuron() if input_content_type != "application/x-npy": raise RuntimeError("Input content type must be application/x-npy") # pre-processing io_bytes_obj = io.BytesIO(payload) npy_payload = np.load(io_bytes_obj) mx_ndarray = mx.nd.array(npy_payload.reshape((1, 28, 28))) print(mx_ndarray) mx_ndarray = mx_ndarray.expand_dims(axis=0) print(mx_ndarray) inference_payload = mx_ndarray.astype(dtype="float32") # prediction/inference mod.forward(Batch([inference_payload])) # post-processing result = mod.get_outputs()[0].asnumpy() result = np.squeeze(result) result_exp = np.exp(result - np.max(result)) result = result_exp / np.sum(result_exp) output_json = json.dumps(result.tolist()) output_content_type = "application/json" return output_json, output_content_type
def get_context(): mxnet_version = version.parse(mx.__version__) if mxnet_version >= version.parse("1.8"): import mx_neuron as neuron return mx.cpu() else: return mx.neuron()
def _load_model(self): """ Load MXNet Model """ #------- Compile and set context based on device ----------- if self.dev_type == 'inf': ctx = mx.neuron() #inferentia context elif self.dev_type == 'gpu': ctx = mx.gpu(self.dev_id) else: ctx = mx.cpu() #-------Standard MXNet checkpoint load ----------- sym, self.args, self.aux = mx.model.load_checkpoint(os.path.join(self.model_dir_path, self.model_prefix), int(self.epoch)) #-------Standard MXNet Module instantiation code----------- self.mod = mx.mod.Module(symbol=sym, context=ctx, label_names=None) print(self.mod.data_names)
def model_fn(model_dir): logging.info('Invoking user-defined model_fn') ctx = mx.neuron() Batch = namedtuple('Batch', ['data']) dtype='float32' # print("param {}".format(os.environ.get('MODEL_NAME_CUSTOM'))) print("ctx {}".format(ctx)) sym, arg_params, aux_params = mx.model.load_checkpoint(os.path.join(model_dir, 'compiled'), 0) mod = mx.mod.Module(symbol=sym, context=ctx, label_names=None) for arg in arg_params: arg_params[arg] = arg_params[arg].astype(dtype) for arg in aux_params: aux_params[arg] = aux_params[arg].astype(dtype) exe = mod.bind(for_training=False, data_shapes=[('data', (1, 1, 28, 28))], label_shapes=mod._label_shapes) mod.set_params(arg_params, aux_params, allow_missing=True) return mod
import mxnet as mx #import neomxnet import os import json import numpy as np from collections import namedtuple import os dtype = 'float32' Batch = namedtuple('Batch', ['data']) ctx = mx.neuron() is_gpu = False def model_fn(model_dir): print("param {}".format(os.environ.get('MODEL_NAME_CUSTOM'))) print("ctx {}".format(ctx)) sym, arg_params, aux_params = mx.model.load_checkpoint( os.path.join(model_dir, os.environ.get('MODEL_NAME_CUSTOM')), 0) mod = mx.mod.Module(symbol=sym, context=ctx, label_names=None) for arg in arg_params: arg_params[arg] = arg_params[arg].astype(dtype) for arg in aux_params: aux_params[arg] = aux_params[arg].astype(dtype) exe = mod.bind(for_training=False, data_shapes=[('data', (1, 3, 224, 224))], label_shapes=mod._label_shapes) mod.set_params(arg_params, aux_params, allow_missing=True) return mod