Skip to content

Instantly share code, notes, and snippets.

@geohot
Created February 23, 2017 09:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save geohot/1f6475bbd3c75f3621acf2c6ea04fc0b to your computer and use it in GitHub Desktop.
Save geohot/1f6475bbd3c75f3621acf2c6ea04fc0b to your computer and use it in GitHub Desktop.
Really Low Quality Bundle Adjustment in Tensorflow
guess = np.concatenate([np.array(map(lambda x: np.array(x[:,3]).ravel(), rts)), fpts[:, 0:3]], axis=0)
selects = []
xys = []
for i, trk in enumerate(good_good_tracks):
for shot, kp in trk:
xy = G.node[(shot,kp)]['kp']/0.81412059
xys.append(xy)
select = np.zeros((len(frms)+len(good_good_tracks),))
select[shot] = 1
select[len(frms)+i] = 1
selects.append(select)
selects, xys = np.array(selects), np.array(xys)
# initial loss
vt = np.dot(selects, guess)
proj = (vt[:, 0:2].T / vt[:, 2]).T
errs = np.sum((xys - proj)**2, axis=1)
print np.sum(errs)
# setup tensorflow
import tensorflow as tf
#fake_guess = np.zeros_like(guess)
#fake_guess[len(frms):, 2] = 1.0
t = tf.Variable(guess, dtype=tf.float32)
selects_tf = tf.placeholder(tf.float32, [None, len(frms)+len(good_good_tracks)])
xys_tf = tf.placeholder(tf.float32, [None, 2])
mask_tf = tf.placeholder(tf.float32, [None])
vt = tf.matmul(selects_tf, t)
proj = tf.transpose(tf.transpose(vt[:, 0:2]) / vt[:, 2])
cost = tf.reduce_sum(tf.transpose(((xys_tf - proj)**2)) * mask_tf)
optimizer = tf.train.AdamOptimizer().minimize(cost)
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)
mask = np.array([1.0] * (selects.shape[0]))
# run for 100 epochs
for i in range(100):
ret = sess.run([optimizer, cost], feed_dict={selects_tf: selects, xys_tf: xys, mask_tf: mask})
if i%10 == 0:
# do ransac
vt = np.dot(selects, t.value().eval(session=sess))
proj = (vt[:, 0:2].T / vt[:, 2]).T
errs = np.sum((xys - proj)**2, axis=1)
mask[np.where(errs>0.00001)] = 0.0
print i, ret, sum(mask)
tfout = t.value().eval(session=sess)
tfrts = np.zeros((len(frms),3,4))
tfrts[:, :, 3] = tfout[0:len(frms)]
tfrts[:, 0, 0] = 1.0
tfrts[:, 1, 1] = 1.0
tfrts[:, 2, 2] = 1.0
tfpts = tfout[len(frms):]
tfrts.shape, tfpts.shape
#plot(tfrts[:, :, 3])
@prclibo
Copy link

prclibo commented Oct 10, 2017

Hi George,

Was googling some words like "tensorflow and bundle adjustment" and find only this page. Just wondering how this "low quality bundle adjustment "is working in your experiments?

Thanks, Bo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment