Learning representations of visual semantics for out-of-distribution generalization