TypeError:“ NoneType”对象在Google Colab中不可调用

时间:2020-09-11 15:50:02

标签: python tensorflow keras cnn

在导入的软件包和模型下面,这些软件包和模型被定义为允许访问建筑操作,

import matplotlib.pyplot as plt
import tensorflow as tf
import numpy as np
import cv2
import os
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.preprocessing import image
from tensorflow.keras.optimizers import RMSpro

现在这是创建的模型的编码,我认为描述模型太重要了

调整图像形状,

train = ImageDataGenerator(rescale=1/255)
validation = ImageDataGenerator(rescale=1/255)

修复数据集目录并访问数据,

train_dataset = train.flow_from_directory(
    'cnn_happy_NotHapp/Basedata/training/',
    target_size=(200,200),
    batch_size = 3,
    class_mode = 'binary')
validation_dataset = validation.flow_from_directory(
    'cnn_happy_NotHapp/Basedata/validation/',
    target_size=(200,200),
    batch_size = 3,
    class_mode = 'binary')

创建CNN模型

model = tf.keras.models.Sequential([tf.keras.layers.Conv2D(16,(3,3), activation='relu', input_shape=(200, 200, 3)),
                                    tf.keras.layers.MaxPool2D(2,2),
                                    ##################################
                                    tf.keras.layers.Conv2D(132,(3,3), activation='relu'),
                                    tf.keras.layers.MaxPool2D(2,2),
                                    ##################################
                                    tf.keras.layers.Conv2D(64,(3,3), activation='relu'),
                                    tf.keras.layers.MaxPool2D(2,2),
                                    ##################################
                                    tf.keras.layers.Flatten(),
                                    ###################################
                                    tf.keras.layers.Dense(512, activation='relu'),
                                    ###################################
                                    tf.keras.layers.Dense(1, activation='sigmoid'),
])

编译模型

model.compile(loss = 'binary_crossentropy',
              optimizer = RMSprop(lr=0.001),
              metrics = ['accuracy '])

安装模型,请注意这里,因为我在这里遇到问题,

model_fit = model.fit(train_dataset,
                      steps_per_epoch=3,
                      epochs= 10,
                      validation_data = validation_dataset)     #error is here

在错误部分下,我要求所有stactoverflow成员用于 仔细阅读并帮助我解决此错误,

Epoch 1/10
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-17-85ae786a1bf1> in <module>()
      2                       steps_per_epoch=3,
      3                       epochs= 10,
----> 4                       validation_data = validation_dataset)

3 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/def_function.py in _call(self, *args, **kwds)
    805       # In this case we have created variables on the first call, so we run the
    806       # defunned version which is guaranteed to never create variables.
--> 807       return self._stateless_fn(*args, **kwds)  # pylint: disable=not-callable
    808     elif self._stateful_fn is not None:
    809       # Release the lock early so that multiple threads can perform the call

TypeError: 'NoneType' object is not callable

注意:我遇到了此错误,无法解决,非常感谢谁在尝试解决此问题并在此处评论以分享答案

1 个答案:

答案 0 :(得分:2)

@AlirezaMoradi请在这里关注

我在下面犯了一个错误,

在模型的编译部分,

model.compile(loss = 'binary_crossentropy',
              optimizer = RMSprop(lr=0.001),
              metrics = ['accuracy '])  #'accuracy ' it will be 'accuracy'

这意味着,由于我的错误,我添加了空白,并在将其删除后解决了该问题。

我很忙,这就是为什么共享解决方案的时间太晚了,对不起,我很抱歉。