def classification_baseline_exp(device='2',
                                lr=1e-3,
                                n_epochs=300,
                                batch_size=128,
                                log_interval=50):

    os.environ['CUDA_VISIBLE_DEVICES'] = str(device)

    standarizer = TaskbStandarizer(data_manager=Dcase18TaskbData())
    mu, sigma = standarizer.load_mu_sigma(mode='train', device='a')

    train_dataset = DevSet(mode='train',
                           device='a',
                           transform=Compose(
                               [Normalize(mean=mu, std=sigma),
                                ToTensor()]))
    test_dataset = DevSet(mode='test',
                          device='a',
                          transform=Compose(
                              [Normalize(mean=mu, std=sigma),
                               ToTensor()]))

    train_loader = DataLoader(dataset=train_dataset,
                              batch_size=batch_size,
                              shuffle=True,
                              num_workers=1)
    test_loader = DataLoader(dataset=test_dataset,
                             batch_size=batch_size,
                             shuffle=False,
                             num_workers=1)

    model = vggish_bn()
    model = model.cuda()

    loss_fn = torch.nn.CrossEntropyLoss()
    optimizer = optim.Adam(model.parameters(), lr=lr)
    scheduler = lr_scheduler.StepLR(optimizer=optimizer,
                                    step_size=30,
                                    gamma=0.5,
                                    last_epoch=-1)

    fit(train_loader=train_loader,
        val_loader=test_loader,
        model=model,
        loss_fn=loss_fn,
        optimizer=optimizer,
        scheduler=scheduler,
        n_epochs=n_epochs,
        log_interval=log_interval,
        metrics=[AccumulatedAccuracyMetric()])
lr = 1e-2
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(train_loader,
    test_loader,
    model,
    loss_fn,
    optimizer,
    scheduler,
    n_epochs,
    cuda,
    log_interval,
    metrics=[AccumulatedAccuracyMetric()])

train_embeddings_baseline, train_labels_baseline = extract_embeddings(
    train_loader, model)
plot_embeddings(train_embeddings_baseline, train_labels_baseline)
val_embeddings_baseline, val_labels_baseline = extract_embeddings(
    test_loader, model)
plot_embeddings(val_embeddings_baseline, val_labels_baseline)

# Set up data loaders
from datasets import SiameseMNIST

siamese_train_dataset = SiameseMNIST(
    train_dataset)  # Returns pairs of images and target same/different
siamese_test_dataset = SiameseMNIST(test_dataset)
batch_size = 128
示例#3
0
def fit(train_loader, val_loader, model, loss_fn, optimizer, n_epochs, cuda, train_mode, metrics = [AccumulatedAccuracyMetric()]):

    """
    Loaders, model, loss function and metrics should work together for a given task,
    i.e. The model should be able to process data output of loaders,
    loss function should process target output of loaders and outputs from the model
    
    Examples: Classification: batch loader, classification model, NLL loss, accuracy metric
    Siamese network: Siamese loader, siamese model, contrastive loss
    Online triplet learning: batch loader, embedding model, online triplet loss
    """

    if train_mode:
        train(train_loader, model, loss_fn, optimizer, n_epochs, cuda, metrics)
    else:
        test(val_loader, model, loss_fn, cuda, metrics)
