|
- import random
- # import torch
- # import torch.nn.functional as F
- import mindspore.ops as ops
- import numpy as np
- import imageio
- from mindspore import Tensor
-
- def random_crop_and_pad_image_and_labels(image, labels, size):
- combined = ops.cat([image, labels], 0)
- last_image_dim = image.shape[0]
- image_shape = image.shape
- combined_pad = ops.pad(combined,
- (0, max(size[1], image_shape[2]) - image_shape[2], 0, max(size[0], image_shape[1]) - image_shape[1]))
- freesize0 = random.randint(0, max(size[0], image_shape[1]) - size[0])
- freesize1 = random.randint(0, max(size[1], image_shape[2]) - size[1])
- combined_crop = combined_pad[:, freesize0:freesize0 + size[0], freesize1:freesize1 + size[1]]
- return (combined_crop[:last_image_dim, :, :], combined_crop[last_image_dim:, :, :])
-
- def random_crop_and_pad_image_and_labels_np(image, labels, size):
- combined = np.concatenate((image, labels), axis=0)
- last_image_dim = image.shape[0]
- image_shape = image.shape
- combined_pad = np.pad(combined,((0,0),(0, max(size[0], image_shape[1]) - image_shape[1]),(0, max(size[1], image_shape[2]) - image_shape[2])),'constant')
- freesize0 = random.randint(0, max(size[0], image_shape[1]) - size[0])
- freesize1 = random.randint(0, max(size[1], image_shape[2]) - size[1])
- combined_crop = combined_pad[:, freesize0:freesize0 + size[0], freesize1:freesize1 + size[1]]
- return (combined_crop[:last_image_dim, :, :], combined_crop[last_image_dim:, :, :])
-
- def random_flip(images, labels):
- # augmentation setting....
- horizontal_flip = 1
- vertical_flip = 1
- transforms = 1
-
- if transforms and vertical_flip and random.randint(0, 1) == 1:
- images = ops.flip(images, [1])
- labels = ops.flip(labels, [1])
- if transforms and horizontal_flip and random.randint(0, 1) == 1:
- images = ops.flip(images, [2])
- labels = ops.flip(labels, [2])
-
- return images, labels
-
- def random_flip_np(images, labels):
- # augmentation setting....
- horizontal_flip = 1
- vertical_flip = 1
- transforms = 1
-
- if transforms and vertical_flip and random.randint(0, 1) == 1:
- images = np.flip(images, axis=1)
- labels = np.flip(labels, axis=1)
- if transforms and horizontal_flip and random.randint(0, 1) == 1:
- images = np.flip(images, axis=2)
- labels = np.flip(labels, axis=2)
-
- return images, labels
-
- if __name__ == '__main__':
- im1 = imageio.imread('/userhome/DVC/PyTorch/data/vimeo_septuplet/sequences/00001/0001/im4.png')
- im1 = im1 / 255.0
- im2 = imageio.imread('/userhome/DVC/PyTorch/data/vimeo_septuplet/sequences/00001/0001/im2.png')
- im2 = im2 / 255.0
- im1 = np.transpose(im1, [2, 0, 1]).astype(np.float32)
- im2 = np.transpose(im2, [2, 0, 1]).astype(np.float32)
- input_image = Tensor.from_numpy(im1)
- ref_image = Tensor.from_numpy(im2)
- im_height=6#256
- im_width=4#256
- input_image, ref_image = random_crop_and_pad_image_and_labels(input_image, ref_image, [im_height, im_width])
- im1, im2 = random_crop_and_pad_image_and_labels_np(im1, im2, [im_height, im_width])
- print("input_image:",input_image)
- print("im1:",im1)
- print("ref_image:",ref_image)
- print("im2:",im2)
- # input_image, ref_image = random_flip(input_image, ref_image)
|