-
Notifications
You must be signed in to change notification settings - Fork 1
/
CAX Identifying Superheroes _Preprocessing.py
336 lines (228 loc) · 10.1 KB
/
CAX Identifying Superheroes _Preprocessing.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
########################################################################################################################
# This code was used to preprocess the training files. The training files were cleaned
# This file was screated by downloading a jupyter notebook from my paperspace account
########################################################################################################################
# In[1]:
get_ipython().system('cp -r data_CAX/train/CAX_Superhero_Train data_CAX/train_xtra')
# In[2]:
import os
PATH = f'data_CAX/train_xtra/'
#Capture the classes in the train directory
trainClasses = os.listdir(f'{PATH}CAX_Superhero_Train')
try:
os.remove(f'{PATH}CAX_Superhero_Train' + '/.DS_Store')
except:
print("ERR: No DS_Store")
trainClasses = os.listdir(f'{PATH}CAX_Superhero_Train')
# In[3]:
trainClasses
# In[4]:
## Clean up the training examples
# Clean Ant-Man
allFiles = os.listdir(PATH + 'CAX_Superhero_Train/' + 'Ant-Man/')
file = [file for file in allFiles if 'cax_antman_train33' in file][0]
source = PATH + 'CAX_Superhero_Train/' + 'Ant-Man/' + file
destination = PATH + 'CAX_Superhero_Train/' + 'Batman/' + file
try:
os.rename(source, destination)
except:
print("ERROR: Moving file " + source)
# In[5]:
# Clean Aquaman
delFiles = ['cax_aquaman_train262', 'cax_aquaman_train329', 'cax_aquaman_train346', 'cax_aquaman_train381', 'cax_aquaman_train378',
'cax_aquaman_train392', 'cax_aquaman_train422', 'cax_aquaman_train433', 'cax_aquaman_train379']
allFiles = os.listdir(PATH + 'CAX_Superhero_Train/' + 'Aquaman/')
for file in delFiles:
filename = [file2 for file2 in allFiles if file in file2][0]
source = PATH + 'CAX_Superhero_Train/' + 'Aquaman/' + filename
try:
os.remove(source)
except:
print("ERROR: Deleting file" + source)
filename = [file2 for file2 in allFiles if 'cax_aquaman_train358' in file2][0]
source = PATH + 'CAX_Superhero_Train/' + 'Aquaman/' + filename
destination = PATH + 'CAX_Superhero_Train/' + 'Batman/' + filename
try:
os.rename(source, destination)
except:
print("ERROR: Moving file " + source)
# In[6]:
# Clean Avengers
allFiles = os.listdir(PATH + 'CAX_Superhero_Train/' + 'Avengers/')
filename = [file2 for file2 in allFiles if 'cax_avengers_train563' in file2][0]
source = PATH + 'CAX_Superhero_Train/' + 'Avengers/' + filename
destination = PATH + 'CAX_Superhero_Train/' + 'Captain America/' + filename
try:
os.rename(source, destination)
except:
print("ERROR: Moving file " + source)
# In[7]:
# Clean Black Panther ::: For some reason this does not work
allFiles = os.listdir(PATH + 'CAX_Superhero_Train/' + 'Black Panther/')
movFiles = ['cax_blackpanther_train1586', 'cax_blackpanther_train1651']
for file in movFiles:
filename = [file2 for file2 in allFiles if file in file2][0]
source = PATH + 'CAX_Superhero_Train/' + 'Black Panther/' + filename
destination = PATH + 'CAX_Superhero_Train/' + 'Avengers/' + filename
try:
os.rename(source, destination)
except:
print("ERROR: Moving file " + source)
# In[8]:
# Clean Captain America
allFiles = os.listdir(PATH + 'CAX_Superhero_Train/' + 'Captain America/')
filename = [file2 for file2 in allFiles if 'cax_capamerica_train2122' in file2][0]
source = PATH + 'CAX_Superhero_Train/' + 'Captain America/' + filename
try:
os.remove(source)
except:
print("ERROR: Deleting file " + source)
movFiles = ['cax_capamerica_train2263', 'cax_capamerica_train2247', 'cax_capamerica_train2272', 'cax_capamerica_train2268',
'cax_capamerica_train2288']
for file in movFiles:
filename = [file2 for file2 in allFiles if file in file2]
if len(filename) > 0:
source = PATH + 'CAX_Superhero_Train/' + 'Captain America/' + filename[0]
destination = PATH + 'CAX_Superhero_Train/' + 'Avengers/' + filename[0]
try:
os.rename(source, destination)
except:
print("ERROR: Moving file " + source)
# In[9]:
# Clean Ghost Rider
allFiles = os.listdir(PATH + 'CAX_Superhero_Train/' + 'Ghost Rider/')
delFiles = ['cax_ghostrider_train2560', 'cax_ghostrider_train2662']
for file in delFiles:
filename = [file2 for file2 in allFiles if file in file2][0]
source = PATH + 'CAX_Superhero_Train/' + 'Ghost Rider/' + filename
try:
os.remove(source)
except:
print("ERROR: Deleting file " + source)
movFiles = ['cax_ghostrider_train2526', 'cax_ghostrider_train2573']
for file in movFiles:
filename = [file2 for file2 in allFiles if file in file2][0]
source = PATH + 'CAX_Superhero_Train/' + 'Ghost Rider/' + filename
destination = PATH + 'CAX_Superhero_Train/' + 'Avengers/' + filename
try:
os.rename(source, destination)
except:
print("ERROR: Moving file " + source)
filename = [file2 for file2 in allFiles if 'cax_ghostrider_train2668' in file2][0]
source = PATH + 'CAX_Superhero_Train/' + 'Ghost Rider/' + filename
destination = PATH + 'CAX_Superhero_Train/' + 'Spiderman/' + filename
try:
os.rename(source, destination)
except:
print("ERROR: Moving file " + source)
# In[10]:
# Clean Hulk
# delete files
allFiles = os.listdir(PATH + 'CAX_Superhero_Train/' + 'Hulk/')
filename = [file2 for file2 in allFiles if 'cax_hulk_train3031' in file2][0]
source = PATH + 'CAX_Superhero_Train/' + 'Hulk/' + filename
try:
os.remove(source)
except:
print("ERROR: Deleting file " + source)
#move files
movFiles = ['cax_hulk_train2818', 'cax_hulk_train2851', 'cax_hulk_train2848', 'cax_hulk_train2883', 'cax_hulk_train2904',
'cax_hulk_train2899', 'cax_hulk_train3035', 'cax_hulk_train3073', 'cax_hulk_train3111']
for file in movFiles:
filename = [file2 for file2 in allFiles if file in file2][0]
source = PATH + 'CAX_Superhero_Train/' + 'Hulk/' + filename
destination = PATH + 'CAX_Superhero_Train/' + 'Avengers/' + filename
try:
os.rename(source, destination)
except:
print("ERROR: Moving file " + source)
# In[11]:
allFiles = os.listdir(PATH + 'CAX_Superhero_Train/' + 'Iron Man/')
filename = [file2 for file2 in allFiles if 'cax_ironman_train3721' in file2][0]
source = PATH + 'CAX_Superhero_Train/' + 'Iron Man/' + filename
destination = PATH + 'CAX_Superhero_Train/' + 'Hulk/' + filename
try:
os.rename(source, destination)
except:
print("ERROR: Moving file " + source)
filename = [file2 for file2 in allFiles if 'cax_ironman_train3763' in file2][0]
source = PATH + 'CAX_Superhero_Train/' + 'Iron Man/' + filename
try:
os.remove(source)
except:
print("ERROR: Deleting file " + source)
movFiles = ['cax_ironman_train3342', 'cax_ironman_train3358', 'cax_ironman_train3357', 'cax_ironman_train3361',
'cax_ironman_train3370', 'cax_ironman_train3395', 'cax_ironman_train3478', 'cax_ironman_train3488',
'cax_ironman_train3485', 'cax_ironman_train3502', 'cax_ironman_train3512', 'cax_ironman_train3540',
'cax_ironman_train3591', 'cax_ironman_train3597', 'cax_ironman_train3610', 'cax_ironman_train3633',
'cax_ironman_train3646', 'cax_ironman_train3703', 'cax_ironman_train3738', 'cax_ironman_train3735',
'cax_ironman_train3738', 'cax_ironman_train3741', 'cax_ironman_train3747', 'cax_ironman_train3751',
'cax_ironman_train3752', 'cax_ironman_train3764', 'cax_ironman_train3785', 'cax_ironman_train3793',
'cax_ironman_train3810', 'cax_ironman_train3815']
for file in movFiles:
filename = [file2 for file2 in allFiles if file in file2][0]
source = PATH + 'CAX_Superhero_Train/' + 'Iron Man/' + filename
destination = PATH + 'CAX_Superhero_Train/' + 'Avengers/' + filename
try:
os.rename(source, destination)
except:
print("ERROR: Moving file " + source)
# In[12]:
# Clean spider4man
allFiles = os.listdir(PATH + 'CAX_Superhero_Train/' + 'Spiderman/')
movFiles = ['cax_spiderman_train4664', 'cax_spiderman_train4663']
for file in movFiles:
filename = [file2 for file2 in allFiles if file in file2][0]
source = PATH + 'CAX_Superhero_Train/' + 'Spiderman/' + filename
destination = PATH + 'CAX_Superhero_Train/' + 'Avengers/' + filename
try:
os.rename(source, destination)
except:
print("ERROR: Moving file " + source)
# In[13]:
# Clean superman
allFiles = os.listdir(PATH + 'CAX_Superhero_Train/' + 'Superman/')
movFiles =['cax_superman_train4725', 'cax_superman_train4809']
for file in movFiles:
filename = [file2 for file2 in allFiles if file in file2][0]
source = PATH + 'CAX_Superhero_Train/' + 'Superman/' + filename
destination = PATH + 'CAX_Superhero_Train/' + 'Batman/' + filename
try:
os.rename(source, destination)
except:
print("ERROR: Moving file " + source)
filename = [file2 for file2 in allFiles if 'cax_superman_train5149' in file2][0]
source = PATH + 'CAX_Superhero_Train/' + 'Superman/' + filename
try:
os.remove(source)
except:
print("ERROR: Deleting file " + source)
# In[14]:
import numpy as np
# Print out the number of samples for each category
for classes in trainClasses:
source = PATH + 'CAX_Superhero_Train/' + classes + "/"
try:
os.remove(source+".DS_Store")
except:
print("Error: No DS_Store found in category: "+ classes)
files = os.listdir(source)
print("Superhero: " + classes + " : Sample Size = " + str(len(files)))
# Create directory in Validation folder
directory = PATH + 'valid/' + classes
if not os.path.exists(directory):
os.makedirs(directory)
# Select 40 random samples
index = np.random.choice(range(len(files)), int(len(files)*0.2) ,replace=False)
for idx in index:
print(files[idx])
source = PATH + 'CAX_Superhero_Train/' + classes + "/" + files[idx]
destination = PATH + 'valid/' + classes + "/" + files[idx]
try:
os.rename(source, destination)
except:
print("ERROR: Moving file " + files[idx])
# In[17]:
get_ipython().system('mv data_CAX/train_xtra/valid data_CAX/valid_xtra')
# In[18]:
get_ipython().system('rm -rf data_CAX/train_xtra/CAX_Superhero_Train')