def main(args): speaker_cfg = args.speaker_cfg speech_cfg = args.speech_cfg args_speaker = read_conf(speaker_cfg, deepcopy(args)) args_speaker.model_path = args.speaker_model args_speech = read_conf(speech_cfg, deepcopy(args)) args_speech.model_path = args.speech_model torch.backends.cudnn.deterministic = True torch.backends.cudnn.benchmark = False print("set seed: ", args_speaker.optimization.seed) torch.manual_seed(args_speaker.optimization.seed) np.random.seed(args_speaker.optimization.seed) random.seed(args_speaker.optimization.seed) torch.cuda.set_device(args.local_rank) if not args.no_dist: torch.distributed.init_process_group(backend="nccl") train_dataset = TIMIT_speaker(args.data_root, train=True, phoneme=True, norm_factor=True) test_dataset = TIMIT_speaker(args.data_root, train=False, phoneme=True, norm_factor=True) pretrained_models = get_pretrained_models(args_speaker, args_speech) loss_factors = { "speaker": args.speaker_factor, "speech": args.speech_factor, "norm": args.norm_factor } if args.target < 0: # non-targeted speaker_loss = SpeakerLoss(pretrained_models['speaker']) else: # targeted attack speaker_loss = SpeakerLossTarget(pretrained_models['speaker'], args.target) loss_all = {} loss_all['speech'] = { 'model': pretrained_models['speech'], 'factor': loss_factors['speech'], 'loss_func': SpeechLoss(pretrained_models['speech'], factor_kld=args.speech_kld_factor) } loss_all['speaker'] = { 'model': pretrained_models['speaker'], 'factor': loss_factors['speaker'], 'loss_func': speaker_loss } loss_all['norm'] = { 'loss_func': MSEWithThreshold(args.norm_clip), 'factor': loss_factors['norm'] } cost = AdversarialLoss(loss_all) model = SpeechTransformer(args.channel, args.kernel_size, args.dilation, args.sample, args.noise_scale) if args.pt_file != 'none': print("load model from:", args.pt_file) if os.path.splitext(args.pt_file)[1] == '.pkl': checkpoint_load = torch.load(args.pt_file) model.load_raw_state_dict(checkpoint_load) else: load_checkpoint(model, args.pt_file) model = model.cuda() if args.eval: assert args.pt_file != 'none', "no pretrained model is provided!" print('only eval the model') evaluate(model, test_dataset, cost) return if args.test: assert args.pt_file != 'none', "no pretrained model is provided!" print("only test the model") filename_list = open("./data/TIMIT/speaker/test.scp", 'r').readlines() filename_list = [_f.strip() for _f in filename_list] label_dict = np.load( os.path.join(args.data_root, "processed", "TIMIT_labels.npy")).item() test_wav(model, filename_list, args.data_root, os.path.join(args.data_root, "output"), pretrained_models['speaker'], label_dict, args.target) return if args.cpu_test: assert args.pt_file != 'none', "no pretrained model is provided!" print("only cpu test the model") filename_list = open("./data/TIMIT/speaker/test.scp", 'r').readlines() filename_list = [_f.strip() for _f in filename_list] label_dict = np.load( os.path.join(args.data_root, "processed", "TIMIT_labels.npy")).item() test_wav_cpu(model, filename_list, args.data_root, os.path.join(args.data_root, "output"), pretrained_models['speaker'], label_dict, args.target) return print("train the model") batch_process = batch_process_speaker eval_hook = EvalHook() optimizer = optim.Adam(model.parameters(), lr=args_speaker.optimization.lr, betas=(0.95, 0.999)) lr_scheduler = optim.lr_scheduler.StepLR(optimizer, 2, 0.5) if args.no_dist: kwarg = { 'shuffle': True, 'worker_init_fn': partial(_init_fn, seed=args_speaker.optimization.seed) } else: train_sampler = torch.utils.data.distributed.DistributedSampler( train_dataset) kwarg = { 'sampler': train_sampler, 'worker_init_fn': partial(_init_fn, seed=args_speaker.optimization.seed) } train_dataloader = DataLoader(train_dataset, args_speaker.optimization.batch_size, num_workers=args.num_workers, pin_memory=True, **kwarg) test_dataloader = DataLoader(test_dataset, args_speaker.optimization.batch_size, shuffle=False, num_workers=args.num_workers, pin_memory=True) trainer = ClassifierTrainer( model, train_dataloader, optimizer, cost, batch_process, args.output_dir, 0, test_dataloader, eval_hook=eval_hook, eval_every=args_speaker.optimization.N_eval_epoch, print_every=args_speaker.optimization.print_every, lr_scheduler=lr_scheduler) trainer.logger.info(args) trainer.run(args_speaker.optimization.N_epochs)
model_list = [ dict(type='Conv2D', in_channels=3, out_channels=32, kernel_size=5, stride=1, padding=2), dict(type='ReLU'), dict(type='MaxPooling', kernel_size=2, stride=2), dict(type='Linear', in_dim=8192, out_dim=10) ] criterion = dict(type='SoftmaxCrossEntropy') model = ConvNet(model_list, criterion) optimizer = SGD(model, learning_rate=0.0001, reg=0.001, momentum=0.9) trainer = ClassifierTrainer() loss_history, train_acc_history = trainer.train(X_train[:50], y_train[:50], model, batch_size=10, num_epochs=10, verbose=True, optimizer=optimizer) plt.plot(train_acc_history) plt.legend(['train', 'val'], loc='upper left') plt.xlabel('epoch') plt.ylabel('accuracy') plt.savefig('train.png')
def main(args): # init for distributed training print("rank:{}, init dataset".format(args.local_rank)) torch.cuda.set_device(args.local_rank) torch.distributed.init_process_group( backend="nccl" ) #, rank=0, world_size=1, init_method="tcp://127.0.0.1:29500") if args.dataset == "birds": data_root = "./data/birds" train_dataset = BirdDataset(data_root=data_root, train=True) val_dataset = BirdDataset(data_root=data_root, train=False) elif args.dataset == "places": data_root = "./data/Places_subset" train_dataset = PlaceSubSet(data_root=data_root, train=True) val_dataset = PlaceSubSet(data_root=data_root, train=False) elif args.dataset == "flowers": data_root = "./data/flowers" train_dataset = FlowerDataset(data_root=data_root, train=True) val_dataset = FlowerDataset(data_root=data_root, train=False) else: raise NotImplementedError train_sampler = torch.utils.data.distributed.DistributedSampler( train_dataset) train_dataloader = DataLoader(train_dataset, batch_size=args.batch_size, num_workers=args.num_workers, sampler=train_sampler) val_dataloader = DataLoader(val_dataset, batch_size=args.batch_size) # define model print("rank:{}, define model".format(args.local_rank)) model = CNNRNN(40, embedding_dim=1024, drop_prob=args.dropout, nhidden=1024, nsent=1024, bidirectional=args.bidirectional, rnn_layers=args.rnn_layers) batch_param = {'length_required': True, 'sorted_required': True} model = model.cuda() model = torch.nn.parallel.DistributedDataParallel( model, device_ids=[args.local_rank], output_device=args.local_rank) # optim optimizer = torch.optim.Adam(model.parameters(), lr=args.learning_rate, weight_decay=1e-5) lr_scheduler = torch.optim.lr_scheduler.StepLR( optimizer, step_size=args.lr_scheduler_step_size, gamma=args.lr_scheduler_gamma) # loss loss_func = LossFunc(args.loss_diff, args.loss_same, args.jel_flag, args.l1_flag, args.lambda_l1, args.distill_flag, args.distill_T, args.lambda_distill) eval_hook = EvalHook() test_func = EvalClass() print("rank:{}, define trainer".format(args.local_rank)) trainer = ClassifierTrainer(model=model, train_dataloader=train_dataloader, val_dataloader=val_dataloader, optimizer=optimizer, loss_func=loss_func, batch_process=batch_process, output_dir=args.output_dir, local_rank=args.local_rank, print_every=args.print_every, eval_every=args.eval_every, lr_scheduler=lr_scheduler, eval_hook=eval_hook, test_func=test_func, batch_param=batch_param, resume_from=args.resume) trainer.run(args.epoch)
process = Preprocessor() # Dataset train_data = TrainDataset(process.get_train()) val_data = TrainDataset(process.get_val()) test_data = TestDataset(process.get_test()) # Dataloader train_loader = DataLoader(train_data, batch_size=32, shuffle=True, drop_last=True) val_loader = DataLoader(val_data, batch_size=100) test_loader = DataLoader(test_data, batch_size=16) model = Classifier().cuda() # Training trainers = ClassifierTrainer(model, train_loader, val_loader, metric=balanced_accuracy_score) trainers.train(max_epoch=5, patience=1) # Visualization trainers.plot_cm() trainers.plot_metric_curve() trainers.plot_loss_curve() # Evaluation trainers.model.eval() pred = [] for x in test_loader: pred.append(trainers.model.predict(x.float().cuda()))
parser.add_argument('-i', '--input_dir', type=str, default='GTSRB-new\RB\val\0-clear', help="input image path") parser.add_argument('-o', '--output_dir', type=str, default='result-2/GTSRB/original/', help="output image path") parser.add_argument('-p', '--checkpoint', type=str, default='checkpoints-new-2/0-0.2/outputs/GTSRB/checkpoints/classifier.pt', help="checkpoint") parser.add_argument('-l', '--log_name', type=str, default='0-0.2.log', help="log name") parser.add_argument('-g', '--gpu_id', type=int, default=0, help="gpu id") opts = parser.parse_args() # Load experiment setting config = get_config(opts.config) # Setup model and data loader trainer = ClassifierTrainer(config) state_dict = torch.load(opts.checkpoint, map_location='cuda:{}'.format(opts.gpu_id)) trainer.net.load_state_dict(state_dict['net']) epochs = state_dict['epochs'] min_loss = state_dict['min_loss'] acc = state_dict['acc'] if 'acc' in state_dict.keys() else 0.0 print("=" * 40) print('Resume from epoch: {}, min-loss: {} acc: {}'.format(epochs, min_loss, acc)) print("=" * 40) trainer.cuda() trainer.eval() pred_acc_list = []
def main(args): args = read_conf(args.cfg, args) torch.manual_seed(args.seed) np.random.seed(args.seed) if args.dataset == 'timit': train_dataset = TIMIT(data_root=args.data_root, datalist_root=args.datalist_root, train=True, oversampling=args.oversampling) test_dataset = TIMIT(data_root=args.data_root, datalist_root=args.datalist_root, train=False) elif args.dataset == 'libri': raise NotImplementedError else: raise NotImplementedError cost = nn.NLLLoss() CNN_arch = { 'input_dim': train_dataset.wlen, 'fs': args.fs, 'cnn_N_filt': args.cnn_N_filt, 'cnn_len_filt': args.cnn_len_filt, 'cnn_max_pool_len': args.cnn_max_pool_len, 'cnn_use_laynorm_inp': args.cnn_use_laynorm_inp, 'cnn_use_batchnorm_inp': args.cnn_use_batchnorm_inp, 'cnn_use_laynorm': args.cnn_use_laynorm, 'cnn_use_batchnorm': args.cnn_use_batchnorm, 'cnn_act': args.cnn_act, 'cnn_drop': args.cnn_drop, } DNN1_arch = { 'fc_lay': args.fc_lay, 'fc_drop': args.fc_drop, 'fc_use_batchnorm': args.fc_use_batchnorm, 'fc_use_laynorm': args.fc_use_laynorm, 'fc_use_laynorm_inp': args.fc_use_laynorm_inp, 'fc_use_batchnorm_inp': args.fc_use_batchnorm_inp, 'fc_act': args.fc_act, } DNN2_arch = { 'input_dim': args.fc_lay[-1], 'fc_lay': args.class_lay, 'fc_drop': args.class_drop, 'fc_use_batchnorm': args.class_use_batchnorm, 'fc_use_laynorm': args.class_use_laynorm, 'fc_use_laynorm_inp': args.class_use_laynorm_inp, 'fc_use_batchnorm_inp': args.class_use_batchnorm_inp, 'fc_act': args.class_act, } model = SpeakerIDNet(CNN_arch, DNN1_arch, DNN2_arch) if args.pt_file != '': print("load model from:", args.pt_file) checkpoint_load = torch.load(args.pt_file) ext = os.path.splitext(args.pt_file)[1] if ext == '.pkl': model.load_raw_state_dict(checkpoint_load) elif ext == '.pickle': model.load_state_dict(checkpoint_load) elif ext == '.pth': load_checkpoint(model, args.pt_file) else: raise NotImplementedError model = model.cuda() if args.eval: print('only eval the model') evaluate(model, test_dataset, cost) return else: print("train the model") optimizer = optim.RMSprop(model.parameters(), lr=args.lr, alpha=0.95, eps=1e-8) train_dataloader = DataLoader(train_dataset, args.batch_size, shuffle=True, num_workers=8, pin_memory=True) test_dataloader = DataLoader(test_dataset, 1, shuffle=False, num_workers=8, pin_memory=True) trainer = ClassifierTrainer(model, train_dataloader, optimizer, cost, batch_process, args.output_dir, 0, test_dataloader, eval_every=args.N_eval_epoch, print_every=args.print_every) trainer.run(args.N_epochs)
def main(): parser = argparse.ArgumentParser() parser.add_argument('--batch_size', help='Batch size', required=False, type=int, default=256) parser.add_argument('--pickle_file', help='Pickle file', required=False, default='./traffic.pickle') parser.add_argument('--learning_rate', help='Learning rate', required=False, type=float, default=0.0001) parser.add_argument('--image_size', help='Image size', required=False, type=int, default=32) parser.add_argument('--num_classes', help="Number of classes", required=False, type=int, default=43) parser.add_argument('--color_channels', help="Color channels", required=False, type=int, default=1) parser.add_argument('--ckpt_dir', help="Check point directory", required=False, default='./modelSave/') parser.add_argument('--num_iter_per_epoch', help="Number of iterations per epoch", required=False, type=int, default=1) parser.add_argument('--num_epochs', help="Number of epochs", required=False, type=int, default=1) parser.add_argument('--load_model', help="Load model or train model from scratch", required=False, type=bool, default=True) args = parser.parse_args() class config: batch_size = args.batch_size pickle_file = args.pickle_file learning_rate = args.learning_rate image_size = args.image_size num_classes = args.num_classes num_channels = args.color_channels num_iter_per_epoch = args.num_iter_per_epoch num_epochs = args.num_epochs checkpoint_dir = args.ckpt_dir load_model = args.load_model sess = tf.Session() data_loader = DataLoader(config=config) model = ClassifierModel(data_loader=data_loader, config=config) trainer = ClassifierTrainer(sess=sess, model=model, config=config, logger=None, dataLoader=data_loader) trainer.train() return