Example #1
0
def get_next_batch_from_path(image_path,
                             image_labels,
                             pointer,
                             IMAGE_HEIGHT=299,
                             IMAGE_WIDTH=299,
                             batch_size=64,
                             is_train=True):
    batch_x = np.zeros([batch_size, IMAGE_HEIGHT, IMAGE_WIDTH, 3])
    num_classes = len(image_labels[0])
    batch_y = np.zeros([batch_size, num_classes])
    for i in range(batch_size):
        image = cv2.imread(image_path[i + pointer * batch_size])
        image = cv2.resize(image,
                           (int(IMAGE_HEIGHT * 1.5), int(IMAGE_WIDTH * 1.5)))
        image = img_crop_pre(image, resize_size=336)
        if is_train:
            image = random_flip(image)
            image = random_rotation(image)
            image = random_crop(image)
            image = random_exposure(image)
        image = cv2.resize(image, (IMAGE_HEIGHT, IMAGE_WIDTH))
        # 选择自己预处理方式:
        '''
        m = image.mean()
        s = image.std()
        min_s = 1.0/(np.sqrt(image.shape[0]*image.shape[1]))
        std = max(min_s, s)
        image = (image-m)/std'''
        # image = (image-127.5)
        image = image / 255.0
        image = image - 0.5
        image = image * 2

        batch_x[i, :, :, :] = image
        # print labels[i+pointer*batch_size]
        batch_y[i] = image_labels[i + pointer * batch_size]
    return batch_x, batch_y
