从 AWS Endpoint Sagemaker 运行代码,将图像关联到代码文件脚本

问题描述 投票:0回答:0

我正在使用 AWS 端点运行 detectron2 实例分段,我使用本教程 https://github.com/aws-samples/amazon-sagemaker-pytorch-detectron2 进行对象检测,并且我适应了实例分段和运行良好,但要绘制掩码和对象标识,我需要在端点外进行,在端点内,我在脚本中只有模型(pth 文件)和设置(yml 文件)。而我想从端点做一切,只是得到最终的结果,那就是分割后的图像。

我在端点上有

my_script.py

##########################################################3
from sqlalchemy import true
from detectron2 import model_zoo
from detectron2.config import get_cfg
from detectron2.engine import DefaultPredictor
from detectron2.data import MetadataCatalog
from detectron2.utils.visualizer import Visualizer, ColorMode
import matplotlib.pyplot as plt
import cv2
import numpy as np
import math
from PIL import Image
import scipy.cluster
import sklearn.cluster


#################################################################
##############
# Macros
##############

LOGGER = logging.Logger("InferenceScript", level=logging.INFO)
HANDLER = logging.StreamHandler(sys.stdout)
HANDLER.setFormatter(logging.Formatter("%(levelname)s | %(name)s | %(message)s"))
LOGGER.addHandler(HANDLER)


static_prefix = os.path.join(os.getcwd(), './')
log_file = os.path.join(static_prefix, 'latest')

##### Iniciando meu código


path_model = "/opt/ml/model/model_final.pth"
path_cfg=  "/opt/ml/model/config.yml"

###Start detectron
cfg = get_cfg() #segmentação de instancia
cfg.merge_from_filestr(path_cfg)
cfg.MODEL.WEIGHTS = str(path_model) 
cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.9

cfg.MODEL.ROI_HEADS.NMS_THRESH_TEST = 0.5
cfg.MODEL.ROI_HEADS.INFERENCE_TH_TEST = 0.05
cfg.MODEL.ROI_HEADS.NMS_TYPE = "fast"
cfg.MODEL.ROI_HEADS.NMS_THRESH = 0.5


##To use only CPU
cfg.MODEL.DEVICE = "cpu"
predictor = DefaultPredictor(cfg)
original = inPut_Image ## how I link "Body" here? 
normalizedImg = np.zeros((1000, 1000))
original = cv2.normalize(original,normalizedImg, 0, 255, cv2.NORM_MINMAX) 
outputs = predictor(original)
MetadataCatalog.get(cfg.DATASETS.TRAIN[0]).thing_classes = ["1","2","3"]
v = Visualizer(original[:, :, ::-1],
               metadata=MetadataCatalog.get(cfg.DATASETS.TRAIN[0]),
               instance_mode=ColorMode.IMAGE_BW)
out = v.draw_instance_predictions(outputs["instances"].to("cpu"))
outPut_imagem = cv2.cvtColor(out.get_image()[:, :, ::-1], cv2.COLOR_RGBA2RGB)
cv2.imwrite('segmented_image.jpg', outPut_image) ## I think this is unnecessary?!

为了运行它,我在我的 jupyter notebook 中尝试了这段代码:

import boto3

client = boto3.client("sagemaker-runtime")
endpoint_name = "my-instance-segmentation"

# Read image
with open("my_image.jpeg", "rb") as f:
    payload = bytearray(f.read())

# Make request
content_type = "image/jpeg"
accept_mime_type = "image/jpeg"

response = client.invoke_endpoint(
    EndpointName=endpoint_name,
    Accept=accept_mime_type,
    ContentType=content_type,
    Body=payload,
)

# Write segmented output image, I want get this result
with open("segmented_image.jpeg", "wb") as f:
    f.write(response["Body"].read())

我用上面的代码作为预感,我在这里得到了他的代码(https://extrapolations.dev/model/instance-segmentation-mask-r-cnn/api/#examples),但当然不会'不工作。因为我无法将图像与我的脚本链接起来。我无法解读

Body
的逻辑,这是图像吗?我走对了?

python amazon-web-services amazon-sagemaker endpoint detectron
© www.soinside.com 2019 - 2024. All rights reserved.