{"cells": [{"cell_type": "markdown", "metadata": {}, "source": "# 0. \u306f\u3058\u3081\u306b\n\u3053\u306e notebook \u306f\u3001 IOAI 2025 \u5bfe\u7b56\u30b5\u30fc\u30d0\u30fc\u7b2c\u4e00\u56de\u30b3\u30f3\u30da\u5fa9\u7fd2\u4f1a\u306e baseline notebook \u3067\u3059\u3002\u81ea\u4f5c\u306e\u30d9\u30fc\u30b9\u30e9\u30a4\u30f3\u3092\u4f7f\u308f\u306a\u3044\u4eba\u306f\u3053\u306e\u30d9\u30fc\u30b9\u30e9\u30a4\u30f3\u3092\u4f7f\u3063\u3066\u304f\u3060\u3055\u3044\u3002\n## 0.1. \u3053\u306e\u30b3\u30f3\u30da\u306b\u3064\u3044\u3066\n- \u753b\u50cf\u30d5\u30a1\u30a4\u30eb\u304c\u4e0e\u3048\u3089\u308c\u3066\u3001\u305d\u306e\u753b\u50cf\u304c\u72ac\u304b\u732b\u304b\u3092\u4e88\u6e2c\u3059\u308b\u3001\u4e8c\u5024\u5206\u985e\u306e\u30b3\u30f3\u30da\u3067\u3059\u3002  \n- \u8a55\u4fa1\u6307\u6a19\u306f [logloss]() \u3067\u3059\u3002\n- **\u4eca\u56de\u306e\u30b3\u30f3\u30da\u306f test \u30c7\u30fc\u30bf\u306b\u5bfe\u3059\u308b\u4e88\u6e2c\u7d50\u679c\u3092 csv \u30d5\u30a1\u30a4\u30eb\u3067\u63d0\u51fa\u3059\u308b\u3060\u3051\u3067\u3044\u3044\u3067\u3059\u3002\u624b\u5143\u3067\u5b9f\u884c\u3057\u3066 csv \u30d5\u30a1\u30a4\u30eb\u3092\u4f5c\u3063\u3066\u3082\u3088\u3057\u3001 kaggle \u4e0a\u3067\u4f5c\u3063\u3066\u63d0\u51fa\u3057\u3066\u3082\u3088\u3057\u3001\u3067\u3059**\n\n## 0.2. \u4eca\u56de\u306e\u76ee\u6a19\n- \u521d\u5fc3\u8005 : \u753b\u50cf\u8a8d\u8b58\u306e\u521d\u6b69\u7684\u306a\u5b9f\u88c5\u306b\u3064\u3044\u3066\u5b66\u3076\u3002\n- \u4e2d\u7d1a\u8005 : \u69d8\u3005\u306a\u753b\u50cf\u8a8d\u8b58\u30e2\u30c7\u30eb\u3084 data augumentation \u624b\u6cd5\u306b\u3064\u3044\u3066\u5b66\u3076\u3002\n- \u4e0a\u7d1a\u8005 : \u30a2\u30f3\u30b5\u30f3\u30d6\u30eb\u3084\u7d30\u304b\u3044\u30c6\u30af\u3092\u7528\u3044\u3066\u3067\u304d\u308b\u3060\u3051\u9ad8\u3044\u30b9\u30b3\u30a2\u3092\u51fa\u3059\n\n## 0.3. \u30c7\u30fc\u30bf\u306b\u3064\u3044\u3066\n- train.zip, test.zip, sample_submission.csv \u3068\u3044\u3046 3 \u3064\u306e\u30d5\u30a1\u30a4\u30eb\u304c\u4e0e\u3048\u3089\u308c\u307e\u3059\n- zip \u306e\u64cd\u4f5c + dataloader \u306e\u4f5c\u308a\u65b9\u306f\u5c11\u3057\u8907\u96d1\u306a\u306e\u3067\u3001\u5b9f\u88c5\u3092\u5909\u3048\u306a\u3044\u3053\u3068\u3092\u63a8\u5968\u3057\u307e\u3059\u3002\n\n## 0.4. \u30b3\u30f3\u30da\u5fa9\u7fd2\u4f1a\u306b\u3064\u3044\u3066\n- \u89e3\u304f\u6642\u306f\u57fa\u672c\u7684\u306b\u81ea\u7531\u306b\u3084\u3063\u3066\u3082\u3089\u3063\u3066\u5927\u4e08\u592b\u3067\u3059\u3002\u305f\u3060\u3057\u3001 IOAI \u306b\u8fd1\u3044\u5f62\u5f0f\u306b\u3059\u308b\u306a\u3089\u691c\u7d22 OK \u3001\u3053\u306e\u30b3\u30f3\u30da\u306e notebook \u3068 discussion \u306f\u53c2\u8003\u306b\u3057\u306a\u3044\u3001\u3068\u304b\u304c\u3044\u3044\u3068\u601d\u3044\u307e\u3059\u3002\n- \u89e3\u304f\u671f\u9593\u306e\u7d42\u4e86\u65e5\u306f 1/22 \u3067\u3059\u3002\u305d\u306e\u5f8c\u3001\u4e0a\u4f4d\u9663\u306e\u89e3\u6cd5\u3092\u898b\u305f\u308a\u3001\u305d\u308c\u305e\u308c\u306e\u89e3\u6cd5\u3092\u898b\u305f\u308a\u3057\u307e\u3059\u3002\u4e00\u3064\u306e google slide \u3092\u307f\u3093\u306a\u3067\u7de8\u96c6\u3059\u308b\u4e88\u5b9a\u3067\u3059\u3002  \n\n## 0.5. \u305d\u306e\u4ed6\n- \u4f55\u3067\u3082\u308f\u304b\u3089\u306a\u3044\u3068\u3053\u308d\u304c\u3042\u3063\u305f\u3089\u3069\u3093\u3069\u3093\u8cea\u554f\u3057\u3066\u304f\u3060\u3055\u3044\uff01\n- \u4e00\u90e8\u8aac\u660e\u306f\u753b\u50cf\u8a8d\u8b58\u30e2\u30c7\u30eb\u306e\u57fa\u672c\u7684\u306a\u77e5\u8b58\u304c\u3042\u308b\u3053\u3068\u3092\u524d\u63d0\u306b\u66f8\u304b\u308c\u3066\u3044\u307e\u3059\u3002\u308f\u304b\u3089\u306a\u3044\u6240\u304c\u3042\u308c\u3070\u9069\u5b9c\u8abf\u3079\u308b\u304b\u8cea\u554f\u3057\u3066\u304f\u3060\u3055\u3044\u3002\n- \u521d\u5fc3\u8005\u3058\u3083\u306a\u3044\u4eba\u3078 : \u4e00\u90e8\u8a18\u8ff0\u304c\u304a\u304b\u3057\u304b\u3063\u305f\u308a\u3057\u305f\u3089\u6307\u6458\u3057\u3066\u304f\u3060\u3055\u308b\u3068\u3042\u308a\u304c\u305f\u3044\u3067\u3059\uff01"}, {"cell_type": "markdown", "metadata": {}, "source": "# 1. \u30e9\u30a4\u30d6\u30e9\u30ea\u8aad\u307f\u8fbc\u307f\u3068\u8a2d\u5b9a"}, {"cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": "# \u3044\u3064\u3082\u306e\nimport os\nimport gc\nimport re\nimport sys\nimport time\nimport copy\nimport random \nimport glob \n\n# zip \u30d5\u30a1\u30a4\u30eb\u7528\nimport zipfile\nimport shutil\n\n# \u3044\u3064\u3082\u306e\nimport numpy as np\nimport pandas as pd\n\n# pytorch \u95a2\u9023\nimport torch\nimport torch.nn as nn\nfrom torch.utils.data import DataLoader, Dataset\n\n# \u753b\u50cf\u95a2\u9023\nimport timm\nimport albumentations as A\nfrom albumentations.pytorch import ToTensorV2   \nfrom PIL import Image\n\n# \u30b9\u30b1\u30b8\u30e5\u30fc\u30e9\u306e\u305f\u3081\u3060\u3051\u306b\u3044\u308c\u308b\nimport transformers\n\n# \u9032\u6357\u30d0\u30fc\nfrom tqdm import tqdm\n\n# loss \u3068 KFold\nfrom sklearn.metrics import log_loss, accuracy_score, roc_auc_score\nfrom sklearn.model_selection import KFold\n\n# \u56f3\u793a\nimport matplotlib.pyplot as plt\n\n# \u8b66\u544a\u306e\u7121\u8996\nimport warnings\nwarnings.filterwarnings(\"ignore\")\n\ndevice = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\nprint(device)"}, {"cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": "class CFG :\n    debug_one_epoch = True\n    debug_one_fold = False\n    only_infer = False\n    num_workers = 16\n    batch_size = 64\n    num_epochs = 10\n    lr = 1e-3\n    early_stopping_round = 5\n    random_seed = 42\n    n_splits = 5\n    model_name = \"resnet18\" # timm \u3067\u4f7f\u3046\u30e2\u30c7\u30eb\u540d\n    pretrained_path = None\n    train_dir = None # \u5b66\u7fd2\u30c7\u30fc\u30bf\u30bb\u30c3\u30c8\u306e\u30d1\u30b9\n    test_dir = None # \u30c6\u30b9\u30c8\u30c7\u30fc\u30bf\u30bb\u30c3\u30c8\u306e\u30d1\u30b9\n    optimizer = torch.optim.AdamW\n    criterion = nn.BCEWithLogitsLoss()\n    scheduler = transformers.get_linear_schedule_with_warmup\n    input_imgsize = 224\n    data_dir = \"../input/dogs-vs-cats-redux-kernels-edition/\"\n    kaggle_working_dir = \"/kaggle/working/\"\n    \ndef seed_torch(seed):\n    random.seed(seed)\n    os.environ['PYTHONHASHSEED'] = str(seed)\n    np.random.seed(seed)\n    torch.manual_seed(seed)\n    torch.cuda.manual_seed(seed)\n    torch.backends.cudnn.deterministic = True\n    \nseed_torch(CFG.random_seed)\n\nif  CFG.debug_one_epoch :\n    CFG.num_epochs = 1\n\nprint('KAGGLE_URL_BASE' in set(os.environ.keys()))\n"}, {"cell_type": "markdown", "metadata": {}, "source": "\u524d\u56de\u304b\u3089\u540d\u524d\u3092\u5909\u3048\u305f CFG \u306f input_imsize \u4ee5\u5916\u306a\u3044\u306f\u305a\u3067\u3059\u3002  \ninput_imsize \u306f\u3001\u5165\u529b\u3059\u308b\u753b\u50cf\u3092\u30e2\u30c7\u30eb\u5074\u304c\u6307\u5b9a\u3057\u305f\u6b63\u65b9\u5f62\u306e\u30b5\u30a4\u30ba\u306b\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u306e\u3067\u3001\u305d\u306e\u8a2d\u5b9a\u3092\u3057\u3066\u3044\u307e\u3059\u3002"}, {"cell_type": "markdown", "metadata": {}, "source": "# 2. submission \u306e\u5f62\u5f0f\u3092\u78ba\u8a8d\u3057\u3088\u3046"}, {"cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": "submission = pd.read_csv(os.path.join(CFG.data_dir, \"sample_submission.csv\"))\nsubmission.head(3)"}, {"cell_type": "markdown", "metadata": {}, "source": "# 3. zip \u30d5\u30a1\u30a4\u30eb\u306e\u89e3\u51cd & \u4e0b\u6e96\u5099\u3092\u3057\u3088\u3046\n\u50d5\u306e\u624b\u5143\u3060\u3068\u89e3\u51cd\u3055\u308c\u3066\u3044\u308b\u306e\u3067\u3059\u304c\u3001 kaggle \u3060\u3068\u3044\u3061\u3044\u3061\u89e3\u51cd\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002"}, {"cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": "if 'KAGGLE_URL_BASE' in set(os.environ.keys()) :\n    kaggle_train_dir = os.path.join(CFG.kaggle_working_dir, \"train\")\n    # \u3059\u3067\u306b\u89e3\u51cd\u3055\u308c\u3066\u3044\u308b\u5834\u5408\u306f\u89e3\u51cd\u3057\u306a\u3044\n    if not os.path.exists(kaggle_train_dir) :\n        shutil.unpack_archive(os.path.join(CFG.data_dir, \"train.zip\"), CFG.kaggle_working_dir)\n    \n    kaggle_test_dir = os.path.join(CFG.kaggle_working_dir, \"test\")\n    if not os.path.exists(kaggle_test_dir) :\n        shutil.unpack_archive(os.path.join(CFG.data_dir, \"test.zip\"), CFG.kaggle_working_dir)\n        \n    CFG.data_dir = CFG.kaggle_working_dir\n    \nCFG.train_dir = os.path.join(CFG.data_dir, \"train\")\nCFG.test_dir = os.path.join(CFG.data_dir, \"test\")"}, {"cell_type": "markdown", "metadata": {}, "source": "train \u3068 test \u306e\u305d\u308c\u305e\u308c\u306e\u753b\u50cf\u306e\u30d1\u30b9\u306e\u30ea\u30b9\u30c8\u3092\u4f5c\u308a\u307e\u3059\u3002\u307e\u305f\u3001\u30c7\u30fc\u30bf\u6570\u3092\u898b\u307e\u3059\u3002"}, {"cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": "train_list = glob.glob(os.path.join(CFG.data_dir, \"train\", \"*.jpg\"))\ntest_list = glob.glob(os.path.join(CFG.data_dir, \"test\", \"*.jpg\"))\n\nprint(f\"train data : {len(train_list)}\")\nprint(f\"test data : {len(test_list)}\")"}, {"cell_type": "markdown", "metadata": {}, "source": "\u72ac\u3068\u732b\u305d\u308c\u305e\u308c\u306e\u753b\u50cf\u304c\u4f55\u679a\u3042\u308b\u304b\u3092\u898b\u307e\u3057\u3087\u3046\u3002"}, {"cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": "print(\"the number of dog : \", len([i for i in train_list if \"dog\" in i]))\nprint(\"the number of cat : \", len([i for i in train_list if \"cat\" in i]))"}, {"cell_type": "markdown", "metadata": {}, "source": "# 4. \u753b\u50cf\u306e\u6271\u3044\u65b9\u306b\u3064\u3044\u3066\u5b66\u307c\u3046"}, {"cell_type": "markdown", "metadata": {}, "source": "## 4.1. \u753b\u50cf\u306e\u8868\u793a\u65b9\u6cd5\u306b\u3064\u3044\u3066\u5b66\u307c\u3046\n\u4eca\u56de\u306f\u9069\u5f53\u306a\u753b\u50cf\u3092\u9078\u3093\u3067\u8868\u793a\u3055\u305b\u307e\u3059\u3002\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u30b3\u30fc\u30c9\u3067\u8868\u793a\u3067\u304d\u307e\u3059\u3002\u3064\u3044\u3067\u306b\u753b\u50cf\u306e\u30b5\u30a4\u30ba\u3082\u898b\u307e\u3059\u3002"}, {"cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": "random_img = random.choice(train_list)\nimg = Image.open(random_img)\nprint(random_img)\nprint(img.size)\nplt.imshow(img)"}, {"cell_type": "markdown", "metadata": {}, "source": "## 4.2. \u753b\u50cf\u306e\u30b3\u30f3\u30d4\u30e5\u30fc\u30bf\u4e0a\u3067\u306e\u6271\u3044\u306b\u3064\u3044\u3066\n\u305d\u3082\u305d\u3082\u753b\u50cf\u30c7\u30fc\u30bf\u3068\u3044\u3046\u306e\u306f\u3069\u306e\u3088\u3046\u306b\u4fdd\u5b58\u3055\u308c\u3066\u3044\u308b\u306e\u3067\u3057\u3087\u3046\u304b\u3002 numpy \u914d\u5217\u306b\u5909\u63db\u3057\u3064\u3064\u3001\u5b9f\u969b\u306b\u78ba\u8a8d\u3057\u3066\u307f\u307e\u3057\u3087\u3046\u3002"}, {"cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": "img_array = np.array(img)\nprint(img_array.shape)"}, {"cell_type": "markdown", "metadata": {}, "source": "\u3053\u3093\u306a\u611f\u3058\u3067\u6271\u3063\u3066\u3044\u307e\u3059\u3002 3 \u6b21\u5143\u914d\u5217\u3060\u3068\u601d\u3063\u3066\u3044\u305f\u3060\u3044\u3066\u69cb\u3044\u307e\u305b\u3093\u3002  \nH \u3092\u7e26\u5e45\u3001 W \u3092\u6a2a\u5e45\u3001 C \u3092\u30c1\u30e3\u30f3\u30cd\u30eb\u6570\uff08\u3053\u3053\u3067\u306f RGB \uff09\u3068\u3057\u3066\u3001  [H,W,C] \u306e\u9806\u306b\u4e26\u3093\u3067\u3044\u308b\u3053\u3068\u3092\u899a\u3048\u3066\u304a\u3044\u3066\u304f\u3060\u3055\u3044\u3002"}, {"cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": "print(img_array[:,:,0])"}, {"cell_type": "markdown", "metadata": {}, "source": "## 4.3. Albumentations \u3092\u7528\u3044\u305f data augumentation \u306b\u3064\u3044\u3066\n### 4.3.1. \u305d\u3082\u305d\u3082 data augumentation \u3068\u306f\n\u305d\u3082\u305d\u3082\u3001 data augumentation \u3068\u306f\u30c7\u30fc\u30bf\u306b\u5bfe\u3057\u3066\u4f55\u3089\u304b\u306e\u64cd\u4f5c\u3092\u52a0\u3048\u308b\u3053\u3068\u3067\u3001\u30c7\u30fc\u30bf\u6570\u3092\u6c34\u5897\u3057\u3059\u308b\u3053\u3068\u3067\u3059\u3002\u30e2\u30c7\u30eb\u306e\u6c4e\u5316\u6027\u80fd\u3092\u4e0a\u3052\u305f\u308a\u3001\u904e\u5b66\u7fd2\u3092\u6291\u3048\u308b\u52b9\u679c\u304c\u3042\u308b\u3053\u3068\u304b\u3089\u3001\u30b3\u30f3\u30da\u3067\u3088\u304f\u4f7f\u308f\u308c\u307e\u3059\u3002  \n\u3053\u3053\u3067\u306f\u3001\u753b\u50cf\u306e\u30c7\u30fc\u30bf\u62e1\u5f35\u7528\u30e9\u30a4\u30d6\u30e9\u30ea\u3067\u3042\u308b [Albumentations](https://github.com/albumentations-team/albumentations) \u3092\u4f7f\u3063\u3066\u3001\u69d8\u3005\u306a\u624b\u6cd5\u3092\u78ba\u8a8d\u3057\u3066\u3044\u304d\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002  \n\n### 4.3.2. resize\n\u307e\u305a\u306f\u3001\u4e00\u756a\u57fa\u672c\u306e resize \u304b\u3089\u3067\u3059\u3002\u4e0e\u3048\u3089\u308c\u305f\u753b\u50cf\u306e\u30b5\u30a4\u30ba\u306f\u30d0\u30e9\u30d0\u30e9\u3067\u3059\u304c\u3001 CNN \u306b\u5165\u529b\u3059\u308b\u969b\u306b\u306f\u6240\u5b9a\u306e\u30b5\u30a4\u30ba\uff08 214x214 \u306a\u3069\uff09\u306b\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u305d\u306e\u30b5\u30a4\u30ba\u5909\u63db\u3092\u30e9\u30a4\u30d6\u30e9\u30ea\u5074\u3067\u81ea\u52d5\u3067\u3084\u3063\u3066\u304f\u308c\u307e\u3059\u3002"}, {"cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": "transform_tmp = A.Compose([\n    A.Resize(CFG.input_imgsize, CFG.input_imgsize),\n])\nimg_transformed_tmp = transform_tmp(image = np.array(img)) # numpy \u914d\u5217\u4ee5\u5916\u53d7\u3051\u53d6\u3063\u3066\u304f\u308c\u306a\u3044\nprint(img_transformed_tmp.keys())\nimg_transformed_tmp = Image.fromarray(img_transformed_tmp[\"image\"])\nprint(img_transformed_tmp.size)\nplt.imshow(img_transformed_tmp)"}, {"cell_type": "markdown", "metadata": {}, "source": "\u7121\u4e8b 224x224 \u306b\u306a\u308a\u307e\u3057\u305f\u306d\u3002  \n### 4.3.3. hoprizontal flip\n\u6b21\u306b\u3001 HorizontalFlip \u3092\u8a66\u3057\u3066\u3044\u304d\u307e\u3059\u3002\u753b\u50cf\u306e\u5de6\u53f3\u3092\u53cd\u8ee2\u3055\u305b\u307e\u3059\u3002\u3053\u308c\u3082\u7acb\u6d3e\u306a data augumentation \u306e\u4e00\u3064\u3067\u3059\u3002"}, {"cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": "transform_tmp = A.Compose([\n    A.Resize(CFG.input_imgsize, CFG.input_imgsize),\n    A.HorizontalFlip(p=1.0),\n])\nimg_transformed_tmp = transform_tmp(image = np.array(img))\nimg_transformed_tmp = Image.fromarray(img_transformed_tmp[\"image\"])\nplt.imshow(img_transformed_tmp)\n"}, {"cell_type": "markdown", "metadata": {}, "source": "### 4.3.4. normalize\nNormalize \u3082\u8a66\u3057\u3066\u307f\u307e\u3057\u3087\u3046\u3002\u3053\u308c\u306f\u3001\u753b\u50cf\u306e\u6b63\u898f\u5316\u3092\u884c\u3046\u95a2\u6570\u3067\u3059\u3002\u5404\u753b\u50cf\u306e\u5024\u3092\u3001\u6307\u5b9a\u3057\u305f\u5e73\u5747\u3068\u6a19\u6e96\u504f\u5dee\u306b\u57fa\u3065\u3044\u3066\u5909\u63db\u3059\u308b\u3053\u3068\u3067\u3001\u5b66\u7fd2\u3092\u5b89\u5b9a\u5316\u3055\u305b\u305f\u308a\u3001\u30c7\u30fc\u30bf\u306e\u30b9\u30b1\u30fc\u30eb\u3092\u4e00\u5b9a\u306e\u7bc4\u56f2\u5185\u306b\u53ce\u3081\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002  \nAlbumentations \u306e normalize \u3067\u306f\u3001 ImageNet \u3068\u3044\u3046\u30c7\u30fc\u30bf\u30bb\u30c3\u30c8\u306e\u5e73\u5747\u306e\u5024\u3092\u30c7\u30d5\u30a9\u30eb\u30c8\u3067\u4f7f\u7528\u3057\u3066\u304f\u308c\u307e\u3059\u3002"}, {"cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": "transform_tmp = A.Compose([\n    A.Resize(CFG.input_imgsize, CFG.input_imgsize),\n    A.Normalize(),\n])\nimg_transformed_tmp = transform_tmp(image = np.array(img))[\"image\"]\nplt.imshow(img_transformed_tmp)"}, {"cell_type": "markdown", "metadata": {}, "source": "\u4eba\u9593\u306b\u306f\u308f\u304b\u308a\u306b\u304f\u304f\u306a\u3063\u305f\u304b\u3082\u77e5\u308c\u307e\u305b\u3093\u304c\u3001 NN \u306b\u3068\u3063\u3066\u306f\u3053\u3063\u3061\u306e\u307b\u3046\u304c\u308f\u304b\u308a\u3084\u3059\u3044\uff08\u591a\u5206\uff09\u3067\u3059\u3002\u5168\u90e8\u5024\u3067\u7ba1\u7406\u3057\u3066\u3044\u308b\u306e\u3067\u3002  \n  \n### 4.3.5. ToTensorv2\nToTensorv2 \u3068\u3044\u3046\u95a2\u6570\u306e\u6319\u52d5\u306b\u3044\u3064\u3044\u3066\u89e3\u8aac\u3057\u3088\u3046\u3068\u601d\u3044\u307e\u3059\u3002\u5143\u3005\u3001 \"HWC\" \u30d5\u30a9\u30fc\u30de\u30c3\u30c8\u3067\u753b\u50cf\u3092\u6271\u3063\u3066\u3044\u307e\u3057\u305f\u304c\u3001 PyTorch \u3092\u59cb\u3081\u3068\u3057\u305f\u30e9\u30a4\u30d6\u30e9\u30ea\u3067\u306f \"CHW\" \u306e\u9806\u756a\u3067\u6271\u3046\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u3053\u308c\u3092\u3059\u308b\u3064\u3044\u3067\u306b\u3001 PyTorch \u306e torch.Tensor \u306b\u5909\u63db\u3057\u3066\u304f\u308c\u308b\u306e\u304c\u3001\u3053\u306e ToTensorv2 \u3067\u3059\u3002 \n~~\u30cd\u30fc\u30df\u30f3\u30b0\u3057\u305f\u4eba\u306b\u3061\u3087\u3063\u3068\u6587\u53e5\u8a00\u3044\u305f\u3044......~~  \n\u203b\u4ee5\u4e0b\u8a73\u3057\u3044\u4eba\u5411\u3051  \ntorchvision \u306e transform \u306b\u4f7f\u3044\u6163\u308c\u3066\u3044\u308b\u4eba\u306f\u3001\u305d\u3063\u3061\u306e ToTensor \u3068\u3044\u3046\u95a2\u6570\u3092\u77e5\u3063\u3066\u3044\u308b\u3068\u601d\u3044\u307e\u3059\u3002\u305d\u3063\u3061\u3068\u306f **\u6319\u52d5\u304c\u304b\u306a\u308a\u9055\u3046\u3089\u3057\u3044** \u306e\u3067\u6c17\u3092\u3064\u3051\u307e\u3057\u3087\u3046\u3002\u8abf\u3079\u305f\u3068\u3053\u308d\u3001\u3042\u3063\u3061\u306f\u52dd\u624b\u306b Normalize \u3057\u3066\u304f\u308c\u305f\u308a\u3001 0-255 \u3067\u306f\u306a\u304f 0-1 \u306b\u30b9\u30b1\u30fc\u30ea\u30f3\u30b0\u3057\u3066\u304f\u308c\u308b\u3089\u3057\u3044\uff1f"}, {"cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [], "source": "transform_tmp = A.Compose([\n    A.Resize(CFG.input_imgsize, CFG.input_imgsize),\n    ToTensorV2()\n])\nimg_transformed_tmp = transform_tmp(image = np.array(img))\nprint(img_transformed_tmp.keys())\nprint(type(img_transformed_tmp[\"image\"]))\nprint(img_transformed_tmp[\"image\"].shape)"}, {"cell_type": "markdown", "metadata": {}, "source": "[3,244,244] \u306b\u306a\u3063\u3066\u3044\u308b\u3053\u3068\u304b\u3089\u5206\u304b\u308b\u901a\u308a\u3001 [C,H,W] \u306e\u9806\u306b\u4e26\u3073\u66ff\u3048\u3089\u308c\u3066\u3044\u307e\u3059\u3002\u3053\u308c\u3067\u4e00\u822c\u7684\u306a CNN \u306b\u5165\u529b\u3067\u304d\u308b\u5f62\u5f0f\u306b\u306a\u308a\u307e\u3057\u305f\u3002  \n### 4.3.6. \u7d42\u308f\u308a\u306b\ndata augumentation \u306b\u306f\u4ed6\u306b\u3082\u307c\u304b\u3057\u305f\u308a\u3001\u4e00\u90e8\u3092\u5207\u308a\u53d6\u3063\u305f\u308a\u3001\u753b\u50cf\u3092\u62e1\u5927\u3057\u305f\u308a\u3068\u672c\u5f53\u306b\u69d8\u3005\u306a\u64cd\u4f5c\u304c\u3042\u308a\u307e\u3059\uff01 [\u3053\u3061\u3089\u306e\u8a18\u4e8b](https://qiita.com/kurilab/items/b69e1be8d0224ae139ad) \u306b\u8272\u3005\u307e\u3068\u307e\u3063\u3066\u3044\u308b\u306e\u3067\u3001\u305f\u304f\u3055\u3093\u624b\u3092\u52d5\u304b\u3057\u3066\u8a66\u3057\u3066\u307f\u3066\u304f\u3060\u3055\u3044\uff01"}, {"cell_type": "markdown", "metadata": {}, "source": "# 5. dataset \u3092\u4f5c\u308d\u3046\n\u9811\u5f35\u3063\u3066\u30c7\u30fc\u30bf\u30bb\u30c3\u30c8\u3092\u4f5c\u308a\u307e\u3059\u3002\u4eca\u56de\u306f\u4e2d\u9593\u306e\u8868\u73fe\u3068\u3057\u3066 pd.dataframe \u306b path \u3068 class \u3092\u307e\u3068\u3081\u3066\u3001\u305d\u308c\u3092\u5143\u306b dataset \u306b\u6e21\u3057\u3066\u3044\u304d\u307e\u3059\u3002 class \u306f\u30b3\u30f3\u30da\u306e\u901a\u308a\u3001\u72ac\u3092 1 \u3001\u732b\u3092 0 \u3068\u3057\u307e\u3059\u3002 test \u30c7\u30fc\u30bf\u306b\u306f -1 \u3092\u5272\u308a\u632f\u308a\u307e\u3059\u3002"}, {"cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [], "source": "# \u30d1\u30b9\u3068 class \u3092\u307e\u3068\u3081\u305f DataFrame \u3092\u4f5c\u6210\ntrain_df = pd.DataFrame(train_list, columns=[\"path\"])\ntrain_df[\"class\"] = train_df[\"path\"].apply(lambda x : x.split(\"/\")[-1].split(\".\")[0])\ntrain_df[\"class\"] = train_df[\"class\"].map({\"dog\" : 1, \"cat\" : 0})\ntest_df = pd.DataFrame(test_list, columns=[\"path\"])\ntest_df[\"class\"] = -1\n# test_df \u306b\u5bfe\u3057\u3066\u306f path \u306e\u6570\u5b57\u304c\u6607\u9806\u3067\u3042\u308b\u3053\u3068\u3092\u4fdd\u8a3c\u3059\u308b\u305f\u3081\u306b id \u3092\u8ffd\u52a0\ntest_df[\"id\"] = test_df[\"path\"].apply(lambda x : int(x.split(\"/\")[-1].split(\".\")[0]))\ntest_df = test_df.sort_values(\"id\").reset_index(drop=True)\n\ntrain_df.head(3)"}, {"cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [], "source": "test_df.head(3)"}, {"cell_type": "markdown", "metadata": {}, "source": "\u5148\u307b\u3069\u884c\u3063\u305f data augumentation \u3092\u307e\u3068\u3081\u3066\u5b9a\u7fa9\u3057\u3066\u3044\u304d\u307e\u3059\u3002"}, {"cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [], "source": "train_transform = A.Compose([\n    A.Resize(CFG.input_imgsize, CFG.input_imgsize),\n    A.HorizontalFlip(p=0.5), # 50% \u306e\u78ba\u7387\u3067\u6c34\u5e73\u53cd\u8ee2\n    A.Normalize(),\n    ToTensorV2()\n])\ntest_transform = A.Compose([\n    A.Resize(CFG.input_imgsize, CFG.input_imgsize),\n    A.Normalize(),\n    ToTensorV2()\n])"}, {"cell_type": "markdown", "metadata": {}, "source": "dataset \u3092\u5b9a\u7fa9\u3057\u3066\u3044\u304d\u307e\u3059\u3002\u3053\u3053\u304b\u3089\u306f\u524d\u56de\u306e notebook \u3082\u53c2\u7167\u3057\u3066\u304f\u3060\u3055\u3044\u3002\u8aac\u660e\u3092\u5c11\u306a\u3081\u306b\u3057\u307e\u3059\u3002"}, {"cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [], "source": "class DogsCatsDataset(Dataset) :\n    def __init__(self, df, transform=None) :\n        self.df = df # \u3055\u3063\u304d\u306e pandas dataframe \u3092\u53d7\u3051\u53d6\u308b\n        self.transform = transform # \u753b\u50cf\u306e\u5909\u63db\u51e6\u7406\u3092\u53d7\u3051\u53d6\u308b\n\n    def __len__(self) :\n        return len(self.df)\n    \n    def __getitem__(self, idx) :\n        img = Image.open(self.df.iloc[idx, 0])\n        img = self.transform(image = np.array(img))[\"image\"]\n        label = self.df.iloc[idx, 1].astype(np.float32)\n        return img, label"}, {"cell_type": "markdown", "metadata": {}, "source": "# 6. \u5b66\u7fd2\u306e\u30eb\u30fc\u30d7\u3092\u5b9a\u7fa9\u3057\u3088\u3046\n## 6.1. train_one_epoch \u3092\u5b9a\u7fa9\u3057\u3088\u3046\n\u524d\u3082\u8a00\u3063\u305f\u901a\u308a\u3001\u500b\u4eba\u7684\u306b\u306f\u3053\u306e\u66f8\u304d\u65b9\u304c\u4e00\u756a\u898b\u3084\u3059\u3044\u3068\u601d\u3063\u3066\u3044\u307e\u3059\u3002 dataloader \u304b\u3089\u53d6\u308a\u51fa\u3059\u3082\u306e\u304c\u753b\u50cf\u3068\u30e9\u30d9\u30eb\u306e\u30bb\u30c3\u30c8\u306b\uff08\u524d\u56de\u304b\u3089\uff09\u5909\u308f\u3063\u3066\u3044\u308b\u3053\u3068\u306b\u6ce8\u610f\u3057\u307e\u3057\u3087\u3046\uff01"}, {"cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [], "source": "def train_one_epoch(model, dataloader, optimizer, scheduler, criterion) :\n    model.train()\n    losses = []\n    for img, label in tqdm(dataloader) :\n        img = img.to(device)\n        label = label.to(device)\n        \n        optimizer.zero_grad()\n        output = model(img)\n        loss = criterion(output.squeeze(-1),label)\n        loss.backward()\n        optimizer.step()\n        scheduler.step()\n        losses.append(loss.item())\n        \n    return np.mean(losses)"}, {"cell_type": "markdown", "metadata": {}, "source": "## 6.2. eval_one_epoch \u3092\u5b9a\u7fa9\u3057\u3088\u3046\n\u4eca\u56de\u306f BCEWithLogitsLoss \u3068\u3044\u3046\u640d\u5931\u95a2\u6570\u3092\u4f7f\u3063\u3066\u5b66\u7fd2\u3092\u9032\u3081\u3066\u3044\u308b\u306e\u3067\u3059\u304c\u3001 logloss \u3082\u51fa\u529b\u3067\u304d\u308b\u3088\u3046\u306b\u3057\u307e\u3059\u3002  \n\u3053\u306e\u6642\u3001 BCEWithLogitsLoss \u306e\u5185\u90e8\u3067 sigmoid \u95a2\u6570\u304c\u9069\u7528\u3055\u308c\u3066\u3044\u3066\u3001 logits \u3068\u3044\u3046\u5024\u3092\u81ea\u52d5\u3067\u78ba\u7387\u306b\u5909\u63db\u3057\u3066\u3044\u308b\u3053\u3068\u306b\u6ce8\u610f\u3057\u307e\u3057\u3087\u3046\u3002\u901a\u5e38\u306e\u30e2\u30c7\u30eb\u306e\u51fa\u529b (output) \u306f logits \u306e\u5024\u306a\u306e\u3067\u3001sigmoid \u95a2\u6570\u3092\u4f7f\u7528\u3057\u3066\u78ba\u7387\u306b\u5909\u63db\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002  \n\u3068\u8a00\u3063\u3066\u3082\u521d\u898b\u306e\u4eba\u306b\u306f\u308f\u304b\u3089\u306a\u3044\u3068\u601d\u3046\u306e\u3067\u3001 [CrossEntropy \u3068 BCEWithLogitsLoss \u306e\u89e3\u8aac](https://qiita.com/ground0state/items/8933f9ef54d6cd005a69) \u3001[sigmoid \u306e\u89e3\u8aac](https://cvml-expertguide.net/terms/dl/layers/activation-function/sigmoid/) \u3001 [sigmoid + BCE \u306e\u89e3\u8aac](https://cvml-expertguide.net/terms/dl/loss-function/loss-function-part1/) \u3068\u304b\u3092\u8aad\u3093\u3067\u3058\u3063\u304f\u308a\u7406\u89e3\u3057\u3066\u304f\u3060\u3055\u3044\uff01\u6700\u60aa\u98db\u3070\u3057\u3066\u3082\u5927\u4e08\u592b\u3067\u3059\uff01  \n\u203b\u8a73\u3057\u3044\u4eba\u306a\u3089\u300c logloss \u3068 BCEWithLogitsLoss \u306f\u540c\u5024\u3067\u306f\uff1f\u300d\u3068\u3044\u3046\u6307\u6458\u3092\u3059\u308b\u304b\u3082\u77e5\u308c\u307e\u305b\u3093\u3002\u53b3\u5bc6\u306b\u306f sigmoid \u3092\u631f\u3093\u3067\u3044\u308b\u306e\u3067\u9055\u3046\u306e\u3067\u3059\u304c\u3001\u305d\u306e\u70b9\u3092\u8003\u616e\u3057\u3066\u8003\u3048\u308b\u3068\u540c\u5024\u306b\u306a\u308b\u3001\u3068\u3044\u3046\u3053\u3068\u3067\u5927\u4e08\u592b\u306a\u306f\u305a\u3067\u3059\u3002"}, {"cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [], "source": "def eval_one_epoch(model, dataloader, criterion) :\n    model.eval()\n    losses = []\n    all_labels = []\n    all_outputs = []\n    with torch.no_grad() :\n        for img, label in tqdm(dataloader) :\n            img = img.to(device)\n            label = label.to(device)\n            output = model(img) # \u4e88\u6e2c\n            loss = criterion(output.squeeze(-1), label) # \u3044\u3089\u306a\u3044\u6b21\u5143\u3092\u6f70\u3057\u3066, loss \u3092\u8a08\u7b97\n            losses.append(loss.item()) # loss \u3092\u30ea\u30b9\u30c8\u306b\u8ffd\u52a0\n            all_labels.extend(label.cpu().numpy()) # \u30e9\u30d9\u30eb\u3092\u30ea\u30b9\u30c8\u306b\u8ffd\u52a0\n            pred = torch.sigmoid(output).cpu().numpy()\n            all_outputs.extend(pred) # \u4e88\u6e2c\u3092\u30ea\u30b9\u30c8\u306b\u8ffd\u52a0\u3001 sigmoid \u3067\u78ba\u7387\u306b\u5909\u63db\n    \n    all_labels = np.array(all_labels)\n    all_outputs = np.array(all_outputs)\n    \n    return {\n        \"bce_loss\" : np.mean(losses),\n        \"log_loss\" : log_loss(all_labels, all_outputs),\n        \"labels\" : all_labels,\n        \"outputs\" : all_outputs\n    }"}, {"cell_type": "markdown", "metadata": {}, "source": "## 6.3. infer \u3092\u5b9a\u7fa9\u3057\u3088\u3046\n\u4f8b\u306e\u3054\u3068\u304f\u5b9a\u7fa9\u3057\u3066\u3044\u304d\u307e\u3059\u3002\u4eca\u56de\u306f test \u306e\u5834\u5408 label \u304c -1 \u306a\u306e\u3067\u3001\u4e00\u5fdc assert \u3092\u304b\u3051\u307e\u3059\u3002"}, {"cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [], "source": "def infer(model, dataloader, test=False) :\n    model.eval()\n    all_outputs = []\n    with torch.no_grad() :\n        for img, label in tqdm(dataloader) :\n            if test :\n                assert(label[0] == -1)\n            img = img.to(device)\n            output = model(img)\n            all_outputs.extend(torch.sigmoid(output).cpu().numpy()) # \u4e88\u6e2c\u3092\u30ea\u30b9\u30c8\u306b\u8ffd\u52a0\u3001 sigmoid \u3067\u78ba\u7387\u306b\u5909\u63db\n            \n            \n    all_outputs = np.array(all_outputs)\n    return all_outputs"}, {"cell_type": "markdown", "metadata": {}, "source": "# 7. CV 1fold \u5206\u306e\u30eb\u30fc\u30d7\u3092\u7d44\u3082\u3046\n\u5148\u7a0b\u306e\u95a2\u6570\u3092\u30d5\u30eb\u6d3b\u7528\u3057\u3066\u7d44\u3093\u3067\u3044\u304d\u307e\u3059\u3002  \n\u4eca\u56de\u7279\u7b46\u3059\u3079\u304d\u70b9\u3068\u3057\u3066\u3001 **[timm](https://huggingface.co/timm)** \u3068\u3044\u3046\u30e9\u30a4\u30d6\u30e9\u30ea\u3092\u4f7f\u3063\u3066\u3044\u308b\u70b9\u304c\u3042\u308a\u307e\u3059\u3002\u3053\u306e\u30e9\u30a4\u30d6\u30e9\u30ea\u306f\u3001\u69d8\u3005\u306a\u753b\u50cf\u5411\u3051\u5b66\u7fd2\u6e08\u307f\u30e2\u30c7\u30eb\u304c\u7c21\u5358\u306b\u4f7f\u3048\u308b\u6700\u5f37\u30e9\u30a4\u30d6\u30e9\u30ea\u3067\u3059\u3002\u4f7f\u3044\u65b9\u8a18\u4e8b\u306f [\u3053\u3061\u3089\u306e\u8a18\u4e8b](https://logmi.jp/main/technology/325674?read_more=1) \u304c\u3068\u3063\u3066\u3082\u308f\u304b\u308a\u3084\u3059\u3044\u3067\u3059\u3002  \n\u4f7f\u3048\u308b\u30e2\u30c7\u30eb\u4e00\u89a7\u306f [\u3053\u3053\uff08\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u6570\u9806\uff09](https://huggingface.co/models?library=timm&sort=downloads) \u306b\u3042\u308a\u307e\u3059\u3002\u3068\u3044\u3063\u3066\u3082\u3069\u308c\u304c\u826f\u3044\u306e\u304b\u308f\u304b\u3089\u306a\u3044\u3068\u601d\u3046\u306e\u3067\u3001\u307e\u305a\u306f [\u3053\u306e\u6bbf\u5802\u5165\u308a\u30e2\u30c7\u30eb\u305f\u3061](https://huggingface.co/collections/timm/timm-takes-on-the-classics-655ce63dcaa21067cd7d3f4c) \u3068\u304b\u3001\u3055\u3063\u304d\u306e\u30e9\u30f3\u30ad\u30f3\u30b0\u306e\u4e0a\u4f4d\u306e\u30e2\u30c7\u30eb\u304b\u3089\u8a66\u3059\u306e\u3092\u304a\u3059\u3059\u3081\u3057\u307e\u3059\u3002\u5927\u62b5\u30e2\u30c7\u30eb\u540d\u3067\u30b0\u30b0\u308b\u3068\u305d\u306e\u30e2\u30c7\u30eb\u306e\u7279\u5fb4\u304c\u51fa\u3066\u304f\u308b\u306e\u3067\u305d\u308c\u3082\u53c2\u8003\u306b\u3057\u3066\u307f\u3066\u304f\u3060\u3055\u3044\uff01 CFG \u306e model_name \u3092\u5909\u3048\u308b\u3060\u3051\u3067\u3059\u3050\u306b\u8a66\u305b\u307e\u3059\uff01  \n\u307e\u305f\u3001\u4eca\u56de\u306f out of fold \u3092\u8a66\u3057\u3066\u307f\u307e\u3059\u3002\u8a73\u3057\u304f\u306f [\u3053\u306e\u8a18\u4e8b](https://qiita.com/Hatomugi/items/620c1bc757266b00e87f) \u3092\u53c2\u8003\u306b\u3057\u3066\u304f\u3060\u3055\u3044\u3002\u77e5\u3089\u306a\u304f\u3066\u3082\u5927\u4e08\u592b\u3067\u3059\u3002"}, {"cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [], "source": "def run_train_cv(train, test):\n    kf = KFold(n_splits=CFG.n_splits, shuffle=True, random_state=CFG.random_seed)\n    oof = np.zeros((len(train), 1)) # out of fold \u306e\u4e88\u6e2c\n    predictions =[]\n    \n    for fold, (train_idx, valid_idx) in enumerate(kf.split(train)) :\n        print(f\"====================fold : {fold}====================\")\n        # df \u3092\u5206\u5272\n        train_df = train.iloc[train_idx].reset_index(drop=True)\n        valid_df = train.iloc[valid_idx].reset_index(drop=True)\n        \n        # dataset \u3068 dataloader \u3092\u4f5c\u6210\n        train_dataset = DogsCatsDataset(train_df, transform=train_transform)\n        valid_dataset = DogsCatsDataset(valid_df, transform=test_transform)\n        test_dataset = DogsCatsDataset(test_df, transform=test_transform)\n        \n        train_loader = DataLoader(train_dataset, batch_size=CFG.batch_size, shuffle=True, num_workers=CFG.num_workers, drop_last=True, pin_memory=True)\n        valid_loader = DataLoader(valid_dataset, batch_size=CFG.batch_size, shuffle=False, num_workers=CFG.num_workers)\n        test_loader = DataLoader(test_dataset, batch_size=CFG.batch_size, shuffle=False, num_workers=CFG.num_workers)\n        \n        # timm \u3067\u30e2\u30c7\u30eb\u3092\u4f5c\u6210\u3001\u5b66\u7fd2\u6e08\u307f\u306e\u91cd\u307f\u3092\u8aad\u307f\u8fbc\u3080\n        model = timm.create_model(CFG.model_name, pretrained=True, num_classes=1)\n        model.to(device)\n        \n        # optimizer \u3068 scheduler \u3092\u4f5c\u6210\n        optimizer = CFG.optimizer(model.parameters(), lr=CFG.lr)\n        training_steps = len(train_loader) * CFG.num_epochs\n        warmup_steps = int(training_steps * 0.1)\n        scheduler = CFG.scheduler(optimizer, num_warmup_steps=warmup_steps, num_training_steps=training_steps)\n        \n        # early stopping \u306e\u305f\u3081\u306e\u5909\u6570\n        best_loss = np.inf\n        early_stopping_round = 0\n        \n        # \u5b66\u7fd2\n        for epoch in range(CFG.num_epochs):\n            start_time = time.time() # \u6642\u9593\u8a08\u6e2c\n            train_loss = train_one_epoch(model, train_loader, optimizer, scheduler, CFG.criterion)\n            valid_result = eval_one_epoch(model, valid_loader, CFG.criterion)\n            print(f\"epoch : {epoch} - train loss : {train_loss} - valid loss : {valid_result['bce_loss']} - valid log loss : {valid_result['log_loss']}\")\n            \n            # valid \u306e loss \u304c\u6539\u5584\u3057\u305f\u5834\u5408\u306b\u30e2\u30c7\u30eb\u3092\u4fdd\u5b58\n            if valid_result[\"bce_loss\"] < best_loss :\n                best_loss = valid_result[\"bce_loss\"]\n                early_stopping_round = 0\n                torch.save(model.state_dict(), f\"{CFG.model_name}_fold{fold}.pth\")\n                \n            # \u6539\u5584\u3057\u306a\u3044\u5834\u5408 early stopping \u306e\u5224\u5b9a\u3092\u3044\u308c\u308b\n            else :\n                early_stopping_round += 1\n                if early_stopping_round > CFG.early_stopping_round :\n                    break\n            \n            print(f\"spend time for epoch {epoch} : {time.time() - start_time}\")\n            \n        oof[valid_idx] = infer(model, valid_loader) # valid \u306b\u5bfe\u3059\u308b\u4e88\u6e2c\u3001\u3053\u308c\u3092 5 \u3064\u306e fold \u3067\u96c6\u3081\u3066\u5408\u4f53\u3059\u308b\u3053\u3068\u3067\u3001\u30ea\u30fc\u30af\u306a\u3057\u3067 train \u306b\u5bfe\u3057\u3066\u4e88\u6e2c\u304c\u51fa\u6765\u308b\n        \n        del model, optimizer, scheduler\n        gc.collect()\n        torch.cuda.empty_cache()\n        \n        if CFG.debug_one_fold :\n            break\n        \n    for fold in range(CFG.n_splits) :\n        model = timm.create_model(CFG.model_name, pretrained=True, num_classes=1)\n        model.load_state_dict(torch.load(f\"{CFG.model_name}_fold{fold}.pth\"))\n        model.to(device)\n        predictions.append(infer(model, test_loader, test=True))\n        if CFG.debug_one_fold :\n            break\n        \n    predictions = np.mean(predictions, axis=0)\n    \n        \n    return {\n        \"oof\" : oof,\n        \"predictions\" : predictions\n    }\n"}, {"cell_type": "markdown", "metadata": {}, "source": "#  8. \u5b9f\u884c\u3057\u3088\u3046"}, {"cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [], "source": "def main() :\n    if CFG.only_infer :\n        test_dataset = DogsCatsDataset(test_df, transform=test_transform)\n        test_loader = DataLoader(test_dataset, batch_size=CFG.batch_size, shuffle=False, num_workers=CFG.num_workers)\n        model = timm.create_model(CFG.model_name, pretrained=True, num_classes=1)\n        model.to(device)\n        predictions = infer(model, test_loader)\n        submission[\"label\"] = predictions\n        submission.to_csv(\"submission.csv\", index=False)\n       \n        \n    else :\n        result = run_train_cv(train_df, test_df)\n        oof_preds = result[\"oof\"]\n        predictions = result[\"predictions\"]\n        submission[\"label\"] = predictions\n        submission.to_csv(\"submission.csv\", index=False)\n        train_df[\"oof_preds\"] = oof_preds   \n        train_df.to_csv(\"oof_preds.csv\", index=False)\n        if CFG.debug_one_fold == False :\n            print(f\"oof log loss : {log_loss(train_df['class'], oof_preds)}\")\n        \nif __name__ == \"__main__\" :\n    main()"}, {"cell_type": "markdown", "metadata": {}, "source": "# 9. \u6539\u5584\u30dd\u30a4\u30f3\u30c8\n- data augumentation \u3092\u8272\u3005\u305f\u3081\u305d\u3046\uff08\u5168\u54e1\uff09 : [\u5148\u7a0b\u3082\u7d39\u4ecb\u3057\u305f\u8a18\u4e8b](https://qiita.com/kurilab/items/b69e1be8d0224ae139ad) \u306b\u8272\u3005\u3042\u308b\u306e\u3067\u3001 train \u306e transform \u306b\u8272\u3005\u8ffd\u52a0\u3057\u3066\u8a66\u3057\u3066\u307f\u3066\u304f\u3060\u3055\u3044\uff01\n- \u30e2\u30c7\u30eb\u3092\u8272\u3005\u305f\u3081\u305d\u3046\uff08\u5168\u54e1\uff09 : timm \u306b\u306f\u672c\u5f53\u306b\u69d8\u3005\u306a\u30e2\u30c7\u30eb\u304c\u3042\u308a\u307e\u3059\u3002 CFG.model_name \u3092\u5909\u3048\u308b\u3060\u3051\u3067\u8a66\u305b\u308b\u306e\u3067\u3084\u3063\u3066\u307f\u3066\u304f\u3060\u3055\u3044\uff01\u30d9\u30fc\u30b9\u30e9\u30a4\u30f3\u3067\u4f7f\u3063\u305f Resnet \u3092\u6539\u5584\u3057\u305f\u69d8\u3005\u306a\u30e2\u30c7\u30eb\u304c\u3042\u308a\u307e\u3059\uff01\n- \u30a2\u30f3\u30b5\u30f3\u30d6\u30eb\u3092\u3057\u3088\u3046\uff08\u521d\u4e2d\u7d1a\u8005\uff5e\uff09 : CNN \u3067\u8272\u3005\u8a66\u305b\u305f\u308a\u3001\u30e2\u30c7\u30eb\u3067\u8272\u3005\u8a66\u305b\u305f\u308a\u3001\u304c\u3057\u3084\u3059\u3044\u306e\u3067\u3084\u308a\u3084\u3059\u3044\u3068\u601d\u3044\u307e\u3059\uff01\n- \u30e2\u30c7\u30eb\u306e\u7279\u6027\u3092\u8003\u3048\u3088\u3046\uff08\u4e2d\u7d1a\u8005\uff5e\uff09 : \u30bf\u30b9\u30af\u306b\u3088\u3063\u3066\u5f97\u610f\u306a\u30e2\u30c7\u30eb / \u305d\u3053\u307e\u3067\u306a\u30e2\u30c7\u30eb\u3001\u306f\u5909\u308f\u3063\u3066\u304d\u307e\u3059\uff01CNN \u3058\u3083\u306a\u3044\u30bf\u30a4\u30d7\u306e\u30e2\u30c7\u30eb\u306f\uff1f\u305d\u306e\u5f97\u610f\u306a\u30bf\u30b9\u30af\u306f\uff1f\u601d\u3044\u51fa\u3057\u307e\u3057\u3087\u3046\n- \u30cf\u30a4\u30d1\u30e9\u8abf\u6574\u3092\u3057\u3088\u3046\uff08\u4e2d\u7d1a\u8005\uff5e\uff09 : lr \u3068\u304b\u306f\u6050\u3089\u304f\u30e2\u30c7\u30eb\u306b\u3088\u3063\u3066\u5909\u3048\u305f\u307b\u3046\u304c\u826f\u3044\u3067\u3059\u3002\n- \u5b9f\u9a13\u7ba1\u7406\u3092\u3057\u3088\u3046\uff08\u4e2d\u7d1a\u8005\uff5e\uff09 : \u305f\u304f\u3055\u3093\u30e2\u30c7\u30eb\u3092\u4f5c\u308b\u3068\u3044\u3046\u3053\u3068\u306f\u3001\u305f\u304f\u3055\u3093\u7ba1\u7406\u3057\u306a\u304d\u3083\u3044\u3051\u306a\u3044\u3068\u3044\u3046\u3053\u3068\u3067\u3059\u3002\n- \u7d30\u304b\u3044\u30c6\u30af\u3092\u305f\u3081\u305d\u3046\uff08\u4e0a\u7d1a\u8005\uff5e\uff09 : \u4eca\u56de\u306f\u524d\u56de\u3088\u308a\u3082\u7d30\u304b\u3044\u5b9f\u9a13\u3092\u7a4d\u307f\u91cd\u306d\u308b\u5fc5\u8981\u304c\u3042\u308b\u3068\u601d\u3044\u307e\u3059\u3002\u8272\u3005\u8a66\u3057\u3066\u30b9\u30b3\u30a2\u3092\u3067\u304d\u308b\u3060\u3051\u4e0b\u3052\u307e\u3057\u3087\u3046\uff01\u7279\u306b\u8a55\u4fa1\u6307\u6a19\u306b\u601d\u3044\u3092\u99b3\u305b\u308b\u3068\u3046\u307e\u304f\u3044\u304f\u3068\u601d\u3044\u307e\u3059\u3002"}], "metadata": {"kernelspec": {"display_name": "Python 3", "language": "python", "name": "python3"}, "language_info": {"codemirror_mode": {"name": "ipython", "version": 3}, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.12"}}, "nbformat": 4, "nbformat_minor": 2}