Example #2
0
    def process_image(self, line, data_dir, mode):
        """ process_image """
        img1, img2, grt1, grt2, img1_name, img2_name, grt1_name, grt2_name = self.load_image(
            line, data_dir, mode=mode)
        grt1 = grt1 + 1 if grt1 is not None else None
        if mode == ModelPhase.TRAIN:
            img1, img2, grt1, grt2 = aug.resize(img1, img2, grt1, grt2, mode)
            img1, img2, grt1, grt2 = aug.rand_crop(
                img1, img2, grt1, grt2, mode=mode)
            if cfg.AUG.RICH_CROP.ENABLE:
                if cfg.AUG.RICH_CROP.BLUR:
                    if cfg.AUG.RICH_CROP.BLUR_RATIO <= 0:
                        n = 0
                    elif cfg.AUG.RICH_CROP.BLUR_RATIO >= 1:
                        n = 1
                    else:
                        n = int(1.0 / cfg.AUG.RICH_CROP.BLUR_RATIO)
                    if n > 0:
                        if np.random.randint(0, n) == 0:
                            radius = np.random.randint(3, 10)
                            if radius % 2 != 1:
                                radius = radius + 1
                            if radius > 9:
                                radius = 9
                            img1 = cv2.GaussianBlur(img1, (radius, radius), 0,
                                                    0)
                            if img2 is not None:
                                img2 = cv2.GaussianBlur(img2, (radius, radius),
                                                        0, 0)

                img1, img2, grt1, grt2 = aug.random_rotation(
                    img1,
                    img2,
                    grt1,
                    grt2,
                    rich_crop_max_rotation=cfg.AUG.RICH_CROP.MAX_ROTATION,
                    mean_value=cfg.DATASET.PADDING_VALUE)

                img1, img2, grt1, grt2 = aug.rand_scale_aspect(
                    img1,
                    img2,
                    grt1,
                    grt2,
                    rich_crop_min_scale=cfg.AUG.RICH_CROP.MIN_AREA_RATIO,
                    rich_crop_aspect_ratio=cfg.AUG.RICH_CROP.ASPECT_RATIO)

                img1, img2 = aug.hsv_color_jitter(
                    img1,
                    img2,
                    brightness_jitter_ratio=cfg.AUG.RICH_CROP.
                    BRIGHTNESS_JITTER_RATIO,
                    saturation_jitter_ratio=cfg.AUG.RICH_CROP.
                    SATURATION_JITTER_RATIO,
                    contrast_jitter_ratio=cfg.AUG.RICH_CROP.
                    CONTRAST_JITTER_RATIO)

            if cfg.AUG.RANDOM_ROTATION90:
                rot_k = np.random.randint(0, 4)
                img1 = np.rot90(img1, k=rot_k)
                img2 = np.rot90(img2, k=rot_k) if img2 is not None else None
                grt1 = np.rot90(grt1, k=rot_k)
                grt2 = np.rot90(grt2, k=rot_k) if grt2 is not None else None

            if cfg.AUG.FLIP:
                if cfg.AUG.FLIP_RATIO <= 0:
                    n = 0
                elif cfg.AUG.FLIP_RATIO >= 1:
                    n = 1
                else:
                    n = int(1.0 / cfg.AUG.FLIP_RATIO)
                if n > 0:
                    if np.random.randint(0, n) == 0:
                        img1 = img1[::-1, :, :]
                        img2 = img2[::-1, :, :] if img2 is not None else None
                        grt1 = grt1[::-1, :]
                        grt2 = grt2[::-1, :] if grt2 is not None else None

            if cfg.AUG.MIRROR:
                if np.random.randint(0, 2) == 1:
                    img1 = img1[:, ::-1, :]
                    img2 = img2[:, ::-1, :] if img2 is not None else None
                    grt1 = grt1[:, ::-1]
                    grt2 = grt2[:, ::-1] if grt2 is not None else None

        elif ModelPhase.is_eval(mode):
            img1, img2, grt1, grt2 = aug.resize(
                img1, img2, grt1, grt2, mode=mode)
            img1, img2, grt1, grt2 = aug.rand_crop(
                img1, img2, grt1, grt2, mode=mode)
            if cfg.TEST.TEST_AUG:
                img1 = self.test_aug(img1)
                img2 = self.test_aug(img2) if img2 is not None else None

        elif ModelPhase.is_visual(mode):
            org_shape = [img1.shape[0], img1.shape[1]]
            img1, img2, grt1, grt2 = aug.resize(
                img1, img2, grt1, grt2, mode=mode)
            valid_shape = [img1.shape[0], img1.shape[1]]
            img1, img2, grt1, grt2 = aug.rand_crop(
                img1, img2, grt1, grt2, mode=mode)
        else:
            raise ValueError("Dataset mode={} Error!".format(mode))

        # Normalize image
        img1 = self.normalize_image(img1)
        img2 = self.normalize_image(img2) if img2 is not None else None

        if grt2 is not None:
            grt = grt1 * cfg.DATASET.NUM_CLASSES + grt2

            unchange_idx = np.where((grt1 - grt2) == 0)
            grt[unchange_idx] = 0
            if cfg.DATASET.NUM_CLASSES == 2:
                grt[np.where(grt != 0)] = 1

            ignore_idx = np.where((grt1 == cfg.DATASET.IGNORE_INDEX)
                                  | (grt2 == cfg.DATASET.IGNORE_INDEX))
            grt[ignore_idx] = cfg.DATASET.IGNORE_INDEX
        else:
            grt = grt1

        if ModelPhase.is_train(mode) or ModelPhase.is_eval(mode):
            grt = np.expand_dims(np.array(grt).astype('int32'), axis=0)
            ignore = (grt != cfg.DATASET.IGNORE_INDEX).astype('int32')

        if cfg.DATASET.INPUT_IMAGE_NUM == 1:
            if ModelPhase.is_train(mode):
                return (img1, grt, ignore)
            elif ModelPhase.is_eval(mode):
                return (img1, grt, ignore)
            elif ModelPhase.is_visual(mode):
                return (img1, grt, img1_name, valid_shape, org_shape)
        else:
            if ModelPhase.is_train(mode):
                return (img1, img2, grt, ignore)
            elif ModelPhase.is_eval(mode):
                return (img1, img2, grt, ignore)
            elif ModelPhase.is_visual(mode):
                return (img1, img2, grt, img1_name, img2_name, valid_shape,
                        org_shape)
