Skip to content

Commit

Permalink
Cleanup of PR #555 merge
Browse files Browse the repository at this point in the history
  • Loading branch information
mittagessen committed Mar 26, 2024
1 parent 761a2bf commit 5af9c8c
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 18 deletions.
2 changes: 1 addition & 1 deletion kraken/lib/dataset/recognition.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ def __init__(self,
reorder: Union[bool, Literal['L', 'R']] = True,
im_transforms: Callable[[Any], torch.Tensor] = transforms.Compose([]),
augmentation: bool = False,
legacy_polygons: bool=False) -> None:
legacy_polygons: bool = False) -> None:
"""
Creates a dataset for a polygonal (baseline) transcription model.
Expand Down
1 change: 0 additions & 1 deletion kraken/lib/dataset/segmentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,6 @@ def __getitem__(self, idx):
im, target = self.transform(im, target)
return {'image': im, 'target': target}
except Exception:
raise
self.failed_samples.add(idx)
idx = np.random.randint(0, len(self.imgs))
logger.debug(traceback.format_exc())
Expand Down
7 changes: 4 additions & 3 deletions kraken/lib/pretrain/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,9 +212,10 @@ def __init__(self,
if format_type == 'binary':
legacy_train_status = train_set.legacy_polygons_status
if val_set and val_set.legacy_polygons_status != legacy_train_status:
logger.warning(
f'Train and validation set have different legacy polygon status: {legacy_train_status} and {val_set.legacy_polygons_status}.'
'Train set status prevails.')
logger.warning('Train and validation set have different legacy '
f'polygon status: {legacy_train_status} and '
f'{val_set.legacy_polygons_status}. Train set '
'status prevails.')
if legacy_train_status == "mixed":
logger.warning('Mixed legacy polygon status in training dataset. Consider recompilation.')
legacy_train_status = False
Expand Down
2 changes: 1 addition & 1 deletion kraken/lib/pretrain/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ def arrange(s, e, length, keep_length):
for length in sorted(lengths, reverse=True):
lens = np.fromiter(
(e - s if e - s >= length + mask_min_space else 0 for s, e in parts),
np.int,
int,
)
l_sum = np.sum(lens)
if l_sum == 0:
Expand Down
7 changes: 4 additions & 3 deletions kraken/lib/segmentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import torch
import torch.nn.functional as F
from PIL import Image, ImageDraw
from PIL.Image import Resampling, Transform
from scipy.ndimage import (binary_erosion, distance_transform_cdt,
gaussian_filter, maximum_filter, affine_transform)
from scipy.signal import convolve2d
Expand Down Expand Up @@ -419,8 +420,8 @@ def _rotate(image: _T_pil_or_np,
if isinstance(image, Image.Image):
# PIL is much faster than scipy
pdata = tform.params.flatten().tolist()[:6]
resample = {0: Image.NEAREST, 1: Image.BILINEAR, 2: Image.BICUBIC, 3: Image.BICUBIC}.get(order, Image.NEAREST)
return tform, image.transform(output_shape[::-1], Image.AFFINE, data=pdata, resample=resample, fillcolor=cval)
resample = {0: Resampling.NEAREST, 1: Resampling.BILINEAR, 2: Resampling.BICUBIC, 3: Resampling.BICUBIC}.get(order, Resampling.NEAREST)
return tform, image.transform(output_shape[::-1], Transform.AFFINE, data=pdata, resample=resample, fillcolor=cval)

# params for scipy
# swap X and Y axis for scipy
Expand Down Expand Up @@ -1332,7 +1333,7 @@ def extract_polygons(im: Image.Image,
for i in range(0, len(source_envelope)-3, 2)
]
# warp
resample = {0: Image.NEAREST, 1: Image.BILINEAR, 2: Image.BICUBIC, 3: Image.BICUBIC}.get(order, Image.NEAREST)
resample = {0: Resampling.NEAREST, 1: Resampling.BILINEAR, 2: Resampling.BICUBIC, 3: Resampling.BICUBIC}.get(order, Resampling.NEAREST)
i = patch.transform((output_shape[1], output_shape[0]), Image.MESH, data=deform_mesh, resample=resample)

yield i.crop(i.getbbox()), line
Expand Down
7 changes: 4 additions & 3 deletions kraken/lib/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -406,9 +406,10 @@ def __init__(self,
if format_type == 'binary':
legacy_train_status = train_set.legacy_polygons_status
if val_set and val_set.legacy_polygons_status != legacy_train_status:
logger.warning(
f'Train and validation set have different legacy polygon status: {legacy_train_status} and {val_set.legacy_polygons_status}.'
'Train set status prevails.')
logger.warning('Train and validation set have different legacy '
f'polygon status: {legacy_train_status} and '
f'{val_set.legacy_polygons_status}. Train set '
'status prevails.')
if legacy_train_status == "mixed":
logger.warning('Mixed legacy polygon status in training dataset. Consider recompilation.')
legacy_train_status = False
Expand Down
6 changes: 3 additions & 3 deletions kraken/lib/vgsl.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ def __init__(self, spec: str) -> None:
'one_channel_mode': None,
'model_type': None,
'hyper_params': {},
'legacy_polygons': False} # enable new polygons by default on new models
'legacy_polygons': False} # enable new polygons by default on new models
self._aux_layers = nn.ModuleDict()

self.idx = -1
Expand Down Expand Up @@ -313,7 +313,7 @@ def _deserialize_layers(name, layer):
'one_channel_mode': '1',
'model_type': None,
'hyper_params': {},
'legacy_polygons': True} # disable new polygons by default on load
'legacy_polygons': True} # disable new polygons by default on load

if 'kraken_meta' in mlmodel.user_defined_metadata:
nn.user_metadata.update(json.loads(mlmodel.user_defined_metadata['kraken_meta']))
Expand Down Expand Up @@ -368,7 +368,7 @@ def aux_layers(self, val: Dict[str, torch.nn.Module]):
@property
def use_legacy_polygons(self):
return self.user_metadata.get('legacy_polygons', True)

@use_legacy_polygons.setter
def use_legacy_polygons(self, val: bool):
self.user_metadata['legacy_polygons'] = val
Expand Down
6 changes: 3 additions & 3 deletions tests/test_newpolygons.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import tempfile
from unittest.mock import Mock, patch
from pathlib import Path
from traceback import print_exception
from traceback import print_exc
import warnings
from typing import Optional, List, Union

Expand Down Expand Up @@ -89,7 +89,7 @@ def _test_krakencli(self, extractor_mock: Mock, *, args, force_no_legacy: bool=F
print("kraken", *args)

if result.exception:
print_exception(result.exception)
print_exc()

self.assertEqual(result.exit_code, 0)
extractor_mock.assert_called()
Expand All @@ -106,7 +106,7 @@ def _test_ketoscli(self, *, args, expect_legacy: bool, check_exit_code: Optional
print("ketos", *args)
if result.exception:
print(result.output)
print_exception(result.exception)
print_exc()

if check_exit_code is not None:
if isinstance(check_exit_code, int):
Expand Down

0 comments on commit 5af9c8c

Please sign in to comment.