Imd*_*que 3 python conv-neural-network keras tensorflow
下面导入的包和模型被定义为允许访问构建操作,
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
Run Code Online (Sandbox Code Playgroud)
现在这是创建的模型的编码,我认为描述模型太重要了,
重新缩放图像形状,
train = ImageDataGenerator(rescale=1/255)
validation = ImageDataGenerator(rescale=1/255)
Run Code Online (Sandbox Code Playgroud)
修复数据集目录并访问数据,
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')
Run Code Online (Sandbox Code Playgroud)
创建 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'),
])
Run Code Online (Sandbox Code Playgroud)
编译模型
model.compile(loss = 'binary_crossentropy',
optimizer = RMSprop(lr=0.001),
metrics = ['accuracy '])
Run Code Online (Sandbox Code Playgroud)
适合模型,请注意这里,因为我在这里遇到了问题,
model_fit = model.fit(train_dataset,
steps_per_epoch=3,
epochs= 10,
validation_data = validation_dataset) #error is here
Run Code Online (Sandbox Code Playgroud)
在错误部分下面,我请求所有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
Run Code Online (Sandbox Code Playgroud)
注意:我遇到了这个错误,我无法解决它,并感谢那些尝试解决它并在此处评论以分享答案的人
@AlirezaMoradi 请关注这里,
我在下面犯了一个错误
在模型的编译部分,
model.compile(loss = 'binary_crossentropy',
optimizer = RMSprop(lr=0.001),
metrics = ['accuracy ']) #'accuracy ' it will be 'accuracy'
Run Code Online (Sandbox Code Playgroud)
这意味着,对于我的错误,我添加了空格,删除后它就解决了。
我很忙,这就是为什么这么晚才分享解决方案,很抱歉迟到了。
| 归档时间: |
|
| 查看次数: |
6899 次 |
| 最近记录: |