5

I have trained a model on AWS SageMaker by using the built-in algorithm Semantic Segmentation. This trained model named as model.tar.gz is stored on S3. So I want to download this file from S3 and then use it to make inference on my local PC without using AWS SageMaker.

Here are the three files:

  1. hyperparams.json: includes the parameters for network architecture, data inputs, and training. Refer to Semantic Segmentation Hyperparameters.

  2. model_algo-1

  3. model_best.params

My code:

import mxnet as mx
from mxnet import image
from gluoncv.data.transforms.presets.segmentation import test_transform
import gluoncv



img = image.imread('./bdd100k/validation/14df900d-c5c145cb.jpg')
img = test_transform(img, ctx)
img = img.astype('float32')


model = gluoncv.model_zoo.PSPNet(2)

# load the trained model
model.load_parameters('./model/model_best.params')

Error:

AssertionError: Parameter 'head.psp.conv1.0.weight' is missing in file './model/model_best.params', which contains parameters: 'layer3.2.bn3.beta', 'layer3.0.conv3.weight', 'conv1.1.running_var', ..., 'layer2.2.bn3.running_mean', 'layer3.4.bn2.running_mean', 'layer4.2.bn3.beta', 'layer3.4.bn3.beta'. Set allow_missing=True to ignore missing parameters.
Flair
  • 2,609
  • 1
  • 29
  • 41
min2bro
  • 4,509
  • 5
  • 29
  • 55
  • (Not am MXNET user) but the problem seems to be the mapping of parameters - and as you mentioned 'head.psp.conv1.0.weight' is missing. Can you construct the model manually with the same parameter names and than load the weights into it? Also, this might be related: https://discuss.mxnet.apache.org/t/loading-model-from-params-and-json-fails/4473/3 – Maria Apr 21 '21 at 08:12

1 Answers1

1

The following should work after extracting model_algo-1 from the tar.gz file. This will run on local ctx.

import gluoncv
from gluoncv import model_zoo
from gluoncv.data.transforms.presets.segmentation import test_transform

model = model_zoo.DeepLabV3(nclass=2, backbone='resnet50', 
pretrained_base=False, height=800, width=1280, crop_size=240)
model.load_parameters("model_algo-1")

img = test_transform(img, ctx)
img = img.astype('float32')

output = model.predict(img)
print(output.shape)
max_predict = mx.nd.squeeze(mx.nd.argmax(output, 1)).asnumpy()
print(max_predict.shape)

prob_mask = mx.nd.squeeze(output).asnumpy()

def NormalizeData(data):
    return (data - np.min(data)) / (np.max(data) - np.min(data))

target_cls_id = 1
prob_mat = prob_mask[target_cls_id, :, :]
norm_prob = NormalizeData(prob_mat)
plt.hist(norm_prob.flatten(), bins=50)
tarokoM
  • 26
  • 3