-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature/weighted multiple datasets #3
base: main
Are you sure you want to change the base?
Changes from all commits
867897c
0300bf9
5e208f3
7723078
ff6a41f
96946b2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,11 +3,8 @@ | |
import time | ||
import datetime | ||
import copy | ||
from collections import deque | ||
|
||
import numpy as np | ||
import torch | ||
import torch.nn as nn | ||
from tqdm import tqdm | ||
import wandb | ||
|
||
|
@@ -71,8 +68,6 @@ def train_model(model, | |
dataset_sizes = {x: len(dataloaders[x].dataset) for x in phases} | ||
num_epochs = n_epochs | ||
|
||
start = time.time() | ||
|
||
|
||
for epoch in range(num_epochs): | ||
start_epoch = time.time() | ||
|
@@ -95,9 +90,6 @@ def train_model(model, | |
running_labels = torch.Tensor() | ||
running_outputs = torch.Tensor() | ||
|
||
#wrong_epoch_images = deque(maxlen=32) | ||
#wrong_epoch_attr = deque(maxlen=32) | ||
|
||
# Iterate over data. | ||
for batch_idx, (inputs, labels) in enumerate(tqdm(dataloaders[phase])): | ||
if metric_eer: | ||
|
@@ -129,11 +121,6 @@ def train_model(model, | |
if metric_eer: | ||
running_outputs = torch.cat((running_outputs, outputs.detach().cpu())) | ||
|
||
#if phase == "train": | ||
# wrong_epoch_images.extend([x for x in inputs[preds!=labels]]) | ||
#if track_images: | ||
# wrong_epoch_attr.extend([(labels[i], preds[i])\ | ||
# for i in (preds!=labels).nonzero().flatten()]) | ||
|
||
if phase == 'train': | ||
scheduler.step() | ||
|
@@ -160,8 +147,7 @@ def train_model(model, | |
if save_curr_model: | ||
model_folder = wandb.run.name if track_experiment else \ | ||
datetime.datetime.now().strftime('%Y-%m-%d_%H-%M-%S') | ||
if not os.path.exists(model_folder): | ||
os.mkdir(model_folder) | ||
os.makedirs(model_folder, exist_ok=True) | ||
torch.save({ | ||
'epoch': epoch, | ||
'model_state_dict': model.state_dict(), | ||
|
@@ -224,10 +210,11 @@ def train(args): | |
} | ||
|
||
train_loader, val_loader = dataloaders.get_dataset_loaders(in_datasets_names, | ||
transformers, | ||
int(args.batch_size), | ||
int(args.num_dataloader_workers), | ||
args.balanced_weights) | ||
transformers, | ||
int(args.batch_size), | ||
int(args.num_dataloader_workers), | ||
args.balanced_weights, | ||
args.multiple_datasets_temperature) | ||
|
||
model = models.get_model(args.backbone, len(train_loader.dataset.classes), | ||
not args.no_transfer_learning, args.freeze_all_but_last) | ||
|
@@ -258,11 +245,15 @@ def train(args): | |
|
||
parser.add_argument("--no_transfer_learning", action=argparse.BooleanOptionalAction) | ||
parser.add_argument("--freeze_all_but_last", action=argparse.BooleanOptionalAction) | ||
parser.add_argument("--weights", type=str) | ||
|
||
# {phase} datasets are hope to have {phase}-named folders inside them | ||
parser.add_argument("--train_datasets", action='store', type=str, nargs="+", required=True) | ||
parser.add_argument("--val_datasets", action='store', type=str, nargs="+", required=True) | ||
parser.add_argument("--balanced_weights", action=argparse.BooleanOptionalAction) | ||
parser.add_argument("--multiple_datasets_temperature", type=float, required=False, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Didn't get that, the temperature is not suppose to be defined by a value for each dataset? Is there any reference for this type of weighting? It would be awesome if you explain a bit this options in the README.md as well. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It was a hacky way to balance multiple datasets that was not very good, honestly. I think it is a nice feature to have, but I would reimplement it with a more clear interface and output. Probably something like passing a list of datasets: [ds1, ds2, ds3] and some sampling weights to them: [0.2, 0.1, 0.7] that would sample 20% of ds1, 10% of ds2 and 70% of ds3. Sounds better, right? |
||
help="Dataset path contains multiple datasets that will be combined, each one " | ||
"having a weight given by a softmax of the datasets size with this temperature.") | ||
|
||
parser.add_argument("--resize_size", default=None) | ||
parser.add_argument("--num_dataloader_workers", default=8) # recomends to be 4 x #GPU | ||
|
@@ -278,11 +269,12 @@ def train(args): | |
parser.add_argument("--wandb_sweep_activated", action=argparse.BooleanOptionalAction) | ||
|
||
parser.add_argument("--augmentation", type=str, default="simple", | ||
choices=["noaug", "simple", "rand-m9-n3-mstd0.5", "rand-mstd1-w0", "random_erase"]) | ||
choices=["noaug", "simple", "rand-m9-n3-mstd0.5", "rand-mstd1-w0", "random_erase"]) | ||
|
||
# options for optimizers | ||
parser.add_argument("--optimizer", default="sgd") # possible adam, adamp and sgd | ||
parser.add_argument("--weight_decay", type=float, default=1e-4) | ||
parser.add_argument("--t_mult", type=int, default=2) | ||
|
||
# options for model saving | ||
parser.add_argument("--save_best_model", action=argparse.BooleanOptionalAction) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know that it was me that did that, but I forgot, if I set balanced_weights the samples will be balanced by the size of the class, that's it? What you implemented is a way that you can give more/less importance to other sources of data, datasets, right?
Additionally, you cannot combine both, right?
Maybe we should change the balanced_weights parameter's name to be more descriptive.