def train(event): global model label_message["text"] = "Training in progress..." label_message.update() cnn.load_data() (x_train, y_train), (x_test, y_test) = cnn.get_data() model = cnn.create_model() model.fit(x_train, y_train, batch_size=100, validation_data=(x_test, y_test)) label_message["text"] = "Done! Now testing..." label_message.update() test_loss, test_acc = model.evaluate(x_test, y_test) label_message["text"] = "Test accuracy: {}%".format( round(test_acc * 100, 2))
validation_data=(X_test, Y_test)) print 'predicting output' output = model.predict(X_test) cnn.print_error_rate_per_category(output, Y_test) def naive(X_train, Y_train, X_test, Y_test): X_train = np.sum(X_train, axis=(1, 3)) X_train = np.mean(X_train, axis=0) print 'Not done yet' if __name__ == '__main__': X_train, Y_train, X_test, Y_test = cnn.load_data() bayes(X_train, Y_train, X_test, Y_test, show_images=True) # mlp(X_train, Y_train[:,0], X_test, Y_test[:,0]) # naive(X_train, Y_train[:,0], X_test, Y_test[:,0]) # X_train -= 86 # X_train /= 255 # X_test -= 86 # X_test /= 255 # plt.hist(X_train.flatten(), bins = 200) # plt.show()
model.add(LSTM(512, return_sequences=False)) if len(Y_shape) == 1: Y_shape = np.array([Y_shape, 1]) model.add(Dense(Y_shape[1])) model.add(Activation('sigmoid')) sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True) model.compile(optimizer=sgd, loss='binary_crossentropy', metrics=['accuracy']) return model if __name__ == '__main__': X_train, Y_train, X_test, Y_test = cnn.load_data() Y_train = Y_train[:, 0] Y_test = Y_test[:, 0] X_train = X_train.reshape((X_train.shape[0], X_train.shape[2], X_train.shape[3])) X_train = X_train.transpose((0, 2, 1)) X_test = X_test.reshape((X_test.shape[0], X_test.shape[2], X_test.shape[3])) X_test = X_test.transpose((0, 2, 1)) print X_train.shape X_train -= 86 X_train /= 255 X_test -= 86
frame_probs = tk.Frame(root) frame_probs.grid(row=0, column=2) labels_prob = [] for i in range(10): labels_prob.append( tk.Label(frame_probs, text="{}: 0.00%".format(i), anchor=tk.W, justify=tk.LEFT)) labels_prob[-1].grid(row=i, sticky="W") root.mainloop() exit(0) cnn.load_data() (x_train, y_train), (x_test, y_test) = cnn.get_data() model = cnn.create_model() model.summary() model.fit(x_train, y_train, batch_size=100, validation_data=(x_test, y_test)) test_loss, test_acc = model.evaluate(x_test, y_test) print('Test accuracy:', test_acc) model.save_weights("models/cnn") model = cnn.create_model() model.load_weights("models/cnn") test_loss, test_acc = model.evaluate(x_test, y_test) print('Test accuracy:', test_acc)
def evaluate_lenet5(learning_rate=0.1, n_epochs=200, dataset='mnist.pkl.gz', nkerns=[20, 50, 50], batch_size=500): """ Demonstrates lenet on MNIST dataset :type learning_rate: float :param learning_rate: learning rate used (factor for the stochastic gradient) :type n_epochs: int :param n_epochs: maximal number of epochs to run the optimizer :type dataset: string :param dataset: path to the dataset used for training /testing (MNIST here) :type nkerns: list of ints :param nkerns: number of kernels on each layer """ rng = numpy.random.RandomState(23455) datasets = load_data(dataset) train_set_x, train_set_y = datasets[0] valid_set_x, valid_set_y = datasets[1] test_set_x, test_set_y = datasets[2] # compute number of minibatches for training, validation and testing n_train_batches = train_set_x.get_value(borrow=True).shape[0] n_valid_batches = valid_set_x.get_value(borrow=True).shape[0] n_test_batches = test_set_x.get_value(borrow=True).shape[0] n_train_batches /= batch_size n_valid_batches /= batch_size n_test_batches /= batch_size # allocate symbolic variables for the data index = T.lscalar() # index to a [mini]batch x = T.matrix('x') # the data is presented as rasterized images y = T.ivector('y') # the labels are presented as 1D vector of # [int] labels ishape = (28, 28) # this is the size of MNIST images ###################### # BUILD ACTUAL MODEL # ###################### print '... building the model' # Reshape matrix of rasterized images of shape (batch_size,56*56) # to a 4D tensor, compatible with our LeNetConvPoolLayer layer0_input = x.reshape((batch_size, 1, 56, 56)) # Construct the first convolutional pooling layer: # filtering reduces the image size to (56-5+1,56-5+1)=(52,52) # maxpooling reduces this further to (52/2,52/2) = (26,26) # 4D output tensor is thus of shape (batch_size,nkerns[0],26,26) layer0 = LeNetConvPoolLayer(rng, input=layer0_input, image_shape=(batch_size, 1, 56, 56), filter_shape=(nkerns[0], 1, 5, 5), poolsize=(2, 2)) # Construct the second convolutional pooling layer # filtering reduces the image size to (26-5+1,26-5+1)=(22,22) # maxpooling reduces this further to (22/2,22/2) = (11,11) # 4D output tensor is thus of shape (batch_size,nkerns[1],11,11) layer1 = LeNetConvPoolLayer(rng, input=layer0.output, image_shape=(batch_size, nkerns[0], 26, 26), filter_shape=(nkerns[1], nkerns[0], 5, 5), poolsize=(2, 2)) # Construct the second convolutional pooling layer # filtering reduces the image size to (11-5+1,11-5+1)=(7,7) # maxpooling reduces this further to (7/2,7/2) = (4,4) # 4D output tensor is thus of shape (batch_size,nkerns[2],11,11) layer2 = LeNetConvPoolLayer(rng, input=layer1.output, image_shape=(batch_size, nkerns[1], 11, 11), filter_shape=(nkerns[2], nkerns[1], 5, 5), poolsize=(2, 2)) # the HiddenLayer being fully-connected, it operates on 2D matrices of # shape (batch_size,num_pixels) (i.e matrix of rasterized images). # This will generate a matrix of shape (20,32*4*4) = (20,512) layer3_input = layer2.output.flatten(2) # construct a fully-connected sigmoidal layer layer3 = HiddenLayer(rng, input=layer3_input, n_in=nkerns[2] * 3 * 3, n_out=500, activation=T.tanh) layer4 = HiddenLayer(rng, input=layer3.output, n_in=500, n_out=100, activation=T.tanh) # classify the values of the fully-connected sigmoidal layer layer5 = LogisticRegression(input=layer4.output, n_in=100, n_out=3) # the cost we minimize during training is the NLL of the model cost = layer5.negative_log_likelihood(y) # create a function to compute the mistakes that are made by the model test_model = theano.function( [index], layer5.errors(y), givens={ x: test_set_x[index * batch_size:(index + 1) * batch_size], y: test_set_y[index * batch_size:(index + 1) * batch_size] }) validate_model = theano.function( [index], layer5.errors(y), givens={ x: valid_set_x[index * batch_size:(index + 1) * batch_size], y: valid_set_y[index * batch_size:(index + 1) * batch_size] }) # create a list of all model parameters to be fit by gradient descent params = layer5.params + layer4.params + layer3.params + layer2.params + layer1.params + layer0.params #only train the classifier layer #params = layer5.params #train the classifier layer and the first convolutional layer #params = layer5.params + layer0.params # create a list of gradients for all model parameters grads = T.grad(cost, params) # train_model is a function that updates the model parameters by # SGD Since this model has many parameters, it would be tedious to # manually create an update rule for each model parameter. We thus # create the updates list by automatically looping over all # (params[i],grads[i]) pairs. updates = [] for param_i, grad_i in zip(params, grads): updates.append((param_i, param_i - learning_rate * grad_i)) train_model = theano.function( [index], [cost], updates=updates, givens={ x: train_set_x[index * batch_size:(index + 1) * batch_size], y: train_set_y[index * batch_size:(index + 1) * batch_size] }) ############### # TRAIN MODEL # ############### print '... training' # early-stopping parameters patience = 10000 # look as this many examples regardless patience_increase = 2 # wait this much longer when a new best is # found improvement_threshold = 0.995 # a relative improvement of this much is # considered significant validation_frequency = min(n_train_batches, patience / 2) # go through this many # minibatche before checking the network # on the validation set; in this case we # check every epoch best_params = None best_validation_loss = numpy.inf best_iter = 0 test_score = 0. start_time = time.clock() epoch = 0 done_looping = False params_save = [] params_save.append([ layer5.W.get_value(), layer5.b.get_value(), layer4.W.get_value(), layer4.b.get_value(), layer3.W.get_value(), layer3.b.get_value(), layer2.W.get_value(), layer2.b.get_value(), layer1.W.get_value(), layer1.b.get_value(), layer0.W.get_value(), layer0.b.get_value() ]) while (epoch < n_epochs) and (not done_looping): epoch = epoch + 1 for minibatch_index in xrange(n_train_batches): iter = (epoch - 1) * n_train_batches + minibatch_index if iter % 100 == 0: print 'training @ iter = ', iter [cost_ij] = train_model(minibatch_index) if (iter + 1) % validation_frequency == 0: # compute zero-one loss on validation set validation_losses = [ validate_model(i) for i in xrange(n_valid_batches) ] this_validation_loss = numpy.mean(validation_losses) print('epoch %i, minibatch %i/%i, validation error %f %%' % \ (epoch, minibatch_index + 1, n_train_batches, \ this_validation_loss * 100.)) # if we got the best validation score until now if this_validation_loss < best_validation_loss: #improve patience if loss improvement is good enough if this_validation_loss < best_validation_loss * \ improvement_threshold: patience = max(patience, iter * patience_increase) # save best validation score and iteration number best_validation_loss = this_validation_loss best_iter = iter # test it on the test set test_losses = [ test_model(i) for i in xrange(n_test_batches) ] test_score = numpy.mean(test_losses) print( (' epoch %i, minibatch %i/%i, test error of best ' 'model %f %%') % (epoch, minibatch_index + 1, n_train_batches, test_score * 100.)) if patience <= iter: done_looping = True break params_save.append([ layer5.W.get_value(), layer5.b.get_value(), layer4.W.get_value(), layer4.b.get_value(), layer3.W.get_value(), layer3.b.get_value(), layer2.W.get_value(), layer2.b.get_value(), layer1.W.get_value(), layer1.b.get_value(), layer0.W.get_value(), layer0.b.get_value() ]) end_time = time.clock() print('Optimization complete.') print('Best validation score of %f %% obtained at iteration %i,'\ 'with test performance %f %%' % (best_validation_loss * 100., best_iter + 1, test_score * 100.)) print >> sys.stderr, ('The code for file ' + os.path.split(__file__)[1] + ' ran for %.2fm' % ((end_time - start_time) / 60.)) return [params_save, train_set_x, valid_set_x, test_set_x]
def evaluate_lenet5(learning_rate=0.1, n_epochs=200, dataset='mnist.pkl.gz', nkerns=[20, 50, 50], batch_size=500): """ Demonstrates lenet on MNIST dataset :type learning_rate: float :param learning_rate: learning rate used (factor for the stochastic gradient) :type n_epochs: int :param n_epochs: maximal number of epochs to run the optimizer :type dataset: string :param dataset: path to the dataset used for training /testing (MNIST here) :type nkerns: list of ints :param nkerns: number of kernels on each layer """ rng = numpy.random.RandomState(23455) datasets = load_data(dataset) train_set_x, train_set_y = datasets[0] valid_set_x, valid_set_y = datasets[1] test_set_x, test_set_y = datasets[2] # compute number of minibatches for training, validation and testing n_train_batches = train_set_x.get_value(borrow=True).shape[0] n_valid_batches = valid_set_x.get_value(borrow=True).shape[0] n_test_batches = test_set_x.get_value(borrow=True).shape[0] n_train_batches /= batch_size n_valid_batches /= batch_size n_test_batches /= batch_size # allocate symbolic variables for the data index = T.lscalar() # index to a [mini]batch x = T.matrix('x') # the data is presented as rasterized images y = T.ivector('y') # the labels are presented as 1D vector of # [int] labels ishape = (28, 28) # this is the size of MNIST images ###################### # BUILD ACTUAL MODEL # ###################### print '... building the model' # Reshape matrix of rasterized images of shape (batch_size,56*56) # to a 4D tensor, compatible with our LeNetConvPoolLayer layer0_input = x.reshape((batch_size, 1, 56, 56)) # Construct the first convolutional pooling layer: # filtering reduces the image size to (56-5+1,56-5+1)=(52,52) # maxpooling reduces this further to (52/2,52/2) = (26,26) # 4D output tensor is thus of shape (batch_size,nkerns[0],26,26) layer0 = LeNetConvPoolLayer(rng, input=layer0_input, image_shape=(batch_size, 1, 56, 56), filter_shape=(nkerns[0], 1, 5, 5), poolsize=(2, 2)) # Construct the second convolutional pooling layer # filtering reduces the image size to (26-5+1,26-5+1)=(22,22) # maxpooling reduces this further to (22/2,22/2) = (11,11) # 4D output tensor is thus of shape (batch_size,nkerns[1],11,11) layer1 = LeNetConvPoolLayer(rng, input=layer0.output, image_shape=(batch_size, nkerns[0], 26, 26), filter_shape=(nkerns[1], nkerns[0], 5, 5), poolsize=(2, 2)) # Construct the second convolutional pooling layer # filtering reduces the image size to (11-5+1,11-5+1)=(7,7) # maxpooling reduces this further to (7/2,7/2) = (4,4) # 4D output tensor is thus of shape (batch_size,nkerns[2],11,11) layer2 = LeNetConvPoolLayer(rng, input=layer1.output, image_shape=(batch_size, nkerns[1], 11, 11), filter_shape=(nkerns[2], nkerns[1], 5, 5), poolsize=(2, 2)) # the HiddenLayer being fully-connected, it operates on 2D matrices of # shape (batch_size,num_pixels) (i.e matrix of rasterized images). # This will generate a matrix of shape (20,32*4*4) = (20,512) layer3_input = layer2.output.flatten(2) # construct a fully-connected sigmoidal layer layer3 = HiddenLayer(rng, input=layer3_input, n_in=nkerns[2] * 3 * 3, n_out=500, activation=T.tanh) layer4 = HiddenLayer(rng, input=layer3.output, n_in = 500, n_out = 100, activation=T.tanh) # classify the values of the fully-connected sigmoidal layer layer5 = LogisticRegression(input=layer4.output, n_in=100, n_out=3) # the cost we minimize during training is the NLL of the model cost = layer5.negative_log_likelihood(y) # create a function to compute the mistakes that are made by the model test_model = theano.function([index], layer5.errors(y), givens={ x: test_set_x[index * batch_size: (index + 1) * batch_size], y: test_set_y[index * batch_size: (index + 1) * batch_size]}) validate_model = theano.function([index], layer5.errors(y), givens={ x: valid_set_x[index * batch_size: (index + 1) * batch_size], y: valid_set_y[index * batch_size: (index + 1) * batch_size]}) # create a list of all model parameters to be fit by gradient descent params = layer5.params + layer4.params + layer3.params + layer2.params + layer1.params + layer0.params #only train the classifier layer #params = layer5.params #train the classifier layer and the first convolutional layer #params = layer5.params + layer0.params # create a list of gradients for all model parameters grads = T.grad(cost, params) # train_model is a function that updates the model parameters by # SGD Since this model has many parameters, it would be tedious to # manually create an update rule for each model parameter. We thus # create the updates list by automatically looping over all # (params[i],grads[i]) pairs. updates = [] for param_i, grad_i in zip(params, grads): updates.append((param_i, param_i - learning_rate * grad_i)) train_model = theano.function([index], [cost], updates=updates, givens={ x: train_set_x[index * batch_size: (index + 1) * batch_size], y: train_set_y[index * batch_size: (index + 1) * batch_size]}) ############### # TRAIN MODEL # ############### print '... training' # early-stopping parameters patience = 10000 # look as this many examples regardless patience_increase = 2 # wait this much longer when a new best is # found improvement_threshold = 0.995 # a relative improvement of this much is # considered significant validation_frequency = min(n_train_batches, patience / 2) # go through this many # minibatche before checking the network # on the validation set; in this case we # check every epoch best_params = None best_validation_loss = numpy.inf best_iter = 0 test_score = 0. start_time = time.clock() epoch = 0 done_looping = False params_save = [] params_save.append([layer5.W.get_value(), layer5.b.get_value(), layer4.W.get_value(), layer4.b.get_value(), layer3.W.get_value(), layer3.b.get_value(), layer2.W.get_value(), layer2.b.get_value(), layer1.W.get_value(), layer1.b.get_value(), layer0.W.get_value(), layer0.b.get_value()]) while (epoch < n_epochs) and (not done_looping): epoch = epoch + 1 for minibatch_index in xrange(n_train_batches): iter = (epoch - 1) * n_train_batches + minibatch_index if iter % 100 == 0: print 'training @ iter = ', iter [cost_ij] = train_model(minibatch_index) if (iter + 1) % validation_frequency == 0: # compute zero-one loss on validation set validation_losses = [validate_model(i) for i in xrange(n_valid_batches)] this_validation_loss = numpy.mean(validation_losses) print('epoch %i, minibatch %i/%i, validation error %f %%' % \ (epoch, minibatch_index + 1, n_train_batches, \ this_validation_loss * 100.)) # if we got the best validation score until now if this_validation_loss < best_validation_loss: #improve patience if loss improvement is good enough if this_validation_loss < best_validation_loss * \ improvement_threshold: patience = max(patience, iter * patience_increase) # save best validation score and iteration number best_validation_loss = this_validation_loss best_iter = iter # test it on the test set test_losses = [test_model(i) for i in xrange(n_test_batches)] test_score = numpy.mean(test_losses) print((' epoch %i, minibatch %i/%i, test error of best ' 'model %f %%') % (epoch, minibatch_index + 1, n_train_batches, test_score * 100.)) if patience <= iter: done_looping = True break params_save.append([layer5.W.get_value(), layer5.b.get_value(), layer4.W.get_value(), layer4.b.get_value(), layer3.W.get_value(), layer3.b.get_value(), layer2.W.get_value(), layer2.b.get_value(), layer1.W.get_value(), layer1.b.get_value(), layer0.W.get_value(), layer0.b.get_value()]) end_time = time.clock() print('Optimization complete.') print('Best validation score of %f %% obtained at iteration %i,'\ 'with test performance %f %%' % (best_validation_loss * 100., best_iter + 1, test_score * 100.)) print >> sys.stderr, ('The code for file ' + os.path.split(__file__)[1] + ' ran for %.2fm' % ((end_time - start_time) / 60.)) return [params_save, train_set_x, valid_set_x, test_set_x]
import sys import tensorflow as tf import os import cv2 import numpy as np import matplotlib.pyplot as plt from matplotlib.image import imread from PIL import Image from tensorflow.keras import datasets, layers, models import matplotlib.pyplot as plt from cnn import load_data IMG_SIZE_X = 437 IMG_SIZE_Y = 308 x, y = load_data("mic0", IMG_SIZE_X, IMG_SIZE_Y) x_train = x[5:3000] y_train = y[5:3000] x_test = x[3000::] y_test = y[3000::] #print(x_train.shape) #plt.imshow(x_train[0]) #print(y_train[8]) #y_train = tf.keras.utils.to_categorical(y_train) #y_test = tf.keras.utils.to_categorical(y_test) model = models.Sequential() model.add( layers.Conv2D(32, (3, 3),
from __future__ import absolute_import, division, print_function import sys import tensorflow as tf from tensorflow.keras import datasets, layers, models from cnn import load_data IMG_SIZE_X = 437 IMG_SIZE_Y = 308 model = models.load_model("200.h5") x_train, y_train, x_test, y_test = load_data("mic0", IMG_SIZE_X, IMG_SIZE_Y) predictions = model.predict(x_train) print(predictions)