def test(epoch, mode=1):
    import matplotlib.pyplot as plt
    from PIL import Image
    from networks.capsulenet.helper_function import combine_images

    if mode == 1:
        num_classes =10
        _,(x_test,y_test) = load_cifar_10()
    else:
        num_classes = 100
        _,(x_test,y_test) = load_cifar_100()
    
    model = CapsNetv2(input_shape=[32, 32, 3],
                        n_class=num_classes,
                        n_route=3)
    model.load_weights('weights/capsule_weights/capsule-cifar-'+str(num_classes)+'weights-{:02d}.h5'.format(epoch)) 
    print("Weights loaded, start validation")   
    # model.load_weights('weights/capsule-weights-{:02d}.h5'.format(epoch))    
    y_pred, x_recon = model.predict([x_test, y_test], batch_size=100)
    print('-'*50)
    # Test acc: 0.7307
    print('Test acc:', np.sum(np.argmax(y_pred, 1) == np.argmax(y_test, 1))/y_test.shape[0])

    img = combine_images(np.concatenate([x_test[:50],x_recon[:50]]))
    image = img*255
    Image.fromarray(image.astype(np.uint8)).save("results/real_and_recon.png")
    print('Reconstructed images are saved to ./results/real_and_recon.png')
    print('-'*50)
    plt.imshow(plt.imread("results/real_and_recon.png", ))
    plt.show()
예제 #2
0
def test(model, data):
    x_test, y_test = data
    y_pred, x_recon = model.predict([x_test, y_test], batch_size=100)
    print('-' * 50)
    print(
        'Test acc:',
        np.sum(np.argmax(y_pred, 1) == np.argmax(y_test, 1)) / y_test.shape[0])

    import matplotlib.pyplot as plt
    from networks.capsulenet.helper_function import combine_images
    from PIL import Image

    img = combine_images(np.concatenate([x_test[:50], x_recon[:50]]))
    image = img * 255
    Image.fromarray(image.astype(np.uint8)).save("real_and_recon.png")
    print()
    print('Reconstructed images are saved to ./real_and_recon.png')
    print('-' * 50)
    plt.imshow(plt.imread("real_and_recon.png", ))
    plt.show()