Exemplo n.º 1
0
def score_from_dataframe(submission_df, ground_truth_df):
    scores = []

    _, id_to_prediction = dataframe_to_dict(submission_df)
    sample_ids, id_to_ground_truth = dataframe_to_dict(ground_truth_df)

    for mask_id in sample_ids:
        if mask_id not in id_to_prediction:
            raise ValueError("Missing id in prediction, can't compute score")
        prediction = rle_decode(id_to_prediction[mask_id], SIZE)
        ground_truth = rle_decode(id_to_ground_truth[mask_id], SIZE)
        score = dice_score(ground_truth, prediction)
        scores.append(score)

    print('Dice score for submission is {}'.format(np.mean(scores)))
Exemplo n.º 2
0
def generate_masks(num_mask = -1):
    df = pd.read_csv(LABEL_FILE_PATH)
    print(df.head())

    if num_mask == -1:
        N = len(df)
    else:
        N = num_mask
    for i in range(N):
        binary_mask =utils.rle_decode(df["rle_mask"].iloc[i], (HEIGHT, LENGTH))
        result = Image.fromarray((binary_mask * 255).astype(np.uint8))
        image_path = MASK_FOLDER_PATH +df["img"].iloc[i] +".jpg"
        print(image_path)
        result.save(image_path)

        if i >= 5:
            break
                          compat=3,
                          kernel=dcrf.DIAG_KERNEL,
                          normalization=dcrf.NORMALIZE_SYMMETRIC)

    #Run Inference for 10 steps
    Q = d.inference(10)

    # Find out the most probable class for each pixel.
    MAP = np.argmax(Q, axis=0)

    return MAP.reshape((original_image.shape[0], original_image.shape[1]))


if __name__ == '__main__':

    df = pd.read_csv('saved/submission.csv')
    test_path = 'input/test/images/'
    """
    Applying CRF on the predicted mask 

    """
    for i in tqdm(range(df.shape[0])):
        if str(df.loc[i, 'rle_mask']) != str(np.nan):
            decoded_mask = rle_decode(df.loc[i, 'rle_mask'], shape=(101, 101))
            # print(decoded_mask.T)
            orig_img = imread(test_path + df.loc[i, 'id'] + '.png')
            crf_output = crf(orig_img, decoded_mask)
            # assert(False)
            df.loc[i, 'rle_mask'] = rle_encode(crf_output.T)

    df.to_csv('saved/submission_CRF.csv', index=False)
    image_path = os.path.join('stage1_test', image_id, 'images',
                              image_id + '.png')

    original_image = cv2.imread(image_path)
    results = model.detect([original_image], verbose=0)
    r = results[0]

    masks = r['masks']

    count = masks.shape[-1]
    occlusion = np.logical_not(masks[:, :, -1]).astype(np.uint8)

    for i in range(count - 2, -1, -1):
        mask = masks[:, :, i] * occlusion
        mask_rle = rle_to_string(rle_encode(mask))

        # Sanity check
        try:
            rle_decode(mask_rle, original_image.shape[:-1])
            output.append([image_id, mask_rle])
            occlusion = np.logical_and(occlusion, np.logical_not(masks[:, :,
                                                                       i]))

        except Exception as e:
            print(e)
            print(image_id)
            print('---')

output_df = pd.DataFrame(output, columns=['ImageId', 'EncodedPixels'])
output_df.to_csv('submission.csv', index=False, encoding='utf-8')
Exemplo n.º 5
0
    def __getitem__(self, index):
        """Generates one batch of data at position 'index'.

        Note: index=0 for batch 1, 2 for batch 2 and so on..

        Arguments:
            index (int): position of the batch in the Sequence.
        Returns:
            A batch.
            X (ndarray): array containing the image
                        4D array of size: batch_size x img_size[0] x img_size[1] x 3 (RGB)
            Y (ndarray): array containing the masks for corresponding images in X
                        4D array of size: batch_size x img_size[0] x img_size[1] x 3 (number of defect classes)
        """
        # (batch size, image height (rows), image width (columns), number of channels (RGB=3))
        X = np.zeros((self.batch_size, self.img_size[1], self.img_size[0], 3),
                     dtype=np.float32)

        # (batch size, image height (rows), image width (columns), number of (defect) classes = 3)
        Y = np.zeros((self.batch_size, self.img_size[1], self.img_size[0],
                      self.no_of_classes),
                     dtype=np.int8)

        class_map = {'scratch': 0, 'blister': 1, 'dent': 2}
        image_names = []
        for idx, annotation_object in self.fetch_data():

            image_name = annotation_object["image_name"]
            image_names.append(image_name)
            X[idx, ] = plt.imread(self.path + image_name)
            masks_rle = []
            defect_classes = []
            bboxes = []
            masks = np.zeros(
                (self.img_size[1], self.img_size[0], self.no_of_classes),
                dtype=np.int8)

            for label in annotation_object["labels"]:
                mask_rle = label["mask"]
                masks_rle.append(mask_rle)
                defect_classes.append(label["class_name"])
                bboxes.append(label["bbox"])

            # mapping defect classes to integers based on class_map dict
            defect_classes = list(map(class_map.get, defect_classes))

            for defect_class, mask, bbox in zip(defect_classes, masks_rle,
                                                bboxes):

                # if brush tool is used
                if mask is not None:
                    x_min, y_min, x_max, y_max = bbox
                    x_diff = x_max - x_min
                    y_diff = y_max - y_min
                    _mask = rle_decode(mask, (x_diff, y_diff))

                    defect_mask = np.zeros(
                        (self.img_size[1], self.img_size[0]), dtype=np.int8)

                    defect_mask[y_min:y_max, x_min:x_max][_mask == 1] = 1

                    masks[:, :, defect_class] = np.logical_or(
                        masks[:, :, defect_class], defect_mask)

            for defect_class in defect_classes:
                Y[idx, :, :, defect_class] = masks[:, :, defect_class]

            if idx == self.batch_size - 1:
                if self.preprocess is not None: X = self.preprocess(X)
                return image_names, X, Y