{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":5441,"databundleVersionId":38425,"sourceType":"competition"}],"dockerImageVersionId":30746,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport os\nimport time\nfrom fastai.vision.all import *\nimport zipfile\nimport albumentations as Alb","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-07-26T07:22:21.345681Z","iopub.execute_input":"2024-07-26T07:22:21.346035Z","iopub.status.idle":"2024-07-26T07:22:31.382228Z","shell.execute_reply.started":"2024-07-26T07:22:21.346006Z","shell.execute_reply":"2024-07-26T07:22:31.381435Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# データセットの展開\nwith zipfile.ZipFile('../input/dogs-vs-cats-redux-kernels-edition/train.zip', 'r') as zip_ref:\n    zip_ref.extractall('.')\nwith zipfile.ZipFile('../input/dogs-vs-cats-redux-kernels-edition/test.zip', 'r') as zip_ref:\n    zip_ref.extractall('.')","metadata":{"execution":{"iopub.status.busy":"2024-07-26T07:22:31.383685Z","iopub.execute_input":"2024-07-26T07:22:31.384194Z","iopub.status.idle":"2024-07-26T07:22:48.026276Z","shell.execute_reply.started":"2024-07-26T07:22:31.384166Z","shell.execute_reply":"2024-07-26T07:22:48.025478Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 不良画像の削除\nbad_images = [\n    'dog.10797.jpg', 'dog.10747.jpg', 'dog.10237.jpg', 'dog.9517.jpg',\n    'dog.8736.jpg', 'dog.5604.jpg', 'dog.1043.jpg', 'cat.4338.jpg',\n    'dog.10161.jpg', 'dog.10190.jpg'\n]\nfor img in bad_images:\n    os.remove(f'./train/{img}')","metadata":{"execution":{"iopub.status.busy":"2024-07-26T07:22:48.027409Z","iopub.execute_input":"2024-07-26T07:22:48.027667Z","iopub.status.idle":"2024-07-26T07:22:48.032898Z","shell.execute_reply.started":"2024-07-26T07:22:48.027644Z","shell.execute_reply":"2024-07-26T07:22:48.031969Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# データセットの読み込み\ntpath = \"./train\"\nftrain = get_image_files(tpath)\nprint('Train set size:', len(ftrain))","metadata":{"execution":{"iopub.status.busy":"2024-07-26T07:22:48.035523Z","iopub.execute_input":"2024-07-26T07:22:48.036122Z","iopub.status.idle":"2024-07-26T07:22:48.245293Z","shell.execute_reply.started":"2024-07-26T07:22:48.036089Z","shell.execute_reply":"2024-07-26T07:22:48.244375Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# データ拡張の定義\nclass AlbTransform(Transform):\n    def __init__(self, aug): self.aug = aug\n    def encodes(self, img: PILImage):\n        aug_img = self.aug(image=np.array(img))['image']\n        return PILImage.create(aug_img)\n    \ndef get_augs():\n    return Alb.Compose([\n        Alb.ShiftScaleRotate(rotate_limit=20, border_mode=0, value=(0,0,0)),\n        Alb.Transpose(),\n        Alb.Flip(),\n        Alb.RandomRotate90(),\n        Alb.RandomBrightnessContrast(),\n        Alb.HueSaturationValue(hue_shift_limit=5, sat_shift_limit=5, val_shift_limit=5),\n        Alb.CoarseDropout()\n    ])\n\nitem_tfms = [Resize(224), AlbTransform(get_augs())] \nbatch_tfms = [Normalize.from_stats(*imagenet_stats), *aug_transforms()]","metadata":{"execution":{"iopub.status.busy":"2024-07-26T07:22:48.246625Z","iopub.execute_input":"2024-07-26T07:22:48.246929Z","iopub.status.idle":"2024-07-26T07:22:48.453755Z","shell.execute_reply.started":"2024-07-26T07:22:48.246904Z","shell.execute_reply":"2024-07-26T07:22:48.452959Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# データローダーの作成\ndls = ImageDataLoaders.from_name_re(\n    path=tpath, fnames=ftrain, pat=r'(.+)\\.\\d+.jpg$', valid_pct=0.1, \n    item_tfms=item_tfms, batch_tfms=batch_tfms, bs=64, shuffle=True\n)\n\nprint('train items:', len(dls.train.items), 'validation items:', len(dls.valid.items))","metadata":{"execution":{"iopub.status.busy":"2024-07-26T07:22:48.455270Z","iopub.execute_input":"2024-07-26T07:22:48.455542Z","iopub.status.idle":"2024-07-26T07:22:50.789469Z","shell.execute_reply.started":"2024-07-26T07:22:48.455518Z","shell.execute_reply":"2024-07-26T07:22:50.788492Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# モデルの作成\nlearn = cnn_learner(dls, resnet50, metrics=[error_rate, accuracy])","metadata":{"execution":{"iopub.status.busy":"2024-07-26T07:22:50.790750Z","iopub.execute_input":"2024-07-26T07:22:50.791056Z","iopub.status.idle":"2024-07-26T07:22:52.281359Z","shell.execute_reply.started":"2024-07-26T07:22:50.791031Z","shell.execute_reply":"2024-07-26T07:22:52.280575Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 学習率のスケジューリングを追加\nlearn.fit_one_cycle(5, lr_max=slice(1e-3, 1e-2))","metadata":{"execution":{"iopub.status.busy":"2024-07-26T07:22:52.282816Z","iopub.execute_input":"2024-07-26T07:22:52.283320Z","iopub.status.idle":"2024-07-26T07:31:35.579595Z","shell.execute_reply.started":"2024-07-26T07:22:52.283284Z","shell.execute_reply":"2024-07-26T07:31:35.578586Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# テストデータの準備\nftest = get_image_files('test')\nprint('Testing', len(ftest), 'items')","metadata":{"execution":{"iopub.status.busy":"2024-07-26T07:31:35.581045Z","iopub.execute_input":"2024-07-26T07:31:35.581335Z","iopub.status.idle":"2024-07-26T07:31:35.683469Z","shell.execute_reply.started":"2024-07-26T07:31:35.581308Z","shell.execute_reply":"2024-07-26T07:31:35.682402Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# テストデータローダーの作成\ntst_dl = dls.test_dl(ftest, with_labels=False, shuffle=False)\ntst_dl.show_batch(max_n=12)","metadata":{"execution":{"iopub.status.busy":"2024-07-26T07:31:35.687186Z","iopub.execute_input":"2024-07-26T07:31:35.687471Z","iopub.status.idle":"2024-07-26T07:31:37.191973Z","shell.execute_reply.started":"2024-07-26T07:31:35.687447Z","shell.execute_reply":"2024-07-26T07:31:37.190993Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 推論の実行\nstartTime = time.time()\npreds = learn.tta(dl=tst_dl, n=5, use_max=False)\nprint('TTA in:', time.time()-startTime, 'secs')","metadata":{"execution":{"iopub.status.busy":"2024-07-26T07:31:37.193170Z","iopub.execute_input":"2024-07-26T07:31:37.193457Z","iopub.status.idle":"2024-07-26T07:36:53.922383Z","shell.execute_reply.started":"2024-07-26T07:31:37.193432Z","shell.execute_reply":"2024-07-26T07:36:53.920889Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 結果の保存\nsubm_df = pd.DataFrame()\nsubm_df['id'] = [item.stem for item in tst_dl.items]\nsubm_df['label'] = preds[0][:,1].clip(0.005, 0.995)\nsubm_df.to_csv('submission.csv', header=True, index=False)","metadata":{"execution":{"iopub.status.busy":"2024-07-26T07:36:53.923964Z","iopub.execute_input":"2024-07-26T07:36:53.925702Z","iopub.status.idle":"2024-07-26T07:36:53.989545Z","shell.execute_reply.started":"2024-07-26T07:36:53.925662Z","shell.execute_reply":"2024-07-26T07:36:53.988689Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# ソフトマックスを使用した予測結果の保存\nsubm_df['label'] = torch.softmax(preds[0], dim=1)[:, 1]\nsubm_df.to_csv('submission-softmax.csv', header=True, index=False)","metadata":{"execution":{"iopub.status.busy":"2024-07-26T07:36:53.991129Z","iopub.execute_input":"2024-07-26T07:36:53.991714Z","iopub.status.idle":"2024-07-26T07:36:54.032691Z","shell.execute_reply.started":"2024-07-26T07:36:53.991679Z","shell.execute_reply":"2024-07-26T07:36:54.031782Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 推論結果の可視化\ntst_dl.show_batch(max_n=12)","metadata":{"execution":{"iopub.status.busy":"2024-07-26T07:36:54.033737Z","iopub.execute_input":"2024-07-26T07:36:54.034051Z","iopub.status.idle":"2024-07-26T07:36:55.795008Z","shell.execute_reply.started":"2024-07-26T07:36:54.034026Z","shell.execute_reply":"2024-07-26T07:36:55.794047Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# クリーンアップ\nfrom shutil import rmtree\nrmtree('./train', ignore_errors=True)\nrmtree('./test', ignore_errors=True)","metadata":{"execution":{"iopub.status.busy":"2024-07-26T07:36:55.796257Z","iopub.execute_input":"2024-07-26T07:36:55.796569Z","iopub.status.idle":"2024-07-26T07:36:57.134958Z","shell.execute_reply.started":"2024-07-26T07:36:55.796542Z","shell.execute_reply":"2024-07-26T07:36:57.133939Z"},"trusted":true},"execution_count":null,"outputs":[]}]}