我正在尝试在 Sagemaker 实例上的 MXNet 上制作迁移学习方法。训练和服务在本地开始没有任何问题,我正在使用该 python 代码来预测:
def predict_mx(net, fname):
with open(fname, 'rb') as f:
img = image.imdecode(f.read())
plt.imshow(img.asnumpy())
plt.show()
data = transform(img, -1, test_augs)
plt.imshow(data.transpose((1,2,0)).asnumpy()/255)
plt.show()
data = data.expand_dims(axis=0)
return net.predict(data.asnumpy().tolist())
我检查data.asnumpy().tolist()
了没问题并且 pyplot 绘制图像(第一个是原始图像,第二个是调整大小的图像)。但net.predict
引发错误:
---------------------------------------------------------------------------
JSONDecodeError Traceback (most recent call last)
<ipython-input-171-ea0f1f5bdc72> in <module>()
----> 1 predict_mx(predictor.predict, './data2/burgers-imgnet/00103785.jpg')
<ipython-input-170-150a72b14997> in predict_mx(net, fname)
30 plt.show()
31 data = data.expand_dims(axis=0)
---> 32 return net(data.asnumpy().tolist())
33
~/Projects/Lab/ML/AWS/v/lib64/python3.6/site-packages/sagemaker/predictor.py in predict(self, data)
89 if self.deserializer is not None:
90 # It's the deserializer's responsibility to close the stream
---> 91 return self.deserializer(response_body, response['ContentType'])
92 data = response_body.read()
93 response_body.close()
~/Projects/Lab/ML/AWS/v/lib64/python3.6/site-packages/sagemaker/predictor.py in __call__(self, stream, content_type)
290 """
291 try:
--> 292 return json.load(codecs.getreader('utf-8')(stream))
293 finally:
294 stream.close()
/usr/lib64/python3.6/json/__init__.py in load(fp, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
297 cls=cls, object_hook=object_hook,
298 parse_float=parse_float, parse_int=parse_int,
--> 299 parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
300
301
/usr/lib64/python3.6/json/__init__.py in loads(s, encoding, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
352 parse_int is None and parse_float is None and
353 parse_constant is None and object_pairs_hook is None and not kw):
--> 354 return _default_decoder.decode(s)
355 if cls is None:
356 cls = JSONDecoder
/usr/lib64/python3.6/json/decoder.py in decode(self, s, _w)
337
338 """
--> 339 obj, end = self.raw_decode(s, idx=_w(s, 0).end())
340 end = _w(s, end).end()
341 if end != len(s):
/usr/lib64/python3.6/json/decoder.py in raw_decode(self, s, idx)
355 obj, end = self.scan_once(s, idx)
356 except StopIteration as err:
--> 357 raise JSONDecodeError("Expecting value", s, err.value) from None
358 return obj, end
JSONDecodeError: Expecting value: line 1 column 1 (char 0)
我试图 json.dumps 我的数据,这没有问题。
请注意,我还没有在 AWS 上部署该服务,我希望能够先在本地测试模型和预测,然后再制造更大的火车并在以后提供服务。
谢谢你的帮助