Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keras Convolution Neural Network

Tags:

keras

theano

Right now I am trying to construct a basic convolutional neural network to do simple classification with mnist dataset using keras. Eventually I want to put my own images in I just want to build a simple network first to make sure I have the structure working. so I downloaded the mnist data as mnint.pkl.gz unpacked it and loaded it into tuples and eventually bumpy arrays. Here is my code:

import numpy as np
from keras.models import Sequential

from keras.layers.core import Dense, Dropout, Activation, Flatten
from keras.layers.convolutional import Convolution2D, MaxPooling2D
from keras.optimizers import SGD
from PIL import Image as IM
import theano
from sklearn.cross_validation import train_test_split
import cPickle
import gzip
f=gzip.open('mnist.pkl.gz')
data1,data2,data3=cPickle.load(f)
f.close()

X=data1[0]
Y=data1[1]

x=X[0:15000,:]
y=Y[0:15000]

X_train,X_test,y_train,y_test=train_test_split(x,y,test_size
=0.33,random_state=99)


model=Sequential()
model.add(Convolution2D(10,5,5,border_mode='valid', 
input_shape=   (1,28,28)))
model.add(Activation('tanh'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Flatten())
model.add(Dense(10))
model.add(Activation('softmax'))
sgd = SGD(lr=0.1, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss='mean_squared_error', optimizer=sgd)
model.fit(X_train,y_train, batch_size=10, nb_epoch=10)
score=model.evaluate(X_test,y_test,batch_size=10)
print(score)

I get an error as such:

'Wrong number of dimensions: expected 4, got 2 with shape 
(10,   784).')

I think this means I need to put it into a theano 4d tensor such that is has (samples,channels,rows,columns) but I have no idea how to do that. Furthermore when I specifically want to solve the problem I am after I will we loading '.png' files in, I was then going to put them into numpy matrices to feed in but it looks like that is not going to work. Can anybody tell me how I can get Images into theano4d tensors to use in this code? Thanks

like image 863
Joshua Mannheimer Avatar asked Mar 01 '26 17:03

Joshua Mannheimer


2 Answers

You are correct that the code is expecting a tensor4. The conventional structure is (batch, channel, width, height). In this case the images are monochrome so channel=1 It looks like you're using a batch size of 10 and the MNIST images are 28 pixels in width and 28 pixels in height.

You can simply reshape the data into the format required. If x is of shape (10, 784) then x.reshape(10, 1, 28, 28) will have the required format.

like image 97
Daniel Renshaw Avatar answered Mar 04 '26 04:03

Daniel Renshaw


The code is expecting a 4-dimensional numpy array, not a Theano tensor (keras does all the Theano tensor manipulation under the hood).

Your inputs, X_train and X_test need to be reshaped as follows:

X_train = X_train.reshape(-1, 1, 28, 28)
X_test = X_test.reshape(-1, 1, 28, 28)
like image 38
Jonathan Avatar answered Mar 04 '26 03:03

Jonathan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!