def test_flow_warp(): net = Net(2) imga = Image.open(IMG1) imgb = Image.open(IMG2) imga = np.asarray(imga).astype('float32') / 255 imgb = np.asarray(imgb).astype('float32') / 255 imga = imga[..., [2, 1, 0]] imgb = imgb[..., [2, 1, 0]] with tf.Session() as sess: net.restore(sess, checkpoint) imga = np.expand_dims(imga, 0) imgb = np.expand_dims(imgb, 0) print(imga.shape, imgb.shape) flow = net.flow_results.eval(feed_dict={ 'flow/inputa:0': imga, 'flow/inputb:0': imgb, }) x = flow u = x[..., 0] v = x[..., 1] image = tf.constant(imgb[..., [2, 1, 0]]) wp = warp(image, u, v, additive_warp=True).eval()[0] wp = (wp * 255).astype('uint8') Image.fromarray(wp, 'RGB').show() wp = flow_warp(image, flow).eval()[0] wp = (wp * 255).astype('uint8') Image.fromarray(wp, 'RGB').show()
def test_output_flow(): """calc flow""" net = Net(2) imga = Image.open(IMG1) imgb = Image.open(IMG2) imga = np.asarray(imga).astype('float32') / 255 imgb = np.asarray(imgb).astype('float32') / 255 imga = imga[..., [2, 1, 0]] imgb = imgb[..., [2, 1, 0]] with tf.Session() as sess: net.restore(sess, checkpoint) imga = np.expand_dims(imga, 0) imgb = np.expand_dims(imgb, 0) print(imga.shape, imgb.shape) flow = net.flow_results.eval(feed_dict={ 'flow/inputa:0': imga, 'flow/inputb:0': imgb, }) x = flow[0] x = _viz_flow(x[..., 0], x[..., 1]) Image.fromarray(x, 'RGB').save('flow.png')
def test_reuse(): net = Net(2) x1 = tf.ones([1, 128, 128, 3]) x2 = tf.ones([1, 128, 128, 3]) y1 = tf.ones([1, 128, 128, 3]) y2 = tf.ones([1, 128, 128, 3]) f1 = net(x1, x2) f2 = net(y1, y2) with tf.Session() as sess: net.restore2(sess, checkpoint, 'FlowNet2') f1 = f1.eval() f2 = f2.eval() assert np.all(np.abs(f1 - f2) < 1.e-6)
def main(): # Create a new network net = FlowNet2(mode=Mode.TEST) # Train on the data net.test( checkpoint='./checkpoints/FlowNet2/flownet-2.ckpt-0', input_a_path=FLAGS.input_a, input_b_path=FLAGS.input_b, out_path=FLAGS.out, )
def main(): flag = parser.parse_args() if flag.name.lower() == 'flownet2': net = FlowNet2(2) ckpt = 'checkpoints/FlowNet2/flownet-2.ckpt-0' elif flag.name.lower() == 'flownets': net = FlowNetS(2) ckpt = 'checkpoints/FlowNetS/flownet-S.ckpt-0' elif flag.name.lower() == 'flownetsd': net = FlowNetSD(2) ckpt = 'checkpoints/FlowNetSD/flownet-SD.ckpt-0' elif flag.name.lower() == 'flownetc': net = FlowNetC(2) ckpt = 'checkpoints/FlowNetC/flownet-C.ckpt-0' elif flag.name.lower() == 'flownetcs': net = FlowNetCS(2) ckpt = 'checkpoints/FlowNetCS/flownet-CS.ckpt-0' elif flag.name.lower() == 'flownetcss': net = FlowNetCSS(2) ckpt = 'checkpoints/FlowNetCSS/flownet-CSS.ckpt-0' else: return with tf.Session() as sess: net.restore(sess, ckpt) tf.logging.info("exporting to {}/{}".format(flag.dir, flag.name)) builder = tf.saved_model.builder.SavedModelBuilder(flag.dir + '/' + flag.name) # build the signature_def_map input_a = sess.graph.get_tensor_by_name('flow/inputa:0') input_b = sess.graph.get_tensor_by_name('flow/inputb:0') output = net.flow_results inputs = { 'input_a': tf.saved_model.utils.build_tensor_info(input_a), 'input_b': tf.saved_model.utils.build_tensor_info(input_b) } outputs = { 'output_flow': tf.saved_model.utils.build_tensor_info(output) } sig = tf.saved_model.signature_def_utils.build_signature_def( inputs=inputs, outputs=outputs, method_name=tf.saved_model.signature_constants.PREDICT_METHOD_NAME) builder.add_meta_graph_and_variables( sess, [tf.saved_model.tag_constants.SERVING], signature_def_map={ tf.saved_model.signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY: sig }, strip_default_attrs=True) builder.save()
from src.flowlib import flow_to_image from src.flownet2.flownet2 import FlowNet2 from src.training_schedules import LONG_SCHEDULE img_dir = [ y for x in os.walk('./test') for y in glob(os.path.join(x[0], '*.png')) ] img_dir.sort() a = cv2.imread(img_dir[0]) / 255.0 b = cv2.imread(img_dir[1]) / 255.0 inputs = { 'input_a': tf.expand_dims(tf.constant(a, dtype=tf.float32), 0), 'input_b': tf.expand_dims(tf.constant(b, dtype=tf.float32), 0) } model = FlowNet2(mode=Mode.TEST, debug=False) predictions = model(inputs, LONG_SCHEDULE) pred_flow = predictions['flow'] saver = tf.train.Saver() with tf.Session() as sess: #saver = tf.train.import_meta_graph('./checkpoints/FlowNet2/flownet-2.ckpt-0.meta') saver.restore(sess, './checkpoints/FlowNet2/flownet-2.ckpt-0') pred = sess.run(pred_flow) print(pred.keys()) pred_flow = pred['flow'] fimg = flow_to_image(pred_flow) cv2.imwrite('aaa.png', fimg)
def test_restore_checkpoint(): """restore from checkpoint""" net = Net(2) # Test Mode with tf.Session() as sess: net.restore(sess, checkpoint)
import os from src.net import Mode from src.flownet2.flownet2 import FlowNet2 from src.dataloader import load_batch from src.dataset_configs import FLYING_CHAIRS_DATASET_CONFIG import keras import tensorflow as tf net = FlowNet2() input_a, input_b, flow = load_batch(FLYING_CHAIRS_DATASET_CONFIG, 'sample', net.global_step) a = 0 """ with tf.Session() as sess: saver = tf.train.import_meta_graph('./checkpoints/FlowNet2/flownet-2.ckpt-0.meta') saver.restore(sess, './checkpoints/FlowNet2/flownet-2.ckpt-0') saver. """