Click here to Skip to main content
15,909,324 members

Comments by cemre aldoğan (Top 3 by date)

cemre aldoğan 10-Jul-23 5:43am View    
Thanks for helping. Val_data["LR_Mean"] was already defined in main py file. What I understood from dictionary is to change "" to ''. Thanks again.
cemre aldoğan 9-Jul-23 12:45pm View    
Deleted
# using matlab imresize
img_LR = util.imresize_np(img_HR, 1 / scale, True)
if img_LR.ndim == 2:
img_LR = np.expand_dims(img_LR, axis=2)

H, W, C = img_LR.shape
LR_size = HR_size #// scale


standarization = False

if standarization==True:
# assuming format CHW
lr_array_mean = img_LR.mean(axis=(1,2), keepdims=True)
lr_array_stddev = img_LR.std(axis=(1, 2), keepdims=True)

hr_array_mean = img_HR.mean(axis=(1, 2), keepdims=True)
hr_array_stddev = img_HR.std(axis=(1, 2), keepdims=True)

lr_array = (img_LR - lr_array_mean)/lr_array_stddev
hr_array = (img_HR - hr_array_mean) / hr_array_stddev
print("DATA STANDARIZED")

# randomly crop
rnd_h = random.randint(0, max(0, H - LR_size))
rnd_w = random.randint(0, max(0, W - LR_size))
img_LR = img_LR[rnd_h:rnd_h + LR_size, rnd_w:rnd_w + LR_size, :]
rnd_h_HR, rnd_w_HR = int(rnd_h * scale), int(rnd_w * scale)
img_HR = img_HR[rnd_h_HR:rnd_h_HR + HR_size, rnd_w_HR:rnd_w_HR + HR_size, :]

# augmentation - flip, rotate
img_LR, img_HR = util.augment([img_LR, img_HR], self.opt['use_flip'], \
self.opt['use_rot'])

# change color space if necessary
if self.opt['color']:
img_LR = util.channel_convert(C, self.opt['color'], [img_LR])[0] # TODO during val no definetion

# BGR to RGB, HWC to CHW, numpy to tensor
if img_HR.shape[2] == 3:
img_HR = img_HR[:, :, [2, 1, 0]]
img_LR = img_LR[:, :, [2, 1, 0]]


img_HR = torch.from_numpy(np.ascontiguousarray(np.transpose(img_HR, (2, 0, 1)))).float()
img_LR = torch.from_numpy(np.ascontiguousarray(np.transpose(img_LR, (2, 0, 1)))).float()
# print("2.................................")
#
# print(img_HR.shape)

if LR_path is None:
LR_path = HR_path
return {'LR': img_LR, 'HR': img_HR, 'LR_path': LR_path, 'HR_path': HR_path, "LR_mean":lr_array_mean, "LR_stddev":lr_array_stddev,
"HR_mean":hr_array_mean, "HR_stddev": hr_array_stddev}

def __len__(self):
# print("EJecuta LEN")

return len(self.paths_HR)
cemre aldoğan 9-Jul-23 12:44pm View    
Deleted
I tried to add lines to LRHR_Dataset.py as follows but it did not work:

standarization = False

if standarization==True:
# assuming format CHW
lr_array_mean = img_LR.mean(axis=(1,2), keepdims=True)
lr_array_stddev = img_LR.std(axis=(1, 2), keepdims=True)

hr_array_mean = img_HR.mean(axis=(1, 2), keepdims=True)
hr_array_stddev = img_HR.std(axis=(1, 2), keepdims=True)

lr_array = (img_LR - lr_array_mean)/lr_array_stddev
hr_array = (img_HR - hr_array_mean) / hr_array_stddev
print("DATA STANDARIZED")

Here is the full .py content with the last additions I just mentioned:

import os.path
import random
import numpy as np
import cv2
from pandas._libs.lib import is_datetime_with_singletz_array
import torch
import torch.utils.data as data
import data.util as util


class LRHRDataset(data.Dataset):
'''
Read LR and HR image pairs.
If only HR image is provided, generate LR image on-the-fly.
The pair is ensured by 'sorted' function, so please check the name convention.
'''

def __init__(self, opt):
super(LRHRDataset, self).__init__()
self.opt = opt
self.paths_LR = None
self.paths_HR = None
self.LR_env = None # environment for lmdb
self.HR_env = None

# read image list from subset list txt
if opt['subset_file'] is not None and opt['phase'] == 'train':
with open(opt['subset_file']) as f:
self.paths_HR = sorted([os.path.join(opt['dataroot_HR'], line.rstrip('\n')) \
for line in f])
if opt['dataroot_LR'] is not None:
raise NotImplementedError('Now subset only supports generating LR on-the-fly.')
else: # read image list from lmdb or image files
self.HR_env, self.paths_HR = util.get_image_paths(opt['data_type'], opt['dataroot_HR'])
self.LR_env, self.paths_LR = util.get_image_paths(opt['data_type'], opt['dataroot_LR'])

assert self.paths_HR, 'Error: HR path is empty.'
if self.paths_LR and self.paths_HR:
assert len(self.paths_LR) == len(self.paths_HR), \
'HR and LR datasets have different number of images - {}, {}.'.format(\
len(self.paths_LR), len(self.paths_HR))

self.random_scale_list = [1]

def __getitem__(self, index):
# print("EJecuta GeT ITEM")
HR_path, LR_path = None, None
scale = 0 #self.opt['scale']
HR_size = self.opt['HR_size']

# get HR image
HR_path = self.paths_HR[index]
img_HR = util.read_img(self.HR_env, HR_path)
# modcrop in the validation / test phase
if self.opt['phase'] != 'train':
# print("Opcion: ", self.opt["phase"])
img_HR = util.modcrop(img_HR, scale)
# change color space if necessary
if self.opt['color']:
img_HR = util.channel_convert(img_HR.shape[2], self.opt['color'], [img_HR])[0]

# get LR image
if self.paths_LR:
LR_path = self.paths_LR[index]
img_LR = util.read_img(self.LR_env, LR_path)
else: # down-sampling on-the-fly
# randomly scale during training
if self.opt['phase'] == 'train':
random_scale = random.choice(self.random_scale_list)
H_s, W_s, _ = img_HR.shape

def _mod(n, random_scale, scale, thres):
rlt = int(n * random_scale)
rlt = (rlt // scale) * scale
return thres if rlt < thres else rlt

H_s = _mod(H_s, random_scale, scale, HR_size)
W_s = _mod(W_s, random_scale, scale, HR_size)
img_HR = cv2.resize(np.copy(img_HR), (W_s, H_s), interpolation=cv2.INTER_LINEAR)