When I tried to run a colab notebook on 2021 June, which was created on 2020 december and ran fine I got an error. So I changed
baseModel = tf.keras.applications.VGG16(weights="imagenet",
include_top= False,
input_tensor=Input(shape=(224, 224, 3)))
to
baseModel = tf.keras.applications.VGG19(weights="imagenet",
include_top= False,
input_shape=(224, 224, 3))
However when I continued to execute the notebook I got an error "ValueError: Attempt to convert a value (None) with an unsupported type (<class 'NoneType'>) to a Tensor." in a later stage.
Code:
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()
Error:
---------------------------------------------------------------------------
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.
Updated imports:
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
As @Frightera suggested, you are mixing keras
and tensorflow.keras
imports. Try the code with all 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()
i had the same issue with an old code. but with a newer version of python the code was not working properly. but i solved the issue by changing it to the latest requirments.
here is the solution https://stackoverflow.com/a/68049002/15345841
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With