Exemple #1
0
def main(args):
    train_loader = torch.utils.data.DataLoader(
          UrbanSound8KDataset("UrbanSound8K_train.pkl", mode),
          batch_size=args.batch_size, shuffle=True,
          num_workers=args.worker_count, pin_memory=True)

    val_loader = torch.utils.data.DataLoader(
         UrbanSound8KDataset("UrbanSound8K_test.pkl", mode),
         batch_size=args.batch_size, shuffle=False,
         num_workers=args.worker_count, pin_memory=True)

    # Cross entropy loss as declared
    criterion = nn.CrossEntropyLoss()
    # Using SGD with momentum as declared
    # Torch implemenation of optim.SGD includes L2 Reg as base
    optimizer = optim.SGD(model.parameters(), lr=args.learning_rate, momentum=args.momentum)
    # Get mode from args, LMC, MC, or MLMC expected
    mode = args.mode
    if(mode == 'LMC'):
        model = CNN(height=85, width=41, channels=3, class_count=10, dropout=args.dropout)
    elif(mode == 'MC'):
        model = CNN(height=85, width=41, channels=3, class_count=10, dropout=args.dropout)
    # TODO Need to add support for MLMC combined since sizes are increased
    optimizer = optim.SGD(model.parameters(), lr=args.learning_rate)


    model = model.to(DEVICE)

    # Run training for all epochs, validation as expected
    for epoch in range(0,args.epochs):
        print("Epoch : " + str(epoch+1))
        trainer(train_loader, model, criterion, args.val_frequency, DEVICE)

        if(epoch+1 % val_frequency == 0):
            validate(val_loader, model, criterion, DEVICE)
Exemple #2
0
def main(args):
    train_dataset=UrbanSound8KDataset('UrbanSound8K_train.pkl', "MC")

    training_class_counts=np.array([6295, 1825, 6248, 5121, 5682, 6282, 1112, 5886, 5819, 6299])
    class_weights=1/training_class_counts
    data_weights=[]

    for i in range(0,len(train_dataset)):
        features, label, filename, labelname=train_dataset.__getitem__(i)
        data_weight=class_weights[label]
        data_weights.append(data_weight)

    sampler = torch.utils.data.sampler.WeightedRandomSampler(torch.tensor(data_weights), len(train_dataset))

    train_loader = torch.utils.data.DataLoader(
        train_dataset,
        shuffle=False,
        batch_size=args.batch_size,
        pin_memory=True,
        num_workers=args.worker_count,
        sampler=sampler,
    )

    test_loader = torch.utils.data.DataLoader(
        UrbanSound8KDataset('UrbanSound8K_test.pkl', "MC"),
        shuffle=False,
        batch_size=args.batch_size,
        num_workers=args.worker_count,
        pin_memory=True,
    )


    model = MCNet(height=85, width=41, channels=1, class_count=10,dropout=args.dropout)

    ## TASK 8: Redefine the criterion to be softmax cross entropy
    criterion = nn.CrossEntropyLoss()

    ## TASK 11: Define the optimizer
    optimizer = optim.Adam(model.parameters(), lr=args.learning_rate, betas=(0.9, 0.999), weight_decay=0.004)

    log_dir = get_summary_writer_log_dir(args)
    print(f"Writing logs to {log_dir}")
    summary_writer = SummaryWriter(
            str(log_dir),
            flush_secs=5
    )
    trainer = Trainer(
        model, train_loader, test_loader, criterion, optimizer, summary_writer, DEVICE
    )

    trainer.train(
        args.epochs,
        args.val_frequency,
        print_frequency=args.print_frequency,
        log_frequency=args.log_frequency,
    )

    summary(model, input_size=(1, 85, 41))

    summary_writer.close()
