コロナウイルスで自粛ムードの真っ只中、Kaggleで『
COVID-19 Xray Dataset (Train & Test Sets)』というデータセットを見つけました。
前回に続いて、このデータを分析してみます。
元データでは、Train用とTest用に分かれていましたが、1つにまとめてdataフォルダー下に'NORMAL'と'PNEUMONIA'のフォルダーとして保存しました。
%reload_ext autoreload %autoreload 2 %matplotlib inline |
from fastai.vision import * from fastai.metrics import accuracy |
Data Preparation
tfms = get_transforms(do_flip=False) |
data=ImageDataBunch.from_folder(path,valid_pct=0.2,ds_tfms=tfms,size=224,bs=16) data.normalize(imagenet_stats) |
サンプルデータの表示
data.show_batch(rows=3,figsize=(5,5)) |
クラス名とクラス数の確認
2
modelの作成import torchvision.models as models |
learn1 = cnn_learner(data, models.resnet101, metrics=accuracy,callback_fns=ShowGraph) |
Learning rate finder
earn1 = cnn_learner(data, models.resnet101, metrics=accuracy, callback_fns=ShowGraph) learn1.unfreeze() learn1.lr_find() learn1.recorder.plot() learn1.recorder.plot_lr() |
Training
learn1 = cnn_learner(data, models.resnet101, metrics=accuracy, callback_fns=ShowGraph) learn1.unfreeze() learn1.fit_one_cycle(20, slice(1e-5,1e-4), pct_start=0.10) learn1.recorder.plot_lr() |
epoch | train_loss | valid_loss | accuracy | time |
---|
0 | 1.104608 | 0.681558 | 0.594595 | 00:06 |
1 | 0.909959 | 0.150244 | 0.918919 | 00:06 |
2 | 0.660842 | 0.083128 | 0.945946 | 00:06 |
3 | 0.513764 | 0.126556 | 0.945946 | 00:06 |
4 | 0.391456 | 0.137053 | 0.945946 | 00:07 |
5 | 0.324914 | 0.126845 | 0.972973 | 00:06 |
6 | 0.268465 | 0.096547 | 0.972973 | 00:06 |
7 | 0.223595 | 0.017147 | 1.000000 | 00:06 |
8 | 0.188071 | 0.105446 | 0.972973 | 00:07 |
9 | 0.160610 | 0.020655 | 1.000000 | 00:06 |
10 | 0.139689 | 0.021469 | 1.000000 | 00:07 |
11 | 0.116390 | 0.014832 | 1.000000 | 00:06 |
12 | 0.110800 | 0.017743 | 1.000000 | 00:06 |
13 | 0.111802 | 0.156424 | 0.945946 | 00:06 |
14 | 0.098927 | 0.156956 | 0.945946 | 00:06 |
15 | 0.091004 | 0.047044 | 0.972973 | 00:06 |
16 | 0.085418 | 0.024739 | 0.972973 | 00:06 |
17 | 0.074240 | 0.007974 | 1.000000 | 00:06 |
18 | 0.075480 | 0.004438 | 1.000000 | 00:06 |
19 | 0.073207 | 0.002889 | 1.000000 | 00:06 |
あまり良い学習は出来てないようです。
training結果の分析
Load pre-trained weights
learn = learn.load("covid19-res101") |
trainingの結果
interp = ClassificationInterpretation.from_learner(learn)
losses,idxs = interp.top_losses()
len(data.valid_ds)==len(losses)==len(idxs) |
True
interp.plot_top_losses(16, figsize=(15,11)) |
Confusion matrixinterp.plot_confusion_matrix(figsize=(12,12), dpi=60) |
分類は100%出来ているようです。
検証データのクラス毎のaccuracy(取り敢えず・・・)
import numpy as np
res_corr=interp.confusion_matrix()
#classの数 class_num=data.c
#class毎のデータ総数 class_sum=np.sum(res_corr, axis=1)
for i in range(class_num): print(data.classes[i],':','{:.4f}'.format(res_corr[i,i]/class_sum[i])) |
NORMAL : 1.0000
PNEUMONIA : 1.0000