コード例 #1
0
ファイル: load_data.py プロジェクト: michaelStettler/BVS
def _load_FEI_semantic(config):
    # get csv
    df = pd.read_csv(config['csv'])
    num_data = len(df.index)
    print("[DATA] Found {} images".format(num_data))

    #  declare x and y
    x = np.zeros((num_data, 224, 224, 3))
    y = np.zeros((num_data, 224, 224, config['num_cat']))

    for idx, row in df.iterrows():
        # load image
        im = cv2.imread(os.path.join(config['img_path'], row['img']))
        im = cv2.cvtColor(im, cv2.COLOR_BGR2RGB)
        # resize and pad image
        img = resize_image(im)
        img = pad_image(img)
        # add image to data
        x[idx] = img

        # load label
        label = np.load(os.path.join(config['label_path'], row['label']))
        # resize and pad label
        label = resize_image(label)
        label = pad_image(label, dim=(224, 224, config['num_cat']))
        # add label to data
        y[idx] = label

    return [x, y]
コード例 #2
0
def undistort(image_path, camera_matrix, distortion_coeffs,
              camera_matrix_with_crop):
    from utils.image_utils import resize_image

    image = cv2.imread(image_path)
    cv2.imshow("distorted", resize_image(image, (1920, 1080)))
    cv2.waitKey()
    undistorted_image = cv2.undistort(image, camera_matrix, distortion_coeffs,
                                      None, camera_matrix_with_crop)
    cv2.imshow("undistorted", resize_image(undistorted_image, (1920, 1080)))
    cv2.waitKey()
コード例 #3
0
    def predict(self):
        sub_list = []
        missing_count = 0
        for i, row in tqdm(self._sample_df.iterrows(),
                           total=len(self._sample_df)):
            image = resize_image('{0}{1}'.format(FGVC6_TEST_IMAGES_FOLDER_PATH,
                                                 row['ImageId']))
            result = self._model.detect([image])[0]
            if result['masks'].size > 0:
                masks, _ = refine_masks(result['masks'], result['rois'])
                for m in range(masks.shape[-1]):
                    mask = masks[:, :, m].ravel(order='F')
                    rle = to_rle(mask)
                    label = result['class_ids'][m] - 1
                    sub_list.append(
                        [row['ImageId'], ' '.join(list(map(str, rle))), label])
            else:
                # The system does not allow missing ids, this is an easy way to fill them
                sub_list.append([row['ImageId'], '1 1', 23])
                missing_count += 1

        submission_df = pd.DataFrame(sub_list,
                                     columns=self._sample_df.columns.values)
        print("Total image results: ", submission_df['ImageId'].nunique())
        print("Missing Images: ", missing_count)
        submission_df.to_csv(FGVC6_SUBMISSION_CSV_PATH, index=False)

        submission_df.head()
コード例 #4
0
ファイル: load_data.py プロジェクト: michaelStettler/BVS
def _load_FEI_SA(config):
    # get csv
    df = pd.read_csv(config['csv'])
    num_data = len(df.index)
    print("[DATA] Found {} images".format(num_data))
    # print(df.head())

    #  declare x and y
    x = np.zeros((num_data, 224, 224, 3))
    y = np.zeros((num_data, 50))

    # create training and label data
    idx = 0
    for index, row in tqdm(df.iterrows()):
        # load img
        im = cv2.imread(os.path.join(config['SA_img_path'], row['img_name']))
        im = cv2.cvtColor(im, cv2.COLOR_BGR2RGB)

        # resize and pad image
        img = resize_image(im)
        img = pad_image(img)

        # add image to data
        x[idx, :, :, :] = img

        # transform string back to array
        S = row['S']
        A = row['A']
        # remove the [] and split with space
        S = S[1:-1].split(' ')
        A = A[1:-1].split(' ')
        # transform to array and remove the empty space
        S = [float(s) for s in S if s != '']
        A = [float(a) for a in A if a != '']
        # set shape and appearance index to the label
        y[idx, :25] = S
        y[idx, 25:] = A

        # increase idx
        idx += 1

    return [x, y]
