def display_batch(X_batch, y_batch): ''' This function displays on a window multiple images with their corresponding labels :param X_batch: Must be the batch image samples :param y_batch: Must be the batch categorical labels Returns the figure and the axis of the graph ''' from dataset import CaptchaDataset dataset = CaptchaDataset() texts = dataset.labels_to_text(y_batch.argmax(axis=2)) n = X_batch.shape[0] # Number of column subplots per row cols = ceil(sqrt(n)) # Number of rows rows = n // cols if n % cols > 0: rows += 1 # Create rows x cols subplots fig, ax = plt.subplots(rows, cols, figsize=(8, 8)) for i in range(0, rows): for j in range(0, cols): if i < rows - 1 or n % cols == 0 or j < n % cols: index = i * cols + j plt.sca(ax[i, j]) plt.imshow(X_batch[index, :, :, 0] * 255, cmap='gray') plt.xticks([]) plt.yticks([]) title = b''.join(texts[index]).decode() plt.title(title) else: ax[i, j].set_visible(False) plt.tight_layout() plt.show() return fig, ax
dataset = CaptchaDataset() X, y = dataset.X, dataset.y # Build the model model = OCRModel() # The next lines will show a bunch of captcha images & the predictions made by # the model indices = np.random.choice(np.arange(0, dataset.num_samples), size=9) X_batch, y_batch = next(iter(InputFlow(X, y, batch_size=9))) # Predict texts inside images texts = [ ''.join([char.item().decode() for char in text]) for text in dataset.labels_to_text(y_batch.argmax(axis=2)) ] texts_pred = [ ''.join([char.item().decode() for char in text]) for text in model.predict_text(X_batch) ] rows, cols = X_batch.shape[0] // 3, 3 fig, ax = plt.subplots(rows, cols, figsize=(20, 10)) for i in range(0, rows): for j in range(0, cols): k = i * cols + j plt.sca(ax[i, j]) plt.imshow(X_batch[k, :, :, 0], cmap='gray')