コード例 #1
0
    def imaugment(self, X, Y=None):
        """
        Preprocess the tuple (image,mask) and then apply if selected:
            augmentation techniques adapted from Keras ImageDataGenerator
            elastic deformation
        """
        if self.params["augmentation"][0] == True:
            X, Y = random_transform(X, Y, **self.params["random_deform"])
            
        if self.params["augmentation"][1] == True:
            [X, Y] = elasticdeform.deform_random_grid([X, Y], 
                axis=[(0, 1), (0, 1)], 
                sigma=self.params["e_deform_g"]["sigma"],
                points=self.params["e_deform_g"]["points"])
                
        if self.params["augmentation"][2] == True:
            r = np.random.normal(self.params["noise"]["mean"],
                self.params["noise"]["std"], X.shape)
            X = X + r.reshape(X.shape)

        if self.params["augmentation"][3]:
            g = 2 * self.params["gamma"]["range"][1]
            while ((g < self.params["gamma"]["range"][0]) | 
                (g > self.params["gamma"]["range"][1])):
                    g = np.random.normal(self.params["gamma"]["mean"],
                        self.params["gamma"]["std"])
            if self.params["norm"] is None:
                temp = (X - np.min(X)) / (np.max(X) - np.min(X))
                temp = np.power(temp, 1 / g)
                X = temp * (np.max(X) - np.min(X)) + np.min(X)
            else:
                X = 2 * np.power(X / 2 + 0.5, 1 / g) - 1
        return X, Y
コード例 #2
0
ファイル: training_data.py プロジェクト: drat/WD_Faker
def async_get_training_data(images, srcPoints, dstPoints, batch_size,
                            workerqueue):

    while 1:
        indices = numpy.random.choice(range(0, images.shape[0]),
                                      size=batch_size,
                                      replace=False)
        for i, index in enumerate(indices):
            image = images[index]
            image = random_transform(image, **random_transform_args)

            closest = (numpy.mean(numpy.square(srcPoints[index] - dstPoints),
                                  axis=(1, 2))).argsort()[:10]
            closest = numpy.random.choice(closest)
            warped_img, target_img, mask_image = random_warp_src_dest(
                image, srcPoints[index], dstPoints[closest])

            if numpy.random.random() < 0.5:
                warped_img = warped_img[:, ::-1]
                target_img = target_img[:, ::-1]
                mask_image = mask_image[:, ::-1]

            if i == 0:
                warped_images = numpy.empty((batch_size, ) + warped_img.shape,
                                            warped_img.dtype)
                target_images = numpy.empty((batch_size, ) + target_img.shape,
                                            warped_img.dtype)
                mask_images = numpy.empty((batch_size, ) + mask_image.shape,
                                          mask_image.dtype)

            warped_images[i] = warped_img
            target_images[i] = target_img
            mask_images[i] = mask_image

        workerqueue.put((warped_images, target_images, mask_images))
コード例 #3
0
ファイル: GAN.py プロジェクト: yibo-liang/fs-rec
def read_image(fn, random_transform_args=random_transform_args):
    image = cv2.imread(fn)
    image = cv2.resize(image, (256, 256)) / 255 * 2 - 1
    image = random_transform(image, **random_transform_args)
    warped_img, target_img = random_warp128(image)

    return warped_img, target_img
コード例 #4
0
    def __getitem__(self, index, tp=True):
        # return 256*256 image
        cls = 'A' if index < len(self.cage) else 'B'
        img = cv2.resize(cv2.imread(self.real_dir[index]), (256, 256)) / 255.0

        img, target_img = random_warp(
            random_transform(img, **self.random_transform_args))
        img = self.model(
            Variable(torch.unsqueeze(self.toTensor(img).float(), 0)), cls)

        img = torch.squeeze(img, 0)
        target_img = self.toTensor(target_img)
        target_img = target_img.float()
        return {'LQ': img, 'GT': target_img}
コード例 #5
0
def get_training_data( images, batch_size ):
    indices = numpy.random.randint( len(images), size=batch_size )
    for i,index in enumerate(indices):
        image = images[index]
        image = random_transform( image, **random_transform_args )
        warped_img, target_img = random_warp( image )

        if i == 0:
            warped_images = numpy.empty( (batch_size,) + warped_img.shape, warped_img.dtype )
            target_images = numpy.empty( (batch_size,) + target_img.shape, warped_img.dtype )

        warped_images[i] = warped_img
        target_images[i] = target_img

    return warped_images, target_images
