Esempio n. 1
0
def gen_image_set(name, ymb, zmb, indexes):
  folder = '%s/%s_frames' % (sample_dir, name)
  if not os.path.exists(folder):
    os.makedirs(folder)
  samples = np.asarray(_gen(zmb, ymb))
  for i in range(len(indexes)):
    path = '%s/%04d.png' % (folder, i)
    grayscale_grid_vis(inverse_transform(samples[indexes[i]]), (1, 1), path)
        g_epoch_costs = [(c / g_batch_count) for c in g_epoch_costs]
        str1 = "Epoch {}, block {}:".format(epoch, block_num)
        g_bc_strs = ["{0:s}: {1:.2f},".format(c_name, g_epoch_costs[c_idx]) \
                     for (c_idx, c_name) in zip(g_bc_idx, g_bc_names)]
        str2 = "    {}".format(" ".join(g_bc_strs))
        joint_str = "\n".join([str1, str2])
        print(joint_str)
        out_file.write(joint_str+"\n")
        out_file.flush()
        epoch_vae_cost += g_epoch_costs[1]
        epoch_iwae_cost += g_epoch_costs[4]
        ######################
        # DRAW SOME PICTURES #
        ######################
        sample_z0mb = np.repeat(rand_gen(size=(20, nz0)), 20, axis=0)
        samples = np.asarray(sample_func(sample_z0mb))
        grayscale_grid_vis(draw_transform(samples), (20, 20), "{}/eval_gen_e{}_b{}.png".format(result_dir, epoch, block_num))
    epoch_vae_cost = epoch_vae_cost / len(Xva_blocks)
    epoch_iwae_cost = epoch_iwae_cost / len(Xva_blocks)
    print("EPOCH {0:d} -- vae: {1:.2f}, iwae: {2:.2f}".format(epoch, epoch_vae_cost, epoch_iwae_cost))







##############
# EYE BUFFER #
##############
         kld_qtiles[0], kld_qtiles[1], kld_qtiles[2], kld_qtiles[3], np.max(vae_klds))
 kld_strs = ["{0:s}: {1:.2f},".format(ln, lk) for ln, lk in zip(vae_layer_names, epoch_layer_klds)]
 str7 = "    module kld -- {}".format(" ".join(kld_strs))
 str8 = "    validation -- nll: {0:.2f}, kld: {1:.2f}, vfe/iwae: {2:.2f}".format( \
         v_epoch_costs[3], v_epoch_costs[4], v_epoch_costs[2])
 joint_str = "\n".join([str1, str2, str2i, str3, str4, str5, str6, str7, str8])
 print(joint_str)
 out_file.write(joint_str+"\n")
 out_file.flush()
 #################################
 # QUALITATIVE DIAGNOSTICS STUFF #
 #################################
 if (epoch < 20) or (((epoch - 1) % 20) == 0):
     # generate some samples from the model prior
     samples = np.asarray(sample_func(sample_z0mb))
     grayscale_grid_vis(draw_transform(samples), (10, 20), "{}/gen_{}.png".format(result_dir, epoch))
     # test reconstruction performance (inference + generation)
     tr_rb = Xtr[0:100,:]
     va_rb = Xva[0:100,:]
     # get the model reconstructions
     tr_rb = train_transform(tr_rb)
     va_rb = train_transform(va_rb)
     tr_recons = recon_func(tr_rb)
     va_recons = recon_func(va_rb)
     # stripe data for nice display (each reconstruction next to its target)
     tr_vis_batch = np.zeros((200, nc, npx, npx))
     va_vis_batch = np.zeros((200, nc, npx, npx))
     for rec_pair in range(100):
         idx_in = 2*rec_pair
         idx_out = 2*rec_pair + 1
         tr_vis_batch[idx_in,:,:,:] = tr_rb[rec_pair,:,:,:]
