Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 2fcfe24

Browse files
tensorflow2-suitability working
1 parent 4b652d9 commit 2fcfe24

File tree

82 files changed

+21716
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

82 files changed

+21716
-0
lines changed

‎src_tensorflow1/DataLoader.py‎

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
'''
2+
3+
This is the part of https://github.com/githubharald/SimpleHTR with simple modification
4+
See License.
5+
'''
6+
7+
from __future__ import division
8+
from __future__ import print_function
9+
10+
11+
import random
12+
import os
13+
import cv2
14+
import numpy as np
15+
16+
from SamplePreprocessor import preprocessor
17+
18+
19+
class FilePaths:
20+
""" Filenames and paths to data """
21+
fnCharList = '../model/charList.txt'
22+
fnWordCharList = '../model/wordCharList.txt'
23+
fnCorpus = '../data/corpus.txt'
24+
fnAccuracy = '../model/accuracy.txt'
25+
fnTrain = '../data/'
26+
fnInfer = '../data/testImage1.png' ## path to recognize the single image
27+
28+
29+
class Sample:
30+
""" Sample from the dataset """
31+
32+
def __init__(self, gtText, filePath):
33+
self.gtText = gtText
34+
self.filePath = filePath
35+
36+
37+
class Batch:
38+
""" Batch containing images and ground truth texts """
39+
40+
def __init__(self, gtTexts, imgs):
41+
self.imgs = np.stack(imgs, axis=0)
42+
self.gtTexts = gtTexts
43+
44+
45+
class DataLoader:
46+
"loads data which corresponds to IAM format, see: http://www.fki.inf.unibe.ch/databases/iam-handwriting-database"
47+
48+
def __init__(self, filePath, batchSize, imgSize, maxTextLen, load_aug=True):
49+
"loader for dataset at given location, preprocess images and text according to parameters"
50+
51+
assert filePath[-1] == '/'
52+
53+
self.dataAugmentation = True # False
54+
self.currIdx = 0
55+
self.batchSize = batchSize
56+
self.imgSize = imgSize
57+
self.samples = []
58+
59+
f = open("../data/" + 'lines.txt')
60+
chars = set()
61+
bad_samples = []
62+
bad_samples_reference = ['a01-117-05-02.png', 'r06-022-03-05.png']
63+
for line in f:
64+
# ignore comment line
65+
if not line or line[0] == '#':
66+
continue
67+
68+
lineSplit = line.strip().split(' ') ## remove the space and split with ' '
69+
# assert len(lineSplit) >= 9
70+
71+
# filename: part1-part2-part3 --> part1/part1-part2/part1-part2-part3.png
72+
fileNameSplit = lineSplit[0].split('-')
73+
#print(fileNameSplit)
74+
fileName = filePath + 'lines/' + fileNameSplit[0] + '/' + fileNameSplit[0] + '-' + fileNameSplit[1] + '/' +\
75+
lineSplit[0] + '.png'
76+
77+
# GT text are columns starting at 10
78+
# see the lines.txt and check where the GT text starts, in this case it is 10
79+
gtText_list = lineSplit[9].split('|')
80+
gtText = self.truncateLabel(' '.join(gtText_list), maxTextLen)
81+
chars = chars.union(set(list(gtText))) ## taking the unique characters present
82+
83+
# check if image is not empty
84+
if not os.path.getsize(fileName):
85+
bad_samples.append(lineSplit[0] + '.png')
86+
continue
87+
88+
# put sample into list
89+
self.samples.append(Sample(gtText, fileName))
90+
91+
92+
# some images in the IAM dataset are known to be damaged, don't show warning for them
93+
if set(bad_samples) != set(bad_samples_reference):
94+
print("Warning, damaged images found:", bad_samples)
95+
print("Damaged images expected:", bad_samples_reference)
96+
97+
# split into training and validation set: 95% - 10%
98+
splitIdx = int(0.95 * len(self.samples))
99+
self.trainSamples = self.samples[:splitIdx]
100+
self.validationSamples = self.samples[splitIdx:]
101+
print("Train: {}, Validation: {}".format(len(self.trainSamples), len(self.validationSamples)))
102+
# put lines into lists
103+
self.trainLines = [x.gtText for x in self.trainSamples]
104+
self.validationLines = [x.gtText for x in self.validationSamples]
105+
106+
# number of randomly chosen samples per epoch for training
107+
self.numTrainSamplesPerEpoch = 9500
108+
109+
# start with train set
110+
self.trainSet()
111+
112+
# list of all chars in dataset
113+
self.charList = sorted(list(chars))
114+
115+
def truncateLabel(self, text, maxTextLen):
116+
# ctc_loss can't compute loss if it cannot find a mapping between text label and input
117+
# labels. Repeat letters cost double because of the blank symbol needing to be inserted.
118+
# If a too-long label is provided, ctc_loss returns an infinite gradient
119+
cost = 0
120+
for i in range(len(text)):
121+
if i != 0 and text[i] == text[i - 1]:
122+
cost += 2
123+
else:
124+
cost += 1
125+
if cost > maxTextLen:
126+
return text[:i]
127+
return text
128+
129+
130+
def trainSet(self):
131+
"switch to randomly chosen subset of training set"
132+
self.dataAugmentation = True
133+
self.currIdx = 0
134+
random.shuffle(self.trainSamples) # shuffle the samples in each epoch
135+
self.samples = self.trainSamples #[:self.numTrainSamplesPerEpoch]
136+
137+
def validationSet(self):
138+
"switch to validation set"
139+
self.dataAugmentation = False
140+
self.currIdx = 0
141+
self.samples = self.validationSamples
142+
143+
def getIteratorInfo(self):
144+
"current batch index and overall number of batches"
145+
return (self.currIdx // self.batchSize + 1, len(self.samples) // self.batchSize)
146+
147+
def hasNext(self):
148+
"iterator"
149+
return self.currIdx + self.batchSize <= len(self.samples)
150+
151+
def getNext(self):
152+
"iterator"
153+
batchRange = range(self.currIdx, self.currIdx + self.batchSize)
154+
gtTexts = [self.samples[i].gtText for i in batchRange]
155+
imgs = [preprocessor(cv2.imread(self.samples[i].filePath, cv2.IMREAD_GRAYSCALE), self.imgSize)
156+
for i in batchRange]
157+
self.currIdx += self.batchSize
158+
return Batch(gtTexts, imgs)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /