Ejemplo n.º 1
0
    def __getitem__(self, idx):
        img_path = os.path.join(self.image_dir, self.ids[idx])
        img = Image.open(img_path + '.png').convert("RGB")
        width, height = img.size

        mask = rle2mask(self.rles[idx], width, height)
        mask = mask.T

        label = self.labels[idx]

        if self.augs is not None:
            augmented = self.augs(image=np.array(img), mask=np.array(mask))
            img = augmented['image']
            mask = augmented['mask']

        img = cv2.resize(img, (config.size, config.size))
        mask = cv2.resize(mask, (config.size, config.size))
        #         mask = cv2.resize(mask, (int(Size/4), int(Size/4))) # ********* the output of network is image_size/4 ***************************

        mask = torch.as_tensor(mask, dtype=torch.float)
        img = transforms.ToTensor()(img)
        #         img = transforms.Normalize(mean=[0.491, 0.491, 0.491],
        #                                    std=[0.249, 0.249, 0.249])(img)

        return img, mask, label, self.ids[idx]
Ejemplo n.º 2
0
 def cal_dice(thres_seg, size_seg, thres_after = 0.2):
     ipos = 0
     dice = 0.0
     for pred, true_rle in zip(preds, trues):
         # post process
         true = rle2mask(true_rle, self.args.width, self.args.height)
         pred = post_process_segment(pred, thres_seg, size_seg, thres_after)
         ipos += 1
         dice += dice_metric(true, pred)
     return dice/len(preds)
Ejemplo n.º 3
0
    def get_masks(self, encoded_masks):
        masks = np.zeros((4, self.shape[0], self.shape[1]), dtype=np.float32)
        for idx, label in enumerate(encoded_masks.values):
            if label is not np.nan:
                mask = utils.rle2mask(label, self.shape)
                masks[idx, :, :] = mask
        resized_masks = np.zeros((4, 350, 525), dtype=np.float32)

        for idx in range(4):
            resized_masks[idx, :, :] = resize(masks[idx, :, :], (525, 350))
        return torch.as_tensor(resized_masks, dtype=torch.float32)
Ejemplo n.º 4
0
def main():
    train = pd.read_csv(os.path.join('..', 'input', 'preprocessed_train.csv'))

    mask_list = []
    for (_, row) in tqdm(train.iterrows()):
        mask = np.zeros((256, 1600, 4))
        for i in range(0, 4):
            mask[:, :, i] = rle2mask(row[f'{i+1}'], (256, 1600))
        with open(
                os.path.join('..', 'input', 'train_masks',
                             row['ImageId'].replace('jpg', 'pkl')), 'wb') as f:
            pickle.dump(mask, f)
Ejemplo n.º 5
0
def plot_mask_on_img(old_img):

    tdf = pd.read_csv('mask_test.csv', index_col=0)
    tdf = tdf.fillna('')

    plt.figure(figsize=[25, 20])
    types = ''
    for index, row in tdf.iterrows():
        img = cv2.imread(old_img)
        img = cv2.resize(img, (525, 350))
        mask_rle = row['EncodedPixels']
        plt.subplot(2, 2, index + 1)
        plt.imshow(img)
        if mask_rle != '':
            types = types + ', ' + row['Image_Label'].split('_')[-1]

        plt.imshow(utils.rle2mask(mask_rle, img.shape), alpha=0.5, cmap='gray')
        plt.title(row['Image_Label'].split('_')[-1], fontsize=25)
        plt.axis('off')

    types_all = types[2:]
    after_mask = ''.join(str(uuid.uuid4()).split('-')) + '.png'
    plt.savefig('static/upload/' + after_mask)
    plt.close()

    imgId = str(random.randint(22200, 24200)) + '.jpg'
    print(imgId)
    for i in types_all.split(', '):
        query = """INSERT INTO `project_id.project_satellite.report`
                   (int64_field_0 , ImageId, Label, exist, Frequent, types )
                   VALUES (2, @d, @a, 1, @b, @c)"""

        query_params = [
            bigquery.ScalarQueryParameter("d", "STRING", imgId),
            bigquery.ScalarQueryParameter("a", "STRING", i),
            bigquery.ScalarQueryParameter("b", "STRING", types_all),
            bigquery.ScalarQueryParameter("c", "INT64",
                                          len(types_all.split(', '))),
        ]

        job_config = bigquery.QueryJobConfig()
        job_config.query_parameters = query_params

        client.query(
            query,
            # Location must match that of the dataset(s) referenced in the query.
            location="US",
            job_config=job_config,
        )

    return after_mask
