def def_bfgs(model_G, layer='conv4', npx=64, alpha=0.002): print('COMPILING...') t = time() # 符号化定义 x_f = T.tensor4() x = T.tensor4() z = T.matrix() # 随机种子 tanh = activations.Tanh() gx = model_G(tanh(z)) # 生成的图像 if layer is 'hog': gx_f = HOGNet.get_hog(gx, use_bin=True, BS=4) else: # 调整图像格式 gx_t = AlexNet.transform_im(gx) gx_net = AlexNet.build_model(gx_t, layer=layer, shape=(None, 3, npx, npx)) AlexNet.load_model(gx_net, layer=layer) # AlexNet截止在layer的输出 gx_f = lasagne.layers.get_output(gx_net[layer], deterministic=True) f_rec = T.mean(T.sqr(x_f - gx_f), axis=(1, 2, 3)) * sharedX(alpha) x_rec = T.mean(T.sqr(x - gx), axis=(1, 2, 3)) cost = T.sum(f_rec) + T.sum(x_rec) grad = T.grad(cost, z) output = [cost, grad, gx] _invert = theano.function(inputs=[z, x, x_f], outputs=output) print('%.2f seconds to compile _bfgs function' % (time() - t)) return _invert, z
def def_bfgs(net, layer='conv4', npx=64, alpha=0.002): print('COMPILING...') t = time() x_f = T.tensor4() x = T.tensor4() z = T.matrix() z = theano.printing.Print('this is z')(z) tanh = activations.Tanh() tz = tanh(z) # tz = printing_op(tz) # tz = z_scale * tz net.labels_var = T.TensorType('float32', [False] * 512) ('labels_var') gx = net.G.eval(z, net.labels_var, ignore_unused_inputs=True) # gx = printing_op(gx) # gx = misc.adjust_dynamic_range(gx, [-1,1], [0,1]) scale_factor = 16 gx = theano.tensor.signal.pool.pool_2d(gx, ds=(scale_factor, scale_factor), mode='average_exc_pad', ignore_border=True) # gx = printing_op(gx) if layer is 'hog': gx_f = HOGNet.get_hog(gx, use_bin=True, BS=4) else: gx_t = AlexNet.transform_im(gx) gx_net = AlexNet.build_model(gx_t, layer=layer, shape=(None, 3, npx, npx)) AlexNet.load_model(gx_net, layer=layer) gx_f = lasagne.layers.get_output(gx_net[layer], deterministic=True) f_rec = T.mean(T.sqr(x_f - gx_f), axis=(1, 2, 3)) * sharedX(alpha) x_rec = T.mean(T.sqr(x - gx), axis=(1, 2, 3)) cost = T.sum(f_rec) + T.sum(x_rec) grad = T.grad(cost, z) output = [cost, grad, gx] _invert = theano.function(inputs=[z, x, x_f], outputs=output) print('%.2f seconds to compile _bfgs function' % (time() - t)) return _invert,z
dg2 = gain_ifn((ndf * 2), 'dg2') db2 = bias_ifn((ndf * 2), 'db2') dw3 = difn((ndf * 4, ndf * 2, 5, 5), 'dw3') dg3 = gain_ifn((ndf * 4), 'dg3') db3 = bias_ifn((ndf * 4), 'db3') dw4 = difn((ndf * 8, ndf * 4, 5, 5), 'dw4') dg4 = gain_ifn((ndf * 8), 'dg4') db4 = bias_ifn((ndf * 8), 'db4') dwy = difn((ndf * 8 * 4 * 4, 1), 'dwy') dwy1 = difn((ndf * 8 * 4 * 4, 1), 'dwy') # models relu = activations.Rectify() sigmoid = activations.Sigmoid() lrelu = activations.LeakyRectify() tanh = activations.Tanh() bce = T.nnet.binary_crossentropy # generator model gen_params = [gw, gg, gb, gw2, gg2, gb2, gw3, gg3, gb3, gw4, gg4, gb4, gwx] def gen(Z, w, g, b, w2, g2, b2, w3, g3, b3, w4, g4, b4, wx): h = relu(batchnorm(T.dot(Z, w), g=g, b=b)) h = h.reshape((h.shape[0], ngf * 8, 4, 4)) h2 = relu( batchnorm(deconv(h, w2, subsample=(2, 2), border_mode=(2, 2)), g=g2, b=b2)) h3 = relu( batchnorm(deconv(h2, w3, subsample=(2, 2), border_mode=(2, 2)),
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