0
$\begingroup$

I'm trying to pass all my images (71 for each class) from folder 'train' to model.fit. The method ReadImages get these images and resize them (because are too big 4288x2848)....

But when i run my code throws this error:

ValueError: Error when checking input: expected conv2d_1_input to have 4 dimensions, but got array with shape (142, 1)

This is my code:

def ReadImages(Path):
    LabelList = list()
    ImageCV = list()
    classes = ["nonPdr", "pdr"]

    # Get all subdirectories
    FolderList = os.listdir(Path)

    # Loop over each directory
    for File in FolderList:
        if(os.path.isdir(os.path.join(Path, File))):
           for index, Image in enumerate(os.listdir(os.path.join(Path, File))):
                # Convert the path into a file
                ImageCV.append(cv2.imread(os.path.join(Path, File) + os.path.sep + Image))
                ImageCV[index] = cv2.resize(ImageCV[index], (700, 600)) 

                # Add a label for each image and remove the file extension
                LabelList.append(classes.index(os.path.splitext(File)[0]))
        else:
            ImageCV.append(cv2.imread(os.path.join(Path, File) + os.path.sep + Image))    
            # Add a label for each image and remove the file extension
            LabelList.append(classes.index(os.path.splitext(File)[0]))
    return ImageCV, LabelList

model = Sequential()
model.add(Conv2D(64, kernel_size=(3,3), padding="same",activation="relu", input_shape=(605,700,3)))
model.add(MaxPooling2D((2, 2)))
model.add(Conv2D(128,  kernel_size=(4,4), padding="same",activation="relu"))
model.add(MaxPooling2D((2, 2)))
model.add(Flatten())
model.add(Dense(1, activation='sigmoid'))
model.compile(optimizer='RMSprop', loss='binary_crossentropy', metrics=['accuracy'])

data, labels = ReadImages(TRAIN_DIR)
print(data[0])
model.fit(np.array(data), np.array(labels), epochs=10, batch_size=20)

model.save('model.h5')

detail: when I print (data[0]) it return:

  [0 0 0]
  [0 0 0]
  ...
  [1 0 0]
  [2 2 2]
  [0 0 0]]

 [[3 0 0]
  [3 0 0]
  [3 0 0]
  ...
  [2 0 0]
  [2 2 2]
  [0 0 0]]

 [[8 0 0]
  [8 0 0]
  [8 0 0]
  ...
  [2 0 0]
  [0 0 0]
  [0 0 0]]

 ...

 [[0 0 0]
  [0 0 0]
  [0 0 0]
  ...
  [0 0 0]
  [0 0 0]
  [0 0 0]]

 [[0 0 0]
  [0 0 0]
  [0 0 0]
  ...
  [0 0 0]
  [0 0 0]
  [0 0 0]]

 [[0 0 0]
  [0 0 0]
  [0 0 0]
  ...
  [0 0 0]
  [0 0 0]
  [0 0 0]]]

What should I do to fix it? I appreciate any help

$\endgroup$
  • $\begingroup$ By the looks of it, I am guessing you have two classes with 71 images and when you are doing np.array(data) It's treating it as a single array. Could you try data.values instead? It should most probably fix the issue. $\endgroup$ – Danny Sep 3 '19 at 16:14
  • $\begingroup$ AttributeError: 'list' object has no attribute 'values' $\endgroup$ – Gilberto Sep 3 '19 at 16:49
  • $\begingroup$ Oh sorry!!! Why do you have lists as the return values? I am assuming you should have a data frame. Can you check data[0] now? $\endgroup$ – Danny Sep 3 '19 at 16:58
  • $\begingroup$ the lists return all the imgs from folder... And the return of data[0] is up on question. Can you be more clear? $\endgroup$ – Gilberto Sep 3 '19 at 17:05
  • $\begingroup$ Apologies for not being clear! I want to check your shape of data[0] and type(data[0]). If type(data[0]) is np.array, then try doing np.stack(data). $\endgroup$ – Danny Sep 3 '19 at 17:14
0
$\begingroup$
def ReadImages(Path):
    LabelList = list()
    ImageCV = list()
    classes = ["nonPdr", "pdr"]

   # Get all subdirectories
   FolderList = [f for f in os.listdir(Path) if not f.startswith('.')]
   print(FolderList)

   # Loop over each directory
   for File in FolderList:
      for index, Image in enumerate(os.listdir(os.path.join(Path, File))):
          # Convert the path into a file
          ImageCV.append(cv2.resize(cv2.imread(os.path.join(Path, File) + os.path.sep + Image), (600,700)))
          LabelList.append(classes.index(os.path.splitext(File)[0])) 

   return ImageCV, LabelList

A bit of change to your code after discussion in the chat room.

| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.