Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- """
- Created on Fri Oct 7 15:44:38 2016
- @author: anpa
- """
- import os
- import sys
- import timeit
- import signal
- import numpy
- import matplotlib.pyplot as plt
- import theano
- import theano.tensor as T
- from dense import DenseLayer
- import updates as update_rules
- import lenet5
- import utils
- from utils import eprint
- theano.config.blas.ldflags='-lopenblas'
- cxxflag1 = '-IC:\\OpenBLAS-v0.2.15-Win64-int32\\include '
- cxxflag2 = '-LC:\\OpenBLAS-v0.2.15-Win64-int32\\lib'
- theano.config.gcc.cxxflags= cxxflag1 + cxxflag2
- # dataset root folder
- data_dir = ''
- # training set input file
- training_im_file = ''
- # training set labels file
- training_lb_file = ''
- # test set input file
- test_im_file = ''
- # test set labels file
- test_lb_file = ''
- # number of samples per batch
- batch_size = 1273
- # number of batches
- nbatch = 637
- # input image dimensions
- img_rows, img_cols = 20, 195
- # number of convolutional filters to use
- nkernels = [30, 30]
- # size of pooling area for max pooling
- pooling_size = (2, 2)
- # first convolution kernel size
- kernel_size = [(3, 16), (2, 5)]
- # number of epochs
- nepochs = 2
- SIGTERM_FLAG = False
- def sigterm_handler(_signo, _stack_frame):
- global SIGTERM_FLAG
- print('SIGTERM Caught!')
- SIGTERM_FLAG = True
- def evaluate_lenet5():
- bg = utils.batch_generator(data_dir + training_im_file,
- data_dir + training_lb_file,
- (img_rows, img_cols), r_eff, nbatch, batch_size)
- train_set_x, train_set_y = next(bg)
- valid_set_x, valid_set_y = utils.get_testset(data_dir + test_im_file,
- data_dir + test_lb_file,
- (img_rows, img_cols), r_eff)
- x = T.dtensor4('x')
- y = T.dvector('y')
- print('... building the model')
- layer0 = lenet5.LeNetConvPoolLayer(
- input=x,
- image_shape=(None, 1, img_rows, img_cols),
- filter_shape=(nkernels[0], 1, kernel_size[0][0], kernel_size[0][1]),
- poolsize=(2, 2)
- )
- layer1 = lenet5.LeNetConvPoolLayer(
- input=layer0.output,
- image_shape=(None, nkernels[0], layer0.dim0, layer0.dim1),
- filter_shape=(nkernels[1], nkernels[0],
- kernel_size[1][0], kernel_size[1][1]),
- poolsize=(2, 2)
- )
- layer2_input = layer1.output.flatten(2)
- # construct a fully-connected sigmoidal layer
- layer2 = DenseLayer(
- input=layer2_input,
- n_in=nkernels[1] * layer1.dim0 * layer1.dim1,
- n_out=20,
- activation=T.tanh
- )
- layer3 = DenseLayer(
- input=layer2.output,
- n_in=20,
- n_out=15,
- activation=T.tanh
- )
- layer4 = DenseLayer(input=layer3.output, rng=rng, n_in=15, n_out=1,
- activation=None)
- cost = layer4.mse(y)
- params = layer4.params + layer3.params + layer2.params + layer1.params + layer0.params
- grads = T.grad(cost, params)
- sgd = update_rules.SGD(lr=learning_rate, momentum=0.9)
- Adam = update_rules.Adam()
- updates = Adam.get_updates(params, cost, grads)
- validate_model = theano.function(
- [],
- cost,
- givens={
- x: valid_set_x,
- y: valid_set_y
- }
- )
- train_model = theano.function(
- [],
- cost,
- updates=updates,
- givens={
- x: train_set_x,
- y: train_set_y
- }
- )
- preds = theano.function(inputs=[],
- outputs=layer4.y_pred,
- givens={
- x: valid_set_x,
- }
- )
- print('... training')
- validation_frequency = 100
- best_validation_loss = numpy.inf
- start_time = timeit.default_timer()
- epoch = 0
- train_cost = []
- test_cost = []
- signal.signal(signal.SIGINT, sigterm_handler)
- while (epoch < nepochs):
- epoch = epoch + 1
- for minibatch_index in range(nbatch):
- iter = (epoch - 1) * nbatch + minibatch_index
- if SIGTERM_FLAG is True:
- sys.exit(1)
- cost_ij = train_model()
- train_cost.append(cost_ij)
- if iter % 100 == 0:
- print('training @ iter = ', iter)
- print('training error %f'%cost_ij)
- for i,p in enumerate(params):
- curr_p = p.get_value(borrow=True)
- if len(curr_p.shape) > 2:
- curr_p = curr_p.flatten()
- numpy.savetxt('parameters'+str(i), curr_p)
- if (iter + 1) % validation_frequency == 0:
- validation_losses = validate_model()
- this_validation_loss = numpy.mean(validation_losses)
- test_cost.append(this_validation_loss)
- print('epoch %i, minibatch %i/%i, validation error %f' %
- (epoch, minibatch_index + 1, nbatch,
- this_validation_loss))
- end_time = timeit.default_timer()
- print('Optimization complete.')
- for i,p in enumerate(params):
- curr_p = p.get_value(borrow=True)
- if len(curr_p.shape) > 2:
- curr_p = curr_p.flatten()
- numpy.savetxt('./parameters'+str(i), curr_p)
- numpy.savetxt('./training_loss', train_cost)
- numpy.savetxt('./test_loss', test_cost)
- print('Best validation score of %f obtained at iteration %i, '
- 'with test performance %f' %
- (best_validation_loss, best_iter + 1, test_score))
- print(('The code for file ' +
- os.path.split(__file__)[1] +
- ' ran for %.2fm' % ((end_time - start_time) / 3600.)),
- file=sys.stderr)
- pds = preds()
- plt.figure('Vanilla')
- plt.scatter(test_set_y, pds)
- plt.plot([numpy.amin(test_set_y),
- numpy.amax(test_set_y)],
- [numpy.amin(test_set_y),
- numpy.amax(test_set_y)])
- plt.show()
- return
- if __name__ == '__main__':
- evaluate_lenet5()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement