tensorflow - 得到 ValueError : Attempt to convert a value (None) with an unsupported type (<class 'NoneType' >) to a Tensor

标签 tensorflow keras deep-learning tf.keras

当我尝试在 2021 年 6 月运行一个 colab notebook 时,它是在 2020 年 12 月创建的并且运行良好,但我遇到了错误。所以我改变了

baseModel = tf.keras.applications.VGG16(weights="imagenet", 
                                     include_top= False,
                                     input_tensor=Input(shape=(224, 224, 3)))

baseModel = tf.keras.applications.VGG19(weights="imagenet", 
                                     include_top= False,
                                     input_shape=(224, 224, 3))

但是,当我继续执行笔记本时,出现错误“ValueError:尝试将具有不受支持的类型 () 的值 (None) 转换为 Tensor。”在稍后的阶段。

代码:

import numpy as np
from tqdm import tqdm
import math
import os

import keras
from keras.models import *
from keras.layers import *
from keras.layers.core import Dense, Flatten
from keras.optimizers import Adam
from keras.metrics import categorical_crossentropy
from keras.preprocessing.image import ImageDataGenerator
from keras.layers.normalization import BatchNormalization
from keras.layers.convolutional import Conv2D
from sklearn.metrics import confusion_matrix
from keras.applications.densenet import DenseNet121
from keras.callbacks import *
from keras import backend as K
K.clear_session()
import itertools
import matplotlib.pyplot as plt
import cv2
import matplotlib.cm as cm

from tensorflow.keras.utils import to_categorical
from sklearn.preprocessing import LabelBinarizer,LabelEncoder
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report
from sklearn.metrics import confusion_matrix


import tensorflow as tf
baseModel = tf.keras.applications.VGG19(weights="imagenet", 
                                     include_top= False,
                                     input_shape=(224, 224, 3))

headModel = baseModel.output
headModel = AveragePooling2D(pool_size=(4, 4))(headModel)
headModel = Flatten(name="flatten")(headModel)
headModel = Dense(64, activation="relu")(headModel)
headModel = Dropout(0.4)(headModel)
headModel = Dense(3, activation="softmax")(headModel)
model = Model(inputs=baseModel.input, outputs=headModel)

model.summary()

错误:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-18-6695ac43a942> in <module>()
      1 headModel = baseModel.output
      2 headModel = AveragePooling2D(pool_size=(4, 4))(headModel)
----> 3 headModel = Flatten(name="flatten")(headModel)
      4 headModel = Dense(64, activation="relu")(headModel)
      5 headModel = Dropout(0.4)(headModel)

5 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/constant_op.py in convert_to_eager_tensor(value, ctx, dtype)
     96       dtype = dtypes.as_dtype(dtype).as_datatype_enum
     97   ctx.ensure_initialized()
---> 98   return ops.EagerTensor(value, ctx.device_name, dtype)
     99 
    100 

ValueError: Attempt to convert a value (None) with an unsupported type (<class 'NoneType'>) to a Tensor.

更新导入:

import numpy as np
from tqdm import tqdm
import math
import os

import tensorflow as tf

import tensorflow.keras
from tensorflow.keras.models import *
from tensorflow.keras.layers import *
from tensorflow.keras.layers import Dense, Flatten
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.metrics import categorical_crossentropy
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.layers import BatchNormalization
from tensorflow.keras.layers import Conv2D
from sklearn.metrics import confusion_matrix
from tensorflow.keras.applications.densenet import DenseNet121
from tensorflow.keras.callbacks import *
from tensorflow.keras import backend as K
K.clear_session()
import itertools
import matplotlib.pyplot as plt
import cv2
import matplotlib.cm as cm

from tensorflow.keras.utils import to_categorical
from sklearn.preprocessing import LabelBinarizer,LabelEncoder
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report
from sklearn.metrics import confusion_matrix

最佳答案

正如@Frightera 所建议的,您混合了 kerastensorflow.keras 导入。尝试使用所有 tensorflow.keras 导入的代码,

import numpy as np
from tqdm import tqdm
import math
import os

from tensorflow.keras.models import *
from tensorflow.keras.layers import *
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.metrics import categorical_crossentropy
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from sklearn.metrics import confusion_matrix
from tensorflow.keras.applications.densenet import DenseNet121
from tensorflow.keras.callbacks import *
from tensorflow.keras import backend as K
K.clear_session()
import itertools
import matplotlib.pyplot as plt
import cv2
import matplotlib.cm as cm

from tensorflow.keras.utils import to_categorical
from sklearn.preprocessing import LabelBinarizer,LabelEncoder
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report
from sklearn.metrics import confusion_matrix

import tensorflow as tf

baseModel = tf.keras.applications.VGG19(weights="imagenet", 
                                     include_top= False,
                                     input_shape=(224, 224, 3))

headModel = baseModel.output
headModel = AveragePooling2D(pool_size=(4, 4))(headModel)
headModel = Flatten(name="flatten")(headModel)
headModel = Dense(64, activation="relu")(headModel)
headModel = Dropout(0.4)(headModel)
headModel = Dense(3, activation="softmax")(headModel)
model = Model(inputs=baseModel.input, outputs=headModel)

model.summary()

关于tensorflow - 得到 ValueError : Attempt to convert a value (None) with an unsupported type (<class 'NoneType' >) to a Tensor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67860096/

相关文章:

python - 不使用 Supervisor 时 Tensorflow 会卡住

python - TensorFlow 模型获得零损失

machine-learning - model.output.op 在 keras 中做什么?

python - 创建三个类而不是两个 - keras 库

python - 让 DQN 解决 A 是否大于 B

python - 将 tensorflow 作为守护进程运行并将所有输出通过管道传输到日志文件

tensorflow - 具有归一化二元交叉熵损失的模型不收敛

machine-learning - 如何正确实现DQN算法

python - 预期 dense_1 具有 2 个维度,但得到形状为 (308, 1, 6) 的数组

Tensorflow lstm 用于情感分析而不是学习。更新