Exemplo n.º 1
0
def recognizing(imageData, loadedFile='data.pkl', kernels=[8, 10]):
    """
        Load the data and build the CNN which have been trained.
        Then print the result.
    """
    dataBase, manLabel = loading(imageData)
    prm0, prm1, prm2, prm3 = load_save_data.loadData(loadedFile)
    m = T.matrix('m')
    inputData = m.reshape((dataBase.shape[0], 1, 57, 47))
    l0 = CNN.ConvPoolLayer(inputData=inputData,
                           filterShape=(kernels[0], 1, 5, 5),
                           imageShape=(dataBase.shape[0], 1, 57, 47),
                           poolsize=(2, 2),
                           weights=prm0[0],
                           biases=prm0[1])
    l1 = CNN.ConvPoolLayer(inputData=l0.output,
                           filterShape=(kernels[1], kernels[0], 5, 5),
                           imageShape=(dataBase.shape[0], kernels[0], 26, 21),
                           poolsize=(2, 2),
                           weights=prm1[0],
                           biases=prm1[1])
    l2 = CNN.HiddenLayer(inputData=l1.output.reshape(
        (dataBase.shape[0], kernels[1] * 11 * 8)),
                         numIn=kernels[1] * 11 * 8,
                         numOut=2000,
                         weights=prm2[0],
                         biases=prm2[1],
                         activation=T.tanh)
    l3 = CNN.LogisticRegression(inputData=l2.output,
                                numIn=2000,
                                numOut=40,
                                weights=prm3[0],
                                biases=prm3[1])

    func = theano.function([m], l3.yRslt)
    result = func(dataBase)
    """for ind in range(dataBase.shape[0]):
        if result[ind] != manLabel[ind]:
            print ('%i is mis-predicted as %i' % (manLabel[ind], result[ind]))
            img1 = dataBase[ind].reshape((57, 47))
            plt.subplot(121)
            plt.imshow(img1)
            pylab.gray()
            img2 = dataBase[result[ind] * 10].reshape((57, 47))
            plt.subplot(122)
            plt.imshow(img2)
            pylab.gray()
            pylab.show()

    """
    #first term is age and second term is gender
    if int(gender_matrix[result[0]]) == 0:
        gender = "male"
    else:
        gender = "female"
    print old_matrix[result[0]], ' ', gender
Exemplo n.º 2
0
    def train(self):
        # self.trainBuntton["state"] = 'disabled'
        #sizes, epochs, eta, update_queue, validation_threshold, error_threshold
        trainingPercent = int(self.trainingPercent.get())
        epochs = int(self.epochs.get())
        et = float(self.error_threshold.get())
        vt = int(self.validation_threshold.get())
        poolinRule = self.poolingRule.get()
        mini_batch_size = int(self.mini_batch_size.get())

        try:
            import pickle
            weights = pickle.load(open(self.address.get(), "rb"))
        except:
            weights = None

        if not self.plotter.is_alive():
            self.plotter.start()

        if self.cnn is not None:
            if self.cnn.exitcode != 0:
                print 'Another learner is working.'
                return
            self.cnn.terminate()

        # # self.cnn = CNN.Network(sizes, epochs, eta, mu, self.updatesQueue, vt, et, widrow, bs, activationFunc, weights)
        # self.cnn = CNN.Network([CNN.ConvPoolLayer(image_shape=(mini_batch_size, 1, 28, 28), filter_shape=(20, 1, 5, 5), poolsize=(2, 2)),
        #                         CNN.FullyConnectedLayer(n_in=20*12*12, n_out=100),
        #                         CNN.SoftmaxLayer(n_in=100, n_out=10)], mini_batch_size, self.updatesQueue)

        self.cnn = CNN.Network([
            CNN.ConvPoolLayer(image_shape=(mini_batch_size, 1, 28, 28),
                              filter_shape=(20, 1, 5, 5),
                              poolsize=(2, 2)),
            CNN.ConvPoolLayer(image_shape=(mini_batch_size, 20, 12, 12),
                              filter_shape=(30, 20, 5, 5),
                              poolsize=(2, 2)),
            CNN.ConvPoolLayer(image_shape=(mini_batch_size, 30, 4, 4),
                              filter_shape=(40, 30, 2, 2),
                              poolsize=(3, 3)),
            CNN.FullyConnectedLayer(n_in=40 * 1 * 1, n_out=100),
            CNN.SoftmaxLayer(n_in=100, n_out=10)
        ], mini_batch_size, self.updatesQueue)
        self.cnn.start()