def get_training_data(images, batch_size):
    indices = numpy.random.randint(len(images), size=batch_size)
    for i, index in enumerate(indices):
        image = images[index]
        image = random_transform(image, **random_transform_args)
        warped_img, target_img = random_warp(image)

        if i == 0:
            warped_images = numpy.empty((batch_size,) + warped_img.shape, warped_img.dtype)
            target_images = numpy.empty((batch_size,) + target_img.shape, warped_img.dtype)

        warped_images[i] = warped_img
        target_images[i] = target_img

    return warped_images, target_images
コード例 #7
0
def async_get_training_data( images, srcPoints, dstPoints, batch_size,workerqueue ):
    """

    :param images: Input of the image group to be converted
    :param srcPoints:landmarkA image
    :param dstPoints:lanmarkB image
    :param batch_size:
    :param workerqueue:
    :return:
    """


    while 1:
      indices = numpy.random.choice(range(0,images.shape[0]),size=batch_size,replace=False)
      for i,index in enumerate(indices):
          image = images[index]
          image = random_transform( image, **random_transform_args )
          #cv2.imshow(image)
          # To find the Most similar image between landmarkA and landmarkB
          closest = ( numpy.mean(numpy.square(srcPoints[index]-dstPoints),axis=(1,2)) ).argsort()[:10]
          closest = numpy.random.choice(closest)
          warped_img, target_img, mask_image = random_warp_src_dest( image,srcPoints[index],dstPoints[ closest ] )
          #print(warped_img.shape, target_img.shape, mask_image.shape)
          """
          cv2.imshow('warped', warped_img)
          cv2.imshow('target', target_img)
          cv2.imshow('mask', mask_image)
          cv2.waitKey()
          cv2.destroyAllWindows()
          """
 
          if numpy.random.random() < 0.5:
            warped_img = warped_img[:,::-1]
            target_img = target_img[:,::-1]
            mask_image = mask_image[:,::-1]


          if i == 0:
              warped_images = numpy.empty( (batch_size,) + warped_img.shape, warped_img.dtype )
              target_images = numpy.empty( (batch_size,) + target_img.shape, warped_img.dtype )
              mask_images = numpy.empty( (batch_size,)   + mask_image.shape, mask_image.dtype )

          warped_images[i] = warped_img
          target_images[i] = target_img
          mask_images[i]   = mask_image


      workerqueue.put( (warped_images, target_images , mask_images ) )
コード例 #8
0
def get_training_data(images, batch_size):
    indices = numpy.random.randint(len(images),
                                   size=batch_size)  #返回一个随机整型数,范围从低(包括)到高(不包括
    for i, index in enumerate(indices):
        image = images[index]
        image = random_transform(image, **random_transform_args)  #进行图片转换,图片增强
        warped_img, target_img = random_warp(image)  #每一张图片都转换

        if i == 0:  #两个新的空数组
            warped_images = numpy.empty((batch_size, ) + warped_img.shape,
                                        warped_img.dtype)
            target_images = numpy.empty((batch_size, ) + target_img.shape,
                                        warped_img.dtype)

        warped_images[i] = warped_img
        target_images[i] = target_img

    return warped_images, target_images
コード例 #9
0
 def imaugment(self, X, Y):
     """
     Preprocess the tuple (image,mask) and then apply if selected:
         augmentation techniques adapted from Keras ImageDataGenerator
         elastic deformation
     """
     if Y is not None and X.shape != Y.shape:
         raise ValueError("image and mask should have the same size")
     #print("preprocessed", X)
     if self.params["augmentation"][0] == True:
         #print("augmented")
         X, Y = random_transform(X, Y, **self.params["random_deform"])
         #print("augmented",X)
     if self.params["augmentation"][1] == True:
         X, Y = deform_pixel(X,Y, **self.params["e_deform_p"])
     if self.params["augmentation"][2] == True:
         X, Y = deform_grid(X,Y, **self.params["e_deform_g"])
         #print("deformed!")
     return X,Y
    def imaugment(self, X, Y):
        """
        Preprocess the tuple (image,mask) and then apply if selected:
            augmentation techniques adapted from Keras ImageDataGenerator
            elastic deformation
        """

        if Y is not None and len(X.shape) == len(
                Y.shape) and X.shape[:3] != Y.shape[:3]:
            raise ValueError("image and mask should have the same size")
        if self.params["intensity_shift"] == True:
            X, Y = shift_intensities(X, Y,
                                     self.params["intensity_shift_params"])
        if self.params["augmentation"][0] == True:
            X, Y = random_transform(X, Y, **self.params["random_deform"])
        if self.params["augmentation"][1] == True:
            X, Y = deform_pixel(X, Y, **self.params["e_deform_p"])
        if self.params["augmentation"][2] == True:
            X, Y = deform_grid(X, Y, **self.params["e_deform_g"])
        return X, Y