我已经在pascal数据集上训练了ML模型,图像大小为224,但是当在新图像(一些是高分辨率的,一些是比pascal图像略高的)上推断时,我在pil2tensor()中得到了错误
@app.route('/analyze', methods=['POST'])
async def analyze(request):
data = await request.form()
img_bytes = await (data['file'].read())
img = open_image(BytesIO(img_bytes))
t_img= PIL.Image.open(BytesIO(img_bytes)).convert('RGB')
t_img = pil2tensor(t_img, np.float32)
t_img = t_img.div_(255)
with torch.no_grad():
# test_output = learn.model.eval()(t_img.unsqueeze_(0).cuda())
test_output = learn.model.eval()(t_img.unsqueeze_(0))对于较小的图像尺寸(如来自google的一些低分辨率图像),ML模型能够在几秒钟内做出正确的推断,但对于稍高分辨率的图像,它需要大约20-40分钟!
发布于 2019-07-30 02:25:51
修复了这个问题,下面是正确的代码:
@app.route('/analyze', methods=['POST'])
async def analyze(request):
data = await request.form()
img_bytes = await (data['file'].read())
img = open_image(BytesIO(img_bytes))
localtime = _utc_to_local(datetime.utcnow())
current_dir = os.path.dirname(__file__)
media_path = os.path.join(current_dir, "media")
media_path_original = os.path.join(media_path, "original")
media_path_processed = os.path.join(media_path, "processed")
img_path = os.path.join(media_path_original, localtime+".jpg")
img.save(img_path)
verify_image(Path(img_path), idx=0, delete=False, max_size=600, dest=Path(media_path_processed))
processed_img_path = os.path.join(media_path_processed, localtime+".jpg")
processed_img = open_image(processed_img_path) if os.path.exists(processed_img_path) else img
processed_img.refresh()
with torch.no_grad():
test_output = learn.model.eval()(processed_img.data.unsqueeze(0))
predictions = show_preds(processed_img, test_output, 0, detect_thresh=0.4, classes=t_classes)
img_height, img_width = processed_img.size
return JSONResponse({'predictions': predictions, 'img_height': img_height, 'img_width': img_width})https://stackoverflow.com/questions/56513831
复制相似问题