-
Notifications
You must be signed in to change notification settings - Fork 11
/
hw_utils.py
executable file
·285 lines (224 loc) · 11.4 KB
/
hw_utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
import os
import sys
from PIL import Image
import pandas as pd
import numpy as np
def scale_invert(raw_path, proc_path,height,width):
"""
Función que escala e invierte cada imagen para almacenarlas en un directorio común.
Se conservan las proporciones de la imagen original y se añade un relleno hasta alcanzar
el ancho objetivo.
Argumentos:
- raw_path: Ruta de la imagen original. (String)
- proc_path: Ruta donde almacenar la imagen procesada. (String)
- height: Altura de las imágenes. (Int)
- width: Anchura de la imágenes. (Int)
"""
# Cargamos la imagen
im = Image.open(raw_path)
# Reescalamos
raw_width, raw_height = im.size
new_width = int(round(raw_width * (height / raw_height)))
im = im.resize((new_width, height), Image.NEAREST)
im_map = list(im.getdata())
im_map = np.array(im_map)
im_map = im_map.reshape(height, new_width).astype(np.uint8)
# Rellenamos e invertimos los valores.
data = np.full((height, width - new_width + 1), 255)
im_map = np.concatenate((im_map, data), axis=1)
im_map = im_map[:, 0:width]
im_map = (255 - im_map)
im_map = im_map.astype(np.uint8)
im = Image.fromarray(im_map)
# Almacenamos todas las imágenes en directorio común
im.save(str(proc_path), "png")
print("Processed image saved: " + str(proc_path))
def extract_training_batch(ctc_input_len,batch_size,im_path,csv_path):
"""
Función que extrae un lote de imágenes y sus transcripciones de manera aleatoria para entrenar la ANN.
Argumentos:
- ctc_input_len: Longitud de la secuencia de entrada a la capa CTC. (Int)
- batch_size: Tamaño del lote. (Int)
- im_path: Ruta al directorio donde se almacenan las imágenes. (String)
- csv_path: Ruta al dataset de entrenamiento. (Int)
Salida:
- batchx: Tensor que contiene las imágenes como matrices de entrada a la ANN.
(Array de Floats: [batch_size, height, width, 1])
- sparse: SparseTensor que contiene las etiquetas como valores enteros positivos. (SparseTensor: indice,values,shape)
- transcriptions: Array con las transcripciones correspondientes a las imágenes de "batchx". (Array de Strings: [batch_size])
- seq_len: Array con la longitud de la secuencia de entrada a la capa CTC, "ctc_input_len". (Array de Ints: [batch_size])
"""
# Extraemos aleatoriamente un DataFrame de tamaño "batch_size" del Dataset de entrenamiento.
df = pd.read_csv(csv_path, sep=",",index_col="index")
df_sample=df.sample(batch_size).reset_index()
# Declaramos las variables para la salida.
batchx = []
transcriptions = []
index = []
values=[]
seq_len=[]
# Creamos el lote a partir del Dataframe de muestras aleatorias.
for i in range(batch_size):
im_apt = df_sample.loc[i, ['image']].as_matrix()
df_y =df_sample.loc[i, ['transcription']].as_matrix()
for fich in im_apt:
# Extraemos la imagen y la mapeamos en una matriz normalizada.
fich = str(fich)
fich = fich.replace("['", "").replace("']", "")
im = Image.open(im_path + fich + ".png")
width, height = im.size
im_map = list(im.getdata())
im_map = np.array(im_map)
im_map = im_map / 255
result=im_map.reshape(height, width,1)
batchx.append(result)
# Extraemos las etiquetas parseando la transcripción.
original=""
for n in list(str(df_y)):
if n == n.lower() and n == n.upper():
if n in "0123456789":
values.append(int(n))
original = original + n
elif n == n.lower():
values.append(int(ord(n) - 61))
original = original + n
elif n == n.upper():
values.append(int((ord(n) - 55)))
original = original + n
# Añadimos el indice del SparseTensor.
for j in range(len(str(df_y))-4):
index.append([i,j])
# Añadimos las transcripciones y la longitud de la secuencia de entrada a la CTC.
transcriptions.append(original)
seq_len.append(ctc_input_len)
# Creamos el Array que contiene todas las imagenes normalizadas el lote, entrada de la ANN.
batchx = np.stack(batchx, axis=0)
# Creamos el SparseTensor con el indice, las etiquetas que representan cada caracter y la longitud máxima de palabra
shape=[batch_size,18]
sparse=index,values,shape
return batchx, sparse, transcriptions, seq_len
def extract_ordered_batch(ctc_input_len,batch_size,im_path,csv_path,cont):
"""
Función que extrae un lote de imágenes y sus transcripciones de manera ordenada para validar o testear la ANN.
Argumentos:
- ctc_input_len: Longitud de la secuencia de entrada a la capa CTC. (Int)
- batch_size: Tamaño del lote. (Int)
- im_path: Ruta al directorio donde se almacenan las imágenes. (String)
- csv_path: Ruta al dataset de validación. (Int)
- cont: Índice auxiliar que permite la extracción de lotes de manera ordenada. (Int)
Salida:
- batchx: Tensor que contiene las imágenes como matrices de entrada a la ANN.
(Array de Floats: [batch_size, height, width, 1])
- sparse: SparseTensor que contiene las etiquetas como valores enteros positivos. (SparseTensor: indice,values,shape)
- transcriptions: Array con las transcripciones correspondientes a las imágenes de "batchx". (Array de Strings: [batch_size])
- seq_len: Array con la longitud de la secuencia de entrada a la capa CTC, "ctc_input_len". (Array de Ints: [batch_size])
- num_samples: Número de muestras extraidas. (Int)
"""
# Extraemos secuencialmente un DataFrame de tamaño "batch_size" del Dataset.
df = pd.read_csv(csv_path, sep=",",index_col="index")
df_sample=df.loc[int(cont*batch_size):int((cont+1)*batch_size)-1,:].reset_index()
num_samples=int(len(df_sample.axes[0]))
# Declaramos las variables para la salida.
batchx = []
transcriptions = []
index = []
values=[]
seq_len=[]
# Creamos el lote a partir del Dataframe de muestras aleatorias.
if len(df_sample.axes[0]) is not 0:
for i in range(len(df_sample.axes[0])):
im_apt = df_sample.loc[i, ['image']].as_matrix()
df_y =df_sample.loc[i, ['transcription']].as_matrix()
for fich in im_apt:
# Extraemos la imagen y la mapeamos en una matriz normalizada.
fich = str(fich)
fich = fich.replace("['", "").replace("']", "")
im = Image.open(im_path + fich + ".png")
width, height = im.size
im_map = list(im.getdata())
im_map = np.array(im_map)
im_map = im_map / 255
result=im_map.reshape(height, width,1)
batchx.append(result)
# Extraemos las etiquetas parseando la transcripción.
original=""
for n in list(str(df_y)):
if n == n.lower() and n == n.upper():
if n in "0123456789":
values.append(int(n))
original=original+n
elif n==n.lower():
values.append(int(ord(n)-61))
original = original + n
elif n==n.upper():
values.append(int((ord(n)-55)))
original = original + n
# Añadimos el indice del SparseTensor.
for j in range(len(str(df_y))-4):
index.append([i,j])
# Añadimos las transcripciones y la longitud de la secuencia de entrada a la CTC.
transcriptions.append(original)
seq_len.append(ctc_input_len)
# Creamos el Array que contiene todas las imagenes normalizadas el lote, entrada de la ANN.
batchx=np.stack(batchx, axis=0)
# Creamos el SparseTensor con el indice, las etiquetas que representan cada caracter y la longitud máxima de palabra
shape=[batch_size,18]
sparse=index,values,shape
return batchx, sparse, transcriptions, seq_len, num_samples
def validation(curr_epoch,ctc_input_len, batch_size, im_path, csv_path, inputs, targets, keep_prob, seq_len, session, cost, ler):
"""
Función que realiza la validación de la ANN sobre un dataset concreto.
Argumentos:
- curr_epoch: Época actual. (Int)
- ctc_input_len: Longitud de la secuencia de entrada a la capa CTC. (Int)
- batch_size: Tamaño del lote. (Int)
- im_path: Ruta al directorio donde se almacenan las imágenes. (String)
- csv_path: Ruta al dataset de validación. (Int)
- inputs: Placeholder de la entrada del model. (placeholder)
- targets: Placeholder de las salidas objetivo. (placeholder)
- keep_prob: Placeholder para la probabilidad de dropout. (placeholder)
- seq_len: Placeholder para la longitud de la secuencia de entrada a la capa CTC. (placeholder)
- session: Sesión actual de TensorFlow. (Session)
- cost: Tensor para la salida del error de la CTC. (Tensor: [1])
- ler: Tensor para la salida del LER. (Tensor:[1])
Salida:
- val_tuple: Resultado de la validación del modelo en una época completa. (Tuple: {'epoch','cost','LER'})
"""
# Variables auxiliares.
cont = 0
total_val_cost = 0
total_val_ler = 0
# Bucle para realizar la validación sobre el Dataset completo
while cont >= 0:
# Extraemos los lotes de forma secuencial mediante "cont"
val_inputs, val_targets, val_original, val_seq_len, num_samples = extract_ordered_batch(
ctc_input_len, batch_size, im_path, csv_path, cont)
# Si el número de muestras extraidas el igual a "batch_size" se ha extraido un lote completo.
if num_samples == batch_size:
val_feed = {inputs: val_inputs,
targets: val_targets,
keep_prob: 1,
seq_len: val_seq_len}
val_cost, val_ler = session.run([cost, ler], val_feed)
total_val_cost += val_cost
total_val_ler += val_ler
cont += 1
# No se ha podido extraer ningún lote más y por lo tanto, se calcula la media de "cost" y "ler"
elif num_samples == 0:
val_tuple = {'epoch': [curr_epoch], 'val_cost': [total_val_cost / (cont + 1)],
'val_ler': [total_val_ler / (cont + 1)]}
cont = -1
# No se ha podido extraer el lote completo, no quedan suficientes muestras en el Dataset y por lo tanto,
# se calcula la media de "cost" y "ler".
else:
val_feed = {inputs: val_inputs,
targets: val_targets,
keep_prob: 1,
seq_len: val_seq_len}
val_cost, val_ler = session.run([cost, ler], val_feed)
total_val_cost += val_cost
total_val_ler += val_ler
val_tuple = {'epoch': [curr_epoch], 'val_cost': [total_val_cost / (cont + 1)],
'val_ler': [total_val_ler / (cont + 1)]}
cont = -1
return val_tuple