コード例 #1
0
ファイル: train.py プロジェクト: jakthra/PseudoRayTracingOSM
def run(args):

    args.cuda = not args.no_cuda and torch.cuda.is_available()

    if args.cuda:
        torch.cuda.empty_cache()

    torch.manual_seed(args.seed)
    if args.cuda:
        print('CUDA enabled')
        torch.cuda.manual_seed(args.seed)

    if not args.model_mode == 'features-only':
        args.use_images = True
        print("Using images.")
    else:
        args.use_images = False

    if args.no_data_augment:
        transform = False
    else:
        transform = True
        print("Using data augmentation")

    num_workers = 4

    # Load data
    #Wtrain_dataset, test_dataset = dataset_factory(use_images=args.use_images, transform=transform, data_augment_angle=args.data_augmentation_angle, image_folder='images/snap_dk_250_64_64') # No image folder means loading from hdf5 file
    train_dataset, test_dataset = dataset_factory()
    train_loader = torch.utils.data.DataLoader(train_dataset,
                                               batch_size=args.batch_size,
                                               shuffle=True,
                                               num_workers=num_workers,
                                               drop_last=True)
    test_loader = torch.utils.data.DataLoader(test_dataset,
                                              batch_size=args.batch_size,
                                              num_workers=num_workers,
                                              drop_last=False,
                                              shuffle=False)
    print(train_dataset.image_size)
    # Instansiate model
    args.num_features = train_dataset.features.shape[
        1] + 1  # Path loss model is an additional feature
    args.image_size = [
        train_dataset.image_size[0], train_dataset.image_size[1]
    ]
    args.out_channels = [
        int(args.out_channels_l1),
        int(args.out_channels_l2), 10, 1
    ]
    args.kernel_size = [(int(args.kernel_size_l1), int(argcs.kernel_size_l1)),
                        (3, 3), (3, 3), (2, 2)]
    args.nn_layers = [int(args.nn_layer_size), int(args.nn_layer_size)]
    args.channels = 1

    #rsrp_mu = train_dataset.target_mu
    #rsrp_std = train_dataset.target_std

    model = SkynetModel(args, train_dataset.target_scaler)
    if args.cuda:
        model.cuda()

    wandb.init(project="osm_pseudo_raytracing", config=args)
    wandb.watch(model)
    # Define loss function, optimizer and LR scheduler
    criterion = nn.MSELoss()
    optimizer = optim.Adam(model.parameters(),
                           lr=args.lr,
                           weight_decay=args.weight_decay)
    scheduler_model = lr_scheduler.ReduceLROnPlateau(optimizer, patience=10)

    # Training loop
    train_loss = []
    test_loss = []

    def train(epoch):
        # Called by the loop
        trainloss = 0
        with tqdm(total=len(train_loader)) as pbar:
            for idx, (feature, image, target,
                      dist_freq_offset) in enumerate(train_loader):
                if args.cuda:
                    image = image.cuda()
                    feature = feature.cuda()
                    target = target.cuda()
                    dist = dist_freq_offset[0].cuda()
                    freq = dist_freq_offset[1].cuda()
                    offset = dist_freq_offset[2].cuda()

                optimizer.zero_grad()

                output, sum_output = model(feature, image, dist, freq, offset)

                loss = criterion(sum_output, target)
                loss.backward()
                trainloss += loss.item()
                optimizer.step()
                pbar.update(1)

            train_loss.append(trainloss / idx)

        pbar.close()

    def test(epoch):
        # Called by the loop
        testloss = 0
        with torch.no_grad():
            with tqdm(total=len(test_loader)) as pbar:
                for idx, (feature, image, target,
                          dist_freq_offset) in enumerate(test_loader):
                    if args.cuda:
                        image = image.cuda()
                        feature = feature.cuda()
                        target = target.cuda()
                        dist = dist_freq_offset[0].cuda()
                        freq = dist_freq_offset[1].cuda()
                        offset = dist_freq_offset[2].cuda()

                    output, sum_output = model(feature, image, dist, freq,
                                               offset)

                    loss = criterion(sum_output, target)
                    testloss += loss.item()
                    pbar.update(1)

                test_loss.append(testloss / idx)
            pbar.close()

    for epoch in range(args.epochs):
        model.train()
        train(epoch)
        model.eval()
        test(epoch)
        scheduler_model.step(test_loss[-1])
        wandb.log({"test_loss": test_loss[-1], "train_loss": train_loss[-1]})
        print("Epoch: {}, train_loss: {}, test_loss: {}".format(
            epoch, train_loss[-1], test_loss[-1]))

        if optimizer.param_groups[0]['lr'] < 1e-7:
            print('Learning rate too low. Early stopping.')
            break

    exp = Experiment('file', config=args.__dict__, root_folder='exps')
    results_dict = dict()
    results_dict['train_loss'] = train_loss
    results_dict['test_loss'] = test_loss
    exp.results = results_dict
    exp.save()

    torch.save(
        model.state_dict(), exp.root_folder +
        '\\models\\{}_model_{:.3f}.pt'.format(exp.id, test_loss[-1]))
コード例 #2
0
def run(args):
    cuda = not args.no_cuda and torch.cuda.is_available()

    if cuda:
        torch.cuda.empty_cache()

    torch.manual_seed(args.seed)
    if cuda:
        print('CUDA enabled')
        torch.cuda.manual_seed(args.seed)

    # Load data

    # Load experiment

    exp_root_path = args.exp_folder + "/"

    exp = load_experiment(args.name, root_path=exp_root_path)
    name = args.name
    args = edict(exp.config)
    args.name = name
    args.cuda = cuda
    args.data_augmentation_angle = 20
    # compatibility
    if not 'offset_811' in args:
        args.offset_811 = 18

    if not 'offset_2630' in args:
        args.offset_2630 = 0

    train_dataset, test_dataset = dataset_factory(
        use_images=args.use_images,
        use_hdf5=True,
        transform=True,
        data_augment_angle=args.data_augmentation_angle)
    train_loader = torch.utils.data.DataLoader(train_dataset,
                                               batch_size=args.batch_size,
                                               shuffle=True,
                                               num_workers=0,
                                               drop_last=True)
    test_loader = torch.utils.data.DataLoader(test_dataset,
                                              batch_size=args.batch_size,
                                              num_workers=0,
                                              drop_last=False,
                                              shuffle=False)
    print(len(test_loader))

    rsrp_mu = train_dataset.target_mu
    rsrp_std = train_dataset.target_std

    model = SkynetModel(args, rsrp_mu=rsrp_mu, rsrp_std=rsrp_std)

    if args.cuda:
        model.cuda()

    # Find model name
    list_of_files = os.listdir('{}models/'.format(
        exp_root_path))  #list of files in the current directory
    for each_file in list_of_files:
        if each_file.startswith(args.name):
            name = each_file

    model.load_state_dict(torch.load('{}models/{}'.format(exp_root_path,
                                                          name)))
    model.eval()
    criterion = nn.MSELoss()
    MSE_loss_batch = 0
    with torch.no_grad():
        for idx, (feature, image, target, dist) in enumerate(test_loader):
            if args.cuda:
                image = image.cuda()
                feature = feature.cuda()
                target = target.cuda()
                dist = dist.cuda()

            correction_, sum_output_ = model(feature, image, dist)
            P = model.predict_physicals_model(feature, dist)

            MSE_loss_batch += criterion(sum_output_, target)
            try:
                p = torch.cat([p, P], 0)
            except:
                p = P

            try:
                correction = torch.cat([correction, correction_], 0)
            except:
                correction = correction_

            try:
                sum_output = torch.cat([sum_output, sum_output_], 0)
            except:
                sum_output = sum_output_

            try:
                features = torch.cat([features, feature], 0)
            except:
                features = feature

    # Check if folder with name in results exist
    results_folder_path = 'results/{}'.format(args.name)
    if not os.path.exists(results_folder_path):
        os.mkdir(results_folder_path)

    # Store predictions
    np.save(results_folder_path + "/correction.npy", correction)
    np.save(results_folder_path + "/sum_output.npy", sum_output)
    np.save(results_folder_path + "/pathloss_model.npy", P)
コード例 #3
0
def run(args):
    cuda = not args.no_cuda and torch.cuda.is_available()

    if cuda:
        torch.cuda.empty_cache()

    torch.manual_seed(args.seed)
    if cuda:
        print('CUDA enabled')
        torch.cuda.manual_seed(args.seed)

    # Load data

    # Load experiment

    exp_root_path = args.exp_folder + "/"

    exp = load_experiment(args.name, root_path=exp_root_path)
    name = args.name
    args = edict(exp.config)
    args.name = name
    args.cuda = cuda

    train_dataset, test_dataset = dataset_factory(args)
    #train_loader = torch.utils.data.DataLoader(dtu_dataset, batch_size=args.batch_size, shuffle=False, drop_last=False)

    target_scaler = train_dataset.datasets[0].target_scaler

    model = SkynetModel(args, target_scaler)

    if args.cuda:
        model.cuda()

    # Find model name
    list_of_files = os.listdir('{}models/'.format(
        exp_root_path))  #list of files in the current directory
    for each_file in list_of_files:
        if each_file.startswith(args.name):
            name = each_file

    print(name)

    model.load_state_dict(torch.load('{}models/{}'.format(exp_root_path,
                                                          name)))
    model.eval()
    criterion = nn.MSELoss()

    def evaluate_dataset(dataset_loader, scaler, args):
        with torch.no_grad():
            MSE = np.zeros((len(dataset_loader), ))
            RMSE = np.zeros((len(dataset_loader), ))
            for idx, (feature, image, target,
                      dist_freq_offset) in enumerate(dataset_loader):
                if args.cuda:
                    image = image.cuda()
                    feature = feature.cuda()
                    target_ = target.cuda()
                    dist = dist_freq_offset[0].cuda()
                    freq = dist_freq_offset[1].cuda()
                    offset = dist_freq_offset[2].cuda()

                correction_, sum_output_ = model(feature, image, dist, freq,
                                                 offset)
                P = model.predict_physicals_model(feature, dist, freq, offset)

                unnorm_predicted = scaler.inverse_transform(
                    sum_output_.cpu().numpy())
                unnorm_target = scaler.inverse_transform(target_.cpu().numpy())
                MSE[idx] = mean_squared_error(unnorm_predicted, unnorm_target)
                RMSE[idx] = mean_squared_error(unnorm_predicted,
                                               unnorm_target,
                                               squared=False)
                # try:
                #     p = torch.cat([p, P], 0)
                # except:
                #     p = P

                # try:
                #     correction = torch.cat([correction, correction_],0)
                # except:
                #     correction = correction_

                try:
                    sum_output = torch.cat([sum_output, sum_output_], 0)
                except:
                    sum_output = sum_output_

        # try:
        #     features = torch.cat([features, feature],0)
        # except:
        #     features = feature

        return sum_output, MSE, RMSE

    test_loader = torch.utils.data.DataLoader(test_dataset,
                                              batch_size=args.batch_size,
                                              drop_last=False,
                                              shuffle=False)
    sum_output, MSE__, RMSE__ = evaluate_dataset(test_loader, target_scaler,
                                                 args)
    print(RMSE__)

    fig = plt.figure()
    ax = plt.subplot(111)
    sns.set(style='darkgrid')
    sns.boxplot(data=RMSE__, width=0.5, ax=ax)
    ax.set_xlabel("Scenarios")
    ax.set_ylabel("RMSE [dB]")
    plt.xticks(plt.xticks()[0], RMSE.keys())
    plt.show()
コード例 #4
0
def run(args):
    cuda = not args.no_cuda and torch.cuda.is_available()


    

    if cuda:
        torch.cuda.empty_cache()

    torch.manual_seed(args.seed)
    if cuda:
        print('CUDA enabled')
        torch.cuda.manual_seed(args.seed)


    # Load data

    # Load experiment

    exp_root_path = args.exp_folder+"/"

    exp = load_experiment(args.name, root_path = exp_root_path)
    name = args.name
    args = edict(exp.config)
    args.name = name
    args.cuda = cuda
    args.data_augmentation_angle = 20
    
    # compatibility 
    if not 'offset_811' in args:
        args.offset_811 = 18
    
    if not 'offset_2630' in args:
        args.offset_2630 = 0
    
    train_dataset, test_dataset = dataset_factory(use_images=args.use_images, transform=True, data_augment_angle=args.data_augmentation_angle)
    train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=args.batch_size, shuffle=True, num_workers=0, drop_last=True)
    test_loader = torch.utils.data.DataLoader(test_dataset, batch_size=args.batch_size, num_workers=0, drop_last=False, shuffle=False)
    print(len(test_loader))


    rsrp_mu = train_dataset.target_mu
    rsrp_std = train_dataset.target_std
   

    model = SkynetModel(args, rsrp_mu = rsrp_mu, rsrp_std = rsrp_std)
 
    if args.cuda:
        model.cuda()

    # Find model name
    list_of_files = os.listdir('{}models/'.format(exp_root_path)) #list of files in the current directory
    for each_file in list_of_files:
        if each_file.startswith(args.name):  
            name = each_file

    print(name)
            

    model.load_state_dict(torch.load('{}models/{}'.format(exp_root_path, name)))
    model.eval()
    criterion = nn.MSELoss()
    MSE_loss_batch = 0
    with torch.no_grad():
        for idx, (feature, image, target, dist) in enumerate(test_loader):
            if args.cuda:
                image = image.cuda()
                feature = feature.cuda()
                target = target.cuda()
                dist = dist.cuda()
            I = image
            for block in model.ImageModel.blocks:
                I = block(I)
                print(I.shape)
