Skip to content

Instantly share code, notes, and snippets.

@jayendra13
Last active August 29, 2015 14:18
Show Gist options
  • Save jayendra13/b0b54534974549d1caed to your computer and use it in GitHub Desktop.
Save jayendra13/b0b54534974549d1caed to your computer and use it in GitHub Desktop.
import os
import numpy as np
from skimage import io
from nolearn.lasagne import NeuralNet
from lasagne.layers import InputLayer
from lasagne.layers import Conv2DLayer
from lasagne.layers import MaxPool2DLayer
from lasagne.layers import DropoutLayer
from lasagne.layers import DenseLayer
from lasagne.updates import nesterov_momentum
from lasagne.nonlinearities import softmax
def load_data():
labels = {}
f = open('trainLabels.csv','r')
lines = f.readline()
lines = f.readlines()
for l in lines:
l = l.strip()
l = l.split(',')
labels[l[0]] = int(l[1])
img_list = os.listdir(r'../data/train')
n = len(img_list)
x = np.zeros((n,1,256,256))
y = np.zeros((n,))
i = 0
for img in img_list:
if img.endswith('.jpeg'):
ii = io.imread(os.path.join(r'../data/train',img))
x[i,:,:] = ii[:,:,1]
y[i] = labels[img.replace('.jpeg','')]
i += 1
print i
x /= 255.0
y = y.astype('int32')
return x,y
def get_test_data():
img_list = os.listdir(r'../data/test')
n = len(img_list)
x = np.zeros((n,1,256,256))
i = 0
lst = []
for img in img_list:
if img.endswith('.jpeg'):
ii = io.imread(os.path.join(r'../data/train',img))
x[i,:,:] = ii[:,:,1]
lst.append(img.replace('.jpeg',''))
i += 1
print i
return x,lst
def build_model():
l = [('input', InputLayer),
('conv1', Conv2DLayer), ('pool1', MaxPool2DLayer), ('drop1',DropoutLayer),
('conv2', Conv2DLayer), ('pool2', MaxPool2DLayer), ('drop2',DropoutLayer),
('conv3', Conv2DLayer), ('pool3', MaxPool2DLayer),('drop3',DropoutLayer),
('output',DenseLayer)]
net = NeuralNet(layers=l, input_shape=(None, 1, 256, 256),
conv1_num_filters=10, conv1_filter_size=(3,3),
pool1_ds = (3,3), drop1_p=0.5,
conv2_num_filters=10, conv2_filter_size=(3,3),
pool2_ds = (3,3), drop2_p=0.5,
conv3_num_filters=10, conv3_filter_size=(3,3),
pool3_ds = (3,3), drop3_p=0.5,
output_num_units = 5,
output_nonlinearity = softmax,
update=nesterov_momentum,
update_learning_rate=0.01,
update_momentum=0.9,
eval_size=0.2, verbose=1, max_epochs=200)
return net
if __name__ == '__main__':
x,y = load_data()
net = build_model()
net = net.fit(x,y)
xtest,lst = get_test_data()
yhat = net.predict(xtest)
yhat = yhat.astype('int32')
f = open('submission.csv','w')
f.write('image,level\n')
i = 0
for l in lst:
f.write(l+','+str(yhat[i])+'\n')
i += 1
f.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment