def main(): train_dataset, val_dataset = create_datasets(Config.DATASET_DIR) train_dataloader = torch.utils.data.DataLoader( train_dataset, batch_size=Config.BATCH_SIZE, num_workers=Config.DATALOADER_WORKER_NUM, shuffle=True, collate_fn=my_collate_fn) val_dataloader = torch.utils.data.DataLoader( val_dataset, batch_size=Config.BATCH_SIZE, num_workers=Config.DATALOADER_WORKER_NUM, shuffle=False, collate_fn=my_collate_fn) model = Net() optimizer = torch.optim.Adam(params=model.parameters(), lr=Config.LEARNING_RATE, weight_decay=Config.WEIGHT_DECAY) trainer = Trainer(optimizer, model, train_dataloader, val_dataloader, resume=Config.RESUME_FROM, log_dir=Config.LOG_DIR, persist_stride=Config.MODEL_SAVE_STRIDE) trainer.train()
def train(args): dataset_dir = get_dataset_dir(args) log_dir = get_log_dir(args) model_class = get_model_class(args) training_set, validation_set, num_classes = create_datasets(dataset_dir) training_dataset = Dataset(training_set, transform_for_training(model_class.IMAGE_SHAPE)) validation_dataset = Dataset(validation_set, transform_for_infer(model_class.IMAGE_SHAPE)) training_dataloader = torch.utils.data.DataLoader( training_dataset, batch_size=args.batch_size, num_workers=6, shuffle=True) validation_dataloader = torch.utils.data.DataLoader( validation_dataset, batch_size=args.batch_size, num_workers=6, shuffle=False) model = model_class(num_classes).to(device) trainables_wo_bn = [ param for name, param in model.named_parameters() if param.requires_grad and "bn" not in name ] trainables_only_bn = [ param for name, param in model.named_parameters() if param.requires_grad and "bn" in name ] optimizer = torch.optim.SGD( [ { "params": trainables_wo_bn, "weight_decay": 0.0001 }, { "params": trainables_only_bn }, ], lr=args.lr, momentum=0.9, ) trainer = Trainer( optimizer, model, training_dataloader, validation_dataloader, max_epoch=args.epochs, resume=args.resume, log_dir=log_dir, ) trainer.train()
def train(args): dataset_dir = get_dataset_dir(args) log_dir = get_log_dir(args) model_class = get_model_class(args) # orgainzesz dataset go more into training_set, validation_set, num_classes = create_datasets(dataset_dir) #look more into training_dataset = Dataset(training_set, transform_for_training(model_class.IMAGE_SHAPE)) validation_dataset = Dataset(validation_set, transform_for_infer(model_class.IMAGE_SHAPE)) training_dataloader = torch.utils.data.DataLoader( training_dataset, batch_size=args.batch_size, num_workers=6, shuffle=True) validation_dataloader = torch.utils.data.DataLoader( validation_dataset, batch_size=args.batch_size, num_workers=6, shuffle=False) # what is Numclases and devie model = model_class(num_classes).to(device) # tain trainables_wo_bn and trainables_only_bn trainables_wo_bn = [ param for name, param in model.named_parameters() if param.requires_grad and 'bn' not in name ] trainables_only_bn = [ param for name, param in model.named_parameters() if param.requires_grad and 'bn' in name ] optimizer = torch.optim.SGD([{ 'params': trainables_wo_bn, 'weight_decay': 0.0001 }, { 'params': trainables_only_bn }], lr=args.lr, momentum=0.9) trainer = Trainer(optimizer, model, training_dataloader, validation_dataloader, max_epoch=args.epochs, resume=args.resume, log_dir=log_dir) trainer.train()
def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument('config', type=str, help='Experiment to run') parser.add_argument('--workers', type=int, default=None, help='set number of workers (default #CPUs - 1)') parser.add_argument('--log_dir', type=str, default="runs/run1", help='directory to save/load the runs and logs') parser.add_argument("--dataroot", type=str, default="data", help="change datasets root path") parser.add_argument("--cuda", action="store_true", help="train on GPUs") parser.add_argument("--single_gpu", action="store_true", help="Disable multi-GPU") parser.add_argument("--preload_vid", action="store_true", help="Load video features into RAM") parser.add_argument("--no_preload_text", action="store_true", help="Do not load text features into RAM") args = parser.parse_args() cfg = utils.load_config(args.config) if cfg.training.random_seed is not None: print('Random seed: {:d}'.format(cfg.training.random_seed)) utils.set_seed(cfg.training.random_seed) num_workers = min(10, cpu_count() - 1) if args.workers is None else args.workers print(f"{num_workers} parallel dataloader workers") dataset_path = Path(args.dataroot) / cfg.dataset.name train_set, val_set = create_datasets(dataset_path, cfg, args.preload_vid, not args.no_preload_text) train_loader, val_loader = create_loaders(train_set, val_set, cfg.training.batch_size, num_workers) trainer = TrainerVideoText(args.log_dir, cfg, args.cuda, args.cuda and not args.single_gpu) trainer.train_loop(train_loader, val_loader) trainer.logger.info("---------- Results ----------") utils.print_csv_results(trainer.log_dir / "train_metrics.csv", cfg, print_fn=trainer.logger.info) trainer.close()
def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument('config', type=str, help='Experiment to run') parser.add_argument('checkpoint', type=str, help='Checkpoint to load') parser.add_argument('--workers', type=int, default=None, help='set number of workers (default #CPUs - 1)') parser.add_argument('--log_dir', type=str, default="runs/eval", help='directory to save/load the runs and logs') parser.add_argument("--dataroot", type=str, default="data", help="change datasets root path") parser.add_argument("--cuda", action="store_true", help="train on GPUs") parser.add_argument("--single_gpu", action="store_true", help="Disable multi-GPU") args = parser.parse_args() cfg = utils.load_config(args.config) utils.set_seed(0) num_workers = min(10, cpu_count() - 1) if args.workers is None else args.workers print(f"{num_workers} parallel dataloader workers") dataset_path = Path(args.dataroot) / cfg.dataset.name train_set, val_set = create_datasets(dataset_path, cfg, False, False) train_loader, val_loader = create_loaders(train_set, val_set, cfg.training.batch_size, num_workers) trainer = TrainerVideoText(args.log_dir, cfg, args.cuda, args.cuda and not args.single_gpu, args.checkpoint, False) trainer.validate(val_loader) trainer.close()
def train(config): train_config = config['train'] global device device = train_config['device'] if not torch.cuda.is_available(): device = 'cpu' tqdm.write('Training on {}'.format(device)) writer = SummaryWriter('log') train_dataset, test_dataset = create_datasets(**config['dataset']) train_dataloader = DataLoader(train_dataset, batch_size=train_config['batch_size'], shuffle=True, collate_fn=collate_fn) test_dataloader = DataLoader(test_dataset, batch_size=train_config['batch_size'], shuffle=False, collate_fn=collate_fn) encoder = Encoder(vocab_size=len(train_dataset.lang1), **config['encoder'], device=device).to(device) decoder = Decoder(vocab_size=len(train_dataset.lang2), **config['decoder']).to(device) encoder_optimizer = optim.Adam(encoder.parameters(), lr=train_config['lr']) decoder_optimizer = optim.Adam(decoder.parameters(), lr=train_config['lr']) criterion = nn.NLLLoss() tqdm.write('[-] Start training! ') epoch_bar = tqdm(range(train_config['n_epochs']), desc='[Total progress]', leave=True, position=0, dynamic_ncols=True) for epoch in epoch_bar: batch_bar = tqdm(range(len(train_dataloader)), desc='[Train epoch {:2}]'.format(epoch), leave=True, position=0, dynamic_ncols=True) encoder.train() decoder.train() train_loss = 0 for batch in batch_bar: (source, target_bos, target_eos) = next(iter(train_dataloader)) encoder_optimizer.zero_grad() decoder_optimizer.zero_grad() source, target_bos, target_eos = source.to(device), target_bos.to( device), target_eos.to(device) encoder_output, encoder_hidden = encoder(source) decoder_output = decoder(target_bos, encoder_hidden) loss = criterion(decoder_output.view(-1, decoder_output.size(-1)), target_eos.view(-1)) train_loss += loss.item() n_hit, n_total = hitRate(decoder_output, target_eos) loss.backward() #print(loss.item()) encoder_optimizer.step() decoder_optimizer.step() batch_bar.set_description( '[Train epoch {:2} | Loss: {:.2f} | Hit: {}/{}]'.format( epoch, loss, n_hit, n_total)) train_loss /= len(train_dataloader) batch_bar = tqdm(range(len(test_dataloader)), desc='[Test epoch {:2}]'.format(epoch), leave=True, position=0, dynamic_ncols=True) encoder.eval() decoder.eval() test_loss = 0 for batch in batch_bar: (source, target_bos, target_eos) = next(iter(test_dataloader)) source, target_bos, target_eos = source.to(device), target_bos.to( device), target_eos.to(device) with torch.no_grad(): encoder_output, encoder_hidden = encoder(source) decoder_output = decoder(target_bos, encoder_hidden) loss = criterion( decoder_output.view(-1, decoder_output.size(-1)), target_eos.view(-1)) test_loss += loss.item() n_hit, n_total = hitRate(decoder_output, target_eos) batch_bar.set_description( '[Test epoch {:2} | Loss: {:.2f} | Hit: {}/{}]'.format( epoch, loss, n_hit, n_total)) test_loss /= len(test_dataloader) writer.add_scalars('Loss', { 'train': train_loss, 'test': test_loss }, epoch) sample(test_dataset, encoder, decoder) tqdm.write('[-] Done!')
logger.info('Running in offline mode...') # Access workspace. logger.info('Accessing workspace...') workspace = Workspace.from_config() experiment = Experiment(workspace, "training-junkyard") run = experiment.start_logging(outputs=None, snapshot_directory=None) # Online run. Use dataset provided by training notebook. else: logger.info('Running in online mode...') experiment = run.experiment workspace = experiment.workspace # Prepare the datasets. dataset_train, dataset_validate, dataset_anomaly = create_datasets( workspace, experiment, run, offline_run, CONFIG) # Create the model. model = Autoencoder(family=CONFIG.MODEL_FAMILY, input_shape=(CONFIG.IMAGE_TARGET_HEIGHT, CONFIG.IMAGE_TARGET_WIDTH, CONFIG.IMAGE_TARGET_DEPTH), filters=CONFIG.FILTERS, latent_dim=CONFIG.LATENT_DIM, size=CONFIG.MODEL_SIZE) #model.summary() # Make sure that output path exists. outputs_path = "outputs" if not os.path.exists(outputs_path): os.mkdir(outputs_path)
from lang import Lang from utils import * from dataset import SentencePairDataset, collate_fn, create_datasets from torch.utils.data import DataLoader if __name__ == '__main__': lang1 = Lang('english', [['hello', 'fda', 'fsdf'], ['okf', 'fda', 'fdsfa']]) print(lang1) # dataset = SentencePairDataset('cmn.txt', 'eng', 'chi') # print(len(dataset)) # print(dataset.lang1) # print(dataset.lang2) # print(dataset[100]) # print(dataset) train_dataset, test_dataset = create_datasets('../data/cmn.txt', 'english', 'chinese', 5, 0.1) print(len(train_dataset), len(test_dataset)) dataloader = DataLoader(train_dataset, batch_size=32, shuffle=True, collate_fn=collate_fn) for i, (source, target_bos, target_eos) in enumerate(dataloader): if i == 0: print(source, target_bos, target_eos) print(len(train_dataset.lang1), len(train_dataset.lang2)) print(len(test_dataset.lang1), len(test_dataset.lang2))
type=bool, default=False, help='Perform inference on the whole test set') parser.add_argument('-i', '--image', type=str, default=None, help='To the image file') FLAGS, unparsed = parser.parse_known_args() # Create all datasets if not os.path.exists(TRAIN_PATH) or not os.path.exists(VAL_PATH) or \ not os.path.exists(TEST_PATH): create_datasets() if not FLAGS.train: if FLAGS.image == None and FLAGS.on_test_batch == False: raise Exception('Path to image file is not provided!') elif FLAGS.on_test_batch: test_model(FLAGS.graph_path, FLAGS.on_test_batch) else: test_model(FLAGS.graph_path, FLAGS.on_test_batch, FLAGS.image) exit(0) # Initialize the ImageDataGenerator train_aug = ImageDataGenerator(rescale=1.0 / 255, rotation_range=20, zoom_range=0.05, width_shift_range=0.05,