Skip to content

Commit 2298650

Browse files
committed
Complete 8th tutorial code
1 parent 5677be2 commit 2298650

File tree

8 files changed

+27
-15
lines changed

8 files changed

+27
-15
lines changed

CHANGELOG.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
## [1.0.2] - 2022-03-... (unreleased)
22
### Changed
33
- changes `OnnxInferenceModel` in `mltu.torch.inferenceModels` to load custom metadata from saved ONNX model
4+
- improved `mltu.dataProvider` to remove bad samples from dataset on epoch end
45

56
### Added:
67
- added `mltu.torch.losses`, used to create PyTorch losses, that may be used in training and validation
78
- added CTC loss to `mltu.torch.losses` that can be used for training CTC based models
89
- added `Model2onnx` and `Tensorboard` callbacks to `mltu.torch.callbacks`, used to create PyTorch callbacks, that may be used in training and validation
9-
- added `CERMetric` to `mltu.torch.metrics`, used to create PyTorch metrics, that may be used in training and validation
10-
- created 08 pytorch tutorial, that shows how to use mltu.torch to train CTC based models
10+
- added `CERMetric` and `WERMetric` to `mltu.torch.metrics`, used to create PyTorch metrics, that may be used in training and validation
11+
- created 08 pytorch tutorial, that shows how to use `mltu.torch` to train CTC based models
12+
1113

1214
## [1.0.1] - 2022-03-06
1315
### Changed
@@ -36,7 +38,7 @@
3638
-
3739
### Added:
3840
- added 05_sound_to_text tutorial
39-
- added WavReader to mltu/preprocessors, used to read wav files and convert them to numpy arrays
41+
- added `WavReader` to `mltu/preprocessors`, used to read wav files and convert them to numpy arrays
4042

4143

4244
## [0.1.7] - 2022-02-03
@@ -46,11 +48,11 @@
4648

4749
## [0.1.5] - 2022-01-10
4850
### Changed
49-
- seperated CWERMetric to SER and WER Metrics in mltu.metrics, Character/word rate was calculatted in a wrong way
51+
- seperated `CWERMetric` to `CER` and `WER` Metrics in `mltu.metrics`, Character/word rate was calculatted in a wrong way
5052
- created @setter for augmentors and transformers in DataProvider, to properlly add augmentors and transformers to the pipeline
5153
- augmentors and transformers must inherit from `mltu.augmentors.base.Augmentor` and `mltu.transformers.base.Transformer` respectively
5254
- updated ImageShowCV2 transformer documentation
53-
- fixed OnnxInferenceModel in mltu.inferenceModels to use CPU even if GPU is available with force_cpu=True flag
55+
- fixed OnnxInferenceModel in `mltu.inferenceModels` to use CPU even if GPU is available with force_cpu=True flag
5456

5557
### Added:
5658
- added RandomSharpen to mltu.augmentors, used for simple image augmentation;

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,5 @@ Each tutorial has its own requirements.txt file for a specific mltu version. As
2222
4. [Handwritten sentence recognition with TensorFlow](https://pylessons.com/handwritten-sentence-recognition), code in ```Tutorials\04_sentence_recognition``` folder;
2323
5. [Introduction to speech recognition with TensorFlow](https://pylessons.com/speech-recognition), code in ```Tutorials\05_speech_recognition``` folder;
2424
6. [Introduction to PyTorch in a practical way](https://pylessons.com/pytorch-introduction), code in ```Tutorials\06_pytorch_introduction``` folder;
25-
7. [Using custom wrapper to simplify PyTorch models training pipeline](https://pylessons.com/pytorch-introduction), code in ```Tutorials\07_pytorch_wrapper``` folder;
25+
7. [Using custom wrapper to simplify PyTorch models training pipeline](https://pylessons.com/pytorch-introduction), code in ```Tutorials\07_pytorch_wrapper``` folder;
26+
8. [Handwriting words recognition with PyTorch](https://pylessons.com/handwriting-recognition-pytorch), code in ```Tutorials\08_handwriting_recognition_torch``` folder;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Using custom wrapper to simplify PyTorch models training pipeline
2+
### Construct an accurate handwriting recognition model with PyTorch! Understand how to use MLTU package, to simplify the PyTorch models training pipeline, and discover methods to enhance your model's accuracy!<br><br>
3+
4+
# **Detailed tutorial**:
5+
### [Handwriting words recognition with PyTorch](https://pylessons.com/handwriting-recognition-pytorch)
6+
7+
<p align="center">
8+
<img src="https://pylessons.com/media/Tutorials/mltu/handwriting-recognition-pytorch/handwriting-recognition-pytorch.png">
9+
</p>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
torch==1.13.1
2+
tensorboard==2.10.1
3+
onnx==1.12.0
4+
torchsummaryX

Tutorials/08_handwriting_recognition_torch/train_torch.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from mltu.torch.callbacks import EarlyStopping, ModelCheckpoint, TensorBoard, Model2onnx, ReduceLROnPlateau
1717

1818
from mltu.preprocessors import ImageReader
19-
from mltu.transformers import ImageResizer, LabelIndexer, LabelPadding
19+
from mltu.transformers import ImageResizer, LabelIndexer, LabelPadding, ImageShowCV2
2020
from mltu.augmentors import RandomBrightness, RandomRotate, RandomErodeDilate, RandomSharpen
2121

2222
from model import Network
@@ -80,12 +80,14 @@ def download_and_unzip(url, extract_to='Datasets', chunk_size=1024*1024):
8080
batch_size=configs.batch_size,
8181
data_preprocessors=[ImageReader()],
8282
transformers=[
83+
# ImageShowCV2(), # uncomment to show images during training
8384
ImageResizer(configs.width, configs.height, keep_aspect_ratio=False),
8485
LabelIndexer(configs.vocab),
8586
LabelPadding(max_word_length=configs.max_text_length, padding_value=len(configs.vocab))
8687
],
8788
use_cache=True,
8889
)
90+
8991
# Split the dataset into training and validation sets
9092
train_dataProvider, test_dataProvider = data_provider.split(split = 0.9)
9193

mltu/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "1.0.1"
1+
__version__ = "1.0.2"

mltu/torch/losses.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,9 @@ def forward(self, output, target):
2626
# Remove padding and blank tokens from target
2727
target_lengths = torch.sum(target != self.blank, dim=1)
2828
target_unpadded = target[target != self.blank].view(-1)
29-
# target_unpadded = []
30-
# for i in range(target.size(0)):
31-
# target_unpadded.append(target[i, :target_lengths[i]])
32-
# target_unpadded = torch.cat(target_unpadded)
33-
3429

3530
output = output.permute(1, 0, 2) # (sequence_length, batch_size, num_classes)
3631
output_lengths = torch.full(size=(output.size(1),), fill_value=output.size(0), dtype=torch.int64)
37-
# target_lengths = torch.full(size=(output.size(1),), fill_value=target.size(1), dtype=torch.int64)
3832

3933
loss = self.ctc_loss(output, target_unpadded, output_lengths, target_lengths)
4034

mltu/transformers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ def __call__(self, data: np.ndarray, label: np.ndarray):
157157
"""
158158
if self.verbose:
159159
if isinstance(label, (str, int, float)):
160-
logger.info('Label: ', label)
160+
logger.info(f'Label: {label}')
161161

162162
cv2.imshow('image', data)
163163
cv2.waitKey(0)

0 commit comments

Comments
 (0)