1 # L_ALL: General ranking loss
2 diff_all = pos_dist - neg_dist + margin # margin = 2.0
3 loss_all = ReLU ( diff_all ) . mean ( )
4
5 # L_HARD: Hardest negative focus
6 min_neg_dist = neg_dist . min ( dim = 1 )
7 diff_hard = pos_dist - min_neg_dist + margin
8 loss_hard = ReLU ( diff_hard ) . mean ( )
9
10 total_loss = loss_all + loss_hard
┌─────────────────────────────────────────────────────────────┐
│ OutfitTransformerCIR │
├─────────────────────────────────────────────────────────────┤
│ │
│ ┌──────────────┐ ┌──────────────┐ │
│ │ ResNet-18 │ │ LaBSE │ │
│ │ (Frozen) │ │ (Frozen) │ │
│ │ 512-dim │ │ 768-dim │ │
│ └──────┬───────┘ └──────┬───────┘ │
│ │ │ │
│ ┌──────▼───────┐ ┌──────▼───────┐ │
│ │ Visual Proj │ │ Text Proj │ ← Trained │
│ │ 512 → 64 │ │ 768 → 64 │ │
│ └──────┬───────┘ └──────┬───────┘ │
│ │ │ │
│ └────────┬──────────┘ │
│ │ │
│ ┌──────▼──────┐ │
│ │ Concat │ │
│ │ 64+64 = 128 │ │
│ └──────┬──────┘ │
│ │ │
│ ┌─────────────▼─────────────┐ │
│ │ [QUERY] + Item Embeddings │ │
│ │ (Learnable Token) │ │
│ └─────────────┬─────────────┘ │
│ │ │
│ ┌─────────────▼─────────────┐ │
│ │ Transformer Encoder │ │
│ │ 6 layers, 16 heads │ │
│ │ d_model=128, ff=512 │ │
│ └─────────────┬─────────────┘ │
│ │ │
│ ┌─────────────▼─────────────┐ │
│ │ Output Projection │ │
│ │ + LayerNorm + L2 Norm │ │
│ └─────────────┬─────────────┘ │
│ │ │
│ ┌──────▼──────┐ │
│ │ 128-dim │ │
│ │ Predicted │ │
│ │ Embedding │ │
│ └─────────────┘ │
│ │
└─────────────────────────────────────────────────────────────┘
1 import torch
2 from model import OutfitTransformerCIR
3
4 # Load model
5 model = OutfitTransformerCIR ( embedding_dim = 128 , nhead = 16 , num_layers = 6 )
6 model . load_state_dict ( torch . load ( "pytorch_model.bin" , map_location = "cpu" ) )
7 model . eval ( )
1 # Assume you have pre-extracted features:
2 # context_images: (1, num_items, 512) - ResNet-18 features
3 # context_texts: (1, num_items, 768) - LaBSE embeddings
4
5 with torch . no_grad ( ) :
6 # Predict missing item embedding
7 predicted_embedding = model ( context_images , context_texts )
8 # predicted_embedding: (1, 128)
9
10 # Use cosine similarity to find closest items in your database
11 similarities = torch . cosine_similarity ( predicted_embedding , item_database )
12 top_matches = similarities . argsort ( descending = True ) [ : 10 ]
1 from torchvision import models , transforms
2 from transformers import AutoTokenizer , AutoModel
3 from PIL import Image
4 import torch . nn as nn
5
6 # Image encoder (ResNet-18)
7 resnet = models . resnet18 ( weights = models . ResNet18_Weights . DEFAULT )
8 resnet = nn . Sequential ( * list ( resnet . children ( ) ) [ : - 1 ] )
9 resnet . eval ( )
10
11 preprocess = transforms . Compose ( [
12 transforms . Resize ( ( 224 , 224 ) ) ,
13 transforms . ToTensor ( ) ,
14 transforms . Normalize ( mean = [ 0.485 , 0.456 , 0.406 ] , std = [ 0.229 , 0.224 , 0.225 ] ) ,
15 ] )
16
17 # Text encoder (LaBSE)
18 tokenizer = AutoTokenizer . from_pretrained ( "sentence-transformers/LaBSE" )
19 labse = AutoModel . from_pretrained ( "sentence-transformers/LaBSE" )
20 labse . eval ( )
21
22 def extract_features ( image_path , text_description ) :
23 # Image: 512-dim
24 image = Image . open ( image_path ) . convert ( 'RGB' )
25 img_tensor = preprocess ( image ) . unsqueeze ( 0 )
26 with torch . no_grad ( ) :
27 img_features = resnet ( img_tensor ) . flatten ( 1 ) # (1, 512)
28
29 # Text: 768-dim
30 inputs = tokenizer ( text_description , return_tensors = "pt" , padding = True , truncation = True )
31 with torch . no_grad ( ) :
32 txt_features = labse ( ** inputs ) . pooler_output # (1, 768)
33
34 return img_features , txt_features
1 @misc{outfit-cir-transformer,
2 author = {Kuyumcu, Furkan},
3 title = {Outfit Transformer CIR: Multilingual Complementary Item Retrieval},
4 year = {2026},
5 publisher = {Hugging Face},
6 url = {https://huggingface.co/fkuyumcu/outfit-cir-transformer}
7 }
1 @inproceedings{sarkar2022outfitbert,
2 title={OutfitTransformer: Learning Outfit Representations for Fashion Recommendation},
3 author={Sarkar, Rohan and others},
4 booktitle={CVPR Workshop on Computer Vision for Fashion, Art, and Design},
5 year={2022}
6 }