def evaluate(cvae, testLoader, exDir, e=1, classifier=None):  #e is the epoch

    cvae.eval()

    #Load test data
    xTest, yTest = prep_data(iter(testLoader).next(), cvae.useCUDA)

    print 'saving a set of samples'
    if cvae.useCUDA:
        z = Variable(torch.randn(xTest.size(0), opts.nz).cuda())
    else:
        z = Variable(torch.randn(xTest.size(0), opts.nz))

    ySmile = Variable(torch.Tensor(yTest.size()).fill_(1)).type_as(yTest)
    samples = cvae.decode(ySmile, z).cpu()
    save_image(samples.data, join(exDir, 'smile_epoch' + str(e) + '.png'))

    yNoSmile = Variable(torch.Tensor(yTest.size()).fill_(0)).type_as(yTest)
    samples = cvae.decode(yNoSmile, z).cpu()
    save_image(samples.data, join(exDir, 'no_smile_epoch' + str(e) + '.png'))

    #check reconstructions after each 10 epochs
    outputs, outMu, outLogVar, outY = cvae(xTest)

    bceLossTest, klLossTest = cvae.loss(rec_x=outputs,
                                        x=xTest,
                                        mu=outMu,
                                        logVar=outLogVar)
    predLabel = torch.floor(outY)

    classScoreTest = binary_class_score(predLabel, yTest, thresh=0.5)
    print 'classification test:', classScoreTest.data[0]

    save_image(xTest.data, join(exDir, 'input.png'))
    save_image(outputs.data, join(exDir, 'output_' + str(e) + '.png'))

    rec1, rec0 = label_switch(xTest.data, yTest, cvae, exDir=exDir)

    # for further eval
    if e == 'evalMode' and classer is not None:
        classer.eval()
        yPred0 = classer(rec0)
        y0 = Variable(torch.LongTensor(yPred0.size()).fill_(0)).type_as(yTest)
        class0 = binary_class_score(yPred0, y0, thresh=0.5)
        yPred1 = classer(rec1)
        y1 = Variable(torch.LongTensor(yPred1.size()).fill_(1)).type_as(yTest)
        class1 = binary_class_score(yPred1, y1, thresh=0.5)

        f = open(join(exDir, 'eval.txt'), 'w')
        f.write('Test MSE:' + str(F.mse_loss(outputs, xTest).data[0]))
        f.write('Class0:' + str(class0.data[0]))
        f.write('Class1:' + str(class1.data[0]))
        f.close()

    return (bceLossTest).data[0] / xTest.size(0), classScoreTest.data[0]
Ejemplo n.º 2
0
            #check reconstructions after each 10 epochs
            outputs, outMu, outLogVar, outY = cvae(Variable(xTest))

            bceLossTest, klLossTest = vae_loss_fn(rec_x=outputs.data,
                                                  x=xTest,
                                                  mu=outMu,
                                                  logVar=outLogVar)
            maxVal, predLabel = torch.max(outY, 1)
            classScoreTest = torch.eq(predLabel,
                                      yTest).float().sum() / yTest.size(0)
            print 'classification test:', classScoreTest.data[0]

            save_image(xTest, join(exDir, 'input.png'))
            save_image(outputs.data, join(exDir, 'output_' + str(e) + '.png'))

            label_switch(xTest, yTest, cvae, exDir=exDir)

            cvae.save_params(exDir=exDir)

            losses['total'].append(epochLoss / Ns)
            losses['kl'].append(epochLoss_kl / Ns)
            losses['bce'].append(epochLoss_bce / Ns)
            losses['test_bce'].append(
                (bceLossTest).data[0] / xTest.size(0))  #append every epoch
            losses['dis'].append(epochLoss_dis / Ns)
            losses['gen'].append(epochLoss_gen / Ns)
            losses['class'].append(epochLoss_class / Ns)
            losses['test_class'].append(classScoreTest.data[0])

            if e > 1:
                plot_losses(losses, exDir, epochs=e + 1)