X_indices = X_data[0]
    X_params = X_data[1]
    X_pcs = X_data[2]
    print("Generator length: " + str(len(update_generator)))
    print("X_indices shape: " + str(X_indices.shape))
    print("X_params shape: " + str(X_params.shape))
    print("X_pcs shape: " + str(X_pcs.shape))

else:
    # Generate/load and format the data
    X_indices = np.array([i for i in range(data_samples)])
    X_params = np.array([zero_params for i in range(data_samples)],
                        dtype="float32")
    if DATA_LOAD_DIR is not None:
        X_params, X_pcs = load_data(DATA_LOAD_DIR,
                                    num_samples=data_samples,
                                    load_silhouettes=False)
    else:
        if not all(value == 0.0 for value in POSE_OFFSET.values()):
            X_params = offset_params(X_params, PARAMS_TO_OFFSET, POSE_OFFSET)
            X_pcs = np.array([
                smpl.set_params(beta=params[72:82],
                                pose=params[0:72],
                                trans=params[82:85]) for params in X_params
            ])
        else:
            X_pcs = np.array([zero_pc for i in range(data_samples)],
                             dtype="float32")

    if MODE == "EULER":
        # Convert from Rodrigues to Euler angles
def training_model_VDN(arg, milestones):
    logging.basicConfig(filename=arg.log_path, level=logging.INFO)  # log file
    logging.info('Started')
    if not os.path.exists(arg.model_path):
        os.makedirs(arg.model_path)
    # load training data
    train_sets = {
        "BSDS500": "*.jpg",
        "Waterloo": "*.bmp",
        "ImageNet": "*.JPEG"
    }
    if arg.confnet == 1:
        model = VDN_NET_one(in_channels=arg.channels, depth_snet=arg.snet)
    else:
        model = VDN_NET(in_channels=arg.channels, depth_snet=arg.snet)
    model = model.float()
    train_paths = np.array(sorted(gd.load_data("datasets", train_sets)))
    ran = bool(arguments.randomize)
    clipping = bool(arguments.clipping)
    data_obj = gd.TrainDataset(train_paths,
                               randomize=ran,
                               patch_size=arg.patch)
    if torch.cuda.is_available():
        model.cuda()
        torch.backends.cudnn.benchmark = True
        data = DataLoader(dataset=data_obj,
                          drop_last=True,
                          batch_size=arg.batch,
                          shuffle=True,
                          num_workers=arg.workers)
    else:
        data = DataLoader(dataset=data_obj,
                          drop_last=True,
                          batch_size=arg.batch,
                          shuffle=True)

    # network parameters
    epsilon = np.sqrt(5.0e-6)
    p_window = 7
    optimizer = optim.Adam(model.parameters(), lr=2e-4)
    scheduler = MultiStepLR(optimizer, milestones=milestones, gamma=arg.gamma)
    if clipping:
        gadient_clip_Dnet = 1000.0
        gadient_clip_Snet = 50.0
        Dnet_parameters = [
            x for name, x in model.named_parameters()
            if 'dnet' in name.lower()
        ]
        Snet_parameters = [
            x for name, x in model.named_parameters()
            if 'snet' in name.lower()
        ]

    print("Training model simulation now!")

    for epoch in range(arg.epochs):
        if clipping:
            grad_D = 0.0
            grad_S = 0.0
        epoch_avg_loss = 0.0
        mse_avg = 0.0
        psnr_avg = 0.0
        ssim_avg = 0.0
        lr = optimizer.param_groups[0]['lr']
        if lr < arg.learning:
            print("reach min learning rate at epoch" + str(epoch))
        for i, batch_data in enumerate(data):
            if torch.cuda.is_available():
                x_batch, y_batch, sigma_arr = Variable(
                    batch_data[0]).cuda(), Variable(
                        batch_data[1]).cuda(), Variable(batch_data[2]).cuda()
            else:
                x_batch, y_batch, sigma_arr = batch_data[0], batch_data[
                    1], batch_data[2]
            optimizer.zero_grad()
            out_D, out_s = model(y_batch)
            loss, loglikelihood, kl_z, kl_sigma = loss_func.get_loss(
                x_batch, y_batch, sigma_arr, p_window,
                out_D[:, :arg.channels, :, :], out_D[:, arg.channels:, :, :],
                out_s[:, :arg.channels, :, :], out_s[:, arg.channels:, :, :],
                epsilon)
            loss.backward()
            if clipping:
                full_grad_D = nn.utils.clip_grad_norm_(Dnet_parameters,
                                                       gadient_clip_Dnet)
                full_grad_S = nn.utils.clip_grad_norm_(Snet_parameters,
                                                       gadient_clip_Snet)
                grad_D = (grad_D * (i / (i + 1)) + full_grad_D / (i + 1))
                grad_S = (grad_S * (i / (i + 1)) + full_grad_S / (i + 1))
            optimizer.step()
            epoch_avg_loss += loss.detach().item()
            predicted_image = y_batch - out_D[:, :arg.channels, :, :].detach(
            ).data
            predicted_image = predicted_image.clamp(0, 1)
            mse = calc_MSE(predicted_image, x_batch)
            mse_avg += mse
            psnr_avg += psnr(predicted_image * 255, x_batch * 255)
            ssim_avg += calculate_ssim(img_as_ubyte(
                predicted_image.permute(2, 3, 1, 0).cpu().numpy()),
                                       img_as_ubyte(
                                           x_batch.permute(2, 3, 1,
                                                           0).cpu().numpy()),
                                       multichannel=True)
            if i == 0:
                print("First ForwardPAss\n Loss: {}, MSE: {}".format(
                    loss.detach().item(), mse))
            if (i + 1) % 100 == 0:
                print("{} - Loss: {}, MSE:{}, epoch:{}".format(
                    i + 1, loss.item(), mse, epoch + 1))
            if i >= 5000:
                break
        if clipping:
            gadient_clip_Dnet = min(gadient_clip_Dnet, grad_D)
            gadient_clip_Dnet = min(gadient_clip_Dnet, grad_S)
        print("----------------------------------------------------------")
        print(
            "Epoch: {},  Avg MSE:{},  Avg Epoch Loss:{},  Avg PSNR:{}, Avg SSIM : {}, LR:{}"
            .format(epoch + 1, mse_avg / (i + 1), epoch_avg_loss / (i + 1),
                    psnr_avg / (i + 1), ssim_avg / (i + 1), lr))
        logging.info("av loss: {}, epoch: {}".format(epoch_avg_loss / (i + 1),
                                                     epoch + 1))
        scheduler.step()
        if epoch % arguments.epoch_save == 0:
            torch.save(
                model.state_dict(),
                os.path.join(arg.model_path,
                             "model_" + str(epoch) + "_epochs.pth"))
            print("saved model as" + arg.model_path)
    print("Finished Training...\n Saving model now.....\n")
    torch.save(model.state_dict(),
               os.path.join(arg.model_path, "final_model.pth"))
    print("saved model as" + os.path.join(arg.model_path, "final_model.pth"))
