Пример #1
0
def augmentation(img1, img2, mask):
    """
    :param img1: T2flair
    :param img2: T1
    :param mask: mask
    theta:是用户指定旋转角度范围,其参数只需指定一个整数即可
    tx,ty:分别是水平位置平移和上下位置平移,其参数可以是[0, 1]的浮点数,也可以大于1,其最大平移距离为图片长或宽的尺寸乘以参数
    shear: 是错切变换,效果就是让所有点的x坐标(或者y坐标)保持不变,而对应的y坐标(或者x坐标)则按比例发生平移,且平移的大小和该点到x轴(或y轴)的垂直距离成正比。
    zx: Zoom in x direction.
    zy: Zoom in y direction
    row_axis: Index of axis for rows in the input image.
    col_axis: Index of axis for columns in the input image.
    channel_axis: Index of axis for channels in the input image.
    cval: Value used for points outside the boundaries
            of the input if `mode='constant'`.
    fill_mode:为填充模式,如前面提到,当对图片进行平移、放缩、错切等操作时,图片中会出现一些缺失的地方,那这些缺失的地方该用什么方式补全呢?就由fill_mode中的参数确定,包括:“constant”、“nearest”(默认)、“reflect”和“wrap”。
    :return:
    """
    # img1
    img1 = apply_affine_transform(img1[..., np.newaxis], theta=5, tx=0, ty=0, shear=0.05, zx=1, zy=1,
                           row_axis=0, col_axis=1, channel_axis=2,fill_mode='nearest', cval=0., order=1)
    img1 = img1[:,0,:]
    # img2
    img2 = apply_affine_transform(img2[..., np.newaxis], theta=5, tx=0, ty=0, shear=0.05, zx=1, zy=1,
                                  row_axis=0, col_axis=1, channel_axis=2, fill_mode='nearest', cval=0., order=1)
    img2 = img2[:,0,:]
    # mask
    mask = apply_affine_transform(mask[..., np.newaxis], theta=5, tx=0, ty=0, shear=0.05, zx=1, zy=1,
                                  row_axis=0, col_axis=1, channel_axis=2, fill_mode='nearest', cval=0., order=1)
    mask = mask[:,0,:]
    return img1, img2, mask
Пример #2
0
def rot_scale_crop(img, rot_deg, zoom_img, zoom_rot, mode, cval, out_img,
                   out_rot):
    # TODO:
    # this doesn't work yet; however, one solution would be to copy apply_affine_transform and modify it to accept and
    # forward the output and output_shape parameters to scipy.ndimage.interpolaton.affine_transform. This would also
    # allow for proper cropping, as well as better performance, and avoiding unnecessary casts on image type without
    # losing any precision (avoid truncations). However, we must compute the shifts for cropping properly. Also, double
    # check the code in NI_GeometricTransform in
    # https://github.com/scipy/scipy/blob/master/scipy/ndimage/src/ni_interpolation.c
    # again, to make sure that outputs are stored as doubles before being cast to output dtype. As far as I can tell
    # after a quick look, this happens, so operations are NOT done in uint8 if input dtype is uint8.
    zoomed = apply_affine_transform(img,
                                    theta=0,
                                    zx=zoom_img,
                                    zy=zoom_img,
                                    row_axis=1,
                                    col_axis=2,
                                    channel_axis=0,
                                    fill_mode=mode,
                                    cval=cval)
    out_img[...] = crop_center_square(zoomed, out_img.shape[1:])

    rot = apply_affine_transform(img,
                                 theta=rot_deg,
                                 zx=zoom_rot,
                                 zy=zoom_rot,
                                 row_axis=1,
                                 col_axis=2,
                                 channel_axis=0,
                                 fill_mode=mode,
                                 cval=cval)

    out_rot[...] = crop_center_square(rot, out_rot.shape[1])

    show(out_img, out_rot)
