CRNN OCR, issue with dynamic width of images. How to tackle and resolve this ?. Need to train on images with different width

I am creating a ocr for urdu. I bucket my data into n number of buckets based on their widths. means all images whose widths are less than 400px are all reshaped to have width of 400px and are stored into a bucket.

The code is previously done and is located on github at, urdu-handwriting-recognition-using-deep-learning.
I want to use the same architecture in this repository coded in keras with tensorflow 2 but can not figure out how this code is handling the dynamic width.

I want the code in tensorflow 2 keras, as the previous version is written in tf1.<>.

I donot want to pad all images, as if max width is 1000 and I pad an image with 600 white pixels it may effect the results and performance. My sequences are padded with 999 for empty space.

Below is how I am creating it. Issue is that if width of image is small, the timestamps gets too small that they are much smaller that the ground truth maximum length.

import tensorflow as tf
from keras.layers import Input, Conv2D, MaxPooling2D, Flatten, Reshape, Dense, Bidirectional, LSTM, Dropout, BatchNormalization

from keras.models import Model


import tensorflow as tf


def build_cnn(input_shape, num_of_characters):
    # Input layer
    inputs = Input(shape=input_shape)

    # Convolutional layers
    x = Conv2D(filters=32, kernel_size=(5, 5), strides=(1, 1), padding='same', activation='relu')(inputs)
    x = MaxPooling2D(pool_size=(2, 2), strides=(2, 2), padding='same')(x)

    x = Conv2D(filters=64, kernel_size=(5, 5), strides=(1, 2), padding='same', activation='relu')(x)
    x = MaxPooling2D(pool_size=(1, 2), strides=(1, 2), padding='same')(x)

    x = Conv2D(filters=128, kernel_size=(5, 5), strides=(1, 2), padding='same', activation='relu')(x)
    x = MaxPooling2D(pool_size=(1, 2), strides=(1, 2), padding='same')(x)

    x = Conv2D(filters=128, kernel_size=(5, 5), strides=(1, 2), padding='same', activation='relu')(x)
    x = MaxPooling2D(pool_size=(1, 2), strides=(1, 2), padding='same')(x)

    x = Conv2D(filters=256, kernel_size=(3, 3), strides=(1, 2), padding='same', activation='relu')(x)
    x = MaxPooling2D(pool_size=(1, 2), strides=(1, 2), padding='same')(x)

    x = Conv2D(filters=256, kernel_size=(3, 3), strides=(1, 2), padding='same', activation='relu')(x)
    x = MaxPooling2D(pool_size=(1, 2), strides=(1, 2), padding='same')(x)

    x = Conv2D(filters=512, kernel_size=(3, 3), strides=(1, 1), padding='same', activation='relu')(x)
    x = MaxPooling2D(pool_size=(1, 1), strides=(1, 1), padding='same')(x)

    print(f"Shape of x : {x.shape}")

    # Get dynamic shape
    batch_size = x.shape[0]
    time_stamps = x.shape[2]  # new width
    features = x.shape[1] * x.shape[3]  # new height * channels (depth)

    # Reshape for LSTM
    x = Reshape((time_stamps, features))(x)

    return inputs, x



def build_rnn(inputs, x):
  # Bidirectional LSTM layers
  x = Bidirectional(LSTM(units=512, return_sequences=True))(x)
  x = Bidirectional(LSTM(units=512, return_sequences=True))(x)

  # Dropout layer
  x = Dropout(0.2)(x)

  # Output layer
  outputs = Dense(num_of_characters, activation='softmax', name="Output_Layer")(x)

  # Define model
  model = Model(inputs=inputs, outputs=outputs)

  return model

def build_model(input_shape, num_of_characters):

  cnn_inputs, cnn_out = build_cnn(input_shape, num_of_characters)
  rnn_out = build_rnn(cnn_inputs, cnn_out)


  return rnn_out


# Example usage
input_shape = (64,5000, 1)  # Example input shape (batchsize, height, width, channels)
num_of_characters = 200  # Number of output classes
model = build_model(input_shape=input_shape, num_of_characters=num_of_characters)
model.summary()

What I want is whatever is the width of the image, My code adjusts it and can process and then calculate the cer and levenshtien accuracy too.

In short I want to train an OCR model on dynamic width images, the height can be same. If there is any approach that the height and width can alter too. Kinldy share that too.

I tried to rehsape the output of the cnn to ( batch size, timestamps(image width after passing through CNN), (height * depth) ) new height and depth after passing through CNN. depth is always 512.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật