Home > database >  How to import images from a folder and set part of them as testing data and part of them as training
How to import images from a folder and set part of them as testing data and part of them as training

Time:09-26

I am trying to import all 60,000 images (50,000 for training and 10,000 for testing) inside a directory (the directory location is known) in Python for image classification using TensorFlow. I want to import all images and let them

path = /home/user/mydirectory

I try code:

import numpy as np                                               
import keras                                                     
from keras.models import Sequential                              
from keras.layers import Dense, Dropout, Activation, Flatten     
from keras.layers import Conv2D, MaxPooling2D                    
from keras.preprocessing.image import ImageDataGenerator         
import matplotlib.pyplot as plt                                  
from keras.utils import np_utils          

from PIL import Image
import glob
image_list = []
for filename in glob.glob(r'/home/user/mydirectory*.gif'): #assuming gif
    im=Image.open(filename)
    image_list.append(im)
(x_train, y_train)=image_list()
(x_test, y_test)=image_list()

However, the error is TypeError: 'list' object is not callable...

CodePudding user response:

You should have

... = image_list

in the last 2 lines above instead of

... = image_list()

as it is a list, not a callable function.

CodePudding user response:

You are calling list image_list() in the last and second last line. You are trying to split into train and label, but you don't have a category added to image_list.

You can create a folder with a category name; for example, suppose you have two classes, dog and cat, then you can create training directory like:

home/usr/mydirectory/train/cat
home/usr/mydirectory/train/dog

and use Image Data Generator:

train_datagen = ImageDataGenerator(rescale = 1./255,
                               shear_range = 0.2,
                               zoom_range = 0.2,
                               horizontal_flip = True)
training_set =train_datagen.flow_from_directory(  
                                            'home/usr/mydirectory/train/',
                                             target_size = (64, 64),
                                             batch_size = 32,
                                             class_mode = 'binary')

Similarly, for test data, you can create a directory with test and store images with the category as the folder name.

  • Related