Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 图像分类器“Node”对象没有属性“output\u masks”_Python - Fatal编程技术网

Python 图像分类器“Node”对象没有属性“output\u masks”

Python 图像分类器“Node”对象没有属性“output\u masks”,python,Python,你好,所以我一直在为大学做这个图像分类器项目,我一直在为如何使用模型和使用什么代码而烦恼,如果我做的一切都是对的,我一直在读这个,但我仍然不知道为什么我总是出错 我使用这些代码 from keras.preprocessing.image import ImageDataGenerator from keras.models import Sequential from keras.layers import Conv2D, MaxPooling2D from keras.layers impo

你好,所以我一直在为大学做这个图像分类器项目,我一直在为如何使用模型和使用什么代码而烦恼,如果我做的一切都是对的,我一直在读这个,但我仍然不知道为什么我总是出错

我使用这些代码

from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D
from keras.layers import Activation, Dropout, Flatten, Dense
from keras import backend as K
from PIL import ImageFile, Image
from tensorflow import keras

print(Image.__file__)
import numpy
import matplotlib.pyplot as plt

# dimensions of our images.
img_width, img_height = 200, 200

train_data_dir = r'C:\Users\Acer\imagerec\Brain\TRAIN'
validation_data_dir = r'C:\Users\Acer\imagerec\Brain\VAL'
nb_train_samples = 140
nb_validation_samples = 40
epochs = 20
batch_size = 5

if K.image_data_format() == 'channels_first':
    input_shape = (1, img_height, img_width)
else:
    input_shape = (img_height, img_width, 1)

from keras.applications.densenet import DenseNet121
from keras.models import Model
from keras.layers import Dense

MN = keras.applications.densenet.DenseNet121(include_top=False,
                                            weights='imagenet', input_tensor=None, input_shape=None,
                                            pooling='avg', classes=1000)
x = MN.output
x = Dense(1, activation='sigmoid')(x)
model = Model(MN.input, x)
model.summary()

model.compile(loss='binary_crossentropy',
              optimizer='rmsprop',
              metrics=['accuracy'])

# this is the augmentation configuration we will use for training
train_datagen = ImageDataGenerator(
    rescale=1. / 255,
    shear_range=0.2,
    zoom_range=0.2,
    horizontal_flip=True)

# this is the augmentation configuration we will use for testing:
# only rescaling
test_datagen = ImageDataGenerator(rescale=1. / 255)

train_generator = train_datagen.flow_from_directory(
    train_data_dir,
    target_size=(img_width, img_height),
    batch_size=batch_size,
    class_mode='binary')

validation_generator = test_datagen.flow_from_directory(
    validation_data_dir,
    target_size=(img_width, img_height),
    batch_size=batch_size,
    class_mode='binary')

model.fit_generator(
    train_generator,
    steps_per_epoch=nb_train_samples // batch_size,
    epochs=epochs,
    validation_data=validation_generator,
    validation_steps=nb_validation_samples // batch_size)

from sklearn.metrics import classification_report
from sklearn.metrics import confusion_matrix
import seaborn as sns

test_steps_per_epoch = numpy.math.ceil(validation_generator.samples / validation_generator.batch_size)

predictions = model.predict_generator(validation_generator, steps=test_steps_per_epoch)
# Get most likely class
predicted_classes = numpy.argmax(predictions, axis=1)
true_classes = validation_generator.classes
class_labels = list(validation_generator.class_indices.keys())
report = classification_report(true_classes, predicted_classes, target_names=class_labels)
print(report)

cm=confusion_matrix(true_classes,predicted_classes)

sns.heatmap(cm, annot=True)

print(cm)

plt.show()
我犯了这个错误

2019-12-09 12:55:02.163825: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
Traceback (most recent call last):
  File "C:/Users/Acer/PycharmProjects/condas/VGG16.py", line 36, in <module>
    x = Dense(1, activation='sigmoid')(x)
  File "C:\Users\Acer\Anaconda3\envs\condas\lib\site-packages\keras\backend\tensorflow_backend.py", line 75, in symbolic_fn_wrapper
    return func(*args, **kwargs)
  File "C:\Users\Acer\Anaconda3\envs\condas\lib\site-packages\keras\engine\base_layer.py", line 475, in __call__
    previous_mask = _collect_previous_mask(inputs)
  File "C:\Users\Acer\Anaconda3\envs\condas\lib\site-packages\keras\engine\base_layer.py", line 1441, in _collect_previous_mask
    mask = node.output_masks[tensor_index]
AttributeError: 'Node' object has no attribute 'output_masks'

Process finished with exit code 1

我正在使用python 3.6

GitHub上有一个关于这个主题的问题

在帖子中,有关于tensorflow和keras关系的内容:

我有一个类似的问题,但架构不同。作为人 建议不要将keras与tensorflow.keras混合使用,因此 尝试交换

import image from keras.models
import Model from keras.layers 
import Dense, GlobalAveragePooling2D
from keras import backend as K 
致:

从tensorflow.keras.preprocessing导入图像 从tensorflow.keras.models从tensorflow.keras.layers导入模型 导入密集、全局平均池2D 从tensorflow.keras导入后端为K 还要确保不要使用keras。代码中的某些内容 也只有进口产品,希望能有所帮助:另外,我使用了Keras2.2.4 tensorflow 1.10.0


你好,谢谢你的回复啊,嗯,我试着理解该怎么做,这是错误的,但我应该再次改变什么?我从tensorflow.keras.preprocessing导入tensorflow.keras.models导入tensorflow.keras.layers导入稠密、全局平均池2D从tensorflow.keras导入keras.models导入Sequential从keras.layers导入Conv2D、MaxPoolig2D从keras.layers导入激活、退出、展平,密集型从keras导入后端作为K从PIL导入图像文件,图像从tensorflow导入kerasim使用keras 2.3.1基本上您必须使用表格tensorflow.keras导入。。。对于tensorflow相关代码,以及从keras导入。。。对于纯keras码。你的问题与这个错误的地址有关。首先,你需要映射你需要的东西和你要得到的keras,这需要tensorflow.keras或者直接从pure keras.hiya获得,所以每当我这样做的时候,这个错误就会出现NameError:name'keras'没有定义尝试使用pip pip install keras或pip3 install keras安装keras来安装keras。似乎您没有keras纯软件包或安装错误。