-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpredict.py
More file actions
40 lines (33 loc) · 1.23 KB
/
Copy pathpredict.py
File metadata and controls
40 lines (33 loc) · 1.23 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
from data_util import test_CustomImageDataset
from torch.utils.data import Dataset, DataLoader, ConcatDataset
import torchvision.transforms as transforms
from pathlib import Path
from PIL import Image
import torch
import os
import pandas as pd
transform = transforms.Compose(
[
transforms.Resize((128, 128)),
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
])
def prediction(net, transform):
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
net = net.to(device)
net.eval()
test_data = test_CustomImageDataset("/scratch/hh3043/ML_contest/separate/test_img", transform=transform)
test_loader = DataLoader(test_data, batch_size=16, shuffle=False, num_workers=3)
predicted_labels = []
with torch.no_grad():
for data in test_loader:
images = data
images = images.to(device)
outputs = net(images)
_, predicted = torch.max(outputs.data, 1)
predicted_labels.extend(predicted.cpu().numpy())
output = pd.DataFrame({
"id": [i for i in range(len(test_data))],
"category": predicted_labels
})
output.to_csv('/scratch/hh3043/ML_contest/my_submission.csv', index=False)