def __getitem__(self, i): if self.mode is 'train' or self.mode is 'val': image_id, mask, border = self.data.iloc[i] # im_path = os.path.join(TEST_PATH, image_id) # image = cv2.imread(im_path) image = self.images[image_id] mask = rle_decode(mask) image, mask = self.apply_aug(image, mask) image = torch.from_numpy(image).float().permute([2, 0, 1]) mask = torch.from_numpy(mask).float().unsqueeze(2).permute([2, 0, 1]) has_ship = torch.sum(mask) > 0 return image, mask, has_ship elif self.mode is 'test': image_id = self.images[i] im_path = os.path.join(TEST_PATH, image_id) image = cv2.imread(im_path) image, _ = self.apply_aug(image) image = torch.from_numpy(image).float().permute([2, 0, 1]) return (image, image_id) else: raise RuntimeError()
erosion = cv2.erode(mask, kernel, iterations=1) return mask - erosion print("Started preprocessing") df = pd.read_csv('data/prediction.csv') df['isempty'] = df.apply(lambda x: 1 if x.EncodedPixels is np.nan else 0, axis=1) ship_counts = df.groupby('ImageId').count()[['EncodedPixels']].rename( {'EncodedPixels': 'ships'}, axis=1) df = df.merge(ship_counts, right_index=True, left_on='ImageId') df['msize'] = df[df.isempty == 0].apply( lambda x: np.sum(rle_decode(x.EncodedPixels)), axis=1) groupz = df.groupby('ImageId').groups k = [(group, df.iloc[groupz[group]].EncodedPixels.values) for group in tqdm.tqdm(groupz)] kk = [(x[0], combine_masks(x[1])) for x in tqdm.tqdm(k)] final = pd.DataFrame(kk) border_mask = final.iloc[:, 1].apply( lambda x: rle_encode(create_border_mask(rle_decode(x)))) final['border'] = border_mask final.columns = ['ImageId', 'mask_nb', 'border'] final = df[['ImageId', 'ships']].merge(final, left_on='ImageId', right_on='ImageId') final.to_csv("data/prediction_p.csv", index=False)
filenames = os.listdir(args.path) dataframes = [pd.read_csv(os.path.join(args.path, name)) for name in filenames] if threshold is None: threshold = len(dataframes) // 2 + 1 test_samples = len(dataframes[0]) print(f"voting threshold = {threshold}") pred_distr = {-1: 0, 0: 0, 1: 0, 2: 0, 3: 0} encoded_pixels = [] encoded_pixels_ch = [] print("processing submissions") for i in tqdm(range(test_samples)): mask = np.zeros((350, 525)) for j in range(len(dataframes)): if dataframes[j].iloc[i]["EncodedPixels"] is not np.nan: mask += rle_decode(dataframes[j].iloc[i]["EncodedPixels"]) mask_final = np.where(mask >= threshold, 1, 0) if mask_final.sum() == 0: pred_distr[-1] += 1 encoded_pixels.append("") else: pred_distr[i % 4] += 1 r = mask2rle(mask_final) encoded_pixels.append(r) print( f"empty={pred_distr[-1]} fish={pred_distr[0]} flower={pred_distr[1]} gravel={pred_distr[2]} sugar={pred_distr[3]}" ) non_empty = pred_distr[0] + pred_distr[1] + pred_distr[2] + pred_distr[3] total = non_empty + pred_distr[-1] print(f"% of non-empty masks {round(non_empty / total, 4)}")
# Initialize mode and load trained weights ckpt_path = "../output/weights/model_96_resnet18f2_0.pth" device = torch.device("cuda") model = smp.Unet(args.model, classes=4, encoder_weights='imagenet', activation=None) salt = model.to(device) model.eval() state = torch.load(ckpt_path, map_location=lambda storage, loc: storage) model.load_state_dict(state["state_dict"]) # start prediction predictions = [] for i, batch in enumerate(tqdm(test_loader)): fnames, images = batch batch_preds = torch.sigmoid(model(images.to(device))) batch_preds = batch_preds.detach().cpu().numpy() for fname, preds in zip(fnames, batch_preds): for cls, pred in enumerate(preds): pred, num = post_process(pred, best_threshold, min_size) rle = rle_decode(pred) name = fname + f"_{cls+1}" predictions.append([name, rle]) # save predictions to submission.csv df = pd.DataFrame(predictions, columns=['ImageId_ClassId', 'EncodedPixels']) df.to_csv("../output/submissions/submission.csv", index=False)
import pandas as pd import matplotlib.pyplot as plt import numpy as np from skimage.measure import label from PIL import Image import cv2 import torch from torch.utils import data from src.utils import rle_decode print("Started preprocessing") df = pd.read_csv('data/train_ship_segmentations_v2.csv') df['isempty'] = df.apply(lambda x: 1 if x.EncodedPixels is np.nan else 0, axis=1) ship_counts = df.groupby('ImageId').count()[['EncodedPixels']].rename( {'EncodedPixels': 'ships'}, axis=1) df = df.merge(ship_counts, right_index=True, left_on='ImageId') df['msize'] = df[df.isempty == 0].apply( lambda x: np.sum(rle_decode(x.EncodedPixels)), axis=1) df.to_csv('data/processed.csv', index=False) print("Finished. Save to data/processed.csv")