示例#4
0
def classification_baseline_exp(device='2',
                                ckpt_prefix='Run01',
                                lr=1e-3,
                                n_epochs=300,
                                batch_size=128,
                                log_interval=50,
                                classify=True,
                                log_level='INFO'):

    kwargs = locals()
    log_file = '{}/ckpt/classification_exp/{}.log'.format(
        ROOT_DIR, ckpt_prefix)
    if not os.path.exists(os.path.dirname(log_file)):
        os.makedirs(os.path.dirname(log_file))
    logging.basicConfig(filename=log_file,
                        level=getattr(logging, log_level.upper(), None))
    logging.info(str(kwargs))

    os.environ['CUDA_VISIBLE_DEVICES'] = str(device)

    standarizer = TaskbStandarizer(data_manager=Dcase18TaskbData())
    mu, sigma = standarizer.load_mu_sigma(mode='train', device='a')

    train_dataset = DevSet(mode='train',
                           device='a',
                           transform=Compose(
                               [Normalize(mean=mu, std=sigma),
                                ToTensor()]))
    test_dataset = DevSet(mode='test',
                          device='a',
                          transform=Compose(
                              [Normalize(mean=mu, std=sigma),
                               ToTensor()]))

    train_loader = DataLoader(dataset=train_dataset,
                              batch_size=batch_size,
                              shuffle=True,
                              num_workers=1)
    test_loader = DataLoader(dataset=test_dataset,
                             batch_size=batch_size,
                             shuffle=False,
                             num_workers=1)

    model = vggish_bn(classify)
    model = model.cuda()

    loss_fn = torch.nn.CrossEntropyLoss()
    optimizer = optim.Adam(model.parameters(), lr=lr)
    scheduler = lr_scheduler.StepLR(optimizer=optimizer,
                                    step_size=30,
                                    gamma=0.5,
                                    last_epoch=-1)

    train_hist = History(name='train/a')
    val_hist = History(name='val/a')
    ckpter = CheckPoint(model=model,
                        optimizer=optimizer,
                        path='{}/ckpt/classification_exp'.format(ckpt_prefix),
                        prefix=ckpt_prefix,
                        interval=1,
                        save_num=1)

    fit(train_loader=train_loader,
        val_loader=test_loader,
        model=model,
        loss_fn=loss_fn,
        optimizer=optimizer,
        scheduler=scheduler,
        n_epochs=n_epochs,
        log_interval=log_interval,
        metrics=[AccumulatedAccuracyMetric()],
        train_hist=train_hist,
        val_hist=val_hist,
        ckpter=ckpter,
        logging=logging)
示例#5
0
def visualize_evolution(train_dataset,
                        test_dataset,
                        dataset_wraper,
                        projection_layers,
                        model,
                        loss_fn,
                        n_epochs=10,
                        n_classes=10):
    # Set up data loaders
    batch_size = 256
    kwargs = {'num_workers': 1, 'pin_memory': True} if cuda else {}

    if dataset_wraper is not None:
        vis_train_loader = torch.utils.data.DataLoader(train_dataset,
                                                       batch_size=batch_size,
                                                       shuffle=True,
                                                       **kwargs)
        vis_test_loader = torch.utils.data.DataLoader(test_dataset,
                                                      batch_size=batch_size,
                                                      shuffle=False,
                                                      **kwargs)
        train_dataset = dataset_wraper(train_dataset)
        test_dataset = dataset_wraper(test_dataset)

    train_loader = torch.utils.data.DataLoader(train_dataset,
                                               batch_size=batch_size,
                                               shuffle=True,
                                               **kwargs)
    test_loader = torch.utils.data.DataLoader(test_dataset,
                                              batch_size=batch_size,
                                              shuffle=False,
                                              **kwargs)
    if dataset_wraper is None:
        vis_train_loader, vis_test_loader = train_loader, test_loader

    embedding_net = EmbeddingNet(projection_layers=projection_layers)
    if model == ClassificationNet:
        model = model(embedding_net, n_classes=n_classes)
    else:
        model = model(embedding_net)
    if cuda:
        model.cuda()
    lr = 1e-2
    optimizer = optim.Adam(model.parameters(), lr=lr)
    scheduler = lr_scheduler.StepLR(optimizer, 8, gamma=0.1, last_epoch=-1)
    log_interval = 50
    metrics = []
    if type(model) is ClassificationNet:
        metrics = [AccumulatedAccuracyMetric()]

    embeddings, labels, titles = [], [], []
    for step in range(n_epochs):
        val_embeddings_baseline, val_labels_baseline, val_variances = extract_embeddings(
            vis_test_loader, model)
        embeddings.append(val_embeddings_baseline)
        labels.append(val_labels_baseline)
        title = 'step {}: train'.format(step)
        if val_variances is not None:
            title += ' (variances explained: {})'.format(val_variances)
        titles.append(title)
        stats = fit(train_loader,
                    test_loader,
                    model,
                    loss_fn,
                    optimizer,
                    scheduler,
                    1,
                    cuda,
                    log_interval,
                    metrics=metrics)
    plot_embeddings(embeddings,
                    labels,
                    titles=titles,
                    subplot_rows=math.ceil(n_epochs / 2),
                    subplot_cols=2,
                    figsize=[40, 10 * n_epochs])
