Skip to content

How can I use pretrained weights to predict #145

Description

@engrsharoonhoney

Code:
import cv2
import numpy as np
import LipNet
from tensorflow.keras.models import load_model

lipnet = LipNet()

videos_path = './data/s1/'
lipnet.model.load_weights("unseen-weights178.h5", by_name=True, skip_mismatch=True)

def process_video(video_path):
cap = cv2.VideoCapture(video_path)
frames = []

while cap.isOpened():
    ret, frame = cap.read()
    if not ret:
        break
   
    gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
   
    resized_frame = cv2.resize(gray_frame, (100, 50))
   
    normalized_frame = resized_frame.astype(np.float32) / 255.0
   
    final_frame = np.expand_dims(normalized_frame, axis=-1)
   
    frames.append(final_frame)

cap.release()

frames_array = np.array(frames)

target_frames = 75
if frames_array.shape[0] < target_frames:
    padding = np.zeros((target_frames - frames_array.shape[0], 50, 100, 1), dtype=np.float32)
    frames_array = np.concatenate((frames_array, padding), axis=0)
elif frames_array.shape[0] > target_frames:
    frames_array = frames_array[:target_frames]

return np.expand_dims(frames_array, axis=0)

video_path = videos_path+"srbo2n.mpg"
video_frames = process_video(video_path)

print("Video Frames: ",video_frames.shape)

sentence_length = np.array([[video_frames.shape[1]]])
output_labels = np.zeros((1, 32)) # Placeholder label sequence
label_length = np.array([[32]]) # Placeholder label length

inputs = [video_frames, sentence_length, output_labels, label_length]

predicted_text = lipnet.model.predict_on_batch(inputs)

print("Predicted Speech:", predicted_text)

#------------------- Error ---------------------------------

Error:

Video Frames: (1, 75, 50, 100, 1)

ValueError Traceback (most recent call last)
Cell In[179], line 72
69 inputs = [video_frames, sentence_length, output_labels, label_length]
71 # Predict text from lip movements
---> 72 predicted_text = lipnet.model.predict_on_batch(inputs)
76 # Check the correct shape before passing to the model
77 # print("Final Input Shape:", video_frames.shape) # Should print (1, 75, 50, 100, 1)
78
79 # Predict text from lip movements
80 # predicted_text = lipnet.model.predict(video_frames)
81 print("Predicted Speech:", predicted_text)

File ~/.config/jupyterlab-desktop/jlab_server/lib/python3.12/site-packages/keras/src/backend/tensorflow/trainer.py:631, in TensorFlowTrainer.predict_on_batch(self, x)
629 def predict_on_batch(self, x):
630 self.make_predict_function()
--> 631 batch_outputs = self.predict_function([(x,)])
632 batch_outputs = tree.map_structure(
633 convert_to_np_if_not_ragged, batch_outputs
634 )
635 return batch_outputs

File ~/.config/jupyterlab-desktop/jlab_server/lib/python3.12/site-packages/tensorflow/python/util/traceback_utils.py:153, in filter_traceback..error_handler(*args, **kwargs)
151 except Exception as e:
152 filtered_tb = _process_traceback_frames(e.traceback)
--> 153 raise e.with_traceback(filtered_tb) from None
154 finally:
155 del filtered_tb

File ~/.config/jupyterlab-desktop/jlab_server/lib/python3.12/site-packages/keras/src/backend/tensorflow/trainer.py:259, in TensorFlowTrainer.make_predict_function..one_step_on_data_distributed(data)
256 @tf.autograph.experimental.do_not_convert
257 def one_step_on_data_distributed(data):
258 data = data[0]
--> 259 outputs = self.distribute_strategy.run(
260 one_step_on_data, args=(data,)
261 )
262 outputs = reduce_per_replica(
263 outputs,
264 self.distribute_strategy,
265 reduction="concat",
266 )
267 return outputs

File ~/.config/jupyterlab-desktop/jlab_server/lib/python3.12/site-packages/keras/src/backend/tensorflow/trainer.py:249, in TensorFlowTrainer.make_predict_function..one_step_on_data(data)
246 @tf.autograph.experimental.do_not_convert
247 def one_step_on_data(data):
248 """Runs a predict test step on a batch of data."""
--> 249 return self.predict_step(data)

File ~/.config/jupyterlab-desktop/jlab_server/lib/python3.12/site-packages/keras/src/backend/tensorflow/trainer.py:104, in TensorFlowTrainer.predict_step(self, data)
102 x, _, _ = data_adapter_utils.unpack_x_y_sample_weight(data)
103 if self._call_has_training_arg:
--> 104 y_pred = self(x, training=False)
105 else:
106 y_pred = self(x)

File ~/.config/jupyterlab-desktop/jlab_server/lib/python3.12/site-packages/keras/src/utils/traceback_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

File ~/.config/jupyterlab-desktop/jlab_server/lib/python3.12/site-packages/keras/src/layers/input_spec.py:245, in assert_input_compatibility(input_spec, inputs, layer_name)
243 if spec_dim is not None and dim is not None:
244 if spec_dim != dim:
--> 245 raise ValueError(
246 f'Input {input_index} of layer "{layer_name}" is '
247 "incompatible with the layer: "
248 f"expected shape={spec.shape}, "
249 f"found shape={shape}"
250 )

ValueError: Input 0 of layer "functional_63" is incompatible with the layer: expected shape=(None, 75, 100, 50, 3), found shape=(1, 75, 50, 100)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions