Spaces:
Sleeping
Sleeping
File size: 5,157 Bytes
9c4b1c4 |
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 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
import sys
import time
import os
import csv
import torch
import json
from util import Logger, printSet
from validate import validate
from networks.resnet import resnet50
from options.test_options import TestOptions
import networks.resnet as resnet
import numpy as np
import random
from data import create_dataloader
from sklearn.metrics import roc_auc_score, accuracy_score
from tqdm import tqdm
import pandas as pd
def seed_torch(seed=1029):
random.seed(seed)
os.environ['PYTHONHASHSEED'] = str(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed) # if you are using multi-GPU.
torch.backends.cudnn.benchmark = False
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.enabled = False
seed_torch(100)
opt = TestOptions().parse(print_options=False)
opt.model_path = os.path.join(f'./checkpoint/{opt.name}/weights/best.pt')
print(f'Model_path {opt.model_path}')
# get model
model = resnet50(num_classes=1)
model.load_state_dict(torch.load(opt.model_path, map_location='cpu'), strict=True)
model.to(opt.device)
model.eval()
opt.no_resize = False
opt.no_crop = True
output_dir = f'./results/{opt.name}/data/{opt.data_keys}'
os.makedirs(output_dir, exist_ok=True)
test_dataloader = create_dataloader(opt, split='test')
model.eval()
# File paths
csv_filename = os.path.join(output_dir, 'results.csv')
metrics_filename = os.path.join(output_dir, 'metrics.json')
image_results_filename = os.path.join(output_dir, 'image_results.json')
# Extract training dataset keys from model name (format: "training_keys_freeze_down" or "training_keys")
training_dataset_keys = []
model_name = opt.name
if '_freeze_down' in model_name:
training_name = model_name.replace('_freeze_down', '')
else:
training_name = model_name
if '&' in training_name:
training_dataset_keys = training_name.split('&')
else:
training_dataset_keys = [training_name]
# Collect all results
all_scores = []
all_labels = []
all_paths = []
image_results = []
start_time = time.time()
# Write CSV header
with open(csv_filename, 'w') as f:
f.write(f"{','.join(['name', 'pro', 'flag'])}\n")
with torch.no_grad():
with tqdm(test_dataloader, unit='batch', mininterval=0.5) as tbatch:
tbatch.set_description(f'Validation')
for (data, labels, paths) in tbatch:
data = data.to(opt.device)
labels = labels.to(opt.device)
scores = model(data).squeeze(1)
# Collect results
for score, label, path in zip(scores, labels, paths):
score_val = score.item()
label_val = label.item()
all_scores.append(score_val)
all_labels.append(label_val)
all_paths.append(path)
image_results.append({
'path': path,
'score': score_val,
'label': label_val
})
# Write to CSV (maintain backward compatibility)
with open(csv_filename, 'a') as f:
for score, label, path in zip(scores, labels, paths):
f.write(f"{path}, {score.item()}, {label.item()}\n")
# Calculate metrics
all_scores = np.array(all_scores)
all_labels = np.array(all_labels)
# Convert scores to probabilities using sigmoid (as done in validate.py)
probabilities = torch.sigmoid(torch.tensor(all_scores)).numpy()
# Convert probabilities to predictions using threshold 0.5 (as done in validate.py)
predictions = (probabilities > 0.5).astype(int)
# Calculate overall metrics
total_accuracy = accuracy_score(all_labels, predictions)
# TPR (True Positive Rate) = TP / (TP + FN) = accuracy on fake images (label==1)
fake_mask = all_labels == 1
if fake_mask.sum() > 0:
tpr = accuracy_score(all_labels[fake_mask], predictions[fake_mask])
else:
tpr = 0.0
# Calculate TNR on real images (label==0) in the test set
real_mask = all_labels == 0
if real_mask.sum() > 0:
# Overall TNR calculated on all real images in the test set
tnr = accuracy_score(all_labels[real_mask], predictions[real_mask])
else:
tnr = 0.0
# AUC calculation (using probabilities)
if len(np.unique(all_labels)) > 1: # Need both classes for AUC
auc = roc_auc_score(all_labels, probabilities)
else:
auc = 0.0
execution_time = time.time() - start_time
# Prepare metrics JSON
metrics = {
'TPR': float(tpr),
'TNR': float(tnr),
'Acc total': float(total_accuracy),
'AUC': float(auc),
'execution time': float(execution_time)
}
# Write metrics JSON
with open(metrics_filename, 'w') as f:
json.dump(metrics, f, indent=2)
# Write individual image results JSON
with open(image_results_filename, 'w') as f:
json.dump(image_results, f, indent=2)
print(f'\nMetrics saved to {metrics_filename}')
print(f'Image results saved to {image_results_filename}')
print(f'\nMetrics:')
print(f' TPR: {tpr:.4f}')
print(f' TNR: {tnr:.4f}')
print(f' Accuracy: {total_accuracy:.4f}')
print(f' AUC: {auc:.4f}')
print(f' Execution time: {execution_time:.2f} seconds')
|