This is a
BERTopic model.
BERTopic is a flexible and modular topic modeling framework that allows for the generation of easily interpretable topics from large datasets.
1from bertopic import BERTopic
2topic_model = BERTopic.load("D0men1c0/ISSR_Visual_Model")
3
4topic_model.get_topic_info()
1val_labels = [...] # list of caption
2val_images = [...] # list of images
3
4topic, _ = topic_model.transform(val_labels, images=val_images)
5all_topic_info = [topic_model.get_topic_info(t) for t in topic]
6all_prediction_info = pd.concat(all_topic_info, ignore_index=True)
7
8# Visualize predictions:
9sample_images = 100
10n_images = min(sample_images, len(val_images))
11n_cols = 4
12n_rows = math.ceil(n_images / n_cols)
13
14fig, axes = plt.subplots(n_rows, n_cols, figsize=(15, n_rows * 3))
15axes = axes.flatten()
16
17for i, (path, (_, row)) in enumerate(zip(val_images[:n_images], all_prediction_info.iterrows())):
18 ax = axes[i]
19 ax.imshow(Image.open(path))
20 ax.axis('off')
21 ax.set_title(f"Topic {row['Topic']}: {row['KeyBERTInspired'][0]}")
22
23# Hide unused axes
24for j in range(n_images, len(axes)):
25 axes[j].axis('off')
26
27plt.tight_layout()
28plt.show()