Esempio n. 4
0
        samples = floatX(_gen(zmb).reshape(-1, nx))

        grad, svgd_grad = _svgd_gradient(samples)
        _train_g(zmb, floatX(svgd_grad.reshape(-1, nc, npx, npx)))  # generator

        _train_d(imb, floatX(samples))  # discriminator

        n_updates += 1

    if iter % 50 == 0:
        joblib.dump([p.get_value() for p in gen_params],
                    'models/%s/%d_gen_params.jl' % (desc, iter))
        joblib.dump([p.get_value() for p in rbm_params],
                    'models/%s/%d_rbm_params.jl' % (desc, iter))

    samples = np.asarray(_gen(sample_zmb))
    grayscale_grid_vis(inverse_transform(samples), (10, 20),
                       '%s/%d.png' % (samples_dir, iter))

# adversarial
logz_approx = ais_logZ(gB.get_value(), gb.get_value(), gc.get_value())
ll_train = _logp_rbm(floatX(trX)) - logz_approx
ll_test = _logp_rbm(floatX(teX)) - logz_approx
print iter, 'train', np.mean(
    ll_train), 'test', ll_test.mean(), 'logz', logz_approx

#np.savez('adv_ll_train.npz', ll=ll_train)
#np.savez('adv_ll_test.npz', ll=ll_test)

print 'DONE!'
    str4 = "    [q50, q80, q90, q95, max](vae-nll): {0:.2f}, {1:.2f}, {2:.2f}, {3:.2f}, {4:.2f}".format(
        nll_qtiles[0], nll_qtiles[1], nll_qtiles[2], nll_qtiles[3], np.max(vae_nlls))
    kld_qtiles = np.percentile(vae_klds, [50., 80., 90., 95.])
    str5 = "    [q50, q80, q90, q95, max](vae-kld): {0:.2f}, {1:.2f}, {2:.2f}, {3:.2f}, {4:.2f}".format(
        kld_qtiles[0], kld_qtiles[1], kld_qtiles[2], kld_qtiles[3], np.max(vae_klds))
    kld_strs = ["{0:s}: {1:.2f},".format(ln, lk) for ln, lk in zip(vae_layer_names, epoch_layer_klds)]
    str6 = "    module kld -- {}".format(" ".join(kld_strs))
    str7 = "    validation -- nll: {0:.2f}, kld: {1:.2f}, vfe/iwae: {2:.2f}".format(
        v_epoch_costs[3], v_epoch_costs[4], v_epoch_costs[2])
    joint_str = "\n".join([str1, str2, str3, str4, str5, str6, str7])
    print(joint_str)
    out_file.write(joint_str + "\n")
    out_file.flush()
    if (epoch <= 10) or ((epoch % 10) == 0):
        recon_count = 25
        recon_input = make_model_input(Xva[:recon_count, :])
        seq_cond_gen_model.set_sample_switch('gen')
        recons = recon_func(*recon_input)
        seq_cond_gen_model.set_sample_switch('inf')
        recons = draw_transform(np.vstack(recons))
        grayscale_grid_vis(recons, (recon_steps + 1, recon_count),
                           "{}/recons_{}.png".format(result_dir, epoch))





##############
# EYE BUFFER #
##############
Esempio n. 6
0
def gen_image(name, ymb, zmb, cols, indexes):
  path = '%s/%s.png' % (sample_dir, name)
  samples = np.asarray(_gen(zmb, ymb))
  grayscale_grid_vis(inverse_transform(samples[indexes]), (samples[indexes].shape[0]/cols, cols), path)
  return samples[indexes]
Esempio n. 7
0
File: job.py Progetto: mehdidc/dcgan
def run(hp, folder):
    trX, trY, nb_classes = load_data()
    k = 1             # # of discrim updates for each gen update
    l2 = 2.5e-5       # l2 weight decay
    b1 = 0.5          # momentum term of adam
    nc = 1            # # of channels in image
    ny = nb_classes   # # of classes
    nbatch = 128      # # of examples in batch
    npx = 28          # # of pixels width/height of images
    nz = 100          # # of dim for Z
    ngfc = 512       # # of gen units for fully connected layers
    ndfc = 512      # # of discrim units for fully connected layers
    ngf = 64          # # of gen filters in first conv layer
    ndf = 64          # # of discrim filters in first conv layer
    nx = npx*npx*nc   # # of dimensions in X
    niter = 200       # # of iter at starting learning rate
    niter_decay = 100 # # of iter to linearly decay learning rate to zero
    lr = 0.0002       # initial learning rate for adam
    scale = 0.02

    k = hp['k']
    l2 = hp['l2']
    #b1 = hp['b1']
    nc = 1
    ny = nb_classes
    nbatch = hp['nbatch']
    npx = 28
    nz = hp['nz']
    ngfc = hp['ngfc']       # # of gen units for fully connected layers
    ndfc = hp['ndfc']      # # of discrim units for fully connected layers
    ngf = hp['ngf']          # # of gen filters in first conv layer
    ndf = hp['ndf']          # # of discrim filters in first conv layer
    nx = npx*npx*nc   # # of dimensions in X
    niter = hp['niter']       # # of iter at starting learning rate
    niter_decay = hp['niter_decay'] # # of iter to linearly decay learning rate to zero
    lr = hp['lr']       # initial learning rate for adam


    scale = hp['scale']

    #k = 1             # # of discrim updates for each gen update
    #l2 = 2.5e-5       # l2 weight decay
    b1 = 0.5          # momentum term of adam
    #nc = 1            # # of channels in image
    #ny = nb_classes   # # of classes
    budget_hours = hp.get('budget_hours', 2)
    budget_secs = budget_hours * 3600

    ntrain = len(trX)
    def transform(X):
        return (floatX(X)).reshape(-1, nc, npx, npx)

    def inverse_transform(X):
        X = X.reshape(-1, npx, npx)
        return X
    
    model_dir = folder
    samples_dir = os.path.join(model_dir, 'samples')
    if not os.path.exists(model_dir):
        os.makedirs(model_dir)
    if not os.path.exists(samples_dir):
        os.makedirs(samples_dir)

    relu = activations.Rectify()
    sigmoid = activations.Sigmoid()
    lrelu = activations.LeakyRectify()
    bce = T.nnet.binary_crossentropy

    gifn = inits.Normal(scale=scale)
    difn = inits.Normal(scale=scale)

    gw  = gifn((nz, ngfc), 'gw')
    gw2 = gifn((ngfc, ngf*2*7*7), 'gw2')
    gw3 = gifn((ngf*2, ngf, 5, 5), 'gw3')
    gwx = gifn((ngf, nc, 5, 5), 'gwx')

    dw  = difn((ndf, nc, 5, 5), 'dw')
    dw2 = difn((ndf*2, ndf, 5, 5), 'dw2')
    dw3 = difn((ndf*2*7*7, ndfc), 'dw3')
    dwy = difn((ndfc, 1), 'dwy')

    gen_params = [gw, gw2, gw3, gwx]
    discrim_params = [dw, dw2, dw3, dwy]

    def gen(Z, w, w2, w3, wx, use_batchnorm=True):
        if use_batchnorm:
            batchnorm_ = batchnorm
        else:
            batchnorm_ = lambda x:x
        h = relu(batchnorm_(T.dot(Z, w)))
        h2 = relu(batchnorm_(T.dot(h, w2)))
        h2 = h2.reshape((h2.shape[0], ngf*2, 7, 7))
        h3 = relu(batchnorm_(deconv(h2, w3, subsample=(2, 2), border_mode=(2, 2))))
        x = sigmoid(deconv(h3, wx, subsample=(2, 2), border_mode=(2, 2)))
        return x

    def discrim(X, w, w2, w3, wy):
        h = lrelu(dnn_conv(X, w, subsample=(2, 2), border_mode=(2, 2)))
        h2 = lrelu(batchnorm(dnn_conv(h, w2, subsample=(2, 2), border_mode=(2, 2))))
        h2 = T.flatten(h2, 2)
        h3 = lrelu(batchnorm(T.dot(h2, w3)))
        y = sigmoid(T.dot(h3, wy))
        return y

    X = T.tensor4()
    Z = T.matrix()

    gX = gen(Z, *gen_params)

    p_real = discrim(X, *discrim_params)
    p_gen = discrim(gX, *discrim_params)

    d_cost_real = bce(p_real, T.ones(p_real.shape)).mean()
    d_cost_gen = bce(p_gen, T.zeros(p_gen.shape)).mean()
    g_cost_d = bce(p_gen, T.ones(p_gen.shape)).mean()

    d_cost = d_cost_real + d_cost_gen
    g_cost = g_cost_d

    cost = [g_cost, d_cost, g_cost_d, d_cost_real, d_cost_gen]

    lrt = sharedX(lr)
    d_updater = updates.Adam(lr=lrt, b1=b1, regularizer=updates.Regularizer(l2=l2))
    g_updater = updates.Adam(lr=lrt, b1=b1, regularizer=updates.Regularizer(l2=l2))
    d_updates = d_updater(discrim_params, d_cost)
    g_updates = g_updater(gen_params, g_cost)
    #updates = d_updates + g_updates

    print 'COMPILING'
    t = time()
    _train_g = theano.function([X, Z], cost, updates=g_updates)
    _train_d = theano.function([X, Z], cost, updates=d_updates)
    _gen = theano.function([Z], gX)
    print '%.2f seconds to compile theano functions'%(time()-t)

    tr_idxs = np.arange(len(trX))
    sample_zmb = floatX(np_rng.uniform(-1., 1., size=(200, nz)))

    def gen_samples(n, nbatch=128):
        samples = []
        labels = []
        n_gen = 0
        for i in range(n/nbatch):
            zmb = floatX(np_rng.uniform(-1., 1., size=(nbatch, nz)))
            xmb = _gen(zmb)
            samples.append(xmb)
            n_gen += len(xmb)
        n_left = n-n_gen
        zmb = floatX(np_rng.uniform(-1., 1., size=(n_left, nz)))
        xmb = _gen(zmb)
        samples.append(xmb)
        return np.concatenate(samples, axis=0)

    s = floatX(np_rng.uniform(-1., 1., size=(10000, nz)))
    n_updates = 0
    n_check = 0
    n_epochs = 0
    n_updates = 0
    n_examples = 0
    t = time()
    begin = datetime.now()
    for epoch in range(1, niter+niter_decay+1): 
        t = time()
        print("Epoch {}".format(epoch))
        trX = shuffle(trX)
        for imb in tqdm(iter_data(trX, size=nbatch), total=ntrain/nbatch):
            imb = transform(imb)
            zmb = floatX(np_rng.uniform(-1., 1., size=(len(imb), nz)))
            if n_updates % (k+1) == 0:
                cost = _train_g(imb, zmb)
            else:
                cost = _train_d(imb, zmb)
            n_updates += 1
            n_examples += len(imb)
        samples = np.asarray(_gen(sample_zmb))
        grayscale_grid_vis(inverse_transform(samples), (10, 20), '{}/{:05d}.png'.format(samples_dir, n_epochs))
        n_epochs += 1
        if n_epochs > niter:
            lrt.set_value(floatX(lrt.get_value() - lr/niter_decay))
        if n_epochs % 50 == 0 or epoch == niter + niter_decay or epoch == 1:
            imgs = []
            for i in range(0, s.shape[0], nbatch):
                imgs.append(_gen(s[i:i+nbatch]))
            img = np.concatenate(imgs, axis=0)
            samples_filename = '{}/{:05d}_gen.npz'.format(model_dir, n_epochs)
            joblib.dump(img, samples_filename, compress=9)
            shutil.copy(samples_filename, '{}/gen.npz'.format(model_dir))
            joblib.dump([p.get_value() for p in gen_params], '{}/d_gen_params.jl'.format(model_dir, n_epochs), compress=9)
            joblib.dump([p.get_value() for p in discrim_params], '{}/discrim_params.jl'.format(model_dir, n_epochs), compress=9)
        print('Elapsed : {}sec'.format(time() - t))

        if (datetime.now() - begin).total_seconds() >= budget_secs:
            print("Budget finished.quit.")
            break
Esempio n. 8
0
            elif (j < fd):
                # guide with samples from examples' posteriors
                lvar_samps.append(mix_post_samples[j])
            else:
                # sample remaining "free" latent variables from prior
                z_shape = [d for d in z_shapes[j]]
                samp_shape = [mix_comps * comp_reps] + z_shape
                z_samps = rand_gen(size=tuple(samp_shape))
                lvar_samps.append(z_samps)
        assert (lvar_samps[-1].shape[0] == (mix_comps * comp_reps))
        # sample using the generated latent variables
        samples = sample_func_scaled(lvar_samps, 1.0, no_scale=[0])
        print('fd={}, samples.shape: {}'.format(fd, samples.shape))
        fix_depth_samples.append(samples)
    # stack the samples from each "fix depth"
    samples = draw_transform(np.vstack(fix_depth_samples))
    print('samples.shape: {}'.format(samples.shape))
    grayscale_grid_vis(samples, (len(fix_depth_samples), mix_comps * comp_reps),
                       "{}/fig_mix_3samples_{}.png".format(result_dir, zzz))








##############
# EYE BUFFER #
##############
Esempio n. 9
0
zmb = floatX(np_rng.normal(0, 1, size=(100, nz)))
xmb = floatX(shuffle(X_test)[:100])
number_z = 5

for epoch in range(1, niter+niter_decay+1):
    X_train = shuffle(X_train)

    logpxz = 0
    for imb in tqdm(iter_data(X_train, size=nbatch), total=ntrain/nbatch):
        imb = floatX(imb)

        logpxz += _train(imb, number_z) * len(imb)

        n_updates+=1

    print epoch, 'logpxz', logpxz / ntrain


    if epoch == 1 or epoch % 5 == 0:
        samples = floatX(_decoder(zmb))
        grayscale_grid_vis(inverse_transform(samples), (10, 10), 'images/%s/samples_%d.png'%(desc, epoch))
        rec_x = _reconstruct(xmb)
        grayscale_grid_vis(inverse_transform(rec_x), (10, 10), 'images/%s/rec_x%d.png'%(desc, epoch))


    if epoch == 1 or epoch % 100 == 0:
        joblib.dump([p.get_value() for p in enc_params], 'models/%s/%d_en_params.jl'%(desc, epoch))
        joblib.dump([p.get_value() for p in dec_params], 'models/%s/%d_de_params.jl'%(desc, epoch))


Esempio n. 10
0
g_updater = updates.Adam(lr=lrt, b1=b1, regularizer=updates.Regularizer(l2=l2))
d_updates = d_updater(discrim_params, d_cost)
g_updates = g_updater(gen_params, g_cost)
updates = d_updates + g_updates

print "COMPILING"
t = time()
_train_g = theano.function([X, Z, Y], cost, updates=g_updates)
_train_d = theano.function([X, Z, Y], cost, updates=d_updates)
_gen = theano.function([Z, Y], gX)
print "%.2f seconds to compile theano functions" % (time() - t)

tr_idxs = np.arange(len(trX))
trX_vis = np.asarray([[trX[i] for i in py_rng.sample(tr_idxs[trY == y], 20)] for y in range(10)]).reshape(200, -1)
trX_vis = inverse_transform(transform(trX_vis))
grayscale_grid_vis(trX_vis, (10, 20), "samples/%s_etl_test.png" % desc)

sample_zmb = floatX(np_rng.uniform(-1.0, 1.0, size=(200, nz)))
sample_ymb = floatX(OneHot(np.asarray([[i for _ in range(20)] for i in range(10)]).flatten(), ny))


def gen_samples(n, nbatch=128):
    samples = []
    labels = []
    n_gen = 0
    for i in range(n / nbatch):
        ymb = floatX(OneHot(np_rng.randint(0, 10, nbatch), ny))
        zmb = floatX(np_rng.uniform(-1.0, 1.0, size=(nbatch, nz)))
        xmb = _gen(zmb, ymb)
        samples.append(xmb)
        labels.append(np.argmax(ymb, axis=1))
Esempio n. 11
0
updates = d_updates + g_updates

print 'COMPILING'
t = time()
_train_g = theano.function([X, Z, Y], cost, updates=g_updates)
_train_d = theano.function([X, Z, Y], cost, updates=d_updates)

###
_gen = theano.function([Z, Y], [gX, yb, yb2, d, h3, h5])
print '%.2f seconds to compile theano functions' % (time() - t)

tr_idxs = np.arange(len(trX))
trX_vis = np.asarray([[trX[i] for i in py_rng.sample(tr_idxs[trY == y], 20)]
                      for y in range(10)]).reshape(200, -1)
trX_vis = inverse_transform(transform(trX_vis))
grayscale_grid_vis(trX_vis, (10, 20), 'samples/%s_etl_test.png' % desc)

sample_zmb = floatX(np_rng.uniform(-1., 1., size=(200, nz)))
sample_ymb = floatX(
    OneHot(
        np.asarray([[i for _ in range(20)] for i in range(10)]).flatten(), ny))


def gen_samples(n, nbatch=128):
    samples = []
    labels = []
    n_gen = 0
    for i in range(n / nbatch):
        ymb = floatX(OneHot(np_rng.randint(0, 10, nbatch), ny))
        zmb = floatX(np_rng.uniform(-1., 1., size=(nbatch, nz)))
        xmb, tmp_yb, yb2, d, h3, h5 = _gen(zmb, ymb)
Esempio n. 12
0
d_updates = d_updater(discrim_params, d_cost)
g_updates = g_updater(gen_params, g_cost)
updates = d_updates + g_updates

print 'COMPILING'
t = time()
_train_g = theano.function([X, Z, Y], cost, updates=g_updates)
_train_d = theano.function([X, Z, Y], cost, updates=d_updates)
_gen = theano.function([Z, Y], gX)
print '%.2f seconds to compile theano functions'%(time()-t)

cols = 10
tr_idxs = np.arange(len(trX))
trX_vis = np.asarray([[trX[i] for i in py_rng.sample(tr_idxs[trY==y], cols)] for y in range(ny)]).reshape(ny * cols, -1)
trX_vis = inverse_transform(transform(trX_vis))
grayscale_grid_vis(trX_vis, (ny, cols), 'samples/test.png')


############
# set up targets normally
steps = 6
numtargets = 9 #This is how many letter you will count
start = 1
targets = np.asarray([[i+start for _ in range(steps)] for i in range(numtargets)])
sample_ymb = floatX(OneHot(targets.flatten(), ny))

# set up random z
sample_zmb = floatX(np_rng.uniform(-1., 1., size=(numtargets * steps, nz)))


Esempio n. 13
0
_train_d = theano.function([X, Z, Y], cost, updates=d_updates)
_gen = theano.function(
    [Z, Y], [gX, out_lYS, out_G3_1, out_G3_2, out_G10, out_G11, out_G12])
print('COMPILING...DONE')
print('%.2f seconds to compile theano functions' % (time() - t))

#
# Saving the training images in grey-scaled.
#
tr_idxs = np.arange(len(trX0))
trX0_vis = np.asarray(
    [[trX0[i] for i in py_rng.sample(tr_idxs[trY0 == y], 20)]
     for y in range(10)]).reshape(200, -1)

trX0_vis = inverse_transform(transform(trX0_vis))
grayscale_grid_vis(trX0_vis, (10, 20), 'samples/%s_etl_test.png' % desc)

f_log = open('logs/%s.ndjson' % desc, 'wb')
log_fields = [
    'n_epochs',
    'n_updates',
    'n_examples',
    'n_seconds',
    '1k_va_nnc_acc',
    '10k_va_nnc_acc',
    '100k_va_nnc_acc',
    '1k_va_nnd',
    '10k_va_nnd',
    '100k_va_nnd',
    'g_cost',
    'd_cost',
Esempio n. 14
0
    'g_cost',
    'd_cost',
]

print desc.upper()
n_updates = 0
n_check = 0
n_epochs = 0
n_updates = 0
n_examples = 0
t = time()
sample_z0mb = rand_gen(size=(200, nz0)) # noise samples for top generator module
for epoch in range(1, niter+niter_decay+1):
    trX = shuffle(trX)
    for imb in tqdm(iter_data(trX, size=nbatch), total=ntrain/nbatch):
        imb = transform(imb)
        z0mb = rand_gen(size=(len(imb), nz0))
        if n_updates % (k+1) == 0:
            cost = _train_g(imb, z0mb)
        else:
            cost = _train_d(imb, z0mb)
        n_updates += 1
        n_examples += len(imb)
    samples = np.asarray(_gen(sample_z0mb))
    grayscale_grid_vis(inverse_transform(samples), (10, 20), "{}/{}.png".format(sample_dir, n_epochs))
    n_epochs += 1
    if n_epochs > niter:
        lrt.set_value(floatX(lrt.get_value() - lr/niter_decay))
    if n_epochs in [1, 5, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 150, 200]:
        joblib.dump([p.get_value() for p in gen_params], "{}/{}_gen_params.jl".format(model_dir, n_epochs))
        joblib.dump([p.get_value() for p in discrim_params], "{}/{}_discrim_params.jl".format(model_dir, n_epochs))
Esempio n. 15
0
def test(nep):
    print 'Testing...'
    if phase == 'TEST':
        load_weights(
            'models/multipie_gan/setting2-cross-94.5/60_gen_params.jl',
            'models/multipie_gan/setting2-cross-94.5/60_discrim_params.jl',
            'models/multipie_gan/setting2-cross-94.5/60_test_params.jl')

    test_nbatch = 2000
    batch_feature = []
    for tmb in tqdm(iter_data(teY, size=test_nbatch),
                    total=len(teY) / test_nbatch):
        batch_feature.append(_eigen_encoder(transform(tmb)))
    probe_feature = np.concatenate(batch_feature, axis=0)

    probe_feature_stat = probe_feature.reshape(len(probe_feature), -1)
    probe_feature_var = np.var(probe_feature_stat, axis=1)
    #print probe_feature_var
    #print probe_feature_var.shape

    gallery_feature = probe_feature[range(7 * n_pos + n_pos / 2, len(teY),
                                          n_lum * n_pos)]
    rates = np.full(n_pos, 0).astype(np.float32)
    for probe_idx in tqdm(range(len(teY))):
        max_distance = -100000.0
        max_idx = 0
        for gallery_idx, feature in enumerate(gallery_feature):
            cos_up = np.inner(probe_feature[probe_idx].reshape(-1, ),
                              feature.reshape(-1, ))
            cos_down = np.sqrt((probe_feature[probe_idx]**2).sum()) * np.sqrt(
                (feature**2).sum())
            distance = cos_up / cos_down
            if distance > max_distance:
                max_distance = distance
                max_idx = gallery_idx
        if probe_idx in range(max_idx * n_lum * n_pos,
                              (max_idx + 1) * n_lum * n_pos):
            rates[probe_idx % n_pos] += 1
    rates /= (len(teY) / n_pos)
    print 'rate:', rates, rates.mean()

    print 'Visualisation'
    sample_visual = sample_for_visual()
    sample_poses = sample_visual[1:]
    sample_to_rotate = sample_visual[0]
    pos_codes = [(_pose_lum_encoder(transform(sample_pos))).mean(0)
                 for sample_pos in sample_poses]
    print len(pos_codes)
    eigen_codes = _eigen_encoder(transform(sample_to_rotate))
    print len(eigen_codes)
    rotated_faces = [[
        _face_rotator(eigen_code.reshape(1, -1, 1, 1),
                      pos_code.reshape(1, -1, 1, 1)) for pos_code in pos_codes
    ] for eigen_code in eigen_codes]
    rotated_faces = np.concatenate([
        transform(sample_to_rotate).reshape(5, 1, 1, 1, 64, 64), rotated_faces
    ],
                                   axis=1)
    rotated_faces = np.array(rotated_faces).reshape(5 * (1 + n_pos), -1)
    #rotated_faces = np.vstack([rotated_faces, transform(sample_to_rotate).reshape(5,-1)])
    grayscale_grid_vis(inverse_transform(rotated_faces), (5, (1 + n_pos)),
                       'samples/test_%d.png' % (nep))
    print rotated_faces.shape
    return rates.mean()
