Moumita Sen Sarma
Moumita Sen Sarma

Reputation: 45

Session Crashed in Google Colab at the begining of first epoch in TimeDistributed CNN model

I'm working with video classification of 5 classes and using TimeDistributed CNN model in Google Colab platform. The train dataset contains 80 videos containing 75 frames each. The validation dataset contains 20 videos containing 75 frames each. So,in total I'm working with 100 videos.The batch size I used is 64. But,at the beginning of first epoch,session gets crashed and ram usage gets full. What should I need to modify to avoid this problem?

model = tf.keras.models.Sequential([

    tf.keras.layers.TimeDistributed(Conv2D(64, (3,3), padding='same', activation='relu'),input_shape=(75,128, 128, 3)),
    tf.keras.layers.TimeDistributed(MaxPooling2D((2, 2))),
    tf.keras.layers.TimeDistributed(Conv2D(64, (3,3), padding='same', activation='relu')),
    tf.keras.layers.TimeDistributed(MaxPooling2D((2, 2))),
    tf.keras.layers.TimeDistributed(Conv2D(128, (3,3), padding='same', activation='relu')),
    tf.keras.layers.TimeDistributed(Conv2D(128, (3,3), padding='same', activation='relu')),
    tf.keras.layers.TimeDistributed(MaxPooling2D((2, 2))),
    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(5, activation='softmax')
])

from tensorflow.keras.optimizers import Adam

model.compile(loss='categorical_crossentropy',
              optimizer=Adam(lr=0.0001),
              metrics=['accuracy'])

history = model.fit_generator(
      train_generator,
      validation_data=validation_generator, 
      validation_steps=1500//64, 
      shuffle=False,    
      steps_per_epoch=8,  
      epochs=5,
      verbose=1)

The message that I get:

WARNING:tensorflow:From <ipython-input-11-c79b34d1df07>:8: Model.fit_generator (from tensorflow.python.keras.engine.training) is deprecated and will be removed in a future version.
Instructions for updating:
Please use Model.fit, which supports generators.
Epoch 1/5

And then,a popup arises and tells me session crashed!! Can anyone help??

Upvotes: 1

Views: 1615

Answers (2)

Ryan Sequeira
Ryan Sequeira

Reputation: 330

You could try a tensorflow's Dataset module. For example, instead of passing a array of images, you pass a list of image path's, the dataset generator at the time of training will only partially load 1 batch of images at a time. This way you won't overwhelm the memory. Here's an example

def preprocess_image(filename):
    """
    Load the specified file as a JPEG image, preprocess it and
    resize it to the target shape.
    """

    image_string = tf.io.read_file(filename)
    image = tf.image.decode_jpeg(image_string, channels=3)
    image = tf.image.convert_image_dtype(image, tf.float32)
    image = tf.image.resize(image, target_shape)
    return image

images = [str(os.path.join(images_path ,f)) for f in os.listdir(images_path)]
dataset = tf.data.Dataset.from_tensor_slices(images)
dataset = dataset.shuffle(buffer_size=1024)
dataset = dataset.map(preprocess_image)

source: https://keras.io/examples/vision/siamese_network/

Upvotes: 0

user13408061
user13408061

Reputation:

Google colab has a very limited ~11 Gb memory for both your data and model. From the details, it's pretty obvious it is a memory issue and not related to model itself.

You can try reduce your dataset so that the memory is not exhausted also add your data processing pipeline for a better look.

Upvotes: 1

Related Questions