import random import numpy as np import torch import torchvision.transforms as transforms import cv2 transform = transforms.Compose([ transforms.ToTensor(), transforms.Normalize(mean = (0.5, 0.5, 0.5), std = (0.5, 0.5, 0.5)) ] ) def tensor2im(image_tensor, imtype=np.uint8, gray=False, rgb2bgr = True ,is0_1 = False): image_tensor =image_tensor.data image_numpy = image_tensor[0].cpu().float().numpy() # if gray: # image_numpy = (image_numpy+1.0)/2.0 * 255.0 # else: if image_numpy.shape[0] == 1: image_numpy = np.tile(image_numpy, (3, 1, 1)) image_numpy = image_numpy.transpose((1, 2, 0)) if not is0_1: image_numpy = (image_numpy + 1)/2.0 image_numpy = np.clip(image_numpy * 255.0,0,255) if rgb2bgr and not gray: image_numpy = image_numpy[...,::-1]-np.zeros_like(image_numpy) return image_numpy.astype(imtype) def im2tensor(image_numpy, imtype=np.uint8, gray=False,bgr2rgb = True, reshape = True, use_gpu = True, use_transform = True): if gray: h, w = image_numpy.shape image_numpy = (image_numpy/255.0-0.5)/0.5 image_tensor = torch.from_numpy(image_numpy).float() if reshape: image_tensor=image_tensor.reshape(1,1,h,w) else: h, w ,ch = image_numpy.shape if bgr2rgb: image_numpy = image_numpy[...,::-1]-np.zeros_like(image_numpy) if use_transform: image_tensor = transform(image_numpy) else: image_numpy = image_numpy/255.0 image_numpy = image_numpy.transpose((2, 0, 1)) image_tensor = torch.from_numpy(image_numpy).float() if reshape: image_tensor=image_tensor.reshape(1,ch,h,w) if use_gpu: image_tensor = image_tensor.cuda() return image_tensor def random_transform_video(src,target,finesize,N): #random crop h,w = target.shape[:2] h_move = int((h-finesize)*random.random()) w_move = int((w-finesize)*random.random()) # print(h,w,h_move,w_move) target = target[h_move:h_move+finesize,w_move:w_move+finesize,:] src = src[h_move:h_move+finesize,w_move:w_move+finesize,:] #random flip if random.random()<0.5: src = src[:,::-1,:] target = target[:,::-1,:] #random color random_num = 15 bright = random.randint(-random_num*2,random_num*2) for i in range(N*3): src[:,:,i]=np.clip(src[:,:,i].astype('int')+bright,0,255).astype('uint8') for i in range(3): target[:,:,i]=np.clip(target[:,:,i].astype('int')+bright,0,255).astype('uint8') return src,target def random_transform_image(img,mask,finesize): # randomsize = int(finesize*(1.2+0.2*random.random())+2) h,w = img.shape[:2] loadsize = min((h,w)) a = (float(h)/float(w))*random.uniform(0.9, 1.1) if h