text stringlengths 0 4.99k |
|---|
label = tf.strings.reduce_join(num_to_char(indices)) |
label = label.numpy().decode(\"utf-8\") |
ax[i // 4, i % 4].imshow(img, cmap=\"gray\") |
ax[i // 4, i % 4].set_title(label) |
ax[i // 4, i % 4].axis(\"off\") |
plt.show() |
png |
You will notice that the content of original image is kept as faithful as possible and has been padded accordingly. |
Model |
Our model will use the CTC loss as an endpoint layer. For a detailed understanding of the CTC loss, refer to this post. |
class CTCLayer(keras.layers.Layer): |
def __init__(self, name=None): |
super().__init__(name=name) |
self.loss_fn = keras.backend.ctc_batch_cost |
def call(self, y_true, y_pred): |
batch_len = tf.cast(tf.shape(y_true)[0], dtype=\"int64\") |
input_length = tf.cast(tf.shape(y_pred)[1], dtype=\"int64\") |
label_length = tf.cast(tf.shape(y_true)[1], dtype=\"int64\") |
input_length = input_length * tf.ones(shape=(batch_len, 1), dtype=\"int64\") |
label_length = label_length * tf.ones(shape=(batch_len, 1), dtype=\"int64\") |
loss = self.loss_fn(y_true, y_pred, input_length, label_length) |
self.add_loss(loss) |
# At test time, just return the computed predictions. |
return y_pred |
def build_model(): |
# Inputs to the model |
input_img = keras.Input(shape=(image_width, image_height, 1), name=\"image\") |
labels = keras.layers.Input(name=\"label\", shape=(None,)) |
# First conv block. |
x = keras.layers.Conv2D( |
32, |
(3, 3), |
activation=\"relu\", |
kernel_initializer=\"he_normal\", |
padding=\"same\", |
name=\"Conv1\", |
)(input_img) |
x = keras.layers.MaxPooling2D((2, 2), name=\"pool1\")(x) |
# Second conv block. |
x = keras.layers.Conv2D( |
64, |
(3, 3), |
activation=\"relu\", |
kernel_initializer=\"he_normal\", |
padding=\"same\", |
name=\"Conv2\", |
)(x) |
x = keras.layers.MaxPooling2D((2, 2), name=\"pool2\")(x) |
# We have used two max pool with pool size and strides 2. |
# Hence, downsampled feature maps are 4x smaller. The number of |
# filters in the last layer is 64. Reshape accordingly before |
# passing the output to the RNN part of the model. |
new_shape = ((image_width // 4), (image_height // 4) * 64) |
x = keras.layers.Reshape(target_shape=new_shape, name=\"reshape\")(x) |
x = keras.layers.Dense(64, activation=\"relu\", name=\"dense1\")(x) |
x = keras.layers.Dropout(0.2)(x) |
# RNNs. |
x = keras.layers.Bidirectional( |
keras.layers.LSTM(128, return_sequences=True, dropout=0.25) |
)(x) |
x = keras.layers.Bidirectional( |
keras.layers.LSTM(64, return_sequences=True, dropout=0.25) |
)(x) |
# +2 is to account for the two special tokens introduced by the CTC loss. |
# The recommendation comes here: https://git.io/J0eXP. |
x = keras.layers.Dense( |
len(char_to_num.get_vocabulary()) + 2, activation=\"softmax\", name=\"dense2\" |
)(x) |
# Add CTC layer for calculating CTC loss at each step. |
output = CTCLayer(name=\"ctc_loss\")(labels, x) |
# Define the model. |
model = keras.models.Model( |
inputs=[input_img, labels], outputs=output, name=\"handwriting_recognizer\" |
) |
# Optimizer. |
opt = keras.optimizers.Adam() |
# Compile the model and return. |
model.compile(optimizer=opt) |
return model |
# Get the model. |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.