diff --git a/config.py b/config.py index 4e866a2..6b4159c 100644 --- a/config.py +++ b/config.py @@ -10,16 +10,15 @@ # data settings dataset_path = "zerobox_dataset" -class_name = "zerobox_class" -modelname = "zerobox_test" +class_name = "zerobox-2009-5" +modelname = "zerobox-2009-5" -# img_size = (448, 448) -img_size = (480, 270) +img_size = (448, 448) img_dims = [3] + list(img_size) add_img_noise = 0.01 # transformation settings -transf_rotations = False +transf_rotations = True transf_brightness = 0.0 transf_contrast = 0.0 transf_saturation = 0.0 @@ -36,7 +35,7 @@ # dataloader parameters n_transforms = 1 # number of transformations per sample in training -n_transforms_test = 64 # number of transformations per sample in testing +n_transforms_test = 16 # number of transformations per sample in testing batch_size = 1 # actual batch size is this value multiplied by n_transforms(_test) batch_size_test = 1 # batch_size * n_transforms // n_transforms_test diff --git a/runTest.py b/runTest.py index 6ad53ed..956519d 100644 --- a/runTest.py +++ b/runTest.py @@ -16,8 +16,9 @@ from utils import * from localization import export_gradient_maps from torch.autograd import Variable +from sklearn.metrics import roc_curve -def test(model, test_loader): +def test(model, test_loader, target_threshold): print("Running test") optimizer = torch.optim.Adam(model.nf.parameters(), lr=c.lr_init, betas=(0.8, 0.8), eps=1e-04, weight_decay=1e-5) # score_obs = Score_Observer('AUROC') @@ -56,10 +57,22 @@ def test(model, test_loader): print(f"test_labels={test_labels}, is_anomaly={is_anomaly},anomaly_score={anomaly_score}") # score_obs.update(roc_auc_score(is_anomaly, anomaly_score), epoch, # print_score=c.verbose or epoch == c.meta_epochs - 1) + # Code to calculate the accurarcy from Lihang's code + is_anomaly_detected = np.array([0 if l < target_threshold else 1 for l in anomaly_score]) - if c.grad_map_viz: - print("saving gradient maps...") - export_gradient_maps(model, test_loader, optimizer, -1) + # calculate test accuracy + error_count = 0 + for i in range(len(is_anomaly)): + if is_anomaly[i] != is_anomaly_detected[i]: + error_count += 1 + + test_accuracy = 1 - float(error_count) / len(is_anomaly) + + print(f"n_transforms_test = {c.n_transforms_test}, target_threshold={target_threshold}, test_accuracy={test_accuracy}") + + # if c.grad_map_viz: + # print("saving gradient maps...") + # export_gradient_maps(model, test_loader, optimizer, -1) def load_testloader(data_dir_test): def target_transform(target): @@ -80,8 +93,8 @@ def target_transform(target): class_idx += 1 augmentative_transforms = [] - # if c.transf_rotations: - # augmentative_transforms += [transforms.RandomRotation(180)] + if c.transf_rotations: + augmentative_transforms += [transforms.RandomRotation(180)] if c.transf_brightness > 0.0 or c.transf_contrast > 0.0 or c.transf_saturation > 0.0: augmentative_transforms += [transforms.ColorJitter(brightness=c.transf_brightness, contrast=c.transf_contrast, saturation=c.transf_saturation)] @@ -100,14 +113,15 @@ def target_transform(target): # train_set, test_set = load_datasets(c.dataset_path, c.class_name) # _, test_loader = make_dataloaders(train_set, test_set) -test_loader = load_testloader("group15B.avi/") -# model = torch.load("../zerobox-v2/zerobox_differnet_model.pt", map_location=torch.device('cpu')) -model = torch.load("models/zerobox_test.pt", map_location=torch.device('cpu')) +test_loader = load_testloader("dataset/group15B.avi.Products/test") +model = torch.load("models/zerobox-2010-1-black-yolo_0_0.10_0.05_0.10_0.05_0.9980.pth", map_location=torch.device('cpu')) +# model = torch.load("models/zerobox_test.pt", map_location=torch.device('cpu')) +target_threshold= 3.5129451751708984 print("starting to run tests after loaded model and test dataset") time_start = time.time() # model = load_model(c.modelname) -test(model, test_loader) +test(model, test_loader, target_threshold) time_end = time.time() time_c = time_end - time_start # 运行所花时间 print("time cost: {:f} s".format(time_c))