Bluelily
Bluelily

Reputation: 63

I need fix with with ValueError in Keras

I am new to ML and CNN in particular, I was following a video tutorial, already followed and practice the lesson. Now, in order to practice more of what I had learnt. I got myself into this error. My datasets consists of annotated image of cancer. My simple setup follow this procedure, the image represent my feature while the annotated description (i.e filename) is label of the datasets.

the following code extracted the the image, normalize it

import numpy as np
import cv2
import pathlib
import sys

DATA_DIR='lung_cancer/'

def load_data(img):
  data_root=pathlib.Path(img)
  all_image_paths = list(data_root.glob('*/*'))
  return all_image_paths


data,target=[],[]

def process_image(image_path):
   min = sys.maxsize
   max = -sys.maxsize
   for image in image_path:
      image = cv2.imread(str(image))
      img = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
      img_resize=cv2.resize(img,(64,64),interpolation=cv2.INTER_AREA)
      np_image = np.asarray(img_resize)
      if min > np_image.min():
         min = np_image.min()
      if max < np_image.max():
         max = np_image.max()

      np_image = np_image.astype('float32')
      np_image -= min
      np_image /= (max - min)
      data.append(np_image)

def data_set_split(img):
     image_paths=load_data(img)
     for image in image_paths:
         label = str(image)
         target.append(label.split('\\')[1])
      process_image(image_paths)

data_set_split(DATA_DIR)
x=np.asarray(data)
target =np.array(target).reshape(-1, 1)

from sklearn.preprocessing import LabelEncoder,OneHotEncoder
label=OneHotEncoder()
y=label.fit_transform(target)
from sklearn.model_selection import train_test_split

x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.20,random_state=54)
from keras.models import Sequential
from keras.layers import Dense, Conv2D, MaxPool2D, Flatten, Dropout, MaxPooling2D
   

when I print out shape of of x_train with x_train.shape, I got this x_train shape : (80, 64, 64)

below is my setup for CNN,

cnn_model = Sequential()
cnn_model.add(Conv2D(32,3,3, input_shape=(64, 64,1), activation='relu'))
cnn_model.add(MaxPooling2D(pool_size=(2,2)))
cnn_model.add(Flatten())

cnn_model.add(Dense(32, activation ='relu'))
cnn_model.add(Dense(10, activation ='sigmoid'))
cnn_model.compile(loss='categorical_crossentropy', metrics=['accuracy'], optimizer='adam')
epoch = 10

The summary of above setup with this code cnn_model.summary() is below :

Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d (Conv2D)              (None, 21, 21, 32)        320       
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 10, 10, 32)        0         
_________________________________________________________________
flatten (Flatten)            (None, 3200)              0         
_________________________________________________________________
dense (Dense)                (None, 32)                102432    
_________________________________________________________________
dense_1 (Dense)              (None, 10)                330       
=================================================================
Total params: 103,082
Trainable params: 103,082
Non-trainable params: 0

so, whenever i execute below section of the code, i got the error bellow

cnn_model.fit(x_train,
              y_train,
              batch_size=10,
              epochs = epoch,
              validation_data=(x_test, y_test)

              )

The error message is

ValueError: Input 0 of layer sequential is incompatible with the layer: : expected min_ndim=4, found ndim=3. Full shape received: [None, 64, 64]

Upvotes: 0

Views: 60

Answers (1)

ATIF ADIB
ATIF ADIB

Reputation: 589

if your input shape is (80, 64, 64), then its incompatible with the first layer of your CNN, which expects the input to be in the shape of (64, 64, 1).

(64, 64, 1) means 1 sample of a 64 x 64 image. To solve the error, reshape your input data using x_train.reshape(64,64,80)

For more details on managing shapes and channels in your CNN read: https://carlthome.github.io/posts/nhwc-vs.-nchw%20/

Upvotes: 1

Related Questions