Esempio n. 16
0
print desc.upper()
n_updates = 0
n_check = 0
n_epochs = 0
n_updates = 0
n_examples = 0
t = time()
sample_z0mb = rand_gen(size=(200,
                             nz0))  # noise samples for top generator module
for epoch in range(1, niter + niter_decay + 1):
    trX = shuffle(trX)
    for imb in tqdm(iter_data(trX, size=nbatch), total=ntrain / nbatch):
        imb = transform(imb)
        z0mb = rand_gen(size=(len(imb), nz0))
        if n_updates % (k + 1) == 0:
            cost = _train_g(imb, z0mb)
        else:
            cost = _train_d(imb, z0mb)
        n_updates += 1
        n_examples += len(imb)
    samples = np.asarray(_gen(sample_z0mb))
    grayscale_grid_vis(inverse_transform(samples), (10, 20),
                       "{}/{}.png".format(sample_dir, n_epochs))
    n_epochs += 1
    if n_epochs > niter:
        lrt.set_value(floatX(lrt.get_value() - lr / niter_decay))
    if n_epochs in [1, 5, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 150, 200]:
        joblib.dump([p.get_value() for p in gen_params],
                    "{}/{}_gen_params.jl".format(model_dir, n_epochs))
        joblib.dump([p.get_value() for p in discrim_params],
                    "{}/{}_discrim_params.jl".format(model_dir, n_epochs))
d_updates = d_updater(discrim_params, d_cost)
g_updates = g_updater(gen_params, g_cost)
updates = d_updates + g_updates

print('Compiling')
t = time()
_train_g = theano.function([X, Z, Y], cost, updates=g_updates)
_train_d = theano.function([X, Z, Y], cost, updates=d_updates)
_gen = theano.function([Z, Y], gX)
print('%.2f seconds to compile theano functions' % (time() - t))

tr_idxs = np.arange(len(trX))
trX_vis = np.asarray([[trX[i] for i in py_rng.sample(tr_idxs[trY == y], 20)]
                      for y in range(10)]).reshape(200, -1)
trX_vis = trX_vis.reshape(-1, npx, npx)
grayscale_grid_vis(trX_vis, (10, 20), 'samples/cond_dcgan_etl_test.png')

sample_zmb = floatX(np_rng.uniform(-1., 1., size=(200, nz)))
sample_ymb = floatX(
    OneHot(
        np.asarray([[i for _ in range(20)] for i in range(10)]).flatten(), ny))


def gen_samples(n, nbatch=128):
    samples = []
    labels = []
    n_gen = 0
    for i in range(n / nbatch):
        ymb = floatX(OneHot(np_rng.randint(0, 10, nbatch), ny))
        zmb = floatX(np_rng.uniform(-1., 1., size=(nbatch, nz)))
        xmb = _gen(zmb, ymb)