1

Below I would like to store the images in my laptop to the variable called X_data by using the function of glob and then split it into training and test set before testing the model.

 import cv2
 import numpy as np
 import tensorflow as tf
 from tensorflow.keras import datasets, layers, models
 import matplotlib.pyplot as plt
 import glob

 X_data = []
 files = glob.glob ("*.png")
 for myFile in files:
    image = cv2.imread(myFile)
    X_data.append(image)

 print('X_data shape:', np.array(X_data).shape)

import numpy
numpy.random.shuffle(X_data)
training, test = X_data[:80,:], X_data[80:,:]

model = models.Sequential()
model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3)))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.Flatten())
model.add(layers.Dense(64, activation='relu'))
model.add(layers.Dense(10))
model.summary()
model.compile(optimizer='adam',
                  loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
                  metrics=['accuracy'])
history = model.fit(training, labels_train, epochs=10, 
                                    validation_data=(test, labels_test))

But I'm getting

ValueError: Failed to find data adapter that can handle input: <class 'numpy.ndarray'>, (<class 'list'> containing values of types {"<class 'int'>"})

Any ideas?

5
  • @desertnaut thanks for your edit, but the problem persists.. is there a problem because I don't specify X_train, X_test, Y_train, Y_test? Commented Apr 23, 2020 at 15:46
  • You do specify them, although with different names. First thing I would check is if you have integers in your arrays; if yes, try changing them to floats. Commented Apr 23, 2020 at 15:49
  • I have added the codes "training = training.astype(float) and test = test.astype(float)" but the problem persists. Commented Apr 23, 2020 at 15:56
  • Which line and variables are causing the problem. A full traceback might help. But you can also provide the shape and dtype of the relevant variables. I'm guessing that tesorflow is objecting to an array with object dtype containing lists (and probably 1d). Find that array and check the len of all its list elements. Do any differ? np.array makes an object array if it can't make a near multidimensional array from the inputs. Commented Apr 23, 2020 at 20:17
  • @hpaulj the error is in the validation_data (last line). labels_test returns a [1]. type(labels_test) returns a list. len(labels_test) returns a 1. Print(test) returns a []. len(test) returns a 0. type(test) returns a numpy.ndarray Commented Apr 23, 2020 at 20:38

1 Answer 1

1

I see couple of problems in your code:

  1. Both train and test are Lists, not Numpy Arrays. Same might be the case with Labels (that part of code is not shared).
  2. The part of code,

    X_data = [] files = glob.glob ("*.png") for myFile in files: image = cv2.imread(myFile) X_data.append(image)

returns a List of Strings, rather than a Numpy Array of Floats and the code,

training = training.astype(float) and test = test.astype(float) will not resolve the issue.

['cat.373.jpg', 'cat.843.jpg', 'cat.970.jpg', 'cat.797.jpg', 'cat.786.jpg',
'cat.1.jpg', 'cat.172.jpg', 'cat.660.jpg'...........]

Is there any specific reason that you wanted to use the function, glob.glob to access the Images from the folders because there are better ways to do it.

One of the most efficient and optimized way to do this is to use Keras ImageDataGenerator and ImageDataGenerator.flow_from_directory.

For example, if your Image Data Directory has Folders, each folder corresponding to each Class, as shown in the screenshot below,

enter image description here

Code to show how to use ImageDataGenerator, along with detailed explanation of every line of code is mentioned below:

train_datagen = ImageDataGenerator(rescale=1./255, # Normalizes every pixel value
    validation_split=0.2) # Setting Validation Data as 20% of Total Data

train_generator = train_datagen.flow_from_directory(
    Image_data_dir, # Traverses through all the Sub Folders (Category) inside this dir
    target_size=(img_height, img_width), # Sets the Image Size
    batch_size=batch_size, # Generates batches of `batch_size`
    class_mode='categorical', # Will Consider Labels as Categorical
    shuffle = True, # Shuffles the Data
    subset='training') # Considers 80% as training data

# Since we don't have separate directory for Validation Data and since we want the Total Data to be Partitioned, we should use "train_datagen"
validation_generator = train_datagen.flow_from_directory(
    Image_data_dir, # Should use the Same Dir as Training for Splitting
    target_size=(img_height, img_width), 
    batch_size=batch_size,
    class_mode='categorical',
    shuffle = True, # Shuffles the Data
    subset='validation') # Considers 20% as Validation data

# Then you can train the model using the code mentioned below
model.fit(
    train_generator,
    steps_per_epoch = train_generator.samples // batch_size,
    validation_data = validation_generator, 
    validation_steps = validation_generator.samples // batch_size,
    epochs = nb_epochs)

Hope this solves your problem. If you still face any error, please share the complete code along with the error trace.

Happy Learning!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.