Search code examples
pythonartificial-intelligenceconv-neural-network

AI-CNN Python loading my own data to Python


I am about writing a code for Image Classification. With ImageDataGenerator i wrote my code but it was working very slow ( 1s = 1 step ) . I want to load my own data as a np array but i got some errors.

The output should be: (15500 , 45 ,45,3) (4000, 1) (15500 , 45 ,45,3) (4000,1)

But i got :

(15500 , 45 ,45,3) (4000, ) (15500 , 45 ,45,3) (4000,)

import numpy as np 
import pandas as pd 
import matplotlib.pyplot as plt
import tensorflow as tf
import keras
import glob
import cv2
import os


TEST_DATADIR = "C:/Users/TCSEAKIN/Desktop/Py/AI-hack/AI/Test"
TRAIN_DATADIR = "C:/Users/TCSEAKIN/Desktop/Py/AI-hack/AI/Training"
CATAGORIES = ["Armut", "Portakal", "Cilek", "Muz", "Portakal", "Elma_Kirmizi", "Elma_Yesil", "Mandalina"]


fruit_images = []
labels = [] 
for category in CATAGORIES :
    path = os.path.join(TRAIN_DATADIR, category)
    for img in os.listdir(path):
        image = cv2.imread(os.path.join(path,img), cv2.IMREAD_COLOR)
        image = cv2.resize(image, (45, 45))
        image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)

        fruit_images.append(image)
        labels.append(path)

fruit_images = np.array(fruit_images)
labels = np.array(labels)


validation_fruit_images = []
validation_labels = [] 
for category in CATAGORIES:
   path = os.path.join(TEST_DATADIR, category)
   for img in os.listdir(path):
        image = cv2.imread(os.path.join(path,img), cv2.IMREAD_COLOR)
        image = cv2.resize(image, (45, 45))
        image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)

        validation_fruit_images.append(image)
        validation_labels.append(path)

validation_fruit_images = np.array(validation_fruit_images)
validation_labels = np.array(validation_labels)



X_train, X_test = fruit_images, validation_fruit_images
Y_train, Y_test = labels, validation_labels

#Normalize color values to between 0 and 1
X_train = X_train/255
X_test = X_test/255


print(X_train.shape)
print(Y_train.shape)
print(X_test.shape)
print(Y_test.shape)

Solution

  • use the reshape function:

    labels = labels.reshape(-1, 1)
    labels.shape