Import cifar-10

398 views
Skip to first unread message

Ali Alani

unread,
May 20, 2017, 3:32:50 AM5/20/17
to Keras-users
Hi guys,

I am already imported the MNIST dataset and train on to the DBN, from the following code 


from sklearn import datasets
print "[X] downloading data..."
dataset = datasets.fetch_mldata("MNIST Original")

(trainX, testX, trainY, testY) = train_test_split(
dataset.data / 255.0, dataset.target.astype("int0"), test_size = 0.16)

and the data was in this shape

trainX = [58000L,784L)
testX = [12000L, 10L)
trainX = [58000L,)
testX = [12000L, )


and now I need to import the CIFAR-10 in the same way to implement, but I got a problem to do that. Please, I need help to import the cifar10 in the same way I imported the MNIST and return the same format. I have tried researching on the internet but there is hardly any help available. Any expert help related to solving this error will be much appreciated.

regards

abhishek....@gmail.com

unread,
May 21, 2017, 7:27:20 AM5/21/17
to Keras-users
There's another way you could go with this.

Download the cifar-10 manually. Convert them to images from the csv file (downloaded from cifar site) using simple numpy python. 
The folder structure could be:
            Parent Folder/
                       Data/
                               Train/
                                       Class 1/ #has images of all class label 1
                                       Class 2/ #has images of all class label 2
                                       ....................... and the rest 8 classes.
                                Val/
                                       Same as Train but with validation images.
                        train.py 


In the train.py (ignore the network structure and parameters. You may use your own parameters and network):

import numpy as np
np.random.seed(123)  # for reproducibility

from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D
from keras.layers import Activation, Dropout, Flatten, Dense
from keras import backend as K

img_width, img_height = 32, 32

#Change the number of images and folders respectively
train_data_dir = 'Data/Train'
validation_data_dir = 'Data/Val'
nb_train_samples = 7964
nb_validation_samples = 2036


epochs = 10
batch_size = 4
num_classes = 10

if K.image_data_format() == 'channels_first':
    input_shape = (3, img_width, img_height)
else:
    input_shape = (img_width, img_height, 3)


model = Sequential()
model.add(Conv2D(32, (3, 3), input_shape=input_shape))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Conv2D(32, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Conv2D(64, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Flatten())
model.add(Dense(64))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(1))
model.add(Activation('sigmoid'))

model.compile(loss='binary_crossentropy',
              optimizer='rmsprop',
              metrics=['accuracy'])

train_datagen = ImageDataGenerator(rescale=1. / 255)
test_datagen = ImageDataGenerator(rescale=1. / 255)

train_generator = train_datagen.flow_from_directory(
    train_data_dir,
    target_size=(img_width, img_height),
    batch_size=batch_size,
    class_mode='binary')

validation_generator = test_datagen.flow_from_directory(
    validation_data_dir,
    target_size=(img_width, img_height),
    batch_size=batch_size,
    class_mode='binary')

model.fit_generator(
    train_generator,
    steps_per_epoch=nb_train_samples // batch_size,
    epochs=epochs,
    validation_data=validation_generator,
    validation_steps=nb_validation_samples // batch_size)

model.save_weights('first_try.h5')


Feel free to revert back if anything is left unclear. 

Regards,
Abhishek

rishi...@gmail.com

unread,
Jul 14, 2017, 11:00:16 AM7/14/17
to Keras-users, abhishek....@gmail.com
Hi Abhishek,

I am working on Keras right now and generating the training data from CIFAR10, although the images are in 32*32 size. Is there any possible way to reshape the images to 224*224 size. From your code,as there were no comments available, I figured that you are loading data and doing some preprocessing but not reshaping the images.

Regards,
Rishik Mani
Reply all
Reply to author
Forward
0 new messages