コード例 #5
0
def run(args):
    cuda = not args.no_cuda and torch.cuda.is_available()

    if cuda:
        torch.cuda.empty_cache()

    torch.manual_seed(args.seed)
    if cuda:
        print('CUDA enabled')
        torch.cuda.manual_seed(args.seed)

    # Load data

    # Load experiment

    exp_root_path = args.exp_folder + "/"

    exp = load_experiment(args.name, root_path=exp_root_path)
    name = args.name
    args = edict(exp.config)
    args.name = name
    args.cuda = cuda
    args.data_augmentation_angle = 20

    # compatibility
    if not 'offset_811' in args:
        args.offset_811 = 18

    if not 'offset_2630' in args:
        args.offset_2630 = 0

    train_dataset, test_dataset = dataset_factory(
        use_images=args.use_images,
        transform=True,
        data_augment_angle=args.data_augmentation_angle)
    train_loader = torch.utils.data.DataLoader(train_dataset,
                                               batch_size=args.batch_size,
                                               shuffle=True,
                                               num_workers=0,
                                               drop_last=True)
    test_loader = torch.utils.data.DataLoader(test_dataset,
                                              batch_size=args.batch_size,
                                              num_workers=0,
                                              drop_last=False,
                                              shuffle=False)
    print(len(test_loader))

    rsrp_mu = train_dataset.target_mu
    rsrp_std = train_dataset.target_std

    model = SkynetModel(args, rsrp_mu=rsrp_mu, rsrp_std=rsrp_std)

    if args.cuda:
        model.cuda()

    # Find model name
    list_of_files = os.listdir('{}models/'.format(
        exp_root_path))  #list of files in the current directory
    for each_file in list_of_files:
        if each_file.startswith(args.name):
            name = each_file

    print(name)

    model.load_state_dict(torch.load('{}models/{}'.format(exp_root_path,
                                                          name)))
    model.eval()
    criterion = nn.MSELoss()
    MSE_loss_batch = 0
    with torch.no_grad():
        for idx, (feature, image, target, dist) in enumerate(test_loader):
            if args.cuda:
                image = image.cuda()
                feature = feature.cuda()
                target = target.cuda()
                dist = dist.cuda()

            correction_, sum_output_ = model(feature, image, dist)
            P = model.predict_physicals_model(feature, dist)

            MSE_loss_batch += criterion(sum_output_, target)
            try:
                p = torch.cat([p, P], 0)
            except:
                p = P

            try:
                correction = torch.cat([correction, correction_], 0)
            except:
                correction = correction_

            try:
                sum_output = torch.cat([sum_output, sum_output_], 0)
            except:
                sum_output = sum_output_

            try:
                features = torch.cat([features, feature], 0)
            except:
                features = feature

    correction_unnorm = (correction * model.rsrp_std)

    def correct_hist_plot(correction_unnorm, features):
        fig = plt.figure(figsize=(7, 3))
        sns.set_style('darkgrid')
        sns.distplot(correction_unnorm[features[:, 7] == 1].cpu().numpy(),
                     label='2630 MHz')
        sns.distplot(correction_unnorm[features[:, 7] != 1].cpu().numpy(),
                     label='811 MHz')
        plt.xlabel('RSRP correction [dB]')
        plt.ylabel('Frequency')
        plt.legend()
        plt.savefig('results/rsrp_correction_hist_{}.eps'.format(args.name))
        plt.savefig('results/rsrp_correction_hist_{}.png'.format(args.name))
        plt.show()

    def cdf_hist_plot(target, predicted, theoretical, mhz):
        fig = plt.figure(figsize=(5, 5))
        sns.set_style('darkgrid')
        sns.distplot(target,
                     hist_kws=dict(cumulative=True),
                     kde_kws=dict(cumulative=True),
                     label='Target')
        sns.distplot(predicted,
                     hist_kws=dict(cumulative=True),
                     kde_kws=dict(cumulative=True),
                     label='Predicted')
        #sns.distplot(theoretical, hist_kws=dict(cumulative=True), kde_kws=dict(cumulative=True), label='38.901 UMa')
        plt.legend()
        plt.xlabel('RSRP [dBm]')
        plt.tight_layout()
        plt.savefig('results/cdf_{}_{}mhz.eps'.format(args.name, mhz))
        plt.savefig('results/cdf_{}_{}mhz.png'.format(args.name, mhz))
        plt.show()

    def hist_plot(target, predicted, theoretical, mhz):
        fig = plt.figure(figsize=(5, 5))
        sns.set_style('darkgrid')
        sns.distplot(target, label='Target')
        sns.distplot(predicted, label='Predicted')
        #sns.distplot(theoretical, label='38.901 UMa')
        plt.xlabel('RSRP [dBm]')
        plt.legend()
        plt.tight_layout()
        plt.savefig('results/hist_{}_{}mhz.eps'.format(args.name, mhz))
        plt.show()

    #cdf_hist_plot(test_dataset.targets, p.cpu().numpy())

    # correction_hist_plot(correction_unnorm, features)

    # Compute RMSE for model and pathloss model

    print('Test MSE batch norm {}'.format(MSE_loss_batch / idx))
    MSE_loss_model = criterion(sum_output.cpu(),
                               torch.from_numpy(test_dataset.targets).float())
    print('Test MSE norm {}'.format(MSE_loss_model.item()))
    RMSE_model = np.sqrt(MSE_loss_model.item()) * model.rsrp_std.numpy()
    print('Test RMSE unnorm {}'.format(RMSE_model))

    MSE_pathloss_model = criterion(
        p.cpu(),
        torch.from_numpy(test_dataset.targets).float())
    print("Pathloss model MSE {}".format(MSE_pathloss_model.item()))
    RMSE_pathloss = np.sqrt(MSE_pathloss_model.item()) * model.rsrp_std.numpy()
    print("Pathloss model RMSE {}".format(RMSE_pathloss))

    # Save JSON with evaluation results
    results = dict()
    results['RMSE_unnorm'] = RMSE_model
    results['MSE_loss'] = MSE_loss_model.item()
    results['MSE_pathloss_model'] = MSE_pathloss_model.item()
    results['RMSE_pathloss'] = RMSE_pathloss

    idx_811mhz = test_dataset.get_811Mhz_idx()
    idx_2630mhz = test_dataset.get_2630Mhz_idx()
    MSE_loss_811mhz = criterion(
        sum_output[idx_811mhz].cpu(),
        torch.from_numpy(test_dataset.targets[idx_811mhz]).float())
    MSE_loss_2630mhz = criterion(
        sum_output[idx_2630mhz].cpu(),
        torch.from_numpy(test_dataset.targets[idx_2630mhz]).float())
    print("MSE Loss at 811 MHz: {}".format(MSE_loss_811mhz))
    print("MSE Loss at 2630 MHz: {}".format(MSE_loss_2630mhz))
    RMSE_811 = np.sqrt(MSE_loss_811mhz.item()) * model.rsrp_std.numpy()
    RMSE_2630 = np.sqrt(MSE_loss_2630mhz.item()) * model.rsrp_std.numpy()
    print("RMSE 811 MHz {}".format(RMSE_811))
    print("RMSE 2630 MHz {}".format(RMSE_2630))
    results['RMSE_811'] = RMSE_811
    results['RMSE_2630'] = RMSE_2630

    MSE_pathloss_811 = criterion(
        p[idx_811mhz].cpu(),
        torch.from_numpy(test_dataset.targets[idx_811mhz]).float())
    MSE_pathloss_2630 = criterion(
        p[idx_2630mhz].cpu(),
        torch.from_numpy(test_dataset.targets[idx_2630mhz]).float())
    RMSE_pathloss_811 = np.sqrt(
        MSE_pathloss_811.item()) * model.rsrp_std.numpy()
    RMSE_pathloss_2630 = np.sqrt(
        MSE_pathloss_2630.item()) * model.rsrp_std.numpy()
    results['RMSE_pathloss_811'] = RMSE_pathloss_811
    results['RMSE_pathloss_2630'] = RMSE_pathloss_2630

    plt.rcParams.update({'font.size': 18})
    pred_811_unnorm = (sum_output[idx_811mhz].cpu().numpy() *
                       test_dataset.target_std) + test_dataset.target_mu
    pred_2630_unnorm = (sum_output[idx_2630mhz].cpu().numpy() *
                        test_dataset.target_std) + test_dataset.target_mu
    uma_811_unnorm = (p[idx_811mhz].cpu().numpy() *
                      test_dataset.target_std) + test_dataset.target_mu
    uma_2630_unnorm = (p[idx_2630mhz].cpu().numpy() *
                       test_dataset.target_std) + test_dataset.target_mu
    cdf_hist_plot(test_dataset.targets_unnorm[idx_811mhz], pred_811_unnorm,
                  uma_811_unnorm, '811')
    cdf_hist_plot(test_dataset.targets_unnorm[idx_2630mhz], pred_2630_unnorm,
                  uma_2630_unnorm, '2630')
    hist_plot(test_dataset.targets_unnorm[idx_811mhz], pred_811_unnorm,
              uma_811_unnorm, '811')
    hist_plot(test_dataset.targets_unnorm[idx_2630mhz], pred_2630_unnorm,
              uma_2630_unnorm, '2630')

    results_file_name = args.name + "_results.json"

    with open('results/evaluations/{}'.format(results_file_name),
              'w') as output:
        json.dump(results, output)