import torch import numpy as np from matplotlib.backends.backend_agg import FigureCanvasAgg from matplotlib.figure import Figure import matplotlib as mpl from matplotlib import cm import cv2 import os from datetime import datetime import shutil import torch.nn.functional as F from torch.autograd import Variable from math import exp import lpips lpips_alex = lpips.LPIPS(net="alex") # best forward scores lpips_vgg = lpips.LPIPS( net="vgg" ) # closer to "traditional" perceptual loss, when used for optimization HUGE_NUMBER = 1e10 TINY_NUMBER = 1e-6 # float32 only has 7 decimal digits precision img_HWC2CHW = lambda x: x.permute(2, 0, 1) gray2rgb = lambda x: x.unsqueeze(2).repeat(1, 1, 3) to8b = lambda x: (255 * np.clip(x, 0, 1)).astype(np.uint8) mse2psnr = lambda x: -10.0 * np.log(x + TINY_NUMBER) / np.log(10.0) def save_current_code(outdir): now = datetime.now() # current date and time date_time = now.strftime("%m_%d-%H:%M:%S") src_dir = "." dst_dir = os.path.join(outdir, "code_{}".format(date_time)) shutil.copytree( src_dir, dst_dir, ignore=shutil.ignore_patterns( "data*", "pretrained*", "logs*", "out*", "*.png", "*.mp4", "*__pycache__*", "*.git*", "*.idea*", "*.zip", "*.jpg", ), ) def img2mse(x, y, mask=None): """ :param x: img 1, [(...), 3] :param y: img 2, [(...), 3] :param mask: optional, [(...)] :return: mse score """ if mask is None: return torch.mean((x - y) * (x - y)) else: return torch.sum((x - y) * (x - y) * mask.unsqueeze(-1)) / ( torch.sum(mask) * x.shape[-1] + TINY_NUMBER ) def img2psnr(x, y, mask=None): return mse2psnr(img2mse(x, y, mask).item()) def cycle(iterable): while True: for x in iterable: yield x def get_vertical_colorbar(h, vmin, vmax, cmap_name="jet", label=None, cbar_precision=2): """ :param w: pixels :param h: pixels :param vmin: min value :param vmax: max value :param cmap_name: :param label :return: """ fig = Figure(figsize=(2, 8), dpi=100) fig.subplots_adjust(right=1.5) canvas = FigureCanvasAgg(fig) # Do some plotting. ax = fig.add_subplot(111) cmap = cm.get_cmap(cmap_name) norm = mpl.colors.Normalize(vmin=vmin, vmax=vmax) tick_cnt = 6 tick_loc = np.linspace(vmin, vmax, tick_cnt) cb1 = mpl.colorbar.ColorbarBase( ax, cmap=cmap, norm=norm, ticks=tick_loc, orientation="vertical" ) tick_label = [str(np.round(x, cbar_precision)) for x in tick_loc] if cbar_precision == 0: tick_label = [x[:-2] for x in tick_label] cb1.set_ticklabels(tick_label) cb1.ax.tick_params(labelsize=18, rotation=0) if label is not None: cb1.set_label(label) fig.tight_layout() canvas.draw() s, (width, height) = canvas.print_to_buffer() im = np.frombuffer(s, np.uint8).reshape((height, width, 4)) im = im[:, :, :3].astype(np.float32) / 255.0 if h != im.shape[0]: w = int(im.shape[1] / im.shape[0] * h) im = cv2.resize(im, (w, h), interpolation=cv2.INTER_AREA) return im def colorize_np( x, cmap_name="jet", mask=None, range=None, append_cbar=False, cbar_in_image=False, cbar_precision=2, ): """ turn a grayscale image into a color image :param x: input grayscale, [H, W] :param cmap_name: the colorization method :param mask: the mask image, [H, W] :param range: the range for scaling, automatic if None, [min, max] :param append_cbar: if append the color bar :param cbar_in_image: put the color bar inside the image to keep the output image the same size as the input image :return: colorized image, [H, W] """ if range is not None: vmin, vmax = range elif mask is not None: # vmin, vmax = np.percentile(x[mask], (2, 100)) vmin = np.min(x[mask][np.nonzero(x[mask])]) vmax = np.max(x[mask]) # vmin = vmin - np.abs(vmin) * 0.01 x[np.logical_not(mask)] = vmin # print(vmin, vmax) else: vmin, vmax = np.percentile(x, (1, 100)) vmax += TINY_NUMBER x = np.clip(x, vmin, vmax) x = (x - vmin) / (vmax - vmin) # x = np.clip(x, 0., 1.) cmap = cm.get_cmap(cmap_name) x_new = cmap(x)[:, :, :3] if mask is not None: mask = np.float32(mask[:, :, np.newaxis]) x_new = x_new * mask + np.ones_like(x_new) * (1.0 - mask) cbar = get_vertical_colorbar( h=x.shape[0], vmin=vmin, vmax=vmax, cmap_name=cmap_name, cbar_precision=cbar_precision ) if append_cbar: if cbar_in_image: x_new[:, -cbar.shape[1] :, :] = cbar else: x_new = np.concatenate((x_new, np.zeros_like(x_new[:, :5, :]), cbar), axis=1) return x_new else: return x_new # tensor def colorize(x, cmap_name="jet", mask=None, range=None, append_cbar=False, cbar_in_image=False): device = x.device x = x.cpu().numpy() if mask is not None: mask = mask.cpu().numpy() > 0.99 kernel = np.ones((3, 3), np.uint8) mask = cv2.erode(mask.astype(np.uint8), kernel, iterations=1).astype(bool) x = colorize_np(x, cmap_name, mask, range, append_cbar, cbar_in_image) x = torch.from_numpy(x).to(device) return x def gaussian(window_size, sigma): gauss = torch.Tensor( [exp(-((x - window_size // 2) ** 2) / float(2 * sigma**2)) for x in range(window_size)] ) return gauss / gauss.sum() def create_window(window_size, channel): _1D_window = gaussian(window_size, 1.5).unsqueeze(1) _2D_window = _1D_window.mm(_1D_window.t()).float().unsqueeze(0).unsqueeze(0) window = Variable(_2D_window.expand(channel, 1, window_size, window_size).contiguous()) return window def _ssim(img1, img2, window, window_size, channel, size_average=True): mu1 = F.conv2d(img1, window, padding=window_size // 2, groups=channel) mu2 = F.conv2d(img2, window, padding=window_size // 2, groups=channel) mu1_sq = mu1.pow(2) mu2_sq = mu2.pow(2) mu1_mu2 = mu1 * mu2 sigma1_sq = F.conv2d(img1 * img1, window, padding=window_size // 2, groups=channel) - mu1_sq sigma2_sq = F.conv2d(img2 * img2, window, padding=window_size // 2, groups=channel) - mu2_sq sigma12 = F.conv2d(img1 * img2, window, padding=window_size // 2, groups=channel) - mu1_mu2 C1 = 0.01**2 C2 = 0.03**2 ssim_map = ((2 * mu1_mu2 + C1) * (2 * sigma12 + C2)) / ( (mu1_sq + mu2_sq + C1) * (sigma1_sq + sigma2_sq + C2) ) if size_average: return ssim_map.mean() else: return ssim_map.mean(1).mean(1).mean(1) class SSIM(torch.nn.Module): def __init__(self, window_size=11, size_average=True): super(SSIM, self).__init__() self.window_size = window_size self.size_average = size_average self.channel = 1 self.window = create_window(window_size, self.channel) def forward(self, img1, img2): (_, channel, _, _) = img1.size() if channel == self.channel and self.window.data.type() == img1.data.type(): window = self.window else: window = create_window(self.window_size, channel) if img1.is_cuda: window = window.to(img1.device) window = window.type_as(img1) self.window = window self.channel = channel return _ssim(img1, img2, window, self.window_size, channel, self.size_average) def ssim_utils(img1, img2, window_size=11, size_average=True): (_, channel, _, _) = img1.size() window = create_window(window_size, channel) if img1.is_cuda: window = window.to(img1.device) window = window.type_as(img1) return _ssim(img1, img2, window, window_size, channel, size_average) def ssim(img1, img2, window_size=11, size_average=True, format="NCHW"): if format == "HWC": img1 = img1.permute([2, 0, 1])[None, ...] img2 = img2.permute([2, 0, 1])[None, ...] elif format == "NHWC": img1 = img1.permute([0, 3, 1, 2]) img2 = img2.permute([0, 3, 1, 2]) return ssim_utils(img1, img2, window_size, size_average) def lpips(img1, img2, net="vgg", format="NCHW"): if format == "HWC": img1 = img1.permute([2, 0, 1])[None, ...] img2 = img2.permute([2, 0, 1])[None, ...] elif format == "NHWC": img1 = img1.permute([0, 3, 1, 2]) img2 = img2.permute([0, 3, 1, 2]) if net == "alex": return lpips_alex(img1, img2) elif net == "vgg": return lpips_vgg(img1, img2) def concat_images(img0,img1,vert=False): if not vert: h0,h1=img0.shape[0],img1.shape[0], if h0