-
Notifications
You must be signed in to change notification settings - Fork 4
/
test.py
213 lines (195 loc) · 8.06 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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import torch
import torch.nn as nn
from torch.utils.data import DataLoader, TensorDataset
import torchvision.transforms as transforms
from sklearn.metrics import average_precision_score, precision_score, recall_score, accuracy_score
import numpy as np
from PIL import Image
import os
import clip
from tqdm import tqdm
import timm
import argparse
import random
import torchvision.models as vis_models
from dataset import *
from augment import ImageAugmentor
from mask import *
from utils import *
import torch.distributed as dist
from torch.nn.parallel import DistributedDataParallel
from torch.utils.data.distributed import DistributedSampler
os.environ['NCCL_BLOCKING_WAIT'] = '1'
os.environ['NCCL_DEBUG'] = 'WARN'
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Settings for your script")
parser.add_argument(
'--model_name',
default='RN50',
type=str,
choices=[
'RN18', 'RN34', 'RN50', 'RN50_mod', 'clip_rn50', 'clip_vitl14',
],
help='Type of model to use; includes ResNet variants'
)
parser.add_argument(
'--clip_ft',
action='store_true',
help='For loading a finetuned clip model'
)
parser.add_argument(
'--mask_type',
default='spectral',
choices=[
'patch',
'spectral',
'pixel',
'nomask'],
help='Type of mask generator'
)
parser.add_argument(
'--band',
default='all',
type=str,
choices=[
'all', 'low', 'mid', 'high',]
)
parser.add_argument(
'--pretrained',
action='store_true',
help='For pretraining'
)
parser.add_argument(
'--ratio',
type=int,
default=50,
help='Ratio of mask to apply'
)
parser.add_argument(
'--batch_size',
type=int,
default=64,
help='Batch Size'
)
parser.add_argument(
'--data_type',
default="Wang_CVPR20",
type=str,
choices=['Wang_CVPR20', 'Ojha_CVPR23'],
help="Dataset Type"
)
parser.add_argument(
'--other_model',
action='store_true',
help='if the model is from my own code'
)
parser.add_argument('--local_rank', type=int, default=0, help='Local rank for distributed training')
args = parser.parse_args()
seed = 42
torch.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
np.random.seed(seed)
random.seed(seed)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
device = torch.device(f'cuda:{args.local_rank}')
torch.cuda.set_device(device)
dist.init_process_group(backend='nccl')
model_name = args.model_name.lower()
finetune = 'ft' if args.pretrained else ''
band = '' if args.band == 'all' else args.band
if args.mask_type != 'nomask':
ratio = args.ratio
checkpoint_path = f'checkpoints/mask_{ratio}/{model_name}{finetune}_{band}{args.mask_type}mask.pth'
else:
ratio = 0
checkpoint_path = f'checkpoints/mask_{ratio}/{model_name}{finetune}.pth'
# Define the path to the results file
results_path = f'results/{args.data_type.lower()}'
os.makedirs(results_path, exist_ok=True)
filename = f'{model_name}{finetune}_{band}{args.mask_type}mask{ratio}.txt'
# Pretty print the arguments
print("\nSelected Configuration:")
print("-" * 30)
print(f"Device: {args.local_rank}")
print(f"Dataset Type: {args.data_type}")
print(f"Model type: {args.model_name}")
print(f"Ratio of mask: {ratio}")
print(f"Batch Size: {args.batch_size}")
print(f"Mask Type: {args.mask_type}")
print(f"Checkpoint Type: {checkpoint_path}")
print(f"Results saved to: {results_path}/{filename}")
print("-" * 30, "\n")
if args.data_type == 'Wang_CVPR20':
datasets = {
'ProGAN': '/home/users/chandler_doloriel/scratch/Datasets/Wang_CVPR2020/testing/progan',
'CycleGAN': '/home/users/chandler_doloriel/scratch/Datasets/Wang_CVPR2020/testing/cyclegan',
'BigGAN': '/home/users/chandler_doloriel/scratch/Datasets/Wang_CVPR2020/testing/biggan',
'StyleGAN': '/home/users/chandler_doloriel/scratch/Datasets/Wang_CVPR2020/testing/stylegan',
'GauGAN': '/home/users/chandler_doloriel/scratch/Datasets/Wang_CVPR2020/testing/gaugan',
'StarGAN': '/home/users/chandler_doloriel/scratch/Datasets/Wang_CVPR2020/testing/stargan',
'DeepFake': '/home/users/chandler_doloriel/scratch/Datasets/Wang_CVPR2020/testing/deepfake',
'SITD': '/home/users/chandler_doloriel/scratch/Datasets/Wang_CVPR2020/testing/seeingdark',
'SAN': '/home/users/chandler_doloriel/scratch/Datasets/Wang_CVPR2020/testing/san',
'CRN': '/home/users/chandler_doloriel/scratch/Datasets/Wang_CVPR2020/testing/crn',
'IMLE': '/home/users/chandler_doloriel/scratch/Datasets/Wang_CVPR2020/testing/imle',
}
# elif args.data_type == 'GenImage':
# datasets = {
# 'VQDM': '/home/users/chandler_doloriel/scratch/Datasets/GenImage/imagenet_vqdm/imagenet_vqdm/val',
# 'Glide': '/home/users/chandler_doloriel/scratch/Datasets/GenImage/imagenet_glide/imagenet_glide/val',
# }
elif args.data_type == 'Ojha_CVPR23':
datasets = {
'Guided': '/home/users/chandler_doloriel/scratch/Datasets/Ojha_CVPR2023/guided',
'LDM_200': '/home/users/chandler_doloriel/scratch/Datasets/Ojha_CVPR2023/ldm_200',
'LDM_200_cfg': '/home/users/chandler_doloriel/scratch/Datasets/Ojha_CVPR2023/ldm_200_cfg',
'LDM_100': '/home/users/chandler_doloriel/scratch/Datasets/Ojha_CVPR2023/ldm_100',
'Glide_100_27': '/home/users/chandler_doloriel/scratch/Datasets/Ojha_CVPR2023/glide_100_27',
'Glide_50_27': '/home/users/chandler_doloriel/scratch/Datasets/Ojha_CVPR2023/glide_50_27',
'Glide_100_10': '/home/users/chandler_doloriel/scratch/Datasets/Ojha_CVPR2023/glide_100_10',
'DALL-E': '/home/users/chandler_doloriel/scratch/Datasets/Ojha_CVPR2023/dalle',
}
else:
raise ValueError("wrong dataset type")
# Initialize a counter
dataset_count = len(datasets)
for dataset_name, dataset_path in datasets.items():
if dist.get_rank() == 0:
print(f"\nEvaluating {dataset_name}")
avg_ap, avg_acc, auc = evaluate_model(
args.model_name,
args.data_type,
args.mask_type,
ratio/100,
dataset_path,
args.batch_size,
checkpoint_path,
device,
args,
)
if dist.get_rank() == 0:
# Write the results to the file
with open(f'{results_path}/{filename}', 'a') as file:
if file.tell() == 0: # Check if the file is empty
file.write("Selected Configuration:\n")
file.write("-" * 28 + "\n")
file.write(f"Device: {args.local_rank}\n")
file.write(f"Dataset Type: {args.data_type}\n")
file.write(f"Model type: {args.model_name}\n")
file.write(f"Ratio of mask: {ratio}\n")
file.write(f"Batch Size: {args.batch_size}\n")
file.write(f"Mask Type: {args.mask_type}\n")
file.write(f"Checkpoint Type: {checkpoint_path}\n")
file.write(f"Results saved to: {results_path}/{filename}\n")
file.write("-" * 28 + "\n\n")
file.write("Dataset, Precision, Accuracy, AUC\n")
file.write("-" * 28)
file.write("\n")
file.write(f"{dataset_name}, {avg_ap*100:.2f}, {avg_acc*100:.2f}, {auc:.3f}\n")
# Decrement the counter
dataset_count -= 1
if dataset_count == 0:
with open(f'{results_path}/{filename}', 'a') as file:
file.write("-" * 28 + "\n")
file.write("\n")