Pytorch Detectron2 圖像預處理

detectron2/data/detection_utils.py

detectron2/data/transforms.py 

 

from detectron2.config import get_cfg
from detectron2.data import detection_utils
from detectron2.data import transforms as T
from PIL import Image
from torchvision import transforms
device = torch.device('cuda')

# 指定需要的變換
transform=transforms.Compose([transforms.Resize(256), transforms.CenterCrop(224), transforms.ToTensor(), 
                transforms.Normalize(mean=[0.485,0.456,0.406], std=[0.229,0.224,0.225])])

# 讀取圖像並施加變換和轉換格式
img = Image.open(root)
img = transform(img).unsqueeze(0)

# 建立模型
cfg = get_cfg()
model = build_resnet(cfg)

# 進行預測
o = model(img)

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章