def validate_model(args):
    if torch.cuda.is_available():
        device = torch.device("cuda")
    else:
        device = torch.device("cpu")
    criterion = CrossEntropyLoss()
    mode = args.mode

    if mode in ['MC','LMC','MLMC']:
        validation_loader = torch.utils.data.DataLoader(
            UrbanSound8KDataset('UrbanSound8K_test.pkl', mode),
            batch_size=args.batch_size,
            shuffle=True,
            num_workers=args.worker_count,
            pin_memory=True
        )
        model = CNN(height=85, width=41, channels=1, class_count=10, dropout=0.5, mode = mode)
        checkpoint = torch.load(args.checkpoint, map_location = device)

        model.load_state_dict(checkpoint['model'])
        print(f"Validating {mode} model with parameters trained for {checkpoint['epoch']} epochs.")

        loss, accuracy, class_accuracies = validate_single(model, validation_loader, criterion, device)

        print(f"accuracy: {accuracy * 100:2.2f}")
        print_class_accuracies(class_accuracies)

    elif mode == 'TSCNN':
        loader_LMC = torch.utils.data.DataLoader(
            UrbanSound8KDataset('UrbanSound8K_test.pkl', 'LMC'),
            batch_size=args.batch_size,
            shuffle=False,
            num_workers=args.worker_count,
            pin_memory=True
        )
        loader_MC = torch.utils.data.DataLoader(
            UrbanSound8KDataset('UrbanSound8K_test.pkl', 'MC'),
            batch_size=args.batch_size,
            shuffle=False,
            num_workers=args.worker_count,
            pin_memory=True
        )
        model1 = CNN(height=85, width=41, channels=1, class_count=10, dropout=0.5, mode = 'LMC')
        model2 = CNN(height=85, width=41, channels=1, class_count=10, dropout=0.5, mode = 'MC')

        checkpoint1 = torch.load(args.checkpoint, map_location = device)
        model1.load_state_dict(checkpoint1['model'])

        checkpoint2 = torch.load(args.checkpoint2, map_location = device)
        model2.load_state_dict(checkpoint2['model'])

        print(f"Validating {mode} model with parameters trained for {checkpoint1['epoch']} and {checkpoint2['epoch']} epochs.")
        accuracy, class_accuracies = validate_double(model1, model2, loader_LMC, loader_MC, criterion, device)

        print(f"accuracy: {accuracy * 100:2.2f}")
        print_class_accuracies(class_accuracies)
    else:
        print('Please provide a valid argument.')
Exemple #4
0
def main(args):
    transform = ToTensor()

    train_data = UrbanSound8KDataset('UrbanSound8K_train.pkl', args.mode)
    val_data = UrbanSound8KDataset('UrbanSound8K_test.pkl', args.mode)

    train_loader = torch.utils.data.DataLoader(
        train_data,
        batch_size=args.batch_size,
        shuffle=True,
        num_workers=args.workers,
        pin_memory=True,
    )
    val_loader = torch.utils.data.DataLoader(
        val_data,
        batch_size=args.batch_size,
        shuffle=False,
        num_workers=args.workers,
        pin_memory=True,
    )

    model = CNN(height=85,
                width=41,
                channels=1,
                class_count=10,
                dropout=args.dropout,
                mode=args.mode)
    criterion = nn.CrossEntropyLoss()

    optimizer = optim.SGD(model.parameters(),
                          lr=args.learning_rate,
                          momentum=args.momentum,
                          weight_decay=args.decay)
    #optimizer = optim.AdamW(model.parameters(), lr=args.learning_rate, betas=(0.9, 0.999), eps=1e-08, weight_decay=args.decay, amsgrad=False)

    log_dir = get_summary_writer_log_dir(args)
    print(f"Writing logs to {log_dir}")
    summary_writer = SummaryWriter(str(log_dir), flush_secs=5)

    trainer = Trainer(model, train_loader, val_loader, criterion, optimizer,
                      summary_writer, DEVICE, args.mode)

    trainer.train(
        args.epochs,
        args.val_frequency,
        print_frequency=args.print_frequency,
        log_frequency=args.log_frequency,
    )

    summary_writer.close()
