forked from uclaacmai/projects-skeleton-code
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwhalecounter.py
67 lines (51 loc) · 1.72 KB
/
whalecounter.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
import os
import torch
from PIL import Image
from PIL import ImageOps
import pandas as pd
import constants
import torchvision
from pathlib import Path
import csv
data = pd.read_csv(constants.DATA + "/train.csv")
data.rename(columns=data.iloc[0]).drop(data.index[0])
images = data.iloc[:, 0]
labels = data.iloc[:, 1]
whaledict = {}
for i in range(len(labels)):
label = labels[i]
image = images[i]
if label == "new_whale":
whaledict[image] = 1
elif label in whaledict:
whaledict[label] += 1
else:
whaledict[label] = 1
anchor = {}
negative = {}
for i in range(len(labels)):
label = labels[i]
image = images[i]
if image in whaledict:
negative[image] = label
elif label in whaledict:
if whaledict[label] > 1:
anchor[image] = label
else:
negative[image] = label
anchorpath = Path(constants.DATA + "/anchorwhales.csv")
negativepath = Path(constants.DATA + "/negativewhales.csv")
with open(anchorpath, 'w', newline = '') as csvfile:
fieldnames = ['Image', 'Id', 'Augment']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
for whale in anchor:
writer.writerow({'Image' : whale, 'Id' : anchor[whale], 'Augment' : 'True'})
writer.writerow({'Image' : whale, 'Id' : anchor[whale], 'Augment' : 'False'})
with open(negativepath, 'w', newline = '') as csvfile:
fieldnames = ['Image', 'Id', 'Augment']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
for whale in negative:
writer.writerow({'Image' : whale, 'Id' : negative[whale], 'Augment' : 'True'})
writer.writerow({'Image' : whale, 'Id' : negative[whale], 'Augment' : 'False'})