Upload data_processing.py
Browse files- data_processing.py +107 -0
data_processing.py
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torchvision
|
| 2 |
+
import torch
|
| 3 |
+
from torch.utils.data import Dataset
|
| 4 |
+
from torch.nn.utils.rnn import pad_sequence
|
| 5 |
+
|
| 6 |
+
class AddGaussianNoise(object):
|
| 7 |
+
def __init__(self, mean=0., std=1., thresh=0.2):
|
| 8 |
+
self.mean = mean
|
| 9 |
+
self.std = std
|
| 10 |
+
self.thresh = thresh
|
| 11 |
+
|
| 12 |
+
def __call__(self, tensor):
|
| 13 |
+
noise = torch.zeros_like(tensor)
|
| 14 |
+
noise[tensor>self.thresh] = 1
|
| 15 |
+
noise *= torch.randn(tensor.size()) * self.std + self.mean
|
| 16 |
+
return tensor + noise
|
| 17 |
+
|
| 18 |
+
def __repr__(self):
|
| 19 |
+
return self.__class__.__name__ + f'(mean={self.mean}, std={self.std})'
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
class TextProcessor:
|
| 23 |
+
def __init__(self, alphabet):
|
| 24 |
+
self.alphabet = alphabet
|
| 25 |
+
self.pad_token = "[PAD]"
|
| 26 |
+
self.stoi = {s: i for i, s in enumerate(self.alphabet,1)}
|
| 27 |
+
self.stoi[self.pad_token] = 0
|
| 28 |
+
self.itos = {i: s for s, i in self.stoi.items()}
|
| 29 |
+
|
| 30 |
+
def encode(self, label):
|
| 31 |
+
return [self.stoi[s] for s in label]
|
| 32 |
+
|
| 33 |
+
def decode(self, ids):
|
| 34 |
+
return ''.join([self.itos[i] for i in ids])
|
| 35 |
+
|
| 36 |
+
def __len__(self):
|
| 37 |
+
return len(self.alphabet) + 1
|
| 38 |
+
|
| 39 |
+
transform_train = torchvision.transforms.Compose(
|
| 40 |
+
[
|
| 41 |
+
torchvision.transforms.Grayscale(),
|
| 42 |
+
torchvision.transforms.ToTensor(),
|
| 43 |
+
torchvision.transforms.RandomApply([
|
| 44 |
+
torchvision.transforms.RandomAdjustSharpness(sharpness_factor=80),
|
| 45 |
+
AddGaussianNoise(mean=1, std=0.005, thresh=0.3),
|
| 46 |
+
])
|
| 47 |
+
]
|
| 48 |
+
)
|
| 49 |
+
|
| 50 |
+
transform_eval = torchvision.transforms.Compose(
|
| 51 |
+
[
|
| 52 |
+
torchvision.transforms.Grayscale(),
|
| 53 |
+
torchvision.transforms.ToTensor()
|
| 54 |
+
]
|
| 55 |
+
)
|
| 56 |
+
|
| 57 |
+
class CRNNDataset(Dataset):
|
| 58 |
+
def __init__(
|
| 59 |
+
self,
|
| 60 |
+
height,
|
| 61 |
+
text_processor:TextProcessor,
|
| 62 |
+
transforms:torchvision.transforms,
|
| 63 |
+
dataset=None
|
| 64 |
+
) -> None:
|
| 65 |
+
super().__init__()
|
| 66 |
+
|
| 67 |
+
self.height = height
|
| 68 |
+
self.transform = transforms
|
| 69 |
+
self.dataset = dataset
|
| 70 |
+
|
| 71 |
+
self.text_processor = text_processor
|
| 72 |
+
|
| 73 |
+
def __len__(self):
|
| 74 |
+
return len(self.dataset)
|
| 75 |
+
|
| 76 |
+
def __getitem__(self, idx):
|
| 77 |
+
dset = self.dataset[idx]
|
| 78 |
+
image, text = dset['image'], dset['text']
|
| 79 |
+
label = torch.tensor(self.text_processor.encode(text), dtype=torch.long)
|
| 80 |
+
original_width, original_height = image.size
|
| 81 |
+
new_width = int(self.height * original_width / original_height) # Calculate width to preserve aspect ratio
|
| 82 |
+
image = image.resize((new_width, self.height))
|
| 83 |
+
image = self.transform(image)
|
| 84 |
+
return image, label
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
def collate_fn(batch):
|
| 88 |
+
images, labels = zip(*batch)
|
| 89 |
+
|
| 90 |
+
max_h = max(img.size(1) for img in images)
|
| 91 |
+
max_w = max(img.size(2) for img in images)
|
| 92 |
+
|
| 93 |
+
padded_images = []
|
| 94 |
+
|
| 95 |
+
for img in images:
|
| 96 |
+
h, w = img.size(1), img.size(2)
|
| 97 |
+
padding = (0, max_w - w, 0, max_h - h) # left, right, top, bottom
|
| 98 |
+
padded_img = torch.nn.functional.pad(img, padding, mode='constant', value=0)
|
| 99 |
+
padded_images.append(padded_img)
|
| 100 |
+
|
| 101 |
+
images = torch.stack(padded_images, 0)
|
| 102 |
+
|
| 103 |
+
target_lengths = torch.tensor([len(label) for label in labels]).long()
|
| 104 |
+
|
| 105 |
+
labels = pad_sequence(labels, batch_first=True, padding_value=0)
|
| 106 |
+
|
| 107 |
+
return images, labels, target_lengths
|