I have 3 folders in my dataset test,train and metadata.
In my train folder i have13000 images of animals. i have 6000 images of animals in my test folder. In my metadata folder i have 2 csv files train,test. In my train.csv file, one column consists of image names in train folder and other column consists of the animal species to which it belongs. There are totally 30 types of species of animal images are given in train folder.
I want to build a neural network that scans the image and predicts to which species the animal in the image belongs to.
For this, im building a convolution neural network using keras. During the image preprocessing , I cannot use
flow_from_directory() as it requires train data to be put in its sub folderfor each species (which is not the way my current dataset is).
I know i must use flow() method. but im not able to find a good tutorial on flow() method of ImageDatagenerator class.
Can someone please tell me how to train my cnn using these data . I know i should convert images to numpy array. I have converted them. now please tell me how do i input it to x,y variables of flow() method.
Hi @Mostly_Panda95,
You can follow the below steps to read the images and convert them to array:
# importing the libraries
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from keras.preprocessing import image
%matplotlib inline
Now we have to read the train.csv and test.csv file
train = pd.read_csv('path of train.csv file')
test = pd.read_csv('path of test.csv file')
Now give the path of train and test folder
TRAIN_PATH = './train/'
TEST_PATH = './test/'
You must replace this path with the path in which your train and test folders are. Now we will read the images from train and test folder:
from PIL import Image
from imageio import imread
from skimage.transform import resize
import cv2
from tqdm import tqdm
# defining a function to read images
def read_img(img_path):
img = image.load_img(img_path, target_size=(224, 224, 3))
img = image.img_to_array(img)
return img
# reading the images
train_img = []
for img_path in tqdm(train.Image_id.values):
train_img.append(read_img(TRAIN_PATH + img_path))
Similarly you can read the test images. Now you have to convert these images to array form:
X_train = np.array(train_img, np.float32)
After reading and converting the images to array form, you need there corresponding labels, i.e. you have to assign a class(out of 30 classes) to each image:
from sklearn.preprocessing import LabelEncoder
lb = LabelEncoder()
lb.fit(train.Animal.values)
y = lb.transform(train.Animal.values)
Now, X_train contains all the images and y contains their corresponding class. You can build you model on them.
2 Likes