A General Formulation for Safely Exploiting Weakly Supervised Data