-
Notifications
You must be signed in to change notification settings - Fork 0
/
dl.py
71 lines (53 loc) · 2.34 KB
/
dl.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
'''
Dataloader for binary image classification dataset
'''
import os
import torch
import math
from torch.utils.data import Dataset
import torchvision.transforms as transforms
import torch.nn.functional as F
import numpy as np
from PIL import Image, ImageFile
ImageFile.LOAD_TRUNCATED_IMAGES = True
class BinaryDataset(Dataset):
"""
Dataset builder for binary image classification dataset.
dir: path to directory containing images
df: dataframe of csv (contains image id's and masks)
"""
def __init__(self, dir, df, transform=None):
self.dir = dir
self.df = df[df.ImageId != "63849d9ce.jpg"] # remove corrupt image from dataframe - specific to airbus dataset
self.transform = transform
# extract binary labels
self.labels = self.df["Class"].to_numpy()
def __len__(self):
return len(self.df["ImageId"])
def __getitem__(self, idx):
# load in image
img = Image.open(self.dir + "/" + self.df["ImageId"].iloc[idx], mode='r')
# apply image transformation
if self.transform is not None:
img = self.transform(img)
# return image and label
return img, torch.tensor(self.labels[idx]).long()
# preprocessing specific to airbus ship detection dataset
def classif_imgs_from_masks(masks_orig, dir):
masks = masks_orig
masks['ships'] = masks['EncodedPixels'].map(lambda c_row: 1 if isinstance(c_row, str) else 0)
unique_img_ids = masks.groupby('ImageId').agg({'ships': 'sum'}).reset_index()
unique_img_ids['Class'] = unique_img_ids['ships'].map(lambda x: 1.0 if x > 0 else 0.0)
# remove too small files
unique_img_ids['file_size_kb'] = unique_img_ids['ImageId'].map(lambda c_img_id:
os.stat(
os.path.join(dir,
c_img_id)).st_size / 1024)
size_thresh = 50 # kb
corrupt = 0
for i in range(len(unique_img_ids)):
if unique_img_ids['file_size_kb'][i] <= size_thresh:
corrupt += 1
unique_img_ids = unique_img_ids[unique_img_ids['file_size_kb'] > size_thresh]
masks.drop(['ships'], axis=1, inplace=True)
return masks, unique_img_ids, corrupt