-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsetup.py
151 lines (124 loc) · 4.6 KB
/
setup.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
# Setup the training and testing data.
import os
import sys
sys.path.insert(0, ".")
import numpy as np
import pandas as pd
import cv2
from matplotlib import pyplot as plt
from tqdm import tqdm
from settings import colab, num_classes, username
def setup_test_image(idx):
"""
Setup a single test image for inference.
Args:
idx (int): Index of the test image.
"""
if colab:
DIR = "/content/"
OUT = "/content/PPNet/"
else:
DIR = "/cluster/scratch/{}/PPNet/".format(username)
OUT = "/scratch/PPNet/"
train_test_split = pd.read_csv(
DIR + "CUB_200_2011/train_test_split.txt", sep=" ", header=None
)
train_test_split.columns = ["image_id", "is_train"]
train_test_split.set_index("image_id", inplace=True)
images = pd.read_csv(DIR + "CUB_200_2011/images.txt", sep=" ", header=None)
images.columns = ["image_id", "file_name"]
images.set_index("image_id", inplace=True)
bb = pd.read_csv(DIR + "CUB_200_2011/bounding_boxes.txt", sep=" ", header=None)
bb.columns = ["image_id", "x", "y", "width", "height"]
bb.set_index("image_id", inplace=True)
if not os.path.isdir(OUT):
os.mkdir(OUT)
os.mkdir(OUT + "datasets")
os.mkdir(OUT + "datasets/cub200_cropped")
os.mkdir(OUT + "datasets/cub200_cropped/train_cropped")
os.mkdir(OUT + "datasets/cub200_cropped/test_cropped")
image_id = idx
file_name = images.loc[idx, "file_name"]
is_train = train_test_split.loc[idx, "is_train"]
typ = "train" if is_train else "test"
if not os.path.isdir(
OUT + "datasets/cub200_cropped/" + typ + "_cropped/" + file_name.split("/")[0]
):
os.mkdir(
OUT
+ "datasets/cub200_cropped/"
+ typ
+ "_cropped/"
+ file_name.split("/")[0]
)
x, y, width, height = bb.loc[idx]
image = cv2.imread(DIR + "CUB_200_2011/images/" + file_name)
image = image[int(y) : int(y + height), int(x) : int(x + width)]
print("Image id: {}.\tIsTrain: {}".format(idx, is_train))
print("File name: {}".format(file_name))
out_file = OUT + "datasets/cub200_cropped/" + typ + "_cropped/" + file_name
cv2.imwrite(out_file, image)
def setup_data(num_classes=200):
"""
Setup training and testing data.
Args:
num_classes (int): Number of classes to use (max: 200).
"""
print("No. of classes used: ", num_classes)
if colab:
DIR = "/content/"
OUT = "/content/PPNet/"
else:
DIR = "/cluster/scratch/{}/PPNet/".format(username)
OUT = "/scratch/PPNet/"
train_test_split = pd.read_csv(
DIR + "CUB_200_2011/train_test_split.txt", sep=" ", header=None
)
train_test_split.columns = ["image_id", "is_train"]
train_test_split.head()
images = pd.read_csv(DIR + "CUB_200_2011/images.txt", sep=" ", header=None)
images.columns = ["image_id", "file_name"]
images.head()
bb = pd.read_csv(DIR + "CUB_200_2011/bounding_boxes.txt", sep=" ", header=None)
bb.columns = ["image_id", "x", "y", "width", "height"]
bb.head()
assert images.shape[0] == train_test_split.shape[0]
assert images.shape[0] == bb.shape[0]
if not os.path.isdir(OUT):
os.mkdir(OUT)
os.mkdir(OUT + "datasets")
os.mkdir(OUT + "datasets/cub200_cropped")
os.mkdir(OUT + "datasets/cub200_cropped/train_cropped")
os.mkdir(OUT + "datasets/cub200_cropped/test_cropped")
for i in tqdm(images.index):
image_id = images.loc[i, "image_id"]
file_name = images.loc[i, "file_name"]
if int(file_name.split(".")[0]) > num_classes:
continue
is_train = train_test_split.loc[i, "is_train"]
assert (
image_id == bb.loc[i, "image_id"]
and image_id == train_test_split.loc[i, "image_id"]
)
typ = "train" if is_train else "test"
if not os.path.isdir(
OUT
+ "datasets/cub200_cropped/"
+ typ
+ "_cropped/"
+ file_name.split("/")[0]
):
os.mkdir(
OUT
+ "datasets/cub200_cropped/"
+ typ
+ "_cropped/"
+ file_name.split("/")[0]
)
_, x, y, width, height = bb.loc[i]
image = cv2.imread(DIR + "CUB_200_2011/images/" + file_name)
image = image[int(y) : int(y + height), int(x) : int(x + width)]
out_file = OUT + "datasets/cub200_cropped/" + typ + "_cropped/" + file_name
cv2.imwrite(out_file, image)
if __name__ == "__main__":
setup_data(num_classes=num_classes)