Exemplo n.º 3
0
def DataTraining(learningRate=0.05,
                 epochs=200,
                 imageData='olivettifaces.gif',
                 kernels=[8, 10],
                 batchsize=40):
    """
        This is the training function, kind of like relaxation step.
        First, give a number(here I choose 23333) to generate random number and load the data.
        Then construct the convolution neural network(It's a hard part, by debugging and debugging the parameters)
        After constructing, we will build three models, and use SGD in training model.
        Then training.
    """
    rng = numpy.random.RandomState(23333)
    imageData = load_save_data.loadImageData(imageData)
    trainX, trainY = imageData[0]
    validX, validY = imageData[1]
    testX, testY = imageData[2]
    numTrainBatches = trainX.get_value(borrow=True).shape[0] / batchsize
    numValidBatches = validX.get_value(borrow=True).shape[0] / batchsize
    numTestBatches = testX.get_value(borrow=True).shape[0] / batchsize

    index = T.lscalar()
    x = T.matrix('x')
    y = T.ivector('y')

    inputData = x.reshape((batchsize, 1, 57, 47))
    l0 = CNN.ConvPoolLayer(inputData=inputData,
                           filterShape=(kernels[0], 1, 5, 5),
                           imageShape=(batchsize, 1, 57, 47),
                           poolsize=(2, 2),
                           rng=rng)
    # 26 = (57 - 5) / 2, 21 = (47 - 5) / 2
    l1 = CNN.ConvPoolLayer(inputData=l0.output,
                           filterShape=(kernels[1], kernels[0], 5, 5),
                           imageShape=(batchsize, kernels[0], 26, 21),
                           poolsize=(2, 2),
                           rng=rng)
    # 11 = (26 - 5 + 1) / 2, 8 = (21 - 5) / 2
    l2 = CNN.HiddenLayer(inputData=l1.output.reshape(
        (batchsize, kernels[1] * 11 * 8)),
                         numIn=kernels[1] * 11 * 8,
                         numOut=2000,
                         activation=T.tanh,
                         rng=rng)
    l3 = CNN.LogisticRegression(inputData=l2.output, numIn=2000, numOut=40)
    cost = l3.logLikelihood(y)
    """
        ================================================================================================================================
    """
    #Here I will give a brief introduction to how to build the model.
    #Using the given index, we can get the corresponding x and y(what given does).
    #Because l3.error() is applied, l2 - l1 - l0 were also applied. i.e. CNN is applied
    #In this way, model was built.
    testModel = theano.function(
        [index],
        l3.error(y),
        givens={
            x: testX[index * batchsize:(index + 1) * batchsize],
            y: testY[index * batchsize:(index + 1) * batchsize]
        })
    validationModel = theano.function(
        [index],
        l3.error(y),
        givens={
            x: validX[index * batchsize:(index + 1) * batchsize],
            y: validY[index * batchsize:(index + 1) * batchsize]
        })
    #SGD(stochastic gradient decent)
    parameters = l0.parameters + l1.parameters + l2.parameters + l3.parameters
    grads = T.grad(cost, parameters)
    updates = [(p, p - learningRate * g) for p, g in zip(parameters, grads)]

    trainingModel = theano.function(
        [index],
        cost,
        updates=updates,
        givens={
            x: trainX[index * batchsize:(index + 1) * batchsize],
            y: trainY[index * batchsize:(index + 1) * batchsize]
        })
    """
        =================================================================================================================================
    """
    print 'training'
    ptc = 800
    ptcInc = 2
    threshold = 0.99
    #To ensure that always validate on validation data
    validationFrequency = min(numTrainBatches, ptc / 2)

    bstValidationL = numpy.inf
    bstIter = 0
    start = time.clock()

    epoch = 0
    done = False
    #Now it's time to train, really tricky.
    #Kind of like SVM, a epoch will go through all data set. For each while loop, we need to train
    #each batch by using the for loop. If the current loss is less than the best validation loss
    #we will update them, and if the current loss is less than the threshold x best validation loss
    #we will update the patience.
    while (epoch < epochs) and (not done):
        epoch += 1
        for ind in xrange(numTrainBatches):
            iter = (epoch - 1) * numTrainBatches + ind
            c = trainingModel(ind)
            if (iter + 1) % validationFrequency == 0:
                validationL = [
                    validationModel(i) for i in xrange(numValidBatches)
                ]
                currentL = numpy.mean(validationL)
                #print('epoch %i, validation error %f %%' % (epoch, currentL * 100.0))
                if currentL < bstValidationL:
                    if currentL < bstValidationL * threshold:
                        ptc = max(ptc, iter * ptcInc)
                    bstValidationL = currentL
                    bstIter = iter
                    load_save_data.saveData(l0.parameters, l1.parameters,
                                            l2.parameters, l3.parameters)
                    testL = [testModel(i) for i in xrange(numTestBatches)]
                    tstscore = numpy.mean(testL)
                    print(('epoch %i, model error: %f %%') %
                          (epoch, tstscore * 100.0))
            if ptc <= iter:
                done = True
                break
    end = time.clock()
    print('Optimization: ')
    print('validation score: %f %% iter: %i' %
          (bstValidationL * 100.0, bstIter + 1))
    print('time: %.2f ' % (end - start))
