我尝试了AutoGluon的图像分类


介绍

我尝试了AutoGluon(https://autogluon.mxnet.io/index.html)的图像分类,这是Google Colaboratory环境中的AutoML库。
基本上,它将是添加到正式快速入门内容中的内容。

环境

在Google合作实验室进行。

Google Colab设置

对于图像分类,使用了深度学习模型,因此将其设置为使用GPU(请参见下图)。

GoogleColab_01.png

GoogleColab_02.png

在Google Colab

上安装库

如果仅使用上述设置,则在运行AutoGluon时,如果没有GPU,则会出现错误。
因此,我参考本文(https://qiita.com/tasmas/items/22cf80a4be80f7ad458e)安装了它。没有特别的错误,我做得很好。

1
2
3
4
!pip uninstall -y mkl
!pip install --upgrade mxnet-cu100
!pip install autogluon
!pip install -U ipykernel

执行

后,重新启动运行时。

运行自动胶水

遵循官方的快速入门。
https://autogluon.mxnet.io/tutorials/image_classification/beginner.html

首先,导入库并下载数据。
数据是位于Kaggle的Shopee-IET的图像数据,衣服等图像分为"婴儿裤","婴儿衬衫","女式休闲鞋"和"女式轻便上衣"四类。
但是,"自动胶合"页面上指向Kaggle的链接已断开。
(尽管Kaggle也有一个页面,但我无法一次找到数据。由于它是来自旧比赛的数据,因此可能已被删除。)

1
2
3
4
5
import autogluon as ag
from autogluon import ImageClassification as task

filename = ag.download('https://autogluon.s3.amazonaws.com/datasets/shopee-iet.zip')
ag.unzip(filename)

分别读取学习数据和评估数据。

1
2
train_dataset = task.Dataset('data/train')
test_dataset = task.Dataset('data/test', train=False)

只需通过"拟合"训练数据即可构建用于图像识别的模型。
太容易了...

1
2
3
4
classifier = task.fit(train_dataset,
                      epochs=5,
                      ngpus_per_trial=1,
                      verbose=False)

以下是合适的标准输出结果。
显然我要带ResNet 50。
最后,它还为您提供学习曲线。这次我只学习了5个纪元,所以只花了几分钟。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
scheduler_options: Key 'training_history_callback_delta_secs': Imputing default value 60
scheduler_options: Key 'delay_get_config': Imputing default value True

Starting Experiments
Num of Finished Tasks is 0
Num of Pending Tasks is 2
scheduler: FIFOScheduler(
DistributedResourceManager{
(Remote: Remote REMOTE_ID: 0,
    <Remote: 'inproc://172.28.0.2/371/1' processes=1 threads=2, memory=13.65 GB>, Resource: NodeResourceManager(2 CPUs, 1 GPUs))
})

100%
2/2 [03:58<00:00, 119.19s/it]
Model file not found. Downloading.
Downloading /root/.mxnet/models/resnet50_v1b-0ecdba34.zip from https://apache-mxnet.s3-accelerate.dualstack.amazonaws.com/gluon/models/resnet50_v1b-0ecdba34.zip...
100%|██████████| 55344/55344 [00:01<00:00, 45529.30KB/s]
[Epoch 5] Validation: 0.456: 100%
5/5 [01:06<00:00, 13.29s/it]




Saving Training Curve in checkpoint/plot_training_curves.png

autogluon_result.png

学习时的准确度约为50%。我认为目前的设定无法解决问题。

1
2
print('Top-1 val acc: %.3f' % classifier.results['best_reward'])
# Top-1 val acc: 0.469

让我们对某个图像数据

进行预测。如果您预测" Baby Shirt"的数据,则可以肯定将其归类为" Baby Shirt"。

1
2
3
4
5
6
7
image = 'data/test/BabyShirt/BabyShirt_323.jpg'
ind, prob, _ = classifier.predict(image, plot=True)

print('The input picture is classified as [%s], with probability %.2f.' %
  (train_dataset.init().classes[ind.asscalar()], prob.asscalar()))

# The input picture is classified as [BabyShirt], with probability 0.61.

使用评估数据计算准确性时,约为70%。

1
2
3
test_acc = classifier.evaluate(test_dataset)
print('Top-1 test acc: %.3f' % test_acc)
# Top-1 test acc: 0.703

这真的很方便,因为您可以在眨眼之间建立模型,但是
我觉得拟合函数非常重要,因此我做了一些研究。

关于fit

的一点研究

拟合函数参数

这次,我在图像识别中使用了拟合功能。源代码(https://github.com/awslabs/autogluon/blob/15c105b0f1d8bdbebcc86bd7e7a3a1b71e83e82b9/autogluon/task/image_classification/image_classification.py#L63)摘录如下。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
    @staticmethod
    def fit(dataset,
            net=Categorical('ResNet50_v1b', 'ResNet18_v1b'),
            optimizer=NAG(
                learning_rate=Real(1e-3, 1e-2, log=True),
                wd=Real(1e-4, 1e-3, log=True),
                multi_precision=False
            ),
            loss=SoftmaxCrossEntropyLoss(),
            split_ratio=0.8,
            batch_size=64,
            input_size=224,
            epochs=20,
            final_fit_epochs=None,
            ensemble=1,
            metric='accuracy',
            nthreads_per_trial=60,
            ngpus_per_trial=1,
            hybridize=True,
            scheduler_options=None,
            search_strategy='random',
            search_options=None,
            plot_results=False,
            verbose=False,
            num_trials=None,
            time_limits=None,
            resume=False,
            output_directory='checkpoint/',
            visualizer='none',
            dist_ip_addrs=None,
            auto_search=True,
            lr_config=Dict(
                lr_mode='cosine',
                lr_decay=0.1,
                lr_decay_period=0,
                lr_decay_epoch='40,80',
                warmup_lr=0.0,
                warmup_epochs=0
            ),
            tricks=Dict(
                last_gamma=False,
                use_pretrained=True,
                use_se=False,
                mixup=False,
                mixup_alpha=0.2,
                mixup_off_epoch=0,
                label_smoothing=False,
                no_wd=False,
                teacher_name=None,
                temperature=20.0,
                hard_weight=0.5,
                batch_norm=False,
                use_gn=False),
            **kwargs):

由于默认情况下在中调用ResNet50,它是

参数之一,因此
您可以看到它早先被调用过。
因此,当您想尝试其他模型时,问题是可以选择哪种模型。

关于致电

的型号

如官方页面(https://autogluon.mxnet.io/tutorials/image_classification/hpo.html)所述,gluoncv model_zoo(https://gluon-cv.mxnet.io/model_zoo/ category.html < br>
)似乎能够获取模型。
截至2020年9月4日的注册型号如下图所示。

gluon.png

除了上面提到的ResNet,我们发现还有一些经过训练的模型,例如MobileNet和VGG。
这些模型已经按精度顺序显示,因此我认为很容易选择。

关于我自己的模型

另一方面,如果您想构建自己的神经网络,则可以使用AutoGluon基础中使用的mxnet使其建立。

https://github.com/awslabs/autogluon/blob/15c105b0f1d8bdbebc86bd7e7a3a1b71e83e82b9/autogluon/task/image_classification/nets.py#L52

1
2
3
4
5
6
7
8
9
10
def mnist_net():
    mnist_net = gluon.nn.Sequential()
    mnist_net.add(ResUnit(1, 8, hidden_channels=8, kernel=3, stride=2))
    mnist_net.add(ResUnit(8, 8, hidden_channels=8, kernel=5, stride=2))
    mnist_net.add(ResUnit(8, 16, hidden_channels=8, kernel=3, stride=2))
    mnist_net.add(nn.GlobalAvgPool2D())
    mnist_net.add(nn.Flatten())
    mnist_net.add(nn.Activation('relu'))
    mnist_net.add(nn.Dense(10, in_units=16))
    return mnist_net

除此之外,还可以设置metricoptimiser,因此我认为进行深度学习会很容易。

在末尾

在图像分析的情况下,有一幅使用Tensorflow,keras等构建计算逻辑的图像,但是当能够如此轻松地构建模型时,我认为图像数据处理(降噪和增强)会很好,因为花费在(等)上的时间似乎相对增加。

因为AutoGluon基于mxnet(深度学习的库(或平台)),所以我确信AutoGluon也支持自然语言处理(NLP)。而是,Auto Gluon-Tabular似乎是一个加alpha元素。

AWS支持AutoGluon和mxnet,因此我想在完成此操作的同时想知道此处的库是否用于AWS ML服务。