Esempio n. 1
0
def predict(prediction_dir='./Test'):
    """The function is used to run predictions on the audio files in the directory `pred_directory`.

    Parameters
    ----------
    net:
        The model that has been trained.
    prediction_dir: string, default ./Test
        The directory that contains the audio files on which predictions are to be made

    """

    if not os.path.exists(prediction_dir):
        warnings.warn("The directory on which predictions are to be made is not found!")
        return

    if len(os.listdir(prediction_dir)) == 0:
        warnings.warn("The directory on which predictions are to be made is empty! Exiting...")
        return

    # Loading synsets
    if not os.path.exists('./synset.txt'):
        warnings.warn("The synset or labels for the dataset do not exist. Please run the training script first.")
        return

    with open("./synset.txt", "r") as f:
        synset = [l.rstrip() for l in f]
    net = get_net(len(synset))
    print("Trying to load the model with the saved parameters...")
    if not os.path.exists("./net.params"):
        warnings.warn("The model does not have any saved parameters... Cannot proceed! Train the model first")
        return

    net.load_parameters("./net.params")
    file_names = os.listdir(prediction_dir)
    full_file_names = [os.path.join(prediction_dir, item) for item in file_names]
    from transforms import MFCC
    mfcc = MFCC()
    print("\nStarting predictions for audio files in ", prediction_dir, " ....\n")
    for filename in full_file_names:
        # Argument kaiser_fast to res_type is faster than 'kaiser_best'. To reduce the load time, passing kaiser_fast.
        X1, _ = librosa.load(filename, res_type='kaiser_fast')
        transformed_test_data = mfcc(mx.nd.array(X1))
        output = net(transformed_test_data.reshape((1, -1)))
        prediction = nd.argmax(output, axis=1)
        print(filename, " -> ", synset[(int)(prediction.asscalar())])
Esempio n. 2
0
                                  interpolation=cv2.INTER_AREA)
                mask = np.expand_dims(mask, axis=-1)
                x_batch.append(image)
                y_batch.append(mask)

            yield np.array(x_batch), np.array(y_batch)


