-
Notifications
You must be signed in to change notification settings - Fork 0
/
infer.py
42 lines (30 loc) · 1.17 KB
/
infer.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
import torch
from model import SegmentationModel
from dataset_processing import validset
import matplotlib.pyplot as plt
def show_image(image, mask, pred_image=None):
if pred_image is None:
f, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 5))
ax1.set_title('IMAGE')
ax1.imshow(image.permute(1, 2, 0).squeeze(), cmap='gray')
ax2.set_title('GROUND TRUTH')
ax2.imshow(mask.permute(1, 2, 0).squeeze(), cmap='gray')
else:
f, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize=(10, 5))
ax1.set_title('IMAGE')
ax1.imshow(image.permute(1, 2, 0).squeeze(), cmap='gray')
ax2.set_title('GROUND TRUTH')
ax2.imshow(mask.permute(1, 2, 0).squeeze(), cmap='gray')
ax3.set_title('MODEL OUTPUT')
ax3.imshow(pred_image.permute(1, 2, 0).squeeze(), cmap='gray')
plt.show()
idx = 20
DEVICE = 'cuda'
model = SegmentationModel()
model.to(DEVICE)
model.load_state_dict(torch.load('best-model.pt'))
image,mask = validset[idx]
logits_mask = model(image.to(DEVICE).unsqueeze(0))
pred_mask = torch.sigmoid(logits_mask)
pred_mask = (pred_mask>0.5)*1.0
show_image(image,mask,pred_mask.detach().cpu().squeeze(0))