Example #3
0
    def process_image(self, line, data_dir, mode):
        """ process_image """
        img, grt, img_name, grt_name = self.load_image(
            line, data_dir, mode=mode)
        if mode == ModelPhase.TRAIN:
            img, grt = aug.resize(img, grt, mode)
            if cfg.AUG.RICH_CROP.ENABLE:
                if cfg.AUG.RICH_CROP.BLUR:
                    if cfg.AUG.RICH_CROP.BLUR_RATIO <= 0:
                        n = 0
                    elif cfg.AUG.RICH_CROP.BLUR_RATIO >= 1:
                        n = 1
                    else:
                        n = int(1.0 / cfg.AUG.RICH_CROP.BLUR_RATIO)
                    if n > 0:
                        if np.random.randint(0, n) == 0:
                            radius = np.random.randint(3, 10)
                            if radius % 2 != 1:
                                radius = radius + 1
                            if radius > 9:
                                radius = 9
                            img = cv2.GaussianBlur(img, (radius, radius), 0, 0)

                img, grt = aug.random_rotation(
                    img,
                    grt,
                    rich_crop_max_rotation=cfg.AUG.RICH_CROP.MAX_ROTATION,
                    mean_value=cfg.DATASET.PADDING_VALUE)

                img, grt = aug.rand_scale_aspect(
                    img,
                    grt,
                    rich_crop_min_scale=cfg.AUG.RICH_CROP.MIN_AREA_RATIO,
                    rich_crop_aspect_ratio=cfg.AUG.RICH_CROP.ASPECT_RATIO)
                img = aug.hsv_color_jitter(
                    img,
                    brightness_jitter_ratio=cfg.AUG.RICH_CROP.
                    BRIGHTNESS_JITTER_RATIO,
                    saturation_jitter_ratio=cfg.AUG.RICH_CROP.
                    SATURATION_JITTER_RATIO,
                    contrast_jitter_ratio=cfg.AUG.RICH_CROP.
                    CONTRAST_JITTER_RATIO)

            if cfg.AUG.FLIP:
                if cfg.AUG.FLIP_RATIO <= 0:
                    n = 0
                elif cfg.AUG.FLIP_RATIO >= 1:
                    n = 1
                else:
                    n = int(1.0 / cfg.AUG.FLIP_RATIO)
                if n > 0:
                    if np.random.randint(0, n) == 0:
                        img = img[::-1, :, :]
                        grt = grt[::-1, :]

            if cfg.AUG.MIRROR:
                if np.random.randint(0, 2) == 1:
                    img = img[:, ::-1, :]
                    grt = grt[:, ::-1]

            img, grt = aug.rand_crop(img, grt, mode=mode)
        elif ModelPhase.is_eval(mode):
            img, grt = aug.resize(img, grt, mode=mode)
            img, grt = aug.rand_crop(img, grt, mode=mode)
        elif ModelPhase.is_visual(mode):
            org_shape = [img.shape[0], img.shape[1]]
            img, grt = aug.resize(img, grt, mode=mode)
            valid_shape = [img.shape[0], img.shape[1]]
            img, grt = aug.rand_crop(img, grt, mode=mode)
        else:
            raise ValueError("Dataset mode={} Error!".format(mode))

        # Normalize image
        img = self.normalize_image(img)

        if ModelPhase.is_train(mode) or ModelPhase.is_eval(mode):
            grt = np.expand_dims(np.array(grt).astype('int32'), axis=0)
            ignore = (grt != cfg.DATASET.IGNORE_INDEX).astype('int32')

        if ModelPhase.is_train(mode):
            return (img, grt, ignore)
        elif ModelPhase.is_eval(mode):
            return (img, grt, ignore)
        elif ModelPhase.is_visual(mode):
            return (img, grt, img_name, valid_shape, org_shape)
Example #4
0
if __name__ == '__main__':

    #    path = '/home/eric/Desktop/CHNCXR_0327_1.jpg'
    path = '/home/eric/Desktop/218.jpeg'
    mask_path = '/home/eric/Desktop/218_mask.jpeg'

    image = cv2.imread(path, 0)
    image = tf.Variable(image, tf.float32)
    mask = cv2.imread(mask_path, 0)
    mask = tf.image.convert_image_dtype(mask, tf.float32)

    image = tf.expand_dims(image, -1)
    mask = tf.expand_dims(mask, -1)
    #    img = data_aug.random_flip(image)
    #img = data_aug.random_light(image)
    img, mask = data_aug.random_rotation(image, mask)
    img, mask = data_aug.random_move(img, mask)
    #print img.get_shape().as_list()
    mask = tf.cast(mask, tf.float32)
    mask = tf.image.resize_images(mask, (1024, 1024))
    img = tf.squeeze(img)
    msk = tf.squeeze(mask)
    msk = tf.cast(msk, tf.uint8)
    out = img * msk

    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        img = sess.run(out)
    cv2.imshow('', np.uint8(img))
    cv2.waitKey()
    cv2.destroyAllWindows()