tf.data.Dataset圖像數(shù)據(jù)增廣(更新中)

效果預(yù)覽:


QQ圖片20190820173615.png

隨機(jī)圖片遮擋

    def random_block(x: tf.Tensor, y: tf.Tensor) -> (tf.Tensor, tf.Tensor):
        # 遮擋塊和圖片的比例
        block_ratio = 0.1
        # 遮擋塊顏色
        block_color = np.random.randint(64, 192)
        # channel last
        width, height = np.shape(x)[:2]
        block_width, block_height = int(width * block_ratio), int(height * block_ratio)
        block = np.full(shape=(block_width, block_height, 3), fill_value=block_color)
        start_width_idx = np.random.randint(0, width - block_width)
        start_height_idx = np.random.randint(0, height - block_height)
        x[start_width_idx: start_width_idx + block_width, start_height_idx:start_height_idx + block_height,
        :] = block
        return x, y
    # 這樣使用該方法
    dataset = dataset.map(lambda x, y: tf.cond(tf.random_uniform([], 0, 1) > 0.4,
                                               lambda: tuple(tf.py_func(random_block, [x, y], [tf.uint8, y.dtype])),
                                               lambda: (x, y)))

隨機(jī)hsv

    def color(x: tf.Tensor, label: tf.Tensor) -> (tf.Tensor, tf.Tensor):
        x = tf.image.random_hue(x, 0.05)
        x = tf.image.random_saturation(x, 0.6, 1.6)
        x = tf.image.random_brightness(x, 0.2)
        x = tf.image.random_contrast(x, 0.7, 1.3)
        return x, label

高斯模糊(最蛋疼的方法)

    def blur(x: tf.Tensor, label: tf.Tensor) -> (tf.Tensor, tf.Tensor):

        def gaussian_kernel(size, sigma):
            x_points = np.arange(-(size - 1) // 2, (size - 1) // 2 + 1, 1)
            y_points = x_points[::-1]
            xs, ys = np.meshgrid(x_points, y_points)
            kernel = np.exp(-(xs ** 2 + ys ** 2) / (2 * sigma ** 2)) / (2 * np.pi * sigma ** 2)
            return kernel / kernel.sum()

        kernel = gaussian_kernel(size=5, sigma=2.5)
        kernel = kernel[:, :, np.newaxis, np.newaxis]
        x = tf.cast(x, tf.float32)
        xr, xg, xb = tf.expand_dims(tf.expand_dims(x[:, :, 0], -1), 0), tf.expand_dims(
            tf.expand_dims(x[:, :, 1], -1), 0), tf.expand_dims(tf.expand_dims(x[:, :, 2],
                                                                              -1), 0)

        xr_blur = tf.nn.conv2d(xr, kernel, strides=[1, 1, 1, 1], padding='SAME')
        xg_blur = tf.nn.conv2d(xg, kernel, strides=[1, 1, 1, 1], padding='SAME')
        xb_blur = tf.nn.conv2d(xb, kernel, strides=[1, 1, 1, 1], padding='SAME')
        xrgb_blur = tf.concat([xr_blur, xg_blur, xb_blur], axis=3)
        xrgb_blur = tf.clip_by_value(xrgb_blur, 0, 255)
        xrgb_blur = tf.cast(tf.squeeze(xrgb_blur), tf.uint8)

        return xrgb_blur, label

高斯模糊(使用tf.py_func調(diào)用普通python方法)

    def blur(x: tf.Tensor, label: tf.Tensor) -> (tf.Tensor, tf.Tensor):
        sigmaX = (1.4 - 1.1) + np.random.random() * 1.1
        sigmaY = (1.4 - 1.1) + np.random.random() * 1.1
        blurred = cv2.GaussianBlur(x, ksize=(0, 0), sigmaX=sigmaX, sigmaY=sigmaY, borderType=cv2.BORDER_DEFAULT)
        return blurred, label
    # 然后在map時(shí),這樣隨機(jī)高斯模糊處理:
    dataset = dataset.map(lambda x, y: tf.cond(tf.random_uniform([], 0, 1) > 0.7,
                                               lambda: tuple(tf.py_func(blur, [x, y], [tf.uint8, y.dtype])),
                                               lambda: (x, y)))

高斯噪音

    def noise(image: tf.Tensor, label: tf.Tensor) -> (tf.Tensor, tf.Tensor):
        noise = tf.random_normal(dtype=tf.float32, shape=(img_size, img_size, 3), stddev=8.0, mean=0.0)
        noise_img = tf.cast(image, tf.float32) + noise
        noise_img = tf.clip_by_value(noise_img, 0, 255)
        return tf.cast(noise_img, tf.uint8), label

圖片旋轉(zhuǎn)(使用tf.py_func調(diào)用普通函數(shù))

    def random_rotate(x: tf.Tensor, y: tf.Tensor) -> (tf.Tensor, tf.Tensor):
        from scipy import misc
        angle = np.random.uniform(low=-10.0, high=10.0)
        return misc.imrotate(x, angle, 'bicubic'), y
    # map時(shí),這樣處理
    dataset = dataset.map(lambda x, y: tf.cond(tf.random_uniform([], 0, 1) > 0.7,
                                               lambda: tuple(tf.py_func(random_rotate, [x, y], [tf.uint8, y.dtype])),
                                               lambda: (x, y)))

按概率隨機(jī)增廣圖片

    augmentations = [noise, color]
    for f in augmentations:
        dataset = dataset.map(
            lambda x, y: tf.cond(tf.random_uniform([], 0, 1) > 0.7, lambda: f(x, y), lambda: (x, y)),
            num_parallel_calls=4)
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容