2

I am running my code on google colabs but my sequential model is not compiling. I could not create a new Sequential mode from a pre-existing model and compile it. I have called compile function from model but I have no idea why the error pops up. I don't know where I messed up. Actually Im doing something different but this is a simplied code with intention to find error.

train_path = '/content/drive/My Drive/ML/train'
model_custom=load_model("/content/drive/My Drive/ML/model_after_vgg_back_up.h5")
model=Sequential()
for layer in model_custom.layers:
 model.add(layer)
model.compile(Adam(lr=0.0001),loss='categorical_crossentropy',metrics=['accuracy'])
train_batches = ImageDataGenerator().flow_from_directory(train_path,target_size=[224,224],classes=['mom','nanu','prabesh','sanu'],batch_size=15)
valid_path = '/content/drive/My Drive/ML/test1'
valid_batches = ImageDataGenerator().flow_from_directory(valid_path,target_size=[224,224],classes=['mom','nanu','prabesh','sanu'],batch_size=15)
model.fit_generator(train_batches,validation_data=valid_batches,validation_steps=1,steps_per_epoch=35,verbose=2,epochs=3) 

Traceback is like follows:

Found 569 images belonging to 4 classes.
Found 12 images belonging to 4 classes.
    ---------------------------------------------------------------------------
    RuntimeError                              Traceback (most recent call last)
    <ipython-input-16-c160c47bcd7c> in <module>()
          7 valid_path = '/content/drive/My Drive/ML/test1'
          8 valid_batches = ImageDataGenerator().flow_from_directory(valid_path,target_size=[224,224],classes=['mom','nanu','prabesh','sanu'],batch_size=15)
    ----> 9 model.fit_generator(train_batches,validation_data=valid_batches,validation_steps=1,steps_per_epoch=35,verbose=2,epochs=3)

    /usr/local/lib/python3.6/dist-packages/keras/legacy/interfaces.py in wrapper(*args, **kwargs)
         89                 warnings.warn('Update your `' + object_name + '` call to the ' +
         90                               'Keras 2 API: ' + signature, stacklevel=2)
    ---> 91             return func(*args, **kwargs)
         92         wrapper._original_function = func
         93         return wrapper

    /usr/local/lib/python3.6/dist-packages/keras/engine/training.py in fit_generator(self, generator, steps_per_epoch, epochs, verbose, callbacks, validation_data, validation_steps, class_weight, max_queue_size, workers, use_multiprocessing, shuffle, initial_epoch)
       1416             use_multiprocessing=use_multiprocessing,
       1417             shuffle=shuffle,
    -> 1418             initial_epoch=initial_epoch)
       1419 
       1420     @interfaces.legacy_generator_methods_support

    /usr/local/lib/python3.6/dist-packages/keras/engine/training_generator.py in fit_generator(model, generator, steps_per_epoch, epochs, verbose, callbacks, validation_data, validation_steps, class_weight, max_queue_size, workers, use_multiprocessing, shuffle, initial_epoch)
         38 
         39     do_validation = bool(validation_data)
    ---> 40     model._make_train_function()
         41     if do_validation:
         42         model._make_test_function()

    /usr/local/lib/python3.6/dist-packages/keras/engine/training.py in _make_train_function(self)
        494     def _make_train_function(self):
        495         if not hasattr(self, 'train_function'):
    --> 496             raise RuntimeError('You must compile your model before using it.')
        497         self._check_trainable_weights_consistency()
        498         if self.train_function is None:

    RuntimeError: You must compile your model before using it.
voice
  • 93
  • 1
  • 1
  • 7

0 Answers0