def main(args):
    transform = transforms.ToTensor()
    mode = args.mode
    train_loader = torch.utils.data.DataLoader(
      UrbanSound8KDataset('UrbanSound8K_train.pkl', mode),
      batch_size=args.batch_size,
      shuffle=True,
      num_workers=args.worker_count,
      pin_memory=True
    )
    val_loader = torch.utils.data.DataLoader(
     UrbanSound8KDataset('UrbanSound8K_test.pkl', mode),
     batch_size=args.batch_size,
     shuffle=True,
     num_workers=args.worker_count,
     pin_memory=True
    )

    ## Build a model based on mode
    if args.mode == 'MLMC':
        model = CNN(height=145, width=41, channels=1, class_count=10, dropout=args.dropout,mode = args.mode)
    else:
        model = CNN(height=85, width=41, channels=1, class_count=10, dropout=args.dropout,mode = args.mode)

    ## Redefine the criterion to be softmax cross entropy
    criterion = nn.CrossEntropyLoss()

    ## Use adam optimizer. AdamW is Adam with L-2 regularisation.
    optimizer = torch.optim.AdamW(model.parameters(), lr=args.learning_rate, weight_decay=args.weight_decay)

    log_dir = get_summary_writer_log_dir(args)
    print(f"Writing logs to {log_dir}")
    summary_writer = SummaryWriter(
            str(log_dir),
            flush_secs=5
    )
    trainer = Trainer(
        model, train_loader, val_loader, criterion, optimizer, summary_writer,
        DEVICE, args.checkpoint_path, checkpoint_frequency = args.checkpoint_frequency
    )

    trainer.train(
        args.epochs,
        args.val_frequency,
        print_frequency=args.print_frequency,
        log_frequency=args.log_frequency,
    )

    summary_writer.close()
Exemple #6
0
def run(mode):

    if torch.cuda.is_available():
        DEVICE = torch.device("cuda")
    else:
        DEVICE = torch.device("cpu")

    mode = mode
    train_loader = torch.utils.data.DataLoader(UrbanSound8KDataset(
        'UrbanSound8K_train.pkl', mode),
                                               batch_size=32,
                                               shuffle=True,
                                               num_workers=8,
                                               pin_memory=True)

    val_loader = torch.utils.data.DataLoader(UrbanSound8KDataset(
        'UrbanSound8K_test.pkl', mode),
                                             batch_size=32,
                                             shuffle=False,
                                             num_workers=8,
                                             pin_memory=True)

    isMLMC = False
    if (mode == 'MLMC'):
        isMLMC = True

    model = CNN(height=41,
                width=85,
                channels=1,
                class_count=10,
                dropout=0.5,
                isMLMC=isMLMC)

    criterion = nn.CrossEntropyLoss()

    optimizer = torch.optim.Adam(model.parameters(),
                                 lr=0.001,
                                 weight_decay=1e-6)

    trainer = Trainer(model, train_loader, val_loader, criterion, optimizer,
                      DEVICE)

    int_results = trainer.train(
        epochs=50,
        print_frequency=50,
        val_frequency=5,
    )

    return int_results
Exemple #7
0
def main():
    global args, best_prec1

    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

    if args.mode != 'LMC+MC':
        print(args.mode)
        model = ESCModel(mode=args.mode)

    model = torch.nn.DataParallel(model, device_ids=None).to(device)

    train_loader = torch.utils.data.DataLoader(UrbanSound8KDataset(
        args.train_pickle, args.mode),
                                               batch_size=args.batch_size,
                                               shuffle=True,
                                               num_workers=args.workers,
                                               pin_memory=True)

    val_loader = torch.utils.data.DataLoader(UrbanSound8KDataset(
        args.test_pickle, args.mode),
                                             batch_size=args.batch_size,
                                             shuffle=False,
                                             num_workers=args.workers,
                                             pin_memory=True)

    criterion = torch.nn.CrossEntropyLoss()

    optimizer = torch.optim.Adam(model.parameters(),
                                 lr=args.lr,
                                 weight_decay=args.weight_decay)
    scheduler = MultiStepLR(optimizer, args.lr_steps, gamma=0.1)

    for epoch in range(args.epochs):
        scheduler.step()
        train(train_loader, model, criterion, optimizer, epoch, device)

        # evaluate on validation set
        prec1 = validate(val_loader, model, criterion, device)
        # remember best prec@1 and save checkpoint
        is_best = prec1 > best_prec1
        best_prec1 = max(prec1, best_prec1)
        save_checkpoint(
            {
                'epoch': epoch + 1,
                'state_dict': model.state_dict(),
                'best_prec1': best_prec1,
            }, is_best)

    summaryWriter.close()
