i am trying to trained a cnn model, but i really don't understand how to do it properly. i still learning about this kind of stuff so i'm really lost. I already tried doing stuff with it but still cannot get my head around it. can someone explain to me how to do it properly. when i try to fit the train data to the model this error pops up.
WARNING:tensorflow:Model was constructed with shape (None, 224, 224, 3) for input KerasTensor(type_spec=TensorSpec(shape=(None, 224, 224, 3), dtype=tf.float32, name='input_1'), name='input_1', description="created by layer 'input_1'"), but it was called on an input with incompatible shape (None,).
Traceback (most recent call last):
File "G:/Skripsi/Program/training.py", line 80, in <module>
train.train()
File "G:/Skripsi/Program/training.py", line 70, in train
model.fit(self.x_train, self.y_train, epochs=2, verbose=1)
File "G:\Skripsi\Program\venv\lib\site-packages\keras\utils\traceback_utils.py", line 67, in error_handler
raise e.with_traceback(filtered_tb) from None
File "G:\Skripsi\Program\venv\lib\site-packages\tensorflow\python\framework\func_graph.py", line 1129, in autograph_handler
raise e.ag_error_metadata.to_exception(e)
ValueError: in user code:
File "G:\Skripsi\Program\venv\lib\site-packages\keras\engine\training.py", line 878, in train_function *
return step_function(self, iterator)
File "G:\Skripsi\Program\venv\lib\site-packages\keras\engine\training.py", line 867, in step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
File "G:\Skripsi\Program\venv\lib\site-packages\keras\engine\training.py", line 860, in run_step **
outputs = model.train_step(data)
File "G:\Skripsi\Program\venv\lib\site-packages\keras\engine\training.py", line 808, in train_step
y_pred = self(x, training=True)
File "G:\Skripsi\Program\venv\lib\site-packages\keras\utils\traceback_utils.py", line 67, in error_handler
raise e.with_traceback(filtered_tb) from None
File "G:\Skripsi\Program\venv\lib\site-packages\keras\engine\input_spec.py", line 227, in assert_input_compatibility
raise ValueError(f'Input {input_index} of layer "{layer_name}" '
ValueError: Exception encountered when calling layer "model" (type Functional).
Input 0 of layer "conv2d" is incompatible with the layer: expected min_ndim=4, found ndim=1. Full shape received: (None,)
Call arguments received:
• inputs=tf.Tensor(shape=(None,), dtype=int32)
• training=True
• mask=None
this is my code for training the model.
from sklearn.model_selection import train_test_split
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Input
from densenet201 import DenseNet201
import tensorflow as tf
import pandas as pd
import numpy as np
import cv2
import os
dataset_folder = "./datasets/train_datasets"
class TrainingPreprocessing:
@staticmethod
def preprocessing_train(path):
images = cv2.imread(path, 3)
images_resize = cv2.resize(src=images, dsize=(224, 224), interpolation=cv2.INTER_LINEAR)
images_normalize = cv2.normalize(images_resize, None, alpha=0, beta=1, norm_type=cv2.NORM_MINMAX,
dtype=cv2.CV_32F)
return images_normalize.reshape(224, 224, 3)
class Training:
@staticmethod
def load_data():
"""Loads and Preprocess dataset"""
train_labels_encode = []
train_labels = []
train_data = []
file_list = os.listdir(dataset_folder)
for folder in file_list:
file_list2 = os.listdir(str(dataset_folder) '/' str(folder))
for images in file_list2:
train_labels_encode.append(folder)
train_labels.append(folder)
train_data.append(np.array(TrainingPreprocessing.preprocessing_train(
str(dataset_folder) '/' str(folder) '/' str(images)
)))
labels = np.array(train_labels_decode)
data = np.array(train_data)
return labels, data
def split_data(self):
"""Split the preprocessed dataset to train and test data"""
x, y = self.load_data()
self.x_train, self.x_test, self.y_train, self.y_test = train_test_split(x, y, test_size=0.20, random_state=0)
print('Training data shape : ', self.x_train.shape, self.y_train.shape)
print('Testing data shape : ', self.x_test.shape, self.y_test.shape)
def train(self):
"""Compile dan fit DenseNet model"""
input_shape = 224, 224, 3
number_classes = 2
model = DenseNet201.densenet(input_shape, number_classes)
model.summary()
model.compile(loss='binary_crossentropy', optimizer='Adam', metrics=["accuracy"])
model.fit(self.x_train, self.y_train, epochs=2, verbose=1)
model.save_weights('densenet201_best_model.h5', overwrite=True)
loss, accuracy = model.evaluate(self.x_test, self.y_test)
print("[INFO] accuracy: {:.2f}%".format(accuracy * 100))
train = Training()
train.split_data()
train.train()
and this is the code for the cnn network
from tensorflow.keras.layers import AveragePooling2D, GlobalAveragePooling2D, MaxPool2D
from tensorflow.keras.layers import Input, Conv2D, BatchNormalization, Dense
from tensorflow.keras.layers import ReLU, concatenate, Dropout
from tensorflow.keras.models import Model
import tensorflow.keras.layers as layers
import tensorflow.keras.backend as K
import tensorflow as tf
class DenseNet201:
def densenet(image_shape, number_classes, growth_rate=32):
def batch_relu_conv(x, growth_rate, kernel=1, strides=1):
x = BatchNormalization()(x)
x = ReLU()(x)
x = Conv2D(growth_rate, kernel, strides=strides, padding='same', kernel_initializer="he_uniform")(x)
return x
def dense_block(x, repetition):
for _ in range(repetition):
y = batch_relu_conv(x, 4 * growth_rate)
y = batch_relu_conv(y, growth_rate, 3)
x = concatenate([y, x])
return x
def transition_layer(x):
x = batch_relu_conv(x, K.int_shape(x)[-1] // 2)
x = AveragePooling2D(2, strides=2, padding='same')(x)
return x
inputs = Input(image_shape)
x = Conv2D(64, 7, strides=2, padding='same', kernel_initializer="he_uniform")(inputs)
x = MaxPool2D(3, strides=2, padding='same')(x)
for repetition in [6, 12, 48, 32]:
d = dense_block(x, repetition)
x = transition_layer(d)
x = GlobalAveragePooling2D ()(d)
output = Dense(number_classes, activation='softmax')(x)
model = Model(inputs, output)
return model
CodePudding user response:
It seems you inverted data and labels (x and y) in the function:
def load_data():
which returns: return labels, data
I think you are calling model.fit(self.x_train, self.y_train, epochs=2, verbose=1)
with label and then data. Hence the model complaining about not getting the expected data shape.