简体   繁体   中英

How to load the image data set from computer and split into two data set for training and testing?

Image data description: 2D binary images with 200x200 size

123 labels are present, each class (label) contains 10 image frames, where the first 4 images I considered as test case remaining will be training dataset.

data_Path='C:\\GaitDatasetB-silh_PerfectlyAlingedImages_Active_EnergyImage\\'

In that code inbuilt mnist dataset is loaded where I want to load my image dataset for classification.

how may I do it?

How to load the image data set from my computer and split into two data set for training and testing? as per described above.

python code:

    import keras
    from keras.datasets import mnist
    from keras.models import Sequential
    from keras.layers import Dense, Dropout, Flatten
    from keras.layers import Conv2D, MaxPooling2D
    import numpy as np

    batch_size = 128
    num_classes = 10
    epochs = 12

    # input image dimensions
    img_rows, img_cols = 28, 28

    # the data, split between train and test sets
    (x_train, y_train), (x_test, y_test) = mnist.load_data() # I want to load data from data_Path='C:\GaitDatasetB-silh_PerfectlyAlingedImages_Active_EnergyImage\'

    x_train = x_train.reshape(60000,28,28,1)
    x_test = x_test.reshape(10000,28,28,1)

    print('x_train shape:', x_train.shape)
    print(x_train.shape[0], 'train samples')
    print(x_test.shape[0], 'test samples')

In that code inbuilt mnist dataset is loaded where I want to load my image dataset for classification.

how may I do it?

reference of code: https://towardsdatascience.com/build-your-own-convolution-neural-network-in-5-mins-4217c2cf964f

There's this package for processing image data. The skimage.io.imread is returning an ndarray , and this works perfectly for keras. So you can read the data like this:

all_images = []
for image_path in os.listdir(path):
  img = io.imread(image_path , as_grey=True)
  img = img.reshape([WIDTH, HEIGHT, 1])
  all_images.append(img)
x_train = np.array(all_images)

Now you have training data ready. you'll need to make an array of labels as well. I call it y_train . You can convert it to one-hot like this:

y_train = keras.utils.to_categorical(y_train, num_classes)

Rest everything is same as MNIST example.

I prepared my code as per your suggestion:

    path1='C:\\Data\\For new Paper3\Old\\GaitDatasetB-silh_PerfectlyAlingedImages_EnergyImage\\';
    all_images = []
    subjects = os.listdir(path1)
    numberOfSubject = len(subjects)
    print('Number of Subjects: ', numberOfSubject)
    for number1 in range(0, numberOfSubject):  # numberOfSubject
        path2 = (path1 + subjects[number1] + '/')
        sequences = os.listdir(path2);
        numberOfsequences = len(sequences)
        for number2 in range(4, numberOfsequences):
            path3 = path2 + sequences[number2]
            img = cv2.imread(path3 , 0)
            img = img.reshape(200, 200, 1)
            all_images.append(img)
    x_train = np.array(all_images)

    y_train = keras.utils.to_categorical(y_train, num_classes)

but last line code is reflecting an error:

y_train = keras.utils.to_categorical(y_train, num_classes) NameError: name 'y_train' is not defined

what should I do to store the labels in y_train variable? at the running time of second for loop the label should be same for all image.

follow my code so that it can be embedded with CNN procedure. https://towardsdatascience.com/build-your-own-convolution-neural-network-in-5-mins-4217c2cf964f

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM