def main(): # Save prediction into a file named 'prediction.obj' or the given argument pred_file_name = sys.argv[1] if len(sys.argv) > 1 else 'prediction.obj' num_imgs = int(sys.argv[2]) if len(sys.argv) > 2 else 1 img_file = sys.argv[3] # load images demo_imgs = load_demo_images(num_imgs, img_file) # Define a network and a solver. Solver provides a wrapper for the test function. my_net = My_ResidualGRUNet(batch=1) # instantiate a network my_net.load('my_3DR2N2/weights.npy') # load downloaded weights solver = Solver(my_net) # instantiate a solver # Run the network voxel_prediction, _ = solver.test_output(demo_imgs) # Save the prediction to a mesh file). voxel2obj(pred_file_name, voxel_prediction[0, :, 1, :, :] > 0.4) if shutil.which('meshlab') is not None: call(['meshlab', pred_file_name]) else: print( 'Meshlab not found: please use visualization of your choice to view %s' % pred_file_name)
def main(): '''Main demo function''' # Save prediction into a file named 'prediction.obj' or the given argument pred_file_name = sys.argv[1] if len(sys.argv) > 1 else 'prediction.obj' # load images demo_imgs = load_demo_images() # Download and load pretrained weights download_model(DEFAULT_WEIGHTS) # Use the default network model NetClass = load_model('ResidualGRUNet') # Define a network and a solver. Solver provides a wrapper for the test function. net = NetClass(compute_grad=False) # instantiate a network net.load(DEFAULT_WEIGHTS) # load downloaded weights solver = Solver(net) # instantiate a solver # Run the network voxel_prediction, _ = solver.test_output(demo_imgs) # Save the prediction to an OBJ file (mesh file). voxel2obj(pred_file_name, voxel_prediction[0, :, 1, :, :] > cfg.TEST.VOXEL_THRESH) # Use meshlab or other mesh viewers to visualize the prediction. # For Ubuntu>=14.04, you can install meshlab using # `sudo apt-get install meshlab` if cmd_exists('meshlab'): call(['meshlab', pred_file_name]) else: print( 'Meshlab not found: please use visualization of your choice to view %s' % pred_file_name)
def pred_show_3D_model(imgs_dir, solver): assert (os.path.isdir(imgs_dir)) for taken_time in os.listdir(imgs_dir): taken_time = os.path.join(imgs_dir, taken_time) if os.path.isdir(taken_time): imgs = load_imgs(taken_time) vox_pred, _ = solver.test_output(imgs) #save the voxel prediction to an OBJ file obj_fn = os.path.join(taken_time, "prediction.obj") vox_pred_thresholded = vox_pred[0, :, 1, :, :] > cfg.TEST.VOXEL_THRESH #save obj file voxel2obj(obj_fn, vox_pred_thresholded)
def image_to_model(name): if request.method == 'POST': imagedata = [] print(len(list(request.files.values()))) print(list(request.files.keys())) for imagefile in request.files.values(): imagedata.append(Image.open(BytesIO(imagefile.read()))) if len(imagedata) == 0: return jsonify({'message': 'No Image Found.'}) voxel_prediction, _ = SOLVER.test_output(process_image(imagedata)) voxel_obj_name = 'img/' + name + '.obj' voxel2obj(voxel_obj_name, voxel_prediction[0, :, 1, :, :] > cfg.TEST.VOXEL_THRESH) response = make_response(send_from_directory(ROOT_PATH,voxel_obj_name)) response.headers["Content-Disposition"] = "attachment; filename=test.obj" return response
def demo(args): ''' Evaluate the network ''' # Make a network and load weights NetworkClass = load_model(cfg.CONST.NETWORK_CLASS) print('Network definition: \n') print(inspect.getsource(NetworkClass.network_definition)) net = NetworkClass(compute_grad=False) net.load(cfg.CONST.WEIGHTS) solver = Solver(net) # set up testing data process. We make only one prefetching process. The # process will return one batch at a time. queue = Queue(cfg.QUEUE_SIZE) data_pair = category_model_id_pair(dataset_portion=cfg.TEST.DATASET_PORTION) processes = make_data_processes(queue, data_pair, 1, repeat=False, train=False) num_data = len(processes[0].data_paths) num_batch = int(num_data / args.batch_size) # Get all test data batch_idx = 0 for batch_img, batch_voxel in get_while_running(processes[0], queue): if batch_idx == num_batch: break pred, loss, activations = solver.test_output(batch_img, batch_voxel) if (batch_idx < args.exportNum): # Save the prediction to an OBJ file (mesh file). print('saving {}/{}'.format(batch_idx, args.exportNum - 1)) voxel2obj('out/prediction_{}b_{}.obj'.format(args.batch_size, batch_idx), pred[0, :, 1, :, :] > cfg.TEST.VOXEL_THRESH) else: break batch_idx += 1 if args.file: # Use meshlab or other mesh viewers to visualize the prediction. # For Ubuntu>=14.04, you can install meshlab using # `sudo apt-get install meshlab` if cmd_exists('meshlab'): call(['meshlab', 'obj/{}.obj'.format(args.file)]) else: print('Meshlab not found: please use visualization of your choice to view %s' % args.file)
def render(): # Save prediction into a file named 'prediction.obj' or the given argument pred_file_name = sys.argv[1] if len( sys.argv) > 1 else 'voxel/prediction.obj' demo_imgs, files = load_demo_images() NetClass = load_model('ResidualGRUNet') net = NetClass(compute_grad=False) # instantiate a network net.load(DEFAULT_WEIGHTS) # load downloaded weights solver = Solver(net) # instantiate a solver voxel_prediction, _ = solver.test_output(demo_imgs) # Save the prediction to an OBJ file (mesh file). voxel2obj(pred_file_name, voxel_prediction[0, :, 1, :, :] > cfg.TEST.VOXEL_THRESH) return files, pred_file_name
def main(): '''Main demo function''' # Save prediction into a file named 'prediction.obj' or the given argument global pred_file_name, demo_imgs if not cfg.TEST.MULTITEST or pred_file_name == '': pred_file_name = sys.argv[1] if len(sys.argv) > 1 else 'prediction.obj' # Download and load pretrained weights download_model(DEFAULT_WEIGHTS) # Use the default network model NetClass = load_model(MODEL_NAME) # print(NetClass) # Define a network and a solver. Solver provides a wrapper for the test function. net = NetClass() # instantiate a network solver = Solver(net) # instantiate a solver solver.load(DEFAULT_WEIGHTS) # load pretrained weights # solver.graph_view(demo_imgs) # return # Run the network voxel_prediction, _ = solver.test_output(demo_imgs) # Save the prediction to an OBJ file (mesh file). # (self.batch_size, 2, n_vox, n_vox, n_vox) # print(type(voxel_prediction[0, :, 1, :, :].data.numpy())) # print(cfg.TEST.VOXEL_THRESH) voxel2obj(pred_file_name, voxel_prediction[0, 1, :, :, :].data.numpy() > cfg.TEST.VOXEL_THRESH) # modified # Use meshlab or other mesh viewers to visualize the prediction. # For Ubuntu>=14.04, you can install meshlab using # `sudo apt-get install meshlab` print('writing voxel to %s' % (pred_file_name)) if cfg.TEST.CALL_MESHLAB: #需要打开meshlab if cmd_exists('meshlab'): call(['meshlab', pred_file_name]) else: print( 'Meshlab not found: please use visualization of your choice to view %s' % pred_file_name)
def main(): '''Main demo function''' # load images input_folder_name = sys.argv[1] if len(sys.argv) > 1 else 'in_demo' imgs = load_input_images(input_folder_name) # get output file name from command line argument output_file_name = sys.argv[2] if len(sys.argv) > 2 else 'demo.obj' # use the default network model NetClass = load_model('ResidualGRUNet') net = NetClass(compute_grad=False) net.load('output/ResidualGRUNet/default_model/weights.npy') solver = Solver(net) # run the network voxel_prediction, _ = solver.test_output(imgs) # save the prediction to an OBJ (mesh) file voxel2obj(output_file_name, voxel_prediction[0, :, 1, :, :] > cfg.TEST.VOXEL_THRESH)
def main(): '''Main demo function''' # Save prediction into a file named 'prediction.obj' or the given argument pred_file_name = sys.argv[1] if len(sys.argv) > 1 else 'prediction.obj' # load images demo_imgs = load_demo_images() # Use the default network model NetClass = load_model('ResidualGRUNet') # Define a network and a solver. Solver provides a wrapper for the test function. net = NetClass() # instantiate a network if torch.cuda.is_available(): net.cuda() net.eval() solver = Solver(net) # instantiate a solver solver.load(DEFAULT_WEIGHTS) # Run the network voxel_prediction, _ = solver.test_output(demo_imgs) voxel_prediction = voxel_prediction.detach().cpu().numpy() # Save the prediction to an OBJ file (mesh file). voxel2obj(pred_file_name, voxel_prediction[0, 1] > cfg.TEST.VOXEL_THRESH) # Use meshlab or other mesh viewers to visualize the prediction. # For Ubuntu>=14.04, you can install meshlab using # `sudo apt-get install meshlab` if cmd_exists('meshlab'): call(['meshlab', pred_file_name]) else: print( 'Meshlab not found: please use visualization of your choice to view %s' % pred_file_name)
def test_net(): ''' Evaluate the network ''' # Make result directory and the result file. result_dir = os.path.join(cfg.DIR.OUT_PATH, cfg.TEST.EXP_NAME) if not os.path.exists(result_dir): os.makedirs(result_dir) result_fn = os.path.join(result_dir, 'result.mat') print("Exp file will be written to: " + result_fn) # Make a network and load weights NetworkClass = load_model(cfg.CONST.NETWORK_CLASS) print('Network definition: \n') print(inspect.getsource(NetworkClass.network_definition)) net = NetworkClass(compute_grad=False) net.load(cfg.CONST.WEIGHTS) solver = Solver(net) # set constants batch_size = cfg.CONST.BATCH_SIZE # set up testing data process. We make only one prefetching process. The # process will return one batch at a time. queue = Queue(cfg.QUEUE_SIZE) data_pair = category_model_id_pair(dataset_portion=cfg.TEST.DATASET_PORTION) processes = make_data_processes(queue, data_pair, 1, repeat=False, train=False) num_data = len(processes[0].data_paths) num_batch = int(num_data / batch_size) # prepare result container results = {'cost': np.zeros(num_batch), 'mAP': np.zeros((num_batch, batch_size))} # Save results for various thresholds for thresh in cfg.TEST.VOXEL_THRESH: results[str(thresh)] = np.zeros((num_batch, batch_size, 5)) # Get all test data batch_idx = 0 for batch_img, batch_voxel in get_while_running(processes[0], queue): if batch_idx == num_batch: break pred, loss, activations = solver.test_output(batch_img, batch_voxel) if(batch_idx < cfg.TEST.EXPORT_NUM): # Save the prediction to an OBJ file (mesh file). voxel2obj('out/prediction{}.obj'.format(batch_idx), pred[0, :, 1, :, :] > cfg.TEST.VOXEL_THRESH) else: break for j in range(batch_size): # Save IoU per thresh for i, thresh in enumerate(cfg.TEST.VOXEL_THRESH): r = evaluate_voxel_prediction(pred[j, ...], batch_voxel[j, ...], thresh) results[str(thresh)][batch_idx, j, :] = r # Compute AP precision = sklearn.metrics.average_precision_score( batch_voxel[j, :, 1].flatten(), pred[j, :, 1].flatten()) results['mAP'][batch_idx, j] = precision # record result for the batch results['cost'][batch_idx] = float(loss) print('%d/%d, costs: %f, mAP: %f' % (batch_idx, num_batch, loss, np.mean(results['mAP'][batch_idx]))) batch_idx += 1 print('Total loss: %f' % np.mean(results['cost'])) print('Total mAP: %f' % np.mean(results['mAP'])) sio.savemat(result_fn, results)