示例#6
0
def train_and_visualize(train_dataset,
                        test_dataset,
                        dataset_wraper,
                        projection_layers,
                        model,
                        loss_fn,
                        n_epochs=10,
                        n_classes=10,
                        use_bn=False):
    # Set up data loaders
    batch_size = 256
    kwargs = {'num_workers': 1, 'pin_memory': True} if cuda else {}

    if dataset_wraper is not None:
        vis_train_loader = torch.utils.data.DataLoader(train_dataset,
                                                       batch_size=batch_size,
                                                       shuffle=True,
                                                       **kwargs)
        vis_test_loader = torch.utils.data.DataLoader(test_dataset,
                                                      batch_size=batch_size,
                                                      shuffle=False,
                                                      **kwargs)
        train_dataset = dataset_wraper(train_dataset)
        test_dataset = dataset_wraper(test_dataset)

    train_loader = torch.utils.data.DataLoader(train_dataset,
                                               batch_size=batch_size,
                                               shuffle=True,
                                               **kwargs)
    test_loader = torch.utils.data.DataLoader(test_dataset,
                                              batch_size=batch_size,
                                              shuffle=False,
                                              **kwargs)
    if dataset_wraper is None:
        vis_train_loader, vis_test_loader = train_loader, test_loader

    embedding_net = EmbeddingNet(projection_layers=projection_layers,
                                 use_bn=use_bn)
    if model == ClassificationNet:
        model = model(embedding_net, n_classes=n_classes)
    else:
        model = model(embedding_net)
    print(model)
    if cuda:
        model.cuda()
    lr = 1e-2
    optimizer = optim.Adam(model.parameters(), lr=lr)
    scheduler = lr_scheduler.StepLR(optimizer, 8, gamma=0.1, last_epoch=-1)
    log_interval = 50

    metrics = []
    if type(model) is ClassificationNet:
        metrics = [AccumulatedAccuracyMetric()]

    stats = fit(train_loader,
                test_loader,
                model,
                loss_fn,
                optimizer,
                scheduler,
                n_epochs,
                cuda,
                log_interval,
                metrics=metrics)
    plot_learning_curves(stats)
    train_embeddings_baseline, train_labels_baseline, train_variances = extract_embeddings(
        vis_train_loader, model)
    val_embeddings_baseline, val_labels_baseline, val_variances = extract_embeddings(
        vis_test_loader, model)
    titles = [
        'train (variances explained: {})'.format(train_variances),
        'valid (variances explained: {})'.format(val_variances)
    ]
    plot_embeddings([train_embeddings_baseline, val_embeddings_baseline],
                    [train_labels_baseline, val_labels_baseline],
                    titles=titles)
示例#7
0
#if 2 GPUS
# model.module.embedding_net.load_state_dict(torch.load('/home/oleksii/JUPYTER_SHARED/PROJECTS/Image_similarity/siamese-triplet/weights/weights_embedding_net_12_(copy).pt'))

#if 1 GPU
model.embedding_net.load_state_dict(torch.load('/home/oleksii/JUPYTER_SHARED/PROJECTS/Image_similarity/siamese-triplet/weights/SiameseNet_01.pt'))

if cuda:
    model.cuda()

