Package: fashion-clip
FashionCLIPQuick StartNameLinkFashionCLIP Feature Extraction and ClassificationTutorial -  FashionCLIP Evaluation with RecListUPDATE (10/03/23): We have updated the model! We found thatlaion/CLIP-ViT-B-32-laion2B-s34B-b79Kcheckpoint (thanksBin!) worked better than original OpenAI CLIP on Fashion. We thus fine-tune a newer (and better!) version of FashionCLIP (henceforth FashionCLIP 2.0), while keeping the architecture the same. We postulate that the perofrmance gains afforded bylaion/CLIP-ViT-B-32-laion2B-s34B-b79Kare due to the increased training data (5x OpenAI CLIP data). Ourthesis, however, remains the same -- fine-tuninglaion/CLIPon our fashion dataset improved zero-shot perofrmance across our benchmarks. See the below table comparing weighted macro F1 score across models.
`ModelFMNISTKAGLDEEPOpenAI CLIP0.660.630.45FashionCLIP0.740.670.48Laion CLIP0.780.710.58FashionCLIP 2.00.830.730.62We are now on Hugging Face! The model is availablehere.We are now onNature Scientific Reports!Citation@Article{Chia2022,
    title="Contrastive language and vision learning of general fashion concepts",
    author="Chia, Patrick John
            and Attanasio, Giuseppe
            and Bianchi, Federico
            and Terragni, Silvia
            and Magalh{\~a}es, Ana Rita
            and Goncalves, Diogo
            and Greco, Ciro
            and Tagliabue, Jacopo",
    journal="Scientific Reports",
    year="2022",
    month="Nov",
    day="08",
    volume="12",
    number="1",
    pages="18958",
    abstract="The steady rise of online shopping goes hand in hand with the development of increasingly complex ML and NLP models. While most use cases are cast as specialized supervised learning problems, we argue that practitioners would greatly benefit from general and transferable representations of products. In this work, we build on recent developments in contrastive learning to train FashionCLIP, a CLIP-like model adapted for the fashion industry. We demonstrate the effectiveness of the representations learned by FashionCLIP with extensive tests across a variety of tasks, datasets and generalization probes. We argue that adaptations of large pre-trained models such as CLIP offer new perspectives in terms of scalability and sustainability for certain types of players in the industry. Finally, we detail the costs and environmental impact of training, and release the model weights and code as open source contribution to the community.",
    issn="2045-2322",
    doi="10.1038/s41598-022-23052-9",
    url="https://doi.org/10.1038/s41598-022-23052-9"
}InformationWe are awaiting the official release of the Farfetch dataset, upon which fine-tuned model weights,
pre-processed image and text vectors will be made public. In the meanwhile, we currently use theHugging Faceimplementation ofCLIPand can use the model weights
fromOpenAIby following the standard hugginface
naming convention (i.e.fclip = FashionCLIP('<username>/<repo_name>', ... )). We also support private
repositories (i.e.fclip = FashionCLIP('<username>/<repo_name>', auth_token=<AUTH_TOKEN>, ... )).See below for further details!OverviewFashionCLIPis a CLIP-like model fine-tuned for the fashion industry. We fine tuneCLIP(Radford et al., 2021on over 700K
<image, text> pairs from the Farfetch dataset[^1].We evaluate FashionCLIP by applying it to open problems in industry such as retrieval, classification
and fashion parsing. Our results demonstrate that fine-tuning helps capture domain-specific concepts
and generalizes them in zero-shot scenarios. We also supplement quantitative tests with qualitative analyses,
and offer preliminary insights into how concepts grounded in a visual space unlocks linguistic generalization.
Please see ourpaperfor more details.In this repository, you will find an API for interacting withFashionCLIPand an interactive demo built usingstreamlit(coming soon!)
which showcases the capabilities ofFashionCLIP.[^1]: Pending official release.API & DemoQuick How ToNeed a quick way to generate embeddings? do you want to test retrieval performance?First of all, you should be able to quickly install this using pip.$ pip install fashion-clipIf you have lists of texts and image paths, it is very easy to generate embeddings:fromfashion_clip.fashion_clipimportFashionCLIPfclip=FashionCLIP('fashion-clip')# we create image embeddings and text embeddingsimage_embeddings=fclip.encode_images(images,batch_size=32)text_embeddings=fclip.encode_text(texts,batch_size=32)# we normalize the embeddings to unit norm (so that we can use dot product instead of cosine similarity to do comparisons)image_embeddings=image_embeddings/np.linalg.norm(image_embeddings,ord=2,axis=-1,keepdims=True)text_embeddings=text_embeddings/np.linalg.norm(text_embeddings,ord=2,axis=-1,keepdims=True)Use ourcolabnotebook to see more functionalities.HF APIfromPILimportImageimportrequestsfromtransformersimportCLIPProcessor,CLIPModelmodel=CLIPModel.from_pretrained("patrickjohncyh/fashion-clip")processor=CLIPProcessor.from_pretrained("patrickjohncyh/fashion-clip")image=Image.open("images/image1.jpg")inputs=processor(text=["a photo of a red shoe","a photo of a black shoe"],images=image,return_tensors="pt",padding=True)outputs=model(**inputs)logits_per_image=outputs.logits_per_image# this is the image-text similarity scoreprobs=logits_per_image.softmax(dim=1)print(probs)image.resize((224,224))Additional Internal FashionCLIP APIInstallationFrom project root, install thefashion-clippackage locally with$ pip install -e .There are two main abstractions to facilitate easy use ofFashionCLIP.First, theFCLIPDatasetclass which encapsulates information related to a given catalog
and exposes information critical forFashionCLIP. Additionally, it provides helper functions
for quick exploration and visualization of data. The main initialization parameters arename: str -> Name of dataset
image_source_path: str -> absolute path to images (can be local or s3) 
image_source_type: str -> type of source (i.e. local or s3)
catalog: List[dict] = None -> list of dicts containing at miniumum the keys ['id', 'image', 'caption']For ease of use, the API also provides access to the dataset (once it is officialy released), used in the paper
for trainingFahionCLIP, by simply specifying the corresponding catalog name.Pre-Included Datasetfrom fashion_clip import FCLIPDataset
dataset = FCLIPDataset(name='FF', 
                       image_source_path='path/to/images', 
                       image_source_type='local')Custom datasetfrom fashion_clip import FCLIPDataset
my_catalog = [{'id': 1, 'image': 'x.jpg', 'caption': 'image x'}]
dataset = FCLIPDataset(name='my_dataset', 
                       image_source_path='path/to/images', 
                       image_source_type='local',
                       catalog=my_catalog)The second abstraction is theFashionCLIPclass, which takes in a Hugging Face CLIP model name and
anFCLIPDataset, and provides convenient functions to perform tasks such as multi-modal retrieval,
zero-shot classification and localization. The initialization parameters forFashionCLIPare as follows:model_name: str -> Name of model OR path to local model
dataset: FCLIPDataset -> Dataset, 
normalize: bool -> option to convert embeddings to unit norm  
approx: bool -> option to use approximate nearest neighborsSimilar to theFCLIPDatasetabstraction, we have included a pre-trainedFashionCLIPmodel from the paper, hostedhere. If an unknown dataset and model combination is received,
the image and caption vectors will be generated upon object instantiation, otherwise pre-computed vectors/embeddings will
be pulled from S3.from fashion_clip import FCLIPDataset, FashionCLIP
dataset = FCLIPDataset(name='FF', 
                       image_source_path='path/to/images', 
                       image_source_type='local')
fclip = FashionCLIP('fasihon-clip', ff_dataset)For further details on how to use the package, refer to the accompanying notebook!