【AutoGluon】画像やテキストを含む表データの分類

2021年12月15日記事を修正しました

はじめに

下記のチュートリアルページを実行してみました。
auto.gluon.ai
扱うデータは表形式データですが文章を含んだ列と画像を含んだ列があります。


そのデータを使った分類問題です。

手順

データのダウンロード

from autogluon.core.utils.loaders import load_zip
download_dir = './ag_petfinder_tutorial'
zip_file = 'https://automl-mm-bench.s3.amazonaws.com/petfinder_kaggle.zip'
load_zip.unzip(zip_file, unzip_dir=download_dir)

2回目以降は改めてダウンロードすることはありません。


しかし、2回目以降でもZIPファイルの解凍の部分で時間がかかります。


改めて解凍しているのかZIPファイル内に含まれるファイルが確かに存在するのかを確認しているのかはわかりませんがメインスクリプトに含めないほうが良いと思います。事前にデータ準備をしておくのが得策です。

学習

import os
import pandas as pd
from autogluon.tabular import FeatureMetadata
from autogluon.tabular.configs.hyperparameter_configs import get_hyperparameter_config
from autogluon.tabular import TabularPredictor

dataset_path = os.path.join('ag_petfinder_tutorial', 'petfinder_processed')

train_data = pd.read_csv(os.path.join(dataset_path, 'train.csv'), index_col=0)

label = 'AdoptionSpeed'

train_data['Images'] = train_data['Images'].apply(lambda ele: os.path.abspath(os.path.join(dataset_path, ele.split(';')[0])))
train_data = train_data.drop(columns='RescuerID')
train_data = train_data.sample(500, random_state=0)

feature_metadata = FeatureMetadata.from_df(train_data)
feature_metadata = feature_metadata.add_special_types({'Images': ['image_path']})

hyperparameters = get_hyperparameter_config('multimodal')

save_path = 'ag-result'
predictor = TabularPredictor(label=label, path=save_path).fit(
    train_data=train_data,
    hyperparameters=hyperparameters,
    feature_metadata=feature_metadata,
    time_limit=900
)

上記スクリプトを実行すると結果は自動でセーブされ、以下のように出力されます。

AutoGluon training complete, total runtime = 500.36s ...
TabularPredictor saved. To load, use: predictor = TabularPredictor.load("ag-result/")

テストデータを使った検証

import os
import pandas as pd
from autogluon.tabular import TabularPredictor

download_dir = './ag_petfinder_tutorial'
dataset_path = os.path.join(download_dir, 'petfinder_processed')

test_data = pd.read_csv(os.path.join(dataset_path, 'dev.csv'), index_col=0)
test_data = test_data.drop(columns='RescuerID')
test_data['Images'] = test_data['Images'].apply(lambda ele: os.path.abspath(os.path.join(dataset_path, ele.split(';')[0])))

save_path = 'ag-result'
predictor = TabularPredictor.load(save_path)

leaderboard = predictor.leaderboard(test_data)

上記スクリプトを実行すると以下のような結果が出力されます。

                 model  score_test  score_val  pred_time_test  pred_time_val    fit_time  pred_time_test_marginal  pred_time_val_marginal  fit_time_marginal  stack_level  can_infer  fit_order
0             CatBoost    0.346449       0.30        0.037772       0.010308    2.238266                 0.037772                0.010308           2.238266            1       True          3
1        TextPredictor    0.325108       0.28       41.052221       1.561025  224.219930                41.052221                1.561025         224.219930            1       True          7
2        LightGBMLarge    0.323441       0.33        0.137053       0.006399    2.181856                 0.137053                0.006399           2.181856            1       True          6
3           LightGBMXT    0.322441       0.35        0.039906       0.004709    0.459762                 0.039906                0.004709           0.459762            1       True          2
4       ImagePredictor    0.310437       0.24       15.980155       0.709053   79.432156                15.980155                0.709053          79.432156            1       True          8
5  WeightedEnsemble_L2    0.306435       0.40       57.645412       2.350449  309.273259                 0.010063                0.000272           0.132887            2       True          9
6              XGBoost    0.290430       0.35        0.165749       0.004742    0.908064                 0.165749                0.004742           0.908064            1       True          4
7             LightGBM    0.289763       0.34        0.154658       0.004182    0.674046                 0.154658                0.004182           0.674046            1       True          1
8       NeuralNetMXNet    0.281761       0.37        0.244793       0.060868    1.667909                 0.244793                0.060868           1.667909            1       True          5

動作環境

Intel Core i7-7700K
RAM 32G
GTX 1080 (8G VRAM)
Ubuntu 20.04 on WSL2
Python 3.8.10
CUDA Toolkit 11.3
autogluon==0.3.2b20211215
mxnet-cu112==1.9.0b20211213
torch==1.10.0+cu113
torchvision==0.11.1+cu113

「MXNet」と「PyTorch」の両方が必要です。
CUDAのバージョンは11.3ですがmxnet-cu112が問題なく動いています。

環境構築

環境構築はこちらを参照して下さい。
touch-sp.hatenablog.com

良い結果を求めて(追記)

変更点

fitを実行するときに「time_limit=900」を消して「presets='best_quality'」を追加しました。

save_path = 'ag-result'
predictor = TabularPredictor(label=label, path=save_path).fit(
    train_data=train_data,
    hyperparameters=hyperparameters,
    feature_metadata=feature_metadata,
    presets='best_quality'
)

結果

AutoGluon training complete, total runtime = 1991.23s ...
TabularPredictor saved. To load, use: predictor = TabularPredictor.load("ag-result/")

500秒程度で終わっていた学習が1990秒かかりました。

                   model  score_test  score_val  pred_time_test  pred_time_val     fit_time  pred_time_test_marginal  pred_time_val_marginal  fit_time_marginal  stack_level  can_infer  fit_order
0        CatBoost_BAG_L1    0.351117      0.354        0.102392       0.052486    14.751874                 0.102392                0.052486          14.751874            1       True          3
1        LightGBM_BAG_L1    0.342114      0.344        0.110783       0.023850     6.291010                 0.110783                0.023850           6.291010            1       True          1
2   LightGBMLarge_BAG_L1    0.337112      0.342        0.457438       0.028369   438.474028                 0.457438                0.028369         438.474028            1       True          6
3  ImagePredictor_BAG_L1    0.335112      0.340       57.186587       3.523249   344.486961                57.186587                3.523249         344.486961            1       True          8
4   TextPredictor_BAG_L1    0.331777      0.322      204.993997       8.726427  1145.227865               204.993997                8.726427        1145.227865            1       True          7
5      LightGBMXT_BAG_L1    0.331444      0.356        0.159990       0.024562     3.109294                 0.159990                0.024562           3.109294            1       True          2
6    WeightedEnsemble_L2    0.330777      0.364       57.357289       3.548074   347.748438                 0.010712                0.000263           0.152183            2       True          9
7         XGBoost_BAG_L1    0.326442      0.340        0.748037       0.033216     4.363334                 0.748037                0.033216           4.363334            1       True          4
8  NeuralNetMXNet_BAG_L1    0.312771      0.310        0.891395       0.406581     9.962783                 0.891395                0.406581           9.962783            1       True          5

今回の例では少しだけ正解率が改善しています。