# Freeze pre-trained layers for embedding extraction
# for layer in model.module.embedding_net.parameters():
#     layer.requires_grad = False

# for layer_n, layer in enumerate(model.module.embedding_net.parameters()): # if 2 GPUS
for layer_n, layer in enumerate(model.embedding_net.parameters()):
    # model.module.embedding_net.parameters() # if 2 GPUS
    model.embedding_net.parameters() # if 1 GPU
    model.embedding_net.parameters()

    if layer_n < 155:
        layer.requires_grad = False

loss_fn = nn.BCELoss() 
lr = 0.5e-3
optimizer = optim.Adam(model.parameters(), lr=lr)
scheduler = lr_scheduler.StepLR(optimizer, 8, gamma=0.5, last_epoch=-1)
n_epochs = 100
log_interval = 1000

fit(train_loader, val_loader, model, loss_fn, optimizer, scheduler, n_epochs, cuda, log_interval, './weights', metrics=[AccumulatedAccuracyMetric()])
示例#8
0
n_items = 7981

embed_dim = 2
embedding_net = EmbeddingNet(embed_dim)
model = ClassificationNet(embedding_net, n_classes=n_items)
if cuda:
    model.cuda()
loss_fn = torch.nn.NLLLoss()
lr = 1e-2
optimizer = optim.Adam(model.parameters(), lr=lr)
scheduler = lr_scheduler.StepLR(optimizer, 8, gamma=0.1, last_epoch=-1)
# n_epochs = 20
n_epochs = 20
log_interval = 1

if not resume_from_pth:
    record_history = fit(train_loader, test_loader, model, loss_fn, optimizer, scheduler, n_epochs, cuda, log_interval, metrics=[AccumulatedAccuracyMetric()])
    plot_history(experiment_folder, record_history)

    torch.save(model.state_dict(), trained_weight_file)

model.load_state_dict(torch.load(trained_weight_file))

train_embeddings_baseline, train_labels_baseline = extract_embeddings(train_loader, model)
plot_embeddings(experiment_folder, 'train', train_embeddings_baseline, train_labels_baseline)
val_embeddings_baseline, val_labels_baseline = extract_embeddings(test_loader, model)
plot_embeddings(experiment_folder, 'test', val_embeddings_baseline, val_labels_baseline)

pass

test_dataset = torchvision.datasets.ImageFolder('/home/wdxia/Finger_ROI_Database/GRG_3852_split_valid',
                                                  transform=transforms.Compose([transforms.Resize((32, 32)), transforms.Grayscale(),
                                                      transforms.ToTensor()
                                                  ]))
batch_size = 32
kwargs = {'num_workers': 1, 'pin_memory': True} if cuda else {}
train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=batch_size, shuffle=True, **kwargs)
test_loader = torch.utils.data.DataLoader(test_dataset, batch_size=batch_size, shuffle=False, **kwargs)

# Set up the network and training parameters

embedding_net = HardNet()
model = ClassificationNet(embedding_net, n_classes=n_classes)
print model
if cuda:
    model.cuda()
loss_fn = torch.nn.NLLLoss()  # 输入由LOG_SOFTMAX计算出来的属于每一类的概率,输出损失
lr = 1e-2
optimizer = optim.Adam(model.parameters(), lr=lr)

''' optimizer (Optimizer): Wrapped optimizer.
        step_size (int): Period of learning rate decay.
        gamma (float): Multiplicative factor of learning rate decay.
            Default: 0.1.
        last_epoch (int): The index of last epoch. Default: -1.'''
scheduler = lr_scheduler.StepLR(optimizer, 8, gamma=0.1, last_epoch=-1)
n_epochs = 100
log_interval = len(train_loader)//batch_size   # 隔多少个batch记录一次

fit(train_loader, test_loader, model, loss_fn, optimizer, scheduler, n_epochs, cuda, log_interval, metrics=[AccumulatedAccuracyMetric()],log_file_path = 'hardnet_softmax_log.txt')