Ejemplo n.º 6
0
    def _PS_data_preprocess_np(fns, df, TARGET_COLUMN, im_height, im_width,
                               im_chan):
        X_train = np.zeros((len(fns), im_height, im_width, im_chan),
                           dtype=np.uint8)
        Y_train = np.zeros((len(fns), im_height, im_width, 1), dtype=np.uint8)
        print("Getting train images and masks ... ")
        # sys.stdout.flush()
        for n, _id in tqdm(enumerate(fns), total=len(fns)):
            dataset = pydicom.read_file(_id)
            _id_keystr = _id.split("/")[-1][:-4]
            X_train[n] = np.expand_dims(dataset.pixel_array, axis=2)
            try:
                mask_data = df.loc[_id_keystr, TARGET_COLUMN]

                if "-1" in mask_data:
                    Y_train[n] = np.zeros((1024, 1024, 1))
                else:
                    if type(mask_data) == str:
                        Y_train[n] = np.expand_dims(
                            utils.rle2mask(df.loc[_id_keystr, TARGET_COLUMN],
                                           1024, 1024).T,
                            axis=2,
                        )
                    else:
                        Y_train[n] = np.zeros((1024, 1024, 1))
                        for x in mask_data:
                            Y_train[n] = Y_train[n] + np.expand_dims(
                                utils.rle2mask(x, 1024, 1024).T, axis=2)
            except KeyError:
                print(
                    f"Key {_id.split('/')[-1][:-4]} without mask, assuming healthy patient."
                )
                # Assume missing masks are empty masks.
                Y_train[n] = np.zeros((1024, 1024, 1))

        print("Done data preprocessing as numpy array!")

        return X_train, Y_train
    def __getitem__(self, idx):
        img_path = self.image_info[idx]["image_path"]
        img = Image.open(img_path + ".png").convert(
            "RGB")  # here it is converted to 3 channels
        width, height = img.size
        img = img.resize((self.width, self.height), resample=Image.BILINEAR)
        info = self.image_info[idx]

        mask = utils.rle2mask(info["annotations"], width, height)

        if mask is None:
            raise ValueError("mask is None!!!!!!!!")

        mask = Image.fromarray(mask.T)
        mask = mask.resize((self.width, self.height), resample=Image.BILINEAR)
        img, mask = self.functional_transforms(
            img, mask)  # pay attention need after mask.T
        mask = np.expand_dims(mask, axis=0)

        if sum(mask.flatten()) == 0:
            xmax = 0
            xmin = 0
            ymax = 0
            ymin = 0
        else:
            pos = np.where(np.array(mask)[0, :, :])
            xmin = np.min(pos[1])
            xmax = np.max(pos[1])
            ymin = np.min(pos[0])
            ymax = np.max(pos[0])

        boxes = torch.as_tensor([[xmin, ymin, xmax, ymax]],
                                dtype=torch.float32)
        labels = torch.ones((1, ), dtype=torch.int64)
        masks = torch.as_tensor(mask, dtype=torch.uint8)

        image_id = torch.tensor([idx])
        area = (boxes[:, 3] - boxes[:, 1]) * (boxes[:, 2] - boxes[:, 0])
        iscrowd = torch.zeros((1, ), dtype=torch.int64)

        target = {}
        target["boxes"] = boxes
        target["labels"] = labels
        target["masks"] = masks
        target["image_id"] = image_id
        target["area"] = area
        target["iscrowd"] = iscrowd

        img = self.to_tensor_transformer(img)
        return img, target
Ejemplo n.º 8
0
    def __getitem__(self, index):
        fn, class_id_str = self.df['ImageId_ClassId'].iloc[index].rsplit(
            '_', -1)
        img = Image.open(self.data_path + fn)
        img = self.transform(img)

        if self.subset == 'train' or self.subset == 'valid':
            mask = rle2mask(self.df['EncodedPixels'].iloc[index], (256, 1600))
            mask = transforms.ToPILImage()(mask)
            mask = self.transform(mask) != 0  # ensure tensor is either 0 or 1
            mask = mask.float()
            return img, mask, int(class_id_str)
        else:
            mask = None
            return img
def save_segmentation(cam, weights, num, path, thresholds = [0.8,0.5,0.7,0.7]):
    th = thresholds
    for img_idx in test_df.index:
        img_name = IMG_PATH + img_idx + ".jpg"
        # while IMG_LIST[k].split(".")[0] not in test_df.index: k = np.random.randint(0, len(IMG_LIST))
        img = cv2.resize(cv2.imread(img_name), (512, 352))
        for label_idx in [0,1,2,3]:
            mask_pred, probs = get_rle_probs(cam, weights, img_name, label_idx)
            label = LABELS[label_idx]
            rle_true = data_df.loc[img_idx.split('.')[0], "rle_" + label]
            rle_pred = mask2rle((mask_pred > th[label_idx]).astype(int))
            mask_true = rle2mask(rle_true)[::4,::4]

            skimage.io.imsave(path + img_idx + "_pred_" + label + ".jpg", (mask_pred > th[label_idx]).astype("uint8")*100)
            skimage.io.imsave(path + img_idx + "_heat_" + label + ".jpg", (mask_pred*100).astype("uint8"))
            skimage.io.imsave(path + img_idx + "_true_" + label + ".jpg", mask_true*100)
            dice = dice_coef(rle_true, rle_pred, probs, th[label_idx])
            print("Dice = " + str(np.round(dice,3)))
Ejemplo n.º 10
0
def get_segmentation_masks(map_size, gaze_sequences):
    # load seg dict
    seg_dict_pth = "/media/pneumothorax/rle_dict.pkl"
    with open(seg_dict_pth, "rb") as pkl_f:
        rle_dict = pickle.load(pkl_f)

    ret_rle = {}
    for gaze_id, val in tqdm(gaze_sequences.items(), total=len(gaze_sequences.keys())):
        img_path = gaze_id.split("/")[-1].split(".dcm")[0]
        rle = rle_dict[img_path]

        if rle != " -1":
            segmask_org = rle2mask(rle, 1024, 1024).T
            segmask = binarize_seg_mask(segmask_org, map_size)
            ret_rle[img_path] = segmask
        else:
            ret_rle[img_path] = rle 

    return ret_rle