Exemplo n.º 4
0
def cnn_mnist(dataset):
    learning_rate = 0.1
    n_epochs = 200
    batch_size = 500

    #nkerns : number of kernels on each layer
    nkerns = [20, 10]

    train_set_x, train_set_y = dataset[0]
    valid_set_x, valid_set_y = dataset[1]
    test_set_x, test_set_y = dataset[2]

    n_train_batches = train_set_x.get_value(borrow=True).shape[0] // batch_size
    n_valid_batches = valid_set_x.get_value(borrow=True).shape[0] // batch_size
    n_test_batches = test_set_x.get_value(borrow=True).shape[0] // batch_size

    index = T.lscalar()
    x = T.matrix('x')
    y = T.ivector('y')

    print('Building the model...')

    layer0_input = x.reshape((batch_size, 1, 28, 28))

    layer0 = CNN.ConvPoolLayer(rng,
                               input=layer0_input,
                               image_shape=(batch_size, 1, 28, 28),
                               filter_shape=(nkerns[0], 1, 5, 5),
                               poolsize=(2, 2))

    layer1 = CNN.ConvPoolLayer(rng,
                               input=layer0.output,
                               image_shape=(batch_size, nkerns[0], 12, 12),
                               filter_shape=(nkerns[1], nkerns[0], 5, 5),
                               poolsize=(2, 2))

    layer2_input = layer1.output.flatten(2)

    layer2 = MultiLayerPerceptron.HiddenLayer(rng,
                                              input=layer2_input,
                                              n_in=nkerns[1] * 4 * 4,
                                              n_out=80,
                                              activation=T.tanh)

    layer3 = LogisticRegression.LogisticRegression(input=layer2.output,
                                                   n_in=80,
                                                   n_out=10)

    cost = layer3.NLL(y)

    test_model = theano.function(
        [index],
        layer3.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]
        })
    print('1. Test built.')
    validate_model = theano.function(
        [index],
        layer3.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]
        })
    print('2. Valid built.')
    params = layer3.params + layer2.params + layer1.params + layer0.params

    grads = T.grad(cost, params)

    updates = [(param_i, param_i - learning_rate * grad_i)
               for param_i, grad_i in zip(params, grads)]
    print('3. Derivative calculated.')
    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]
        })
    print('4. Train built.')
    print('Train model...')
    patience = 10000
    patience_increase = 2
    improvement_threshold = 0.995
    validation_frequency = min(n_train_batches, patience // 2)

    best_validation_loss = numpy.inf
    best_iter = 0
    test_score = 0
    start_time = timeit.default_timer()

    epoch = 0
    done_looping = False

    while (epoch < n_epochs) and (not done_looping):
        epoch = epoch + 1

        for minibatch_index in range(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:
                validation_losses = [
                    validate_model(i) for i in range(n_valid_batches)
                ]
                this_validation_loss = numpy.mean(validation_losses)
                print('epoch %i, minibatch %i/%i, validation error %.2f %%' %
                      (epoch, minibatch_index + 1, n_train_batches,
                       this_validation_loss * 100))

                if this_validation_loss < best_validation_loss:
                    if this_validation_loss < best_validation_loss * improvement_threshold:
                        patience = max(patience, iter * patience_increase)

                    best_validation_loss = this_validation_loss
                    best_iter = iter

                    test_losses = [
                        test_model(i) for i in range(n_test_batches)
                    ]
                    test_score = numpy.mean(test_losses)

                    print(
                        '\tepoch %i, minibatch %i/%i, test error of best model %.2f %%'
                        % (epoch, minibatch_index + 1, n_train_batches,
                           test_score * 100))

            if patience <= iter:
                done_looping = True
                break

    end_time = timeit.default_timer()
    print('Optimization complete')
    print(
        'Best validation score of %f %% obtained at iteration %i, with test performance %.2f %%'
        % (best_validation_loss * 100, best_iter + 1, test_score * 100))