metal3d / keras-video-generators

Keras generators to generate sequences from videos as input
MIT License
99 stars 32 forks source link

Error: Frame count is not OK for video #36

Open orenpapers opened 3 years ago

orenpapers commented 3 years ago

I have the following code that is based on the example: And I get the error:

Epoch 1/3
21/69 [========>.....................] - ETA: 54:24 - loss: 1.5353Frame count is not OK for video /content/drive/My Drive/videos/dataset/A/video7.avi, 2 total, 1 extracted

while fitting. Any idea why? Thanks!

sequence_lengths = None

numberOfVideos = 10
videoLength = 5
numberOfPrediction = 5

def defModel():

  frames, channels, rows, columns = 5,3,224,224

  video = Input(shape=(frames,
                    rows,
                    columns,
                    channels))

  cnn_base = VGG16(
                # input_shape=(rows,columns,channels),
                weights="imagenet",
                include_top=True)
  cnn_base.trainable = False

  cnn = Model(cnn_base.input, cnn_base.layers[-3].output, name="VGG_fm")

  encoded_frames = TimeDistributed(cnn , name = "encoded_frames")(video)
  encoded_sequence = LSTM(4, name = "encoded_seqeunce")(encoded_frames)
  hidden_layer = Dense(4, activation="relu" , name = "hidden_layer")(encoded_sequence)
  outputs = Dense(numberOfPrediction, activation="softmax")(hidden_layer)

  model = Model([video], outputs)  
  model.compile(loss='categorical_crossentropy', optimizer='adam')
  model.summary()

  return model

model = defModel()

import os
import glob
from keras_video import VideoFrameGenerator

classes = ["A","B","C","D","E"]
classes.sort()
 SIZE = (224, 224)
CHANNELS = 3

# pattern to get videos and classes
glob_pattern='/content/drive/videos/dataset/{classname}/*.avi'

data_aug = tensorflow.keras.preprocessing.image.ImageDataGenerator(
    zoom_range=.1,
    horizontal_flip=True,
    rotation_range=8,
    width_shift_range=.2,
    height_shift_range=.2)

train = VideoFrameGenerator(
    classes=classes, 
    glob_pattern=glob_pattern,
    split_val=.33, 
    shuffle=True,
    target_shape=SIZE,
    nb_channel=CHANNELS,
    transformation=data_aug,
    use_frame_cache=True)

valid = train.get_validation_generator()

EPOCHS=3

callbacks = [
    keras.callbacks.ReduceLROnPlateau(verbose=1),
    keras.callbacks.ModelCheckpoint(
        'chkp/weights.{epoch:02d}-{val_loss:.2f}.hdf5',
        verbose=1),
]
model.fit_generator(
    train,
    validation_data=valid,
    verbose=1,
    epochs=EPOCHS,
    # callbacks=callbacks
)
LS4203 commented 3 years ago

This basically means the video does not contain those amount of frames. Navigate to that specific video file and you will find it out it does not have those frames. Usually, the video is either short or corrupt. Highly, likely corrupt! Just delete that video and the error will go away.

metal3d commented 3 years ago

That's a problem I want to resolve