Ejemplo n.º 11
0
        def cal_dice(thres_seg, size_seg, thres_after, thres_oth=-float('inf'), size_oth=0):
            ipos = 0
            dice = 0.0
            if self.args.use_weight:
                nnormal = self.nweight
            else:
                nnormal = len(self.dataloader.dataset)

            for pred, other, true_rle in zip(preds, others, trues):
                # post process
                true = rle2mask(true_rle, self.args.width, self.args.height)
                pred = post_process_single(pred, other, thres_seg, size_seg, thres_after, thres_oth, size_oth)
                if self.args.use_weight:
                    dice += dice_metric(true, pred)*self.weight[ipos]
                else:
                    dice += dice_metric(true, pred)
                ipos += 1

            return dice/nnormal
Ejemplo n.º 12
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 4 (number of defect classes)

        Note: If subset =' train', both the images along with its masks is returned. This is essentially the information
        contained in the train.csv file. If subset = 'test', only the images in the test_images folder is returned
        """
        # (batch size, image height, image width, number of channels (RGB=3))
        X = np.zeros((self.batch_size, self.img_size[0], self.img_size[1], 3),
                     dtype=np.float32)

        # (batch size, image height, image width, number of (defect) classes = 4 (one hot coded) )
        Y = np.zeros((self.batch_size, self.img_size[0], self.img_size[1], 4),
                     dtype=np.int8)

        # Generate indexes of the batch
        indexes = self.indexes[index * self.batch_size:(index + 1) *
                               self.batch_size]

        for idx, (imgID, masks) in enumerate(self.data[indexes]):
            self.info[index * self.batch_size + idx] = imgID
            X[idx, ] = plt.imread(self.data_path + imgID)[::2, ::2]
            if self.subset == 'train':
                defectsIDs = masks['defectIDs']
                masks = masks['masks']
                for m in range(len(defectsIDs)):
                    Y[idx, :, :, defectsIDs[m]] = rle2mask(masks[m])[::2, ::2]
        if self.preprocess != None: X = self.preprocess(X)
        if self.subset == 'train':
            return X, Y
        else:
            return X
Ejemplo n.º 13
0
    def __getitem__(self, idx):
        'get one image along with its masks on four categories'
        fpath = self.fpaths[idx]
        fname = fpath.split('/')[-1]
        # get the image
        image = cv2.imread(fpath)
        mask  = np.zeros((self.height, self.width, self.category))
        # if this is for training
        if self.mask_df is not None:
            # get the masks
            for classid in range(self.category):
                rle = self.mask_df.loc[fname + '_{:d}'.format(classid + 1), 'EncodedPixels']
                mask[:,:,classid] = rle2mask(rle, self.width, self.height)
        image, mask = np.uint8(image), np.uint8(mask)

        # if there is augmentation
        if self.augment is not None:
            augmented = self.augment(image = image, mask = mask)
            image, mask = augmented['image'], augmented['mask']
            # do additional augmentations for training data set
            if self.augTag:
                if not self.args.conservative:
                    image, mask = random_crop_shift_pad(image, mask)
                else:
                    image, mask = random_crop_shift_pad_old(image, mask)

        # do simple normalization
        else:
            image, mask = image/255, mask
        
        # do regression task
        if self.args.output == 1:
            area = np.array([self.stat_mask(mask[:,:,j]) for j in range(self.category)]).astype(np.float32) 
            return image.astype(np.float32), mask.astype(np.float32), area
        # do classification task
        elif self.args.output == 2:
            area = np.array([self.stat_mask(mask[:,:,j])>0 for j in range(self.category)]).astype(np.float32)
            return image.astype(np.float32), mask.astype(np.float32), area
        # vanilla version (0,3)
        else:     
            return image.astype(np.float32), mask.astype(np.float32)
Ejemplo n.º 14
0
    def __getitem__(self, idx):
        img_path = self.image_info[idx]["image_path"]
        img = Image.open(img_path + '.png').convert("RGB")
        width, height = img.size
        info = self.image_info[idx]

        mask = rle2mask(info['annotations'], width, height)
        mask = mask.T

        #         mask = np.expand_dims(mask, axis=0)

        #         mask = mask.T*255
        #         kernel = np.ones((6,6), np.uint8)
        #         dilation_mask = cv2.dilate(mask.astype(np.uint8), kernel, iterations=2) / 255
        # #         mask = Image.fromarray(dilation_mask/255)

        if self.augs is not None:
            img = np.array(img)
            mask = np.array(mask)
            #             size = random.randint(512,1024)
            #             img = cv2.resize(img, (config.size, config.size))
            #             mask = cv2.resize(mask, (config.size, config.size))
            augmented = self.augs(image=img, mask=mask)
            img = augmented['image']
            mask = augmented['mask']

        img = cv2.resize(img, (config.size, config.size))
        mask = cv2.resize(mask, (config.size, config.size))

        mask = torch.as_tensor(mask, dtype=torch.float)

        img = transforms.ToTensor()(img)
        #         img = transforms.Normalize(mean=[0.491, 0.491, 0.491],
        #                                    std=[0.249, 0.249, 0.249])(img)

        return img, mask
Ejemplo n.º 15
0
    augment_test = Compose([
        Normalize(mean=(test_mean, test_mean, test_mean),
                  std=(test_std, test_std, test_std)),
        ToFloat(max_value=1.)
    ],
                           p=1)

    ########################################################################
    # do some simple checking
    if args.test_run:
        # check rle2mask and mask2rle
        mask_df = pd.read_csv(TRAIN_MASKS).set_index(['ImageId_ClassId'
                                                      ]).fillna('-1')
        for i, pixel in enumerate(mask_df['EncodedPixels']):
            if pixel != '-1':
                rle_pass = mask2rle(rle2mask(pixel, 1600, 256))
                if rle_pass != pixel:
                    print(i)

        # check dataloader
        steel_ds = SteelDataset(TRAIN_FILES, args, mask_df=mask_df)
        steel_ds_train = SteelDataset(TRAIN_FILES,
                                      args,
                                      mask_df=mask_df,
                                      augment=augment_train)
        steel_ds_valid = SteelDataset(VALID_FILES,
                                      args,
                                      mask_df=mask_df,
                                      augment=augment_valid)
        res = steel_ds_train[1]
        image, mask = res[0], res[1]
Ejemplo n.º 16
0
    df['ImageId'] = df['ImageId_ClassId'].map(lambda x: x.split("_")[0])
    df['ClassId'] = df[['ImageId_ClassId',
                        'EncodedPixels']].apply(lambda x: x[0].split("_")[1]
                                                if x[1] is not np.nan else '0',
                                                axis=1)
    df.drop(columns='ImageId_ClassId', inplace=True)
    df.drop_duplicates(inplace=True)

    # drop row with label 0 which exist label != 0
    max_class_df = df.groupby("ImageId")['ClassId'].max().reset_index()
    max_class_df = max_class_df[max_class_df['ClassId'] != '0']
    df = df[~((df['ImageId'].isin(max_class_df['ImageId'].values)) &
              (df['ClassId'] == '0'))]

    # split to train and validation
    # Didn't use
    # tv_df = df.groupby("ImageId")['ClassId'].sum().reset_index()
    # train_images, val_images = train_test_split(tv_df['ImageId'].values, stratify=tv_df['ClassId'].values, test_size=0.2)

    images = df['ImageId'].unique()
    for image in tqdm(images):
        temp_df = df[df['ImageId'] == image]
        mask = np.zeros((4, 256, 1600), dtype=np.uint8)
        for row in temp_df.values:
            if row[0] is not np.nan:
                mask[int(row[2]) - 1] = rle2mask(row[0])
        mask = np.moveaxis(mask, 0, -1)
        cv2.imwrite(
            os.path.join(config['path_to_data'], 'train_masks',
                         image.replace('.jpg', '.png')), mask)
Ejemplo n.º 17
0
import os
import numpy as np
import pandas as pd

from tqdm import tqdm
from utils import mask2rle, rle2mask

import matplotlib.pyplot as plt

original_size = (1400, 2100)
new_size = (350, 525)  # ResNet

dataset_dir = './satellite_dataset/'
processed_dataset_dir = './satellite_dataset/preprocess_images'

df = pd.read_csv(os.path.join(dataset_dir, '350_525_train.csv'))

for idx, row in tqdm(df.iterrows()):
    encoded_pixels = row[1]
    names = row[0].split('_')  # i.g) 021401240.jpg, Flower
    filename = names[1] + '_' + names[0]

    if encoded_pixels is not np.nan:
        mask = rle2mask(encoded_pixels, new_size[::-1])
        plt.imsave(os.path.join(processed_dataset_dir + '/gt_train_images',
                                filename),
                   mask,
                   cmap='gray')
Ejemplo n.º 18
0
    def __getitem__(self, idx):
        """
        Fetch index idx image and label from dataset.
        Args:
            idx: (int) index in [0, 1, ..., size_of_dataset-1]
        Returns:
            image: (Tensor) transformed image
            label: (int) corresponding label of image
        """

        img_id, label = self.file_markers[idx]

        subclass_label = 0

        if self.source == "cxr_a":
            img_pth = os.path.join(self.data_dir,
                                   f"cxr_ibm/dicom_images/{img_id}")
        elif self.source == "cxr_p":
            img_pth = os.path.join(self.data_dir,
                                   f"pneumothorax/dicom_images/{img_id}")

            if self.args.machine == "meteor":
                tube_path = '/media/pneumothorax/cxr_tube_dict.pkl'
            elif self.args.machine == "gemini":
                tube_path = '/media/nvme_data/jupinder_cxr_robustness_results/cxr_tube_dict.pkl'
            else:
                raise ValueError("Machine type not known")

            with open(tube_path, 'rb') as f:
                cxr_tube_dict = pickle.load(f)

            image_name = img_id.split("/")[-1].split(".dcm")[0]

            if self.subclass:
                subclass_label = 1 if cxr_tube_dict[image_name] is True else 0

        else:
            img_pth = img_id

        if self.ood:
            img = Image.open(img_pth)
        else:
            ds = pydicom.dcmread(img_pth)
            img = ds.pixel_array
            img = Image.fromarray(np.uint8(img))

        img = self.transform(img)

        if not self.gan:
            if img.shape[0] == 1:
                img = torch.cat([img, img, img])

        if img.shape[0] >= 4:
            img = img[:3]

        if self.gan:
            return img, label

        if self.split_type in ['train', 'val']:

            gaze_attribute = self.gaze_features[img_id][0]

            if self.gaze_task == "data_augment":

                gaze_map = gaze_attribute.reshape(4, 4)
                gaze_binary_mask = torch.zeros(224, 224)
                divisor = int(224 / 4)

                for i in range(4):
                    for j in range(4):
                        if gaze_map[i, j] != 0:
                            gaze_binary_mask[(divisor) * (i):(divisor) *
                                             (i + 1),
                                             (divisor) * (j):(divisor) *
                                             (j + 1)] = torch.ones(
                                                 divisor, divisor)

                gaze_binary_mask = gaze_binary_mask.unsqueeze(0)
                gaze_binary_mask = torch.cat(
                    [gaze_binary_mask, gaze_binary_mask, gaze_binary_mask])
                img = img * gaze_binary_mask

            if self.gaze_task == "cam_reg_convex":
                gaze_attribute_dict = {
                    "gaze_attribute": gaze_attribute,
                    "average_hm": self.average_heatmap
                }

                return img, label, gaze_attribute_dict

            ### neet to return regular image, label, and masked image
            if self.gaze_task == "actdiff":

                rle = self.rle_dict[img_id.split("/")[-1].split(".dcm")[0]]
                y_true = rle != " -1"

                ### use the segmentation mask
                if y_true:
                    # extract segmask
                    segmask_org = rle2mask(rle, 1024, 1024).T

                    if self.args.actdiff_segmask_size != self.IMG_SIZE:

                        segmask_int = nn.functional.max_pool2d(
                            torch.tensor(segmask_org).unsqueeze(0),
                            int(1024 / self.args.actdiff_segmask_size)
                        ).squeeze().numpy()
                        segmask_int = (segmask_int > 0) * 1
                        segmask = resize(segmask_int,
                                         (self.IMG_SIZE, self.IMG_SIZE))
                        segmask = (segmask > 0) * 1

                    else:
                        segmask = resize(segmask_org,
                                         (self.IMG_SIZE, self.IMG_SIZE))
                        segmask = (segmask > 0) * 1

                    segmask = torch.from_numpy(segmask)
                    segmask = torch.where(segmask > 0,
                                          torch.ones(segmask.shape),
                                          torch.zeros(segmask.shape)).long()
                    img_masked = create_masked_image(img, segmask)
                    return img, label, img_masked

                ## we don't have a segmentation mask for the image, as in actdiff set segmentation mask to all 1
                else:
                    segmask = torch.ones((self.IMG_SIZE, self.IMG_SIZE)).long()
                    img_masked = create_masked_image(img, segmask)
                    return img, label, img_masked

            ### need to return regular image, label, and masked image from the gaze heatmap
            if self.gaze_task == "actdiff_gaze":
                ### obtain 7 x 7 gaze heatmap
                gaze_map = gaze_attribute.reshape(
                    self.args.actdiff_gazemap_size,
                    self.args.actdiff_gazemap_size)
                gaze_map = (gaze_map > self.args.actdiff_gaze_threshold) * 1.0

                ### resize up to 224 x 224
                gaze_map = resize(
                    gaze_map, (self.IMG_SIZE,
                               self.IMG_SIZE))  ### change to change_map_size
                gaze_map = torch.from_numpy(gaze_map)
                gaze_map = torch.where(gaze_map > 0,
                                       torch.ones(gaze_map.shape),
                                       torch.zeros(gaze_map.shape)).long()

                ### get masked image,
                img_masked = create_masked_image(img, gaze_map)

                return img, label, img_masked

            ### neet to return regular image, label, and masked image
            if self.gaze_task == "actdiff_lungmask":

                if self.args.actdiff_segmentation_classes == 'positive':

                    ## we have lungmasks for these right now
                    if label == 1:
                        img_name = img_id.replace("/", "_").split(".dcm")[0]
                        lung_mask = np.load(
                            f"./lung_segmentations/annotations/{img_name}_lungmask.npy"
                        )
                        lung_mask = np.where(lung_mask > 0,
                                             np.ones(lung_mask.shape),
                                             np.zeros(lung_mask.shape))

                        if self.args.actdiff_lungmask_size != self.IMG_SIZE:
                            lung_mask_int = nn.functional.max_pool2d(
                                torch.tensor(lung_mask).unsqueeze(0),
                                int(224 / self.args.actdiff_lungmask_size)
                            ).squeeze().numpy()
                            lung_mask_int = (lung_mask_int > 0) * 1
                            lung_mask = resize(lung_mask_int,
                                               (self.IMG_SIZE, self.IMG_SIZE))
                            lung_mask = (lung_mask > 0) * 1

                        lung_mask = torch.from_numpy(lung_mask)
                        lung_mask = torch.where(lung_mask > 0,
                                                torch.ones(lung_mask.shape),
                                                torch.zeros(
                                                    lung_mask.shape)).long()

                        img_masked = create_masked_image_advanced_augmentations(
                            img,
                            lung_mask,
                            augmentation=self.args.actdiff_augmentation_type,
                            masked_normalization_vals=self.
                            masked_normalization_values,
                            seed=self.seed)
                        return img, label, img_masked

                    else:
                        segmask = torch.ones(
                            (self.IMG_SIZE, self.IMG_SIZE)).long()
                        img_masked = create_masked_image_advanced_augmentations(
                            img,
                            segmask,
                            augmentation='normal',
                            masked_normalization_vals=self.
                            masked_normalization_values,
                            seed=self.seed)
                        return img, label, img_masked

                elif self.args.actdiff_segmentation_classes == 'all':

                    img_name = img_id.replace("/", "_").split(".dcm")[0]
                    lung_mask = np.load(
                        f"./lung_segmentations/annotations/{img_name}_lungmask.npy"
                    )
                    lung_mask = np.where(lung_mask > 0,
                                         np.ones(lung_mask.shape),
                                         np.zeros(lung_mask.shape))

                    if self.args.actdiff_lungmask_size != self.IMG_SIZE:
                        lung_mask_int = nn.functional.max_pool2d(
                            torch.tensor(lung_mask).unsqueeze(0),
                            int(224 / self.args.actdiff_lungmask_size)
                        ).squeeze().numpy()
                        lung_mask_int = (lung_mask_int > 0) * 1
                        lung_mask = resize(lung_mask_int,
                                           (self.IMG_SIZE, self.IMG_SIZE))
                        lung_mask = (lung_mask > 0) * 1

                    lung_mask = torch.from_numpy(lung_mask)
                    lung_mask = torch.where(lung_mask > 0,
                                            torch.ones(lung_mask.shape),
                                            torch.zeros(
                                                lung_mask.shape)).long()

                    img_masked = create_masked_image_advanced_augmentations(
                        img,
                        lung_mask,
                        augmentation=self.args.actdiff_augmentation_type,
                        masked_normalization_vals=self.
                        masked_normalization_values,
                        seed=self.seed)
                    return img, label, img_masked

            if self.gaze_task == "segmentation_reg":

                rle = self.rle_dict[img_id.split("/")[-1].split(".dcm")[0]]

                y_true = rle != " -1"

                if y_true:

                    # extract segmask
                    segmask_org = rle2mask(rle, 1024, 1024).T
                    segmask = resize(segmask_org, (7, 7))
                    segmask = torch.FloatTensor(segmask)
                    return img, label, segmask

                else:
                    segmask = -1 * torch.ones((7, 7))
                    return img, label, segmask

            if self.gaze_task is None:
                return img, label, 0

            return img, label, gaze_attribute

        else:

            if self.gaze_task == 'cam_error_analysis':
                return img, label, img_id

            return img, label, subclass_label
    def __getitem__(self, idx):
        img_path = self.image_info[idx]["image_path"]
        img = Image.open(img_path + '.png').convert("RGB")
        width, height = img.size
        image = img.resize((self.width, self.height), resample=Image.BILINEAR)
        info = self.image_info[idx]

        mask = rle2mask(info['annotations'], width, height)
        mask = Image.fromarray(mask.T)
        mask = mask.resize((self.width, self.height), resample=Image.BILINEAR)
        mask = np.expand_dims(mask, axis=0)

        image_id = torch.tensor([idx])

        aspect_ratio = image.size[1] / image.size[0]

        Transform = []
        ResizeRange = random.randint(300, 320)

        Transform.append(
            T.Resize((int(ResizeRange * aspect_ratio), ResizeRange)))
        p_transform = random.random()

        if (self.mode == 'train') and p_transform <= self.augmentation_prob:

            RotationDegree = random.randint(0, 3)
            RotationDegree = self.RotationDegree[RotationDegree]

            if (RotationDegree == 90) or (RotationDegree == 270):

                aspect_ratio = 1 / aspect_ratio

            Transform.append(T.RandomRotation(
                (RotationDegree, RotationDegree)))
            RotationRange = random.randint(-10, 10)
            Transform.append(T.RandomRotation((RotationRange, RotationRange)))
            CropRange = random.randint(250, 270)
            Transform.append(
                T.CenterCrop((int(CropRange * aspect_ratio), CropRange)))
            Transform = T.Compose(Transform)

            image = Transform(image)
            mask = Transform(mask)

            ShiftRange_left = random.randint(0, 20)
            ShiftRange_upper = random.randint(0, 20)
            ShiftRange_right = image.size[0] - random.randint(0, 20)
            ShiftRange_lower = image.size[1] - random.randint(0, 20)
            image = image.crop(box=(ShiftRange_left, ShiftRange_upper,
                                    ShiftRange_right, ShiftRange_lower))
            mask = mask.crop(box=(ShiftRange_left, ShiftRange_upper,
                                  ShiftRange_right, ShiftRange_lower))

            if random.random() < 0.5:

                image = F.hflip(image)

                mask = F.hflip(mask)

            if random.random() < 0.5:

                image = F.vflip(image)

                mask = F.vflip(mask)

            Transform = T.ColorJitter(brightness=0.2, contrast=0.2, hue=0.02)
            image = Transform(image)
            Transform = []

        Transform.append(
            T.Resize(
                (int(256 * aspect_ratio) - int(256 * aspect_ratio) % 16, 256)))
        Transform.append(T.ToTensor())
        Transform = T.Compose(Transform)

        image = Transform(image)

        mask = Transform(mask)
        Norm_ = T.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))

        image = Norm_(image)
        return image, mask
Ejemplo n.º 20
0
    def __getitem__(self, idx):
        # load images ad masks
        img_path = os.path.join(self.root, "train_images", self.imgs[idx])
        mask_path = os.path.join(self.root, "train_mask", self.masks[idx])
        img = cv2.imread(img_path)
        # note that we haven't converted the mask to RGB,
        # because each color corresponds to a different instance
        # with 0 being background
        #mask = Image.open(mask_path)
        with open(mask_path, 'r') as f:
            mask = json.load(f)
            f.close()

        # convert the PIL Image into a numpy array
        #mask = np.array(mask)
        # instances are encoded as different colors
        obj_ids = len(mask)
        # first id is the background, so remove it
        #obj_ids = obj_ids[1:]

        # split the color-encoded mask into a set
        # of binary masks
        masks = []
        #masks = mask == obj_ids[:, None, None]
        for i in range(0, obj_ids):
            masks.append(rle2mask(mask[i]['mask'], img.shape))
        # get bounding box coordinates for each mask
        num_objs = obj_ids
        boxes = []
        for i in range(0, num_objs):
            pos = np.where(masks[i] == 1)
            xmin = np.min(pos[1])
            xmax = np.max(pos[1])
            ymin = np.min(pos[0])
            ymax = np.max(pos[0])
            boxes.append([xmin, ymin, xmax, ymax])

        # convert everything into a torch.Tensor
        boxes = torch.as_tensor(boxes, dtype=torch.float32)
        # there is only one class
        #labels = torch.ones((num_objs,), dtype=torch.int64)
        labels = [x['id'] for x in mask]
        labels = torch.as_tensor((labels), dtype=torch.int64)
        masks = torch.as_tensor(masks, dtype=torch.uint8)

        image_id = torch.tensor([idx])
        area = (boxes[:, 3] - boxes[:, 1]) * (boxes[:, 2] - boxes[:, 0])
        # suppose all instances are not crowd
        iscrowd = torch.zeros((num_objs, ), dtype=torch.int64)

        target = {}
        target["boxes"] = boxes
        target["labels"] = labels
        target["masks"] = masks
        target["image_id"] = image_id
        target["area"] = area
        target["iscrowd"] = iscrowd

        if self.transforms is not None:
            img, target = self.transforms(img, target)

        return img, target
Ejemplo n.º 21
0
	def get_mask(self, encode, width, height):
		if encode ==[] or encode == ' -1':
			return rle2mask(' -1',width,height)
		else :
			return rle2mask(encode[0],width,height)       			# Isnt it just returning one of the mask not all.
Ejemplo n.º 22
0
    df = pd.read_csv(os.path.join(config['path_to_data'], 'train.csv'))
    df['ImageId'] = df['Image_Label'].map(lambda x: x.split("_")[0])
    df['ClassId'] = df[['Image_Label',
                        'EncodedPixels']].apply(lambda x: x[0].split("_")[1]
                                                if x[1] is not np.nan else '0',
                                                axis=1)
    df.drop(columns='Image_Label', inplace=True)
    df.drop_duplicates(inplace=True)

    # drop row with label 0 which exist label != 0
    max_class_df = df.groupby("ImageId")['ClassId'].max().reset_index()
    max_class_df = max_class_df[max_class_df['ClassId'] != '0']
    df = df[~((df['ImageId'].isin(max_class_df['ImageId'].values)) &
              (df['ClassId'] == '0'))]
    print(df.columns)
    images = df['ImageId'].unique()
    for image in tqdm(images):
        temp_df = df[df['ImageId'] == image]
        mask = np.zeros(
            (config['n_classes'], config['height'], config['width']),
            dtype=np.uint8)
        for row in temp_df.values:
            if row[0] is not np.nan:
                mask[mapping[row[2]]] = rle2mask(row[0],
                                                 shape=(config['width'],
                                                        config['height']))
        mask = np.moveaxis(mask, 0, -1)
        cv2.imwrite(
            os.path.join(config['path_to_data'], 'train_masks',
                         image.replace('.jpg', '.png')), mask)
Ejemplo n.º 23
0
def preproc(input_img, input_csv, limit=-1):
    input_glob = input_img + "*/*/*.dcm"
    train_fns = sorted(glob.glob(input_glob))[:limit]
    df_full = pd.read_csv(input_csv, index_col='ImageId')

    im_height = 1024
    im_width = 1024
    im_chan = 1
    # Get train images and masks
    X_train = np.zeros((len(train_fns), im_height, im_width, im_chan),
                       dtype=np.uint8)
    Y_train = np.zeros((len(train_fns), im_height, im_width, 1), dtype=np.bool)
    print('Getting train images and masks ... ')
    sys.stdout.flush()
    strCount, arrCount, noMaskCount, minus1Count = 0, 0, 0, 0

    # iterate every training dicom file
    for n, _id in tqdm(enumerate(train_fns), total=len(train_fns)):
        # read image from this dicom file
        dataset = pydicom.read_file(_id)
        # expand the image pixel vector into image pixel matrix
        X_train[n] = np.expand_dims(dataset.pixel_array, axis=2)
        try:
            # retrieve the encoded pixels by: df_full.loc[_id.split('/')[-1][:-4],' EncodedPixels']
            tempRle = df_full.loc[_id.split('/')[-1][:-4], ' EncodedPixels']
            if '-1' in tempRle:
                # if a -1 is marked in training rle set for this image
                # need to mark every pixel as 1
                Y_train[n] = np.zeros((1024, 1024, 1))
                minus1Count += 1
            else:
                if type(tempRle) == str:
                    Y_train[n] = np.expand_dims(rle2mask(tempRle, 1024, 1024),
                                                axis=2)
                    strCount += 1
                else:
                    Y_train[n] = np.zeros((1024, 1024, 1))
                    for x in tempRle:
                        Y_train[n] = Y_train[n] + \
                            np.expand_dims(rle2mask(x, 1024, 1024), axis=2)
                    arrCount += 1
    #                 print(_id)
        except KeyError:
            print(
                f"Key {_id.split('/')[-1][:-4]} without mask, assuming healthy patient."
            )
            # Assume missing masks are empty masks.
            Y_train[n] = np.zeros((1024, 1024, 1))
            noMaskCount += 1

    print('Done!')
    print('strCount:' + str(strCount))
    print('arrCount:' + str(arrCount))
    print('noMaskCount:' + str(noMaskCount))
    print('minus1Count:' + str(minus1Count))
    im_height = 128

    im_width = 128
    X_train = X_train.reshape((-1, im_height, im_width, 1))
    Y_train = Y_train.reshape((-1, im_height, im_width, 1))
    return X_train, Y_train
Ejemplo n.º 24
0







			






mask = ui.rle2mask(df.loc[0].EncodedPixels)
img[mask==1] = 255


fig = plt.figure(figsize=(8,50))
ax = fig.add_subplot(1,1,1)
plt.imshow(img, cmap='gray')
ax.set_xticks(np.arange(0, 1601, 64), minor=True)
ax.set_yticks(np.arange(0, 257, 64), minor=True)
ax.grid(which='minor', alpha=1)

size = 64
x = img.shape[0] // size
y = img.shape[1] // size
subImg = np.zeros((size,size))
Ejemplo n.º 25
0
                                          test_size=0.1)

    print(len(df_train), len(df_valid))

    if plot_beginning_images:
        columns = 1
        rows = 4
        fig = plt.figure(figsize=(20, columns * rows + 6))
        for i in range(1, columns * rows + 1):
            fn, class_id_str = df_train['ImageId_ClassId'].iloc[i].rsplit(
                '_', -1)
            class_id = int(class_id_str)
            fig.add_subplot(rows, columns, i).set_title(fn)
            img = cv2.imread(path + 'train_images/' + fn)
            img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
            mask = rle2mask(df_train['EncodedPixels'].iloc[i], (256, 1600))

            # different color codings for different classes
            if class_id == 1:
                img[mask == 1, 0] = 255
            elif class_id == 2:
                img[mask == 1, 1] = 255
            elif class_id == 3:
                img[mask == 1, 2] = 255
            else:
                img[mask == 1, 0:2] = 255

            plt.imshow(img)
        plt.show()

    # Define transformation(if needed augmentation can be applied here)
Ejemplo n.º 26
0
PATH = '/Users/nikita/Programming/SIIM/SIIM/train-rle.csv'  # "../../data/ibespalov/SIIM_ACR/train-rle.csv"
PATH_TO_SAVE = '/Users/nikita/Programming/SIIM/SIIM/train.csv'  # "../../data/ibespalov/SIIM_ACR/train.csv"

mask = pd.read_csv(PATH)

k = mask.groupby('ImageId').count()[" EncodedPixels"]
list_ImageId = k.index[k > 1]

for i in tqdm(range(len(list_ImageId))):
    tmp = mask.loc[mask['ImageId'] == list_ImageId[i]]
    final_mask = np.zeros((1024, 1024))
    for index, row in tmp.iterrows():
        RLE_mask = row[" EncodedPixels"]
        if RLE_mask.strip() != str(-1):
            rle_mask = rle2mask(RLE_mask[1:], 1024, 1024).T
        else:
            rle_mask = np.zeros((1024, 1024))
        final_mask[np.where(rle_mask)] = 1
    final_rle = mask_to_rle(final_mask.T, 1024, 1024)

    new_csv['ImageId'].append(list_ImageId[i])
    new_csv[' EncodedPixels'].append(final_rle)

k = mask.groupby('ImageId').count()[" EncodedPixels"]
list_ImageId = k.index[k == 1]

for i in tqdm(range(len(list_ImageId))):
    new_csv['ImageId'].append(list_ImageId[i])
    new_csv[' EncodedPixels'].append(mask.loc[
        mask['ImageId'] == list_ImageId[i]][" EncodedPixels"].values[0])
Ejemplo n.º 27
0
'''
(height, width)
'''
original_size = (1400, 2100)
new_size = (350, 525)  # ResNet
interpolation = cv2.INTER_CUBIC

df = pd.read_csv(os.path.join(dataset_dir, 'train.csv'))

print('update train.csv ...')

# masking 영역을 ratio 맞게 resize 하기
for idx, row in tqdm(df.iterrows()):
    encoded_pixels = row[1]
    if encoded_pixels is not np.nan:
        mask = rle2mask(encoded_pixels, shape=original_size[::-1])
        mask = cv2.resize(mask, new_size[::-1], interpolation=interpolation)

        rle = mask2rle(mask)
        df.at[idx, 'EncodedPixels'] = rle

df.to_csv(os.path.join(dataset_dir, '350_525_train.csv'), index=False)


# Resizing Train and Test Images

train_images_dir = os.path.join(dataset_dir, 'train_images')
train_image_files = os.listdir(train_images_dir)

test_images_dir = os.path.join(dataset_dir, 'test_images')
test_image_files = os.listdir(test_images_dir)