Roger Federer came from a set down to defeat Joao Sousa 6-7 6-4 6-2 and seal his quarter-final place at the Gerry Weber Open in Halle.