-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
106 lines (82 loc) · 3.56 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import numpy as np
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import os
import torch
from torch.utils.data import Dataset
from torchvision import datasets
from torchvision import transforms
from tqdm import tqdm
import cv2 as cv
from torch.utils.data import DataLoader
from torch import optim
import torchvision.models as models
from dataset import AverageMeter, ExDark_pytorch
from model import *
import torch.nn as nn
from dataset import draw_bounding_boxes, IOU, accuracy
import cv2
import argparse
parser = argparse.ArgumentParser(description='PyTorch Training')
parser.add_argument('--gpu', default="0", type=str,
help='id(s) for CUDA_VISIBLE_DEVICES')
parser.add_argument('--test_model', default="classification", type=str,
help='classification or localization')
parser.add_argument("--pretrained", default=None, type=str,
help="path to pretrained classification or localization model")
parser.add_argument("--test_annotator", default=None, type=str,
help="path to annotation csv file")
parser.add_argument("--size", default=128, type=int,
help="size = weight = height of the image to train")
parser.add_argument("--enhanced_type", default=None, type=str,
help="low light enhanced technique to train [linear_gray_transform,log_transform, gamma_transform, HE, Autoencoder]")
parser.add_argument("--batch", default=32, type=int,
help="batch size for training")
parser.add_argument("--epochs", default=200, type=int,
help="Number of epochs for training")
args = parser.parse_args()
def test_classify(test_loader):
model = Classification().cuda().train()
if args.pretrained:
model.load_state_dict(torch.load(args.pretrained))
loss_meter = AverageMeter()
acc_meter = AverageMeter()
with torch.no_grad():
for (imgs, labels, bbs, img_path) in tqdm(test_loader):
imgs, labels = imgs.cuda(), labels.cuda()
outputs = model(imgs)
acc = accuracy(torch.round(outputs), labels)
print("\n", acc)
acc_meter.update(acc, imgs.shape[0])
print("Accuracy Score", acc_meter.avg)
def test_localize(test_loader):
model = Localization().train().cuda()
if args.pretrained:
model.load_state_dict(torch.load(args.pretrained))
iou_meter = AverageMeter()
with torch.no_grad():
for (imgs, labels, bbs, img_path) in tqdm(test_loader):
imgs, bbs = imgs.cuda(), bbs.cuda()
position_outputs = model(imgs)
iouscore = IOU(bbs, position_outputs).mean()
iou_meter.update(iouscore.item(), imgs.shape[0])
print("IOU score", iou_meter.avg)
def main():
os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu
transform = transforms.Compose([
transforms.ToPILImage(),
transforms.Resize((args.size, args.size)),
transforms.ToTensor()
])
# --------------------- Dataloader -------------------------------
test_dataset = ExDark_pytorch(annotations_file=args.test_annotator,
transform=transform,
enhance_type=args.enhanced_type)
test_loader = DataLoader(test_dataset, batch_size=args.batch, shuffle=False)
if args.test_model == "classification":
test_classify(test_loader)
elif args.test_model == "localization":
test_localize(test_loader)
if __name__ == "__main__":
main()