[1fc74a]: / CRCNet / predict.py

Download this file

53 lines (40 with data), 1.6 kB

 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
from __future__ import print_function
import torch
import torch.nn.functional as F
from torchvision import datasets, transforms, models
import torch.utils.data.dataset
import numpy as np
from tqdm import tqdm
import pandas as pd
import utils
def predict(checkpoint, test_file, gpu_id=0):
torch.cuda.set_device(gpu_id)
model = models.densenet169(num_classes = 2).cuda()
# Load the pretrained model
model.load_state_dict(torch.load(checkpoint, map_location='cpu'))
# Set the model in evaluation mode. In this case this is for the Dropout layers
model = model.half()
model.eval()
normalize = transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
dataset_test = utils.CSVDataset(
test_file,
transforms.Compose([
transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor(),
normalize,
]))
test_loader = torch.utils.data.DataLoader(dataset_test, batch_size=32, shuffle=False, num_workers=4)
probs = []
targets = []
for data, target in tqdm(test_loader):
# Send the data and label to the device
data, target = data.half().cuda(), target.cuda()
# Forward pass the data through the model
output = model(data)
output = F.softmax(output, dim=1)
probs.extend(output.detach().cpu().numpy())
targets.extend(target.detach().cpu().numpy())
return probs, targets
if __name__ == '__main__':
pass