简体   繁体   English

从本地文件夹导入图像,而不是使用 Keras/Tensorflow 数据集

[英]importing images from local folder instead of using Keras/Tensorflow dataset

Hi Can someone please help me to change this code so that it wont get data from keras mnist.嗨,有人可以帮我更改此代码,以免它从 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.以及在这段代码中我们可以使用 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'])
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.注意排序的变化。

For doing what you want, You have multiple options.为了做你想做的事,你有多种选择。 I write an approach with using flow_from_directory .我使用flow_from_directory编写了一种方法。 For using this appraoch your data in local folder should be like this:为了使用这种方法,您在本地文件夹中的数据应该是这样的:

在此处输入图像描述


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')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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