def preprocess(img, imgSize, dataAugmentation=False):
    "put img into target img of size imgSize, transpose for TF and normalize gray-values"

    # there are damaged files in IAM dataset - just use black image instead
    if img is None:
        img = np.zeros([imgSize[1], imgSize[0]])

    # increase dataset size by applying random stretches to the images
    if dataAugmentation:
        img = img.reshape(img.shape[0], img.shape[1], 1)
        brightness = np.random.uniform(0.5, 1.5)
        contrast = np.random.uniform(0.5, 1.5)
        rotate = np.random.uniform(-4, 4)
        shear = np.random.uniform(-2, 2)
        sharp = np.random.uniform(0.2, 2)
        image_affine = apply_affine_transform(img, theta=rotate, shear=shear)
        image_bright = apply_brightness_shift(image_affine,
                                              brightness=brightness)
        image_contrast = apply_contrast_shift(image_bright, contrast=contrast)
        image_sharp = apply_sharpness_shift(image_contrast, sharpness=sharp)
        stretch = (random.random() - 0.5)  # -0.5 .. +0.5
        wStretched = max(int(img.shape[1] * (1 + stretch)),
                         1)  # random width, but at least 1
        img = cv2.resize(image_sharp, (wStretched, image_sharp.shape[0]))

    # create target image and copy sample image into it
    (wt, ht) = imgSize
    (h, w) = img.shape
    target = np.ones([ht, wt]) * 255
    if h < ht and w < wt:
        top_padding = int((ht - h) / 2)
        left_padding = int((wt - w) / 2)
        target[top_padding:top_padding + h,
               left_padding:left_padding + w] = img
    else:
        fx = w / wt
        fy = h / ht
        f = max(fx, fy)
        newSize = (
            max(min(wt, int(w / f)), 1), max(min(ht, int(h / f)), 1)
        )  # scale according to f (result at least 1 and at most wt or ht)
        img = cv2.resize(img, newSize)
        top_padding = int((ht - newSize[1]) / 2)
        left_padding = int((wt - newSize[0]) / 2)
        target[top_padding:top_padding + newSize[1],
               left_padding:left_padding + newSize[0]] = img
    # cv2.imwrite("16_8_before_transpose.jpg", target)

    # transpose for TF
    img = cv2.transpose(target)
    # cv2.imwrite("16_8_after_transpose.jpg", img)

    # normalize
    (m, s) = cv2.meanStdDev(img)
    m = m[0][0]
    s = s[0][0]
    img = img - m
    img = img / s if s > 0 else img
    # cv2.imwrite("16_8_norm.jpg", img)
    return img
Пример #4
0
def tran_one(img, d=15):
    degree = np.random.randint(-d, d)
    img = iprocess.random_brightness(img, (0.5, 1.5))
    if np.random.rand() >= 0.5:
       img = np.fliplr(img)
 
    return iprocess.apply_affine_transform(img, degree)
Пример #5
0
def generator():
	while True:
		modality = np.random.randint(4,size=BATCH)
		usuario = np.random.randint(50,size=BATCH)

		X = np.zeros((BATCH,NUM_FRAMES,112,112,3))
		Y = []
		Y2=[]
		for i in range(BATCH):
			users = os.listdir(directory+dirl[modality[i]])
				
			images = wsort(directory+dirl[modality[i]]+users[usuario[i]]+'/') #all the images
			numImages = len(images)

			imagens = np.random.randint(numImages)
			indice = 0

			valor = np.random.random()
			if valor < 0.25:
				flagFlip = 1
			elif valor < 0.50 and valor >= 0.25:
				flagFlip = 2
			elif valor < 0.75 and valor >= 0.50:
				flagFlip = 3
			else:
				flagFlip = 4

				
			for j in range(imagens,imagens+128,8):
				imagem=image.load_img(directory+dirl[modality[i]]+users[usuario[i]]+'/'+images[(j)%numImages],target_size=(112,112))
				imagem = image.img_to_array(imagem)
				# here you put your function to subtract the mean of vggface2 dataset
				imga = utils.preprocess_input(imagem,version=2) #subtract the mean of vggface dataset
				
				if flagFlip == 1:
					X[i,indice,:,:,:] = np.flip(imga,axis=1)
				elif flagFlip == 2:
					X[i,indice,:,:,:] = image.apply_affine_transform(imga,theta=30, channel_axis=2, fill_mode='nearest',cval=0.,order=1)
				elif flagFlip == 3:
					X[i,indice,:,:,:] = np.flip(imga,axis=0)
				else:
					X[i,indice,:,:,:]=imga

				indice = indice+1
			sets = dirl[modality[i]].split('/')[1]
			# You can train the model using Training and Development sets
			if sets == 'Training':
				label = readCSV(dirLabels[0]+'/'+users[usuario[i]]+'_Depression.csv')
			else:
				label = readCSV(dirLabels[1]+'/'+users[usuario[i]]+'_Depression.csv')
			Y.append(label)
			
		
		Y=np.array(Y)
		
		yield X,Y
