def _model(self): gpu_count = mx.context.num_gpus() ctx = mx.gpu() if gpu_count > 0 else mx.cpu() if self.ml_model_name and self.ml_model_name.split( '_')[0] == 'resnet18': model = vision.resnet18_v1(classes=len(self.LABELS), ctx=ctx) else: model = vision.mobilenet_v2_1_0(classes=len(self.LABELS), ctx=ctx) model_path = Path(self.ml_model.path) / Path( os.listdir(self.ml_model.path)[0]) model.load_parameters(str(model_path), ctx=ctx) def get_value(img: Array3D) -> Tuple[Union[str, Tuple], float]: data = img.reshape((1, ) + img.shape) data = mx.nd.array(data) scores = model(mx.nd.array(self.img_transforms(data), ctx=ctx)).softmax().asnumpy() val = self.LABELS[int(np.argmax(scores, axis=1)[0])] prob = scores[0][int(np.argmax(scores, axis=1)[0])] return val, prob return get_value
def mobilenetmxnetload(): net = vision.mobilenet_v2_1_0(pretrained=True) net.hybridize() return net
out = out.as_in_context(mx.cpu()) out = out.asnumpy() res = '' for item in out: res += str(item) +' ' #print(res) fw1.write(res+'\n') label = root.strip().split('/')[-1] if(train == True): fw2.write(label+'\n') print(label) else: fw2.write(file+'\n') print(file) fw1.close() fw2.close() print('net done!!!') pretrained_net = models.mobilenet_v2_1_0(pretrained=True) net = nn.HybridSequential() for layer in pretrained_net.features: net.add(layer) print(net) ctx = mx.gpu() net.collect_params().reset_ctx(ctx) net.hybridize() dir = 'data/test_b' suffix=['jpg'] batch_net(dir,suffix,net,train=False)
def load_mobilenet_v2_1_0(ctx): return vision.mobilenet_v2_1_0(pretrained=True, ctx=ctx, prefix="")
import sys, os import numpy as np import matplotlib.pyplot as plt import matplotlib.axes as axes from mxnet.gluon import loss as gloss, nn from mxnet.gluon.model_zoo import vision from mxnet.gluon import data as gdata from common_mx import * if __name__ == '__main__': prefix = 'model_zoo' syms = prefix + '-symbol.json' params = prefix + '-0000.params' onnx_file = prefix + '.onnx' input_shape = (1, 3, 224, 224) mobile_net = vision.mobilenet_v2_1_0(pretrained=True, ctx=ctx) mobile_net.hybridize() mobile_net(mx.nd.ones(input_shape, ctx=ctx)) mobile_net.export(prefix) # convert to onnx onnx_model_path = onnx_mxnet.export_model(syms, params, [input_shape], np.float32, onnx_file) # Load onnx model model_proto = onnx.load_model(onnx_model_path) # Check if converted ONNX protobuf is valid checker.check_graph(model_proto.graph)
################## # Hyperparameter # #----------------# ctx = mx.cpu() lr = 0.05 epochs = 10 momentum = 0.9 batch_size = 64 #----------------# # Hyperparameter # ################## ################## model from mxnet.gluon.model_zoo import vision net = vision.mobilenet_v2_1_0(classes=10, pretrained=False, ctx=ctx) # net = vision.mobilenet_v2_0_75(classes=10, pretrained=False, ctx=ctx) # net = vision.mobilenet_v2_0_5(classes=10, pretrained=False, ctx=ctx) # net = vision.mobilenet_v2_0_25(classes=10, pretrained=False, ctx=ctx) ################## 그래프 import gluoncv inputShape = (1, 3, 224, 224) gluoncv.utils.viz.plot_network(net, shape=inputShape) ##### 전처리 ############################################## def transformer(data, label): data = mx.image.imresize(data, 224, 224) data = mx.nd.transpose(data, (2, 0, 1)) data = data.astype(np.float32)