Saving the image using Torchvision

I am trying to save the bicubic and HR_np images. I can plot it right but I am having a hard time in saving it. Below is my code. I tried using torchvision.utils.save_image(imgs[‘bicubic_np’], ‘ccc.png’.format(), nrow=8, padding=2, normalize=False, range=None, scale_each=False, pad_value=0) but it did not work. Tell me where I am going wrong.

from future import print_function
import matplotlib.pyplot as plt
%matplotlib inline

import argparse
import os
os.environ[‘CUDA_VISIBLE_DEVICES’] = ‘0’

import numpy as np
from models import *

import torch
import torch.optim

from skimage.measure import compare_psnr
from models.downsampler import Downsampler

from utils.sr_utils import *

torch.backends.cudnn.enabled = True
torch.backends.cudnn.benchmark =True
dtype = torch.cuda.FloatTensor

imsize = -1
factor = 4 # 8
enforse_div32 = ‘CROP’ # we usually need the dimensions to be divisible by a power of two (32 in this case)
PLOT = True

To produce images from the paper we took *_GT.png images from LapSRN viewer for corresponding factor,

e.g. x4/zebra_GT.png for factor=4, and x8/zebra_GT.png for factor=8

path_to_image = ‘/home/smitha/Documents/Falcon.png’
imgs = load_LR_HR_imgs_sr(path_to_image , imsize, factor, enforse_div32)

imgs[‘bicubic_np’], imgs[‘sharp_np’], imgs[‘nearest_np’] = get_baselines(imgs[‘LR_pil’], imgs[‘HR_pil’])

if PLOT:
plot_image_grid([imgs[‘HR_np’], imgs[‘bicubic_np’], imgs[‘sharp_np’], imgs[‘nearest_np’]], 4,12);
print (‘PSNR bicubic: %.4f PSNR nearest: %.4f’ % (
compare_psnr(imgs[‘HR_np’], imgs[‘bicubic_np’]),
compare_psnr(imgs[‘HR_np’], imgs[‘nearest_np’])))
input_depth = 32

INPUT = ‘noise’
pad = ‘reflection’
OPT_OVER = ‘net’

LR = 0.01
tv_weight = 0.0

OPTIMIZER = ‘adam’

if factor == 4:
num_iter = 2000
reg_noise_std = 0.03
elif factor == 8:
num_iter = 4000
reg_noise_std = 0.05
assert False, ‘We did not experiment with other factors’
net_input = get_noise(input_depth, INPUT, (imgs[‘HR_pil’].size[1], imgs[‘HR_pil’].size[0])).type(dtype).detach()

NET_TYPE = ‘skip’ # UNet, ResNet
net = get_net(input_depth, ‘skip’, pad,


mse = torch.nn.MSELoss().type(dtype)

img_LR_var = np_to_torch(imgs[‘LR_np’]).type(dtype)

downsampler = Downsampler(n_planes=3, factor=factor, kernel_type=KERNEL_TYPE, phase=0.5, preserve_size=True).type(dtype)
def closure():
global i, net_input

if reg_noise_std > 0:
    net_input = net_input_saved + (noise.normal_() * reg_noise_std)

out_HR = net(net_input)
out_LR = downsampler(out_HR)

total_loss = mse(out_LR, img_LR_var) 

if tv_weight > 0:
    total_loss += tv_weight * tv_loss(out_HR)


# Log
psnr_LR = compare_psnr(imgs['LR_np'], torch_to_np(out_LR))
psnr_HR = compare_psnr(imgs['HR_np'], torch_to_np(out_HR))
print ('Iteration %05d    PSNR_LR %.3f   PSNR_HR %.3f' % (i, psnr_LR, psnr_HR), '\r', end='')

# History
psnr_history.append([psnr_LR, psnr_HR])

if PLOT and i % 100 == 0:
    out_HR_np = torch_to_np(out_HR)
    plot_image_grid([imgs['HR_np'], imgs['bicubic_np'], np.clip(out_HR_np, 0, 1)], factor=13, nrow=3)

i += 1

return total_loss

psnr_history = []
net_input_saved = net_input.detach().clone()
noise = net_input.detach().clone()

i = 0
p = get_params(OPT_OVER, net, net_input)
optimize(OPTIMIZER, p, closure, LR, num_iter)
out_HR_np = np.clip(torch_to_np(net(net_input)), 0, 1)
result_deep_prior = put_in_center(out_HR_np, imgs[‘orig_np’].shape[1:])

For the paper we acually took _bicubic.png files from LapSRN viewer and used result_deep_prior as our result

out_HR_np], factor=4, nrow=1);

What shape and type does imgs['bicubic_np'] have?

This code works fine:

images1 = [torch.randn(3, 96, 96) for _ in range(16)]
images2 = torch.randn(16, 3, 96, 96)
save_image(images1, 'test1.png', nrow=4)
save_image(images2, 'test2.png', nrow=4)

This is the type and shape <class ‘numpy.ndarray’> (1, 576, 576)
I used this code.


Also, now it shows save_image not found

I’ve imported save_image from torchvision.utils. You could also write torchvision.utils.save_image instead.
Cast the numpy array to a tensor using torch.from_numpy() and try it again.

I am new to this. I have never used torch.from_numpy(). Can you tell me how to do this?

Sure, no worries.
Here is a small example using random values for your imgs:

imgs = {}
imgs['bicubic_np'] = np.random.randn(1, 576, 576)
tensors_to_plot = torch.from_numpy(imgs['bicubic_np'])
torchvision.utils.save_image(tensors_to_plot, 'test.png')

You have just to use the last two lines of code as your array is already filled. :wink:


Yay! :smile: It worked. Thank you!