-
Notifications
You must be signed in to change notification settings - Fork 0
/
datautils.py
158 lines (130 loc) · 5.17 KB
/
datautils.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
'''
This modules changes the Zenodo data file to an annotation file (name vs label)
for using to make the Pytorch dataset.
'''
import os
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
#import keras_ocr
import cv2
import math
import torch
from PIL import Image
from torchvision.io import read_image, ImageReadMode
from torch.utils.data import Dataset, DataLoader, random_split
from torchvision import datasets
from torchvision.transforms import ToTensor, Resize
os.environ['KMP_DUPLICATE_LIB_OK'] = 'True'
'''
Credits for Inpaiting section:
Carlo Borella at https://towardsdatascience.com/remove-text-from-images-using-cv2-and-keras-ocr-24e7612ae4f4
'''
# Inpaiting to remove text from images
def midpoint(x1, y1, x2, y2):
x_mid = int((x1 + x2)/2)
y_mid = int((y1 + y2)/2)
return (x_mid, y_mid)
'''
def inpaint_text(img_path, pipeline):
# read image
img = keras_ocr.tools.read(img_path)
# generate (word, box) tuples
prediction_groups = pipeline.recognize([img])
mask = np.zeros(img.shape[:2], dtype="uint8")
for box in prediction_groups[0]:
x0, y0 = box[1][0]
x1, y1 = box[1][1]
x2, y2 = box[1][2]
x3, y3 = box[1][3]
x_mid0, y_mid0 = midpoint(x1, y1, x2, y2)
x_mid1, y_mi1 = midpoint(x0, y0, x3, y3)
thickness = int(math.sqrt( (x2 - x1)**2 + (y2 - y1)**2 ))
cv2.line(mask, (x_mid0, y_mid0), (x_mid1, y_mi1), 255,
thickness)
img = cv2.inpaint(img, mask, 7, cv2.INPAINT_NS)
return(img)
'''
# This function generates a meshgrid based on the size of a square image
def get_mgrid(sidelen, dim=2):
tensors = tuple(dim * [torch.linspace(0, 1, steps=sidelen)])
mgrid = torch.stack(torch.meshgrid(*tensors, indexing='ij'), dim=-1)
mgrid = mgrid.reshape(-1, dim)
return mgrid
# A function to resize all the images in a directory prior to the training process
# This makes the training much faster if we are to deal with a fixed set of images
def resize_images(path, dest, size = (224,224)):
'''
path: images directory
dest: destination folder
size: final size of the images
'''
dirs = os.listdir(path)
for ii, item in enumerate(dirs):
image = np.asarray(Image.open(path + item).convert("L"))
print(f'Image {ii+1} shape: {image.shape}')
imResize = cv2.resize(image, size)
cv2.imwrite(dest + item, imResize)
print(f'Image {ii+1} Done!')
# A torch dataset for working with normal US images
class US_Dataset(Dataset):
def __init__(self, annotations_file, img_dir, transform=None, target_transform=None, device='cpu'):
self.img_labels = pd.read_csv(annotations_file, sep=';')
self.img_dir = img_dir
self.transform = transform
self.target_transform = target_transform
self.device = device
def __len__(self):
return len(self.img_labels)
def __getitem__(self, idx):
img_path = os.path.join(self.img_dir, self.img_labels.iloc[idx, 0])
image = read_image(img_path, mode=ImageReadMode.GRAY).to(self.device) / 255
label = torch.tensor(self.img_labels.iloc[idx, 1]).to(self.device)
if self.transform:
image = self.transform(image)
if self.target_transform:
label = self.target_transform(label)
return image, label
# Torch dataset for working with INRs
class INR_Dataset(Dataset):
def __init__(self, data_table, img_dir, size = 224, device='cpu'):
super().__init__()
self.data = data_table
self.img_dir = img_dir
#self.transform = transform # in case of other transforms
self.size = size
self.device = device
def __len__(self):
return len(self.data)
def __getitem__(self, idx):
img_path = self.img_dir + self.data.iloc[idx, 0]
image = read_image(img_path, mode=ImageReadMode.GRAY).to(self.device) / 255
transform = Resize((self.size, self.size), antialias=True)
image = transform(image)
pixels = image.permute(1, 2, 0).view(-1, 1)
coords = get_mgrid(self.size, 2).to(self.device)
return coords, pixels, image
if __name__ == "__main__":
import pandas as pd
labels = {
'Fetal abdomen': 0,
'Trans-thalamic': 1,
'Trans-cerebellum': 2,
'Trans-ventricular': 3,
'Fetal femur': 4,
'Fetal thorax': 5,
'Maternal cervix': 6,
'Other': 7
}
data_table = pd.read_csv('Data/FETAL_PLANES_DB_data.csv', sep=';')
brain_dropped = data_table[data_table['Plane'] != 'Fetal brain'].loc[:, ['Image_name', 'Plane']]
brain_data = data_table[data_table['Plane'] == 'Fetal brain'].loc[:, ['Image_name', 'Brain_plane']]
brain_data = brain_data.set_axis(['Image_name', 'Plane'], axis=1)
annotations = pd.concat([brain_dropped, brain_data], ignore_index=True)
annotations = annotations.replace(to_replace=labels)
annotations['Image_name'] += '.png'
#print (brain_dropped)
#print (brain_data)
#print (pd.concat([brain_dropped, brain_data], ignore_index=True))
#print (annotations)
annotations.to_csv(path_or_buf='Data/annotations.csv', sep=';', index=False)