Peter
Peter

Reputation: 23

Keras: notImplementedError/RuntimeError when using fit_generator

I am having troubles with keras and tensorflow, using the following code:

from tensorflow.keras.layers import Activation, Conv2D
from tensorflow.keras import Model

from data import DataGenerator
from config import train_datapath, test_datapath


training_generator = DataGenerator(train_datapath)
validation_generator = DataGenerator(test_datapath)

class model(Model):
    def __init__(self):
        super(model, self).__init__()
        self.conv1 = Conv2D(filters=2, kernel_size=1, strides=1, padding='same', input_shape=(256, 256, 1))
        self.act1 = Activation('relu')

    def call(self, input):
        """Run the model."""
        return self.act1(self.conv1(input))

model = model()
model.compile(optimizer='adam', loss='mean_squared_error')
history = model.fit_generator(training_generator, epochs=5000, verbose=1, validation_data=(validation_generator),
                              use_multiprocessing=False)

Running that code gives the following error:

Using TensorFlow backend.
Traceback (most recent call last):
  File "C:/Users/...py", line 23, in <module>
    use_multiprocessing=False)
  File "C:\Users\...\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\keras\engine\training.py", line 2161, in fit_generator
    '`fit_generator` is not yet enabled for unbuilt Model subclasses')
NotImplementedError: `fit_generator` is not yet enabled for unbuilt Model subclasses

I've tried to find some hints, if I really try to do something, that isn't intended, but it's seems unlogical to me, since this way is very comfortable for defining more complex nets and since I've allready worked with pytorch, I'm pretty sure, this should work with tf and keras also.

There is a thread with the same error, but for a sequential net implementation, which ist obviously not, what I'm aiming for.

If I'm using a direct import for keras, I'm getting a different error:

Using TensorFlow backend.
Traceback (most recent call last):
  File "C:/Users/...py", line 23, in <module>
    use_multiprocessing=False)
  File "C:\Users\...\AppData\Local\Programs\Python\Python36\lib\site-packages\keras\legacy\interfaces.py", line 91, in wrapper
    return func(*args, **kwargs)
  File "C:\Users\...\AppData\Local\Programs\Python\Python36\lib\site-packages\keras\engine\training.py", line 1418, in fit_generator
    initial_epoch=initial_epoch)
  File "C:\Users\...\AppData\Local\Programs\Python\Python36\lib\site-packages\keras\engine\training_generator.py", line 40, in fit_generator
    model._make_train_function()
  File "C:\Users\...\AppData\Local\Programs\Python\Python36\lib\site-packages\keras\engine\training.py", line 496, in _make_train_function
    raise RuntimeError('You must compile your model before using it.')
RuntimeError: You must compile your model before using it.

Process finished with exit code 1

But I'm compiling the model one row above calling the model.fit_generator() function ... And when I'm not completely wrong, I've also given the correct input-shape, so that shouldn't be the problem either ...

Working with windows, python 3.6, tensorflow-gpu 1.12.

Upvotes: 2

Views: 2348

Answers (1)

Daniel M&#246;ller
Daniel M&#246;ller

Reputation: 86600

You're not creating a model anywhere.

You need:

model = Model(inputTensors, outputTensors)

Or at least at some point in your class super(model,self).__init__(inputTensors,outputTensors).

Ideally:

def createModel():
    inputs = Input((256,256,1))
    outputs = Conv2D(filters=2, kernel_size=1, strides=1, 
                     padding='same', activation='relu')(inputs)
    return Model(inputs,outputs)

If you do want to have a subclass for some reason:

class MyModel(Model):
    def init(self):
        inputs = Input((256,256,1))
        self.conv1 = Conv2D(filters=2, kernel_size=1, strides=1, padding='same')
        self.act1 = Activation('relu')
        outputs = self.act1(self.conv1(inputs))

        super(MyModel,self).__init__(inputs,outputs)

Upvotes: 1

Related Questions