def test_grid_to_quads(): N = [2, 3] R, C = get_mesh_grid(N) patches = grid_to_quads(R, C) sess = tf.InteractiveSession() print(N) print(sess.run([R, C])) print(sess.run(patches))
def test_get_quad_index_basis_by_lines(): from grid import get_mesh_grid, fit_lines_to_grid_tf from quad import grid_to_quads N = [9, 10] R, C = get_mesh_grid(N) quads = grid_to_quads(R, C) lines = tf.constant([[0.1, 0.2, 0.3, 0.4], [0.2, 0.3, 0.1, 0.5], [0.5, 0.7, 0.1, 0.9], [0.4, 0.1, 0.5, 0.7]], dtype=tf.float32) lines = fit_lines_to_grid_tf(lines, N) index, coeff, v, v1, v2 = get_quad_index_basis_by_lines(lines, N) print("quads, index", quads, index) quads_l = tf.gather(quads, index) sess = tf.InteractiveSession() _lines, _quads_l, _coeff, _v, _v1, _v2 = sess.run([lines, quads_l, coeff, v, v1, v2]) for i in range(_lines.shape[0]): print(_lines[i]) print(_lines[i, 2:] - _lines[i, :2]) print(np.sum(_coeff[i] * _quads_l[i], axis=(0, 1))) print("quad", _quads_l[i]) print("v, v1, v2", _v[i], _v1[i], _v2[i]) print(_coeff[i]) print('----')
def test_Vq(): N = [3, 3] R, C = get_mesh_grid(N) Vq = get_Vq(R, C) sess = tf.InteractiveSession() print(sess.run(Vq))
def model_fn(image, angle, N, lambda_s=1e-7, lambda_b=10, lambda_l=1e-5, lambda_r=1e-5): import matplotlib.pyplot as plt lines = lsd(image, N) R0, C0 = get_mesh_grid(N) theta_lines = get_theta_by_lines(lines, angle) theta_index = get_bin_by_theta(theta_lines, angle) theta0 = tf.scatter_add( tf.Variable(np.zeros(90), trainable=False, dtype=tf.float32), theta_index, theta_lines) / tf.scatter_add( tf.Variable(np.zeros(90), trainable=False, dtype=tf.float32), theta_index, tf.ones_like(theta_lines)) theta = tf.get_variable('theta', shape=[90], dtype=tf.float32, trainable=True) R = tf.get_variable('R', shape=R0.get_shape().as_list(), dtype=tf.float32, trainable=True) C = tf.get_variable('C', shape=C0.get_shape().as_list(), dtype=tf.float32, trainable=True) assign_op = tf.group(tf.assign(theta, theta0), tf.assign(R, R0), tf.assign(C, C0)) loss = lambda_s * shape_preservation_energy(theta, R, C, R0, C0) + \ lambda_b * boundary_preservation_energy(R, C) + \ lambda_l * line_preservation_energy(theta, N, R, C, lines, angle) + \ lambda_r * rotation_energy(theta, angle) shape = image.get_shape().as_list() flow = tf.stack([(R - R0), (C - C0)], axis=-1) movement = tf.reduce_mean(tf.sqrt(tf.reduce_sum(flow**2, axis=-1))) new_image = warp( image, R0, C0, R, C ) #tf.cast(tf.contrib.image.dense_image_warp(tf.cast(tf.expand_dims(image, axis=0), tf.float32), flow)[0], tf.uint8) # TODO sparse_image_warp opt = tf.train.AdamOptimizer(1e-1) train_op = opt.minimize(loss, var_list=[theta, R, C]) with tf.Session() as sess: _image_in = sess.run(image) plt.imshow(_image_in) plt.show() sess.run(tf.global_variables_initializer()) sess.run(assign_op) print("opt by theta") for i in range(2000): _, _loss, _movement = sess.run([train_op, loss, movement]) print("loss: %f, movement: %f" % (_loss, _movement)) if i % 100 == 0: _image = sess.run(new_image) plt.imshow(np.concatenate([_image, _image_in], axis=1)) plt.show() print(sess.run(theta))