Learning Globally-Consistent Local Distance Functions for Shape-Based Image Retrieval and Classification