-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprepare_data.py
More file actions
executable file
·394 lines (320 loc) · 11.3 KB
/
Copy pathprepare_data.py
File metadata and controls
executable file
·394 lines (320 loc) · 11.3 KB
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
#!/apps/anaconda3/bin/python
import io
from multiprocessing.pool import ThreadPool
import numpy as np
import os
import pandas as pd
import requests
import shutil
import glob
from tqdm import tqdm
import random
import torch
import torch.nn as nn
import torchvision
import re
from PIL import Image, ImageFile
ImageFile.LOAD_TRUNCATED_IMAGES = True
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
split = 0.9
main_dir = "images"
model_dir = "./output/03-19_12-08-23/model.bin"
def label_dicts():
"""Returns dictionaries mapping level-3 labels to levels 1 and 2, respectively."""
# Index of labels corresponds to label column in
# dataframe e.g., 'affection' is '0' in train/test
labels = [
("affection", "love", "+"),
("cheerfullness", "joy", "+"),
("confusion", "sadness", "-"),
("contentment", "joy", "+"),
("disappointment", "sadness", "-"),
("disgust", "anger", "-"),
("enthrallment", "joy", "+"),
("envy", "anger", "-"),
("exasperation", "anger", "-"),
("gratitude", "love", "+"),
("horror", "fear", "-"),
("irritabilty", "anger", "-"),
("lust", "love", "+"),
("neglect", "sadness", "-"),
("nervousness", "fear", "-"),
("optimism", "joy", "+"),
("pride", "joy", "+"),
("rage", "anger", "-"),
("relief", "joy", "+"),
("sadness", "sadness", "-"),
("shame", "sadness", "-"),
("suffering", "sadness", "-"),
("surprise", "surprise", "+"),
("sympathy", "sadness", "-"),
("zest", "joy", "+"),
]
lvl_one = {}
lvl_two = {}
lvl_three = {}
for idx, val in enumerate(labels):
lvl_one[idx] = val[2]
lvl_two[idx] = val[1]
lvl_three[idx] = val[0]
return lvl_one, lvl_two, lvl_three
def load_data():
## Load train/test WEBEmo URLs and labels and shuffle
df = pd.concat(
[
pd.read_csv(
r"~/VisualEmotion/data/WEBEmo/train25.txt", sep=" ", header=None
).rename(columns={0: "url", 1: "label"}),
pd.read_csv(
r"~/VisualEmotion/data/WEBEmo/test25.txt", sep=" ", header=None
).rename(columns={0: "url", 1: "label"}),
]
).sample(frac=1)
## split train/test
n_split = int(len(df) * split)
df["index"] = np.arange(len(df))
df["split"] = df.apply(
lambda x: "train" if x["index"] < n_split else "test", axis=1
)
## generate data dir
_, lvl_2, lvl_3 = label_dicts()
df["root_dir"] = df.apply(
lambda x: f"./data/{main_dir}/"
+ x["split"]
+ "/"
+ lvl_2[x["label"]]
+ "/"
+ lvl_3[x["label"]],
axis=1,
)
## move disgust
df.loc[df.label == 5, "root_dir"] = df.loc[df.label == 5, "root_dir"].apply(
lambda x: x.replace("anger/disgust", "disgust")
)
## mkdirs
dirs = df["root_dir"].unique()
for d in dirs:
if not os.path.exists(d):
os.makedirs(d)
return df
def save_imgs(df):
"""Retrieves an image from its URL and saves locally.
Args:
df (dataframe): Pandas dataframe containing image
URLs and associated labels.
Returns:
None. Image is saved locally.
"""
response = requests.get(df[1]["url"])
img = Image.open(io.BytesIO(response.content))
img_name = df[1]["url"].split("/")[-1]
label = str(df[1]["label"])
path = os.path.join(df[1]["root_dir"], label + "_" + img_name)
img.save(path)
return
def pool_image_retrieval(df):
"""Utilizes multithreading to call `save_imgs()` function.
Args:
df (dataframe): Pandas dataframe containing image
URLs and associated labels.
Returns:
None. Images are saved locally.
"""
pool = ThreadPool(os.cpu_count())
pool.map(func=save_imgs, iterable=df.iterrows())
return
def get_WEBEmo():
df = load_data()
pool_image_retrieval(df)
def get_UnbiasedEmo():
for path in glob.glob("./data/UnBiasedEmo/images/*"):
klass = path.split("/")[-1]
imgs = glob.glob(path + "/**/*.jpg", recursive=True)
# imgs = glob.glob(path+'/*/*.jpg')
random.shuffle(imgs)
n = len(imgs)
n_split = int(n * split)
print(f"class: {klass}, all: {n}, train: {n_split}, test: {n-n_split}")
for i, img in enumerate(tqdm(imgs)):
if i < n_split:
tgt = img.replace("UnBiasedEmo/images", f"{main_dir}/train")
else:
tgt = img.replace("UnBiasedEmo/images", f"{main_dir}/test")
if not os.path.exists("/".join(tgt.split("/")[:-1])):
os.makedirs("/".join(tgt.split("/")[:-1]))
# print(img, tgt)
if os.path.exists(img):
shutil.move(img, tgt)
def get_Emotion6():
if os.path.exists("./data/Emotion-6/images/anger/digust"): # typo in their dataset
shutil.move(
"./data/Emotion-6/images/anger/digust", "./data/Emotion-6/images/disgust"
)
for path in glob.glob("./data/Emotion-6/images/*"):
klass = path.split("/")[-1]
imgs = glob.glob(path + "/**/*.jpg", recursive=True)
random.shuffle(imgs)
n = len(imgs)
n_split = int(n * split)
print(f"class: {klass}, all: {n}, train: {n_split}, test: {n-n_split}")
for i, img in enumerate(tqdm(imgs)):
if i < n_split:
tgt = img.replace("Emotion-6/images", f"{main_dir}/train")
else:
tgt = img.replace("Emotion-6/images", f"{main_dir}/test")
if not os.path.exists("/".join(tgt.split("/")[:-1])):
os.makedirs("/".join(tgt.split("/")[:-1]))
# print(img, tgt)
if os.path.exists(img):
shutil.move(img, tgt)
def load_state_dict_unsafe(model, state_dict):
"""
Load state dict to provided model while ignore exceptions.
"""
missing_keys = []
unexpected_keys = []
error_msgs = []
# copy state_dict so _load_from_state_dict can modify it
metadata = getattr(state_dict, "_metadata", None)
state_dict = state_dict.copy()
if metadata is not None:
state_dict._metadata = metadata
def load(module, prefix=""):
local_metadata = {} if metadata is None else metadata.get(prefix[:-1], {})
module._load_from_state_dict(
state_dict,
prefix,
local_metadata,
True,
missing_keys,
unexpected_keys,
error_msgs,
)
for name, child in module._modules.items():
if child is not None:
load(child, prefix + name + ".")
load(model)
load = None # break load->load reference cycle
return {
"unexpected_keys": unexpected_keys,
"missing_keys": missing_keys,
"error_msgs": error_msgs,
}
def inference(model, imgF, path=f"./data/{main_dir}"):
model.eval()
# dataloader
transform_test = torchvision.transforms.Compose(
[
torchvision.transforms.Resize(256),
torchvision.transforms.CenterCrop(224),
torchvision.transforms.ToTensor(),
torchvision.transforms.Normalize(
mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]
),
]
)
preds = [] # Logits: [N * 6]
for name in ["train", "test"]:
loader = imgF(path + f"/{name}/love", transform=transform_test)
loader = torch.utils.data.DataLoader(
loader, batch_size=256, drop_last=False, num_workers=0
)
indices = [it[0] for it in loader.dataset.imgs]
t = []
for feat, _ in tqdm(loader, desc=name, total=len(loader)):
feat = feat.to(device)
with torch.no_grad():
pred = model(feat).softmax(dim=1)
t.append(
pd.DataFrame(
pred.cpu().numpy(),
columns=["anger", "disgust", "fear", "joy", "sadness", "surprise"],
)
)
t = pd.concat(t, axis=0)
t.index = indices
preds.append(t)
preds = pd.concat(preds, axis=0)
return preds
def relabel_love(init_state=model_dir):
# model
model_name = "resnet50"
model = eval("torchvision.models." + model_name)(pretrained=False)
model.fc = nn.Linear(model.fc.in_features, 6)
res = load_state_dict_unsafe(model, torch.load(init_state, map_location="cpu"))
model.to(device)
print(res)
pred_love = inference(model, torchvision.datasets.ImageFolder)
joy = pred_love[pred_love.idxmax(axis=1) == "joy"]
neutral = pred_love[~pred_love.index.isin(joy.index)]
neutral = ((neutral - 1 / 6) ** 2).sum(axis=1).sort_values()
topn = len(neutral[neutral < neutral.quantile(0.3)])
neutral = neutral.head(topn)
return neutral, joy
def move_relabel(move=False, move_list=""):
if move_list and move:
move_list = pd.read_csv(move_list, index_col=0)
for _, (src, tgt) in tqdm(move_list.iterrows()):
if os.path.exists(src):
shutil.move(src, tgt)
return
neutral, joy = relabel_love()
neutral = neutral.index.tolist()
joy = joy.index.tolist()
move_list = []
# move to neutral
for name in ["train", "test"]:
if not os.path.exists(f"./data/{main_dir}/{name}/neutral"):
os.makedirs(f"./data/{main_dir}/{name}/neutral")
for i, img in enumerate(tqdm(neutral)):
split = re.search(f"{main_dir}/([a-zA-Z]+)/love", img)[0].split("/")[1]
src = img
if split == "train":
tgt = img.replace("train/love", "train/neutral/love")
elif split == "test":
tgt = img.replace("test/love", "test/neutral/love")
if not os.path.exists("/".join(tgt.split("/")[:-1])):
os.makedirs("/".join(tgt.split("/")[:-1]))
move_list.append((src, tgt))
if os.path.exists(src) and move:
shutil.move(src, tgt)
# move to joy
for i, img in enumerate(tqdm(joy)):
split = re.search(f"{main_dir}/([a-zA-Z]+)/love", img)[0].split("/")[1]
src = img
if split == "train":
tgt = img.replace("train/love", "train/joy/love")
elif split == "test":
tgt = img.replace("test/love", "test/joy/love")
if not os.path.exists("/".join(tgt.split("/")[:-1])):
os.makedirs("/".join(tgt.split("/")[:-1]))
move_list.append((src, tgt))
if os.path.exists(src) and move:
shutil.move(src, tgt)
move_list = pd.DataFrame(move_list, columns=["src", "tgt"])
model_name = model_dir.split("/")[-2]
move_list.to_csv(f"./data/moveList_{model_name}.csv")
def show_image():
from IPython.display import display, Image
model_name = model_dir.split("/")[-2]
move_list = pd.read_csv(f"./data/moveList_{model_name}.csv", index_col=0)
move_list = move_list.sample(frac=1)
cnt = 0
for _, (src, tgt) in tqdm(move_list.iterrows()):
if cnt > 8:
break
klass = tgt.split("/")[4]
if klass == "neutral":
cnt += 1
display(Image(filename=src))
if __name__ == "__main__":
random.seed(42)
np.random.seed(42)
## WEBEmo
get_WEBEmo()
## UnbiasedEmo
get_UnbiasedEmo()
## Emotion-6
get_Emotion6()
## Inference on Love, relabel and move
move_relabel(move=True)