def resnet_pipeline()

in container_images/batch_image/inference.py [0:0]


def resnet_pipeline(filePath, fileName):

    # set context
    ctx = mx.cpu()

    # load pre-trained model
    net = gluon.model_zoo.vision.resnet50_v1(pretrained=True, ctx=ctx)
    net.hybridize(static_alloc=True, static_shape=True)

    # load labels
    lblPath = gluon.utils.download('http://data.mxnet.io/models/imagenet/synset.txt')
    with open(lblPath, 'r') as f:
        labels = [l.rstrip() for l in f]

    # download and format image as (batch, RGB, width, height)
    img = mx.image.imread(filePath)
    img = mx.image.imresize(img, 224, 224) # resize
    img = mx.image.color_normalize(img.astype(dtype='float32')/255,
                                   mean=mx.nd.array([0.485, 0.456, 0.406]),
                                   std=mx.nd.array([0.229, 0.224, 0.225])) # normalize
    img = img.transpose((2, 0, 1)) # channel first
    img = img.expand_dims(axis=0) # batchify
    img = img.as_in_context(ctx)

    prob = net(img).softmax() # predict and normalize output
    idx = prob.topk(k=5)[0] # get top 5 result
    inferenceCsv = "InputFile,Probability,Label"
    for i in idx:
        i = int(i.asscalar())
        #print('With prob = %.5f, it contains %s' % (prob[0,i].asscalar(), labels[i]))
        inferenceCsv  = inferenceCsv + "\n" + fileName + ",{:.2f}".format(prob[0,i].asscalar()) + ','+ labels[i]

    return inferenceCsv