def run_test(img1, img2, test_out_path, test_checkpoints, save_image=False, save_flo=True): prediction = net_structure.net_structure(img1, img2) pred_flow = prediction['flow'] saver = tf.train.Saver() with tf.Session() as sess: if test_checkpoints: saver.restore(sess, test_checkpoints) print 'restored checkpoints' pred_flow = sess.run(pred_flow)[0, :, :, :] unique_name = 'flow_predict_old' + str(uuid.uuid4()) if save_image: flow_img = flow_to_image(pred_flow) full_out_path = os.path.join(test_out_path, unique_name + '.png') print unique_name imsave(full_out_path, flow_img) if save_flo: full_out_path = os.path.join(test_out_path, unique_name + '.flo') write_flow(pred_flow, full_out_path)
def test(checkpoint, input_a_path, input_b_path, img_num, out_path, save_image=True, save_flo=True): input_a = cv2.imread(input_a_path) input_b = cv2.imread(input_b_path) # input_a = cv2.resize(input_a, (512, 384)) # input_b = cv2.resize(input_b, (512, 384)) # Convert from RGB -> BGR input_a = input_a[..., [2, 1, 0]] input_b = input_b[..., [2, 1, 0]] # Scale from [0, 255] -> [0.0, 1.0] if needed # if input_a.max() > 1.0: # input_a = input_a / 255.0 # if input_b.max() > 1.0: # input_b = input_b / 255.0 input_a = input_a / 255.0 input_b = input_b / 255.0 # TODO: This is a hack, we should get rid of this # training_schedule = LONG_SCHEDULE inputs = { 'input_a': tf.expand_dims(tf.constant(input_a, dtype=tf.float32), 0), 'input_b': tf.expand_dims(tf.constant(input_b, dtype=tf.float32), 0), } batch_img1 = create_batch([img1_path]) batch_img2 = create_batch([img2_path]) predictions = net_structure.net_structure(batch_img1, batch_img2) pred_flow = predictions['flow'] init = tf.global_variables_initializer() saver = tf.train.Saver() sess = tf.Session() sess.run(init) saver.restore(sess, checkpoint) pred_flow = sess.run(pred_flow)[0, :, :, :] # unique_name = 'flow-' + str(uuid.uuid4()) unique_name = 'flow_predict_old_' + img_num print unique_name if save_image: flow_img = flow_to_image(pred_flow) full_out_path = os.path.join(out_path, unique_name + '.png') imsave(full_out_path, flow_img) if save_flo: full_out_path = os.path.join(out_path, unique_name + '.flo') write_flow(pred_flow, full_out_path)
def save_results(self, save_dir): batchsize = self.flow_t.size(0) img_target_paths = self.input['img_target_paths'][0] for i in range(batchsize): util.util.mkdir(save_dir + 'flow/' + img_target_paths[i][-17:-9]) util.util.mkdir(save_dir + 'png/' + img_target_paths[i][-17:-9]) write_flow( self.flow_t[i].data.cpu().numpy().transpose(1, 2, 0), save_dir + 'flow/' + img_target_paths[i][-17:-3] + 'flo') save_flow_image( self.flow_t[i].data.cpu().numpy().transpose(1, 2, 0), save_dir + 'png/' + img_target_paths[i][-17:-3] + 'png')
def flow_name_transfer(src_dir,dest_dir,file_num,im_ext): src_fw_pref='ForwardFlow' src_bw_pref='BackwardFlow' dest_fw_pref='fw' dest_bw_pref='bw' set_name='MOT16-02' base_num=511 for file_id in xrange(file_num): #src & dest src_num_name=str(file_id).zfill(3) dest_num_name=str(file_id+base_num).zfill(6) #fw src_fw_name=src_fw_pref+src_num_name+im_ext src_fw_path=os.path.join(src_dir,src_fw_name) dest_fw_name=dest_fw_pref+'_'+set_name+'_'+dest_num_name+im_ext dest_fw_path=os.path.join(dest_dir,dest_fw_name) fw_flow=read_flow(src_fw_path) write_flow(fw_flow,dest_fw_path) #bw src_bw_name=src_bw_pref+src_num_name+im_ext src_bw_path=os.path.join(src_dir,src_bw_name) dest_bw_name=dest_bw_pref+'_'+set_name+'_'+dest_num_name+im_ext dest_bw_path=os.path.join(dest_dir,dest_bw_name) bw_flow=read_flow(src_bw_path) write_flow(bw_flow,dest_bw_path) print src_bw_path print dest_bw_path print 'flow_name_transfer...'
def test(self, checkpoint, input_path, out_path, save_image=False, save_flo=False): img_file_list = os.listdir(input_path) img_file_list.sort(key=lambda f: int(filter(str.isdigit, f))) img_list = [] for img_file_name in img_file_list: img_file_path = os.path.join(input_path, img_file_name) img = imread(img_file_path) img = cv2.resize(img, (512, 384), interpolation=cv2.INTER_AREA) # Convert from RGB -> BGR img = img[..., [2, 1, 0]] img_list.append(img) input_a = np.stack(img_list[:-1], axis=0) input_b = np.stack(img_list[1:], axis=0) # Scale from [0, 255] -> [0.0, 1.0] if needed if input_a.max() > 1.0: input_a = input_a / 255.0 if input_b.max() > 1.0: input_b = input_b / 255.0 # TODO: This is a hack, we should get rid of this training_schedule = LONG_SCHEDULE inputs = { 'input_a': tf.constant(input_a, dtype=tf.float32), 'input_b': tf.constant(input_b, dtype=tf.float32), } predictions = self.model(inputs, training_schedule) pred_flow = predictions['flow'] saver = tf.train.Saver() num = int(filter(str.isdigit, input_path)) file = open(os.path.join(out_path, str(num) + '_flow.csv'), 'w') writer = csv.writer(file, delimiter=',', quotechar='|') with tf.Session() as sess: saver.restore(sess, checkpoint) pred_flow_seq = sess.run(pred_flow) pred_flow_lists = np.split(pred_flow_seq, len(pred_flow_seq), axis=0) for t, pred_flow in enumerate(pred_flow_lists): # unique_name = input_a_path.split('.')[-2].split('/')[-1]+'and'+input_b_path.split('.')[-2].split('/')[-1] unique_name = str(t) pred_flow = np.squeeze(pred_flow) shape = np.shape(pred_flow) mean_flow = np.mean( pred_flow[shape[0] / 4:shape[0] / 4 * 3, shape[1] / 4:shape[1] / 4 * 3, :], axis=(0, 1)) writer.writerow(mean_flow) if save_image: flow_img = flow_to_image(pred_flow) full_out_path = os.path.join(out_path, unique_name + '.png') imsave(full_out_path, flow_img) if save_flo: full_out_path = os.path.join(out_path, unique_name + '.flo') write_flow(pred_flow, full_out_path) file.close()
def test_flow(opt): ##### load testing list ##### with open(opt.dataset_dir + "test_flow.txt", 'r') as f: test_files = f.readlines() input_list = [] for seq in test_files: seq = seq.split(' ') input_list.append(opt.dataset_dir+seq[0]+'/'+seq[1][:-1]) if not os.path.exists(opt.output_dir): os.makedirs(opt.output_dir) ##### init ##### # TODO: currently assuming batch_size = 1 assert opt.batch_size == 1 tgt_image_uint8 = tf.placeholder(tf.uint8, [opt.batch_size, opt.img_height, opt.img_width, 3], name='tgt_input') src_image_stack_uint8 = tf.placeholder(tf.uint8, [opt.batch_size, opt.img_height, opt.img_width, opt.num_source * 3], name='src_stack_input') intrinsics = tf.placeholder(tf.float32, [opt.batch_size, 3, 3], name='intrinsics_input') loader = DataLoader(opt) intrinsics_ms = loader.get_multi_scale_intrinsics(intrinsics, opt.num_scales) # currently assume a sequence is fed and the tgt->src_id flow is computed src_id = int(opt.num_source // 2) bs = opt.batch_size model = GeoNetModel(opt, tgt_image_uint8, src_image_stack_uint8, intrinsics_ms) fetches = {} fetches["pred_flow"] = model.fwd_full_flow_pyramid[0][bs*src_id:bs*(src_id+1)] saver = tf.train.Saver([var for var in tf.model_variables()]) config = tf.ConfigProto() config.gpu_options.allow_growth = True ##### Go! ##### output_file = opt.output_dir + '/' + os.path.basename(opt.init_ckpt_file) if not os.path.exists(output_file): os.makedirs(output_file) binary_dir = os.path.join(output_file, 'binary') color_dir = os.path.join(output_file, 'color') png_dir = os.path.join(output_file, 'png') if (not os.path.exists(binary_dir)): os.makedirs(binary_dir) if (not os.path.exists(color_dir)): os.makedirs(color_dir) if (not os.path.exists(png_dir)): os.makedirs(png_dir) with tf.Session(config=config) as sess: saver.restore(sess, opt.init_ckpt_file) pred_all = [] img_num = len(input_list) for tgt_idx in range(img_num): if (tgt_idx+1) % 100 == 0: print('processing: %d/%d' % (tgt_idx+1, img_num)) image_seq = cv2.imread(input_list[tgt_idx]+'.jpg') tgt_image, src_image_stack = unpack_image_sequence(image_seq, opt.img_height, opt.img_width, opt.num_source) with open(input_list[tgt_idx]+'_cam.txt', 'r') as cf: cam_file = cf.readlines() cam_file = cam_file[0].split(',') cam_file = np.array([float(d) for d in cam_file]) cam_file = np.reshape(cam_file, (3,3)) pred = sess.run(fetches, feed_dict={tgt_image_uint8: tgt_image[None, :, :, :], src_image_stack_uint8: src_image_stack[None, :, :, :], intrinsics: cam_file[None,:,:]}) pred_flow=pred['pred_flow'][0] # save flow flow_fn = '%.6d.png' % tgt_idx color_fn = os.path.join(color_dir, flow_fn) color_flow = fl.flow_to_image(pred_flow) color_flow = cv2.cvtColor(color_flow, cv2.COLOR_RGB2BGR) color_flow = cv2.imwrite(color_fn, color_flow) png_fn = os.path.join(png_dir, flow_fn) mask_blob = np.ones((opt.img_height, opt.img_width), dtype = np.uint16) fl.write_kitti_png_file(png_fn, pred_flow, mask_blob) binary_fn = flow_fn.replace('.png', '.flo') binary_fn = os.path.join(binary_dir, binary_fn) fl.write_flow(pred_flow, binary_fn)
# # There is some non-deterministic nan-bug in caffe # it seems to be a race-condition # print('Network forward pass using %s.' % args.caffemodel) i = 1 while i <= 5: i += 1 net.forward(**input_dict) containsNaN = False for name in net.blobs: blob = net.blobs[name] has_nan = np.isnan(blob.data[...]).any() if has_nan: print('blob %s contains nan' % name) containsNaN = True if not containsNaN: print('Succeeded.') break else: print('**************** FOUND NANs, RETRYING ****************') blob = np.squeeze(net.blobs['predict_flow_final'].data).transpose(1, 2, 0) write_flow(args.out, blob)
import argparse from scipy import misc import caffe import tempfile from math import ceil parser = argparse.ArgumentParser() parser.add_argument('listfile', help='one line should contain paths "img0.ext img1.ext gt.flo out.flo"') args = parser.parse_args() if(not os.path.exists(args.listfile)): raise BaseException('listfile does not exist: '+args.listfile) def readTupleList(filename): list = [] for line in open(filename).readlines(): if line.strip() != '': list.append(line.split()) return list ops = readTupleList(args.listfile) for ent in ops: fn_img = ent[0] fn_flo = ent[3] fn_out = fn_flo[:fn_flo.rindex('.')] + '_crf.flo' crf_flo = apply_crf(fn_img, fn_flo) write_flow(fn_out, crf_flo) print("Wrote to: " + fn_out)
flows = np.expand_dims(np.array(range(1, flow_range + 1)), axis=-1) q = q * flows q = np.sum(q, axis=0) q = q - shift # Find out the most probable class for each pixel. #MAP = np.argmax(Q, axis=0) #MAP = MAP - shift flow_xy_out[:, :, axis] = q.reshape(flow.shape) return flow_xy_out if __name__ == '__main__': if len(sys.argv) != 4: print("Usage: python {} IMAGE_0 FLO_FILE OUTPUT_FLO_FILE".format( sys.argv[0])) print("") print("IMAGE_0 is the first input image.") print("FLO_FILE is the flow predictions generated by the FlowNet.") print( "OUTPUT_FLO_FILE is the output file where the updated flow should be written." ) sys.exit(1) fn_im = sys.argv[1] fn_flo = sys.argv[2] fn_output = sys.argv[3] flow_xy_out = apply_crf(fn_im, fn_flo) # Write to output file: write_flow(fn_output, flow_xy_out)