Skip to content

Commit 1684f6c

Browse files
author
AlexDerhacobian
committed
created db
1 parent c26b3c2 commit 1684f6c

File tree

6 files changed

+33
-288
lines changed

6 files changed

+33
-288
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
ezmode/__pycache__/*
2+
*.egg-info

ezmode/data.py

-96
Original file line numberDiff line numberDiff line change
@@ -153,102 +153,6 @@ def get_user_actions(self, start_idx, end_idx, with_labels = True):
153153

154154
return data
155155

156-
'''
157-
Returns mapping from class index to corresponding importance weight
158-
'''
159-
def get_imp_weights(self,
160-
rare_class,
161-
binary = True,
162-
exp_factor = 1):
163-
164-
self.get_class_dict(binary = binary, rare_class = rare_class)
165-
weights = {}
166-
167-
if (binary):
168-
num_exs_pos = len(self.train_df[self.train_df['label'] == 1])
169-
cls_weight_pos = (1 / num_exs_pos) ** exp_factor
170-
weights[1] = cls_weight_pos
171-
172-
num_exs_neg = len(self.train_df[self.train_df['label'] == 0])
173-
cls_weight_neg = (1 / num_exs_neg) ** exp_factor
174-
weights[0] = cls_weight_neg
175-
176-
177-
'''
178-
TODO ELSE NON-BINARY CASE
179-
'''
180-
181-
total_weights = np.sum([weights[cls] for cls in self.train_df['label']])
182-
for cls_idx in weights.keys():
183-
weights[cls_idx] /= total_weights
184-
185-
return weights
186-
187-
188-
'''
189-
Return train data as df, if out is a path, write data to path
190-
'''
191-
def get_early_stopping_val_data(self,
192-
root = True,
193-
zero_index = True,
194-
shuffle = True,
195-
out = None,
196-
binary = False,
197-
rare_class = None):
198-
199-
val_data = []
200-
with self.engine.connect() as con:
201-
rs = con.execute(
202-
'SELECT annotations.image_base_path, '
203-
'annotations.xmin, '
204-
'annotations.ymin, '
205-
'annotations.xmax, '
206-
'annotations.ymax, '
207-
'annotations.label '
208-
'FROM annotations '
209-
'INNER JOIN labels ON annotations.label=labels.label '
210-
'WHERE annotations.split=\'val_es\'')
211-
for row in rs:
212-
image_base_path = str(row[0])
213-
214-
xmin = int(row[1])
215-
ymin = int(row[2])
216-
xmax = int(row[3])
217-
ymax = int(row[4])
218-
219-
label = int(row[5])
220-
221-
if (root):
222-
image_base_path = os.path.join(self.root, image_base_path)
223-
224-
val_data.append([image_base_path, xmin, ymin, xmax, ymax, label])
225-
226-
val_df = pd.DataFrame(data = val_data, columns = ['image', 'xmin', 'ymin', 'xmax', 'ymax', 'label'])
227-
228-
#Shuffle training data
229-
if (shuffle):
230-
231-
print("shuffling")
232-
val_df = val_df.sample(frac = 1).reset_index(drop = True)
233-
234-
#Zero index the data labels for binary classification tasks
235-
if (binary and rare_class != None):
236-
237-
binary_labels = []
238-
239-
for label in val_df['label'].to_numpy():
240-
if (label == rare_class):
241-
binary_labels.append(1)
242-
else:
243-
binary_labels.append(0)
244-
245-
val_df['label'] = binary_labels
246-
247-
#Zero index the data labels for N-way classification tasks
248-
if (out != None):
249-
val_df.to_csv(out)
250-
251-
return val_df
252156

253157
'''
254158
Return train data as df, if out is a path, write data to path

ezmode/infer.py

+6-18
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import subprocess
12
import ezmode
23
import pandas as pd
34
from ezmode import data
@@ -13,25 +14,16 @@ def __init__(self,
1314
self.model = model
1415
self.gpus = gpus
1516

16-
def write_bash_scripts(self, script = None):
17+
def deploy(self, script = None):
1718
self.dataloader.init_metadata()
1819
val_csv = self.dataloader.get_val_data()
1920
val_data = pd.read_csv(val_csv)
2021

2122
videos = val_data['vid_base_path'].tolist()
2223
num_videos = len(videos)
2324

24-
if (script == None):
25-
infer_script_fname = os.path.join('/'.join(ezmode.__file__.split('/')[:-1]), 'infer_script.py')
26-
else:
27-
infer_script_fname = script
28-
29-
#assert(os.path.exists(infer_script_fname))
30-
31-
bash_dest = os.path.join(self.dataloader.working_dir, 'run_infer.sh')
32-
3325
processes = []
34-
26+
3527
videos_left = num_videos
3628

3729
for gpu in range(self.gpus):
@@ -56,11 +48,7 @@ def write_bash_scripts(self, script = None):
5648
f'--num_videos {num_videos} '
5749
f'--to_process {to_process} \n')
5850

51+
infer_job = [subprocess.Popen(process, shell = True) for process in processes]
52+
for infer_job in infer_jobs:
53+
infer_job.wait()
5954

60-
61-
if (os.path.exists(bash_dest)):
62-
os.remove(bash_dest)
63-
64-
f = open(bash_dest, "a")
65-
f.writelines(processes)
66-
f.close()

ezmode/select.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
num_labeled = 0
99
true_pos = 0
1010

11-
class Selector:
11+
class SelectEngine:
1212
def __init__(self,
1313
dataloader,
1414
rare_class,

ezmode/train.py

+25-61
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@
1313
from torchvision import transforms
1414
from torchvision.models.detection.faster_rcnn import FastRCNNPredictor
1515
from .data import DataLoader
16+
from .utils import FRCNNDataLoader
1617

17-
class Trainer:
18+
class TrainEngine:
1819
def __init__(self,
1920
model_backbone,
2021
dataloader,
@@ -29,61 +30,26 @@ def __init__(self,
2930
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
3031
])
3132

32-
def acc(self, labels, pred):
33-
34-
acc = (labels == pred.argmax(-1)).float().mean().item()
35-
return acc
36-
37-
def run(self, model, lr, train_data, nb_epochs):
33+
def run(self, model, loader, lr, nb_epochs):
34+
model.train()
3835
optimizer = torch.optim.SGD(
3936
filter(lambda p: p.requires_grad, model.parameters()),
40-
lr,
41-
momentum=0.9,
42-
weight_decay=1e-4
43-
)
44-
45-
for epoch in range(nb_epochs):
46-
print("Training on Epoch={}".format(epoch+1))
47-
for i in tqdm.tqdm(range(len(train_data))):
48-
cur_row = train_data.iloc[i]
49-
img_path = cur_row['image']
50-
print(img_path)
51-
assert(os.path.exists(img_path))
52-
im = cv2.imread(img_path)
53-
inp = cv2.cvtColor(im, cv2.COLOR_BGR2RGB)
54-
inp = self.transform(inp)
55-
inp = inp.unsqueeze(0)
56-
57-
boxes = torch.tensor([[cur_row['xmin'], cur_row['ymin'], cur_row['xmax'], cur_row['ymax']]]).type(torch.FloatTensor).cuda()
58-
labels = torch.tensor([cur_row['label']], dtype=torch.int64).cuda()
59-
image_id = torch.tensor([1], dtype=torch.int64).cuda()
60-
61-
bbox_area = (cur_row['xmax'] - cur_row['xmin']) * (cur_row['ymax'] - cur_row['ymin'])
62-
bbox_area = torch.tensor(bbox_area).cuda()
63-
64-
iscrowd = torch.zeros((1,), dtype=torch.int64).cuda()
65-
66-
67-
target = {
68-
'boxes': boxes,
69-
'labels': labels,
70-
'image_id': image_id,
71-
'area': bbox_area,
72-
'iscrowd': iscrowd
73-
}
74-
inp = inp.cuda()
75-
model.cuda()
76-
outp, detections = model(inp, [target])
77-
loss_classifier = outp['loss_classifier']
78-
loss_box_reg = outp['loss_box_reg']
79-
loss_objectness = outp['loss_objectness']
80-
loss_rpn_box_reg = outp['loss_rpn_box_reg']
81-
82-
print(outp)
83-
losses = sum(loss for loss in outp.values())
84-
optimizer.zero_grad()
85-
losses.backward()
86-
optimizer.step()
37+
lr = lr,
38+
momentum=0.9, weight_decay=1e-4
39+
)
40+
for epoch in range(nb_epochs):
41+
self.train_epoch(model, optimizer, loader)
42+
43+
def train_epoch(self, model, optimizer, loader):
44+
for batch_idx, (target, inp) in enumerate(loader):
45+
inp = inp.cuda(non_blocking=True)
46+
outp, detection = model(inp, [target])
47+
48+
losses = sum(loss for loss in outp.values())
49+
50+
optimizer.zero_grad()
51+
losses.backward()
52+
optimizer.step()
8753

8854

8955
def load_model(self, model_backbone, model_path, num_classes):
@@ -113,25 +79,23 @@ def save_model(self,
11379
model,
11480
lr,
11581
nb_epochs):
116-
print("Saving trained Faster-RCNN model...")
11782

11883
dest = os.path.join(self.dataloader.round_working_dir, 'model_lr={}_epochs={}_backbone={}.pth'.format(lr, nb_epochs, self.model_backbone))
11984
torch.save(model.state_dict(), dest)
12085

121-
print("Done! Saved to {}".format(dest))
86+
print("Done Training! Model saved to {}".format(dest))
12287
return dest
12388

124-
def train(self, lr, nb_epochs):
89+
def train(self, lr, nb_epochs, batch_size):
12590

12691
train_data = self.dataloader.get_train_data()
92+
dset = FRCNNDataLoader(train_data)
93+
loader = torch.utils.data.DataLoader(dataset = dset, batch_size = batch_size, shuffle=True)
12794

12895
num_classes = self.dataloader.get_num_classes()
129-
print(num_classes)
130-
13196
model = self.load_model(self.model_backbone, self.model_path, num_classes)
13297

133-
self.run(model, lr, train_data, nb_epochs)
98+
self.run(model, loader, lr, nb_epochs)
13499

135100
model_dest = self.save_model(model, lr, nb_epochs)
136-
137101
return model_dest

ezmode/train_batch.py

-112
This file was deleted.

0 commit comments

Comments
 (0)