AutoMM for Entity Extraction with Text and Image - Quick Start¶
We have introduced how to train an entity extraction model with text data. Here, we move a step further by integrating data of other modalities. In many real-world applications, textual data usually comes with data of other modalities. For example, Twitter allows you to compose tweets with text, photos, videos, and GIFs. Amazon.com uses text, images, and videos to describe their products. These auxiliary modalities can be leveraged as additional context resolution of entities. Now, with AutoMM, you can easily exploit multimodal data to enhance entity extraction without worrying about the details.
import os
import pandas as pd
import warnings
warnings.filterwarnings('ignore')
Get the Twitter Dataset¶
In the following example, we will demonstrate how to build a multimodal named entity recognition model with a real-world Twitter dataset. This dataset consists of scrapped tweets from 2016 to 2017, and each tweet was composed of one sentence and one image. Let’s download the dataset.
download_dir = './ag_automm_tutorial_ner'
zip_file = 'https://automl-mm-bench.s3.amazonaws.com/ner/multimodal_ner.zip'
from autogluon.core.utils.loaders import load_zip
load_zip.unzip(zip_file, unzip_dir=download_dir)
Downloading ./ag_automm_tutorial_ner/file.zip from https://automl-mm-bench.s3.amazonaws.com/ner/multimodal_ner.zip...
0%| | 0.00/423M [00:00<?, ?iB/s]
2%|▏ | 8.27M/423M [00:00<00:05, 82.7MiB/s]
4%|▍ | 17.6M/423M [00:00<00:04, 88.7MiB/s]
7%|▋ | 29.3M/423M [00:00<00:03, 102MiB/s]
10%|▉ | 41.0M/423M [00:00<00:03, 108MiB/s]
12%|█▏ | 51.8M/423M [00:00<00:04, 75.3MiB/s]
15%|█▍ | 61.6M/423M [00:00<00:05, 71.6MiB/s]
16%|█▋ | 69.5M/423M [00:01<00:07, 47.7MiB/s]
19%|█▉ | 81.1M/423M [00:01<00:05, 60.6MiB/s]
21%|██ | 89.0M/423M [00:01<00:05, 60.7MiB/s]
24%|██▍ | 101M/423M [00:01<00:04, 72.9MiB/s]
26%|██▌ | 109M/423M [00:01<00:06, 51.4MiB/s]
28%|██▊ | 120M/423M [00:01<00:05, 58.7MiB/s]
31%|███ | 132M/423M [00:01<00:04, 70.0MiB/s]
33%|███▎ | 140M/423M [00:02<00:04, 69.7MiB/s]
36%|███▌ | 152M/423M [00:02<00:03, 80.5MiB/s]
38%|███▊ | 161M/423M [00:02<00:04, 64.1MiB/s]
40%|████ | 170M/423M [00:02<00:03, 70.3MiB/s]
42%|████▏ | 178M/423M [00:02<00:03, 69.8MiB/s]
45%|████▍ | 189M/423M [00:02<00:03, 59.7MiB/s]
47%|████▋ | 201M/423M [00:02<00:03, 71.6MiB/s]
50%|█████ | 212M/423M [00:03<00:02, 82.3MiB/s]
53%|█████▎ | 223M/423M [00:03<00:02, 89.0MiB/s]
56%|█████▌ | 235M/423M [00:03<00:01, 96.5MiB/s]
58%|█████▊ | 247M/423M [00:03<00:01, 103MiB/s]
61%|██████ | 258M/423M [00:03<00:01, 97.4MiB/s]
64%|██████▎ | 270M/423M [00:03<00:01, 103MiB/s]
66%|██████▌ | 280M/423M [00:03<00:01, 99.1MiB/s]
69%|██████▉ | 292M/423M [00:03<00:01, 102MiB/s]
72%|███████▏ | 304M/423M [00:03<00:01, 106MiB/s]
74%|███████▍ | 314M/423M [00:04<00:01, 100MiB/s]
77%|███████▋ | 326M/423M [00:04<00:00, 105MiB/s]
80%|███████▉ | 338M/423M [00:04<00:00, 109MiB/s]
83%|████████▎ | 350M/423M [00:04<00:00, 112MiB/s]
85%|████████▌ | 361M/423M [00:04<00:00, 93.6MiB/s]
88%|████████▊ | 373M/423M [00:04<00:00, 99.1MiB/s]
91%|█████████ | 383M/423M [00:04<00:00, 64.8MiB/s]
93%|█████████▎| 395M/423M [00:04<00:00, 75.3MiB/s]
96%|█████████▌| 404M/423M [00:05<00:00, 67.5MiB/s]
98%|█████████▊| 416M/423M [00:05<00:00, 78.5MiB/s]
100%|██████████| 423M/423M [00:05<00:00, 78.3MiB/s]
Next, we will load the CSV files.
dataset_path = download_dir + '/multimodal_ner'
train_data = pd.read_csv(f'{dataset_path}/twitter17_train.csv')
test_data = pd.read_csv(f'{dataset_path}/twitter17_test.csv')
label_col = 'entity_annotations'
We need to expand the image paths to load them in training.
image_col = 'image'
train_data[image_col] = train_data[image_col].apply(lambda ele: ele.split(';')[0]) # Use the first image for a quick tutorial
test_data[image_col] = test_data[image_col].apply(lambda ele: ele.split(';')[0])
def path_expander(path, base_folder):
path_l = path.split(';')
p = ';'.join([os.path.abspath(base_folder+path) for path in path_l])
return p
train_data[image_col] = train_data[image_col].apply(lambda ele: path_expander(ele, base_folder=dataset_path))
test_data[image_col] = test_data[image_col].apply(lambda ele: path_expander(ele, base_folder=dataset_path))
train_data[image_col].iloc[0]
'/home/ci/autogluon/docs/tutorials/multimodal/multimodal_prediction/ag_automm_tutorial_ner/multimodal_ner/twitter2017_images/17_06_1818.jpg'
Each row consists of the text and image of a single tweet and the entity_annotataions which contains the named entity annotations for the text column. Let’s look at an example row and display the text and picture of the tweet.
example_row = train_data.iloc[0]
example_row
text_snippet Uefa Super Cup : Real Madrid v Manchester United
image /home/ci/autogluon/docs/tutorials/multimodal/m...
entity_annotations [{"entity_group": "B-MISC", "start": 0, "end":...
Name: 0, dtype: object
Below is the image of this tweet.
example_image = example_row[image_col]
from IPython.display import Image, display
pil_img = Image(filename=example_image, width =300)
display(pil_img)
As you can see, this photo contains the logos of the Real Madrid football club, Manchester United football club, and the UEFA super cup. Clearly, the key information of the tweet sentence is coded here in a different modality.
Training¶
Now let’s fit the predictor with the training data.
Firstly, we need to specify the problem_type to ner.
As our annotations are used for text columns, to ensure the model to locate the correct text column for entity extraction,
we need to set the corresponding column type to text_ner using the column_types parameter in cases where multiple text columns are present.
Here we set a tight time budget for a quick demo.
from autogluon.multimodal import MultiModalPredictor
import uuid
label_col = "entity_annotations"
model_path = f"./tmp/{uuid.uuid4().hex}-automm_multimodal_ner"
predictor = MultiModalPredictor(problem_type="ner", label=label_col, path=model_path)
predictor.fit(
train_data=train_data,
column_types={"text_snippet":"text_ner"},
time_limit=300, #second
)
=================== System Info ===================
AutoGluon Version: 1.4.1b20250926
Python Version: 3.12.10
Operating System: Linux
Platform Machine: x86_64
Platform Version: #1 SMP Wed Mar 12 14:53:59 UTC 2025
CPU Count: 8
Pytorch Version: 2.7.1+cu126
CUDA Version: 12.6
GPU Count: 1
Memory Avail: 28.45 GB / 30.95 GB (91.9%)
Disk Space Avail: 175.12 GB / 255.99 GB (68.4%)
===================================================
AutoMM starts to create your model. ✨✨✨
To track the learning progress, you can open a terminal and launch Tensorboard:
```shell
# Assume you have installed tensorboard
tensorboard --logdir /home/ci/autogluon/docs/tutorials/multimodal/multimodal_prediction/tmp/e6ec12183e39401abdf0615e96e9bfd8-automm_multimodal_ner
```
INFO: Seed set to 0
self._config={'image': {'missing_value_strategy': 'zero'}, 'text': {'normalize_text': False}, 'categorical': {'minimum_cat_count': 100, 'maximum_num_cat': 20, 'convert_to_text': True, 'convert_to_text_template': 'latex'}, 'numerical': {'convert_to_text': False, 'scaler_with_mean': True, 'scaler_with_std': True}, 'document': {'missing_value_strategy': 'zero'}, 'label': {'numerical_preprocessing': 'standardscaler'}, 'pos_label': None, 'ignore_label': None, 'column_features_pooling_mode': 'concat', 'mixup': {'turn_on': False, 'mixup_alpha': 0.8, 'cutmix_alpha': 1.0, 'cutmix_minmax': None, 'prob': 1.0, 'switch_prob': 0.5, 'mode': 'batch', 'turn_off_epoch': 5, 'label_smoothing': 0.1}, 'modality_dropout': 0, 'templates': {'turn_on': False, 'num_templates': 30, 'template_length': 2048, 'preset_templates': ['super_glue', 'rte'], 'custom_templates': None}}
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[8], line 7
5 model_path = f"./tmp/{uuid.uuid4().hex}-automm_multimodal_ner"
6 predictor = MultiModalPredictor(problem_type="ner", label=label_col, path=model_path)
----> 7 predictor.fit(
8 train_data=train_data,
9 column_types={"text_snippet":"text_ner"},
10 time_limit=300, #second
11 )
File ~/autogluon/multimodal/src/autogluon/multimodal/predictor.py:543, in MultiModalPredictor.fit(self, train_data, presets, tuning_data, max_num_tuning_data, id_mappings, time_limit, save_path, hyperparameters, column_types, holdout_frac, teacher_predictor, seed, standalone, hyperparameter_tune_kwargs, clean_ckpts, predictions, labels, predictors)
540 assert isinstance(predictors, list)
541 learners = [ele if isinstance(ele, str) else ele._learner for ele in predictors]
--> 543 self._learner.fit(
544 train_data=train_data,
545 presets=presets,
546 tuning_data=tuning_data,
547 max_num_tuning_data=max_num_tuning_data,
548 time_limit=time_limit,
549 save_path=save_path,
550 hyperparameters=hyperparameters,
551 column_types=column_types,
552 holdout_frac=holdout_frac,
553 teacher_learner=teacher_learner,
554 seed=seed,
555 standalone=standalone,
556 hyperparameter_tune_kwargs=hyperparameter_tune_kwargs,
557 clean_ckpts=clean_ckpts,
558 id_mappings=id_mappings,
559 predictions=predictions,
560 labels=labels,
561 learners=learners,
562 )
564 return self
File ~/autogluon/multimodal/src/autogluon/multimodal/learners/base.py:683, in BaseLearner.fit(self, train_data, presets, tuning_data, time_limit, save_path, hyperparameters, column_types, holdout_frac, teacher_learner, seed, standalone, hyperparameter_tune_kwargs, clean_ckpts, **kwargs)
676 self.fit_sanity_check()
677 self.prepare_fit_args(
678 time_limit=time_limit,
679 seed=seed,
680 standalone=standalone,
681 clean_ckpts=clean_ckpts,
682 )
--> 683 fit_returns = self.execute_fit()
684 self.on_fit_end(
685 training_start=training_start,
686 strategy=fit_returns.get("strategy", None),
(...)
689 clean_ckpts=clean_ckpts,
690 )
692 return self
File ~/autogluon/multimodal/src/autogluon/multimodal/learners/base.py:595, in BaseLearner.execute_fit(self)
593 return dict()
594 else:
--> 595 attributes = self.fit_per_run(**self._fit_args)
596 self.update_attributes(**attributes) # only update attributes for non-HPO mode
597 return attributes
File ~/autogluon/multimodal/src/autogluon/multimodal/learners/ner.py:203, in NERLearner.fit_per_run(self, max_time, save_path, ckpt_path, resume, enable_progress_bar, seed, hyperparameters, advanced_hyperparameters, config, df_preprocessor, data_processors, model, standalone, clean_ckpts)
201 config = self.update_config_by_data_per_run(config=config, df_preprocessor=df_preprocessor)
202 output_shape = self.get_output_shape_per_run(df_preprocessor=df_preprocessor)
--> 203 model = self.get_model_per_run(
204 model=model,
205 config=config,
206 df_preprocessor=df_preprocessor,
207 output_shape=output_shape,
208 )
209 model = self.compile_model_per_run(config=config, model=model)
210 peft_param_names = self.get_peft_param_names_per_run(model=model, config=config)
File ~/autogluon/multimodal/src/autogluon/multimodal/learners/ner.py:105, in NERLearner.get_model_per_run(self, model, config, df_preprocessor, output_shape)
97 def get_model_per_run(
98 self,
99 model: nn.Module,
(...)
102 output_shape: int,
103 ):
104 if model is None:
--> 105 model = create_fusion_model(
106 config=config,
107 num_classes=output_shape,
108 num_numerical_columns=len(df_preprocessor.numerical_feature_names),
109 num_categories=df_preprocessor.categorical_num_categories,
110 )
111 return model
File ~/autogluon/multimodal/src/autogluon/multimodal/models/utils.py:1649, in create_fusion_model(config, num_classes, classes, num_numerical_columns, num_categories, numerical_fill_values, pretrained)
1645 single_models.append(model)
1647 if len(single_models) > 1:
1648 # must have one fusion model if there are multiple independent models
-> 1649 model = fusion_model(models=single_models)
1650 elif len(single_models) == 1:
1651 model = single_models[0]
File ~/autogluon/multimodal/src/autogluon/multimodal/models/fusion/fusion_ner.py:67, in MultimodalFusionNER.__init__(self, prefix, models, hidden_features, num_classes, adapt_in_features, activation, dropout_prob, normalization, loss_weight)
23 def __init__(
24 self,
25 prefix: str,
(...)
33 loss_weight: Optional[float] = None,
34 ):
35 """
36 Parameters
37 ----------
(...)
65 The weight of individual models.
66 """
---> 67 super().__init__(
68 prefix=prefix,
69 models=models,
70 loss_weight=loss_weight,
71 )
72 logger.debug("initializing MultimodalFusionNER")
74 if loss_weight is not None:
TypeError: AbstractMultimodalFusionModel.__init__() got an unexpected keyword argument 'loss_weight'
Under the hood, AutoMM automatically detects the data modalities, selects the related models from the multimodal model pools, and trains the selected models. If multiple backbones are available, AutoMM appends a late-fusion model on top of them.
Evaluation¶
predictor.evaluate(test_data, metrics=['overall_recall', "overall_precision", "overall_f1"])
Prediction¶
You can easily obtain the predictions by calling predictor.predict().
prediction_input = test_data.drop(columns=label_col).head(1)
predictions = predictor.predict(prediction_input)
print('Tweet:', prediction_input.text_snippet[0])
print('Image path:', prediction_input.image[0])
print('Predicted entities:', predictions[0])
for entity in predictions[0]:
print(f"Word '{prediction_input.text_snippet[0][entity['start']:entity['end']]}' belongs to group: {entity['entity_group']}")
Reloading and Continuous Training¶
The trained predictor is automatically saved and you can easily reload it using the path. If you are not satisfied with the current model performance, you can continue training the loaded model with new data.
new_predictor = MultiModalPredictor.load(model_path)
new_model_path = f"./tmp/{uuid.uuid4().hex}-automm_multimodal_ner_continue_train"
new_predictor.fit(train_data, time_limit=60, save_path=new_model_path)
test_score = new_predictor.evaluate(test_data, metrics=['overall_f1'])
print(test_score)
Other Examples¶
You may go to AutoMM Examples to explore other examples about AutoMM.
Customization¶
To learn how to customize AutoMM, please refer to Customize AutoMM.