データセットの作成
データはこちらからダウンロードさせて頂きました。import glob import random import pandas as pd cat_files = glob.glob('train/cat*') dog_files = glob.glob('train/dog*') cat_train = random.sample(cat_files, 10000) dog_train = random.sample(dog_files, 10000) cat_test = list(set(cat_files) - set(cat_train)) dog_test = list(set(dog_files) - set(dog_train)) train_dataset_list = [] for image_path in cat_train: train_dataset_list.append({ 'image': image_path, 'label': 0 }) for image_path in dog_train: train_dataset_list.append({ 'image': image_path, 'label': 1 }) train_df = pd.DataFrame(train_dataset_list) train_df.to_pickle('train_df.pkl') test_dataset_list = [] for image_path in cat_test: test_dataset_list.append({ 'image': image_path, 'label': 0 }) for image_path in dog_test: test_dataset_list.append({ 'image': image_path, 'label': 1 }) test_df = pd.DataFrame(test_dataset_list) test_df.to_pickle('test_df.pkl')
学習
Pythonスクリプト
import pandas as pd from autogluon.vision import ImageDataset from autogluon.vision import ImagePredictor from autogluon .core.space import Categorical train_df = pd.read_pickle('train_df.pkl') train_dataset = ImageDataset(train_df) predictor = ImagePredictor() model = Categorical('resnet101_v1d') hyperparameters = {'model':model, 'batch_size':32, 'epochs': 5} hyperparameter_tune_kwargs={'num_trials': 2} predictor.fit( train_dataset, hyperparameters = hyperparameters, hyperparameter_tune_kwargs = hyperparameter_tune_kwargs ) predictor.save('predictor.ag')
学習結果
Finished, total runtime is 2082.66 s { 'best_config': { 'estimator': <class 'gluoncv.auto.estimators.image_classification.image_classification.ImageClassificationEstimator'>, 'gpus': [0], 'img_cls': { 'batch_norm': False, 'last_gamma': False, 'model': 'resnet101_v1d', 'use_gn': False, 'use_pretrained': True, 'use_se': False}, 'train': { 'batch_size': 32, 'crop_ratio': 0.875, 'data_dir': 'auto', 'dtype': 'float32', 'early_stop_baseline': -inf, 'early_stop_max_value': inf, 'early_stop_min_delta': 0.001, 'early_stop_patience': 10, 'epochs': 5, 'hard_weight': 0.5, 'input_size': 224, 'label_smoothing': False, 'log_interval': 50, 'lr': 0.01, 'lr_decay': 0.1, 'lr_decay_epoch': '40, 60', 'lr_decay_period': 0, 'lr_mode': 'step', 'mixup': False, 'mixup_alpha': 0.2, 'mixup_off_epoch': 0, 'mode': '', 'momentum': 0.9, 'no_wd': False, 'num_training_samples': -1, 'num_workers': 8, 'output_lr_mult': 0.1, 'pretrained_base': True, 'rec_train': 'auto', 'rec_train_idx': 'auto', 'rec_val': 'auto', 'rec_val_idx': 'auto', 'resume_epoch': 0, 'start_epoch': 0, 'teacher': None, 'temperature': 20, 'transfer_lr_mult': 0.01, 'use_rec': False, 'warmup_epochs': 0, 'warmup_lr': 0.0, 'wd': 0.0001}, 'valid': {'batch_size': 32, 'num_workers': 8}}, 'total_time': 2082.6618723869324, 'train_acc': 0.9678047153024911, 'valid_acc': 0.99}
テストデータを用いた検証
Pythonスクリプト
import pandas as pd from autogluon.vision import ImageDataset from autogluon.vision import ImagePredictor test_df = pd.read_pickle('test_df.pkl') test_dataset = ImageDataset(test_df) predictor = ImagePredictor.load('predictor.ag') result = predictor.evaluate(test_dataset) print('Top-1 test acc: %.3f' % result['top1'])
検証結果
Top-1 test acc: 0.994
推論
Pythonスクリプト
from autogluon.vision import ImagePredictor predictor = ImagePredictor.load('predictor.ag') proba = predictor.predict_proba('test1.jpg') print(proba)
推論結果
0 1 0 0.000141 0.999859
動作環境
Intel Core i7-7700K RAM 32G NVIDIA GTX 1080 (VRAM 8G)
Ubuntu 20.04 on WSL2 Python 3.8.10
mxnet-cu112==1.9.0 autogluon==0.3.2b20211230
さいごに
スクリプトはGitHubに公開しています。GitHub - dai-ichiro/ImageClassification
追記
AutoGluonのAutoMMPredcitorを使う方法も別記事に書きました。touch-sp.hatenablog.com