I'm trying to train a efficientnetb3 model while performing GLCM Transformations on the input images , However I'm facing this issue : ValueError: all the input arrays must have same number of dimensions, but the array at index 0 has 2 dimension(s) and the array at index 1 has 1 dimension(s) Can anyone point out the wrong logic in the below code .
import os
import numpy as np
from tensorflow.keras.applications import EfficientNetB3
from tensorflow.keras.models import Sequential, Model
from tensorflow.keras.layers import Dense, GlobalAveragePooling2D, Input, Dropout
from tensorflow.keras.callbacks import ModelCheckpoint
from sklearn.model_selection import train_test_split
from skimage import color, feature, io
from skimage.transform import resize
import tensorflow as tf
def compute_glcm_transformed_image(image_path, target_size=(300, 300)):
img = io.imread(image_path)
img_gray = color.rgb2gray(img)
img_resized = resize(img_gray, target_size)
glcm = feature.greycomatrix((img_resized * 255).astype(np.uint8), [5], [0], 256, symmetric=True, normed=True)
glcm_features = np.ravel(glcm)
return np.concatenate((img_resized, glcm_features), axis=-1)
main_directory = "input_path_where there are folders (representing class names ) and respective images inside"
data_list = []
labels_list = []
# Iterate through subfolders (each representing a class)
for class_folder in os.listdir(main_directory):
class_path = os.path.join(main_directory, class_folder)
if os.path.isdir(class_path):
# Iterate through images in the class folder
for filename in os.listdir(class_path):
if filename.endswith(".jpg") or filename.endswith(".png"):
image_path = os.path.join(class_path, filename)
transformed_image = compute_glcm_transformed_image(image_path)
data_list.append(transformed_image.reshape(img_height, img_width, channels + len(glcm_features)))
labels_list.append(class_folder)
X = np.array(data_list)
y = np.array(labels_list)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
img_height = 300
img_width = 300
channels = X.shape[-1]
new_input = Input(shape=(img_height, img_width, channels), name='image_input')
base_model = EfficientNetB3(weights='imagenet', include_top=False, input_tensor=new_input)
x = base_model.output
x = GlobalAveragePooling2D()(x)
x = Dense(1024, activation='relu')(x)
x = Dense(1024, activation='relu')(x)
x = Dropout(0.2)(x)
x = Dense(512, activation='relu')(x)
x = Dense(256, activation='relu')(x)
preds = Dense(len(os.listdir(main_directory)), activation='softmax')(x)
model = Model(inputs=base_model.input, outputs=preds)
outputpath = "Somepath"
model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.0001),
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
callbacks = [ModelCheckpoint(filepath=os.path.join(outputpath, 'model_b3.h5'),
monitor='val_accuracy',
mode='max',
save_best_only=True)]
history = model.fit(X_train, y_train, epochs=5, validation_data=(X_test, y_test), callbacks=callbacks)
The problem lies in the
return np.concatenate((img_resized, glcm_features), axis=-1)
statement. As the error message is telling you,img_resized
is a two-dimensional array whileglcm_features
is one-dimensional. As a consequence they can not be concatenated. The following interactive session illustrates the issue: