#step2 获取无标签数据集, 有标签训练数据集, 有标签测试数据集
    unlabeledData, trainData, trainLabels, testData, testLabels = GetDivideSets(
    )

    #step3 用无标签数据集训练自编码的特征
    theta = sparseAutoencoder.initiallize(hiddenSize, inputSize)
    [X,cost,d]=sop.fmin_l_bfgs_b(lambda (x) :sparseAutoencoder.sparseAutoencoderCost(x, inputSize, \
    hiddenSize, la, sparsityParam, beta, unlabeledData),x0=theta,maxiter=400,disp=1)
    W1 = X[0:hiddenSize * inputSize].reshape(hiddenSize, inputSize)
    W1 = W1.T
    opttheta = X

    #step4 获得有标签训练集的激活值并用来训练softmax分类器的权值
    trainImages = feedforward(opttheta, hiddenSize, inputSize, trainData)
    thetaSoftmax = softmax.initiallize(numClasses, hiddenSize)
    la = 1e-4
    [X, cost, d] = sop.fmin_l_bfgs_b(lambda (x): softmax.SoftmaxCost(
        trainImages, trainLabels, x, la, hiddenSize, numClasses),
                                     x0=thetaSoftmax,
                                     maxiter=100,
                                     disp=1)

    #step5 获得有标签测试集的激活值并且给出softmax分类准确率, 5分类的准确率应该在98%附近
    testImages = feedforward(opttheta, hiddenSize, inputSize, testData)
    optthetaSoftmax = X
    accuracy = softmax.predict(optthetaSoftmax, testImages, testLabels,
                               hiddenSize, numClasses)
    print accuracy

    #画出权值图像
    hiddenSize = 200
    sparsityParam = 0.1
    la = 3e-3
    beta = 3

    #step2 获取无标签数据集, 有标签训练数据集, 有标签测试数据集
    unlabeledData, trainData, trainLabels, testData, testLabels = GetDivideSets()

    #step3 用无标签数据集训练自编码的特征
    theta = sparseAutoencoder.initiallize(hiddenSize, inputSize)
    [X,cost,d]=sop.fmin_l_bfgs_b(lambda (x) :sparseAutoencoder.sparseAutoencoderCost(x, inputSize, \
    hiddenSize, la, sparsityParam, beta, unlabeledData),x0=theta,maxiter=400,disp=1)
    W1 = X[0:hiddenSize*inputSize].reshape(hiddenSize, inputSize)
    W1 = W1.T
    opttheta = X

    #step4 获得有标签训练集的激活值并用来训练softmax分类器的权值
    trainImages = feedforward(opttheta, hiddenSize, inputSize, trainData)
    thetaSoftmax = softmax.initiallize(numClasses, hiddenSize)
    la = 1e-4
    [X,cost,d] = sop.fmin_l_bfgs_b(lambda (x) :softmax.SoftmaxCost(trainImages, trainLabels,x,la,hiddenSize,numClasses),x0=thetaSoftmax,maxiter=100,disp=1)

    #step5 获得有标签测试集的激活值并且给出softmax分类准确率, 5分类的准确率应该在98%附近
    testImages = feedforward(opttheta, hiddenSize, inputSize,          testData)
    optthetaSoftmax = X
    accuracy = softmax.predict(optthetaSoftmax,testImages,testLabels,hiddenSize,numClasses)
    print accuracy

    #画出权值图像
    sparseAutoencoder.showimg(W1, hiddenSize, 28)
Esempio n. 3
0
        pooledFeaturesThis = cnnPool(poolDim, convolvedFeaturesThis)
        pooledFeaturesTrain[featureStart:featureEnd, :, :, :] = pooledFeaturesThis
        convolvedFeaturesThis = cnnConvolve(patchDim, stepSize, testImages, Wt, bt, white, meanPatch)
        pooledFeaturesThis = cnnPool(poolDim, convolvedFeaturesThis)
        pooledFeaturesTest[featureStart:featureEnd, :, :, :] = pooledFeaturesThis

    cnnPooledFeatures = {}
    cnnPooledFeatures['pooledFeaturesTrain'] = pooledFeaturesTrain
    cnnPooledFeatures['pooledFeaturesTest'] = pooledFeaturesTest
    sio.savemat('cnnPooledFeatures.mat', cnnPooledFeatures)

    #step7 用卷积特征来进行softmax预测,这个就很快了,十几秒, 正确率为80%左右
    #Mat1 = sio.loadmat('cnnPooledFeatures.mat')
    #pooledFeaturesTrain = Mat1['pooledFeaturesTrain']
    #pooledFeaturesTest = Mat1['pooledFeaturesTest']
    softmaxLambda = 1e-4
    numClasses = 4
    inputSize = int(pooledFeaturesTrain.size/numTrainImages)
    print inputSize
    softmaxX = np.transpose(pooledFeaturesTrain, (0, 2, 3, 1))
    softmaxX = softmaxX.reshape(inputSize, numTrainImages)
    softmaxY = trainLabels
    theta = softmax.initiallize(numClasses, inputSize)
    [X,cost,d] = sop.fmin_l_bfgs_b(lambda (x) :softmax.SoftmaxCost(softmaxX, softmaxY, x, softmaxLambda, inputSize, numClasses),x0=theta,maxiter=200,disp=1)

    softmaxX = np.transpose(pooledFeaturesTest, (0, 2, 3, 1))
    softmaxX = softmaxX.reshape(inputSize, numTestImages)
    softmaxY = testLabels
    accuracy = softmax.predict(X,softmaxX,softmaxY,inputSize,numClasses)
    print accuracy
Esempio n. 4
0
    #step3 训练第一层神经网络的权值
    [sae1OptTheta, cost, d]=sop.fmin_l_bfgs_b(lambda (x) :sparseAutoencoder.sparseAutoencoderCost(x, inputSize, \
    hiddenSizeL1, la, sparsityParam, beta, trainData),x0=sae1Theta,maxiter=400,disp=1)
    sae1Features = feedforward(sae1OptTheta, hiddenSizeL1, inputSize,
                               trainData)

    #step4 训练第二层神经网络的权值
    sae2Theta = sparseAutoencoder.initiallize(hiddenSizeL2, hiddenSizeL1)
    [sae2OptTheta, cost, d]=sop.fmin_l_bfgs_b(lambda (x) : sparseAutoencoder.sparseAutoencoderCost(x, hiddenSizeL1, \
    hiddenSizeL2, la, sparsityParam, beta, sae1Features),x0=sae2Theta, maxiter=400,disp=1)
    sae2Features = feedforward(sae2OptTheta, hiddenSizeL2, hiddenSizeL1,
                               sae1Features)

    #step5 训练softmax权值
    saeSoftmaxTheta = softmax.initiallize(numClasses, hiddenSizeL2)
    laSoftmax = 1e-4
    [saeSoftmaxOptTheta, cost, d] = sop.fmin_l_bfgs_b(lambda (x) :softmax.SoftmaxCost(sae2Features, \
    trainLabels, x, laSoftmax, hiddenSizeL2, numClasses), x0=saeSoftmaxTheta, maxiter=100, disp=1)

    #step6 微调栈式神经网络的所有权值
    stackedAETheta, netconfig = initaillizeAETheta(sae1OptTheta, sae2OptTheta,
                                                   saeSoftmaxOptTheta,
                                                   hiddenSizeL1, hiddenSizeL2,
                                                   inputSize)
    [stackedAEOptTheta, cost, d] = sop.fmin_l_bfgs_b(lambda (x):stackedAECost(x, inputSize, hiddenSizeL2, \
    numClasses, netconfig, la, trainData, trainLabels), x0 = stackedAETheta, maxiter = 400, disp=1)

    #step7 给出栈式神经网络的10分类的准确率, 微调前的应该在90%左右, 微调后在97%左右
    accuracyBeforeFine = predict(stackedAETheta, inputSize, hiddenSizeL2,
                                 numClasses, netconfig, testData, testLabels)
    trainLabels = trainLabels[0:length]
    sae1Theta = sparseAutoencoder.initiallize(hiddenSizeL1, inputSize)

    #step3 训练第一层神经网络的权值
    [sae1OptTheta, cost, d]=sop.fmin_l_bfgs_b(lambda (x) :sparseAutoencoder.sparseAutoencoderCost(x, inputSize, \
    hiddenSizeL1, la, sparsityParam, beta, trainData),x0=sae1Theta,maxiter=400,disp=1)
    sae1Features = feedforward(sae1OptTheta, hiddenSizeL1, inputSize, trainData)

    #step4 训练第二层神经网络的权值
    sae2Theta = sparseAutoencoder.initiallize(hiddenSizeL2, hiddenSizeL1)
    [sae2OptTheta, cost, d]=sop.fmin_l_bfgs_b(lambda (x) : sparseAutoencoder.sparseAutoencoderCost(x, hiddenSizeL1, \
    hiddenSizeL2, la, sparsityParam, beta, sae1Features),x0=sae2Theta, maxiter=400,disp=1)
    sae2Features = feedforward(sae2OptTheta, hiddenSizeL2, hiddenSizeL1, sae1Features)

    #step5 训练softmax权值
    saeSoftmaxTheta = softmax.initiallize(numClasses, hiddenSizeL2)
    laSoftmax = 1e-4
    [saeSoftmaxOptTheta, cost, d] = sop.fmin_l_bfgs_b(lambda (x) :softmax.SoftmaxCost(sae2Features, \
    trainLabels, x, laSoftmax, hiddenSizeL2, numClasses), x0=saeSoftmaxTheta, maxiter=100, disp=1)

    #step6 微调栈式神经网络的所有权值
    stackedAETheta, netconfig = initaillizeAETheta(sae1OptTheta, sae2OptTheta, saeSoftmaxOptTheta, hiddenSizeL1, hiddenSizeL2, inputSize)
    [stackedAEOptTheta, cost, d] = sop.fmin_l_bfgs_b(lambda (x):stackedAECost(x, inputSize, hiddenSizeL2, \
    numClasses, netconfig, la, trainData, trainLabels), x0 = stackedAETheta, maxiter = 400, disp=1)

    #step7 给出栈式神经网络的10分类的准确率, 微调前的应该在90%左右, 微调后在97%左右
    accuracyBeforeFine = predict(stackedAETheta, inputSize, hiddenSizeL2, numClasses, netconfig, testData, testLabels)
    accuracyAfterFine = predict(stackedAEOptTheta, inputSize, hiddenSizeL2, numClasses, netconfig, testData, testLabels)
    print 'accuracyBeforeFine: '+str(accuracyBeforeFine)
    print 'accuracyAfterFine: '+str(accuracyAfterFine)
Esempio n. 6
0
            featureStart:featureEnd, :, :, :] = pooledFeaturesThis

    cnnPooledFeatures = {}
    cnnPooledFeatures['pooledFeaturesTrain'] = pooledFeaturesTrain
    cnnPooledFeatures['pooledFeaturesTest'] = pooledFeaturesTest
    sio.savemat('cnnPooledFeatures.mat', cnnPooledFeatures)

    #step7 用卷积特征来进行softmax预测,这个就很快了,十几秒, 正确率为80%左右
    #Mat1 = sio.loadmat('cnnPooledFeatures.mat')
    #pooledFeaturesTrain = Mat1['pooledFeaturesTrain']
    #pooledFeaturesTest = Mat1['pooledFeaturesTest']
    softmaxLambda = 1e-4
    numClasses = 4
    inputSize = int(pooledFeaturesTrain.size / numTrainImages)
    print inputSize
    softmaxX = np.transpose(pooledFeaturesTrain, (0, 2, 3, 1))
    softmaxX = softmaxX.reshape(inputSize, numTrainImages)
    softmaxY = trainLabels
    theta = softmax.initiallize(numClasses, inputSize)
    [X, cost, d] = sop.fmin_l_bfgs_b(lambda (x): softmax.SoftmaxCost(
        softmaxX, softmaxY, x, softmaxLambda, inputSize, numClasses),
                                     x0=theta,
                                     maxiter=200,
                                     disp=1)

    softmaxX = np.transpose(pooledFeaturesTest, (0, 2, 3, 1))
    softmaxX = softmaxX.reshape(inputSize, numTestImages)
    softmaxY = testLabels
    accuracy = softmax.predict(X, softmaxX, softmaxY, inputSize, numClasses)
    print accuracy