Exemple #8
0
def main(args):
    transform = transforms.ToTensor()
    args.dataset_root.mkdir(parents=True, exist_ok=True)

    train_loader = torch.utils.data.DataLoader(
        UrbanSound8KDataset('UrbanSound8K_train.pkl', mode=args.mode),
        batch_size=32, shuffle=True,
        num_workers=8, pin_memory=True)
    test_loader = torch.utils.data.DataLoader(
        UrbanSound8KDataset('UrbanSound8K_test.pkl', mode=args.mode),
        batch_size=32, shuffle=False,
        num_workers=8, pin_memory=True)

    # Change the input dimensions if we are using MLMC.
    # Direct user to correct file if they want to use TSCNN.
    if args.mode == "LMC" or args.mode == "MC":
        model = CNN(height=85, width=41, channels=1, class_count=10, dropout=args.dropout, mode=args.mode)
    elif args.mode == "MLMC":
        model = CNN(height=145, width=41, channels=1, class_count=10, dropout=args.dropout, mode=args.mode)
    elif args.mode == "TSCNN":
        print("Use file late_fusion.py to run TSCNN with trained LMCNet and MCNet")

    criterion = nn.CrossEntropyLoss()

    # Paper specifies "variant of stochastic gradient descent" with reference
    # pointing to Adam. Weight decay used for L2 regularisation.
    optimizer = optim.Adam(model.parameters(), lr=args.learning_rate, weight_decay=args.weight_decay)

    log_dir = get_summary_writer_log_dir(args)
    print(f"Writing logs to {log_dir}")
    summary_writer = SummaryWriter(
            str(log_dir),
            flush_secs=5
    )
    trainer = Trainer(
        model, train_loader, test_loader, criterion, optimizer, summary_writer,
        DEVICE, args.checkpoint_path, args.checkpoint_frequency
    )

    trainer.train(
        args.epochs,
        args.val_frequency,
        print_frequency=args.print_frequency,
        log_frequency=args.log_frequency,
    )

    summary_writer.close()
Exemple #9
0
def abstractedDataLoader(dataset, datatype):
    loader = torch.utils.data.DataLoader( 
      UrbanSound8KDataset(dataset, datatype), 
      batch_size=32, shuffle=True, 
      num_workers=8, pin_memory=True
    ) 

    return loader
Exemple #10
0
def main(args):
    args.dataset_root.mkdir(parents=True, exist_ok=True)

    train_loader = torch.utils.data.DataLoader(UrbanSound8KDataset(
        'UrbanSound8K_train.pkl', args.audiotype),
                                               batch_size=32,
                                               shuffle=True,
                                               num_workers=8,
                                               pin_memory=True)

    val_loader = torch.utils.data.DataLoader(UrbanSound8KDataset(
        'UrbanSound8K_test.pkl', args.audiotype),
                                             batch_size=32,
                                             shuffle=False,
                                             num_workers=8,
                                             pin_memory=True)

    model = CNN(height=85,
                width=41,
                channels=1,
                class_count=10,
                dropout=args.dropout)
    criterion = nn.CrossEntropyLoss()
    optimizer = optim.Adam(model.parameters(),
                           lr=args.learning_rate,
                           weight_decay=5e-3,
                           betas=(0.9, 0.9))

    log_dir = get_summary_writer_log_dir(args)
    print(f"Writing logs to {log_dir}")
    summary_writer = SummaryWriter(str(log_dir), flush_secs=5)

    trainer = Trainer(model, train_loader, val_loader, criterion, optimizer,
                      summary_writer, DEVICE)

    trainer.train(
        args.epochs,
        args.val_frequency,
        print_frequency=args.print_frequency,
        log_frequency=args.log_frequency,
    )

    summary_writer.close()
