-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
61 lines (44 loc) · 1.55 KB
/
test.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
from skimage import io
import torch
from torch.utils.data import DataLoader
from torchvision import transforms
from PIL import Image
import glob
from data_loader import Rescale
from data_loader import ToTensor
from data_loader import SalObjDataset
from model import DACNet
def normPRED(d):
ma = torch.max(d)
mi = torch.min(d)
dn = (d-mi)/(ma-mi)
return dn
def save_output(image_name, pred, d_dir):
predict = pred
predict = predict.squeeze()
predict_np = predict.cpu().data.numpy()
im = Image.fromarray(predict_np*255).convert('RGB')
image = io.imread(image_name)
imo = im.resize((image.shape[1], image.shape[0]), resample=Image.BILINEAR)
img_name = image_name.split("/")[-1]
imidx = img_name.split(".")[0]
imo.save(d_dir+imidx+'.png')
image_dir = ""
prediction_dir = ""
model_dir = ""
img_name_list = glob.glob(image_dir + '*.bmp')
test_salobj_dataset = SalObjDataset(img_name_list = img_name_list, lbl_name_list = [],transform=transforms.Compose([Rescale(256),ToTensor(flag=0)]))
test_salobj_dataloader = DataLoader(test_salobj_dataset, batch_size=1,shuffle=False)
net = DACNet(3,1)
net.load_state_dict(torch.load(model_dir))
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
net.to(device)
net.eval()
for i_test, data_test in enumerate(test_salobj_dataloader):
inputs_test = data_test['image']
inputs_test = inputs_test.type(torch.FloatTensor).to(device)
d1,d2,d3,d4,d5,d6,d7= net(inputs_test)
pred = d1[:,0,:,:]
pred = normPRED(pred)
save_output(img_name_list[i_test],pred,prediction_dir)
del d1,d2,d3,d4,d5,d6,d7