Keras:使用fit_generator时出现notImplementedError / RuntimeError

时间:2018-11-29 04:25:22

标签: python tensorflow keras

使用以下代码,我在使用keras和tensorflow时遇到麻烦:

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)

运行该代码会出现以下错误:

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

我已经尝试找到一些提示,如果我真的尝试做某事,那不是故意的,但是对我来说这似乎是不合逻辑的,因为这种方式非常适合定义更复杂的网络,并且我已经准备好了我很确定与pytorch一起使用,这也应该与tf和keras一起使用。

有一个thread带有相同的错误,但是对于顺序的网络实现,显然,这不是我想要的目标。

如果我使用的是喀拉拉邦的直接进口商品,则会收到其他错误:

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

但是我在调​​用model.fit_generator()函数的上方一行编译模型... 当我没有完全错的时候,我还给出了正确的输入形状,因此也不应该成为问题...

使用Windows,python 3.6,tensorflow-gpu 1.12。

1 个答案:

答案 0 :(得分:1)

您没有在任何地方创建模型。

您需要:

$PWD/
  builds/
      one/
        bar/
          bar.go
        foo/
          foo.go
        one.go

或者至少在您的课程model = Model(inputTensors, outputTensors) 中的某个时候。

理想情况:

super(model,self).__init__(inputTensors,outputTensors)

如果您出于某些原因想要拥有子类,则:

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)