def compile_model(model, loss="ce"): if loss is not None: loss = models.get_loss(loss, model.output_shape[1]) model.compile(optimizer="adam", loss=[loss], metrics=[metrics.sparse_categorical_accuracy]) else: model.compile(optimizer="adam")
def main(config): train_loader = get_dataloader(config['data_loader']['type'], config['data_loader']['args']) criterion = get_loss(config).cuda() model = get_model(config) trainer = Trainer(config=config, model=model, criterion=criterion, train_loader=train_loader) trainer.train()
def evaluate(self, dataset, num_classes, num_shots, inner_batch_size, inner_iters, transductive): """ Run a single evaluation of the model. Samples a few-shot learning task and measures performance. Args: dataset: a sequence of data classes, where each data class has a sample(n) method. input_ph: placeholder for a batch of samples. label_ph: placeholder for a batch of labels. minimize_op: TensorFlow Op to minimize a loss on the batch specified by input_ph and label_ph. predictions: a Tensor of integer label predictions. num_classes: number of data classes to sample. num_shots: number of examples per data class. inner_batch_size: batch size for every inner-loop training iteration. inner_iters: number of inner-loop iterations. Returns: The number of correctly predicted samples. This always ranges from 0 to num_classes. """ model_clone = clone_model(num_classes, self.model_state) optimizer_clone = get_optimizer(model_clone, self.op_state) model_clone.train() train, test = dataset.get_random_task_split(num_classes, train_shots=num_shots, test_shots=1) """ Sampling with replacement """ sampler = RandomSampler(train, replacement=True, num_samples=inner_batch_size * inner_iters) train_loader = DataLoader(train, batch_size=inner_batch_size, sampler=sampler, pin_memory=self.pin_memory) for _, (inputs, labels) in enumerate(train_loader): inputs, labels = Variable_(inputs, labels, self.cuda) prediction = model_clone(inputs) loss = get_loss(prediction, labels) optimizer_clone.zero_grad() loss.backward() optimizer_clone.step() return self._test_predictions(model_clone, train, test, transductive, self.cuda)
def train_step(self, dataset, num_classes, num_shots, inner_batch_size, inner_iters, meta_step_size, meta_batch_size): """ Perform a Reptile training step. Args: dataset: object contains images to be trained. num_classes: number of data classes to sample. num_shots: number of examples per data class. inner_batch_size: batch size for every inner-loop training iteration. inner_iters: number of inner-loop iterations. meta_step_size: interpolation coefficient. meta_batch_size: how many inner-loops to run. """ updates = [] for _ in range(meta_batch_size): model = clone_model(num_classes, self.model_state) optimizer = get_optimizer(model, self.op_state) model.train() mini_train, _ = dataset.get_random_task_split( num_classes, train_shots=num_shots, test_shots=0) """ Sampling without replacement """ train_loader = DataLoader(mini_train, batch_size=inner_batch_size, drop_last=True, shuffle=True, pin_memory=self.pin_memory) for _, (inputs, labels) in enumerate(train_loader): inputs, labels = Variable_(inputs, labels, self.cuda) last_backup = model.state_dict() prediction = model(inputs) loss = get_loss(prediction, labels) optimizer.zero_grad() loss.backward() optimizer.step() updates.append(subtract_vars(model.state_dict(), last_backup)) self.op_state = optimizer.state_dict() update = average_vars(updates) self.model_state = add_vars(self.model_state, scale_vars(update, meta_step_size)) self.op_state = optimizer.state_dict()
def main(config): import torch from models import get_model, get_loss from data_loader import get_dataloader from trainer import Trainer from post_processing import get_post_processing from utils import get_metric if torch.cuda.device_count() > 1: torch.cuda.set_device(args.local_rank) torch.distributed.init_process_group( backend="nccl", init_method="env://", world_size=torch.cuda.device_count(), rank=args.local_rank) config['distributed'] = True else: config['distributed'] = False config['local_rank'] = args.local_rank train_loader = get_dataloader(config['dataset']['train'], config['distributed']) assert train_loader is not None if 'validate' in config['dataset']: validate_loader = get_dataloader(config['dataset']['validate'], False) else: validate_loader = None criterion = get_loss(config['loss']).cuda() model = get_model(config['arch']) post_p = get_post_processing(config['post_processing']) metric = get_metric(config['metric']) trainer = Trainer(config=config, model=model, criterion=criterion, train_loader=train_loader, post_process=post_p, metric_cls=metric, validate_loader=validate_loader) trainer.train()
def compile_model(model, loss='ce'): loss = models.get_loss(loss, model.output_shape[1]) model.compile(optimizer='adam', loss=[loss], metrics=[metrics.sparse_categorical_accuracy])
def main(config): train_loader = get_dataloader(config['data_loader']['type'], config['data_loader']['args']) print(train_loader) criterion = get_loss(config).cuda()
def compile_model(model, loss='ce'): loss = models.get_loss(loss, model.output_shape[1]) model.compile(optimizer='adam', loss=[loss], metrics=['accuracy'])