Exemple #11
0
def build_dataloader(args):
    """ Build a training and test dataloader for the UrbanSound8K dataset

    Args:
        args: the CLI args

    Returns:
        A tuple of training loader, testing loader and individual inverted class weights
    """

    # Load data set and compute class weightings
    train_dataset = UrbanSound8KDataset(
        args.dataset_root / 'UrbanSound8K_train.pkl',
        args.mode,
        augmentation_length=args.augmentation_length)
    class_weights, sample_weights = calculate_weights(train_dataset)

    # Define a weighted sampler for the dataset
    weighted_sampler = torch.utils.data.WeightedRandomSampler(
        sample_weights, len(train_dataset))

    # Configure data loaders for testing and training
    train_loader = torch.utils.data.DataLoader(
        train_dataset,
        batch_size=args.batch_size,
        shuffle=False,
        pin_memory=True,
        num_workers=args.worker_count,
        sampler=weighted_sampler,
    )

    test_loader = torch.utils.data.DataLoader(
        UrbanSound8KDataset(args.dataset_root / 'UrbanSound8K_test.pkl',
                            args.mode),
        shuffle=False,
        batch_size=args.batch_size,
        num_workers=args.worker_count,
        pin_memory=True,
    )

    return train_loader, test_loader, class_weights
def main(args):
    criterion = nn.CrossEntropyLoss()

    if args.mode in ["LMC", "MC", "MLMC"]:
        test_loader = torch.utils.data.DataLoader(UrbanSound8KDataset(
            'UrbanSound8K_test.pkl', mode=args.mode),
                                                  batch_size=32,
                                                  shuffle=False,
                                                  num_workers=8,
                                                  pin_memory=True)
        if args.mode in ["LMC", "MC"]:
            model = CNN(height=85,
                        width=41,
                        channels=1,
                        class_count=10,
                        dropout=0.5,
                        mode=args.mode)
        else:
            model = CNN(height=145,
                        width=41,
                        channels=1,
                        class_count=10,
                        dropout=0.5,
                        mode=args.mode)
        checkpoint = torch.load("checkpoint" + args.mode + ".pkl",
                                map_location=DEVICE)
        model.load_state_dict(checkpoint["model"])
        validator = Validator(model, test_loader, criterion, DEVICE)
        validator.validate(args.mode)
    elif args.mode == "TSCNN":
        if os.path.isfile("LMCscores.pkl") and os.path.isfile("MCscores.pkl"):
            LMCscores = pickle.load(open("LMCscores.pkl", 'rb'))
            MCscores = pickle.load(open("MCscores.pkl", 'rb'))
            TSCNN(LMCscores, MCscores, DEVICE)
        else:
            print(
                "Run both LMCNet and MCNet to obtain scores before running TSCNN"
            )
    else:
        print("Input one of LMC, MC, MLMC, or TSCNN as the mode argument")
Exemple #13
0
def fetch_loader(
    train: bool,
    mode: str,
    spec_augment: float,
    freq_mask=20,  # shape of logmelspec
    time_mask=5,
):
    """
    train: is loader train True/False (else test)
    mode: what data type to load
    spec_augment: augment data or not
    """
    if train:
        path = 'UrbanSound8K_train.pkl'
    else:
        path = 'UrbanSound8K_test.pkl'

    loader = torch.utils.data.DataLoader(UrbanSound8KDataset(
        path, mode, spec_augment, freq_mask, time_mask),
                                         batch_size=32,
                                         shuffle=train,
                                         num_workers=8,
                                         pin_memory=True)
    return loader
Exemple #14
0
def main(args):
    mode = args.mode

    transform = torchvision.transforms.ColorJitter(
        brightness=args.data_aug_brightness)

    train_loader = torch.utils.data.DataLoader(UrbanSound8KDataset(
        "UrbanSound8K_train.pkl", mode),
                                               batch_size=args.batch_size,
                                               shuffle=True,
                                               num_workers=args.worker_count,
                                               pin_memory=True)

    if (mode != 'TSCNN'):
        test_loader = torch.utils.data.DataLoader(
            UrbanSound8KDataset("UrbanSound8K_test.pkl", mode),
            batch_size=args.batch_size,
            shuffle=False,
            num_workers=args.worker_count,
            pin_memory=True)
    else:
        test_loaderLMC = torch.utils.data.DataLoader(
            UrbanSound8KDataset("UrbanSound8K_test.pkl", 'LMC'),
            batch_size=args.batch_size,
            shuffle=False,
            num_workers=args.worker_count,
            pin_memory=True)
        test_loaderMC = torch.utils.data.DataLoader(
            UrbanSound8KDataset("UrbanSound8K_test.pkl", 'MC'),
            batch_size=args.batch_size,
            shuffle=False,
            num_workers=args.worker_count,
            pin_memory=True)

    criterion = nn.CrossEntropyLoss()

    if (mode == 'LMC'):
        model = CNN(height=85,
                    width=41,
                    channels=1,
                    class_count=10,
                    dropout=args.dropout,
                    mode=1)
    elif (mode == 'MC'):
        model = CNN(height=85,
                    width=41,
                    channels=1,
                    class_count=10,
                    dropout=args.dropout,
                    mode=2)
    elif (mode == 'MLMC'):
        model = CNN(height=145,
                    width=41,
                    channels=1,
                    class_count=10,
                    dropout=args.dropout,
                    mode=3)
    elif (mode == 'TSCNN'):
        modelLMC = CNN(height=85,
                       width=41,
                       channels=1,
                       class_count=10,
                       dropout=args.dropout,
                       mode=1)
        modelMC = CNN(height=85,
                      width=41,
                      channels=1,
                      class_count=10,
                      dropout=args.dropout,
                      mode=2)

        # Load state from files
        modelLMC.load_state_dict(torch.load("LMC.pth"))
        modelMC.load_state_dict(torch.load("MC.pth"))
        # Set up arbitrary optimizer to init trainers
        optimizer = optim.SGD(modelLMC.parameters(),
                              lr=args.learning_rate,
                              momentum=0.9,
                              weight_decay=0.0001)

    if (mode != 'TSCNN'):
        optimizer = optim.SGD(model.parameters(),
                              lr=args.learning_rate,
                              momentum=0.9,
                              weight_decay=0.0001)

    log_dir = get_summary_writer_log_dir(args)
    print(f"Writing logs to {log_dir}")
    summary_writer = SummaryWriter(str(log_dir), flush_secs=5)
    if mode == 'TSCNN':
        trainerLMC = Trainer(modelLMC, train_loader, test_loaderLMC, criterion,
                             optimizer, summary_writer, DEVICE, args)
        trainerMC = Trainer(modelMC, train_loader, test_loaderMC, criterion,
                            optimizer, summary_writer, DEVICE, args)
        new_tscnn(trainerLMC, trainerMC)
        summary_writer.close()
        exit()
    else:
        trainer = Trainer(model, train_loader, test_loader, criterion,
                          optimizer, summary_writer, DEVICE, args)

    print("EPOCHS")
    print(args.epochs)

    trainer.train(
        args.epochs,
        args.val_frequency,
        print_frequency=args.print_frequency,
        log_frequency=args.log_frequency,
    )

    summary_writer.close()
Exemple #15
0
from torch.nn import functional as F
from torch.optim.optimizer import Optimizer
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter
from torchvision import transforms
import matplotlib.pyplot as plt

from dataset import UrbanSound8KDataset

summary_writer = SummaryWriter('logs', flush_secs=5)

print("cuda is available: ", torch.cuda.is_available())
device = torch.device('cuda' if torch.cuda.is_available() else "cpu")

#region data
train_loader_LMC = torch.utils.data.DataLoader(UrbanSound8KDataset(
    'UrbanSound8K_train.pkl', 'LMC'),
                                               batch_size=32,
                                               shuffle=True,
                                               num_workers=8,
                                               pin_memory=True)

test_loader_LMC = torch.utils.data.DataLoader(UrbanSound8KDataset(
    'UrbanSound8K_test.pkl', 'LMC'),
                                              batch_size=32,
                                              shuffle=False,
                                              num_workers=8,
                                              pin_memory=True)

