예제 #1
0
def gen_samples(n, nbatch=128):
    samples = []
    labels = []
    n_gen = 0
    for i in range(n / nbatch):
        print 'i:', i
        # ymb.shape = (nbatch, ny)
        ymb = floatX(OneHot(np_rng.randint(0, 10, nbatch), NUM_LABEL))
        print 'gen_samples: ymb:', ymb.shape
        print ymb

        # zmb.shape = (nbatch, DIM_Z)
        zmb = floatX(np_rng.uniform(-1., 1., size=(nbatch, DIM_Z)))
        print 'gen_samples: zmb:', zmb.shape
        print zmb

        # xmd
        xmb = _gen(zmb, ymb)
        print 'gen_samples: xmb:', xmb.shape
        print rH2

        samples.append(xmb)
        labels.append(np.argmax(ymb, axis=1))
        n_gen += len(xmb)
    n_left = n - n_gen
    ymb = floatX(OneHot(np_rng.randint(0, 10, n_left), NUM_LABEL))
    zmb = floatX(np_rng.uniform(-1., 1., size=(n_left, DIM_Z)))
    xmb = _gen(zmb, ymb)
    samples.append(xmb)
    labels.append(np.argmax(ymb, axis=1))
    return np.concatenate(samples, axis=0), np.concatenate(labels, axis=0)
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)
        samples.append(xmb)
        labels.append(np.argmax(ymb, axis=1))
        n_gen += len(xmb)
    n_left = n - n_gen
    ymb = floatX(OneHot(np_rng.randint(0, 10, n_left), ny))
    zmb = floatX(np_rng.uniform(-1., 1., size=(n_left, nz)))
    xmb = _gen(zmb, ymb)
    samples.append(xmb)
    labels.append(np.argmax(ymb, axis=1))
    return np.concatenate(samples, axis=0), np.concatenate(labels, axis=0)
예제 #3
0
def gen_samples(n, nbatch, ysize):
    samples = []
    labels = []
    n_gen = 0
    for i in range(n / nbatch):
        ymb = floatX(OneHot(np_rng.randint(0, 10, nbatch), ysize))
        zmb = sample_z(nbatch, Z_SIZE)
        xmb, ot_lYS, ot_G3_1, ot_G3_2, ot_G10, ot_G11, ot_G12 = _gen(zmb, ymb)
        samples.append(xmb)
        labels.append(np.argmax(ymb, axis=1))
        n_gen += len(xmb)

    # fraction part
    n_left = n - n_gen
    ymb = floatX(OneHot(np_rng.randint(0, 10, nbatch), ysize))
    zmb = sample_z(nbatch, Z_SIZE)
    xmb, ot_lYS, ot_G3_1, ot_G3_2, ot_G10, ot_G11, ot_G12 = _gen(zmb, ymb)

    xmb = xmb[0:n_left]

    samples.append(xmb)
    labels.append(np.argmax(ymb, axis=1))
    return np.concatenate(samples, axis=0), np.concatenate(labels, axis=0)
예제 #4
0
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)
        print 'tmp_yb:', tmp_yb.shape
        print 'yb2:', yb2.shape
        print 'd:', d.shape
        print 'h3:', h3.shape
        print 'h5:', h5.shape
        sys.exit()
        samples.append(xmb)
        labels.append(np.argmax(ymb, axis=1))
        n_gen += len(xmb)
    n_left = n - n_gen
    ymb = floatX(OneHot(np_rng.randint(0, 10, n_left), ny))
    zmb = floatX(np_rng.uniform(-1., 1., size=(n_left, nz)))
    xmb = _gen(zmb, ymb)
    samples.append(xmb)
    labels.append(np.argmax(ymb, axis=1))
    return np.concatenate(samples, axis=0), np.concatenate(labels, axis=0)
예제 #5
0
    return (floatX(X) / 255.).reshape(-1, nc, npx, npx)


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

gX = gen(Z, Y, *gen_params)
dX = discrim(X, Y, *discrim_params)