Пример #6
0
    def __call__(self, x):
        res_x = x
        if self.flip:
            res_x = np.fliplr(res_x)
        if self.tx != 0 or self.ty != 0:
            res_x = apply_affine_transform(res_x, tx=self.tx, ty=self.ty, channel_axis=2, fill_mode='reflect')
        if self.k_90_rotate != 0:
            res_x = np.rot90(res_x, self.k_90_rotate)

        return res_x
    def _apply_transform(self, x, transform_parameters):
        """Applies a transformation to an image according to given parameters.
        # Arguments
            x: 3D tensor, single image.
            transform_parameters: Dictionary with string - parameter pairs
                describing the transformation.
                Currently, the following parameters
                from the dictionary are used:
                - `'theta'`: Float. Rotation angle in degrees.
                - `'tx'`: Float. Shift in the x direction.
                - `'ty'`: Float. Shift in the y direction.
                - `'shear'`: Float. Shear angle in degrees.
                - `'zx'`: Float. Zoom in the x direction.
                - `'zy'`: Float. Zoom in the y direction.
                - `'flip_horizontal'`: Boolean. Horizontal flip.
                - `'flip_vertical'`: Boolean. Vertical flip.
                - `'channel_shift_intensity'`: Float. Channel shift intensity.
                - `'brightness'`: Float. Brightness shift intensity.
        # Returns
            A transformed version of the input (same shape).
        """
        # x is a single image, so it doesn't have image number at index 0
        img_row_axis = self.row_axis - 1
        img_col_axis = self.col_axis - 1
        img_channel_axis = self.channel_axis - 1

        x = apply_affine_transform(x,
                                   transform_parameters.get('theta', 0),
                                   transform_parameters.get('tx', 0),
                                   transform_parameters.get('ty', 0),
                                   transform_parameters.get('shear', 0),
                                   transform_parameters.get('zx', 1),
                                   transform_parameters.get('zy', 1),
                                   row_axis=img_row_axis,
                                   col_axis=img_col_axis,
                                   channel_axis=img_channel_axis,
                                   fill_mode=self.fill_mode,
                                   cval=self.cval)

        if transform_parameters.get('channel_shift_intensity') is not None:
            x = apply_channel_shift(
                x, transform_parameters['channel_shift_intensity'],
                img_channel_axis)

        if transform_parameters.get('flip_horizontal', False):
            x = self._flip_axis(x, img_col_axis)

        if transform_parameters.get('flip_vertical', False):
            x = self._flip_axis(x, img_row_axis)

        if transform_parameters.get('brightness') is not None:
            x = apply_brightness_shift(x, transform_parameters['brightness'])

        return x
Пример #8
0
 def test_img_transforms(self):
     x = np.random.random((3, 200, 200))
     _ = preprocessing_image.random_rotation(x, 20)
     _ = preprocessing_image.random_shift(x, 0.2, 0.2)
     _ = preprocessing_image.random_shear(x, 2.)
     _ = preprocessing_image.random_zoom(x, (0.5, 0.5))
     _ = preprocessing_image.apply_channel_shift(x, 2, 2)
     _ = preprocessing_image.apply_affine_transform(x, 2)
     with self.assertRaises(ValueError):
         preprocessing_image.random_zoom(x, (0, 0, 0))
     _ = preprocessing_image.random_channel_shift(x, 2.)
Пример #9
0
def augment(x_0, x_1, y):
    theta = (np.random.uniform(-15, 15) * np.pi) / 180.
    rotation_matrix = np.array([[np.cos(theta), -np.sin(theta), 0],
                                [np.sin(theta),
                                 np.cos(theta), 0], [0, 0, 1]])

    shear = np.random.uniform(-.1, .1)
    shear_matrix = np.array([[1, -np.sin(shear), 0], [0, np.cos(shear), 0],
                             [0, 0, 1]])

    zx, zy = np.random.uniform(.9, 1.1, 2)
    zoom_matrix = np.array([[zx, 0, 0], [0, zy, 0], [0, 0, 1]])

    augmentation_matrix = np.dot(np.dot(rotation_matrix, shear_matrix),
                                 zoom_matrix)
    x_0 = apply_affine_transform(x_0[..., np.newaxis],
                                 theta=theta,
                                 tx=0,
                                 ty=0,
                                 shear=shear,
                                 zx=zx,
                                 zy=zy,
                                 channel_axis=2)
    x_1 = apply_affine_transform(x_1[..., np.newaxis],
                                 theta=theta,
                                 tx=0,
                                 ty=0,
                                 shear=shear,
                                 zx=zx,
                                 zy=zy,
                                 channel_axis=2)
    y = apply_affine_transform(y[..., np.newaxis],
                               theta=theta,
                               tx=0,
                               ty=0,
                               shear=shear,
                               zx=zx,
                               zy=zy,
                               channel_axis=2)

    return x_0[..., 0], x_1[..., 0], y[..., 0]
Пример #10
0
 def __call__(self, x):
     res_x = x
     if self.flip:
         res_x = np.fliplr(res_x)
     zx = np.random.randint(4, 9) / 10
     zy = np.random.randint(4, 9) / 10
     res_x = apply_affine_transform(res_x,
                                    theta=self.theta,
                                    zx=zx,
                                    zy=zy,
                                    channel_axis=2,
                                    fill_mode='reflect')
     return res_x
Пример #11
0
def random_zoom(x,
                zoom_range,
                row_axis=1,
                col_axis=2,
                channel_axis=0,
                fill_mode='nearest',
                cval=0.,
                z_known=None):
    """Performs a random spatial zoom of a Numpy image tensor.

    # Arguments
        x: Input tensor. Must be 3D.
        zoom_range: Tuple of floats; zoom range for width and height.
        row_axis: Index of axis for rows in the input tensor.
        col_axis: Index of axis for columns in the input tensor.
        channel_axis: Index of axis for channels in the input tensor.
        fill_mode: Points outside the boundaries of the input
            are filled according to the given mode
            (one of `{'constant', 'nearest', 'reflect', 'wrap'}`).
        cval: Value used for points outside the boundaries
            of the input if `mode='constant'`.
        z_known: Value to disable randomness or None.

    # Returns
        Zoomed Numpy image tensor.

    # Raises
        ValueError: if `zoom_range` isn't a tuple.
    """
    if z_known is None:
        if len(zoom_range) != 2:
            raise ValueError(
                '`zoom_range` should be a tuple or list of two floats. '
                'Received arg: ', zoom_range)
        if zoom_range[0] == 1 and zoom_range[1] == 1:
            zx, zy = 1, 1
        else:
            zx, zy = np.random.uniform(zoom_range[0], zoom_range[1], 2)
    else:
        zx, zy = z_known
    x = apply_affine_transform(x,
                               zx=zx,
                               zy=zy,
                               channel_axis=channel_axis,
                               fill_mode=fill_mode,
                               cval=cval,
                               row_axis=row_axis,
                               col_axis=col_axis)
    return x
