[tag: Load and preprocess images]
images = []
for image_path in image_paths:
img = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
img = cv2.resize(img, (128, 32))
images.append(img)
images = np.array(images) / 255.0
images = np.expand_dims(images, axis=-1) ** # Add channel dimension for grayscale images**
label_set = list(set(labels)) # Convert labels to a set and then a list
label_to_index = {label: index for index, label in enumerate(label_set)}
labels = [label_to_index[label] for label in labels]
[tag: Convert labels to one-hot encoding]
max_label_len = max([len(str(label)) for label in labels]) # Adjust this as per your needs
labels_padded = np.zeros((len(labels), max_label_len))
for i, label in enumerate(labels):
labels_padded[i, :len(str(label))] = list(str(label))
[tag: input format]
input_img = Input(shape=(32, 128, 1), name='image_input')
labels_input = Input(shape=(max_label_len,), name='label_input')
input_length = Input(shape=(1,), name='input_length')
label_length = Input(shape=(1,), name='label_length')
above code is the input for CNN layers ,I need improvement in that section
[tag:CNN layers]
x = Conv2D(32, (3, 3), activation='relu', padding='same')(input_img)
x = MaxPooling2D((2, 2))(x)
x = Conv2D(64, (3, 3), activation='relu', padding='same')(x)
x = MaxPooling2D((2, 2))(x)
```This code giving me error at the time of training the dataset so I need to make some changes in the above code ```
new_shape = (8, 2048) # Ensure the reshaped dimensions match the total size of the tensor
x = Reshape(target_shape=new_shape)(x) # Adjust shape to match the output tensor size
x = Bidirectional(LSTM(128, return_sequences=True))(x)
x = Bidirectional(LSTM(128, return_sequences=True))(x)
y_pred = Dense(len(label_set) + 1, activation='softmax', name='y_pred')(x) # +1 for CTC blank label
#Dense layer with softmax activation
def ctc_lambda_func(args):
y_true, y_pred, input_length, label_length = args
return tf.keras.backend.ctc_batch_cost(y_true, y_pred, input_length, label_length)
After Using above code getting following error
`—————————————————————————
ValueError Traceback (most recent call last)
Cell In[23], line 5
2 train_outputs = {‘ctc’: np.zeros((len(images),))}
4 # Train the model
—-> 5 ctc_model.fit(train_inputs, train_outputs, epochs=10, batch_size=32)
6 # Dummy data for CTC
7 train_outputs = {‘ctc’: np.zeros((len(images),))}
File ~AppDataLocalProgramsPythonPython312Libsite-packageskerassrcutilstraceback_utils.py:122, in filter_traceback..error_handler(*args, **kwargs)
119 filtered_tb = _process_traceback_frames(e.traceback)
120 # To get the full stack trace, call:
121 # keras.config.disable_traceback_filtering()
–> 122 raise e.with_traceback(filtered_tb) from None
123 finally:
124 del filtered_tb
Cell In[18], line 4, in ctc_lambda_func(args)
2 def ctc_lambda_func(args):
3 y_true, y_pred, input_length, label_length = args
—-> 4 return tf.keras.backend.ctc_batch_cost(y_true, y_pred, input_length, label_length)
ValueError: Exception encountered when calling Lambda.call().
Input tensor functional_1_1/ctc_1/Cast_2:0
enters the loop with shape (1, 1), but has shape (1, None) after one iteration. To allow the shape to vary across iterations, use the shape_invariants
argument of tf.while_loop to specify a less-specific shape.
Arguments received by Lambda.call():
• inputs=[‘tf.Tensor(shape=(None, 1), dtype=float32)’, ‘tf.Tensor(shape=(None, 8, 4), dtype=float32)’, ‘tf.Tensor(shape=(None, 1), dtype=float32)’, ‘tf.Tensor(shape=(None, 1), dtype=float32)’]
• mask=[‘None’, ‘None’, ‘None’, ‘None’]
• training=True`
Kedar Shinge is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.