def setup(self, stage: str = None) -> None: if stage == "fit" or stage is None: with h5py.File(PROCESSED_DATA_FILENAME, "r") as f: self.x_trainval = f["x_train"][:] self.y_trainval = f["y_train"][:].squeeze().astype(int) data_trainval = BaseDataset(self.x_trainval, self.y_trainval, transform=self.transform) self.data_train, self.data_val = split_dataset(base_dataset=data_trainval, fraction=TRAIN_FRAC, seed=42) if stage == "test" or stage is None: with h5py.File(PROCESSED_DATA_FILENAME, "r") as f: self.x_test = f["x_test"][:] self.y_test = f["y_test"][:].squeeze().astype(int) self.data_test = BaseDataset(self.x_test, self.y_test, transform=self.transform)
def setup(self, stage: str = None) -> None: print("EMNISTLinesDataset loading data from HDF5...") if stage == "fit" or stage is None: with h5py.File(self.data_filename, "r") as f: x_train = f["x_train"][:] y_train = f["y_train"][:].astype(int) x_val = f["x_val"][:] y_val = f["y_val"][:].astype(int) self.data_train = BaseDataset(x_train, y_train, transform=self.transform) self.data_val = BaseDataset(x_val, y_val, transform=self.transform) if stage == "test" or stage is None: with h5py.File(self.data_filename, "r") as f: x_test = f["x_test"][:] y_test = f["y_test"][:].astype(int) self.data_test = BaseDataset(x_test, y_test, transform=self.transform)
def setup(self, stage: str = None) -> None: print("EMNISTLines2 loading data from HDF5...") if stage == "fit" or stage is None: with h5py.File(self.data_filename, "r") as f: x_train = f["x_train"][:] y_train = torch.LongTensor(f["y_train"][:]) x_val = f["x_val"][:] y_val = torch.LongTensor(f["y_val"][:]) self.data_train = BaseDataset(x_train, y_train, transform=get_transform(augment=self.augment)) self.data_val = BaseDataset(x_val, y_val, transform=get_transform(augment=self.augment)) if stage == "test" or stage is None: with h5py.File(self.data_filename, "r") as f: x_test = f["x_test"][:] y_test = torch.LongTensor(f["y_test"][:]) self.data_test = BaseDataset(x_test, y_test, transform=get_transform(augment=False))
def setup(self, stage: str = None) -> None: print(f"IAMSyntheticParagraphs.setup({stage}): Loading trainval IAM paragraph regions and lines...") if stage == "fit" or stage is None: line_crops, line_labels = load_line_crops_and_labels("trainval", PROCESSED_DATA_DIRNAME) X, para_labels = generate_synthetic_paragraphs(line_crops=line_crops, line_labels=line_labels) Y = convert_strings_to_labels(strings=para_labels, mapping=self.inverse_mapping, length=self.output_dims[0]) transform = get_transform(image_shape=self.dims[1:], augment=self.augment) # type: ignore self.data_train = BaseDataset(X, Y, transform=transform)
def _load_dataset(split: str, augment: bool) -> BaseDataset: crops, labels = load_processed_crops_and_labels(split) X = [resize_image(crop, IMAGE_SCALE_FACTOR) for crop in crops] Y = convert_strings_to_labels(strings=labels, mapping=self.inverse_mapping, length=self.output_dims[0]) transform = get_transform(image_shape=self.dims[1:], augment=augment) # type: ignore return BaseDataset(X, Y, transform=transform)
def setup(self, stage: str = None) -> None: with open(PROCESSED_DATA_DIRNAME / "_max_aspect_ratio.txt") as file: max_aspect_ratio = float(file.read()) image_width = int(IMAGE_HEIGHT * max_aspect_ratio) assert image_width <= IMAGE_WIDTH if stage == "fit" or stage is None: x_trainval, labels_trainval = load_line_crops_and_labels( "trainval", PROCESSED_DATA_DIRNAME) assert self.output_dims[0] >= max( [len(_) for _ in labels_trainval]) + 2 # Add 2 for start/end tokens. y_trainval = convert_strings_to_labels(labels_trainval, self.inverse_mapping, length=self.output_dims[0]) data_trainval = BaseDataset(x_trainval, y_trainval, transform=get_transform( IMAGE_WIDTH, self.augment)) self.data_train, self.data_val = split_dataset( base_dataset=data_trainval, fraction=TRAIN_FRAC, seed=42) # Note that test data does not go through augmentation transforms if stage == "test" or stage is None: x_test, labels_test = load_line_crops_and_labels( "test", PROCESSED_DATA_DIRNAME) assert self.output_dims[0] >= max([len(_) for _ in labels_test]) + 2 y_test = convert_strings_to_labels(labels_test, self.inverse_mapping, length=self.output_dims[0]) self.data_test = BaseDataset(x_test, y_test, transform=get_transform(IMAGE_WIDTH)) if stage is None: self._verify_output_dims(labels_trainval=labels_trainval, labels_test=labels_test)
def setup(self, stage: str = None): with open(PROCESSED_DATA_DIRNAME / '_max_aspect_ratio.txt') as file: max_aspect_ratio = float(file.read()) image_width = int(IMAGE_HEIGHT * max_aspect_ratio) assert image_width <= IMAGE_WIDTH with open(PROCESSED_DATA_DIRNAME / 'trainval' / '_labels.json') as file: labels_trainval = json.load(file) with open(PROCESSED_DATA_DIRNAME / 'test' / '_labels.json') as file: labels_test = json.load(file) max_label_length = max([len(label) for label in labels_trainval + labels_test]) + 2 # Add 2 because of start and end tokens. output_dims = (max_label_length, 1) if output_dims != self.output_dims: raise RuntimeError(dims, output_dims) if stage == "fit" or stage is None: filenames_trainval = sorted( (PROCESSED_DATA_DIRNAME / 'trainval').glob('*.png'), key=lambda filename: int(Path(filename).stem) ) x_trainval = [Image.open(filename) for filename in filenames_trainval] y_trainval = convert_strings_to_labels(labels_trainval, self.inverse_mapping, length=self.output_dims[0]) data_trainval = BaseDataset(x_trainval, y_trainval, transform=get_transform(IMAGE_WIDTH, self.augment)) train_size = int(TRAIN_FRAC * len(data_trainval)) val_size = len(data_trainval) - train_size self.data_train, self.data_val = torch.utils.data.random_split( data_trainval, [train_size, val_size], generator=torch.Generator().manual_seed(42) ) # Note that test data does not go through augmentation transforms if stage == "test" or stage is None: filenames_test = sorted( (PROCESSED_DATA_DIRNAME / 'test').glob('*.png'), key=lambda filename: int(Path(filename).stem) ) x_test = [Image.open(filename) for filename in filenames_test] y_test = convert_strings_to_labels(labels_test, self.inverse_mapping, length=self.output_dims[0]) self.data_test = BaseDataset(x_test, y_test, transform=get_transform(IMAGE_WIDTH))
def setup(self, stage: str = None): if stage == "fit" or stage is None: with h5py.File(PROCESSED_DATA_FILENAME, "r") as f: self.x_trainval = f["x_train"][:] self.y_trainval = f["y_train"][:].squeeze().astype(int) data_trainval = BaseDataset(self.x_trainval, self.y_trainval, transform=self.transform) train_size = int(TRAIN_FRAC * len(data_trainval)) val_size = len(data_trainval) - train_size self.data_train, self.data_val = torch.utils.data.random_split( data_trainval, [train_size, val_size], generator=torch.Generator().manual_seed(42)) if stage == "test" or stage is None: with h5py.File(PROCESSED_DATA_FILENAME, "r") as f: self.x_test = f["x_test"][:] self.y_test = f["y_test"][:].squeeze().astype(int) self.data_test = BaseDataset(self.x_test, self.y_test, transform=self.transform)