-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
412 lines (333 loc) · 13 KB
/
utils.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
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
from numpy import average
import tensorflow as tf
from tensorflow.keras import layers
import os
import shutil
import glob
import numpy as np
import gc
import categorization as cat
from scipy.spatial.distance import pdist, squareform
import pandas as pd
from nltk.corpus import wordnet as wn
def make_output_model(model, average_pooling=True, flatten=True):
"""Return a model that has outputs at every convolution and dense layer."""
outputs = []
for layer in model.layers:
if isinstance(layer, layers.Conv2D):
dataFormat = layer.data_format
if average_pooling:
layer = layers.GlobalAvgPool2D(data_format=dataFormat)(layer.output)
if flatten:
if average_pooling:
layer = layers.Flatten(data_format=dataFormat)(layer)
else:
layer = layers.Flatten(data_format=dataFormat)(layer.output)
if average_pooling or flatten:
outputs.append(layer)
else:
outputs.append(layer.output)
elif isinstance(layer, layers.Dense):
outputs.append(layer.output)
return tf.keras.Model(model.input, outputs)
def add_CUB200_data(source_dir, target_dir):
"""
Add CUB200 data from the source_dir to the target_dir, empties bird
directory in target_dir and copies images for the source_dir into the train
val directories in target_dir according to the train_test_split file.
"""
# Empty the bird directory in target_dir
birdDir = "0085_bird"
train_dir = os.path.join(target_dir, "train", birdDir)
val_dir = os.path.join(target_dir, "val", birdDir)
test_dir = os.path.join(target_dir, "test", birdDir)
for dir in [train_dir, val_dir, test_dir]:
for file in os.listdir(dir):
# Recursively remove
if os.path.isdir(os.path.join(dir, file)):
shutil.rmtree(os.path.join(dir, file))
else:
os.remove(os.path.join(dir, file))
# Load info files in source_dir
with open(os.path.join(source_dir, "train_test_split.txt"), "r") as f:
splitInfo = f.readlines()
with open(os.path.join(source_dir, "images.txt"), "r") as f:
images = f.readlines()
# Loop through images
for img, split in zip(images, splitInfo):
img = img.split(" ")[1].strip()
split = split.split(" ")[1].strip()
# Check if intermediate directories exist
trainDir = os.path.join(target_dir, "train", birdDir, img.split("/")[0])
valDir = os.path.join(target_dir, "val", birdDir, img.split("/")[0])
testDir = os.path.join(target_dir, "test", birdDir, img.split("/")[0])
if not os.path.exists(trainDir):
os.makedirs(trainDir)
if not os.path.exists(valDir):
os.makedirs(valDir)
if not os.path.exists(testDir):
os.makedirs(testDir)
# Copy image to correct directory
if split == "1":
src = os.path.join(source_dir, "images", img)
dst = os.path.join(train_dir, img)
shutil.copy(src, dst)
elif split == "0":
src = os.path.join(source_dir, "images", img)
valDst = os.path.join(val_dir, img)
testDst = os.path.join(test_dir, img)
shutil.copy(src, valDst)
shutil.copy(src, testDst)
return None
def split_CUB200_data(source_dir, target_dir):
"""
Split CUB200 data from the source_dir into the train and val directories in
target_dir according to the split file.
"""
# Load info files in source_dir
with open(os.path.join(source_dir, "train_test_split.txt"), "r") as f:
splitInfo = f.readlines()
with open(os.path.join(source_dir, "images.txt"), "r") as f:
images = f.readlines()
# Check if target_dir exists, make it if not
if not os.path.exists(target_dir):
os.makedirs(target_dir)
# Loop through images
trainImgCount = 0
valImgCount = 0
for img, split in zip(images, splitInfo):
img = img.split(" ")[1].strip()
split = split.split(" ")[1].strip()
trainVal = "train" if split == "1" else "val"
if trainVal == "train":
trainImgCount += 1
else:
valImgCount += 1
# Check if directory exists
className = img.split("/")[0].strip()
targetDir = os.path.join(target_dir, trainVal, f"CUB_{className}")
if not os.path.exists(targetDir):
os.makedirs(targetDir)
# Copy image to correct directory
src = os.path.join(source_dir, "images", img)
dst = os.path.join(targetDir, img.split("/")[-1])
shutil.copy(src, dst)
# Count how many categories there are
categories = os.listdir(os.path.join(target_dir, "train"))
nCategories = len(categories)
# Print info
print(f"Found {trainImgCount} training images.")
print(f"Found {valImgCount} validation images.")
print(f"Split across {nCategories} categories.")
return None
def get_reps_over_training(modelDir: str, layer: str, images: np.ndarray):
"""
Return representation from layer of the images over training from modelDir.
We expect modelDir to have saved models over epochs.
"""
# Get model files
modelFiles = glob.glob(os.path.join(modelDir, "*.hdf5"))
modelFiles.sort()
# Load one model to figure out size
model = tf.keras.models.load_model(modelFiles[0])
x = model.get_layer(layer).output
# Preallocate shape of representations
reps = np.zeros([len(modelFiles), images.shape[0]] + list(x.shape[1:]))
# Loop through models
for i, modelFile in enumerate(modelFiles):
# Cleanup so we don't run out of GPU memory
del model
tf.keras.backend.clear_session()
gc.collect()
# Load model
model = tf.keras.models.load_model(modelFile)
# Get output at target layer
x = model.get_layer(layer).output
# Compile model
model = tf.keras.models.Model(inputs=model.input, outputs=x)
# Get representations
reps[i] = model.predict(images)
return reps
def compute_sims_over_training(
modelDir: str, layer: str, images: np.ndarray, custom_objects: dict = None
):
"""
Return a similarity matrix over training from modelDir at layer using images.
"""
# Get model files
modelFiles = glob.glob(os.path.join(modelDir, "*.hdf5"))
modelFiles.sort()
# Preallocate similarity matrix
simMat = np.zeros([len(modelFiles), images.shape[0], images.shape[0]])
# Loop through models
for i, modelFile in enumerate(modelFiles):
print(f"Processing file: {modelFile}")
# Load model
model = tf.keras.models.load_model(modelFile, custom_objects=custom_objects)
# Get output at target layer
x = model.get_layer(layer).output
# Compile model
model = tf.keras.models.Model(inputs=model.input, outputs=x)
# Get representations
reps = model.predict(images)
# Flatten reps
reps = reps.reshape(reps.shape[0], -1)
# Calculate similarity matrix using GCM (note that we're using the parameters r=2, c=1, p=1)
simMat[i] = np.exp(
-1
* squareform(pdist(reps, metric="euclidean"))
* ((1 / reps.shape[1]) ** (1 / 2))
)
# Cleanup so we don't run out of GPU memory
del model
tf.keras.backend.clear_session()
gc.collect()
return simMat
def get_image_info(directory):
"""
Recursively go through the directory and build a dataframe with the
information about the images.
"""
# Create dataframe
df = pd.DataFrame(columns=["path", "name", "super", "basic", "sub", "set"])
# Loop through train directory first
for root, dirs, files in os.walk(os.path.join(directory, "train")):
# Loop through files
for file in files:
# Ignore dstore
if file == ".DS_Store":
continue
# Add to dataframe
df = pd.concat(
[
df,
pd.DataFrame(
{
"path": [os.path.join(root, file)],
"name": [file],
"super": [root.split("/")[-3]],
"basic": [root.split("/")[-2]],
"sub": [root.split("/")[-1]],
"set": ["train"],
}
),
],
ignore_index=True,
)
# Loop through test directory
for root, dirs, files in os.walk(os.path.join(directory, "test")):
# Loop through files
for file in files:
# Ignore dstore
if file == ".DS_Store":
continue
# Add to dataframe
df = pd.concat(
[
df,
pd.DataFrame(
{
"path": [os.path.join(root, file)],
"name": [file],
"super": [root.split("/")[-3]],
"basic": [root.split("/")[-2]],
"sub": [root.split("/")[-1]],
"set": ["test"],
}
),
],
ignore_index=True,
)
return df
def get_category_nodes(data_dir, img_info):
"""
Return a dictionary of indices for all levels of the hierarchy derived from
img_info by looking in data_dir.
"""
category_nodes = {}
# List folders in data_dir
cats = os.listdir(data_dir)
cats.sort()
# Start at the basic level for imgInfo
basicCats = img_info["basic"].unique()
# For each basic cats, find the node
for cat in basicCats:
nodes = [i for i, folder in enumerate(cats) if cat in folder]
if len(nodes) == 0:
raise ValueError(f"Could not find node for {cat}")
elif len(nodes) > 1:
print(f"Found multiple nodes for {cat}")
print(f"Nodes: {nodes}")
print(f"Labels: {[cats[i] for i in nodes]}")
choice = input("Which one to use (1, 2, ... n) ? ")
category_nodes[cat] = nodes[int(choice) - 1]
else:
category_nodes[cat] = nodes[0]
# Get all the names of the categories
synsets = []
for cat in cats:
tmp = wn.synsets(cat.split("_")[-1], pos=wn.NOUN)
if len(tmp) > 0:
synsetList = sum(tmp[0].hypernym_paths(), [])
synsets.append(synsetList)
else:
synsets.append(None)
print("Could not find synset for ", cat)
# For each super cat, use wordnet hierarchy to figure out valid nodes
superCats = img_info["super"].unique()
for cat in superCats:
# Get the synset for the super category
synset = wn.synsets(cat, pos=wn.NOUN)[0]
superNodes = []
for i, synsetList in enumerate(synsets):
if synsetList is None:
continue
if synset in synsetList:
superNodes.append(i)
category_nodes[cat] = superNodes
# For each subordinate cat, look in the basic cat directory to find node
subCats = img_info["sub"].unique()
for cat in subCats:
basicCat = img_info.loc[img_info["sub"] == cat, "basic"].unique()[0]
basicNode = category_nodes[basicCat]
subNodes = os.listdir(os.path.join(data_dir, cats[basicNode]))
subNodes.sort()
category_nodes[cat] = subNodes.index(cat)
return category_nodes
def find_nested_synsets(targetSynset, synsets):
"""
Return the index and synsets from the list synsets that is nested under
the targetSynset
"""
nestedIdxs, nestedSynsets = [], []
for i, synset in enumerate(synsets):
lowest_common_hypernyms = synset.lowest_common_hypernyms(targetSynset)
if targetSynset in lowest_common_hypernyms:
nestedIdxs.append(i)
nestedSynsets.append(synset)
return nestedIdxs, nestedSynsets
def extract_training_data(directory):
"""
Return a dataframe of training trajectory information for each model in
directory.
"""
# List folders in directory
folders = os.listdir(directory)
# Check the first folder to get the dataframe spec
files = os.listdir(os.path.join(directory, folders[0]))
csv = [file for file in files if file.endswith(".csv")][0]
df = pd.read_csv(os.path.join(directory, folders[0], csv))
# Loop through the rest of the folders and concatenate
for folder in folders[1:]:
files = os.listdir(os.path.join(directory, folder))
csv = [file for file in files if file.endswith(".csv")][0]
df = pd.concat(
[
df,
pd.read_csv(os.path.join(directory, folder, csv)),
],
ignore_index=True,
)
return df, csv.split(".csv")[0].split("-")[1:]
if __name__ == "__main__":
extract_training_data("./models/deepCats/AlexNet/twoHotFreezeBasic/")