doctr-synth-generator 0.3.0__tar.gz → 0.3.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. {doctr_synth_generator-0.3.0 → doctr_synth_generator-0.3.1}/PKG-INFO +28 -27
  2. {doctr_synth_generator-0.3.0 → doctr_synth_generator-0.3.1}/README.md +27 -26
  3. {doctr_synth_generator-0.3.0 → doctr_synth_generator-0.3.1}/doctr_synth_generator.egg-info/PKG-INFO +28 -27
  4. {doctr_synth_generator-0.3.0 → doctr_synth_generator-0.3.1}/generator/dataset_generator.py +11 -0
  5. {doctr_synth_generator-0.3.0 → doctr_synth_generator-0.3.1}/generator/doctr_dataset.py +71 -42
  6. doctr_synth_generator-0.3.1/generator/version.py +1 -0
  7. {doctr_synth_generator-0.3.0 → doctr_synth_generator-0.3.1}/scripts/generate_grid.py +6 -1
  8. {doctr_synth_generator-0.3.0 → doctr_synth_generator-0.3.1}/setup.py +1 -1
  9. doctr_synth_generator-0.3.0/generator/version.py +0 -1
  10. {doctr_synth_generator-0.3.0 → doctr_synth_generator-0.3.1}/LICENSE +0 -0
  11. {doctr_synth_generator-0.3.0 → doctr_synth_generator-0.3.1}/doctr_synth_generator.egg-info/SOURCES.txt +0 -0
  12. {doctr_synth_generator-0.3.0 → doctr_synth_generator-0.3.1}/doctr_synth_generator.egg-info/dependency_links.txt +0 -0
  13. {doctr_synth_generator-0.3.0 → doctr_synth_generator-0.3.1}/doctr_synth_generator.egg-info/entry_points.txt +0 -0
  14. {doctr_synth_generator-0.3.0 → doctr_synth_generator-0.3.1}/doctr_synth_generator.egg-info/requires.txt +0 -0
  15. {doctr_synth_generator-0.3.0 → doctr_synth_generator-0.3.1}/doctr_synth_generator.egg-info/top_level.txt +0 -0
  16. {doctr_synth_generator-0.3.0 → doctr_synth_generator-0.3.1}/doctr_synth_generator.egg-info/zip-safe +0 -0
  17. {doctr_synth_generator-0.3.0 → doctr_synth_generator-0.3.1}/generator/__init__.py +0 -0
  18. {doctr_synth_generator-0.3.0 → doctr_synth_generator-0.3.1}/generator/__main__.py +0 -0
  19. {doctr_synth_generator-0.3.0 → doctr_synth_generator-0.3.1}/generator/api.py +0 -0
  20. {doctr_synth_generator-0.3.0 → doctr_synth_generator-0.3.1}/generator/augmentations/__init__.py +0 -0
  21. {doctr_synth_generator-0.3.0 → doctr_synth_generator-0.3.1}/generator/augmentations/augmentation_pipeline.py +0 -0
  22. {doctr_synth_generator-0.3.0 → doctr_synth_generator-0.3.1}/generator/augmentations/random_blur.py +0 -0
  23. {doctr_synth_generator-0.3.0 → doctr_synth_generator-0.3.1}/generator/augmentations/random_gaussian_noise.py +0 -0
  24. {doctr_synth_generator-0.3.0 → doctr_synth_generator-0.3.1}/generator/augmentations/random_jpeg_compression.py +0 -0
  25. {doctr_synth_generator-0.3.0 → doctr_synth_generator-0.3.1}/generator/augmentations/random_perspective.py +0 -0
  26. {doctr_synth_generator-0.3.0 → doctr_synth_generator-0.3.1}/generator/augmentations/random_pixel_dropout.py +0 -0
  27. {doctr_synth_generator-0.3.0 → doctr_synth_generator-0.3.1}/generator/augmentations/random_rotate.py +0 -0
  28. {doctr_synth_generator-0.3.0 → doctr_synth_generator-0.3.1}/generator/components/__init__.py +0 -0
  29. {doctr_synth_generator-0.3.0 → doctr_synth_generator-0.3.1}/generator/components/background_downloader.py +0 -0
  30. {doctr_synth_generator-0.3.0 → doctr_synth_generator-0.3.1}/generator/components/background_manager.py +0 -0
  31. {doctr_synth_generator-0.3.0 → doctr_synth_generator-0.3.1}/generator/components/config.py +0 -0
  32. {doctr_synth_generator-0.3.0 → doctr_synth_generator-0.3.1}/generator/components/corpus_downloader.py +0 -0
  33. {doctr_synth_generator-0.3.0 → doctr_synth_generator-0.3.1}/generator/components/dataset_balancer.py +0 -0
  34. {doctr_synth_generator-0.3.0 → doctr_synth_generator-0.3.1}/generator/components/dataset_splitter.py +0 -0
  35. {doctr_synth_generator-0.3.0 → doctr_synth_generator-0.3.1}/generator/components/font_downloader.py +0 -0
  36. {doctr_synth_generator-0.3.0 → doctr_synth_generator-0.3.1}/generator/components/font_selector.py +0 -0
  37. {doctr_synth_generator-0.3.0 → doctr_synth_generator-0.3.1}/generator/components/generator.py +0 -0
  38. {doctr_synth_generator-0.3.0 → doctr_synth_generator-0.3.1}/generator/components/page_generator.py +0 -0
  39. {doctr_synth_generator-0.3.0 → doctr_synth_generator-0.3.1}/generator/components/text_renderer.py +0 -0
  40. {doctr_synth_generator-0.3.0 → doctr_synth_generator-0.3.1}/generator/components/text_styling.py +0 -0
  41. {doctr_synth_generator-0.3.0 → doctr_synth_generator-0.3.1}/generator/components/vocab_coverage.py +0 -0
  42. {doctr_synth_generator-0.3.0 → doctr_synth_generator-0.3.1}/generator/components/vocabs.py +0 -0
  43. {doctr_synth_generator-0.3.0 → doctr_synth_generator-0.3.1}/pyproject.toml +0 -0
  44. {doctr_synth_generator-0.3.0 → doctr_synth_generator-0.3.1}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: doctr-synth-generator
