Sneriko commited on
Commit
d0c3a43
·
verified ·
1 Parent(s): 6ecbb5d

Create README.md

Browse files
Files changed (1) hide show
  1. README.md +311 -0
README.md ADDED
@@ -0,0 +1,311 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ '[object Object]': null
3
+ datasets:
4
+ - Riksarkivet/goteborgs_poliskammare_fore_1900_lines
5
+ - Riksarkivet/krigshovrattens_dombocker_lines
6
+ - Riksarkivet/svea_hovratt_lines
7
+ - Riksarkivet/bergskollegium_relationer_och_skrivelser_lines
8
+ - Riksarkivet/frihetstidens_utskottshandlingar_lines
9
+ - Riksarkivet/carl_fredrik_pahlmans_resejournaler_lines
10
+ - Riksarkivet/trolldomskommissionen_lines
11
+ - Riksarkivet/gota_hovratt_lines
12
+ - Riksarkivet/bergmastaren_i_nora_htr_lines
13
+ - Riksarkivet/alvsborgs_losen_lines
14
+ - Riksarkivet/jonkopings_radhusratt_och_magistrat_lines
15
+ language:
16
+ - sv
17
+ metrics:
18
+ - cer
19
+ - wer
20
+ base_model:
21
+ - microsoft/trocr-base-handwritten
22
+ pipeline_tag: image-to-text
23
+ tags:
24
+ - swedish lion libre
25
+ - htr
26
+ - swedish
27
+ - historical
28
+ - handwriting
29
+ ---
30
+
31
+ # Swedish Lion Libre
32
+
33
+ An HTR model for historical swedish developed by the Swedish National Archives in collaboration with The City Arhive of Stockholm, The Finish National Archives and Jämtlands Fornskriftsällskap. The model is trained for swedish handwriting dating from ca 1600-1900
34
+
35
+ ## Model Details
36
+
37
+ ### Model Description
38
+
39
+ - **Developed by:** The Swedish National Archives
40
+ - **Model type:** TrOCR base handwritten
41
+ - **Language(s) (NLP):** Historical Swedish handwriting
42
+ - **License:** {{ license | default("[More Information Needed]", true)}}
43
+ - **Finetuned from model:** trocr-base-handwritten
44
+
45
+ ## Uses
46
+
47
+ The model is trained on Swedish running-text handwriting dating from the start of the 17th century to the end of the 19th century. Like most current HTR-models
48
+ it operates on a text-line level, so it's intended use is within an HTR'pipeline that segments the text into text-lines, which are transcribed by the model.
49
+
50
+ ### Direct Use
51
+
52
+ <!-- This section is for the model use without fine-tuning or plugging into a larger ecosystem/app. -->
53
+
54
+ The model can be used without fine-tuning on all handwriting but performs best on the type of handwriting it was trained on, which is Swedish handwriting from 1600-1900.
55
+ See below for detailed test and evaluation results.
56
+
57
+ ### Downstream Use
58
+
59
+ The model can be fine-tuned on other types of handwriting, or if you plan to use it to transcribe some specific material that is within it's domain but not included in the training data, for instance if you got a large letter collection dating from the 17th century, it can be fine-tuned on a small amount of manually transcribed in-domain data, say 20-50 letters, and then used to transcribe the entire collection.
60
+
61
+ ### Out-of-Scope Use
62
+
63
+ The model wont work well out-of-the-box for other languages than Swedish, and it wont work well for printed text.
64
+
65
+ ## How to Get Started with the Model
66
+
67
+ Use the code below to get started with the model, but bare in mind that the image has to be a single text-line.
68
+
69
+ ```python
70
+ from transformers import TrOCRProcessor, VisionEncoderDecoderModel
71
+ from PIL import Image
72
+ import requests
73
+
74
+ img_path = 'path/to/image'
75
+ image = Image.open(img_path)
76
+
77
+ processor = TrOCRProcessor.from_pretrained("microsoft/trocr-base-handwritten")
78
+ model = VisionEncoderDecoderModel.from_pretrained('Riksarkivet/trocr-base-handwritten-hist-swe-2')
79
+ pixel_values = processor(images=image, return_tensors="pt").pixel_values
80
+
81
+ generated_ids = model.generate(pixel_values)
82
+ generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
83
+ ```
84
+
85
+ If you want to transcribe entire pages, consider using HTRflow, a package developed by The Swedish National Archives and intended for streamlining large and small scale HTR/OCR-projects. Install the package, write a pipeline config yaml, where you specify the models to use by their huggingface id, add preprocessing or post-processing steps, and then run the pipeline with `htrflow pipeline <path/to/yaml> <path/to/image/images>`. A .yaml file for an entire pipeline, transcribing full pages, could look like this:
86
+
87
+ ```yaml
88
+
89
+ # Demo pipeline for running text
90
+
91
+ steps:
92
+
93
+ # Region segmentation
94
+ - step: Segmentation
95
+ settings:
96
+ model: yolo
97
+ model_settings:
98
+ model: Riksarkivet/yolov9-regions-1
99
+ generation_settings:
100
+ conf: 0.3
101
+ batch_size: 32
102
+
103
+ # Line segmentation
104
+ - step: Segmentation
105
+ settings:
106
+ model: yolo
107
+ model_settings:
108
+ model: Riksarkivet/yolov9-lines-within-regions-1
109
+ generation_settings:
110
+ conf: 0.3
111
+ batch_size: 16
112
+
113
+ - step: TextRecognition
114
+ settings:
115
+ model: WordLevelTrocr
116
+ model_settings:
117
+ model: Riksarkivet/trocr-base-handwritten-hist-swe-2
118
+ generation_settings:
119
+ batch_size: 16
120
+ num_beams: 1
121
+
122
+ - step: ReadingOrderMarginalia
123
+ settings:
124
+ two_page: always
125
+
126
+ - step: RemoveLowTextConfidencePages
127
+ settings:
128
+ threshold: 0.95
129
+
130
+ - step: RemoveLowTextConfidenceLines
131
+ settings:
132
+ threshold: 0.95
133
+
134
+ # Export to Alto and Page XML
135
+ - step: Export
136
+ settings:
137
+ dest: outputs/new_models/alto
138
+ format: alto
139
+
140
+ - step: Export
141
+ settings:
142
+ dest: outputs/new_models/page
143
+ format: page
144
+
145
+ # Sets label format to regionX_lineY_wordZ
146
+ labels:
147
+ level_labels:
148
+ - region
149
+ - line
150
+ - word
151
+ sep: _
152
+ template: "{label}{number}"
153
+ ```
154
+
155
+ See the documentation for the HTRflow package for further instructions on specific steps and customizations
156
+
157
+
158
+ ## Training Details
159
+
160
+ ### Training Data
161
+
162
+ We cannot publically release all data the model was trained on, since we ourselves haven't created all the data, but below are links to the datasets we can release publically:
163
+
164
+ [Göteborgs poliskammare 1850-1900](https://huggingface.co/datasets/Riksarkivet/goteborgs_poliskammare_fore_1900_lines)
165
+ [Krigshovrättens domböcker](https://huggingface.co/datasets/Riksarkivet/krigshovrattens_dombocker_lines)
166
+ [Svea hovrätt](https://huggingface.co/datasets/Riksarkivet/svea_hovratt_lines)
167
+ [Bergskollegium](https://huggingface.co/datasets/Riksarkivet/bergskollegium_relationer_och_skrivelser_lines)
168
+ [Frihetstidens utskottshanlingar](https://huggingface.co/datasets/Riksarkivet/frihetstidens_utskottshandlingar_lines)
169
+ [Carl-Fredrik Påhlmans resejournaler](https://huggingface.co/datasets/Riksarkivet/carl_fredrik_pahlmans_resejournaler_lines)
170
+ [Trolldomskommissionen](https://huggingface.co/datasets/Riksarkivet/trolldomskommissionen_lines)
171
+ [Göta hovrätt](https://huggingface.co/datasets/Riksarkivet/gota_hovratt_lines)
172
+ [Bergmästaren i Nora](https://huggingface.co/datasets/Riksarkivet/bergmastaren_i_nora_htr_lines)
173
+ [Älvsborgs lösen](https://huggingface.co/datasets/Riksarkivet/alvsborgs_losen_lines)
174
+ [Jönköpings rådhusrätt magistrat](https://huggingface.co/datasets/Riksarkivet/jonkopings_radhusratt_och_magistrat_lines)
175
+
176
+ ### Training Procedure
177
+
178
+ <!-- This relates heavily to the Technical Specifications. Content here should link to that section when it is relevant to the training procedure. -->
179
+
180
+ #### Preprocessing
181
+
182
+ The text-line polygons were masked out and placed against a white backgroundy, with dimensions decided by the polygon's bounding box.
183
+
184
+
185
+ #### Training Hyperparameters
186
+
187
+ See config.json at [model repo](https://huggingface.co/Riksarkivet/trocr-base-handwritten-hist-swe-2/tree/main)
188
+ **training regime**: bf16
189
+ **learning rate**: 5e-5
190
+ **weight decay**: 0.01
191
+
192
+
193
+ ## Evaluation
194
+
195
+ ### In-Domain Evaluation Data (Sorted by CER)
196
+
197
+ These are the character and word error rates on evaluation data taken from the same archives that was included in the training set. Of course the evaluation samples aren't part of the training data. The number of samples included in the training-set give an indication of how the model improves by fine-tuning it on some specific material within the model's range.
198
+
199
+ | Dataset | WER | CER | Train Lines | Eval Lines |
200
+ |---------|-----|-----|-------------|------------|
201
+ | krigshovrattens_dombocker_lines | 0.0330 | 0.0075 | 16,887 | 1,877 |
202
+ | stockholms_stadsarkiv_allmana_barnhuset_1700_lines | 0.0647 | 0.0120 | 565 | 142 |
203
+ | stockholms_stadsarkiv_blandat_2_1700_lines | 0.0807 | 0.0170 | 25,024 | 2,781 |
204
+ | goteborgs_poliskammare_fore_1900_lines | 0.0800 | 0.0187 | 339,297 | 17,858 |
205
+ | stockholms_stadsarkiv_stockholms_domkapitel_1700_lines | 0.0948 | 0.0187 | 96,409 | 5,075 |
206
+ | stockholms_stadsarkiv_politikollegiet_1700_lines | 0.1108 | 0.0225 | 120,238 | 6,329 |
207
+ | bergskollegium_relationer_och_skrivelser_lines | 0.1056 | 0.0253 | 62,201 | 6,912 |
208
+ | stockholms_stadsarkiv_stadens_kamnarsratt_1700_lines | 0.1252 | 0.0278 | 38,330 | 4,259 |
209
+ | svea_hovratt_lines | 0.1484 | 0.0313 | 36,884 | 4,099 |
210
+ | stockholms_stadsarkiv_stockholms_domkapitel_1800_lines | 0.1400 | 0.0324 | 2,070 | 230 |
211
+ | stockholms_stadsarkiv_handelskollegiet_1600_1700_lines | 0.1785 | 0.0350 | 9,201 | 1,023 |
212
+ | frihetstidens_utskottshandlingar_lines | 0.1481 | 0.0362 | 13,490 | 1,499 |
213
+ | stockholms_stadsarkiv_kungliga_hovkonsistoriet_1700_lines | 0.1541 | 0.0364 | 5,753 | 640 |
214
+ | national_archives_finland_court_records_lines | 0.1607 | 0.0368 | 147,456 | 7,761 |
215
+ | stockholms_stadsarkiv_blandat_1600_1700_lines | 0.1505 | 0.0379 | 16,137 | 1,794 |
216
+ | stockholms_stadsarkiv_blandat_3_1600_lines | 0.1633 | 0.0400 | 43,142 | 4,794 |
217
+ | stockholms_stadsarkiv_norra_forstadens_kamnarsratt_1600_1700_lines | 0.1755 | 0.0463 | 18,474 | 2,053 |
218
+ | carl_fredrik_pahlmans_resejournaler_lines | 0.1768 | 0.0482 | 7,081 | 787 |
219
+ | stockholms_stadsarkiv_sollentuna_haradsratt_1700_1800_lines | 0.1921 | 0.0505 | 19,096 | 2,122 |
220
+ | stockholms_stadsarkiv_byggningskollegium_1600_lines | 0.2262 | 0.0514 | 3,104 | 345 |
221
+ | ra_enstaka_sidor_lines | 0.1991 | 0.0538 | 5,078 | 565 |
222
+ | trolldomskommissionen_lines | 0.2321 | 0.0600 | 33,498 | 3,722 |
223
+ | stockholms_stadsarkiv_stockholms_domkapitel_1600_lines | 0.2170 | 0.0607 | 11,619 | 1,292 |
224
+ | stockholms_stadsarkiv_botkyrka_kyrkoarkiv_1600_1800_lines | 0.2548 | 0.0627 | 3,617 | 402 |
225
+ | gota_hovratt_lines | 0.2450 | 0.0630 | 2,421 | 269 |
226
+ | bergmastaren_i_nora_htr_lines | 0.2558 | 0.0709 | 7,916 | 880 |
227
+ | bergskollegium_advokatfiskalkontoret_lines | 0.2906 | 0.0722 | 2,411 | 268 |
228
+ | jl_fornsallskap_jamtlands_domsaga_lines | 0.2585 | 0.0732 | 60,544 | 6,728 |
229
+ | alvsborgs_losen_lines | 0.1896 | 0.0806 | 5,632 | 626 |
230
+ | jonkopings_radhusratt_och_magistrat_lines | 0.2864 | 0.0853 | 1,179 | 131 |
231
+ | national_archives_finland_letters_recipes_lines | 0.3857 | 0.1360 | 651 | 163 |
232
+
233
+
234
+
235
+ ### Testing Data
236
+
237
+ #### Out-of-Domain Test Data (Sorted by CER)
238
+
239
+ These are all test-sets taken from archives that we're not at all included in the training data. So these are the results one would expect if one uses this model out-of-the-box on just any running text document within the models time-span. The entire test-suite is available here: [test-suite for htr](https://huggingface.co/datasets/Riksarkivet/eval_htr_out_of_domain_lines)
240
+
241
+ | Dataset | WER | CER | Eval Lines |
242
+ |---------|-----|-----|-----------------|
243
+ | 1792_R0002231_eval_lines | 0.1190 | 0.0250 | 501 |
244
+ | 1794-1795_A0068546_eval_lines | 0.1503 | 0.0303 | 510 |
245
+ | 1775-1786_A0068551_eval_lines | 0.2203 | 0.0543 | 525 |
246
+ | 1841_Z0000017_eval_lines | 0.2247 | 0.0555 | 470 |
247
+ | 1690_A0066756_eval_lines | 0.2571 | 0.0611 | 249 |
248
+ | 1716_A0017151_eval_lines | 0.2517 | 0.0650 | 558 |
249
+ | 1824_H0000743_eval_lines | 0.2684 | 0.0674 | 260 |
250
+ | 1699-1700_C0113233_eval_lines | 0.2713 | 0.0691 | 394 |
251
+ | 1845-1857_B0000011_eval_lines | 0.2546 | 0.0706 | 153 |
252
+ | 1812_A0069332_eval_lines | 0.2868 | 0.0793 | 69 |
253
+ | 1659-1674_R0000568_eval_lines | 0.3278 | 0.0886 | 304 |
254
+ | 1755-1756_C0112394_eval_lines | 0.3440 | 0.0918 | 248 |
255
+ | 1723_H0000374_eval_lines | 0.3105 | 0.1140 | 378 |
256
+ | 1887-1892_A0002409_eval_lines | 0.3670 | 0.1297 | 784 |
257
+ | 1679_R0002397_eval_lines | 0.4768 | 0.1422 | 88 |
258
+ | 1800_C0101725_eval_lines | 0.4459 | 0.1767 | 37 |
259
+ | 1871_K0017448_eval_lines | 0.4504 | 0.1945 | 331 |
260
+ | 1654_R0001308_eval_lines | 0.5200 | 0.2179 | 199 |
261
+
262
+ #### Metrics
263
+
264
+ ## Character Error Rate (CER)
265
+
266
+ Character Error Rate (CER) is a metric used to evaluate the performance of a Handwritten Text Recognition (HTR) system by comparing the recognized text to the reference (ground truth) text at the character level.
267
+
268
+ The CER is calculated using the following formula:
269
+
270
+ $$
271
+ CER = \frac{S + D + I}{N}
272
+ $$
273
+
274
+ Where:
275
+ - \( S \) = Number of substitutions (incorrect characters)
276
+ - \( D \) = Number of deletions (missing characters)
277
+ - \( I \) = Number of insertions (extra characters)
278
+ - \( N \) = Total number of characters in the reference text
279
+
280
+ A lower CER indicates better recognition accuracy.
281
+
282
+ ## Word Error Rate (WER)
283
+
284
+ Word Error Rate (WER) is a metric used to assess the accuracy of an HTR system at the word level by comparing the recognized text to the reference text.
285
+
286
+ The WER is calculated using the following formula:
287
+
288
+ $$
289
+ WER = \frac{S + D + I}{N}
290
+ $$
291
+
292
+ Where:
293
+ - \( S \) = Number of substitutions (incorrect words)
294
+ - \( D \) = Number of deletions (missing words)
295
+ - \( I \) = Number of insertions (extra words)
296
+ - \( N \) = Total number of words in the reference text
297
+
298
+ Similar to CER, a lower WER indicates better word-level accuracy.
299
+
300
+
301
+ ## Technical Specifications
302
+
303
+ ### Model Architecture
304
+
305
+ See config.json at [model repo](https://huggingface.co/Riksarkivet/trocr-base-handwritten-hist-swe-2/tree/main)
306
+
307
+ ## Citation
308
+
309
+ [TrOCR paper](https://arxiv.org/abs/2109.10282)
310
+
311
+ <!-- If there is a paper or blog post introducing the model, the APA and Bibtex information for that should go in this section. -->