Struggling to put my input images in the right format for CNN (tensorflow)

24 Views Asked by At

I am trying to build my first image classifier to distinguish cats and dogs using a CNN, and I am struggling to load my data. The data is stored on my computer in two folders: one for my training data set and one for my testing data set. The images in my training folder are called cat.2.jpg or dog.5.jpg for example (using the data set from this competition https://www.kaggle.com/c/dogs-vs-cats), and the images in the testing folder are called 23.jpg etc (i.e. no labels).

The way I have my code set up so far is as follows:

  1. Load the data
import os
import pandas as pd
from PIL import Image
import numpy as np
import tensorflow as tf
from keras.preprocessing.image import load_img, img_to_array

# Paths to train and test folders
train_folder = "/Users/Sara/Documents/Datasets/CatsDogs/train"
test_folder = "/Users/Sara/Documents/Datasets/CatsDogs/test1"

# Function to load and preprocess images
def load_images(folder):
    data = []
    for filename in os.listdir(folder):
        if filename.endswith(".jpg"):
            img = Image.open(os.path.join(folder, filename))
            img = img.resize((224, 224))  # Resize images to 224x224 
            img_array = tf.convert_to_tensor(np.array(img) / 255.0)  # Normalise pixel values to [0, 1]
            data.append({'filename': filename, 'image': img_array})
    return data

# Load training and test images
train_data = load_images(train_folder)
test_data = load_images(test_folder)

# Create DataFrames
df_train = pd.DataFrame(train_data)
df_test = pd.DataFrame(test_data)

# Extract labels from filenames (for training data)
df_train['label'] = df_train['filename'].apply(lambda x: x.split('.')[0])  # Extract label from filename
df_train['label'] = df_train['label'].map({'cat': 0, 'dog': 1})  # Map labels to numeric values
  1. Build (a simple) CNN
from tensorflow import keras
from tensorflow.keras import layers

model = keras.Sequential([
    layers.InputLayer(shape = [224,224,3]),
    
    layers.Conv2D(kernel_size = 3, filters = 32, activation="relu"),
    layers.MaxPool2D(),

    layers.Conv2D(kernel_size = 3, filters = 64, activation="relu"),
    layers.MaxPool2D(),

    layers.Flatten(),
    layers.Dense(units=128, activation='relu'),
    layers.Dense(units = 2, activation='sigmoid'),
])

model.compile(
    optimizer = 'adam',
    loss = 'binary_crossentropy',
    metrics = ['binary_accuracy']
)
  1. Train the Model
history = model.fit(
    df_train, 
    epochs = 10, 
    verbose = 1)

The problem is that running the last step, I get the following error: 'ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type tensorflow.python.framework.ops.EagerTensor).'

I have tried a couple of different things, and tried adding the 'tf.convert_to_tensor' above, but I keep getting the error. I am assuming that the error comes from the way my data is loaded, but I don't understand how to fix it.

0

There are 0 best solutions below