def evaluate(ae): test_idx = 0 log_string(tf_util.toGreen('=============Testing=============')) recon_losses = [] #try: # while not ae.coord.should_stop(): #ae.sess.run(ae.assign_i_op, feed_dict={ae.set_i_to_pl: i}) for test_idx in range(100): tic = time.time() feed_dict = {ae.is_training: False, ae.data_loader.is_training: False} ops_to_run = [ae.counter, ae.recon_loss] stuff = ae.sess.run(ops_to_run, feed_dict=feed_dict) step, recon_loss = stuff toc = time.time() recon_losses.append(recon_loss) #log_string('Iteration: {} time {}, loss: {}, depth_recon_loss: {}, sn_recon_loss {}, mask_cls_loss {}'.format(i, \ # toc-tic, loss, depth_recon_loss, sn_recon_loss, mask_cls_loss)) #test_idx += 1 log_string(tf_util.toGreen('===========Done testing===========')) toc = time.time() mean_recon_loss = np.mean(np.asarray(recon_losses)) log_string(tf_util.toRed('Test time {}s, recon loss: {}.'.format(\ toc-tic, mean_recon_loss))) losses = {'loss_depth_recon': mean_recon_loss} return losses
def check_delete(): if FLAGS.force_delete: return True delete_key = raw_input( tf_util.toRed( '===== %s exists. Delete? [y (or enter)/N] ' % FLAGS.LOG_DIR)) return delete_key == 'y' or delete_key == ''
def evaluate(ae): test_idx = 0 log_string(tf_util.toGreen('=============Testing=============')) recon_losses = [] IoUs = [] #try: # while not ae.coord.should_stop(): #ae.sess.run(ae.assign_i_op, feed_dict={ae.set_i_to_pl: i}) for test_idx in range(100): tic = time.time() feed_dict = {ae.is_training: False, ae.data_loader.is_training: False} ops_to_run = [ae.counter, ae.recon_loss, ae.preds, ae.voxel_batch] stuff = ae.sess.run(ops_to_run, feed_dict=feed_dict) step, recon_loss, preds, voxel_batch = stuff toc = time.time() recon_losses.append(recon_loss) def compute_IoU(a, b): a[a >= 0.5] = 1 a[a < 0.5] = 0 inter = a * b sum_inter = np.sum(inter[:]) union = a + b union[union > 0.5] = 1 sum_union = np.sum(union[:]) return sum_inter * 1.0 / sum_union preds = np.squeeze(preds) voxel_batch = np.squeeze(voxel_batch) for pred, vox_gt in zip(preds, voxel_batch): iou = compute_IoU(pred, vox_gt) IoUs.append(iou) #log_string('Iteration: {} time {}, loss: {}, depth_recon_loss: {}, sn_recon_loss {}, mask_cls_loss {}'.format(i, \ # toc-tic, loss, depth_recon_loss, sn_recon_loss, mask_cls_loss)) #test_idx += 1 log_string(tf_util.toGreen('===========Done testing===========')) toc = time.time() mean_recon_loss = np.mean(np.asarray(recon_losses)) mean_IoU = np.mean(np.asarray(IoUs)) log_string(tf_util.toRed('Test time {}s, mean recon loss: {}, mean IoU: {}.'.format(\ toc-tic, mean_recon_loss, mean_IoU))) losses = {'loss_recon_loss': mean_recon_loss, 'mean_IoU': mean_IoU} return losses
def test(ae): test_idx = 0 log_string(tf_util.toGreen('=============Testing=============')) recon_losses = [] IoUs = [] size_test = int( np.load(os.path.join(FLAGS.data_path, FLAGS.data_file + '_test.npy'))) #try: # while not ae.coord.should_stop(): #ae.sess.run(ae.assign_i_op, feed_dict={ae.set_i_to_pl: i}) from data.write_tfrecords_vox import get_models, read_bv splits = ['test'] category_name = FLAGS.category_name model_ids = get_models(category_name, splits=splits) voxel_dir = './voxels' for model_id in range(model_ids): tic = time.time() vox_name = os.path.join( voxel_dir, '{}/{}/model.binvox'.format(category_name, model_id)) vox_model = read_bv(vox_name) vox_model_zoom = ndimg.zoom(vox_model, FLAGS.vox_factor, order=0) # nearest neighbor interpolation feed_dict = {ae.is_training: False, ae.data_loader.is_training: False} ops_to_run = [ae.counter, ae.recon_loss, ae.preds] stuff = ae.sess.run(ops_to_run, feed_dict=feed_dict) step, recon_loss, preds = stuff toc = time.time() recon_losses.append(recon_loss) #log_string('Iteration: {} time {}, loss: {}, depth_recon_loss: {}, sn_recon_loss {}, mask_cls_loss {}'.format(i, \ # toc-tic, loss, depth_recon_loss, sn_recon_loss, mask_cls_loss)) #test_idx += 1 log_string(tf_util.toGreen('===========Done testing===========')) toc = time.time() mean_recon_loss = np.mean(np.asarray(recon_losses)) log_string(tf_util.toRed('Test time {}s, recon loss: {}.'.format(\ toc-tic, mean_recon_loss))) losses = {'loss_depth_recon': mean_recon_loss} return losses
def test(ae): test_idx = 0 log_string(tf_util.toGreen('=============Testing=============')) loss = [] depth_losses = [] sn_losses = [] mask_losses = [] #try: # while not ae.coord.should_stop(): #ae.sess.run(ae.assign_i_op, feed_dict={ae.set_i_to_pl: i}) for test_idx in range(1500): tic = time.time() feed_dict = {ae.is_training: False, ae.data_loader.is_training: False} ops_to_run = [ ae.opt_step, ae.merge_train, ae.counter, ae.loss_tensor, ae.depth_recon_loss, ae.sn_recon_loss, ae.mask_cls_loss ] stuff = ae.sess.run(ops_to_run, feed_dict=feed_dict) opt, summary, step, loss, depth_recon_loss, sn_recon_loss, mask_cls_loss = stuff toc = time.time() depth_losses.append(depth_recon_loss) sn_losses.append(sn_recon_loss) mask_losses.append(mask_cls_loss) #log_string('Iteration: {} time {}, loss: {}, depth_recon_loss: {}, sn_recon_loss {}, mask_cls_loss {}'.format(i, \ # toc-tic, loss, depth_recon_loss, sn_recon_loss, mask_cls_loss)) #test_idx += 1 log_string(tf_util.toGreen('===========Done testing===========')) toc = time.time() mean_depth_loss = np.mean(np.asarray(depth_losses)) mean_sn_loss = np.mean(np.asarray(sn_losses)) mean_mask_loss = np.mean(np.asarray(mask_losses)) log_string(tf_util.toRed('Test time {}s, depth recon loss: {}, sn recon loss: {}, mask cls loss:{}.'.format(\ toc-tic, mean_depth_loss, mean_sn_loss, mean_mask_loss))) losses = {'loss_depth_recon': mean_depth_loss,\ 'loss_sn_recon': mean_sn_loss,\ 'loss_mask_cls': mean_mask_loss} return losses
# os.system('rm -rf %s/*'%FLAGS.LOG_DIR) if not (FLAGS.restore): def check_delete(): if FLAGS.force_delete: return True delete_key = raw_input( tf_util.toRed( '===== %s exists. Delete? [y (or enter)/N] ' % FLAGS.LOG_DIR)) return delete_key == 'y' or delete_key == '' if check_delete(): os.system('rm -rf %s/*' % FLAGS.LOG_DIR) #os.system('rm -rf %s/*'%FLAGS.CHECKPOINT_DIR) print tf_util.toRed('Deleted.' + FLAGS.LOG_DIR) else: print tf_util.toRed('Overwrite.') else: print tf_util.toRed('To Be Restored...') #tf_util.mkdir(os.path.join(FLAGS.LOG_DIR, 'saved_images')) ##os.system('cp %s %s' % (MODEL_FILE, FLAGS.LOG_DIR)) # bkp of model def ##os.system('cp train.py %s' % (FLAGS.LOG_DIR)) # bkp of train procedure FLAGS.LOG_FOUT = open(os.path.join(FLAGS.LOG_DIR, 'log_train.txt'), 'w') FLAGS.LOG_FOUT.write(str(FLAGS) + '\n') ##prepare_plot() #log_string(tf_util.toYellow('<<<<'+FLAGS.task_name+'>>>> '+str(tf.flags.FLAGS.__flags))) ##### log writing
if __name__ == "__main__": #MODEL = importlib.import_module(FLAGS.model_file) # import network module #MODEL_FILE = os.path.join(BASE_DIR, 'models', FLAGS.model_file+'.py') FLAGS.LOG_DIR = FLAGS.LOG_DIR + '/' + FLAGS.task_name FLAGS.CHECKPOINT_DIR = os.path.join(FLAGS.CHECKPOINT_DIR, FLAGS.task_name) tf_util.mkdir(FLAGS.CHECKPOINT_DIR) if not os.path.exists(FLAGS.LOG_DIR): os.mkdir(FLAGS.LOG_DIR) print tf_util.toYellow('===== Created %s.' % FLAGS.LOG_DIR) else: # os.system('rm -rf %s/*'%FLAGS.LOG_DIR) if not (FLAGS.restore): delete_key = raw_input( tf_util.toRed('===== %s exists. Delete? [y (or enter)/N] ' % FLAGS.LOG_DIR)) if delete_key == 'y' or delete_key == "": os.system('rm -rf %s/*' % FLAGS.LOG_DIR) os.system('rm -rf %s/*' % FLAGS.CHECKPOINT_DIR) print tf_util.toRed('Deleted.' + FLAGS.LOG_DIR + FLAGS.CHECKPOINT_DIR) else: print tf_util.toRed('Overwrite.') else: print tf_util.toRed('To Be Restored...') tf_util.mkdir(os.path.join(FLAGS.LOG_DIR, 'saved_images')) #os.system('cp %s %s' % (MODEL_FILE, FLAGS.LOG_DIR)) # bkp of model def #os.system('cp train.py %s' % (FLAGS.LOG_DIR)) # bkp of train procedure FLAGS.LOG_FOUT = open(os.path.join(FLAGS.LOG_DIR, 'log_train.txt'), 'w')
def test(ae): test_idx = 0 log_string(tf_util.toGreen('=============Testing=============')) recon_losses = [] IoUs = [] size_test = int( np.load(os.path.join(FLAGS.data_path, FLAGS.data_file + '_test.npy'))) #try: # while not ae.coord.should_stop(): #ae.sess.run(ae.assign_i_op, feed_dict={ae.set_i_to_pl: i}) split = 'test' category_name = FLAGS.category_name model_ids = [] listFile = os.path.join( "./data/render_scripts/lists/{}_lists/{}_idx.txt".format( category_name, split)) with open(listFile) as file: for line in file: model = line.strip() model_ids.append(model) voxel_dir = './voxels' ii = 0 for model_id in model_ids: ii += 1 tic = time.time() vox_name = os.path.join( voxel_dir, '{}/{}/model.binvox'.format(category_name, model_id)) vox_model = read_bv(vox_name) #vox_model_zoom = ndimg.zoom(vox_model, FLAGS.vox_factor, order=0) # nearest neighbor interpolation vox_model_zoom = downsample(vox_model, int(1 / FLAGS.vox_factor)) feed_dict = { ae.is_training: False, ae.voxel_batch: vox_model_zoom[None, ..., None] } ops_to_run = [ae.counter, ae.recon_loss, ae.preds] stuff = ae.sess.run(ops_to_run, feed_dict=feed_dict) step, recon_loss, preds = stuff preds = np.squeeze(preds) def compute_IoU(a, b): a[a >= 0.5] = 1 a[a < 0.5] = 0 inter = a * b sum_inter = np.sum(inter[:]) union = a + b union[union > 0.5] = 1 sum_union = np.sum(union[:]) return sum_inter * 1.0 / sum_union toc = time.time() iou = compute_IoU(preds, vox_model_zoom) if np.isnan(iou): iou = 0 recon_losses.append(recon_loss) IoUs.append(iou) print('model_id: {}, loss: {}, IoU: {}'.format(model_id, recon_loss, iou)) #if ii > 100: # break #log_string('Iteration: {} time {}, loss: {}, depth_recon_loss: {}, sn_recon_loss {}, mask_cls_loss {}'.format(i, \ # toc-tic, loss, depth_recon_loss, sn_recon_loss, mask_cls_loss)) #test_idx += 1 log_string(tf_util.toGreen('===========Done testing===========')) toc = time.time() mean_recon_loss = np.mean(np.asarray(recon_losses)) mean_IoU = np.mean(np.asarray(IoUs)) log_string(tf_util.toRed('Test time {}s, mean recon loss: {}, mean IoU: {}.'.format(\ toc-tic, mean_recon_loss, mean_IoU))) losses = {'loss_depth_recon': mean_recon_loss} return losses
def test(ae, test_list_path): test_idx = 0 log_string(tf_util.toGreen('=============Testing=============')) loss = [] depth_losses = [] sn_losses = [] mask_losses = [] ae.loss_tensor = getattr(ae, FLAGS.loss_name) results_dir = FLAGS.save_result_path if not os.path.exists(results_dir): os.mkdir(results_dir) with open(test_list_path, 'r') as f: model_names = f.readlines() toc = time.time() plt.figure() for model_name in model_names: model_directory = os.path.join(FLAGS.test_data_dir, model_name[:-1]) model_save_dir = os.path.join(results_dir, model_name[:-1]) depth_save_dir = os.path.join(model_save_dir, 'depth') mask_save_dir = os.path.join(model_save_dir, 'mask') sn_save_dir = os.path.join(model_save_dir, 'sn') compare_save_dir = os.path.join(model_save_dir, 'compare') if not os.path.exists(model_save_dir): os.mkdir(model_save_dir) if not os.path.exists(depth_save_dir): os.mkdir(depth_save_dir) if not os.path.exists(mask_save_dir): os.mkdir(mask_save_dir) if not os.path.exists(sn_save_dir): os.mkdir(sn_save_dir) if not os.path.exists(compare_save_dir): os.mkdir(compare_save_dir) for a in azim_all: for e in elev_all: image_name = os.path.join( model_directory, 'RGB_{}_{}.png'.format(int(a), int(e))) invZ_name = os.path.join( model_directory, 'invZ_{}_{}.npy'.format(int(a), int(e))) invZ_save_npy = os.path.join( depth_save_dir, 'invZ_{}_{}.npy'.format(int(a), int(e))) invZ_save_png = os.path.join( depth_save_dir, 'invZ_{}_{}.png'.format(int(a), int(e))) mask_save_png = os.path.join( mask_save_dir, 'mask_{}_{}.png'.format(int(a), int(e))) mask_save_npy = os.path.join( mask_save_dir, 'mask_{}_{}.npy'.format(int(a), int(e))) sn_save_npy = os.path.join( sn_save_dir, 'sn_{}_{}.npy'.format(int(a), int(e))) sn_save_png = os.path.join( sn_save_dir, 'sn_{}_{}.png'.format(int(a), int(e))) rgb_single, mask_single_gt = read_png_to_uint8(image_name) invZ_single_gt = np.load(invZ_name) sn_single_gt = get_surface_from_depth(invZ_single_gt) #print mask_single.shape #print mask_single.dtype if invZ_single_gt.ndim == 2: invZ_single_gt = invZ_single_gt[None, :, :, None] if mask_single_gt.ndim == 2: mask_single_gt = mask_single_gt[None, :, :, None] if mask_single_gt.shape[2] == 3: mask_single_gt = mask_single_gt[:, :, 0] mask_single_gt = mask_single_gt[None, :, :, None] if rgb_single.ndim == 3: rgb_single = rgb_single[None, ...] if sn_single_gt.ndim == 3: sn_single_gt = sn_single_gt[None, ...] feed_dict = { ae.is_training: False, ae.rgb_batch: rgb_single, ae.invZ_batch: invZ_single_gt, ae.mask_batch: mask_single_gt, ae.sn_batch: sn_single_gt } ops_to_run = [ ae.invZ_pred, ae.mask_pred, ae.sn_pred, ae.loss_tensor, ae.depth_recon_loss, ae.sn_recon_loss, ae.mask_cls_loss, ae.global_i ] stuff = ae.sess.run(ops_to_run, feed_dict=feed_dict) invZ_pred, mask_pred, sn_pred, loss, depth_recon_loss, sn_recon_loss, mask_cls_loss, global_step = stuff depth_losses.append(depth_recon_loss) sn_losses.append(sn_recon_loss) mask_losses.append(mask_cls_loss) print global_step invZ_pred = np.squeeze(np.asarray(invZ_pred, dtype=np.float32), axis=[0, -1]) mask_pred = np.squeeze(np.asarray(mask_pred, dtype=np.float32), axis=[0, -1]) sn_pred = np.squeeze(np.asarray(sn_pred, dtype=np.float32), axis=0) np.save(invZ_save_npy, invZ_pred) sm.imsave(invZ_save_png, invZ_pred) np.save(mask_save_npy, mask_pred) sm.imsave(mask_save_png, mask_pred) np.save(sn_save_npy, sn_pred) sm.imsave(sn_save_png, sn_pred) invZ_single = np.squeeze(invZ_single_gt) Z_single = process_invZ(invZ_single) Z_pred = process_invZ(invZ_pred) plt.subplot(121) plt.imshow(Z_single, cmap='gray', vmin=0, vmax=1) plt.subplot(122) plt.imshow(Z_pred, cmap='gray', vmin=0, vmax=1) #plt.show() compare_a_b(np.squeeze(invZ_single_gt), invZ_pred, compare_save_dir, a, e, mode='invZ') compare_a_b(np.squeeze(mask_single_gt), mask_pred, compare_save_dir, a, e, mode='mask') compare_a_b(np.squeeze(sn_single_gt), sn_pred, compare_save_dir, a, e, mode='sn') tic = time.time() mean_depth_loss = np.mean(np.asarray(depth_losses)) mean_sn_loss = np.mean(np.asarray(sn_losses)) mean_mask_loss = np.mean(np.asarray(mask_losses)) log_string(tf_util.toRed('Test time {}s, depth recon loss: {}, sn recon loss: {}, mask cls loss:{}.'.format(\ toc-tic, mean_depth_loss, mean_sn_loss, mean_mask_loss)))
# R0 = tw2R(tws0[i]) # R = tw2R(tws1[i]) # delta_R = np.dot(R, R0.T) # delta_degree = np.rad2deg(np.linalg.norm(R2tw(delta_R))) # error_list.append(delta_degree) # return error_list if __name__ == "__main__": #MODEL = importlib.import_module(FLAGS.model_file) # import network module #MODEL_FILE = os.path.join(BASE_DIR, 'models', FLAGS.model_file+'.py') FLAGS.LOG_DIR = FLAGS.LOG_DIR + '/' + FLAGS.task_name #FLAGS.CHECKPOINT_DIR = os.path.join(FLAGS.CHECKPOINT_DIR, FLAGS.task_name) #tf_util.mkdir(FLAGS.CHECKPOINT_DIR) if not os.path.exists(FLAGS.LOG_DIR): print tf_util.toRed('===== %s not exists!.' % FLAGS.LOG_DIR) sys.exit() #else: # # os.system('rm -rf %s/*'%FLAGS.LOG_DIR) # if not(FLAGS.restore): # # def check_delete(): # if FLAGS.force_delete: # return True # delete_key = raw_input(tf_util.toRed('===== %s exists. Delete? [y (or enter)/N] '%FLAGS.LOG_DIR)) # return delete_key == 'y' or delete_key == '' # # if check_delete(): # os.system('rm -rf %s/*'%FLAGS.LOG_DIR) # #os.system('rm -rf %s/*'%FLAGS.CHECKPOINT_DIR) # print tf_util.toRed('Deleted.'+FLAGS.LOG_DIR)