def test(nef, ngf, nc, batch_size, Z, ctx, pretrained_encoder_path, pretrained_generator_path, output_path, data_path, activation, save_embedding, embedding_path = ''):
    '''Test the VAE with a pretrained encoder and generator.
    Keep the batch size 1'''
    #encoder
    z_mu, z_lv, z = encoder(nef, Z, batch_size)
    symE = mx.sym.Group([z_mu, z_lv, z])

    #generator
    symG = generator(ngf, nc, no_bias=True, fix_gamma=True, eps=1e-5 + 1e-12, z_dim = Z, activation=activation )

    # ==============data==============
    X_test, image_names = get_data(data_path, activation)
    test_iter = mx.io.NDArrayIter(X_test, batch_size=batch_size, shuffle=False)

    # =============module E=============
    modE = mx.mod.Module(symbol=symE, data_names=('data',), label_names=None, context=ctx)
    modE.bind(data_shapes=test_iter.provide_data)
    modE.load_params(pretrained_encoder_path)

    # =============module G=============
    modG = mx.mod.Module(symbol=symG, data_names=('rand',), label_names=None, context=ctx)
    modG.bind(data_shapes=[('rand', (1, Z, 1, 1))])
    modG.load_params(pretrained_generator_path)

    print('Testing...')

    # =============test===============
    test_iter.reset()
    for t, batch in enumerate(test_iter):

        #update discriminator on decoded
        modE.forward(batch, is_train=False)
        mu, lv, z = modE.get_outputs()
        mu = mu.reshape((batch_size, Z, 1, 1))
        sample = mx.io.DataBatch([mu], label=None, provide_data = [('rand', (batch_size, Z, 1, 1))])
        modG.forward(sample, is_train=False)
        outG = modG.get_outputs()

        visual(output_path + '/' + 'gout'+str(t), outG[0].asnumpy(), activation)
        visual(output_path +  '/' + 'data'+str(t), batch.data[0].asnumpy(), activation)
        image_name = image_names[t].split('.')[0]

        if save_embedding:
            savemat(embedding_path+'/'+image_name+'.mat', {'embedding':mu.asnumpy()})
def test ( nef , ngf , nc , batch_size , Z , ctx , pretrained_encoder_path , pretrained_generator_path , output_path , data_path , activation , save_embedding , embedding_path = '' ) : z_mu , z_lv , z = encoder ( nef , Z , batch_size ) symE = mx . sym . Group ( [ z_mu , z_lv , z ] ) symG = generator ( ngf , nc , no_bias = True , fix_gamma = True , eps = 1e-5 + 1e-12 , z_dim = Z , activation = activation ) X_test , image_names = get_data ( data_path , activation ) test_iter = mx . io . NDArrayIter ( X_test , batch_size = batch_size , shuffle = False ) modE = mx . mod . Module ( symbol = symE , data_names = ( 'data' , ) , label_names = None , context = ctx ) modE . bind ( data_shapes = test_iter . provide_data ) modE . load_params ( pretrained_encoder_path ) modG = mx . mod . Module ( symbol = symG , data_names = ( 'rand' , ) , label_names = None , context = ctx ) modG . bind ( data_shapes = [ ( 'rand' , ( 1 , Z , 1 , 1 ) ) ] ) modG . load_params ( pretrained_generator_path ) print ( 'Testing...' ) test_iter . reset ( ) for t , batch in enumerate ( test_iter ) : modE . forward ( batch , is_train = False ) mu , lv , z = modE . get_outputs ( ) mu = mu . reshape ( ( batch_size , Z , 1 , 1 ) ) sample = mx . io . DataBatch ( [ mu ] , label = None , provide_data = [ ( 'rand' , ( batch_size , Z , 1 , 1 ) ) ] ) modG . forward ( sample , is_train = False ) outG = modG . get_outputs ( ) visual ( output_path + '/' + 'gout' + str ( t ) , outG [ 0 ] . asnumpy ( ) , activation ) visual ( output_path + '/' + 'data' + str ( t ) , batch . data [ 0 ] . asnumpy ( ) , activation ) image_name = image_names [ t ] . split ( '.' ) [ 0 ] if save_embedding : savemat ( embedding_path + '/' + image_name + '.mat' , { 'embedding' : mu . asnumpy ( ) } )
Test the VAE with a pretrained encoder and generator.
    Keep the batch size 1