def main(): cuda = torch.cuda.is_available() anchor_transform = transforms.Compose([ transforms.RandomAffine(degrees=90, translate=(0.25, 0.25)), transforms.RandomHorizontalFlip(), transforms.RandomVerticalFlip(), transforms.CenterCrop(128), transforms.Resize(IMG_SIZE), transforms.ToTensor(), transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]) ]) train_transforms = transforms.Compose([ transforms.Resize(IMG_SIZE), transforms.ToTensor(), transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]) ]) # Let's use 12 while developing as it reduces the start time. dset_train = GeoTileDataset(TILE_FILE, transform=train_transforms, center_transform=anchor_transform) pd_files = dset_train.get_file_df() weights = pd_files.frequency train_sampler = WeightedRandomSampler(weights, len(dset_train)) # Should numworkers be 1? kwargs = {'num_workers': 8, 'pin_memory': True} if cuda else {} online_train_loader = DataLoader(dset_train, batch_size=BATCH_SIZE, sampler=train_sampler, **kwargs) model = Loc2Vec() if cuda: model.cuda() loss_fn = OnlineTripletLoss(MARGIN, HardestNegativeTripletSelector(MARGIN), SemihardNegativeTripletSelector(MARGIN), RandomNegativeTripletSelector(MARGIN)) optimizer = optim.Adam(model.parameters(), lr=1e-3) scheduler = lr_scheduler.StepLR(optimizer, 16, gamma=0.1, last_epoch=-1) # Mixed precision training model, optimizer = amp.initialize(model, optimizer, opt_level="O1") # if torch.cuda.device_count() > 1: # print("Let's use", torch.cuda.device_count(), "GPUs!") # model = nn.DataParallel(model) fit(online_train_loader, online_train_loader, model, loss_fn, optimizer, scheduler, N_EPOCHS, cuda, LOG_INTERVAL)
trainLoader = torch.utils.data.DataLoader(dataset = trainDataset, batch_size=mb_size, shuffle=False, num_workers=1, sampler = sampler) n_sampE, IE_dim = X_trainE.shape h_dim = hdm Z_in = h_dim marg = mrg lrE = lre epoch = epch costtr = [] auctr = [] costts = [] aucts = [] triplet_selector = RandomNegativeTripletSelector(marg) triplet_selector2 = AllTripletSelector() class AEE(nn.Module): def __init__(self): super(AEE, self).__init__() self.EnE = torch.nn.Sequential( nn.Linear(IE_dim, h_dim), nn.BatchNorm1d(h_dim), nn.ReLU(), nn.Dropout()) def forward(self, x): output = self.EnE(x) return output class OnlineTriplet(nn.Module):
train_dataset, batch_sampler=train_batch_sampler, **kwargs) online_test_loader = torch.utils.data.DataLoader( test_dataset, batch_sampler=test_batch_sampler, **kwargs) # Set up the network and training parameters from networks import EmbeddingNet from losses import OnlineTripletLoss from utils import AllTripletSelector, HardestNegativeTripletSelector, RandomNegativeTripletSelector, SemihardNegativeTripletSelector # Strategies for selecting triplets within a minibatch from metrics import AverageNonzeroTripletsMetric margin = 1. embedding_net = EmbeddingNet() model = embedding_net if cuda: model.cuda() loss_fn = OnlineTripletLoss(margin, RandomNegativeTripletSelector(margin)) lr = 1e-3 optimizer = optim.Adam(model.parameters(), lr=lr, weight_decay=1e-4) scheduler = lr_scheduler.StepLR(optimizer, 8, gamma=0.1, last_epoch=-1) n_epochs = 20 log_interval = 50 fit(online_train_loader, online_test_loader, model, loss_fn, optimizer, scheduler, n_epochs, cuda, log_interval,
def main(): torch.manual_seed(args.seed) #设置随机种子 train_dataset = SpeakerTrainDataset() #设置训练集读取 n_classes = train_dataset.n_classes #说话人数 batch_sampler = BalancedBatchSampler(train_dataset.labels, train_dataset.count, 10, 10) print('Num of classes: {}'.format(n_classes)) model = ResNet(layers=[1, 1, 1], embedding_size=args.embedding_size, n_classes=n_classes) model.to(device) if args.optimizer == 'sgd': #优化器使用sgd optimizer = torch.optim.SGD(model.parameters(), lr=args.lr, momentum=args.momentum, dampening=args.dampening, weight_decay=args.wd) elif args.optimizer == 'adagrad': #优化器使用adagrad optimizer = torch.optim.Adagrad(model.parameters(), lr=args.lr, lr_decay=args.lr_decay, weight_decay=args.wd) else: #优化器使用adam optimizer = torch.optim.Adam(model.parameters(), lr=args.lr, weight_decay=args.wd) selector = RandomNegativeTripletSelector(args.m) criterion = OnlineTripletLoss(args.m, selector) start = 1 if args.resume: #是否从之前保存的模型开始 if os.path.isfile(args.resume): print('=> loading checkpoint {}'.format(args.resume)) checkpoint = torch.load(args.resume) if args.start is not None: start = start else: start = checkpoint['epoch'] + 1 if args.load_optimizer: optimizer.load_state_dict(checkpoint['optimizer']) model.load_state_dict(checkpoint['state_dict']) else: print('=> no checkpoint found at {}'.format(args.resume)) train_loader = DataLoader(train_dataset, batch_sampler=batch_sampler, num_workers=8, pin_memory=True) test_dataset = SpeakerTestDataset(transform=args.transform) test_loader = DataLoader(test_dataset, batch_size=args.test_batch_size, shuffle=False, num_workers=8, pin_memory=True) for epoch in range(start, args.epochs + 1): train(epoch, model, criterion, optimizer, train_loader) test(model, test_loader) #测试 task = pd.read_csv('task/task.csv', header=None, delimiter='[ ]', engine='python') pred = pd.read_csv(args.final_dir + 'pred.csv', engine='python') y_true = np.array(task.iloc[:, 0]) y_pred = np.array(pred.iloc[:, -1]) eer, thresh = cal_eer(y_true, y_pred) print('\nEER : {:.3%}'.format(eer)) print('Threshold: {:.5f}'.format(thresh))
def main(): torch.manual_seed(seed) # 设置随机种子 train_dataset = SpeakerTrainDataset() # 设置训练集读取 n_classes = train_dataset.n_classes # 说话人数 batch_sampler = BalancedBatchSampler(train_dataset.labels, train_dataset.count, spks_per_batch, utts_per_spk) print('Num of classes: {}'.format(n_classes)) encoder = Encoder(expansion, blocks, embedding_dim).to(device) if optimizer == 'sgd': # 优化器使用sgd encoder_optimizer = optim.SGD(encoder.parameters(), lr=lr, momentum=momentum, weight_decay=weight_decay) elif optimizer == 'adagrad': # 优化器使用adagrad encoder_optimizer = optim.Adagrad(encoder.parameters(), lr=lr, weight_decay=weight_decay) else: # 优化器使用adam encoder_optimizer = optim.Adam(encoder.parameters(), lr=lr, weight_decay=weight_decay) selector = RandomNegativeTripletSelector(margin) triplet_criterion = OnlineTripletLoss(margin, selector).to(device) start = 1 metric = AverageNonzeroTripletsMetric() if resume: # 是否从之前保存的模型开始 if os.path.isfile(os.path.join(final_dir, "net.pth")): print('=> loading checkpoint {}'.format( os.path.join(final_dir, "net.pth"))) checkpoint = torch.load(os.path.join(final_dir, "net.pth")) start = checkpoint['epoch'] + 1 if load_optimizer: encoder_optimizer.load_state_dict( checkpoint['encoder_optimizer']) encoder.load_state_dict(checkpoint['encoder_state_dict']) else: print('=> no checkpoint found at {}'.format( os.path.join(final_dir, "net.pth"))) train_loader = DataLoader(train_dataset, batch_sampler=batch_sampler, num_workers=8, pin_memory=True) test_dataset = SpeakerTestDataset() test_loader = DataLoader(test_dataset, batch_size=1, shuffle=False, num_workers=8, pin_memory=True) for epoch in range(start, epochs + 1): fake_fbank = train(epoch, encoder, triplet_criterion, encoder_optimizer, train_loader, metric) test(encoder, test_loader) # 测试 task = pd.read_csv(TRIAL_FILE, header=None, delimiter='[,]', engine='python') pred = pd.read_csv(final_dir + prediction, engine='python') y_true = np.array(task.iloc[:, 0]) y_pred = np.array(pred.iloc[:, -1]) eer, thresh = cal_eer(y_true, y_pred) print('EER : {:.3%}'.format(eer)) print('Threshold: {:.5f}'.format(thresh)) logger.log_value('eer', eer)
if cuda: model.cuda() if args.classify: loss_fn = torch.nn.CrossEntropyLoss() else: margin = args.margin if args.triplets == 'batch-hard': loss_fn = OnlineTripletLoss(margin, HardestNegativeTripletSelector(margin)) if args.triplets == 'semi-hard': loss_fn = OnlineTripletLoss( margin, SemihardNegativeTripletSelector(margin)) if args.triplets == 'random-negative': loss_fn = OnlineTripletLoss(margin, RandomNegativeTripletSelector(margin)) optimizer = optim.Adam(model.parameters(), lr=args.lr, weight_decay=args.decay) scheduler = lr_scheduler.StepLR(optimizer, 8, gamma=0.1, last_epoch=-1) n_epochs = args.epochs log_interval = args.log if args.checkpoint: checkpoint = torch.load('checkpoint.pth.tar') model.load_state_dict(checkpoint['model_state_dict']) optimizer.load_state_dict(checkpoint['optimizer_state_dict']) scheduler.load_state_dict(checkpoint['scheduler_state_dict']) epoch = checkpoint['epoch'] print("Restarting training from checkpoint...") if args.classify:
def main(): torch.manual_seed(seed) # 设置随机种子 train_dataset = SpeakerTrainDataset() # 设置训练集读取 n_classes = train_dataset.n_classes # 说话人数 batch_sampler = BalancedBatchSampler(train_dataset.labels, train_dataset.count, spks_per_batch, utts_per_spk) print('Num of classes: {}'.format(n_classes)) encoder = Encoder(expansion, blocks, embedding_dim).to(device) generator = Generator(expansion, blocks, embedding_dim, FEATURE_LEN, latent_dim).to(device) discriminator = Discriminator(expansion, blocks, embedding_dim).to(device) classifier = Classifier(expansion, blocks, n_classes).to(device) if optimizer == 'sgd': # 优化器使用sgd generator_optimizer = optim.SGD([{ 'params': generator.parameters() }, { 'params': encoder.parameters() }, { 'params': discriminator.parameters() }], lr=lr, momentum=momentum, weight_decay=weight_decay) discriminator_optimizer = optim.SGD( [{ 'params': discriminator.parameters() }, { 'params': classifier.parameters() }], lr=lr, momentum=momentum, weight_decay=weight_decay) elif optimizer == 'adagrad': # 优化器使用adagrad generator_optimizer = optim.Adagrad( [{ 'params': generator.parameters() }, { 'params': encoder.parameters() }, { 'params': discriminator.parameters() }], lr=lr, weight_decay=weight_decay) discriminator_optimizer = optim.Adagrad( [{ 'params': discriminator.parameters() }, { 'params': classifier.parameters() }], lr=lr, weight_decay=weight_decay) else: # 优化器使用adam generator_optimizer = optim.Adam([{ 'params': generator.parameters() }, { 'params': encoder.parameters() }, { 'params': discriminator.parameters() }], lr=lr, weight_decay=weight_decay) discriminator_optimizer = optim.Adam( [{ 'params': discriminator.parameters() }, { 'params': classifier.parameters() }], lr=lr, weight_decay=weight_decay) gen_scheduler = lr_scheduler.StepLR(generator_optimizer, 200, gamma=0.1, last_epoch=-1) dis_scheduler = lr_scheduler.StepLR(discriminator_optimizer, 200, gamma=0.1, last_epoch=-1) selector = RandomNegativeTripletSelector(margin) triplet_criterion = OnlineTripletLoss(margin, selector).to(device) bce_criterion = nn.BCELoss().to(device) softmax_criterion = nn.CrossEntropyLoss().to(device) start = 1 metric = AverageNonzeroTripletsMetric() if resume: # 是否从之前保存的模型开始 if os.path.isfile(os.path.join(final_dir, "net.pth")): print('=> loading checkpoint {}'.format( os.path.join(final_dir, "net.pth"))) checkpoint = torch.load(os.path.join(final_dir, "net.pth")) start = checkpoint['epoch'] + 1 if load_optimizer: generator_optimizer.load_state_dict( checkpoint['generator_optimizer']) discriminator_optimizer.load_state_dict( checkpoint['discriminator_optimizer']) generator.load_state_dict(checkpoint['generator_state_dict']) discriminator.load_state_dict( checkpoint['discriminator_state_dict']) else: print('=> no checkpoint found at {}'.format( os.path.join(final_dir, "net.pth"))) train_loader = DataLoader(train_dataset, batch_sampler=batch_sampler, num_workers=8, pin_memory=True) test_dataset = SpeakerTestDataset() test_loader = DataLoader(test_dataset, batch_size=1, shuffle=False, num_workers=8, pin_memory=True) for epoch in range(start, epochs + 1): real_data, fake_fbank = train(epoch, encoder, generator, discriminator, classifier, triplet_criterion, bce_criterion, softmax_criterion, generator_optimizer, discriminator_optimizer, train_loader, metric) save_image(torch.Tensor(random.sample(real_data.tolist(), 4)), 'real_fbank/{}.png'.format(epoch), nrow=2, normalize=True) save_image(torch.Tensor(random.sample(fake_fbank.tolist(), 4)), 'fake_fbank/{}.png'.format(epoch), nrow=2, normalize=True) test(encoder, test_loader) # 测试 gen_scheduler.step() dis_scheduler.step() task = pd.read_csv(TRIAL_FILE, header=None, delimiter='[,]', engine='python') pred = pd.read_csv(final_dir + prediction, engine='python') y_true = np.array(task.iloc[:, 0]) y_pred = np.array(pred.iloc[:, -1]) eer, thresh = cal_eer(y_true, y_pred) logger.log_value('eer', eer, epoch) print('EER : {:.3%}'.format(eer)) print('Threshold: {:.5f}'.format(thresh))
def main(): args = get_args() logdir = 'log/{}-emb{}-{}layers-{}resblk-lr{}-wd{}-maxlen{}-alpha10-margin{}'\ '{}class-{}sample-{}selector'\ .format(args.name, args.embedding_size, args.layers, args.resblk, args.lr, args.wd, args.maxlen, args.margin, args.n_classes, args.n_samples, args.selection) if not os.path.exists(logdir): os.makedirs(logdir) resblock = [] for i in range(args.layers): resblock.append(args.resblk) if args.train: logger = Logger(logdir) if not os.path.exists(args.trainfeature): os.mkdir(args.trainfeature) extractFeature(args.training-dataset, args.trainfeature) trainset = DeepSpkDataset(args.trainfeature, args.maxlen) pre_loader = DataLoader(trainset, batch_size = 128, shuffle = True, num_workers = 8) train_batch_sampler = BalancedBatchSampler(trainset.train_labels, n_classes = args.n_classes, n_samples = args.n_samples) kwargs = {'num_workers' : 1, 'pin_memory' : True} online_train_loader = torch.utils.data.DataLoader(trainset, batch_sampler=train_batch_sampler, **kwargs) margin = args.margin embedding_net = EmbeddingNet(resblock, embedding_size = args.embedding_size, layers = args.layers) model = DeepSpeaker(embedding_net, trainset.get_num_class()) device = torch.device('cuda:0') model.to(device) # 要在初始化optimizer之前把model转换到GPU上,这样初始化optimizer的时候也是在GPU上 optimizer = optim.SGD(model.embedding_net.parameters(), lr = args.lr, momentum = 0.99, weight_decay = args.wd) start_epoch = 0 if args.resume: if os.path.isfile(args.resume): print('=> loading checkpoint {}'.format(args.resume)) checkpoint = torch.load(args.resume) start_epoch = checkpoint['epoch'] model.embedding_net.load_state_dict(checkpoint['state_dict']) optimizer.load_state_dict(checkpoint['optimizer']) else: print('=> no checkpoint found at {}'.format(args.resume)) pretrain_epoch = args.pretrain_epoch if args.selection == 'randomhard': selector = RandomNegativeTripletSelector(margin) if args.selection == 'hardest': selector = HardestNegativeTripletSelector(margin) if args.selection == 'semihard': selector = SemihardNegativeTripletSelector(margin) if args.selection == 'all': print('warning : select all triplet may take very long time') selector = AllTripletSelector() loss_fn = OnlineTripletLoss(margin, selector) scheduler = lr_scheduler.StepLR(optimizer, step_size = args.lr_adjust_step, gamma = args.lr_decay, last_epoch = -1) n_epochs = args.n_epochs log_interval = 50 fit(online_train_loader, pre_loader, model, loss_fn, optimizer, scheduler, pretrain_epoch, n_epochs, True, device, log_interval, log_dir = logdir, eval_path = args.evalfeature, logger = logger, metrics = [AverageNonzeroTripletsMetric()], evaluatee = args.eval, start_epoch = start_epoch) else: if not os.path.exists(args.testfeature): os.mkdir(args.testfeature) extractFeature(args.test-dataset, args.testfeature) model = EmbeddingNet(resblock, embedding_size = args.embedding_size, layers = args.layers) model.cpu() if args.model: if os.path.isfile(args.model): print('=> loading checkpoint {}'.format(args.model)) checkpoint = torch.load(args.model) model.load_state_dict(checkpoint['state_dict']) else: print('=> no checkpoint found at {}'.format(args.model)) thres = np.loadtxt(logdir + '/thres.txt') acc = np.loadtxt(logdir + '/acc.txt') idx = np.argmax(acc) best_thres = thres[idx] predict(model, args.testfeature, best_thres)
def main(args): if os.path.exists('models') is False: os.makedirs('models') # img_list, base_path, item_dict = read_data(args.dataset, args.bbox) img_list, base_path, item_dict = read_data("DeepFashion2", bbox_gt=False) model_save_path = args.model_path # 'models/siames_triplet_df2.pth' # writer = SummaryWriter('runs/fashion_mnist_experiment_1') model = ResNetbasedNet() if os.path.exists(model_save_path): model.load_state_dict(torch.load(model_save_path)) cuda = torch.cuda.is_available() device = torch.device("cuda" if torch.cuda.is_available() else "cpu") if cuda: if torch.cuda.device_count() > 1: model = nn.DataParallel(model) model.to(device) kwargs = {'num_workers': 8, 'pin_memory': True} if cuda else {} if not args.phase: train_dataset = DeepFashionDataset(img_list['train'], root=base_path) train_batch_sampler = BalancedBatchSampler(train_dataset.labels, train_dataset.source, n_classes=32, n_samples=4) online_train_loader = torch.utils.data.DataLoader( train_dataset, batch_sampler=train_batch_sampler, **kwargs) test_dataset = DeepFashionDataset(img_list['validation'], root=base_path) test_batch_sampler = BalancedBatchSampler(test_dataset.labels, test_dataset.source, n_classes=32, n_samples=4) online_test_loader = torch.utils.data.DataLoader( test_dataset, batch_sampler=test_batch_sampler, **kwargs) margin = 1. loss_fn = OnlineTripletLoss(margin, RandomNegativeTripletSelector(margin)) lr = 1e-3 optimizer = optim.Adam(model.parameters(), lr=lr, weight_decay=1e-4) scheduler = lr_scheduler.StepLR(optimizer, 5, gamma=0.1, last_epoch=-1) n_epochs = 20 log_interval = 200 fit(online_train_loader, online_test_loader, model, loss_fn, optimizer, scheduler, n_epochs, cuda, log_interval, model_save_path, metrics=[AverageNonzeroTripletsMetric()]) else: model.eval() gallery_dataset = DeepFashionDataset(img_list['validation'], root=base_path) acc, query_cnt = get_topK_acc(gallery_dataset, item_dict['validation'], model, cuda, open('retrieval_result.txt', 'a'), 100) np.savetxt('TopK_accuracy.txt', np.concatenate((acc, np.asarray([query_cnt]))), fmt='%1.5f', delimiter=',')