def __init__( self, items: Iterator, classes: Collection = None, label_delim: str = None, **kwargs, ): super().__init__(items, classes=classes, **kwargs) self.loss_func = OnlineContrastiveLoss(1.0, HardNegativePairSelector())
def siamese_embedding_learner(data, pretrained_model_class, emsize=128, margin=1.0, callback_fns=None): meta = cnn_config(pretrained_model_class) model = create_cnn_model(pretrained_model_class, emsize) learn = Learner( data, model, loss_func=OnlineContrastiveLoss(margin, HardNegativePairSelector()), callback_fns=callback_fns, ) learn.split(meta["split"]) apply_init(model[1], nn.init.kaiming_normal_) return learn
online_train_loader = torch.utils.data.DataLoader( 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 OnlineContrastiveLoss from utils import AllPositivePairSelector, HardNegativePairSelector # Strategies for selecting pairs within a minibatch margin = 1. embedding_net = EmbeddingNet() model = embedding_net if cuda: model.cuda() loss_fn = OnlineContrastiveLoss(margin, HardNegativePairSelector()) lr = 1e-3 optimizer = optim.Adam(model.parameters(), lr=lr) 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) train_embeddings_ocl, train_labels_ocl = extract_embeddings( train_loader, model) plot_embeddings(train_embeddings_ocl, train_labels_ocl) val_embeddings_ocl, val_labels_ocl = extract_embeddings(test_loader, model) plot_embeddings(val_embeddings_ocl, val_labels_ocl)
def main(): # print('fsafsdaf:', args.training_dataset, args.arch) print(">> Creating directory if it does not exist:\n>> '{}'".format( args.directory)) if not os.path.exists(args.directory): os.makedirs(args.directory) log_dir = os.path.join(args.directory, 'log') if not os.path.exists(log_dir): os.mkdir(log_dir) params = {'architecture': args.arch, 'pooling': args.pool} n_classes = args.n_classes n_samples = args.n_samples cuda = args.cuda input_size = args.image_size transform, transform_te, transform_label = init_transform(input_size) kwargs = {'num_workers': 1, 'pin_memory': True} if cuda else {} online_train_loader, online_test_loader = init_data_loader( args.root, n_classes, n_samples, transform, transform_te, transform_label, kwargs) # Set up the network and training parameters model = init_network(params) parameters = [] # add feature parameters parameters.append({'params': model.features.parameters()}) if cuda: # print('model cuda:', cuda) model.cuda() pos_margin = 1.0 neg_margin = 0.3 # define optimizer if args.optimizer == 'sgd': optimizer = torch.optim.SGD(parameters, args.lr, momentum=args.momentum, weight_decay=args.weight_decay) elif args.optimizer == 'adam': optimizer = torch.optim.Adam(parameters, args.lr, weight_decay=args.weight_decay) metrics = [AverageNonzeroTripletsMetric()] if args.loss.startswith('OnlineContrastiveEucLoss'): loss_fn = OnlineContrastiveEucLoss(pos_margin, neg_margin, HardNegativePairSelector()) elif args.loss.startswith('OnlineContrastiveCosLoss'): loss_fn = OnlineContrastiveCosLoss(args.loss_margin) elif args.loss.startswith('OnlineTriplet'): loss_fn = OnlineTripletLoss( args.loss_margin, HardestNegativeTripletSelector(args.loss_margin)) exp_decay = math.exp(-0.01) scheduler = torch.optim.lr_scheduler.ExponentialLR(optimizer, gamma=exp_decay) writer = SummaryWriter(log_dir=log_dir) writer.add_graph(model.features, torch.ones([1, 3, 224, 224]).cuda()) fit(online_train_loader, online_test_loader, model, loss_fn, optimizer, scheduler, writer, metrics=metrics, args=args)