def simulated_noise_test(noise_type,
                         model_path,
                         plot_noise=False,
                         stats=False,
                         plot_images=False,
                         conf1=False):
    test_sets = {"LIVE1": "*.bmp", "CBSD68": "*.png", "Set5": "*.bmp"}
    test_paths = sorted(
        gd.load_data(os.path.join("datasets", "test_data"), test_sets))
    data_obj = gd.TestDataset(test_paths, noise_type=noise_type, iid=True)
    if torch.cuda.is_available():
        if conf1:
            model = NET1.VDN_NET(in_channels=3, depth_snet=5).cuda()
        else:
            model = NET.VDN_NET(in_channels=3, depth_snet=5).cuda()
        model.load_state_dict(torch.load(model_path))
    else:
        model = NET.VDN_NET(in_channels=3, depth_snet=5)
        model.load_state_dict(torch.load(model_path, map_location='cpu'))
    model.eval()
    PSNR = 0.0
    SSIM = 0.0
    for idx in range(data_obj.__len__()):
        image, noisy = data_obj.__getitem__(idx)
        if torch.cuda.is_available():
            image, noisy = Variable(image.cuda()), Variable(noisy.cuda())
        _, ch, ht, wt = noisy.shape
        model_out, model_out2 = model(noisy)
        alpha = model_out2[:, :ch, ]
        beta = model_out2[:, ch:, ]
        result = beta / (alpha + 1)
        noise = noisy - model_out[:, :ch, ]
        clean_img_pred = noise.view(ch, ht, wt).permute(1, 2, 0).clamp(0, 1)
        image = image.view(ch, ht, wt).permute(1, 2, 0)
        if stats:
            PSNR += psnr(image * 255, clean_img_pred * 255)
            SSIM += compare_ssim(img_as_ubyte(image.cpu().detach().numpy()),
                                 img_as_ubyte(
                                     clean_img_pred.cpu().detach().numpy()),
                                 multichannel=True)
        if plot_noise:
            fig = plt.figure()
            ax = plt.axes(projection='3d')
            x = np.arange(0, ht, 1)
            y = np.arange(0, wt, 1)
            X, Y = np.meshgrid(x, y)
            Z1 = np.exp((img_as_float(result[0, 1, ].detach().numpy())))
            surf = ax.plot_surface(X[:100, :100],
                                   Y[:100, :100],
                                   Z1[:100, :100],
                                   rstride=1,
                                   cstride=1,
                                   cmap="viridis",
                                   antialiased=True,
                                   edgecolor="none")
            plt.savefig("simulation_results/approx_noise2_" + str(idx) +
                        ".png")
            plt.show()
        if plot_images:
            plt.subplots_adjust(wspace=0.2)
            plt.subplot(131)
            plt.imshow(image[100:250, 100:250, ])
            plt.title('Groundtruth')
            plt.subplot(132)
            plt.imshow(noisy[0].permute(1, 2, 0)[100:250, 100:250, ])
            plt.title('Noisy Image')
            plt.subplot(133)
            plt.imshow(
                img_as_ubyte(clean_img_pred.detach().numpy())[100:250,
                                                              100:250, ])
            plt.title('Denoised Image')
            plt.savefig("simulation_results/denoised_simlation" + str(idx) +
                        ".png")
            plt.show()
    print("average PSNR = ", PSNR / data_obj.__len__())
    print("average SSIM = ", SSIM / data_obj.__len__())
Exemplo n.º 4
0
        # Index hidden state of last time step
        # out.size() --> 100, 32, 100
        # out[:, -1, :] --> 100, 100 --> just want last time step hidden states!
        out = self.fc(out[:, -1, :])
        # out.size() --> 100, 10
        return out


############ main ############
seq_len = 300
batch_size = 1024
num_epochs = 20  #n_iters / (len(train_X) / batch_size)

# setup data
X_train, y_train, X_test, y_test = load_data(seq_len)
X_train, y_train, X_test, y_test = X_train, y_train, X_test, y_test

train = torch.utils.data.TensorDataset(X_train, y_train)
test = torch.utils.data.TensorDataset(X_test, y_test)

train_loader = torch.utils.data.DataLoader(dataset=train,
                                           batch_size=batch_size,
                                           shuffle=False,
                                           drop_last=True)

test_loader = torch.utils.data.DataLoader(dataset=test,
                                          batch_size=batch_size,
                                          shuffle=False,
                                          drop_last=True)