train_loader_MC = torch.utils.data.DataLoader(UrbanSound8KDataset(
    'UrbanSound8K_train.pkl', 'MC'),
                                              batch_size=32,
Exemple #16
0
def main():

    parser = argparse.ArgumentParser(description="ESC Fusion model testing")
    parser.add_argument('mode', choices=['LMC', 'MC', 'MLMC', 'LMC+MC'])
    parser.add_argument('--weights_dir', type=str)
    parser.add_argument('--scores_input', nargs='+', type=Path)
    parser.add_argument('--scores_output', type=Path)
    parser.add_argument('--test_pickle')
    parser.add_argument('--mapping')
    parser.add_argument('--max_num', type=int, default=-1)
    parser.add_argument('--average', action='store_true')
    parser.add_argument('-j',
                        '--workers',
                        default=4,
                        type=int,
                        metavar='N',
                        help='number of data loading workers (default: 4)')

    args = parser.parse_args()

    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

    if args.mode != 'LMC+MC':
        print(args.mode)
        net = ESCModel(mode=args.mode)

        weights = '{weights_dir}/model_best.pth.tar'.format(
            weights_dir=args.weights_dir)
        checkpoint = torch.load(weights)
        print("model epoch {} best prec@1: {}".format(
            checkpoint['epoch'], checkpoint['best_prec1']))

        base_dict = {
            '.'.join(k.split('.')[1:]): v
            for k, v in list(checkpoint['state_dict'].items())
        }
        net.load_state_dict(base_dict)

        test_loader = torch.utils.data.DataLoader(UrbanSound8KDataset(
            args.test_pickle, args.mode),
                                                  batch_size=1,
                                                  shuffle=False,
                                                  num_workers=args.workers,
                                                  pin_memory=True)

        net = torch.nn.DataParallel(net, device_ids=None).to(device)
        with torch.no_grad():
            net.eval()

            results = []
            total_num = len(test_loader.dataset)

            proc_start_time = time.time()
            max_num = args.max_num if args.max_num > 0 else total_num
            for i, (data, label, fname) in enumerate(test_loader):
                if i >= max_num:
                    break
                data = data.to(device)
                rst = net(data)
                rst = rst.cpu().numpy().squeeze()
                label_ = label.item()
                results.append((rst, label_, fname))

                cnt_time = time.time() - proc_start_time
                print(
                    'video {} done, total {}/{}, average {} sec/video'.format(
                        i, i + 1, total_num,
                        float(cnt_time) / (i + 1)))

        if not args.scores_output.parent.exists():
            args.scores_output.parent.mkdir(parents=True)
        pickle.dump(results, open(args.scores_output, 'wb'))
        scores = np.array([res[0] for res in results])
        labels = np.array([res[1] for res in results])
        fname = np.array([res[2] for res in results])

        print_accuracy(scores,
                       labels,
                       fname,
                       pickle.load(open(args.mapping, 'rb')),
                       average_segments=args.average)
    else:
        lmc_results = pickle.load(open(args.scores_input[0], 'rb'))
        mc_results = pickle.load(open(args.scores_input[1], 'rb'))
        lmc_scores = np.array([res[0] for res in lmc_results])
        mc_scores = np.array([res[0] for res in mc_results])
        scores = np.array([lmc_scores, mc_scores])
        labels = np.array([res[1] for res in lmc_results])
        fname = np.array([res[2] for res in lmc_results])

        print_accuracy(scores,
                       labels,
                       fname,
                       pickle.load(open(args.mapping, 'rb')),
                       average_segments=args.average,
                       fuse=True)
Exemple #17
0
MLMC_test_accuracy = []

MC_train_loss = []
MC_test_loss = []
MC_train_accuracy = []
MC_test_accuracy = []


summary_writer = SummaryWriter('logs',flush_secs=5)

print("cuda is available: ", torch.cuda.is_available())
device = torch.device('cuda' if torch.cuda.is_available() else "cpu")

#region data
train_loader_LMC = torch.utils.data.DataLoader(
    UrbanSound8KDataset('UrbanSound8K_train.pkl', 'LMC'),
    batch_size=32, shuffle=True,
    num_workers=8, pin_memory=True)

#train_loader_LMC.input.to(device)

test_loader_LMC = torch.utils.data.DataLoader(
    UrbanSound8KDataset('UrbanSound8K_test.pkl', 'LMC'),
    batch_size=32, shuffle=False,
    num_workers=8, pin_memory=True)

# 
# from PIL import Image
# for i,(input,target,filenames) in enumerate(train_loader_LMC):
#     input2d = np.squeeze(input[i], axis=0)
#     plt.imshow(input2d.numpy())