My code: “ import matplotlib.pyplot as plt import numpy as… · The Chat Archive
Explore / Programming My code: “ import matplotlib.pyplot as plt import numpy as np import pandas as pd import… Save Share Download ▾ ReportMy code:
“
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns
import cv2
import random
import tensorflow as tf
import tkinter as tk
from tkinter import filedialog
from PIL import ImageTk, Image
from ipywidgets import interact, interactive, fixed, interact_manual
import ipywidgets as widgets
from IPython.display import display, clear_output
from tensorflow.keras.preprocessing import image
from tensorflow.keras.optimizers import Adam, SGD, RMSprop, AdamW, Adadelta, Adagrad, Adamax, Adafactor, Nadam, Ftrl
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tqdm import tqdm
import os
from sklearn.utils import shuffle
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import train_test_split
from tensorflow.keras.models import Sequential, Model, load_model
from tensorflow.keras.layers import (
GlobalAveragePooling2D,
Dropout,
Dense,
Conv2D,
MaxPooling2D,
Flatten,
Dropout,
BatchNormalization,
Activation,
concatenate,
Conv2DTranspose,
Input,
Reshape,
UpSampling2D,
)
from tensorflow.keras.applications import (
EfficientNetV2B0,
EfficientNetV2B1,
EfficientNetV2B2,
EfficientNetV2B3,
EfficientNetV2L,
EfficientNetV2M,
EfficientNetV2S,
VGG16, VGG19, Xception,
ResNet50, ResNet101, ResNet152, ResNetRS50, ResNetRS101,
InceptionResNetV2, ConvNeXtXLarge, ConvNeXtBase, DenseNet121, MobileNetV2, NASNetLarge, NASNetMobile
)
from tensorflow.keras.utils import to_categorical
from tensorflow.keras.callbacks import EarlyStopping, ReduceLROnPlateau, TensorBoard, ModelCheckpoint
from sklearn.metrics import classification_report, confusion_matrix
import ipywidgets as widgets
import io
from PIL import Image
from IPython.display import display, clear_output
from warnings import filterwarnings
from google.colab import drive
drive.mount(”/content/gdrive")
def load_data(data_folders):
X_data = [] # Combined data
y_class_labels = [] # Combined classification labels
y_seg_labels = [] # Combined segmentation labels
for folderPath in data_folders:
for label in labels:
label_folder_path = os.path.join(folderPath, label)
for filename in tqdm(os.listdir(label_folder_path)):
if filename.endswith(“.jpg”):
img = cv2.imread(os.path.join(label_folder_path, filename))
img = cv2.resize(img, (image_size, image_size))
X_data.append(img)
y_class_labels.append(label)
seg_filename = filename.split(“.”)[0] + “.png”
seg_img = cv2.imread(os.path.join(label_folder_path, seg_filename), 0)
seg_img = cv2.resize(seg_img, (image_size, image_size))
seg_img = np.where(seg_img > 0, 1, 0) # Convert segmentation mask to binary
y_seg_labels.append(seg_img)
X_data = np.array(X_data)
y_class_labels = np.array(y_class_labels)
y_seg_labels = np.array(y_seg_labels)
X_data, y_class_labels, y_seg_labels = shuffle(X_data, y_class_labels, y_seg_labels, random_state=101)
return X_data, y_class_labels, y_seg_labels
def split_data(X_data, y_class_labels, class_data_counts):
X_train, y_train_class = [], []
X_val, y_val_class = [], []
X_test, y_test_class = [], []
for label, count in class_data_counts.items():
label_indices = np.where(y_class_labels == label)[0]
class_X_data = X_data[label_indices]
class_y_class_labels = y_class_labels[label_indices]
AssistantCopy response
The error you're encountering originates from this part of your code:
base_model = ResNet50(weights=None, include_top=False, input_shape=input_shape)
x = GlobalAveragePooling2D()(base_model)
The cause of the error is attempting to pass a model (base_model, which is an instance of keras.engine.functional.Functional) directly into a layer (GlobalAveragePooling2D) as its input. In Keras, layers expect tensors as inputs, not models. However, the output tensors of a model can certainly be connected to subsequent layers.
To fix this, you need to use the output attribute of base_model to get the tensor that you can use as input to the next layer in your model. Modify the line indicated above to fix the error:
x = GlobalAveragePooling2D()(base_model.output)
Copy first prompt
Details
Provider openai
Model provenance As recorded by source dataset
Turns 1 (2 messages)
Language —
Added to archive Nov 21, 2026
ID 01m7y2gd5ydqfvg07ghnth3z2bSource & license Imported from Suzhen/CodeChat (source ). Imported records earn no Research Points.
Upstream record: 044b5b0396760ddbbd4bd65805a3201d
Suzhen/CodeChat, revision 1681793a6cfb27b68c1f285bd1e78588132bc1c1.
License: ODC-BY-1.0
Dataset releases Not yet included in a published release.
train_count, val_count, test_count = count
class_X_train = class_X_data[:train_count]
class_y_train_class = class_y_class_labels[:train_count]
class_X_val = class_X_data[train_count: train_count + val_count]
class_y_val_class = class_y_class_labels[train_count: train_count + val_count]
class_X_test = class_X_data[train_count + val_count: train_count + val_count + test_count]
class_y_test_class = class_y_class_labels[train_count + val_count: train_count + val_count + test_count]
X_train.extend(class_X_train)
y_train_class.extend(class_y_train_class)
X_val.extend(class_X_val)
y_val_class.extend(class_y_val_class)
X_test.extend(class_X_test)
y_test_class.extend(class_y_test_class)
Convert class labels to categorical label_encoder = LabelEncoder()
y_train_class_encoded = label_encoder.fit_transform(y_train_class)
y_train_class_categorical = to_categorical(y_train_class_encoded)
y_val_class_encoded = label_encoder.transform(y_val_class)
y_val_class_categorical = to_categorical(y_val_class_encoded)
y_test_class_encoded = label_encoder.transform(y_test_class)
y_test_class_categorical = to_categorical(y_test_class_encoded)
return (
np.array(X_train),
np.array(y_train_class_categorical),
np.array(X_val),
np.array(y_val_class_categorical),
np.array(X_test),
np.array(y_test_class_categorical),
)
def count_labels(y_class_categorical, label_encoder):
Convert one-hot encoded labels back to label encoded y_class_labels = np.argmax(y_class_categorical, axis=1)
Convert label encoded labels back to original class names y_class_names = label_encoder.inverse_transform(y_class_labels)
unique, counts = np.unique(y_class_names, return_counts=True)
return dict(zip(unique, counts))
def build_model(input_shape, num_classes):
base_model = ResNet50(weights= None, include_top=False, input_shape=input_shape)
x = GlobalAveragePooling2D()(base_model)
x = Dropout(0.5)(x)
Classification output layer predictions = Dense(num_classes, activation=‘softmax’)(x)
Define the model model = Model(inputs=base_model.input, outputs=predictions)
return model
Categorical_Focal_loss = tf.keras.losses.CategoricalFocalCrossentropy(
alpha=0.25, #0.25 default
gamma=2.0, #2
from_logits=False,
label_smoothing=0.0,
axis=-1,)
def train_model(model, X_train, y_train_class, X_val, y_val_class, batch_size, epochs):
checkpoint = ModelCheckpoint(
“classification_best_weights.h5”,
monitor=“val_accuracy”,
save_best_only=True,
mode=“max”,
verbose=1,)
reduce_lr = ReduceLROnPlateau(
monitor=“val_accuracy”,
factor=0.3,
patience=2,
min_delta=0.001,
mode=“auto”,
verbose=1,)
tensorboard = TensorBoard(log_dir=“logs”)
model.compile(
optimizer=Adam(lr=0.001),
loss=Categorical_Focal_loss,
metrics=[“accuracy”],)
history = model.fit(
X_train,
y_train_class,
validation_data=(X_val, y_val_class),
epochs=epochs,
verbose=1,
batch_size=batch_size,
callbacks=[checkpoint, reduce_lr, tensorboard],)
return history
def evaluate_model(model, X_test, y_test_class):
Load the best model weights best_model = load_model(“classification_best_weights.h5”)
Evaluate the model on test data test_loss, test_acc = best_model.evaluate(X_test, y_test_class)
print(“Test Loss:”, test_loss)
print(“Test Accuracy:”, test_acc)
return test_acc
def plot_performance(history):
Plot classification accuracy plt.figure(figsize=(10, 4))
Accuracy plot plt.subplot(1, 2, 1)
plt.plot(history.history[‘accuracy’], label=‘Training Accuracy’)
plt.plot(history.history[‘val_accuracy’], label=‘Validation Accuracy’)
plt.title(‘Classification Accuracy’)
plt.xlabel(‘Epochs’)
plt.ylabel(‘Accuracy’)
plt.legend()
Loss plot plt.subplot(1, 2, 2)
plt.plot(history.history[‘loss’], label=‘Training Loss’)
plt.plot(history.history[‘val_loss’], label=‘Validation Loss’)
plt.title(‘Classification Loss’)
plt.xlabel(‘Epochs’)
plt.ylabel(‘Loss’)
plt.legend()
plt.tight_layout()
plt.show()
Image size and labels setup image_size = 224
labels = [“bridge”, “dirty”, “good”, “lift”, “shift”]
Data folders data_folders = [
“/content/gdrive/MyDrive/FYP_8/jit0/f_ffc/train”,
“/content/gdrive/MyDrive/FYP_8/jit0/f_ffc/val”,
“/content/gdrive/MyDrive/FYP_8/jit0/f_ffc/test”,
]
Load and split the data X_data, y_class_labels, y_seg_labels = load_data(data_folders)
Define train:val:test ratio for each class (ratio x4 = exact) class_data_counts = {“bridge”: [80, 80, 80],
“dirty”: [80, 80, 80],
“good”: [80, 80, 80],
“lift”: [80, 80, 80],
“shift”: [80, 80, 80]
}
X_train, y_train_class, X_val, y_val_class, X_test, y_test_class = split_data(X_data, y_class_labels, class_data_counts)
Initialize the label encoder label_encoder = LabelEncoder()
label_encoder.fit(y_class_labels)
Count the number of images of each class in the train, validation, and test sets train_counts = count_labels(y_train_class, label_encoder)
val_counts = count_labels(y_val_class, label_encoder)
test_counts = count_labels(y_test_class, label_encoder)
print(“Train counts: “, train_counts,” Total in train set:”, sum(train_counts.values()))
print(“Validation counts:”, val_counts, " Total in validation set:“, sum(val_counts.values()))
print(“Test counts: “, test_counts,” Total in test set:”, sum(test_counts.values()))
Train model n times input_shape = (image_size, image_size, 3)
num_classes = len(labels)
model = build_model(input_shape, num_classes)
model.summary()
test_acc_list = []
for i in range(5):
print(f”\nTrain {i+1}:\n")
#model = build_model(input_shape, num_classes)
batch_size = 16
epochs = 50
history = train_model(model, X_train, y_train_class, X_val, y_val_class, batch_size, epochs)
Evaluate model on test data test_acc = evaluate_model(model, X_test, y_test_class)
test_acc_list.append(test_acc)
Calculate average test classification accuracy average_test_acc = sum(test_acc_list) / len(test_acc_list)
print(“Test Classification Accuracy List:”, test_acc_list)
print(“Average Test Classification Accuracy:”, average_test_acc)
“
Error:---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
in <cell line: 4>()
2 input_shape = (image_size, image_size, 3)
3 num_classes = len(labels)
----> 4 model = build_model(input_shape, num_classes)
5 model.summary()
6 test_acc_list = []
2 frames
/usr/local/lib/python3.10/dist-packages/keras/src/engine/input_spec.py in assert_input_compatibility(input_spec, inputs, layer_name)
211 # which does not have a shape attribute.
212 if not hasattr(x, “shape”):
–> 213 raise TypeError(
214 f"Inputs to a layer should be tensors. Got ‘{x}’ “
215 f”(of type {type(x)}) as input for layer ‘{layer_name}’.”
TypeError: Inputs to a layer should be tensors. Got ‘<keras.src.engine.functional.Functional object at 0x788c4fe78430>’ (of type <class ‘keras.src.engine.functional.Functional’>) as input for layer ‘global_average_pooling2d_1’.
This change tells Keras to take the output tensor of the base_model as the input to the GlobalAveragePooling2D layer, which properly chains the model and the additional layers together.
Here is the corrected part of your code:
def build_model(input_shape, num_classes):
base_model = ResNet50(weights=None, include_top=False, input_shape=input_shape)
x = GlobalAveragePooling2D()(base_model.output) # Modified line
x = Dropout(0.5)(x)
# Classification output layer
predictions = Dense(num_classes, activation='softmax')(x)
# Define the model
model = Model(inputs=base_model.input, outputs=predictions)
return model
Please make sure to replace this corrected function in your code. This should resolve the TypeError you encountered.
Copy first response