-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpick_indices_afhq.py
149 lines (130 loc) · 4.71 KB
/
pick_indices_afhq.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
import pickle
import torch
import torch.nn as nn
import torch.optim as optim
import torchvision
from torchvision import datasets, models, transforms
import torch.backends.cudnn as cudnn
import numpy as np
import matplotlib.pyplot as plt
import time
import os
from tqdm import tqdm
from configs.paths_config import (
DATASET_PATHS,
MODEL_PATHS,
HYBRID_MODEL_PATHS,
HYBRID_CONFIG,
)
from datasets.data_utils import get_dataset
import torchvision.transforms as tfs
import random
random.seed(0)
def shuffle_helper(lst):
random.shuffle(lst)
return lst
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") # device object
train_transform = tfs.Compose(
[
transforms.Resize((256, 256)),
# transforms.RandomHorizontalFlip(), # data augmentation
tfs.ToTensor(),
tfs.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5), inplace=True),
]
)
test_transform = tfs.Compose(
[
transforms.Resize((256, 256)),
tfs.ToTensor(),
tfs.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5), inplace=True),
]
)
data_dir = "data/afhq"
train_dataset = datasets.ImageFolder(os.path.join(data_dir, "train"), train_transform)
test_dataset = datasets.ImageFolder(os.path.join(data_dir, "val"), test_transform)
print("Train dataset size:", len(train_dataset))
print("Test dataset size:", len(test_dataset))
print(train_dataset.class_to_idx)
print(test_dataset.class_to_idx)
print("Train dataset size:", len(train_dataset))
print("Test dataset size:", len(test_dataset))
source_index_train = shuffle_helper(np.arange(len(train_dataset)))
target_index_train = shuffle_helper(np.arange(len(train_dataset)))
source_index_test = shuffle_helper(np.arange(len(test_dataset)))
target_index_test = shuffle_helper(np.arange(len(test_dataset)))
saved_indices = {
"source_index_train": [],
"target_index_train": [],
"source_index_test": [],
"target_index_test": [],
}
class_names = train_dataset.classes
# print('Class names:', class_names)
model = models.resnet18(pretrained=True)
num_features = model.fc.in_features
model.fc = nn.Linear(
num_features, len(train_dataset.classes)
) # multi-class classification (num_of_class == 307)
model = model.to(device)
# quit()
if True: # device == 'cuda':
model = torch.nn.DataParallel(model)
cudnn.benchmark = True
save_path = "afhq_resnet18.pth"
model.module.load_state_dict(torch.load(save_path, map_location="cuda"))
"""train dataset"""
model.eval()
with torch.no_grad():
for i in tqdm(range(0, len(source_index_train))):
# if len(saved_indices["target_index_train"]) > 500:
# break
source_inputs, source_labels = train_dataset[source_index_train[i]]
target_inputs, target_labels = train_dataset[target_index_train[i]]
source_inputs, target_inputs = source_inputs.unsqueeze(0).to(
device
), target_inputs.unsqueeze(0).to(device)
source_outputs, target_outputs = (
model(source_inputs).max(dim=1)[1].item(),
model(target_inputs).max(dim=1)[1].item(),
)
if (
source_outputs == source_labels
and target_outputs == target_labels
and source_outputs != target_outputs
and source_labels != target_labels
and source_labels == train_dataset.class_to_idx["dog"]
):
saved_indices["source_index_train"].append(source_index_train[i])
saved_indices["target_index_train"].append(target_index_train[i])
else:
pass
"""test dataset"""
model.eval()
with torch.no_grad():
for i in tqdm(range(0, len(source_index_test))):
# if len(saved_indices["target_index_test"]) > 500:
# break
source_inputs, source_labels = test_dataset[source_index_test[i]]
target_inputs, target_labels = test_dataset[target_index_test[i]]
source_inputs, target_inputs = source_inputs.unsqueeze(0).to(
device
), target_inputs.unsqueeze(0).to(device)
source_outputs, target_outputs = (
model(source_inputs).max(dim=1)[1].item(),
model(target_inputs).max(dim=1)[1].item(),
)
if (
source_outputs == source_labels
and target_outputs == target_labels
and source_outputs != target_outputs
and source_labels != target_labels
and source_labels == test_dataset.class_to_idx["dog"]
):
saved_indices["source_index_test"].append(source_index_test[i])
saved_indices["target_index_test"].append(target_index_test[i])
else:
pass
for k, v in saved_indices.items():
print(len(v))
with open("saved_indices/saved_indices_afhq_dog.pkl", "wb") as f:
pickle.dump(saved_indices, f)