if __name__ == '__main__':
    train_masks_csv = pd.read_csv('../input/train_masks.csv')
    image_list = list(train_masks_csv['img'])
    mask_list = list(train_masks_csv["rle_mask"])

    ids_train, ids_valid = train_test_split(train_masks_csv)

    model = get_net(input_shape=(HEIGHT, WIDTH, 3))

    callbacks = [
        EarlyStopping(monitor='val_dice_coef',
                      patience=10,
                      verbose=1,
                      min_delta=1e-4,
                      mode='max'),
        ReduceLROnPlateau(monitor='val_dice_coef',
                          factor=0.2,
                          patience=5,
                          verbose=1,
                          epsilon=1e-4,
                          mode='max'),
        ModelCheckpoint(monitor='val_dice_coef',
                        filepath='logs/model_weights.hdf5',
Esempio n. 3
0
File: train.py Progetto: gchoi/CAM
import torch
import torch.nn as nn
from torch.autograd import Variable
import model
import utils
import os

if not os.path.exists('./model'):
    os.mkdir('model/')

train_loader = utils.load_data_STL10()

is_cuda = torch.cuda.is_available()

cnn = model.get_net()

criterion = nn.CrossEntropyLoss()
if is_cuda:
    criterion = criterion.cuda()

optimizer = torch.optim.Adam(cnn.parameters(), lr=0.0001)

min_loss = 999

print("START TRAINIG!")

for epoch in range(100):
    epoch_loss = 0
    for i, (images, labels) in enumerate(train_loader):
        images = Variable(images)
        labels = Variable(labels)
Esempio n. 4
0
def gen_soft_labels(c):
    c.setdefault(hebbian=False, distributed=False)
    net = get_net(c)
    opt = get_opt(c, net)
    net, opt, step = c.init_model(net, opt=opt, step='max', train=True)

    print('generating soft labels...')
    data_gen_tr = SequentialIteratorGenSoft(c,
                                            c.get('gen_soft_batch'),
                                            split='train')
    # data_gen_tr = iter(data_gen_tr)
    clear_gpu_memory()
    net.eval()
    with torch.no_grad():
        i = 0
        for batch in tqdm(data_gen_tr):
            x = to_torch(batch, c.device).t()
            # print(x.size())
            # print(x[0:20])
            inputs, labels = x[:-1], x[1:]
            probs, _ = net(inputs, labels)

            # loss_hard = -torch.log(probs.gather(1, labels).squeeze(1)).mean()

            values, indices = torch.topk(probs, c.get('topk'), dim=1)

            indices_ = indices.cpu().numpy()
            values_ = values.cpu().numpy()
            labels_ = labels.cpu().numpy()
            # print(indices_[0:5])
            # print(labels_[0:5])
            # exit(0)

            if probs.size(0) != inputs.size(0):
                indices_ = indices_[-inputs.size(0):, :]
                values_ = values_[-inputs.size(0):, :]
                # labels_ = labels_[-inputs.size(0):, :]

            if i == 0:
                all_soft_indices = indices_
                all_soft_values = values_
            else:
                all_soft_indices = np.concatenate((all_soft_indices, indices_),
                                                  axis=0)
                all_soft_values = np.concatenate((all_soft_values, values_),
                                                 axis=0)

            # print(all_soft_indices.shape)
            # print(all_soft_values.shape)

            i += 1
            # if i > 100:
            #     break
        all_soft_indices = np.concatenate(
            (all_soft_indices[0:1, :], all_soft_indices), axis=0)
        all_soft_values = np.concatenate(
            (all_soft_values[0:1, :], all_soft_values), axis=0)
        np.save(
            c.get('file_out_path') + 'all_soft_indices' +
            str(c.get('worker')) + '.npy', all_soft_indices)
        np.save(
            c.get('file_out_path') + 'all_soft_values' + str(c.get('worker')) +
            '.npy', all_soft_values)

        in_indices = np.load(
            c.get('file_out_path') + 'all_soft_indices' +
            str(c.get('worker')) + '.npy')

        cnt = 0.
        # print(in_indices.shape)
        # print(len(data.tokens))
        for k in range(len(data_gen_tr.tokens)):
            # print(data.tokens[k])
            # print(in_indices[k])
            if data_gen_tr.tokens[k] in in_indices[k]:
                cnt += 1
        print(cnt / len(data_gen_tr.tokens))
        feas1 = feas1.as_in_context(ctx)
        feas2 = feas2.as_in_context(ctx)
        output = net(feas1, feas2)
        cross_entropy = softmax_cross_entropy(output, label)
        loss += nd.mean(cross_entropy).asscalar()
    return loss / len(data)


def SaveTest(test_data, net, ctx, name, ids, synsets):
    outputs = []
    for data1, data2, label in tqdm(test_data):
        data1 = data1.as_in_context(ctx)
        data2 = data2.as_in_context(ctx)
        output = nd.softmax(net(data1, data2))
        outputs.extend(output.asnumpy())
    with open(name, 'w') as f:
        f.write('id,' + ','.join(synsets) + '\n')
        for i, output in zip(ids, outputs):
            f.write(
                i.split('.')[0] + ',' + ','.join([str(num)
                                                  for num in output]) + '\n')


net = get_net(netparams, mx.gpu())
net.hybridize()

softmax_cross_entropy = gluon.loss.SoftmaxCrossEntropyLoss()
print(get_loss(valid_data, net, mx.gpu()))

SaveTest(test_data, net, mx.gpu(), csvname, ids_synsets[0], ids_synsets[1])
Esempio n. 6
0
def main():
    # parameters
    SEED = 1

    NUM_INIT_LB = 10000
    NUM_QUERY = 1000
    NUM_ROUND = 10

    DATA_NAME = 'MNIST'
    # DATA_NAME = 'FashionMNIST'
    # DATA_NAME = 'SVHN'
    # DATA_NAME = 'CIFAR10'

    args_pool = {
        'MNIST': {
            'n_epoch':
            10,
            'transform':
            transforms.Compose([
                transforms.ToTensor(),
                transforms.Normalize((0.1307, ), (0.3081, ))
            ]),
            'loader_tr_args': {
                'batch_size': 64,
                'num_workers': 1
            },
            'loader_te_args': {
                'batch_size': 1000,
                'num_workers': 1
            },
            'optimizer_args': {
                'lr': 0.01,
                'momentum': 0.5
            }
        },
        'FashionMNIST': {
            'n_epoch':
            10,
            'transform':
            transforms.Compose([
                transforms.ToTensor(),
                transforms.Normalize((0.1307, ), (0.3081, ))
            ]),
            'loader_tr_args': {
                'batch_size': 64,
                'num_workers': 1
            },
            'loader_te_args': {
                'batch_size': 1000,
                'num_workers': 1
            },
            'optimizer_args': {
                'lr': 0.01,
                'momentum': 0.5
            }
        },
        'SVHN': {
            'n_epoch':
            20,
            'transform':
            transforms.Compose([
                transforms.ToTensor(),
                transforms.Normalize((0.4377, 0.4438, 0.4728),
                                     (0.1980, 0.2010, 0.1970))
            ]),
            'loader_tr_args': {
                'batch_size': 64,
                'num_workers': 1
            },
            'loader_te_args': {
                'batch_size': 1000,
                'num_workers': 1
            },
            'optimizer_args': {
                'lr': 0.01,
                'momentum': 0.5
            }
        },
        'CIFAR10': {
            'n_epoch':
            20,
            'transform':
            transforms.Compose([
                transforms.ToTensor(),
                transforms.Normalize((0.4914, 0.4822, 0.4465),
                                     (0.2470, 0.2435, 0.2616))
            ]),
            'loader_tr_args': {
                'batch_size': 64,
                'num_workers': 1
            },
            'loader_te_args': {
                'batch_size': 1000,
                'num_workers': 1
            },
            'optimizer_args': {
                'lr': 0.05,
                'momentum': 0.3
            }
        }
    }
    args = args_pool[DATA_NAME]

    # set seed
    np.random.seed(SEED)
    torch.manual_seed(SEED)
    torch.backends.cudnn.enabled = False

    # load dataset
    X_tr, Y_tr, X_te, Y_te = get_dataset(DATA_NAME)
    X_tr = X_tr[:40000]
    Y_tr = Y_tr[:40000]

    # start experiment
    n_pool = len(Y_tr)
    n_test = len(Y_te)
    print('number of labeled pool: {}'.format(NUM_INIT_LB))
    print('number of unlabeled pool: {}'.format(n_pool - NUM_INIT_LB))
    print('number of testing pool: {}'.format(n_test))

    # generate initial labeled pool
    idxs_lb = np.zeros(n_pool, dtype=bool)
    idxs_tmp = np.arange(n_pool)
    np.random.shuffle(idxs_tmp)
    idxs_lb[idxs_tmp[:NUM_INIT_LB]] = True

    # load network
    net = get_net(DATA_NAME)
    handler = get_handler(DATA_NAME)

    strategy = RandomSampling(X_tr, Y_tr, idxs_lb, net, handler, args)
    # strategy = LeastConfidence(X_tr, Y_tr, idxs_lb, net, handler, args)
    # strategy = MarginSampling(X_tr, Y_tr, idxs_lb, net, handler, args)
    # strategy = EntropySampling(X_tr, Y_tr, idxs_lb, net, handler, args)
    # strategy = LeastConfidenceDropout(X_tr, Y_tr, idxs_lb, net, handler, args, n_drop=10)
    # strategy = MarginSamplingDropout(X_tr, Y_tr, idxs_lb, net, handler, args, n_drop=10)
    # strategy = EntropySamplingDropout(X_tr, Y_tr, idxs_lb, net, handler, args, n_drop=10)
    # strategy = KMeansSampling(X_tr, Y_tr, idxs_lb, net, handler, args)
    # strategy = KCenterGreedy(X_tr, Y_tr, idxs_lb, net, handler, args)
    # strategy = BALDDropout(X_tr, Y_tr, idxs_lb, net, handler, args, n_drop=10)
    # strategy = CoreSet(X_tr, Y_tr, idxs_lb, net, handler, args)
    # strategy = AdversarialBIM(X_tr, Y_tr, idxs_lb, net, handler, args, eps=0.05)
    # strategy = AdversarialDeepFool(X_tr, Y_tr, idxs_lb, net, handler, args, max_iter=50)
    # albl_list = [MarginSampling(X_tr, Y_tr, idxs_lb, net, handler, args),
    #              KMeansSampling(X_tr, Y_tr, idxs_lb, net, handler, args)]
    # strategy = ActiveLearningByLearning(X_tr, Y_tr, idxs_lb, net, handler, args, strategy_list=albl_list, delta=0.1)

    # print info
    print(DATA_NAME)
    print('SEED {}'.format(SEED))
    print(type(strategy).__name__)

    # round 0 accuracy
    strategy.train()
    P = strategy.predict(X_te, Y_te)
    acc = np.zeros(NUM_ROUND + 1)
    acc[0] = 1.0 * (Y_te == P).sum().item() / len(Y_te)
    print('Round 0\ntesting accuracy {}'.format(acc[0]))

    for rd in range(1, NUM_ROUND + 1):
        print('Round {}'.format(rd))

        # query
        q_idxs = strategy.query(NUM_QUERY)
        idxs_lb[q_idxs] = True

        # update
        strategy.update(idxs_lb)
        strategy.train()

        # round accuracy
        P = strategy.predict(X_te, Y_te)
        acc[rd] = 1.0 * (Y_te == P).sum().item() / len(Y_te)
        print('testing accuracy {}'.format(acc[rd]))

    # print results
    print('SEED {}'.format(SEED))
    print(type(strategy).__name__)
    print(acc)
Esempio n. 7
0
    # evaluate on test
    data_test = SequentialIterator(config, config.eval_batch, split="test")
    print("Final Evaluation")
    distiller.model_summary(net, "sparsity", 'wikitext-103')
    eval_output = evaluate(config.var(use_cache=True, n_cache=2000, cache_thetaa=best_theta, cache_lambda=best_lambda), data_test, net)
    config.log("VAL RESULT: ppl(%.3lf) theta(%.3lf) lambda(%.3lf)" % (best_ppl, best_theta, best_lambda))
    config.log("TEST RESULT: %s" % eval_output)
    return eval_output




if __name__ == '__main__':
    config = Config.from_args()
    print("config=", config)
    net = get_net(config)

    if config.get("summary"):
        opt = get_opt(config, net)
        net, opt, step = config.init_model(net, opt=opt, step='max', train=True)
        config.log("===> summary of model @ step %d" % step)
        distiller.model_summary(net, config.summary, 'wikitext-103')
        exit(0)

    if config.get("compress"):
        config.log("===> compress from: %s" % config.compress)
        compression_scheduler = distiller.config.file_config(net, None, config.compress)
        

    if config.get('eval_cache_search'):
        evaluate_cache_search(config, net)
Esempio n. 8
0
# start experiment
n_pool = len(Y_tr)
n_test = len(Y_te)
print('number of labeled pool: {}'.format(NUM_INIT_LB))
print('number of unlabeled pool: {}'.format(n_pool - NUM_INIT_LB))
print('number of testing pool: {}'.format(n_test))

# generate initial labeled pool
idxs_lb = np.zeros(n_pool, dtype=bool)
idxs_tmp = np.arange(n_pool)
np.random.shuffle(idxs_tmp)
idxs_lb[idxs_tmp[:NUM_INIT_LB]] = True

# load network
net = get_net(DATA_NAME)
handler = get_handler(DATA_NAME)

strategy = RandomSampling(X_tr, Y_tr, idxs_lb, net, handler, args)
# strategy = LeastConfidence(X_tr, Y_tr, idxs_lb, net, handler, args)
# strategy = MarginSampling(X_tr, Y_tr, idxs_lb, net, handler, args)
# strategy = EntropySampling(X_tr, Y_tr, idxs_lb, net, handler, args)
# strategy = LeastConfidenceDropout(X_tr, Y_tr, idxs_lb, net, handler, args, n_drop=10)
# strategy = MarginSamplingDropout(X_tr, Y_tr, idxs_lb, net, handler, args, n_drop=10)
# strategy = EntropySamplingDropout(X_tr, Y_tr, idxs_lb, net, handler, args, n_drop=10)
# strategy = KMeansSampling(X_tr, Y_tr, idxs_lb, net, handler, args)
# strategy = KCenterGreedy(X_tr, Y_tr, idxs_lb, net, handler, args)
# strategy = BALDDropout(X_tr, Y_tr, idxs_lb, net, handler, args, n_drop=10)
# strategy = CoreSet(X_tr, Y_tr, idxs_lb, net, handler, args)
# strategy = AdversarialBIM(X_tr, Y_tr, idxs_lb, net, handler, args, eps=0.05)
# strategy = AdversarialDeepFool(X_tr, Y_tr, idxs_lb, net, handler, args, max_iter=50)
import megengine as mge
import cv2
import torch
import megengine.functional as F
import numpy as np
import torchvision

from model import get_net

model = get_net()
model.load_state_dict(mge.load('models/save.ae.40.mge'))

model.eval()

# dataset
from megengine.data import RandomSampler, SequentialSampler, DataLoader
from megengine.data.dataset import MNIST
from megengine.data.transform import RandomResizedCrop, Normalize, ToMode, Pad, Compose
root_dir = '/data/.cache/dataset/MNIST'
mnist_train_dataset = MNIST(root=root_dir, train=True, download=False)
mnist_test_dataset = MNIST(root=root_dir, train=False, download=False)

random_sampler = RandomSampler(dataset=mnist_train_dataset, batch_size=256)
sequential_sampler = SequentialSampler(dataset=mnist_test_dataset, batch_size=256)

mnist_train_dataloader = DataLoader(
    dataset=mnist_train_dataset,
    sampler=random_sampler,
    transform=Compose([
            RandomResizedCrop(output_size=28),
            # mean 和 std 分别是 MNIST 数据的均值和标准差,图片数值范围是 0~255
def train(train_dir=None, train_csv=None, epochs=30, batch_size=32):
    """Function responsible for running the training the model."""

    if not train_dir or not os.path.exists(train_dir) or not train_csv:
        warnings.warn("No train directory could be found ")
        return
    # Make a dataset from the local folder containing Audio data
    print("\nMaking an Audio Dataset...\n")
    tick = time.time()
    aud_dataset = AudioFolderDataset(train_dir,
                                     train_csv=train_csv,
                                     file_format='.wav',
                                     skip_header=True)
    tock = time.time()

    print("Loading the dataset took ", (tock - tick), " seconds.")
    print("\n=======================================\n")
    print("Number of output classes = ", len(aud_dataset.synsets))
    print("\nThe labels are : \n")
    print(aud_dataset.synsets)
    # Get the model to train
    net = model.get_net(len(aud_dataset.synsets))
    print("\nNeural Network = \n")
    print(net)
    print("\nModel - Neural Network Generated!\n")
    print("=======================================\n")

    #Define the loss - Softmax CE Loss
    softmax_loss = gluon.loss.SoftmaxCELoss(from_logits=False,
                                            sparse_label=True)
    print("Loss function initialized!\n")
    print("=======================================\n")

    #Define the trainer with the optimizer
    trainer = gluon.Trainer(net.collect_params(), 'adadelta')
    print("Optimizer - Trainer function initialized!\n")
    print("=======================================\n")
    print("Loading the dataset to the Gluon's OOTB Dataloader...")

    #Getting the data loader out of the AudioDataset and passing the transform
    from transforms import MFCC
    aud_transform = MFCC()
    tick = time.time()

    audio_train_loader = gluon.data.DataLoader(
        aud_dataset.transform_first(aud_transform),
        batch_size=32,
        shuffle=True)
    tock = time.time()
    print("Time taken to load data and apply transform here is ",
          (tock - tick), " seconds.")
    print("=======================================\n")

    print("Starting the training....\n")
    # Training loop
    tick = time.time()
    batch_size = batch_size
    num_examples = len(aud_dataset)

    for epoch in range(epochs):
        cumulative_loss = 0
        for data, label in audio_train_loader:
            with autograd.record():
                output = net(data)
                loss = softmax_loss(output, label)
            loss.backward()

            trainer.step(batch_size)
            cumulative_loss += mx.nd.sum(loss).asscalar()

        if epoch % 5 == 0:
            train_accuracy = evaluate_accuracy(audio_train_loader, net)
            print("Epoch {}. Loss: {} Train accuracy : {} ".format(
                epoch, cumulative_loss / num_examples, train_accuracy))
            print("\n------------------------------\n")

    train_accuracy = evaluate_accuracy(audio_train_loader, net)
    tock = time.time()
    print("\nFinal training accuracy: ", train_accuracy)

    print("Training the sound classification for ", epochs,
          " epochs, MLP model took ", (tock - tick), " seconds")
    print("====================== END ======================\n")

    print("Trying to save the model parameters here...")
    net.save_parameters("./net.params")
    print("Saved the model parameters in current directory.")
Esempio n. 11
0
import os
"""
Set general arguements
"""

dataset_name = 'MNIST'
data_transform = transforms.Compose(
    [transforms.ToTensor(),
     transforms.Normalize((0.1307, ), (0.3081, ))])
handler = get_handler(dataset_name)
n_classes = 10
n_epoch = 10
"""
Define model architecture
"""
net = get_net(dataset_name)
"""
Get dataset, set data handlers
Use 10k training dataset from MNIST as the entire dataset
Further split into Test/Train, then split train into labeled and unlabeled
X (Training): 1000 datapoints
X_NOLB (unlabeled datapool): 7000
X_TE (Testing): 2000 datapoints
X_TOLB: Datapoints that require human labeling
"""
# FIX, do this in data object?
_, _, X_INIT, Y_INIT = get_dataset(dataset_name)

X_TR, X_TE, Y_TR, Y_TE = train_test_split(X_INIT,
                                          Y_INIT,
                                          test_size=0.2,
Esempio n. 12
0
def train(c):
    c.setdefault(hebbian=False)
    net = get_net(c)

    emb_params = count_params(net.embed) + count_params(net.loss.projections) + count_params(net.loss.clusters)
    opt = get_opt(c, net)
    net, opt, step = c.init_model(net, opt=opt, step='max', train=True)
    if c.get('distillation_teacher') == 'file':
        data_tr_distill = DistillationSampleIterator(c, c.train_batch, split='train')
        iter_tr_distill = iter(data_tr_distill)

    step_lr = scheduler(c, opt, step)
    data_tr = SampleIterator(c, c.train_batch, split='valid' if c.debug else 'train')
    iter_tr = iter(data_tr)
    data_val = SequentialIterator(c, c.eval_batch, split='valid')

    s = Namespace(net=net, opt=opt, step=step)
    c.on_train_start(s)

    c.log('Embedding has %s parameters' % emb_params)

    if c.hebbian:
        counters = [torch.ones(end - start, dtype=torch.long, device=c.device) for start, end in zip([0] + c.cutoffs, c.cutoffs + [c.n_vocab])]
        temp_counters = [torch.zeros_like(x) for x in counters]

    best_val_loss = np.inf
    if s.results is not None and 'val_loss' in s.results.columns:
        best_val_loss = s.results['val_loss'].dropna().max()
    try:
        while step < s.step_max:
            step_lr(step)

            if c.get('distillation_teacher') == 'file':
                x_hard_labels, x_soft_labels, x_soft_probs = next(iter_tr_distill)

                x_hard_labels = to_torch(x_hard_labels, c.device).t()

                x_soft_labels = to_torch(x_soft_labels, c.device)
                x_soft_labels = x_soft_labels.permute(1, 0, 2)

                x_soft_probs = to_torch(x_soft_probs, c.device)
                x_soft_probs = x_soft_probs.permute(1, 0, 2)

                inputs, hard_labels = x_hard_labels[:-1], x_hard_labels[1:]
                soft_labels = x_soft_labels[1:]
                soft_probs = x_soft_probs[1:]

                t_s = time()

                preds = net(inputs=inputs, labels=hard_labels, soft_labels=soft_labels, soft_probs=soft_probs,
                            is_distilling=True, current_step=step)
                loss = preds['loss']
                total_loss = loss
                extras = {}

            else:
                x = to_torch(next(iter_tr), c.device).t()

                t_s = time()
                inputs, labels = x[:-1], x[1:]
                preds = net(inputs, labels)
                loss = preds['loss']
                if c.model_class == 'UniversalTransformer':
                    act_loss = preds['act_loss']
                    total_loss = act_loss + loss
                    extras = dict(act_loss=from_torch(act_loss), n_updates=from_torch(preds['n_updates'].mean()))
                else:
                    total_loss = loss
                    extras = {}

            opt.zero_grad()
            if torch.isnan(total_loss):
                raise RuntimeError('Encountered nan loss during training')
            with amp.scale_loss(total_loss, opt) as scaled_loss:
                scaled_loss.backward()
            torch.nn.utils.clip_grad_norm_(net.parameters(), c.get('clip_grad', 0.5))
            opt.step()

            if c.hebbian:
                hebbian_weight_update(c, net, preds['hiddens'], counters, temp_counters)

            time_model = np.round(time() - t_s, 5)

            loss = from_torch(loss)
            perplexity = np.nan if loss > 5 else np.e ** loss
            step_result = pd.Series(Dict(
                loss=loss,
                perplexity=perplexity,
                time=time_model,
                **extras
            )).add_prefix('train_')
            step_result['lr'] = next(iter(opt.param_groups))['lr']
            step_result['theta'] = from_torch(preds['theta'])
            step_result['lambda'] = from_torch(preds['lambda'])

            s.step = step = step + 1
            if step % c.step_eval == 0:
                step_result = step_result.append(
                    pd.Series(evaluate(c, data_val, net)).add_prefix('val_')
                )
                s.record_step = step_result['val_loss'] < best_val_loss
                clear_gpu_memory()
            s.step_result = step_result
            c.on_step_end(s)
    except Exception as e:
        import traceback
        err = traceback.format_exc()
        if c.main:
            c.log(err)
        else:
            print(err)
    finally:
        c.on_train_end(s)
Esempio n. 13
0
import torch
import os
from query_strategies import RandomSampling, LeastConfidence, MarginSampling, EntropySampling, \
    LeastConfidenceDropout, MarginSamplingDropout, EntropySamplingDropout, \
    KMeansSampling, KCenterGreedy, BALDDropout, CoreSet, \
    AdversarialBIM, AdversarialDeepFool, ActiveLearningByLearning

# parameters
SEED = 1

NUM_INIT_LB = 10000
NUM_QUERY = 1000
NUM_ROUND = 10

configuration = config.load_config()
net = get_net(configuration.net)()
if configuration.load_model:
    try:
        net.load_state_dict(torch.load(configuration.model_file))
    except:
        print("No model file found at", configuration.model_file)

# set seed
np.random.seed(SEED)
torch.manual_seed(SEED)
# torch.backends.cudnn.enabled = False

# load dataset
dataset = datasets.ImageFolder(
    root=configuration.labeled_training_dir,
    transform=transforms.Compose([
Esempio n. 14
0
                                              batch_size=opt.batch_size,
                                              shuffle=True,
                                              num_workers=opt.nThreads,
                                              drop_last=True)
    validset = MyImageLoader(opt.root_dir, 'valid', opt.classes,
                             opt.input_size)
    validloader = torch.utils.data.DataLoader(validset,
                                              batch_size=opt.batch_size,
                                              shuffle=False,
                                              num_workers=opt.nThreads,
                                              drop_last=True)

    if opt.checkpoint_path is not None:
        net = model.load_net(opt.classes, opt.checkpoint_path).cuda()
    else:
        net = model.get_net(opt.model_name, opt.classes, opt.pretrained).cuda()

    if len(GPU_ID) > 1:
        net = torch.nn.DataParallel(net, device_ids=GPU_ID)
    else:
        os.environ["CUDA_VISIBLE_DEVICES"] = str(GPU_ID[0])

    # set loss function
    if opt.use_posweight:
        print('Input pos_weight into loss function...')
        pos_weight = calculate_pos_weight(opt.classes, trainset.image_classes)
        pos_weight = torch.from_numpy(pos_weight.astype(np.float32)).cuda()
    else:
        pos_weight = None

    if opt.focalloss_gamma > 0:
Esempio n. 15
0
def train(train_dir=None, train_csv=None, epochs=30, batch_size=32):
    """Function responsible for running the training the model."""

    if not train_dir or not os.path.exists(train_dir) or not train_csv:
        warnings.warn("No train directory could be found ")
        return
    # Make a dataset from the local folder containing Audio data
    print("\nMaking an Audio Dataset...\n")
    tick = time.time()
    aud_dataset = AudioFolderDataset(train_dir, train_csv=train_csv, file_format='.wav', skip_header=True)
    tock = time.time()

    print("Loading the dataset took ", (tock-tick), " seconds.")
    print("\n=======================================\n")
    print("Number of output classes = ", len(aud_dataset.synsets))
    print("\nThe labels are : \n")
    print(aud_dataset.synsets)
    # Get the model to train
    net = model.get_net(len(aud_dataset.synsets))
    print("\nNeural Network = \n")
    print(net)
    print("\nModel - Neural Network Generated!\n")
    print("=======================================\n")

    #Define the loss - Softmax CE Loss
    softmax_loss = gluon.loss.SoftmaxCELoss(from_logits=False, sparse_label=True)
    print("Loss function initialized!\n")
    print("=======================================\n")

    #Define the trainer with the optimizer
    trainer = gluon.Trainer(net.collect_params(), 'adadelta')
    print("Optimizer - Trainer function initialized!\n")
    print("=======================================\n")
    print("Loading the dataset to the Gluon's OOTB Dataloader...")

    #Getting the data loader out of the AudioDataset and passing the transform
    from transforms import MFCC
    aud_transform = MFCC()
    tick = time.time()

    audio_train_loader = gluon.data.DataLoader(aud_dataset.transform_first(aud_transform), batch_size=32, shuffle=True)
    tock = time.time()
    print("Time taken to load data and apply transform here is ", (tock-tick), " seconds.")
    print("=======================================\n")


    print("Starting the training....\n")
    # Training loop
    tick = time.time()
    batch_size = batch_size
    num_examples = len(aud_dataset)

    for epoch in range(epochs):
        cumulative_loss = 0
        for data, label in audio_train_loader:
            with autograd.record():
                output = net(data)
                loss = softmax_loss(output, label)
            loss.backward()

            trainer.step(batch_size)
            cumulative_loss += mx.nd.sum(loss).asscalar()

        if epoch%5 == 0:
            train_accuracy = evaluate_accuracy(audio_train_loader, net)
            print("Epoch {}. Loss: {} Train accuracy : {} ".format(epoch, cumulative_loss/num_examples, train_accuracy))
            print("\n------------------------------\n")

    train_accuracy = evaluate_accuracy(audio_train_loader, net)
    tock = time.time()
    print("\nFinal training accuracy: ", train_accuracy)

    print("Training the sound classification for ", epochs, " epochs, MLP model took ", (tock-tick), " seconds")
    print("====================== END ======================\n")

    print("Trying to save the model parameters here...")
    net.save_parameters("./net.params")
    print("Saved the model parameters in current directory.")
Esempio n. 16
0
    # plot
    if valid_data is not None:
        plt.plot(plt_train_acc)
        plt.plot(plt_valid_acc)
        plt.legend(['train_acc', 'test_acc'])
        plt.savefig("Loss.png")


ctx = utils.try_gpu()
num_epochs = 200
learning_rate = 0.1
weight_decay = 5e-4
lr_period = 80
lr_decay = 0.1

net = model.get_net(ctx)
net.hybridize()
train(net, train_data, valid_data, num_epochs, learning_rate, weight_decay,
      ctx, lr_period, lr_decay)

import numpy as np
import pandas as pd

net = model.get_net(ctx)
net.hybridize()
train(net, train_valid_data, None, num_epochs, learning_rate, weight_decay,
      ctx, lr_period, lr_decay)

preds = []
for data, label in test_data:
    output = net(data.as_in_context(ctx))
Esempio n. 17
0
        RandomResizedCrop(output_size=28),
        # mean 和 std 分别是 MNIST 数据的均值和标准差,图片数值范围是 0~255
        #Normalize(mean=0.1307*255, std=0.3081*255),
        #Pad(2),
        # 'CHW'表示把图片由 (height, width, channel) 格式转换成 (channel, height, width) 格式
        #ToMode('CHW'),
    ]))
mnist_test_dataloader = DataLoader(
    dataset=mnist_test_dataset,
    sampler=sequential_sampler,
)

# model
from model import get_net

net = get_net()

optimizer = optim.Adam(
    net.parameters(),
    lr=0.01,
)


def get_kl_divergence(mean, var):

    return 1 / 2 * (mean**2 + var - F.log(var) - 1).sum(axis=1).mean()


data = mge.tensor()
label = mge.tensor(dtype="float32")
code = mge.tensor(dtype="float32")
Esempio n. 18
0
import os
import torch
import torch.nn as nn
from torch.autograd import Variable
import model
import utils

if not os.path.exists('./model'):
    os.mkdir('model/')

train_loader = utils.load_data_stl10()

is_cuda = torch.cuda.is_available()

device = torch.device("cuda" if is_cuda else "cpu")
cnn = model.get_net().to(device)

criterion = nn.CrossEntropyLoss().to(device)

optimizer = torch.optim.Adam(cnn.parameters(), lr=0.0001)

min_loss = 999

print("START TRAINING")

for epoch in range(100):
    epoch_loss = 0
    for i, (images, labels) in enumerate(train_loader):
        images, labels = images.to(device), labels.to(device)
        optimizer.zero_grad()
        outputs, _ = cnn(images)
Esempio n. 19
0
print('number of labeled pool: {}'.format(NUM_INIT_LB))
print('number of unlabeled pool: {}'.format(n_pool - NUM_INIT_LB))
print('number of testing pool: {}'.format(n_test))

# setting training parameters
alpha = 2e-3
epoch = 80

# Generate the initial labeled pool
idxs_lb = np.zeros(n_pool, dtype=bool)
idxs_tmp = np.arange(n_pool)
np.random.shuffle(idxs_tmp)
idxs_lb[idxs_tmp[:NUM_INIT_LB]] = True

# loading neural network
net_fea, net_clf, net_dis = get_net(DATA_NAME)

# here the training handlers and testing handlers are different
train_handler = get_handler(DATA_NAME)
test_handler = dataset.get_handler(DATA_NAME)

strategy = WAAL(X_tr, Y_tr, idxs_lb, net_fea, net_clf, net_dis, train_handler,
                test_handler, args)

# print information
print(DATA_NAME)
#print('SEED {}'.format(SEED))
print(type(strategy).__name__)

# round 0 accuracy
strategy.train(alpha=alpha, total_epoch=epoch)
Esempio n. 20
0
torch.backends.cudnn.benchmark = False


# load dataset
X_tr, Y_tr, X_te, Y_te = get_dataset(DATA_NAME)

# start experiment
n_pool = len(Y_tr)
n_test = len(Y_te)

# generate initial labeled pool
idxs_lb = np.zeros(n_pool, dtype=bool)
idxs_lb[0:NUM_INIT_LB] = True

# load network
net = get_net(DATA_NAME, model=selected_model)
handler = get_handler(DATA_NAME)

print(net)

#-- Strategy --
if AL_type == 'Random':
    strategy = RandomSampling(X_tr, Y_tr, idxs_lb, net, handler, args, al_apply=al_train_apply, cm_train_apply=cm_train_apply)
elif AL_type == 'CutoutSampling':
    strategy = CutoutSampling(X_tr, Y_tr, idxs_lb, net, handler, args, al_apply=al_train_apply, alpha=alpha, cm_train_apply=cm_train_apply)  # ALT
elif AL_type == 'CutMixEntropy':
    strategy = CutMixEntropySampling(X_tr, Y_tr, idxs_lb, net, handler, args, al_apply=al_train_apply, cm_train_apply=cm_train_apply) #
elif AL_type == 'MarginSampling':
    strategy = MarginSampling(X_tr, Y_tr, idxs_lb, net, handler, args,  al_apply=al_train_apply, cm_train_apply=cm_train_apply)
elif AL_type == 'EntropySampling':
    strategy = EntropySampling(X_tr, Y_tr, idxs_lb, net, handler, args,  al_apply=al_train_apply, cm_train_apply=cm_train_apply)