尝试使用SageMaker Neo编译SageMaker语义分割模型(内置算法)时出现缺少-symbol.json错误



我已经使用内置的SageMaker语义分割算法训练了一个SageMaker语义分割模型。这将ok部署到SageMaker端点,我可以从中成功地在云中运行推理。我想在边缘设备(AWS Panorama Appliance(上使用该模型,这意味着使用SageMaker Neo将模型编译为目标设备的规格。

然而,无论我的目标设备是什么(Neo设置(,我似乎都无法用Neo编译模型,因为我得到了以下错误:

ClientError: InputConfiguration: No valid Mxnet model file -symbol.json found

语义分割模型的model.tar.gz包含hyperparams.json、model_algo-1、model_best.params。根据文档,model_algo-1是序列化的mxnet模型。Neo不支持胶子模型吗?

顺便说一句,我在另一个SageMaker内置算法k-Nearest Neighbour(k-NN(中遇到了完全相同的问题。它似乎也是在没有-symbol.json.的情况下编译的

我是否可以运行一些脚本来重新创建-symbol.json文件或转换编译后的sagemaker模型?

在用Estimator构建了我的模型后,我用SageMaker Neo编译了它,代码是:

optimized_ic = my_estimator.compile_model(
target_instance_family="ml_c5",
target_platform_os="LINUX",
target_platform_arch="ARM64",
input_shape={"data": [1,3,512,512]},  
output_path=s3_optimized_output_location,
framework="mxnet",
framework_version="1.8", 
)

我希望它可以编译,但这就是我得到错误的地方,说模型缺少*-symbol.json文件。

由于某种原因,AWS决定不使其内置算法与Neo直接兼容。。。但是,您可以使用model.tar.gz输出文件重新设计网络参数,然后进行编译。

步骤1:从tar文件中提取模型

import tarfile
#path to local tar file
model = 'ss_model.tar.gz'
#extract tar file 
t = tarfile.open(model, 'r:gz')
t.extractall()

这应该输出两个文件:model_algo-1,model_best.params

  1. 为您选择的架构从gluon模型动物园将权重加载到网络中

在这种情况下,我使用了带有resnet50 的DeepLabv3

import gluoncv
import mxnet as mx
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")
  1. 通过使用新模型进行预测来检查参数是否已正确加载

使用用于训练的图像。

#use cpu
ctx = mx.cpu(0)
#decode image bytes of loaded file
img = image.imdecode(imbytes)
#transform image
img = test_transform(img, ctx)
img = img.astype('float32')
print('tranformed image shape: ', img.shape)
#get prediction
output = model.predict(img)
  1. Sagemaker Neo要求混合模式输出

图像形状兼容性的附加检查

model.hybridize()
model(mx.nd.ones((1,3,800,1280)))
export_block('deeplabv3-res50', model, data_shape=(3,800,1280), preprocess=None, layout='CHW')
  1. 将模型重新编译为tar.gz格式

这包含Neo查找的params和json文件。

tar = tarfile.open("comp_model.tar.gz", "w:gz")
for name in ["deeplabv3-res50-0000.params", "deeplabv3-res50-symbol.json"]:
tar.add(name)
tar.close()
  1. 将tar.gz文件保存到s3,然后使用Neo GUI进行编译

相关内容

最新更新