This model is a multimodal encoder specifically designed to generate rich, dense feature representations (embeddings) of individual comic book panels. It serves as "Stage 3" of the
Comic Analysis Framework v2.0.
By combining visual details, extracted text (dialogue/narration), and compositional metadata (bounding box coordinates), it generates a single 512-dimensional vector per panel. These embeddings are highly optimized for downstream sequential narrative modeling (Stage 4) and comic retrieval tasks.
The model was trained on a dataset of approximately
1 million comic pages, filtered specifically for narrative/story content using
CoSMo (Comic Stream Modeling).
The encoder was trained from scratch (with frozen base backbones) using three simultaneous objectives:
You can use this model to extract 512-d embeddings from comic panels. The codebase required to run this model is available in the
Comic Analysis GitHub Repository under
src/version2/stage3_panel_features_framework.py.
1import torch
2from PIL import Image
3import torchvision.transforms as T
4from transformers import AutoTokenizer
5# Requires cloning the GitHub repo for the framework class
6from stage3_panel_features_framework import PanelFeatureExtractor
7
8device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
9
10# 1. Initialize Model
11model = PanelFeatureExtractor(
12 visual_backbone='both',
13 visual_fusion='attention',
14 feature_dim=512
15).to(device)
16
17# Load weights from Hugging Face
18state_dict = torch.hub.load_state_dict_from_url(
19 "https://huggingface.co/RichardScottOZ/comic-panel-encoder-v1/resolve/main/best_model.pt",
20 map_location=device
21)
22model.load_state_dict(state_dict)
23model.eval()
24
25# 2. Prepare Inputs
26# Image
27image = Image.open('sample_panel.jpg').convert('RGB')
28transform = T.Compose([
29 T.Resize((224, 224)),
30 T.ToTensor(),
31 T.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
32])
33img_tensor = transform(image).unsqueeze(0).unsqueeze(0).to(device) # (B=1, N=1, C, H, W)
34
35# Text
36tokenizer = AutoTokenizer.from_pretrained("sentence-transformers/all-MiniLM-L6-v2")
37text_enc = tokenizer(["Batman punches the Joker"], return_tensors='pt', padding=True)
38input_ids = text_enc['input_ids'].unsqueeze(0).to(device)
39attn_mask = text_enc['attention_mask'].unsqueeze(0).to(device)
40
41# Composition (e.g., Aspect Ratio, Area, Center X, Center Y)
42comp_feats = torch.zeros(1, 1, 7).to(device)
43
44# Modality Mask [Vision, Text, Comp]
45modality_mask = torch.tensor([[[1.0, 1.0, 1.0]]]).to(device)
46
47batch = {
48 'images': img_tensor,
49 'input_ids': input_ids,
50 'attention_mask': attn_mask,
51 'comp_feats': comp_feats,
52 'modality_mask': modality_mask
53}
54
55# 3. Generate Embedding
56with torch.no_grad():
57 panel_embedding = model(batch)
58
59print(f"Embedding shape: {panel_embedding.shape}") # Output: torch.Size([1, 512])
If you use this model or the associated framework, please link back to the
Comic Analysis GitHub Repository.