Utils

Utility functions

Compute attribute index


source

ComputeIndex

 ComputeIndex (codes)

Initialize self. See help(type(self)) for accurate signature.

device = 'cuda:0'
codes = {
        'exposure-time': torch.tensor([10, 50, 100], dtype=torch.float32, device=device),
        'optical-setup': torch.tensor([0, 1], dtype=torch.float32).to(device),
        'camera': torch.tensor([0, 1], dtype=torch.float32).to(device)
    }
kwargs = {
        'exposure-time': torch.tensor([100], dtype=torch.float32).to(device),
        'optical-setup': torch.tensor([1], dtype=torch.float32).to(device),
        'camera': torch.tensor([0], dtype=torch.float32).to(device)
    }

fn = ComputeIndex(codes)

print('index: ', fn(1, **kwargs))
index:  tensor([10.], device='cuda:0')

One-Hot encoding


source

ComputeOneHot

 ComputeOneHot (codes)

Initialize self. See help(type(self)) for accurate signature.

fn1hot = ComputeOneHot(codes)

print('one hot encoding: ', fn1hot(1, **kwargs))
one hot encoding:  tensor([[0., 0., 1., 0., 1., 1., 0.]], device='cuda:0')

Normal Distribution


source

sum_except_batch

 sum_except_batch (x, num_dims=1)

Sums all dimensions except the first. Args: x: Tensor, shape (batch_size, …) num_dims: int, number of batch dims (default=1) Returns: x_sum: Tensor, shape (batch_size,)


source

StandardNormal

 StandardNormal ()

A multivariate Normal with zero mean and unit covariance.

Base Utilities


source

attributesFromDict

 attributesFromDict (d)

The attributesFromDict function simplifies the conversion of dictionary keys and values into object attributes, allowing dynamic attribute creation for configuration objects. This utility is handy for initializing model or dataset configurations directly from dictionaries, improving code readability and maintainability.


source

np2tensor

 np2tensor (n:<built-infunctionarray>)

transform numpy array (image) to torch Tensor BGR -> RGB (h,w,c) -> (c,h,w)

a = np.random.rand(4, 4)
assert np2tensor(a).type() == 'torch.DoubleTensor'
a = np.random.randint(0, high=255, size=(4,4))
assert np2tensor(a).type() == 'torch.LongTensor'

source

np2tensor_multi

 np2tensor_multi (n:<built-infunctionarray>)

source

tensor2np

 tensor2np (t:torch.Tensor)

transform torch Tensor to numpy having opencv image form. RGB -> BGR (c,h,w) -> (h,w,c)


source

imread_tensor

 imread_tensor (name='test')

source

imwrite_tensor

 imwrite_tensor (t, name='test.png')

source

rot_hflip_img

 rot_hflip_img (img:torch.Tensor, rot_times:int=0, hflip:int=0)

rotate ‘90 x times degree’ & horizontal flip image (shape of img: b,c,h,w or c,h,w)


source

psnr

 psnr (x, y, mask=None, max_val=1.0)

source

ssim

 ssim (img1:torch.Tensor, img2:torch.Tensor, data_range)

image value range : [0 - data_range] clipping for model output


source

AverageMeter

 AverageMeter ()

Computes and stores the average and current value.


source

setup_determinism

 setup_determinism (seed)

source

get_gaussian_2d_filter

 get_gaussian_2d_filter (window_size, sigma, channel=1,
                         device=device(type='cpu'))

return 2d gaussian filter window as tensor form Arg: window_size : filter window size sigma : standard deviation


source

get_mean_2d_filter

 get_mean_2d_filter (window_size, channel=1, device=device(type='cpu'))

return 2d mean filter as tensor form Args: window_size : filter window size


source

mean_conv2d

 mean_conv2d (x, window_size=None, window=None, filter_type='gau',
              sigma=None, keep_sigma=False, padd=True)

color channel-wise 2d mean or gaussian convolution Args: x : input image window_size : filter window size filter_type(opt) : ‘gau’ or ‘mean’ sigma : standard deviation of gaussian filter


source

get_file_name_from_path

 get_file_name_from_path (path)

source

get_histogram

 get_histogram (data, bin_edges=None, cnt_regr=1)

source

kl_div_forward

 kl_div_forward (p, q)

source

kl_div_3_data

 kl_div_3_data (real_noise, gen_noise, bin_edges=None, left_edge=0.0,
                right_edge=1.0)

source

load_numpy_from_raw

 load_numpy_from_raw (path, dtype='float32')

source

make_predefiend_1d_to_2d

 make_predefiend_1d_to_2d (arr)

source

save_img

 save_img (dir_name, file_name, img)

File Manager


source

FileManager

 FileManager (session_name, output_path=None)

Initialize self. See help(type(self)) for accurate signature.

Logging

Progress Message


source

ProgressMsg

 ProgressMsg (max_iter, min_time_interval=0.1)

Args: max_iter : (max_epoch, max_data_length, …) min_time_interval (second)

logging.basicConfig(
        format='%(message)s',
        level=logging.INFO,
        handlers=[logging.StreamHandler()]
        )

min_time = 1
max_iter = 1

pp = ProgressMsg((max_iter,min_time))
ss = (0, 0)

pp.start(ss)

for i in range(0, max_iter):
    for j in range(max_iter):
        for k in range(max_iter):
            time.sleep(0.5)
            pp.print_prog_msg((i, j))
        logging.info('ttt')
ttt
>>> progress : 0.00%, elapsed: 0:00:00, remaining: INF, total: INF                  

Logger


source

bcolors

 bcolors ()

Initialize self. See help(type(self)) for accurate signature.


source

Logger

 Logger (max_iter:tuple=None, log_dir:str=None, log_file_option:str='w',
         log_lvl:str='note', log_file_lvl:str='info',
         log_include_time:bool=True)

Args: session_name (str) max_iter (tuple) : max iteration for progress log_dir (str) : if None, no file out for logging log_file_option (str) : ‘w’ or ‘a’ log_lvl (str) : ‘debug’ < ‘note’ < ‘info’ < ‘highlight’ < ‘val’ log_include_time (bool)