Graph-based Inference with Constraints for Object Detection and Segmentation