Пример #12
0
def random_shift(x,
                 wrg,
                 hrg,
                 row_axis=1,
                 col_axis=2,
                 channel_axis=0,
                 fill_mode='nearest',
                 cval=0.,
                 tx=None,
                 ty=None):
    """Performs a random spatial shift of a Numpy image tensor.

    # Arguments
        x: Input tensor. Must be 3D.
        wrg: Width shift range, as a float fraction of the width.
        hrg: Height shift range, as a float fraction of the height.
        row_axis: Index of axis for rows in the input tensor.
        col_axis: Index of axis for columns in the input tensor.
        channel_axis: Index of axis for channels in the input tensor.
        fill_mode: Points outside the boundaries of the input
            are filled according to the given mode
            (one of `{'constant', 'nearest', 'reflect', 'wrap'}`).
        cval: Value used for points outside the boundaries
            of the input if `mode='constant'`.
        tx : Value to disable randomness in X or None.
        ty : Value to disable randomness in Y or None.

    # Returns
        Shifted Numpy image tensor.
    """
    h, w = x.shape[row_axis], x.shape[col_axis]
    tx = np.random.uniform(-hrg, hrg) * h if tx is None else tx
    ty = np.random.uniform(-wrg, wrg) * w if ty is None else ty

    tx *= h
    ty *= w
    x = apply_affine_transform(x,
                               tx=tx,
                               ty=ty,
                               channel_axis=channel_axis,
                               fill_mode=fill_mode,
                               cval=cval)
    return x
Пример #13
0
def transform_pic(pic):
    theta = np.random.uniform(-20, 20)
    ty = np.random.uniform(-0.2, 0.2)
    tx = np.random.uniform(-0.2, 0.2)
    shear = np.random.uniform(-0.2, 0.2)
    zy = np.random.uniform(0.8, 1)
    zx = np.random.uniform(0.8, 1)
    pic = apply_affine_transform(pic,
                                 theta=theta,
                                 tx=tx,
                                 ty=ty,
                                 shear=shear,
                                 zx=zx,
                                 zy=zy,
                                 channel_axis=2)
    if random.randint(0, 1):
        pic = flip_axis(pic, 0)
    if random.randint(0, 1):
        pic = flip_axis(pic, 1)
    return pic
Пример #14
0
 def test_deterministic_transform(self):
     x = np.ones((32, 32, 3))
     generator = image.ImageDataGenerator(rotation_range=90,
                                          fill_mode='constant')
     x = np.random.random((32, 32, 3))
     assert np.allclose(
         generator.apply_transform(x, {'flip_vertical': True}),
         x[::-1, :, :])
     assert np.allclose(
         generator.apply_transform(x, {'flip_horizontal': True}),
         x[:, ::-1, :])
     x = np.ones((3, 3, 3))
     x_rotated = np.array([[[0., 0., 0.], [0., 0., 0.], [1., 1., 1.]],
                           [[0., 0., 0.], [1., 1., 1.], [1., 1., 1.]],
                           [[0., 0., 0.], [0., 0., 0.], [1., 1., 1.]]])
     assert np.allclose(generator.apply_transform(x, {'theta': 45}),
                        x_rotated)
     assert np.allclose(
         image.apply_affine_transform(x,
                                      theta=45,
                                      channel_axis=2,
                                      fill_mode='constant'), x_rotated)
Пример #15
0
 def test_deterministic_transform(self):
     x = np.ones((32, 32, 3))
     generator = image.ImageDataGenerator(
         rotation_range=90,
         fill_mode='constant')
     x = np.random.random((32, 32, 3))
     assert np.allclose(generator.apply_transform(x, {'flip_vertical': True}),
                        x[::-1, :, :])
     assert np.allclose(generator.apply_transform(x, {'flip_horizontal': True}),
                        x[:, ::-1, :])
     x = np.ones((3, 3, 3))
     x_rotated = np.array([[[0., 0., 0.],
                            [0., 0., 0.],
                            [1., 1., 1.]],
                           [[0., 0., 0.],
                            [1., 1., 1.],
                            [1., 1., 1.]],
                           [[0., 0., 0.],
                            [0., 0., 0.],
                            [1., 1., 1.]]])
     assert np.allclose(generator.apply_transform(x, {'theta': 45}),
                        x_rotated)
     assert np.allclose(image.apply_affine_transform(
         x, theta=45, channel_axis=2, fill_mode='constant'), x_rotated)
