Tensorflow Get-ValueError:尝试将不支持类型(<;class';NoneType';>;)的值(None)转换为张量

Tensorflow Get-ValueError:尝试将不支持类型(<;class';NoneType';>;)的值(None)转换为张量,tensorflow,keras,deep-learning,tf.keras,Tensorflow,Keras,Deep Learning,Tf.keras,当我在2021年6月尝试运行colab笔记本时,我遇到了一个错误。colab笔记本创建于2020年12月,运行良好。所以我改变了 baseModel = tf.keras.applications.VGG16(weights="imagenet", include_top= False, input_tensor=Input(s

当我在2021年6月尝试运行colab笔记本时,我遇到了一个错误。colab笔记本创建于2020年12月,运行良好。所以我改变了

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

然而,当我继续执行笔记本时,我在稍后阶段遇到了一个错误“ValueError:尝试将不支持类型()的值(None)转换为张量”

代码:

错误:

---------------------------------------------------------------------------
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.

正如@Frightera所建议的,您正在混合
keras
tensorflow.keras
导入。使用所有
tensorflow.keras
imports

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

我无法在Colab上重新解释这个问题。您可以添加所有导入吗?只是为了确保不要混合
keras
tf.keras
导入。编辑问题以包括所有要混合的导入
keras
tf.keras
。尝试从
tf.keras.layers
导入图层。它解决了很多问题。更新的导入将添加到问题中,以防其他人再次遇到相同的问题。您可以接受当前的答案,这对我来说不是问题:)Thx Shubham。已经做到了,而且成功了。也相应地更新了问题。
---------------------------------------------------------------------------
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
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()