_gen = theano.function([Z, Y], gX)
_discrim = theano.function([X, Y], dX)

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

scores = _discrim(samples, sample_ymb)
color_grid_vis(inverse_transform(samples), (ny, 10), 'samples.png')

for i in range(ny):
    Z = T.matrix()
    X = T.tensor4()
    Y = T.matrix()

    gX = gen(Z, Y, *gen_params)
    dX = discrim(X, Y, *discrim_params)

    _gen = theano.function([Z, Y], gX)
    _discrim = theano.function([X, Y], dX)
d_updates = d_updater(discrim_params, d_cost)
g_updates = g_updater(gen_params, g_cost)

print 'COMPILING'
t = time()
_gen = theano.function([Z, Y], gX)
_train_d = theano.function([X, X0, Y], d_cost, updates=d_updates)
_train_g = theano.function([Z, Y, deltaX], g_cost, updates=g_updates)
_vgd_gradient = theano.function([X0, X1, Y], vgd_gradient(X0, X1, Y))
_reconstruction_cost = theano.function([X], T.mean(mse_data))
print '%.2f seconds to compile theano functions' % (time() - t)

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))

n_updates = 0

t = time()
for epoch in range(niter):
    print 'cifar 10, vgd, %s, iter %d' % (desc, epoch)
    trX, trY = shuffle(trX, trY)
    for imb, ymb in tqdm(iter_data(trX, trY, size=nbatch),
                         total=ntrain / nbatch):
        imb = transform(imb.reshape(imb.shape[0], nc, npx, npx))
        ymb = floatX(OneHot(ymb, ny))
        zmb = floatX(np_rng.uniform(-1., 1., size=(imb.shape[0], nz)))

        # generate samples
        samples = _gen(zmb, ymb)
예제 #7
0
_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)
        print 'tmp_yb:', tmp_yb.shape
        print 'yb2:', yb2.shape
        print 'd:', d.shape
        print 'h3:', h3.shape
        print 'h5:', h5.shape
예제 #8
0

#
#
# main
#
#

if __name__ == '__main__':
    NUM_CLASSES = 10  # # of classes
    nz = 100  # # of dim for Z

    Z = T.matrix('random')
    Y = T.matrix('label')

    #####
    mnist = BinaryMnist()
    generator = mnist.makeGeneratorLayers(NUM_MINIBATCH, Z, nz, Y, NUM_CLASSES)
    out = ll.get_output(generator)

    print 'compiling...'
    out_func = theano.function([Z, Y], out, mode='DebugMode')
    print 'compiling...DONE'

    #test
    Zval = floatX(np_rng.uniform(-1.0, 1.0, size=(NUM_MINIBATCH, nz)))
    Yval = floatX(OneHot(np_rng.randint(0, 10, NUM_MINIBATCH), NUM_CLASSES))

    ret = out_func(Zval, Yval)
    print 'ret', ret.shape
예제 #9
0
    # Generate images at this time.
    genout, out_lYS, out_G3_1, out_G3_2, out_G10, out_G11, out_G12 = _gen(
        sample_zmb, sample_ymb)
    samples = genout
    grayscale_grid_vis(inverse_transform(samples), (10, 10),
                       'samples/%s/%d.png' % (desc, n_epochs))

    #
    for imb0, imb, ymb in tqdm(iter_data(trX0, trX, trY, size=MINIBATCH_SIZE),
                               total=ntrain / MINIBATCH_SIZE):
        # X:real data
        if not IS_BINARY:
            # transform imb to (?, 1, 28, 28)
            imb = transform(imb)
            ymb = floatX(np.uint8(OneHot(ymb, NUM_Y)))

        #imb:[0.0, 255]
        imb = expandRows(imb, MINIBATCH_SIZE)
        if at_first is True:
            print 'imb:', imb.shape, np.min(imb), np.max(imb)

        # Y: label
        ymb = expandRows(ymb, MINIBATCH_SIZE)
        if at_first is True:
            print 'ymb:', ymb.shape, np.min(ymb), np.max(ymb)

        # Z: random variabel from Gaussian
        zmb = sample_z(len(imb), Z_SIZE)
        if at_first is True:
            print_out(zmb, 'zmb')
예제 #10
0
def mnistGANcond():
    """
    This example loads the 32x32 imagenet model used in the paper,
    generates 400 random samples, and sorts them according to the
    discriminator's probability of being real and renders them to
    the file samples.png
    """

    nc = 1
    npx = 28
    ngf = 64  # # of gen filters in first conv layer
    ndf = 128
    ny = 10  # # of classes

    nz = 100  # # of dim for Z
    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 = 10  # # of classes
    nbatch = 128  # # of examples in batch
    npx = 28  # # of pixels width/height of images
    nz = 100  # # of dim for Z
    ngfc = 1024  # # of gen units for fully connected layers
    ndfc = 1024  # # 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 = 100  # # of iter at starting learning rate
    niter_decay = 100  # # of iter to linearly decay learning rate to zero
    lr = 0.0002

    relu = activations.Rectify()
    sigmoid = activations.Sigmoid()
    lrelu = activations.LeakyRectify()
    tanh = activations.Tanh()

    model_path = 'dcgan_code-master/mnist/models/cond_dcgan/'
    gen_params = [
        sharedX(p) for p in joblib.load(model_path + '200_gen_params.jl')
    ]
    discrim_params = [
        sharedX(p) for p in joblib.load(model_path + '200_discrim_params.jl')
    ]

    def gen(Z, Y, w, w2, w3, wx):
        yb = Y.dimshuffle(0, 1, 'x', 'x')
        Z = T.concatenate([Z, Y], axis=1)
        h = relu(batchnorm(T.dot(Z, w)))
        h = T.concatenate([h, Y], axis=1)
        h2 = relu(batchnorm(T.dot(h, w2)))
        h2 = h2.reshape((h2.shape[0], ngf * 2, 7, 7))
        h2 = conv_cond_concat(h2, yb)
        h3 = relu(
            batchnorm(deconv(h2, w3, subsample=(2, 2), border_mode=(2, 2))))
        h3 = conv_cond_concat(h3, yb)
        x = sigmoid(deconv(h3, wx, subsample=(2, 2), border_mode=(2, 2)))
        return x

    def discrim(X, Y, w, w2, w3, wy):
        yb = Y.dimshuffle(0, 1, 'x', 'x')
        X = conv_cond_concat(X, yb)
        h = lrelu(dnn_conv(X, w, subsample=(2, 2), border_mode=(2, 2)))
        h = conv_cond_concat(h, yb)
        h2 = lrelu(
            batchnorm(dnn_conv(h, w2, subsample=(2, 2), border_mode=(2, 2))))
        h2 = T.flatten(h2, 2)
        h2 = T.concatenate([h2, Y], axis=1)
        h3 = lrelu(batchnorm(T.dot(h2, w3)))
        h3 = T.concatenate([h3, Y], axis=1)
        y = sigmoid(T.dot(h3, wy))
        return y

    def inverse_transform(X):
        X = (X.reshape(-1, nc, npx, npx).transpose(0, 2, 3, 1) + 1.) / 2.
        return X

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

    gX = gen(Z, Y, *gen_params)
    dX = discrim(X, Y, *discrim_params)

    _gen = theano.function([Z, Y], gX)
    _discrim = theano.function([X, Y], dX)

    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))
    samples = _gen(sample_zmb, sample_ymb)
    scores = _discrim(samples, sample_ymb)
    print(scores[1:10])
    sort = np.argsort(scores.flatten())[::-1]
    samples = samples[sort]
    print(np.shape(inverse_transform(samples)))
    print(min(scores))
    print(max(scores))

    color_grid_vis(inverse_transform(samples), (20, 20), 'samples.png')

    return inverse_transform(samples), sample_ymb