Пример #16
0
    def scipy_image_augmentation(self, img, theta=15, tx=0., ty=0., zoom=0.15):

        if zoom != 1:
            #zx, zy = np.random.uniform(1 - zoom, 1 + zoom, 2)
            zx = zy = np.random.uniform(1 - zoom, 1 + zoom)
        else:
            zx, zy = 1, 1

        if theta != 0:
            theta = np.random.uniform(-theta, theta)

        #m_inv = cv2.getRotationMatrix2D((img.shape[1]//2, img.shape[0]//2), theta, scale)
        ''' ADD translation to Rotation Matrix '''
        if tx != 0. or ty != 0.:
            h, w = img.shape[0], img.shape[1]
            ty = np.random.uniform(-ty, ty) * h
            tx = np.random.uniform(-tx, tx) * w

        return apply_affine_transform(img,
                                      theta=theta,
                                      tx=tx,
                                      ty=ty,
                                      zx=zx,
                                      zy=zy)
Пример #17
0
def random_shear(x,
                 intensity,
                 row_axis=1,
                 col_axis=2,
                 channel_axis=0,
                 fill_mode='nearest',
                 cval=0.,
                 known_intensity=None):
    """Performs a random spatial shear of a Numpy image tensor.

    # Arguments
        x: Input tensor. Must be 3D.
        intensity: Transformation intensity.
        row_axis: Index of axis for rows in the input tensor.
        col_axis: Index of axis for columns in the input tensor.
        channel_axis: Index of axis for channels in the input tensor.
        fill_mode: Points outside the boundaries of the input
            are filled according to the given mode
            (one of `{'constant', 'nearest', 'reflect', 'wrap'}`).
        cval: Value used for points outside the boundaries
            of the input if `mode='constant'`.
        known_intensity: Value to disable randomness or None.

    # Returns
        Sheared Numpy image tensor.
    """
    shear = np.random.uniform(
        -intensity, intensity) if known_intensity is None else known_intensity
    x = apply_affine_transform(x,
                               shear=shear,
                               channel_axis=channel_axis,
                               fill_mode=fill_mode,
                               cval=cval,
                               row_axis=row_axis,
                               col_axis=col_axis)
    return x
Пример #18
0
def random_rotation(x,
                    rg,
                    row_axis=1,
                    col_axis=2,
                    channel_axis=0,
                    fill_mode='nearest',
                    cval=0.,
                    theta=None):
    """Performs a random rotation of a Numpy image tensor.

    # Arguments
        x: Input tensor. Must be 3D.
        rg: Rotation range, in degrees.
        row_axis: Index of axis for rows in the input tensor.
        col_axis: Index of axis for columns in the input tensor.
        channel_axis: Index of axis for channels in the input tensor.
        fill_mode: Points outside the boundaries of the input
            are filled according to the given mode
            (one of `{'constant', 'nearest', 'reflect', 'wrap'}`).
        cval: Value used for points outside the boundaries
            of the input if `mode='constant'`.
        theta: Value to disable randomness or None.

    # Returns
        Rotated Numpy image tensor.
    """
    theta = np.pi / 180 * np.random.uniform(-rg,
                                            rg) if theta is None else theta
    x = apply_affine_transform(x,
                               theta=theta,
                               channel_axis=channel_axis,
                               fill_mode=fill_mode,
                               cval=cval,
                               row_axis=row_axis,
                               col_axis=col_axis)
    return x
