-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpredict.py
74 lines (59 loc) · 2.49 KB
/
predict.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import os
import time
import cv2
import torch
from torchvision import transforms
import numpy as np
from PIL import Image
from src import UNet, UNet_3Plus, UNet_2Plus, AttU_Net
def time_synchronized():
torch.cuda.synchronize() if torch.cuda.is_available() else None
return time.time()
def main():
classes = 1 # exclude background
weights_path = r"E:\Project\Python\ImageSegmentation\new_weights\AttU_Netcovid.pth"
img_path = r"E:\Project\Python\ImageSegmentation\kneedata\test\images\72.png"
assert os.path.exists(weights_path), f"weights {weights_path} not found."
assert os.path.exists(img_path), f"image {img_path} not found."
mean = (0.709, 0.381, 0.224)
std = (0.127, 0.079, 0.043)
# get devices
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
print("using {} device.".format(device))
# create model
# model = UNet(in_channels=3, num_classes=classes+1, base_c=32)
model = AttU_Net(in_channels=3, num_classes=2)
#model = UNet_3Plus(in_channels=3, n_classes=classes + 1)
#model = UNet_2Plus(in_channels=3, n_classes=classes + 1)
# load weights
model.load_state_dict(torch.load(weights_path, map_location='cpu')['model'])
model.to(device)
# load image
original_img = Image.open(img_path).convert('RGB')
# from pil image to tensor and normalize
data_transform = transforms.Compose([transforms.ToTensor(),
transforms.CenterCrop(256),
transforms.Normalize(mean=mean, std=std)])
img = data_transform(original_img)
# expand batch dimension
img = torch.unsqueeze(img, dim=0)
model.eval() # 进入验证模式
with torch.no_grad():
# init model
img_height, img_width = img.shape[-2:]
init_img = torch.zeros((1, 3, img_height, img_width), device=device)
model(init_img)
t_start = time_synchronized()
output = model(img.to(device))
t_end = time_synchronized()
print("inference+NMS time: {}".format(t_end - t_start))
prediction = output['out'].argmax(1).squeeze(0)
prediction = prediction.to("cpu").numpy().astype(np.uint8)
# 将前景对应的像素值改成255(白色)
prediction[prediction == 1] = 255
# 将不敢兴趣的区域像素设置成0(黑色)
prediction[prediction == 0] = 0
mask = Image.fromarray(prediction)
mask.save("test_result1.png")
if __name__ == '__main__':
main()