3
- Version: 0.3.0
3
+ Version: 0.3.1
4
4
  Summary: A synthetic data generator for training OCR models
5
5
  Author-email: Felix Dittrich <felixdittrich92@gmail.com>
6
6
  Maintainer: Felix Dittrich
@@ -250,7 +250,7 @@ Dynamic: license-file
250
250
  ![Build Status](https://github.com/felixdittrich92/docTR-Synth-Generator/workflows/builds/badge.svg)
251
251
  [![codecov](https://codecov.io/gh/felixdittrich92/docTR-Synth-Generator/graph/badge.svg?token=31MDR20JGI)](https://codecov.io/gh/felixdittrich92/docTR-Synth-Generator)
252
252
  [![CodeFactor](https://www.codefactor.io/repository/github/felixdittrich92/doctr-synth-generator/badge)](https://www.codefactor.io/repository/github/felixdittrich92/doctr-synth-generator)
253
- [![Pypi](https://img.shields.io/badge/pypi-v0.3.0a0-blue.svg)](https://pypi.org/project/docTR-Synth-Generator/)
253
+ [![Pypi](https://img.shields.io/badge/pypi-v0.3.1a0-blue.svg)](https://pypi.org/project/docTR-Synth-Generator/)
254
254
 
255
255
  # docTR-Synth-Generator
256
256
  A tool to generate synthetic OCR datasets - made for docTR
@@ -579,19 +579,16 @@ treat them the same: recognition yields the label string; detection yields
579
579
  from generator.components import GenerationConfig
580
580
  from generator.doctr_dataset import build_detection_datasets, synth_worker_init_fn
581
581
 
582
- cfg = GenerationConfig.flat(
583
- task="detection",
584
- languages=["en", "de"],
585
- num_images=50_000, # POOL size (word variety + vocab coverage)
586
- auto_download_backgrounds=True,
587
- )
582
+ # num_images is the total dataset size; val_percent carves out the val set.
583
+ cfg = GenerationConfig.flat(task="detection", num_images=50_000, val_percent=0.2)
588
584
  train_set, val_set = build_detection_datasets(
589
585
  cfg,
590
- train_samples=args.epochs and 20_000, # virtual epoch length (len(dataset))
591
- val_samples=2_000,
586
+ languages=["german", "english", "french", "italian", "spanish", "malay"], # VOCABS keys or ISO codes
592
587
  use_polygons=args.rotation, # straight boxes unless --rotation
593
- sample_transforms=batch_transforms, # the script's existing transforms
588
+ sample_transforms=sample_transforms, # the script's image+target transforms
589
+ img_transforms=img_transforms, # the script's image-only transforms
594
590
  )
591
+ # len(train_set) == 40_000, len(val_set) == 10_000
595
592
  ```
596
593
 
597
594
  **Recognition** - in `references/recognition/train.py`, replace the
@@ -601,26 +598,29 @@ train_set, val_set = build_detection_datasets(
601
598
  from generator.components import GenerationConfig
602
599
  from generator.doctr_dataset import build_recognition_datasets, synth_worker_init_fn
603
600
 
604
- cfg = GenerationConfig.flat(task="recognition", num_images=100_000)
601
+ cfg = GenerationConfig.flat(task="recognition", num_images=10_000, val_percent=0.2)
605
602
  train_set, val_set = build_recognition_datasets(
606
603
  cfg,
607
- train_samples=50_000,
608
- val_samples=5_000,
609
- vocab=args.vocab, # e.g. ["german", "urdu", "odia"] - the model's vocab
604
+ vocab=args.vocab, # e.g. ["multilingual"] - a VOCABS key / list = the model's vocab
610
605
  img_transforms=img_transforms, # the script's existing resize/aug
611
606
  )
607
+ # len(train_set) == 8_000, len(val_set) == 2_000
612
608
  ```
613
609
 
614
610
  Pass `vocab` the **same vocab you train the model on** - a `VOCABS` key, a
615
611
  literal charset, or a list of keys whose union is the model's vocab (e.g.
616
- `["german", "urdu", "odia"]`). Every generated label is then guaranteed to
617
- contain only characters in that vocab, so docTR's label encoder never hits an
618
- out-of-vocab character (which would otherwise crash training on the first batch).
619
- Corpus words outside the vocab are dropped, character coverage is guaranteed
620
- *within* the vocab, and the corpus languages are derived from the vocab keys
621
- automatically (`german` -> `de`); a key with no corpus (e.g. `odia`) still has
622
- its characters covered via synthesis. Set `config.languages` explicitly to pull
623
- different corpora. The same restriction applies to the offline generator via
612
+ `["multilingual"]` or `["german", "urdu", "odia"]`). Every generated label is
613
+ then guaranteed to contain only characters in that vocab, so docTR's label
614
+ encoder never hits an out-of-vocab character (which would otherwise crash
615
+ training on the first batch). Corpus words outside the vocab are dropped,
616
+ character coverage is guaranteed *within* the vocab (missing characters are
617
+ synthesised so the set stays balanced), and the corpus languages are derived
618
+ from the vocab keys automatically (`german` -> `de`). For detection, pass
619
+ `languages=[...]` (VOCABS keys like `"german"`, `"malay"`, or ISO codes); for
620
+ recognition you can pass `languages=[...]` too. A combined vocab like
621
+ `"multilingual"` maps to no single language, so pass `languages=[...]` if you
622
+ want real multilingual words rather than mostly-synthesised coverage. The same
623
+ restriction applies to the offline generator via
624
624
  `GenerationConfig.flat(target_vocab=[...])`.
625
625
 
626
626
  The `DataLoader` lines stay as they are - just keep
@@ -642,10 +642,11 @@ train_loader = DataLoader(
642
642
 
643
643
  Notes:
644
644
 
645
- - **Pool size vs epoch length.** `config.num_images` sizes the word *pool*
646
- (variety and per-split vocab coverage); `train_samples` / `val_samples` set
647
- the virtual epoch length (`len(dataset)`). Samples are generated fresh, so the
648
- epoch length is just how many iterations you want per epoch.
645
+ - **Dataset size & split.** `num_images` is the total dataset size; `val_percent`
646
+ carves out the validation set, so `len(train_set)` / `len(val_set)` follow
647
+ directly (50k @ 0.2 -> 40k / 10k). Samples are generated fresh on every access,
648
+ so this is effectively the iterations per epoch. Pass `train_samples` /
649
+ `val_samples` only if you want to override the derived sizes.
649
650
  - **Seeding.** The train set draws a fresh random sample on every access (new
650
651
  data every epoch - the whole point of on-the-fly); the val set is a
651
652
  reproducible fixed virtual set (seeded per index) so metrics stay comparable.
@@ -2,7 +2,7 @@
2
2
  ![Build Status](https://github.com/felixdittrich92/docTR-Synth-Generator/workflows/builds/badge.svg)
3
3
  [![codecov](https://codecov.io/gh/felixdittrich92/docTR-Synth-Generator/graph/badge.svg?token=31MDR20JGI)](https://codecov.io/gh/felixdittrich92/docTR-Synth-Generator)
4
4
  [![CodeFactor](https://www.codefactor.io/repository/github/felixdittrich92/doctr-synth-generator/badge)](https://www.codefactor.io/repository/github/felixdittrich92/doctr-synth-generator)
5
- [![Pypi](https://img.shields.io/badge/pypi-v0.3.0a0-blue.svg)](https://pypi.org/project/docTR-Synth-Generator/)
5
+ [![Pypi](https://img.shields.io/badge/pypi-v0.3.1a0-blue.svg)](https://pypi.org/project/docTR-Synth-Generator/)
6
6
 
7
7
  # docTR-Synth-Generator
8
8
  A tool to generate synthetic OCR datasets - made for docTR
@@ -331,19 +331,16 @@ treat them the same: recognition yields the label string; detection yields
331
331
  from generator.components import GenerationConfig
332
332
  from generator.doctr_dataset import build_detection_datasets, synth_worker_init_fn
333
333
 
334
- cfg = GenerationConfig.flat(
335
- task="detection",
336
- languages=["en", "de"],
337
- num_images=50_000, # POOL size (word variety + vocab coverage)
338
- auto_download_backgrounds=True,
339
- )
334
+ # num_images is the total dataset size; val_percent carves out the val set.
335
+ cfg = GenerationConfig.flat(task="detection", num_images=50_000, val_percent=0.2)
340
336
  train_set, val_set = build_detection_datasets(
341
337
  cfg,
342
- train_samples=args.epochs and 20_000, # virtual epoch length (len(dataset))
343
- val_samples=2_000,
338
+ languages=["german", "english", "french", "italian", "spanish", "malay"], # VOCABS keys or ISO codes
344
339
  use_polygons=args.rotation, # straight boxes unless --rotation
345
- sample_transforms=batch_transforms, # the script's existing transforms
340
+ sample_transforms=sample_transforms, # the script's image+target transforms
341
+ img_transforms=img_transforms, # the script's image-only transforms
346
342
  )
343
+ # len(train_set) == 40_000, len(val_set) == 10_000
347
344
  ```
348
345
 
349
346
  **Recognition** - in `references/recognition/train.py`, replace the
@@ -353,26 +350,29 @@ train_set, val_set = build_detection_datasets(
353
350
  from generator.components import GenerationConfig
354
351
  from generator.doctr_dataset import build_recognition_datasets, synth_worker_init_fn
355
352
 
356
- cfg = GenerationConfig.flat(task="recognition", num_images=100_000)
353
+ cfg = GenerationConfig.flat(task="recognition", num_images=10_000, val_percent=0.2)
357
354
  train_set, val_set = build_recognition_datasets(
358
355
  cfg,
359
- train_samples=50_000,
360
- val_samples=5_000,
361
- vocab=args.vocab, # e.g. ["german", "urdu", "odia"] - the model's vocab
356
+ vocab=args.vocab, # e.g. ["multilingual"] - a VOCABS key / list = the model's vocab
362
357
  img_transforms=img_transforms, # the script's existing resize/aug
363
358
  )
359
+ # len(train_set) == 8_000, len(val_set) == 2_000
364
360
  ```
365
361
 
366
362
  Pass `vocab` the **same vocab you train the model on** - a `VOCABS` key, a
367
363
  literal charset, or a list of keys whose union is the model's vocab (e.g.
368
- `["german", "urdu", "odia"]`). Every generated label is then guaranteed to
369
- contain only characters in that vocab, so docTR's label encoder never hits an
370
- out-of-vocab character (which would otherwise crash training on the first batch).
371
- Corpus words outside the vocab are dropped, character coverage is guaranteed
372
- *within* the vocab, and the corpus languages are derived from the vocab keys
373
- automatically (`german` -> `de`); a key with no corpus (e.g. `odia`) still has
374
- its characters covered via synthesis. Set `config.languages` explicitly to pull
375
- different corpora. The same restriction applies to the offline generator via
364
+ `["multilingual"]` or `["german", "urdu", "odia"]`). Every generated label is
365
+ then guaranteed to contain only characters in that vocab, so docTR's label
366
+ encoder never hits an out-of-vocab character (which would otherwise crash
367
+ training on the first batch). Corpus words outside the vocab are dropped,
368
+ character coverage is guaranteed *within* the vocab (missing characters are
369
+ synthesised so the set stays balanced), and the corpus languages are derived
370
+ from the vocab keys automatically (`german` -> `de`). For detection, pass
371
+ `languages=[...]` (VOCABS keys like `"german"`, `"malay"`, or ISO codes); for
372
+ recognition you can pass `languages=[...]` too. A combined vocab like
373
+ `"multilingual"` maps to no single language, so pass `languages=[...]` if you
374
+ want real multilingual words rather than mostly-synthesised coverage. The same
375
+ restriction applies to the offline generator via
376
376
  `GenerationConfig.flat(target_vocab=[...])`.
377
377
 
378
378
  The `DataLoader` lines stay as they are - just keep
@@ -394,10 +394,11 @@ train_loader = DataLoader(
394
394
 
395
395
  Notes:
396
396
 
397
- - **Pool size vs epoch length.** `config.num_images` sizes the word *pool*
398
- (variety and per-split vocab coverage); `train_samples` / `val_samples` set
399
- the virtual epoch length (`len(dataset)`). Samples are generated fresh, so the
400
- epoch length is just how many iterations you want per epoch.
397
+ - **Dataset size & split.** `num_images` is the total dataset size; `val_percent`
398
+ carves out the validation set, so `len(train_set)` / `len(val_set)` follow
399
+ directly (50k @ 0.2 -> 40k / 10k). Samples are generated fresh on every access,
400
+ so this is effectively the iterations per epoch. Pass `train_samples` /
401
+ `val_samples` only if you want to override the derived sizes.
401
402
  - **Seeding.** The train set draws a fresh random sample on every access (new
402
403
  data every epoch - the whole point of on-the-fly); the val set is a
403
404
  reproducible fixed virtual set (seeded per index) so metrics stay comparable.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: doctr-synth-generator
3
- Version: 0.3.0
3
+ Version: 0.3.1
4
4
  Summary: A synthetic data generator for training OCR models
5
5
  Author-email: Felix Dittrich <felixdittrich92@gmail.com>
6
6
  Maintainer: Felix Dittrich
@@ -250,7 +250,7 @@ Dynamic: license-file
250
250
  ![Build Status](https://github.com/felixdittrich92/docTR-Synth-Generator/workflows/builds/badge.svg)
251
251
  [![codecov](https://codecov.io/gh/felixdittrich92/docTR-Synth-Generator/graph/badge.svg?token=31MDR20JGI)](https://codecov.io/gh/felixdittrich92/docTR-Synth-Generator)
252
252
  [![CodeFactor](https://www.codefactor.io/repository/github/felixdittrich92/doctr-synth-generator/badge)](https://www.codefactor.io/repository/github/felixdittrich92/doctr-synth-generator)
253
- [![Pypi](https://img.shields.io/badge/pypi-v0.3.0a0-blue.svg)](https://pypi.org/project/docTR-Synth-Generator/)
253
+ [![Pypi](https://img.shields.io/badge/pypi-v0.3.1a0-blue.svg)](https://pypi.org/project/docTR-Synth-Generator/)
254
254
 
255
255
  # docTR-Synth-Generator
256
256
  A tool to generate synthetic OCR datasets - made for docTR
@@ -579,19 +579,16 @@ treat them the same: recognition yields the label string; detection yields
579
579
  from generator.components import GenerationConfig
580
580
  from generator.doctr_dataset import build_detection_datasets, synth_worker_init_fn
581
581
 
582
- cfg = GenerationConfig.flat(
583
- task="detection",
584
- languages=["en", "de"],
585
- num_images=50_000, # POOL size (word variety + vocab coverage)
586
- auto_download_backgrounds=True,
587
- )
582
+ # num_images is the total dataset size; val_percent carves out the val set.
583
+ cfg = GenerationConfig.flat(task="detection", num_images=50_000, val_percent=0.2)
588
584
  train_set, val_set = build_detection_datasets(
589
585
  cfg,
590
- train_samples=args.epochs and 20_000, # virtual epoch length (len(dataset))
591
- val_samples=2_000,
586
+ languages=["german", "english", "french", "italian", "spanish", "malay"], # VOCABS keys or ISO codes
592
587
  use_polygons=args.rotation, # straight boxes unless --rotation
593
- sample_transforms=batch_transforms, # the script's existing transforms
588
+ sample_transforms=sample_transforms, # the script's image+target transforms
589
+ img_transforms=img_transforms, # the script's image-only transforms
594
590
  )
591
+ # len(train_set) == 40_000, len(val_set) == 10_000
595
592
  ```
596
593
 
597
594
  **Recognition** - in `references/recognition/train.py`, replace the
@@ -601,26 +598,29 @@ train_set, val_set = build_detection_datasets(
601
598
  from generator.components import GenerationConfig
602
599
  from generator.doctr_dataset import build_recognition_datasets, synth_worker_init_fn
603
600
 
604
- cfg = GenerationConfig.flat(task="recognition", num_images=100_000)
601
+ cfg = GenerationConfig.flat(task="recognition", num_images=10_000, val_percent=0.2)
605
602
  train_set, val_set = build_recognition_datasets(
606
603
  cfg,
607
- train_samples=50_000,
608
- val_samples=5_000,
609
- vocab=args.vocab, # e.g. ["german", "urdu", "odia"] - the model's vocab
604
+ vocab=args.vocab, # e.g. ["multilingual"] - a VOCABS key / list = the model's vocab
610
605
  img_transforms=img_transforms, # the script's existing resize/aug
611
606
  )
607
+ # len(train_set) == 8_000, len(val_set) == 2_000
612
608
  ```
613
609
 
614
610
  Pass `vocab` the **same vocab you train the model on** - a `VOCABS` key, a
615
611
  literal charset, or a list of keys whose union is the model's vocab (e.g.
616
- `["german", "urdu", "odia"]`). Every generated label is then guaranteed to
617
- contain only characters in that vocab, so docTR's label encoder never hits an
618
- out-of-vocab character (which would otherwise crash training on the first batch).
619
- Corpus words outside the vocab are dropped, character coverage is guaranteed
620
- *within* the vocab, and the corpus languages are derived from the vocab keys
621
- automatically (`german` -> `de`); a key with no corpus (e.g. `odia`) still has
622
- its characters covered via synthesis. Set `config.languages` explicitly to pull
623
- different corpora. The same restriction applies to the offline generator via
612
+ `["multilingual"]` or `["german", "urdu", "odia"]`). Every generated label is
613
+ then guaranteed to contain only characters in that vocab, so docTR's label
614
+ encoder never hits an out-of-vocab character (which would otherwise crash
615
+ training on the first batch). Corpus words outside the vocab are dropped,
616
+ character coverage is guaranteed *within* the vocab (missing characters are
617
+ synthesised so the set stays balanced), and the corpus languages are derived
618
+ from the vocab keys automatically (`german` -> `de`). For detection, pass
619
+ `languages=[...]` (VOCABS keys like `"german"`, `"malay"`, or ISO codes); for
620
+ recognition you can pass `languages=[...]` too. A combined vocab like
621
+ `"multilingual"` maps to no single language, so pass `languages=[...]` if you
622
+ want real multilingual words rather than mostly-synthesised coverage. The same
623
+ restriction applies to the offline generator via
624
624
  `GenerationConfig.flat(target_vocab=[...])`.
625
625
 
626
626
  The `DataLoader` lines stay as they are - just keep
@@ -642,10 +642,11 @@ train_loader = DataLoader(
642
642
 
643
643
  Notes:
644
644
 
645
- - **Pool size vs epoch length.** `config.num_images` sizes the word *pool*
646
- (variety and per-split vocab coverage); `train_samples` / `val_samples` set
647
- the virtual epoch length (`len(dataset)`). Samples are generated fresh, so the
648
- epoch length is just how many iterations you want per epoch.
645
+ - **Dataset size & split.** `num_images` is the total dataset size; `val_percent`
646
+ carves out the validation set, so `len(train_set)` / `len(val_set)` follow
647
+ directly (50k @ 0.2 -> 40k / 10k). Samples are generated fresh on every access,
648
+ so this is effectively the iterations per epoch. Pass `train_samples` /
649
+ `val_samples` only if you want to override the derived sizes.
649
650
  - **Seeding.** The train set draws a fresh random sample on every access (new
650
651
  data every epoch - the whole point of on-the-fly); the val set is a
651
652
  reproducible fixed virtual set (seeded per index) so metrics stay comparable.
@@ -172,6 +172,7 @@ class SyntheticDatasetGenerator:
172
172
  reproduces the same balance and per-split character coverage without
173
173
  ever touching disk. ``config.core.num_images`` controls the pool size.
174
174
  """
175
+ self._resolve_background_dir() # so on-the-fly crops get real (downloaded) backgrounds
175
176
  return self._prepare_train_val()
176
177
 
177
178
  def build_detection_pool(self) -> list[str]:
@@ -184,6 +185,16 @@ class SyntheticDatasetGenerator:
184
185
  self._resolve_background_dir()
185
186
  return self._resolve_word_pool()
186
187
 
188
+ def resolve_backgrounds(self) -> str | None:
189
+ """Resolve (downloading if needed) the background dir and return it.
190
+
191
+ Mutates ``config.resources.bg_image_dir`` to the resolved directory.
192
+ Useful when rendering pages directly via ``PageGenerator`` instead of
193
+ going through :meth:`generate_dataset` or the dataset factories.
194
+ """
195
+ self._resolve_background_dir()
196
+ return self.config.resources.bg_image_dir
197
+
187
198
  def _vocab_charset(self) -> set[str] | None:
188
199
  """Characters recognition labels must stay within (the model's vocab).
189
200
 
@@ -8,7 +8,6 @@ from __future__ import annotations
8
8
  import os
9
9
  import random
10
10
  import time
11
- import warnings
12
11
  from dataclasses import replace
13
12
  from typing import Any, Callable
14
13
 
@@ -321,58 +320,76 @@ class SyntheticDetectionDataset(_BaseSynthDataset):
321
320
  # --------------------------------------------------------------------------- #
322
321
  # Convenience factories: build matched (train, val) datasets in one call.
323
322
  # --------------------------------------------------------------------------- #
323
+ def _to_corpus_languages(languages: list[str]) -> list[str]:
324
+ """Map human-readable VOCABS keys (``"german"``) to corpus ISO codes (``"de"``).
325
+
326
+ Anything already an ISO code (or unknown) is passed through unchanged.
327
+ """
328
+ return [VOCAB_TO_LANGUAGE.get(lang, lang) for lang in languages]
329
+
330
+
331
+ def _split_sizes(config: GenerationConfig, train_samples: int | None, val_samples: int | None) -> tuple[int, int]:
332
+ """Per-epoch ``(train, val)`` sizes derived from ``num_images`` and ``val_percent``.
333
+
334
+ Explicit ``train_samples`` / ``val_samples`` override the derived values.
335
+ """
336
+ total = max(1, int(config.core.num_images))
337
+ val = val_samples if val_samples is not None else max(1, round(total * config.core.val_percent))
338
+ train = train_samples if train_samples is not None else max(1, total - round(total * config.core.val_percent))
339
+ return int(train), int(val)
340
+
341
+
324
342
  def build_recognition_datasets(
325
343
  config: GenerationConfig,
326
344
  *,
327
- train_samples: int,
328
- val_samples: int,
329
345
  vocab: str | list[str] | None = None,
346
+ languages: list[str] | None = None,
330
347
  img_transforms: Callable | None = None,
331
348
  sample_transforms: Callable | None = None,
349
+ train_samples: int | None = None,
350
+ val_samples: int | None = None,
332
351
  val_seed: int = 1234,
333
352
  ) -> tuple[SyntheticRecognitionDataset, SyntheticRecognitionDataset]:
334
- """Build ``(train, val)`` on-the-fly recognition datasets sharing one pipeline.
353
+ """Build ``(train, val)`` on-the-fly recognition datasets from a config.
335
354
 
336
- The recognition pools (with vocab coverage) are built once. The train set
337
- draws fresh crops every access; the val set is a reproducible virtual set.
338
- ``config.core.num_images`` sets the *pool* size (variety); ``train_samples`` /
339
- ``val_samples`` set the per-epoch iteration counts.
355
+ Sizes come straight from the config: ``num_images`` total, split into
356
+ ``len(train)`` / ``len(val)`` by ``val_percent`` (override with the optional
357
+ ``train_samples`` / ``val_samples``). The train set draws fresh crops every
358
+ access; the val set is reproducible. Words, fonts and (any) backgrounds are
359
+ downloaded/cached once in the parent process.
340
360
 
341
361
  Pass ``vocab`` to pin the docTR vocab to train against - a ``VOCABS`` key, a
342
- literal charset, or a list such as ``["german", "urdu", "odia"]`` (their
343
- union). Every generated label is then guaranteed to fall within that
344
- character set, so a docTR recognition model trained on the matching
345
- ``--vocab`` never hits a label it cannot encode. When ``config.core.languages`` is
346
- empty the corpus languages are derived from the vocab keys (``german`` ->
347
- ``de``); keys with no corpus mapping are still covered via synthesis.
362
+ literal charset, or a list such as ``["german", "urdu"]`` (their union).
363
+ Every label is restricted to that character set, and characters missing from
364
+ the corpus are synthesised so coverage stays balanced - a model trained on
365
+ the matching ``--vocab`` never hits an un-encodable label.
366
+
367
+ Corpus languages come from ``languages`` (ISO codes or VOCABS keys) when
368
+ given, else the language(s) the ``vocab`` keys map to, else
369
+ ``config.core.languages``. Combined vocabs such as ``"multilingual"`` have no
370
+ single language, so pass ``languages=[...]`` for real multilingual words
371
+ rather than mostly-synthesised coverage.
348
372
  """
349
- if vocab is not None:
373
+ if languages is not None:
374
+ langs = _to_corpus_languages(languages)
375
+ elif vocab is not None:
350
376
  keys = [vocab] if isinstance(vocab, str) else list(vocab)
351
377
  derived = [VOCAB_TO_LANGUAGE[k] for k in keys if k in VOCAB_TO_LANGUAGE]
352
- unmapped = [k for k in keys if k not in VOCAB_TO_LANGUAGE]
353
- # Use the vocab-derived corpus languages unless the user picked their own
354
- # (i.e. set config.core.languages to something other than the "en" default).
355
- user_chose = bool(config.core.languages) and list(config.core.languages) != ["en"] # type: ignore[arg-type]
356
- languages = list(config.core.languages) if user_chose else (derived or config.core.languages) # type: ignore[arg-type]
357
- if unmapped and not user_chose:
358
- warnings.warn(
359
- f"No corpus language mapping for vocab key(s) {unmapped}; their characters "
360
- "will still be covered via synthesis, but pass an explicit `languages=[...]` "
361
- "on the config to also pull real words for them.",
362
- stacklevel=2,
363
- )
364
- config = replace(
365
- config,
366
- core=replace(config.core, languages=languages),
367
- coverage=replace(config.coverage, target_vocab=vocab, restrict_to_vocab=True),
368
- )
378
+ langs = derived or list(config.core.languages or ["en"])
379
+ else:
380
+ langs = list(config.core.languages or ["en"])
369
381
 
382
+ config = replace(config, core=replace(config.core, languages=langs))
383
+ if vocab is not None:
384
+ config = replace(config, coverage=replace(config.coverage, target_vocab=vocab, restrict_to_vocab=True))
385
+
386
+ train_n, val_n = _split_sizes(config, train_samples, val_samples)
370
387
  pipeline = SyntheticDatasetGenerator(config)
371
388
  train_pool, val_pool = pipeline.build_recognition_pools()
372
389
  train_set = SyntheticRecognitionDataset(
373
390
  train_pool,
374
391
  config,
375
- train_samples,
392
+ train_n,
376
393
  vocab=vocab,
377
394
  img_transforms=img_transforms,
378
395
  sample_transforms=sample_transforms,
@@ -381,7 +398,7 @@ def build_recognition_datasets(
381
398
  val_set = SyntheticRecognitionDataset(
382
399
  val_pool,
383
400
  config,
384
- val_samples,
401
+ val_n,
385
402
  vocab=vocab,
386
403
  img_transforms=img_transforms,
387
404
  sample_transforms=sample_transforms,
@@ -393,24 +410,36 @@ def build_recognition_datasets(
393
410
  def build_detection_datasets(
394
411
  config: GenerationConfig,
395
412
  *,
396
- train_samples: int,
397
- val_samples: int,
413
+ languages: list[str] | None = None,
398
414
  use_polygons: bool = False,
399
415
  img_transforms: Callable | None = None,
400
416
  sample_transforms: Callable | None = None,
417
+ train_samples: int | None = None,
418
+ val_samples: int | None = None,
401
419
  val_seed: int = 1234,
402
420
  ) -> tuple[SyntheticDetectionDataset, SyntheticDetectionDataset]:
403
- """Build ``(train, val)`` on-the-fly detection datasets sharing one pipeline.
421
+ """Build ``(train, val)`` on-the-fly detection datasets from a config.
422
+
423
+ Like :func:`build_recognition_datasets`, sizes come from the config
424
+ (``num_images`` total, split by ``val_percent``; override with
425
+ ``train_samples`` / ``val_samples``). Backgrounds, words and fonts are
426
+ downloaded/cached once in the parent process, so auto-download just works.
404
427
 
405
- Backgrounds and the word pool are resolved once in the parent process; the
406
- train set draws fresh pages every access while the val set is reproducible.
428
+ ``languages`` accepts ISO codes or VOCABS keys (e.g. ``"german"``,
429
+ ``"english"``, ``"malay"``); when omitted ``config.core.languages`` is used.
430
+ Set ``use_polygons=True`` to keep rotated quads (matches the training
431
+ script's ``--rotation`` flag); otherwise axis-aligned boxes are returned.
407
432
  """
433
+ if languages is not None:
434
+ config = replace(config, core=replace(config.core, languages=_to_corpus_languages(languages)))
435
+
436
+ train_n, val_n = _split_sizes(config, train_samples, val_samples)
408
437
  pipeline = SyntheticDatasetGenerator(config)
409
- pool = pipeline.build_detection_pool()
438
+ pool = pipeline.build_detection_pool() # resolves backgrounds + builds the word pool
410
439
  train_set = SyntheticDetectionDataset(
411
440
  pool,
412
441
  config,
413
- train_samples,
442
+ train_n,
414
443
  use_polygons=use_polygons,
415
444
  img_transforms=img_transforms,
416
445
  sample_transforms=sample_transforms,
@@ -419,7 +448,7 @@ def build_detection_datasets(
419
448
  val_set = SyntheticDetectionDataset(
420
449
  pool,
421
450
  config,
422
- val_samples,
451
+ val_n,
423
452
  use_polygons=use_polygons,
424
453
  img_transforms=img_transforms,
425
454
  sample_transforms=sample_transforms,
@@ -0,0 +1 @@
1
+ __version__ = '0.3.1'
@@ -104,7 +104,12 @@ def recognition_crop(corpus, languages, seed, bg_dir=None):
104
104
  words = corpus.build_vocabulary(languages, words_per_language=6000)
105
105
  gen = TextImageGenerator(
106
106
  GenerationConfig.flat(
107
- task="recognition", languages=languages, auto_download_fonts=True, bg_image_dir=bg_dir, **CACHE
107
+ task="recognition",
108
+ languages=languages,
109
+ auto_download_fonts=True,
110
+ auto_download_backgrounds=False,
111
+ bg_image_dir=bg_dir,
112
+ **CACHE,
108
113
  )
109
114
  )
110
115
  random.seed(seed * 13)
@@ -9,7 +9,7 @@ from pathlib import Path
9
9
  from setuptools import setup
10
10
 
11
11
  PKG_NAME = "doctr-synth-generator"
12
- VERSION = os.getenv("BUILD_VERSION", "0.3.0a0")
12
+ VERSION = os.getenv("BUILD_VERSION", "0.3.1a0")
13
13
 
14
14
 
15
15
  if __name__ == "__main__":
@@ -1 +0,0 @@
1
- __version__ = '0.3.0'