MRF training and inference via dual decomposition