作为一种解决方案,我建议您使用
tf.image.extract_patches
函数从图像中提取面片,并对每个面片应用经过训练的分类器。这有几个好处:
-
你会得到一个密集的响应图,你可以进一步处理,以准确地确定字母出现在整个图像中的位置。
-
由于这是一个内置的TensorFlow Op,您可以通过将所有这些作为单个Keras模型实现和运行来简化流程,从而利用批处理以及加快CPU/GPU处理的优势。
以下是解决方案的示意图:
import tensorflow as tf
from tensorflow.keras.layers import Input, Reshape, TimeDistributed
whole_images = Input(shape=(img_rows, img_cols, 1))
patches = tf.image.extract_patches(
whole_images,
sizes=[1, 25, 25, 1],
strides=[1, 1, 1, 1],
rates=[1, 1, 1, 1],
padding='SAME'
)
reshaped_patches = Reshape((-1, 25, 25, 1))(patches)
dense_map = TimeDistributed(letter_classifier)(reshaped_patches)
dense_map = Reshape(tf.shape(patches)[1:-1])(dense_map)
image_classifier = Model(whole_images, dense_map)
output = image_classifier(my_images)