首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >高分辨率图像的目标检测推理在cpu上花费了大量的时间

高分辨率图像的目标检测推理在cpu上花费了大量的时间
EN

Stack Overflow用户
提问于 2019-06-09 18:33:05
回答 1查看 114关注 0票数 0

我已经在pascal数据集上训练了ML模型,图像大小为224,但是当在新图像(一些是高分辨率的,一些是比pascal图像略高的)上推断时,我在pil2tensor()中得到了错误

代码语言:javascript
复制
@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分钟!

EN

回答 1

Stack Overflow用户

发布于 2019-07-30 02:25:51

修复了这个问题,下面是正确的代码:

代码语言:javascript
复制
@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})
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56513831

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档