(model_G, layer='conv4', npx=64, alpha=0.002)
| 31 | |
| 32 | |
| 33 | def def_bfgs(model_G, layer='conv4', npx=64, alpha=0.002): |
| 34 | print('COMPILING...') |
| 35 | t = time() |
| 36 | |
| 37 | x_f = T.tensor4() |
| 38 | x = T.tensor4() |
| 39 | z = T.matrix() |
| 40 | tanh = activations.Tanh() |
| 41 | gx = model_G(tanh(z)) |
| 42 | |
| 43 | if layer is 'hog': |
| 44 | gx_f = HOGNet.get_hog(gx, use_bin=True, BS=4) |
| 45 | else: |
| 46 | gx_t = AlexNet.transform_im(gx) |
| 47 | gx_net = AlexNet.build_model(gx_t, layer=layer, shape=(None, 3, npx, npx)) |
| 48 | AlexNet.load_model(gx_net, layer=layer) |
| 49 | gx_f = lasagne.layers.get_output(gx_net[layer], deterministic=True) |
| 50 | |
| 51 | f_rec = T.mean(T.sqr(x_f - gx_f), axis=(1, 2, 3)) * sharedX(alpha) |
| 52 | x_rec = T.mean(T.sqr(x - gx), axis=(1, 2, 3)) |
| 53 | cost = T.sum(f_rec) + T.sum(x_rec) |
| 54 | grad = T.grad(cost, z) |
| 55 | output = [cost, grad, gx] |
| 56 | _invert = theano.function(inputs=[z, x, x_f], outputs=output) |
| 57 | |
| 58 | print('%.2f seconds to compile _bfgs function' % (time() - t)) |
| 59 | return _invert, z |
| 60 | |
| 61 | |
| 62 | def def_predict(model_P): |
no test coverage detected