Skip to content

Commit

Permalink
update logger and trainer (clip regularization if excessive)
Browse files Browse the repository at this point in the history
  • Loading branch information
fcdl94 authored and fcdl94 committed Jan 26, 2021
1 parent 346aa37 commit 327cd04
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 11 deletions.
6 changes: 3 additions & 3 deletions data/coco-stuff/make_annotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@

for split in ["train2017", "val2017"]:
annotations = f"{orig_path}/annotations/{split}"
nov_ann = f"{dest_path}/annotations_my/{split}"
nov_ann = f"{dest_path}/annotations_obj/{split}"

# clear folder if exists
if osp.exists(nov_ann):
print("Removing existing")
os.rmdir(nov_ann)
os.makedirs(nov_ann)

# remap labels in the novel interval (+1 for Stuff, +1 and stuff on 0 for objects)
# remap labels in the novel interval (+1 for Stuff, +1 excluding stuff for objects)
mapping = np.zeros((256,), dtype=np.int8)
for i, cl in enumerate(range(91)):
for i, cl in enumerate(range(182)):
mapping[cl] = i + 1
mapping[255] = 255
target_transform = lambda x: Image.fromarray(mapping[x])
Expand Down
8 changes: 4 additions & 4 deletions data/coco/make_annotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,22 @@
import os.path as osp
from tqdm import tqdm

orig_path = '' # insert here the source path where original COCO annotations are
orig_path = '.' # insert here the source path where original COCO annotations are
dest_path = '.' # the destination folder, which should be this one'

for split in ["train2017", "val2017"]:
annotations = f"{orig_path}/annotations/{split}"
nov_ann = f"{dest_path}/annotations_obj/{split}"
nov_ann = f"{dest_path}/annotations_my/{split}"

# clear folder if exists
if osp.exists(nov_ann):
print("Removing existing")
os.rmdir(nov_ann)
os.makedirs(nov_ann)

# remap labels in the novel interval (+1 for Stuff, +1 excluding stuff for objects)
# remap labels in the novel interval (+1 for Stuff, +1 and stuff on 0 for objects)
mapping = np.zeros((256,), dtype=np.int8)
for i, cl in enumerate(range(182)):
for i, cl in enumerate(range(91)):
mapping[cl] = i + 1
mapping[255] = 255
target_transform = lambda x: Image.fromarray(mapping[x])
Expand Down
10 changes: 7 additions & 3 deletions methods/trainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from modules.classifier import IncrementalClassifier, CosineClassifier, SPNetClassifier
from .utils import get_scheduler, get_batch, MeanReduction

CLIP = 10

class Trainer:
def __init__(self, task, device, logger, opts):
Expand Down Expand Up @@ -211,10 +212,13 @@ def train(self, cur_epoch, train_loader, metrics=None, print_int=10, n_iter=1):

loss = self.reduction(criterion(outputs, labels), labels)

loss_tot = loss + rloss
assert not torch.isnan(loss_tot), "Error, loss is NaN!"
loss_tot.backward()
if rloss <= CLIP:
loss_tot = loss + rloss
else:
print(f"Warning, rloss is {rloss}! Term ignored Skipped")
loss_tot = loss

loss_tot.backward()
optim.step()
scheduler.step()

Expand Down
2 changes: 1 addition & 1 deletion utils/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def error(self, msg):

def log_results(self, task, name, results, novel=False):
if self.rank == 0:
file_name = f"{task.task}.csv"
file_name = f"{task.task}-n{task.nshot}.csv"
file_name = file_name if not novel else f"{file_name}_novel.csv"
dir_path = f"{self.logdir_results}/{task.dataset}"
path = f"{dir_path}/{file_name}"
Expand Down

0 comments on commit 327cd04

Please sign in to comment.