Пример #19
0
    def random_transform(self, x, mask):
        """Randomly augment a image tensor and mask.

        # Arguments
            x: 3D tensor, single image.

        # Returns
            A randomly transformed version of the input (same shape).
        """
        # x is a single image, so it doesn't have image number at index 0
        img_row_axis = 0
        img_col_axis = 1
        img_channel_axis = 2

        # use composition of homographies
        # to generate final transform that needs to be applied
        if self.rotation_range and np.random.random() < 0.3:
            theta = np.pi / 180 * np.random.uniform(-self.rotation_range, self.rotation_range)
        else:
            theta = 0

        if self.height_shift_range:
            uniform = np.random.uniform(-self.height_shift_range, self.height_shift_range)
            tx = uniform * x.shape[img_row_axis]
            tmx = uniform * mask.shape[img_row_axis]
        else:
            tx = 0
            tmx = 0

        if self.width_shift_range:
            random_uniform = np.random.uniform(-self.width_shift_range, self.width_shift_range)
            ty = random_uniform * x.shape[img_col_axis]
            tmy = random_uniform * mask.shape[img_col_axis]
        else:
            ty = 0
            tmy = 0

        if self.shear_range:
            shear = np.random.uniform(-self.shear_range, self.shear_range)
        else:
            shear = 0

        if self.zoom_range[0] == 1 and self.zoom_range[1] == 1:
            zx, zy = 1, 1
        else:
            zx, zy = np.random.uniform(self.zoom_range[0], self.zoom_range[1], 2)

        transform_matrix = None
        transform_matrix_mask = None
        if theta != 0:
            rotation_matrix = np.array([[np.cos(theta), -np.sin(theta), 0],
                                        [np.sin(theta), np.cos(theta), 0],
                                        [0, 0, 1]])
            transform_matrix = rotation_matrix
            transform_matrix_mask = rotation_matrix

        if tx != 0 or ty != 0:
            shift_matrix = np.array([[1, 0, tx],
                                     [0, 1, ty],
                                     [0, 0, 1]])
            shift_matrix_mask = np.array([[1, 0, tmx],
                                          [0, 1, tmy],
                                          [0, 0, 1]])

            transform_matrix = shift_matrix if transform_matrix is None else np.dot(transform_matrix, shift_matrix)
            transform_matrix_mask = shift_matrix_mask if transform_matrix_mask is None else np.dot(transform_matrix_mask, shift_matrix_mask)

        if shear != 0:
            shear_matrix = np.array([[1, -np.sin(shear), 0],
                                     [0, np.cos(shear), 0],
                                     [0, 0, 1]])
            transform_matrix = shear_matrix if transform_matrix is None else np.dot(transform_matrix, shear_matrix)
            transform_matrix_mask = shear_matrix if transform_matrix_mask is None else np.dot(transform_matrix_mask, shear_matrix)

        if zx != 1 or zy != 1:
            zoom_matrix = np.array([[zx, 0, 0],
                                    [0, zy, 0],
                                    [0, 0, 1]])
            transform_matrix = zoom_matrix if transform_matrix is None else np.dot(transform_matrix, zoom_matrix)
            transform_matrix_mask = zoom_matrix if transform_matrix_mask is None else np.dot(transform_matrix_mask, zoom_matrix)

        if transform_matrix is not None:
            h, w = x.shape[img_row_axis], x.shape[img_col_axis]
            transform_matrix = transform_matrix_offset_center(transform_matrix, h, w)
            x = apply_affine_transform(x, transform_matrix, img_channel_axis, fill_mode=self.fill_mode, cval=self.cval)

        if transform_matrix_mask is not None:
            h, w = mask.shape[img_row_axis], mask.shape[img_col_axis]
            transform_matrix_mask = transform_matrix_offset_center(transform_matrix_mask, h, w)
            mask[:, :, :] = apply_affine_transform(mask[:, :, :], transform_matrix_mask, img_channel_axis, fill_mode='constant', cval=0.)
        if self.channel_shift_range != 0:
            x = random_channel_shift(x, self.channel_shift_range, img_channel_axis)
        # if self.horizontal_flip:
        #     if np.random.random() < 0.5:
        #         x = flip_axis(x, img_col_axis)
        #         mask = flip_axis(mask, img_col_axis)
        #
        # if self.vertical_flip:
        #     if np.random.random() < 0.5:
        #         x = flip_axis(x, img_row_axis)
        #         mask = flip_axis(mask, img_row_axis)

        return x, mask