def build_loader(config): """ Build and return DataLoader """ return get_loader(name=config['name'], datadir=config['datadir'], batch_size=config['batch_size'], num_workers=config['num_workers'])
def main(config): if not os.path.exists(config.checkpoint_dir): os.makedirs(config.checkpoint_dir) print('[*] Load Dataset') train_loader, test_loader = get_loader(config) print('[*] Train') trainer = Trainer(config, train_loader) trainer.train() print('[*] test') tester = Tester(config, test_loader) tester.test()
def main(version, config): # for fast training cudnn.benchmark = True train_data_loader, test_data_loader = get_loader(config) solver = Solver(version, train_data_loader, test_data_loader, vars(config)) if config.mode == 'train': temp_save_path = os.path.join(config.model_save_path, version) mkdir(temp_save_path) solver.train() elif config.mode == 'test': if config.dataset == 'voc': temp_save_path = os.path.join(config.result_save_path, config.pretrained_model) mkdir(temp_save_path) elif config.dataset == 'coco': temp_save_path = os.path.join(config.result_save_path, version) mkdir(temp_save_path) solver.test()
def main(version, config, output_txt): # for fast training cudnn.benchmark = True data_loader = get_loader(config) solver = Solver(version=version, data_loader=data_loader, config=vars(config), output_txt=output_txt) if config.mode == 'train': temp_save_path = osp.join(config.model_save_path, version) mkdir(temp_save_path) solver.train() elif config.mode == 'test': if config.dataset == 'voc': temp_save_path = osp.join(config.model_test_path, config.pretrained_model) mkdir(temp_save_path) solver.test()
def train_model(args, device, parallel): """ The function to train the model :param args: the class carries configuration parameters defined in config.py :param device: the device to run the model :return: """ model = network_io.create_model(args) log_dir = os.path.join(args['save_dir'], 'log') writer = SummaryWriter(log_dir=log_dir) # TODO add write_graph back, probably need to swith to tensorboard in pytorch if parallel: model.encoder = network_utils.DataParallelPassThrough(model.encoder) model.decoder = network_utils.DataParallelPassThrough(model.decoder) if args['optimizer']['aux_loss']: model.cls = network_utils.DataParallelPassThrough(model.cls) print('Parallel training mode enabled!') train_params = model.set_train_params( (args['optimizer']['learn_rate_encoder'], args['optimizer']['learn_rate_decoder'])) # make optimizer optm = network_io.create_optimizer(args['optimizer']['name'], train_params, args['optimizer']['learn_rate_encoder']) criterions = network_io.create_loss(args, device=device) cls_criterion = None with_aux = False if args['optimizer']['aux_loss']: with_aux = True cls_criterion = metric_utils.BCEWithLogitLoss( device, eval(args['trainer']['class_weight'])) scheduler = optim.lr_scheduler.MultiStepLR( optm, milestones=eval(args['optimizer']['decay_step']), gamma=args['optimizer']['decay_rate']) # if not resume, train from scratch if args['trainer']['resume_epoch'] == 0 and args['trainer'][ 'finetune_dir'] == 'None': print('Training decoder {} with encoder {} from scratch ...'.format( args['decoder_name'], args['encoder_name'])) elif args['trainer']['resume_epoch'] == 0 and args['trainer'][ 'finetune_dir']: print('Finetuning model from {}'.format( args['trainer']['finetune_dir'])) if args['trainer']['further_train']: network_utils.load(model, args['trainer']['finetune_dir'], relax_load=True, optm=optm, device=device) else: network_utils.load(model, args['trainer']['finetune_dir'], relax_load=True) else: print('Resume training decoder {} with encoder {} from epoch {} ...'. format(args['decoder_name'], args['encoder_name'], args['trainer']['resume_epoch'])) network_utils.load_epoch(args['save_dir'], args['trainer']['resume_epoch'], model, optm, device) # prepare training print('Total params: {:.2f}M'.format(network_utils.get_model_size(model))) model.to(device) for c in criterions: c.to(device) # make data loader ds_cfgs = [a for a in sorted(args.keys()) if 'dataset' in a] assert ds_cfgs[0] == 'dataset' train_val_loaders = {'train': [], 'valid': []} if args['dataset']['load_func'] == 'default': load_func = data_utils.default_get_stats else: load_func = None for ds_cfg in ds_cfgs: mean, std = network_io.get_dataset_stats( args[ds_cfg]['ds_name'], args[ds_cfg]['data_dir'], mean_val=(eval(args[ds_cfg]['mean']), eval(args[ds_cfg]['std'])), load_func=load_func, file_list=args[ds_cfg]['train_file']) tsfm_train, tsfm_valid = network_io.create_tsfm(args, mean, std) train_loader = DataLoader( data_loader.get_loader(args[ds_cfg]['data_dir'], args[ds_cfg]['train_file'], transforms=tsfm_train, n_class=args[ds_cfg]['class_num'], with_aux=with_aux), batch_size=int(args[ds_cfg]['batch_size']), shuffle=True, num_workers=int(args['dataset']['num_workers']), drop_last=True) train_val_loaders['train'].append(train_loader) if 'valid_file' in args[ds_cfg]: valid_loader = DataLoader( data_loader.get_loader(args[ds_cfg]['data_dir'], args[ds_cfg]['valid_file'], transforms=tsfm_valid, n_class=args[ds_cfg]['class_num'], with_aux=with_aux), batch_size=int(args[ds_cfg]['batch_size']), shuffle=False, num_workers=int(args[ds_cfg]['num_workers'])) print('Training model on the {} dataset'.format( args[ds_cfg]['ds_name'])) train_val_loaders['valid'].append(valid_loader) # train the model loss_dict = {} for epoch in range(int(args['trainer']['resume_epoch']), int(args['trainer']['epochs'])): # each epoch has a training and validation step for phase in ['train', 'valid']: start_time = timeit.default_timer() if phase == 'train': model.train() else: model.eval() # TODO align aux loss and normal train loss_dict = model.step( train_val_loaders[phase], device, optm, phase, criterions, eval(args['trainer']['bp_loss_idx']), True, mean, std, loss_weights=eval(args['trainer']['loss_weights']), use_emau=args['use_emau'], use_ocr=args['use_ocr'], cls_criterion=cls_criterion, cls_weight=args['optimizer']['aux_loss_weight']) network_utils.write_and_print(writer, phase, epoch, int(args['trainer']['epochs']), loss_dict, start_time) scheduler.step() # save the model if epoch % int(args['trainer']['save_epoch']) == 0 and epoch != 0: save_name = os.path.join(args['save_dir'], 'epoch-{}.pth.tar'.format(epoch)) network_utils.save(model, epoch, optm, loss_dict, save_name) # save model one last time save_name = os.path.join( args['save_dir'], 'epoch-{}.pth.tar'.format(int(args['trainer']['epochs']))) network_utils.save(model, int(args['trainer']['epochs']), optm, loss_dict, save_name) writer.close()
transforms.ToTensor(), # convert the PIL Image to a tensor transforms.Normalize( (0.485, 0.456, 0.406), # normalize image for pre-trained model (0.229, 0.224, 0.225)) ]) # Set the minimum word count threshold. vocab_threshold = 5 # Specify the batch size. batch_size = 10 # Obtain the data loader. data_loader = get_loader(transform=transform_train, mode='train', batch_size=batch_size, vocab_threshold=vocab_threshold, vocab_from_file=True) sample_caption = 'A person doing a trick on a rail while riding a skateboard.' sample_tokens = nltk.tokenize.word_tokenize(str(sample_caption).lower()) print(sample_tokens) sample_caption = [] # Continue the list by adding integers that correspond to each of the tokens in the caption. start_word = data_loader.dataset.vocab.start_word print('Special start word:', start_word) sample_caption.append(data_loader.dataset.vocab(start_word)) print(sample_caption)
import numpy as np import matplotlib.pyplot as plt device = torch.device("cuda" if torch.cuda.is_available() else "cpu") # --- Loader --- # Define a transform to pre-process the testing images. transform_test = transforms.Compose([ transforms.Resize(256), # smaller edge of image resized to 256 transforms.RandomCrop(224), # get 224x224 crop from random location transforms.ToTensor(), # convert the PIL Image to a tensor transforms.Normalize((0.485, 0.456, 0.406), # normalize image for pre-trained model (0.229, 0.224, 0.225))]) # Create the data loader. data_loader = get_loader(transform=transform_test, mode='test') # Obtain sample image before and after pre-processing. orig_image, image = next(iter(data_loader)) # Visualize sample image, before pre-processing. plt.imshow(np.squeeze(orig_image)) plt.title('example image') plt.show() # --- Load Model --- # Specify the saved models to load. encoder_file = 'encoder-3.pkl' decoder_file = 'decoder-3.pkl' # Select appropriate values for the Python variables below.
from data.data_loader import get_loader import torchvision.transforms as transforms import torch from trainer import Trainer # get args from opts import args devices = [a for a in range(torch.cuda.device_count())] print(args) # init data loader train_loader, num_samples, vocab_size = get_loader(args.data_dir, args.dataset, 'train', batch_size=args.batch_size * len(devices), shuffle=True, num_workers=4) val_loader, num_samples, vocab_size = get_loader( args.data_dir, args.dataset, 'test', batch_size=args.batch_size * len(devices), shuffle=False, num_workers=4 if args.cmd == 'train' else 0, unary_mode=args.loader_unary_mode) # init model if not args.no_mm: if 'tandemnet2v2' in args.name: from model.tandemnet2_v2 import DistillModel
def model_setting(args): loader, tokenizer = get_loader(args) if args.text_processor == 'roberta': config = RobertaConfig() roberta = RobertaModel(config) # text_processor = roberta.from_pretrained('roberta-base') ## 텍스트를 분할해서 로벌타에 넣어보자 if args.dataset == 'MissO_split' or args.dataset == 'TVQA_split': text_processor_que = roberta.from_pretrained('roberta-base') text_processor_utt = roberta.from_pretrained('roberta-base') elif args.eval == 'True': memory_processor = roberta.from_pretrained('roberta-base') logic_processor = roberta.from_pretrained('roberta-base') else: text_processor = roberta.from_pretrained('roberta-base') elif args.text_processor == 'bert': config = BertConfig() bert = BertModel(config) text_processor = bert.from_pretrained('bert-base-uncased') else: text_processor = None if args.eval == 'False': if args.only_text_input == 'True': model = QuestionLevelDifficultyOT(args, tokenizer, text_processor) else: if args.dataset == 'MissO_split' or args.dataset == 'TVQA_split': model = QuestionLevelDifficulty_M_split( args, tokenizer, text_processor_que, text_processor_utt) else: model = QuestionLevelDifficulty_M(args, tokenizer, text_processor) criterion = get_loss_func(tokenizer) optimizer = get_optim(args, model) scheduler = get_scheduler(optimizer, args, loader['train']) model.to(args.device) criterion.to(args.device) config = { 'loader': loader, 'optimizer': optimizer, 'criterion': criterion, 'scheduler': scheduler, 'tokenizer': tokenizer, 'args': args, 'model': model } else: memory_model = QuestionLevelDifficulty_M(args, tokenizer, memory_processor) logic_model = QuestionLevelDifficulty_L(args, tokenizer, logic_processor) memory_model.to(args.device) logic_model.to(args.device) config = { 'loader': loader, 'tokenizer': tokenizer, 'args': args, 'memory_model': memory_model, 'logic_model': logic_model } return config