コード例 #5
0
    def visualize(self):
        for i in range(9):
            image_id = self._sample_df.sample()['ImageId'].values[0]
            image_path = str('{0}{1}'.format(FGVC6_TEST_IMAGES_FOLDER_PATH,
                                             image_id))

            img = cv2.imread(image_path)
            img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

            result = self._model.detect([resize_image(image_path)])
            r = result[0]

            if r['masks'].size > 0:
                masks = np.zeros(
                    (img.shape[0], img.shape[1], r['masks'].shape[-1]),
                    dtype=np.uint8)
                for m in range(r['masks'].shape[-1]):
                    masks[:, :,
                          m] = cv2.resize(r['masks'][:, :, m].astype('uint8'),
                                          (img.shape[1], img.shape[0]),
                                          interpolation=cv2.INTER_NEAREST)

                y_scale = img.shape[0] / IMAGE_SIZE
                x_scale = img.shape[1] / IMAGE_SIZE
                rois = (r['rois'] *
                        [y_scale, x_scale, y_scale, x_scale]).astype(int)

                masks, rois = refine_masks(masks, rois)
            else:
                masks, rois = r['masks'], r['rois']

            visualize.display_instances(img,
                                        rois,
                                        masks,
                                        r['class_ids'],
                                        ['bg'] + self._class_names,
                                        r['scores'],
                                        title=image_id,
                                        figsize=(12, 12))
コード例 #6
0
ファイル: load_data.py プロジェクト: michaelStettler/BVS
def _load_FEI(config):
    # get csv
    df = pd.read_csv(config['csv'])
    num_data = len(df.index)
    print("[DATA] Found {} images".format(num_data))
    # print(df.head())

    #  declare x and y
    x = np.zeros((num_data, 224, 224, 3))
    y = np.zeros(num_data)

    # create training and label data
    idx = 0
    for index, row in tqdm(df.iterrows()):
        # load img
        im = cv2.imread(os.path.join(row['path'], row['image']))
        im = cv2.cvtColor(im, cv2.COLOR_BGR2RGB)

        # resize and pad image
        img = resize_image(im)
        img = pad_image(img)

        # add image to data
        x[idx, :, :, :] = img

        category = -1
        if row['category'] == "face":
            category = 0
        elif row['category'] == "non_face":
            category = 1
        else:
            warnings.warn(
                "WARNING! image: '{}' category '{}' seems not to exists!".
                format(row['image'], row['category']))
        y[idx] = category

        idx += 1

    return [x, y]
コード例 #7
0
 def load_image(self, image_id):
     return resize_image(self.image_info[image_id]['path'])
コード例 #8
0
ファイル: train.py プロジェクト: vicmancr/mnms_example
def augmentation_function(images, labels, exp_config, **kwargs):
    '''
    Function for augmentation of minibatches. It will transform a set of images and corresponding labels
    by a number of optional transformations. Each image/mask pair in the minibatch will be seperately transformed
    with random parameters. 
    :param images: A numpy array of shape [minibatch, X, Y, (Z), nchannels]
    :param labels: A numpy array containing a corresponding label mask
    :param do_rotations: Rotate the input images by a random angle between -15 and 15 degrees.
    :param do_scaleaug: Do scale augmentation by sampling one length of a square, then cropping and upsampling the image
                        back to the original size. 
    :param do_fliplr: Perform random flips with a 50% chance in the left right direction. 
    :return: A mini batch of the same size but with transformed images and masks. 
    '''

    do_rotations = kwargs.get('do_rotations', False)
    do_strong_rotations = kwargs.get('do_strong_rotations', False)
    do_shear = kwargs.get('do_shear', False)
    do_scaleaug = kwargs.get('do_scaleaug', False)
    do_fliplr = kwargs.get('do_fliplr', False)
    do_disturb = kwargs.get('do_disturb', False)
    mixup = kwargs.get('mixup', False)


    new_images = []
    new_labels = []
    num_images = images.shape[0]

    for ii in range(num_images):

        img = np.squeeze(images[ii,...])
        lbl = np.squeeze(labels[ii,...])

        # ROTATE
        if do_rotations:
            angles = kwargs.get('angles', (-15,15))
            random_angle = np.random.uniform(angles[0], angles[1])
            img = image_utils.rotate_image(img, random_angle)
            lbl = image_utils.rotate_image(lbl, random_angle, interp=cv2.INTER_NEAREST)

        # SHEAR
        if do_shear:
            shear_factor = kwargs.get('shear_factor', 0.2)
            img = image_utils.shear_image(img, shear_factor)
            lbl = image_utils.shear_image(lbl, shear_factor, interp=cv2.INTER_NEAREST)

        # STRONG ROTATE
        if do_strong_rotations:
            angles = kwargs.get('angles', (-90,90))
            random_angle = np.random.uniform(angles[0], angles[1])
            img = image_utils.rotate_image(img, random_angle)
            lbl = image_utils.rotate_image(lbl, random_angle, interp=cv2.INTER_NEAREST)
            coin_flip = np.random.randint(2)
            if coin_flip == 0:
                img = np.flipud(img)
                lbl = np.flipud(lbl)

        # Add RANDOM NOISE to image
        if do_disturb > 0:
            coin_flip = np.random.randint(2)
            if coin_flip == 0:
                noise_mask = np.random.normal(0, 0.1, size=img.shape)
                img = img + noise_mask

        # RANDOM CROP SCALE
        if do_scaleaug:
            offset = kwargs.get('offset', 30)
            n_x, n_y = img.shape
            r_y = np.random.random_integers(n_y-offset, n_y)
            p_x = np.random.random_integers(0, n_x-r_y)
            p_y = np.random.random_integers(0, n_y-r_y)

            img = image_utils.resize_image(img[p_y:(p_y+r_y), p_x:(p_x+r_y)],(n_x, n_y))
            lbl = image_utils.resize_image(lbl[p_y:(p_y + r_y), p_x:(p_x + r_y)], (n_x, n_y), interp=cv2.INTER_NEAREST)

        # RANDOM FLIP
        if do_fliplr:
            coin_flip = np.random.randint(2)
            if coin_flip == 0:
                img = np.fliplr(img)
                lbl = np.fliplr(lbl)

        # MIXUP
        if mixup:
            if not (exp_config.batch_size % 2):
                raise ValueError('Batch size must be even to apply mixup. Current batch size is {}'.format(exp_config.batch_size))
            l = np.random.uniform()
            if ii % 2:
                img = l*np.squeeze(images[ii-1,...]) + (1-l)*img
            else:
                img = l*img + (1-l)*np.squeeze(images[ii+1,...])

        new_images.append(img[..., np.newaxis])
        new_labels.append(lbl[...])

    sampled_image_batch = np.asarray(new_images)
    sampled_label_batch = np.asarray(new_labels)

    return sampled_image_batch, sampled_label_batch
コード例 #9
0
def load_image_gt(dataset,
                  config,
                  image_id,
                  augment=False,
                  augmentation=None,
                  use_mini_mask=False):
    # 载入图片和语义分割效果
    image = dataset.load_image(image_id)
    mask, class_ids = dataset.load_mask(image_id)
    # print("\nbefore:",image_id,np.shape(mask),np.shape(class_ids))
    # 原始shape
    original_shape = image.shape
    # 获得新图片,原图片在新图片中的位置,变化的尺度,填充的情况等
    image, window, scale, padding, crop = utils.resize_image(
        image,
        min_dim=config.IMAGE_MIN_DIM,
        min_scale=config.IMAGE_MIN_SCALE,
        max_dim=config.IMAGE_MAX_DIM,
        mode=config.IMAGE_RESIZE_MODE)
    mask = utils.resize_mask(mask, scale, padding, crop)
    # print("\nafter:",np.shape(mask),np.shape(class_ids))
    # print(np.shape(image),np.shape(mask))
    # 可以把图片进行翻转
    if augment:
        logging.warning("'augment' is deprecated. Use 'augmentation' instead.")
        if random.randint(0, 1):
            image = np.fliplr(image)
            mask = np.fliplr(mask)

    if augmentation:
        import imgaug
        # 可用于图像增强
        MASK_AUGMENTERS = [
            "Sequential", "SomeOf", "OneOf", "Sometimes", "Fliplr", "Flipud",
            "CropAndPad", "Affine", "PiecewiseAffine"
        ]

        def hook(images, augmenter, parents, default):
            """Determines which augmenters to apply to masks."""
            return augmenter.__class__.__name__ in MASK_AUGMENTERS

        image_shape = image.shape
        mask_shape = mask.shape
        det = augmentation.to_deterministic()
        image = det.augment_image(image)
        mask = det.augment_image(mask.astype(np.uint8),
                                 hooks=imgaug.HooksImages(activator=hook))
        assert image.shape == image_shape, "Augmentation shouldn't change image size"
        assert mask.shape == mask_shape, "Augmentation shouldn't change mask size"
        mask = mask.astype(np.bool)
    # 检漏,防止某些层内部实际上不存在语义分割情况
    _idx = np.sum(mask, axis=(0, 1)) > 0

    # print("\nafterer:",np.shape(mask),np.shape(_idx))
    mask = mask[:, :, _idx]
    class_ids = class_ids[_idx]
    # 找到mask对应的box
    bbox = utils.extract_bboxes(mask)

    active_class_ids = np.zeros([dataset.num_classes], dtype=np.int32)
    source_class_ids = dataset.source_class_ids[dataset.image_info[image_id]
                                                ["source"]]
    active_class_ids[source_class_ids] = 1

    if use_mini_mask:
        mask = utils.minimize_mask(bbox, mask, config.MINI_MASK_SHAPE)

    # 生成Image_meta
    image_meta = utils.compose_image_meta(image_id, original_shape,
                                          image.shape, window, scale,
                                          active_class_ids)

    return image, image_meta, class_ids, bbox, mask