TypeError:"NoneType"对象在Google Colab中不可调用 [英] TypeError: "NoneType" object is not callable in Google Colab

查看:60
本文介绍了TypeError:"NoneType"对象在Google Colab中不可调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 将matplotlib.pyplot导入为plt将tensorflow导入为tf将numpy导入为np导入cv2导入操作系统从tensorflow.keras.preprocessing.image导入ImageDataGenerator从tensorflow.keras.preprocessing导入图像从tensorflow.keras.optimizers导入RMSpro 

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

调整图像形状,

  train = ImageDataGenerator(rescale = 1/255)验证= 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',优化器= RMSprop(lr = 0.001),指标= ['准确性']) 

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

  model_fit = model.fit(train_dataset,steps_per_epoch = 3,epochs = 10,验证数据=验证数据集)#错误在这里 

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

  Epoch 1/10---------------------------------------------------------------------------TypeError Traceback(最近一次通话)< ipython-input-17-85ae786a1bf1>在< module>()中2 steps_per_epoch = 3,3个时代= 10,---->4验证数据=验证数据集)3帧_call中的/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/def_function.py(self,* args,** kwds)805#在这种情况下,我们在第一次调用时创建了变量,因此我们运行806#禁用版本,保证永远不会创建变量.->807 return self._stateless_fn(* args,** kwds)#pylint:disable = not-callable808 elif self._stateful_fn不是None:809#及早释放锁,以便多个线程可以执行调用TypeError:"NoneType"对象不可调用 

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

解决方案

@AlirezaMoradi请在这里关注,

我在下面犯了一个错误,

在模型的编译部分,

  model.compile(loss ='binary_crossentropy',优化器= RMSprop(lr = 0.001),metrics = ['accuracy'])#'accuracy'将是'accuracy' 

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

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

below imported packages and models which is define to allow to access the building operations,

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

Now here is the coding of the created model, I think it is too importat to describe the model,

Rescale the images shapes,

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

Fixed the dataset directory and access the data,

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')

Create the CNN model

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'),
])

Compile the model

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

Fit the model and please noticed here because i faced problem here,

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

Below the error section, I request all of stactoverflow members for carefully read and help me for solving this error,

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

Note: I am suffering from this error , I can't solved it and advanced thanks who are try to solve it and comment here for sharing the answer

解决方案

@AlirezaMoradi please concern here,

I made a mistakes below,

In compile section of the model,

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

That means,for my mistakes I add white space and after removing It solved.

I was busy that's why it is late for sharing the solution and I am sorry for late.

这篇关于TypeError:"NoneType"对象在Google Colab中不可调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