|
| 1 | +# Multimodal Topic Modelling ***(BETA)*** |
| 2 | + |
| 3 | +!!! note |
| 4 | + Multimodal modeling is still a BETA feature in Turftopic, and it is likely that we will add more features and change the interface in the near future. |
| 5 | + |
| 6 | +Some corpora spread across multiple modalities. |
| 7 | +A good example of this would be news articles with images attached. |
| 8 | +Turftopic now supports multimodal modelling with a number of models. |
| 9 | + |
| 10 | + |
| 11 | +## Multimodal Encoders |
| 12 | + |
| 13 | +In order for images to be usable in Turftopic, you will need an embedding model that can both encode texts and images. |
| 14 | +You can both use models that are supported in SentenceTransformers, or those that support the MTEB multimodal encoder interface. |
| 15 | + |
| 16 | + |
| 17 | +!!! quote "Use a multimodal encoder model " |
| 18 | + === "SentenceTransformers" |
| 19 | + |
| 20 | + ```python |
| 21 | + from turftopic import KeyNMF |
| 22 | + |
| 23 | + multimodal_keynmf = KeyNMF(10, encoder="clip-ViT-B-32") |
| 24 | + ``` |
| 25 | + |
| 26 | + === "MTEB/MIEB" |
| 27 | + !!! tip |
| 28 | + You can find current state-of-the-art embedding models and their capabilities on the [Massive Image Embedding Benchmark leaderboard](http://mteb-leaderboard.hf.space/?benchmark_name=MIEB%28Multilingual%29). |
| 29 | + |
| 30 | + ```bash |
| 31 | + pip install "mteb<2.0.0" |
| 32 | + ``` |
| 33 | + |
| 34 | + ```python |
| 35 | + from turftopic import KeyNMF |
| 36 | + import mteb |
| 37 | + |
| 38 | + encoder = mteb.get_model("kakaobrain/align-base") |
| 39 | + |
| 40 | + multimodal_keynmf = KeyNMF(10, encoder="clip-ViT-B-32") |
| 41 | + ``` |
| 42 | + |
| 43 | +## Corpus Structure |
| 44 | + |
| 45 | +Currently all documents **have to have** an image attached to them, and only one image. |
| 46 | +This is a limitation, and we will address it in the future. |
| 47 | +Images can both be represented as file paths or `PIL.Image` objects. |
| 48 | + |
| 49 | +```python |
| 50 | +from PIL import Image |
| 51 | + |
| 52 | +images: list[Image] = [Image.open("file_path/something.jpeg"), ...] |
| 53 | +texts: list[str] = [...] |
| 54 | + |
| 55 | +len(images) == len(texts) |
| 56 | +``` |
| 57 | + |
| 58 | +## Basic Usage |
| 59 | + |
| 60 | +All multimodal models have a `fit_multimodal()`/`fit_transform_multimodal()` method, |
| 61 | +that you can use to discover topics in multimodal corpora. |
| 62 | + |
| 63 | +!!! quote "Fit a multimodal model on a corpus" |
| 64 | + === "KeyNMF" |
| 65 | + |
| 66 | + ```python |
| 67 | + from turftopic import KeyNMF |
| 68 | + |
| 69 | + model = KeyNMF(12, encoder="clip-ViT-B-32") |
| 70 | + model.fit_multimodal(texts, images=images) |
| 71 | + model.plot_topics_with_images() |
| 72 | + ``` |
| 73 | + |
| 74 | + === "SemanticSignalSeparation" |
| 75 | + |
| 76 | + ```python |
| 77 | + from turftopic import SemanticSignalSeparation |
| 78 | + |
| 79 | + model = SemanticSignalSeparation(12, encoder="clip-ViT-B-32") |
| 80 | + model.fit_multimodal(texts, images=images) |
| 81 | + model.plot_topics_with_images() |
| 82 | + ``` |
| 83 | + |
| 84 | + === "Clustering Models" |
| 85 | + |
| 86 | + ```python |
| 87 | + from turftopic import ClusteringTopicModel |
| 88 | + |
| 89 | + # BERTopic-style |
| 90 | + model = ClusteringTopicModel(encoder="clip-ViT-B-32", feature_importance="c-tf-idf") |
| 91 | + # Top2Vec-style |
| 92 | + model = ClusteringTopicModel(encoder="clip-ViT-B-32", feature_importance="centroid") |
| 93 | + model.fit_multimodal(texts, images=images) |
| 94 | + model.plot_topics_with_images() |
| 95 | + ``` |
| 96 | + |
| 97 | + === "GMM" |
| 98 | + |
| 99 | + ```python |
| 100 | + from turftopic import GMM |
| 101 | + |
| 102 | + model = GMM(12, encoder="clip-ViT-B-32") |
| 103 | + model.fit_multimodal(texts, images=images) |
| 104 | + model.plot_topics_with_images() |
| 105 | + ``` |
| 106 | + |
| 107 | + === "AutoEncodingTopicModel" |
| 108 | + |
| 109 | + ```python |
| 110 | + from turftopic import AutoEncodingTopicModel |
| 111 | + |
| 112 | + # CombinedTM |
| 113 | + model = AutoEncodingTopicModel(12, combined=True, encoder="clip-ViT-B-32") |
| 114 | + # ZeroShotTM |
| 115 | + model = AutoEncodingTopicModel(12, combined=False, encoder="clip-ViT-B-32") |
| 116 | + model.fit_multimodal(texts, images=images) |
| 117 | + model.plot_topics_with_images() |
| 118 | + ``` |
| 119 | + |
| 120 | +<iframe src="../images/multimodal.html", title="Multimodal KeyNMF on IKEA catalogue", style="height:350px;width:100%;padding:0px;border:none;"></iframe> |
| 121 | + |
| 122 | +## API reference |
| 123 | + |
| 124 | +::: turftopic.multimodal.MultimodalModel |
| 125 | + |
| 126 | +::: turftopic.encoders.multimodal.MultimodalEncoder |
| 127 | + |
| 128 | + |
| 129 | + |
0 commit comments