-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
48 lines (37 loc) · 1.36 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
# Author: Daiwei (David) Lu
# Make predictions on test images
import torch
from torch.utils.data import Dataset
from torchvision import transforms
import argparse
from load import TumorImage, Rescale, ToTensor, Normalize
from model import ResNet, BasicBlock
MODEL_PATH = './resnet34pre771.pth'
def test_model(path):
device = torch.device("cuda")
model = ResNet(BasicBlock, [3,4,6,3])
model = model.to(device)
model.load_state_dict(torch.load(MODEL_PATH))
model.eval()
with torch.no_grad():
dataset = TumorImage(path,
transform=transforms.Compose([
Rescale(256),
ToTensor(),
Normalize()
]))
dataloader = torch.utils.data.DataLoader(dataset, batch_size=1)
for input in dataloader:
input = input.float().cuda().to(device)
output = model(input)
val = torch.max(output, 1)[1]
return val.view(-1).cpu().numpy()[0]
def main():
# Training settings
parser = argparse.ArgumentParser(description='Test Prediction')
parser.add_argument('path', metavar='P', type=str,
help='path of file for prediction')
args = parser.parse_args()
print(test_model(args.path))
if __name__ == '__main__':
main()