Home > Blockchain >  importing images from local folder instead of using Keras/Tensorflow dataset
importing images from local folder instead of using Keras/Tensorflow dataset

Time:06-28

Hi Can someone please help me to change this code so that it wont get data from keras mnist. instead it will be getting data from local folder. where do i need to make changes in it. and where in this code we can use shuffle = true.

# Credits: https://github.com/keras-team/keras/blob/master/examples/mnist_cnn.py

from __future__ import print_function
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
from keras import backend as K
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()

if K.image_data_format() == 'channels_first':
    x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols)
    x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols)
    input_shape = (1, img_rows, img_cols)
else:
    x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1)
    x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1)
    input_shape = (img_rows, img_cols, 1)
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255
print('x_train shape:', x_train.shape)
print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples')
# convert class vectors to binary class matrices
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)
model1 = Sequential()
model1.add(Conv2D(32, kernel_size=(3, 3),
                 activation='relu',
                 input_shape=input_shape))
model1.add(Conv2D(32, (3, 3), activation='relu'))
model1.add(MaxPooling2D(pool_size=(2, 2)))
model1.add(Conv2D(64, (3, 3), activation='relu'))
model1.add(Dropout(0.25))
model1.add(Flatten())
model1.add(Dense(128, activation='relu'))
model1.add(Dropout(0.5))
model1.add(Dense(num_classes, activation='softmax'))
model1.compile(loss=keras.losses.categorical_crossentropy,
              optimizer=keras.optimizers.Adadelta(),
              metrics=['accuracy'])

CodePudding user response:

x = # some array containing image data
y = # some array containing labels

(x_train, x_test), (y_train, y_test) = sklearn.model_selection.train_test_split(x, y)  # shuffle=True by default

Note the change in ordering.

CodePudding user response:

For doing what you want, You have multiple options. I write an approach with using enter image description here


Loading images from path:

import tensorflow as tf

datagen = tf.keras.preprocessing.image.ImageDataGenerator(rescale=1./255)
train_generator = datagen.flow_from_directory(
        'data/train',
        target_size=(100, 100),
        batch_size=32,
        shuffle=True,
        class_mode='categorical')
test_generator = datagen.flow_from_directory(
        'data/test',
        target_size=(100, 100),
        batch_size=32,
        class_mode='categorical')

Output:

Found 30 images belonging to 3 classes.
Found 30 images belonging to 3 classes.

Train model after loading data: (accuracy decrease because images are random numbers and they are not real images.)

from tensorflow.keras import Sequential
from tensorflow.keras.layers import Conv2D, Flatten, Dense, Dropout, MaxPooling2D


model1 = Sequential()
model1.add(Conv2D(32, kernel_size=(3, 3),
                 activation='relu',
                 input_shape=(100,100,3)))
model1.add(Conv2D(32, (3, 3), activation='relu'))
model1.add(MaxPooling2D(pool_size=(2, 2)))
model1.add(Conv2D(64, (3, 3), activation='relu'))
model1.add(Dropout(0.25))
model1.add(Flatten())
model1.add(Dense(128, activation='relu'))
model1.add(Dropout(0.5))
model1.add(Dense(4, activation='softmax'))
model1.compile(loss=tf.keras.losses.categorical_crossentropy,
              optimizer=tf.keras.optimizers.Adadelta(),
              metrics=['accuracy'])

model1.fit(train_generator, epochs=2)
model1.evaluate(test_generator)

Output:

Epoch 1/2
1/1 [==============================] - 3s 3s/step - loss: 1.3012 - accuracy: 0.3667
Epoch 2/2
1/1 [==============================] - 2s 2s/step - loss: 1.3266 - accuracy: 0.3000
1/1 [==============================] - 1s 754ms/step - loss: 1.3075 - accuracy: 0.3333
[1.3074820041656494, 0.3333333432674408]

Generating random images in path like the image:

import numpy as np
from PIL import Image

for idx, loc in enumerate(['data/train', 'data/test']*10):
    for category in ['apple', 'orange', 'banana']:
        imarray = np.random.rand(100,100,3) * 255
        im = Image.fromarray(imarray.astype('uint8')).convert('RGB')
        im.save(f'{loc}/{category}/img_{idx}.png')
  • Related