{"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":3649,"databundleVersionId":46718,"sourceType":"competition"}],"dockerImageVersionId":30732,"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 # データ操作と分析のためのライブラリ\nfrom tqdm import tqdm # プログレスバーを表示するライブラリ\nfrom PIL import Image # 画像の読み込みと操作のためのライブラリ\nimport torch\nimport torch.nn as nn # PyTorchのニューラルネットワークモジュール\nfrom torch.utils.data import Dataset, DataLoader # PyTorchのデータセットとデータローダーのユーティリティ\nimport torchvision.datasets as datasets # PyTorchのデータセットモジュール\nfrom torchvision import transforms # 画像の前処理と変換のためのモジュール\nimport os  # OSの機能を使用するための標準ライブラリ\nimport random # ランダム操作のための標準ライブラリ","metadata":{"execution":{"iopub.status.busy":"2024-07-19T07:34:27.589511Z","iopub.execute_input":"2024-07-19T07:34:27.589864Z","iopub.status.idle":"2024-07-19T07:34:32.716330Z","shell.execute_reply.started":"2024-07-19T07:34:27.589837Z","shell.execute_reply":"2024-07-19T07:34:32.715573Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## ハイパーパラメータの設定","metadata":{}},{"cell_type":"code","source":"batch_size = 64 # バッチサイズ。トレーニング時に同時に処理するサンプル数。大きすぎるとメモリ不足になる可能性があり、小さすぎるとトレーニングが遅くなる。\nlearning_rate = 1e-4 # 学習率。モデルのパラメータを更新する際のステップの大きさを決定する。小さすぎると収束が遅くなり、大きすぎると収束しない可能性がある。\nnum_epochs = 10 # エポック数。データセット全体を何回繰り返して学習するかを示す。エポック数を増やすとモデルがより多く学習するが、過学習のリスクもある。\nimg_size = 128 # 画像サイズ。入力画像をこのサイズにリサイズする。元の画像がこのサイズにリサイズされてモデルに入力される。","metadata":{"execution":{"iopub.status.busy":"2024-07-19T07:34:32.718012Z","iopub.execute_input":"2024-07-19T07:34:32.718428Z","iopub.status.idle":"2024-07-19T07:34:32.722994Z","shell.execute_reply.started":"2024-07-19T07:34:32.718395Z","shell.execute_reply":"2024-07-19T07:34:32.722157Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## ランダムシードの設定","metadata":{}},{"cell_type":"code","source":"random_seed = 42# 任意のランダムシードを選ぶ\nrandom.seed(random_seed)# Pythonの標準ランダムシードを設定\nnp.random.seed(random_seed)# NumPyのランダムシードを設定\nos.environ[\"PYTHONHASHSEED\"] = str(random_seed)# Pythonのハッシュシードを設定\n\n# PyTorchのランダムシードを設定\ntorch.manual_seed(random_seed)# PyTorchのCPUランダムシードを設定\ntorch.cuda.manual_seed(random_seed)# PyTorchのGPUランダムシードを設定\ntorch.cuda.manual_seed_all(random_seed)# すべてのGPUのランダムシードを設定\n\n# cuDNNの設定を変更して再現性を確保\ntorch.backends.cudnn.deterministic = True # cuDNNを用いた演算の再現性を確保\ntorch.backends.cudnn.benchmark = False # cuDNNを用いた演算の再現性を確保\ntorch.use_deterministic_algorithms(False) # PyTorchによる演算において決定論的なアルゴリズムを使用しない","metadata":{"execution":{"iopub.status.busy":"2024-07-19T07:34:32.724187Z","iopub.execute_input":"2024-07-19T07:34:32.724458Z","iopub.status.idle":"2024-07-19T07:34:32.741495Z","shell.execute_reply.started":"2024-07-19T07:34:32.724434Z","shell.execute_reply":"2024-07-19T07:34:32.740698Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## CSVファイルの読み込み","metadata":{}},{"cell_type":"code","source":"# Read csv\nsample = pd.read_csv('/kaggle/input/cifar-10/sampleSubmission.csv')","metadata":{"execution":{"iopub.status.busy":"2024-07-19T07:34:32.742610Z","iopub.execute_input":"2024-07-19T07:34:32.742890Z","iopub.status.idle":"2024-07-19T07:34:32.893340Z","shell.execute_reply.started":"2024-07-19T07:34:32.742866Z","shell.execute_reply":"2024-07-19T07:34:32.892540Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## データの形式を確認するための先頭から10行を表示","metadata":{}},{"cell_type":"code","source":"# Check the form of data\nsample.head(10)","metadata":{"execution":{"iopub.status.busy":"2024-07-19T07:34:32.896003Z","iopub.execute_input":"2024-07-19T07:34:32.896320Z","iopub.status.idle":"2024-07-19T07:34:32.914864Z","shell.execute_reply.started":"2024-07-19T07:34:32.896290Z","shell.execute_reply":"2024-07-19T07:34:32.913889Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 前処理","metadata":{}},{"cell_type":"code","source":"# 訓練データ用の前処理パイプライン\ntrain_transforms = transforms.Compose(\n    [\n        transforms.Resize(img_size),# 画像のリサイズ\n        transforms.RandomRotation(20), # ランダムに画像を回転させる\n        transforms.RandomHorizontalFlip(0.5), # ランダムに画像を水平方向に反転させる\n        transforms.ToTensor(), # テンソルに変換\n        transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),# 正規化\n    ]\n)\n# テストデータ用の前処理パイプライン\ntest_transforms = transforms.Compose(\n    [\n        transforms.Resize(img_size),# 画像のリサイズ\n        transforms.ToTensor(),# テンソルに変換\n        transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),# 正規化\n    ]\n)\n","metadata":{"execution":{"iopub.status.busy":"2024-07-19T07:34:32.915985Z","iopub.execute_input":"2024-07-19T07:34:32.916293Z","iopub.status.idle":"2024-07-19T07:34:32.923675Z","shell.execute_reply.started":"2024-07-19T07:34:32.916267Z","shell.execute_reply":"2024-07-19T07:34:32.922590Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## データローダーの定義","metadata":{}},{"cell_type":"code","source":"# CIFAR-10データセットの読み込みとデータローダーの定義\ntrain_dataset = datasets.CIFAR10(root='./data', train=True, transform=train_transforms, download=True)\nvalid_dataset = datasets.CIFAR10(root='./data', train=False, transform=test_transforms, download=True)\n# 訓練データと検証データのデータローダーの定義\ntrain_loader = DataLoader(train_dataset, batch_size=batch_size, shuffle=False)\nvalid_loader = DataLoader(valid_dataset, batch_size=batch_size, shuffle=False)","metadata":{"execution":{"iopub.status.busy":"2024-07-19T07:34:32.924911Z","iopub.execute_input":"2024-07-19T07:34:32.925238Z","iopub.status.idle":"2024-07-19T07:34:54.550005Z","shell.execute_reply.started":"2024-07-19T07:34:32.925205Z","shell.execute_reply":"2024-07-19T07:34:54.549187Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train_dataset)# 訓練データセットの基本情報を表示","metadata":{"execution":{"iopub.status.busy":"2024-07-19T07:34:54.551082Z","iopub.execute_input":"2024-07-19T07:34:54.551381Z","iopub.status.idle":"2024-07-19T07:34:54.556324Z","shell.execute_reply.started":"2024-07-19T07:34:54.551355Z","shell.execute_reply":"2024-07-19T07:34:54.555290Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(valid_dataset)# 検証データセットの基本情報を表示","metadata":{"execution":{"iopub.status.busy":"2024-07-19T07:34:54.557435Z","iopub.execute_input":"2024-07-19T07:34:54.557726Z","iopub.status.idle":"2024-07-19T07:34:54.565646Z","shell.execute_reply.started":"2024-07-19T07:34:54.557702Z","shell.execute_reply":"2024-07-19T07:34:54.564812Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from torchvision import datasets, transforms\nimport torchvision\nimport matplotlib.pyplot as plt\n\n# CIFAR-10データセットの読み込みと前処理の定義\ntrain_dataset_show = datasets.CIFAR10(root='./data', train=True, transform=train_transforms, download=True)\n\n# データローダーの作成（バッチサイズを4に設定し、シャッフルする）\ntrain_loader_show = torch.utils.data.DataLoader(train_dataset_show, batch_size=4, shuffle=True)\n\n# 画像を表示する関数の定義\ndef imshow(img):\n    img = img / 2 + 0.5  # 逆正規化\n    npimg = img.numpy()\n    plt.imshow(np.transpose(npimg, (1, 2, 0)))\n    plt.show()\n\n# データローダーからバッチを取得\ndataiter_show = iter(train_loader_show)\nimages_show, labels_show = next(dataiter_show)\n\n# 画像を表示\nimshow(torchvision.utils.make_grid(images_show))","metadata":{"execution":{"iopub.status.busy":"2024-07-19T07:34:54.566651Z","iopub.execute_input":"2024-07-19T07:34:54.566952Z","iopub.status.idle":"2024-07-19T07:34:55.862277Z","shell.execute_reply.started":"2024-07-19T07:34:54.566928Z","shell.execute_reply":"2024-07-19T07:34:55.861358Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch.nn.functional as F\nfrom functools import partial\n\nfrom timm.models.layers import DropPath, trunc_normal_\n\nclass Mlp(nn.Module):\n    def __init__(self, in_features, hidden_features=None, out_features=None, act_layer=nn.GELU, drop=0.):\n        super().__init__()\n        out_features = out_features or in_features\n        hidden_features = hidden_features or in_features\n        self.fc1 = nn.Linear(in_features, hidden_features)\n        self.act = act_layer()\n        self.fc2 = nn.Linear(hidden_features, out_features)\n        self.drop = nn.Dropout(drop)\n\n    def forward(self, x):\n        x = self.fc1(x)\n        x = self.act(x)\n        x = self.drop(x)\n        x = self.fc2(x)\n        x = self.drop(x)\n        return x\n\n\nclass Attention(nn.Module):\n    def __init__(self, dim, num_heads=8, qkv_bias=False, qk_scale=None, attn_drop=0., proj_drop=0.):\n        super().__init__()\n        self.num_heads = num_heads\n        head_dim = dim // num_heads\n        # NOTE scale factor was wrong in my original version, can set manually to be compat with prev weights\n        self.scale = qk_scale or head_dim ** -0.5\n\n        self.qkv = nn.Linear(dim, dim * 3, bias=qkv_bias)\n        self.attn_drop = nn.Dropout(attn_drop)\n        self.proj = nn.Linear(dim, dim)\n        self.proj_drop = nn.Dropout(proj_drop)\n\n    def forward(self, x):\n        B, N, C = x.shape\n        qkv = self.qkv(x).reshape(B, N, 3, self.num_heads, C // self.num_heads).permute(2, 0, 3, 1, 4)\n        q, k, v = qkv[0], qkv[1], qkv[2]  # make torchscript happy (cannot use tensor as tuple)\n\n        attn = (q @ k.transpose(-2, -1)) * self.scale\n        attn = attn.softmax(dim=-1)\n        attn = self.attn_drop(attn)\n\n        x = (attn @ v).transpose(1, 2).reshape(B, N, C)\n        x = self.proj(x)\n        x = self.proj_drop(x)\n        return x\n\n\nclass Block(nn.Module):\n    \n    def __init__(self, dim, num_heads, mlp_ratio=4., qkv_bias=False, qk_scale=None, drop=0., attn_drop=0.,\n                 drop_path=0., act_layer=nn.GELU, norm_layer=partial(nn.LayerNorm, eps=1e-6)):\n        super().__init__()\n        self.norm1 = norm_layer(dim)\n        self.attn = Attention(\n            dim, num_heads=num_heads, qkv_bias=qkv_bias, qk_scale=qk_scale, attn_drop=attn_drop, proj_drop=drop)\n        # NOTE: drop path for stochastic depth, we shall see if this is better than dropout here\n        self.drop_path = DropPath(drop_path) if drop_path > 0. else nn.Identity()\n        self.norm2 = norm_layer(dim)\n        mlp_hidden_dim = int(dim * mlp_ratio)\n        self.mlp = Mlp(in_features=dim, hidden_features=mlp_hidden_dim, act_layer=act_layer, drop=drop)\n        \n    def forward(self, x):\n        x = x + self.drop_path(self.attn(self.norm1(x)))\n        x = x + self.drop_path(self.mlp(self.norm2(x)))\n        return x\n    \nclass ConvBlock(nn.Module):\n    \n    def __init__(self, inplanes, outplanes, stride=1, res_conv=False, act_layer=nn.ReLU, groups=1,\n                 norm_layer=partial(nn.BatchNorm2d, eps=1e-6), drop_block=None, drop_path=None):\n        super(ConvBlock, self).__init__()\n        \n        expansion = 4\n        med_planes = outplanes // expansion\n        \n        self.conv1 = nn.Conv2d(inplanes, med_planes, kernel_size=1, stride=1, padding=0, bias=False)\n        self.bn1 = norm_layer(med_planes)\n        self.act1 = act_layer(inplace=True)\n        \n        self.conv2 = nn.Conv2d(med_planes, med_planes, kernel_size=3, stride=stride, groups=groups, padding=1, bias=False)\n        self.bn2 = norm_layer(med_planes)\n        self.act2 = act_layer(inplace=True)\n        \n        self.conv3 = nn.Conv2d(med_planes, outplanes, kernel_size=1, stride=1, padding=0, bias=False)\n        self.bn3 = norm_layer(outplanes)\n        self.act3 = act_layer(inplace=True)\n        \n        if res_conv:\n            self.residual_conv = nn.Conv2d(inplanes, outplanes, kernel_size=1, stride=stride, padding=0, bias=False)\n            self.residual_bn = norm_layer(outplanes)\n            \n        self.res_conv = res_conv\n        self.drop_block = drop_block\n        self.drop_path = drop_path\n        \n    def zero_init_last_bn(self):\n        nn.init.zeros_(self.bn3.weight)\n        \n    def forward(self, x, x_t=None, return_x_2=True):\n        residual = x\n        \n        x = self.conv1(x)\n        x = self.bn1(x)\n        if self.drop_block is not None:\n            x = self.drop_block(x)\n        x = self.act1(x)\n        \n        x = self.conv2(x) if x_t is None else self.conv2(x + x_t)\n        x = self.bn2(x)\n        if self.drop_block is not None:\n            x = self.drop_block(x)\n        x2 = self.act2(x)\n        \n        x = self.conv3(x2)\n        x = self.bn3(x)\n        if self.drop_block is not None:\n            x = self.drop_block(x)\n            \n        if self.drop_path is not None:\n            x = self.drop_path(x)\n            \n        if self.res_conv:\n            residual = self.residual_conv(residual)\n            residual = self.residual_bn(residual)\n            \n        x += residual\n        x = self.act3(x)\n        \n        if return_x_2:\n            return x, x2\n        else:\n            return x\n        \nclass FCUDown(nn.Module):\n    \"\"\" CNN feature maps -> Transformer patch embeddings\n    \"\"\"\n    \n    def __init__(self, inplanes, outplanes, dw_stride, act_layer=nn.GELU,\n                 norm_layer=partial(nn.LayerNorm, eps=1e-6)):\n        super(FCUDown, self).__init__()\n        self.dw_stride = dw_stride\n        \n        self.conv_project = nn.Conv2d(inplanes, outplanes, kernel_size=1, stride=1, padding=0)\n        self.sample_pooling = nn.AvgPool2d(kernel_size=dw_stride, stride=dw_stride)\n        \n        self.ln = norm_layer(outplanes)\n        self.act = act_layer()\n        \n    def forward(self, x, x_t):\n        x = self.conv_project(x)  # [N, C, H, W]\n        \n        x = self.sample_pooling(x).flatten(2).transpose(1, 2)\n        x = self.ln(x)\n        x = self.act(x)\n        \n        x = torch.cat([x_t[:, 0][:, None, :], x], dim=1)\n        \n        return x\n    \nclass FCUUp(nn.Module):\n    \"\"\" Transformer patch embeddings -> CNN feature maps\n    \"\"\"\n    \n    def __init__(self, inplanes, outplanes, up_stride, act_layer=nn.ReLU,\n                 norm_layer=partial(nn.BatchNorm2d, eps=1e-6),):\n        super(FCUUp, self).__init__()\n        \n        self.up_stride = up_stride\n        self.conv_project = nn.Conv2d(inplanes, outplanes, kernel_size=1, stride=1, padding=0)\n        self.bn = norm_layer(outplanes)\n        self.act = act_layer()\n        \n    def forward(self, x, H, W):\n        B, _, C = x.shape\n        # [N, 197, 384] -> [N, 196, 384] -> [N, 384, 196] -> [N, 384, 14, 14]\n        x_r = x[:, 1:].transpose(1, 2).reshape(B, C, H, W)\n        x_r = self.act(self.bn(self.conv_project(x_r)))\n        \n        return F.interpolate(x_r, size=(H * self.up_stride, W * self.up_stride))\n    \nclass Med_ConvBlock(nn.Module):\n    \"\"\" special case for Convblock with down sampling,\n    \"\"\"\n    def __init__(self, inplanes, act_layer=nn.ReLU, groups=1, norm_layer=partial(nn.BatchNorm2d, eps=1e-6),\n                 drop_block=None, drop_path=None):\n        \n        super(Med_ConvBlock, self).__init__()\n        \n        expansion = 4\n        med_planes = inplanes // expansion\n        \n        self.conv1 = nn.Conv2d(inplanes, med_planes, kernel_size=1, stride=1, padding=0, bias=False)\n        self.bn1 = norm_layer(med_planes)\n        self.act1 = act_layer(inplace=True)\n        \n        self.conv2 = nn.Conv2d(med_planes, med_planes, kernel_size=3, stride=1, groups=groups, padding=1, bias=False)\n        self.bn2 = norm_layer(med_planes)\n        self.act2 = act_layer(inplace=True)\n        \n        self.conv3 = nn.Conv2d(med_planes, inplanes, kernel_size=1, stride=1, padding=0, bias=False)\n        self.bn3 = norm_layer(inplanes)\n        self.act3 = act_layer(inplace=True)\n        \n        self.drop_block = drop_block\n        self.drop_path = drop_path\n        \n    def zero_init_last_bn(self):\n        nn.init.zeros_(self.bn3.weight)\n        \n    def forward(self, x):\n        residual = x\n        \n        x = self.conv1(x)\n        x = self.bn1(x)\n        if self.drop_block is not None:\n            x = self.drop_block(x)\n        x = self.act1(x)\n        \n        x = self.conv2(x)\n        x = self.bn2(x)\n        if self.drop_block is not None:\n            x = self.drop_block(x)\n        x = self.act2(x)\n        \n        x = self.conv3(x)\n        x = self.bn3(x)\n        if self.drop_block is not None:\n            x = self.drop_block(x)\n\n        if self.drop_path is not None:\n            x = self.drop_path(x)\n\n        x += residual\n        x = self.act3(x)\n\n        return x\n\n\nclass ConvTransBlock(nn.Module):\n    \"\"\"\n    Basic module for ConvTransformer, keep feature maps for CNN block and patch embeddings for transformer encoder block\n    \"\"\"\n\n    def __init__(self, inplanes, outplanes, res_conv, stride, dw_stride, embed_dim, num_heads=12, mlp_ratio=4.,\n                 qkv_bias=False, qk_scale=None, drop_rate=0., attn_drop_rate=0., drop_path_rate=0.,\n                 last_fusion=False, num_med_block=0, groups=1):\n\n        super(ConvTransBlock, self).__init__()\n        expansion = 4\n        self.cnn_block = ConvBlock(inplanes=inplanes, outplanes=outplanes, res_conv=res_conv, stride=stride, groups=groups)\n\n        if last_fusion:\n            self.fusion_block = ConvBlock(inplanes=outplanes, outplanes=outplanes, stride=2, res_conv=True, groups=groups)\n        else:\n            self.fusion_block = ConvBlock(inplanes=outplanes, outplanes=outplanes, groups=groups)\n\n        if num_med_block > 0:\n            self.med_block = []\n            for i in range(num_med_block):\n                self.med_block.append(Med_ConvBlock(inplanes=outplanes, groups=groups))\n            self.med_block = nn.ModuleList(self.med_block)\n\n        self.squeeze_block = FCUDown(inplanes=outplanes // expansion, outplanes=embed_dim, dw_stride=dw_stride)\n\n        self.expand_block = FCUUp(inplanes=embed_dim, outplanes=outplanes // expansion, up_stride=dw_stride)\n\n        self.trans_block = Block(\n            dim=embed_dim, num_heads=num_heads, mlp_ratio=mlp_ratio, qkv_bias=qkv_bias, qk_scale=qk_scale,\n            drop=drop_rate, attn_drop=attn_drop_rate, drop_path=drop_path_rate)\n\n        self.dw_stride = dw_stride\n        self.embed_dim = embed_dim\n        self.num_med_block = num_med_block\n        self.last_fusion = last_fusion\n\n    def forward(self, x, x_t):\n        x, x2 = self.cnn_block(x)\n\n        _, _, H, W = x2.shape\n\n        x_st = self.squeeze_block(x2, x_t)\n\n        x_t = self.trans_block(x_st + x_t)\n\n        if self.num_med_block > 0:\n            for m in self.med_block:\n                x = m(x)\n\n        x_t_r = self.expand_block(x_t, H // self.dw_stride, W // self.dw_stride)\n        x = self.fusion_block(x, x_t_r, return_x_2=False)\n\n        return x, x_t\n\n\nclass Conformer(nn.Module):\n\n    def __init__(self, patch_size=16, in_chans=3, num_classes=0, base_channel=64, channel_ratio=4, num_med_block=0,\n                 embed_dim=384, depth=12, num_heads=6, mlp_ratio=4., qkv_bias=False, qk_scale=None,\n                 drop_rate=0., attn_drop_rate=0., drop_path_rate=0.):\n\n        # Transformer\n        super().__init__()\n        self.num_classes = num_classes\n        self.num_features = self.embed_dim = embed_dim  # num_features for consistency with other models\n        assert depth % 3 == 0\n\n        self.cls_token = nn.Parameter(torch.zeros(1, 1, embed_dim))\n        self.trans_dpr = [x.item() for x in torch.linspace(0, drop_path_rate, depth)]  # stochastic depth decay rule\n\n        # Classifier head\n        self.trans_norm = nn.LayerNorm(embed_dim)\n        self.trans_cls_head = nn.Linear(embed_dim, num_classes) if num_classes > 0 else nn.Identity()\n        self.pooling = nn.AdaptiveAvgPool2d(1)\n        self.conv_cls_head = nn.Linear(int(256 * channel_ratio), num_classes)\n\n        # Stem stage: get the feature maps by conv block (copied form resnet.py)\n        self.conv1 = nn.Conv2d(in_chans, 64, kernel_size=7, stride=2, padding=3, bias=False)  # 1 / 2 [112, 112]\n        self.bn1 = nn.BatchNorm2d(64)\n        self.act1 = nn.ReLU(inplace=True)\n        self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)  # 1 / 4 [56, 56]\n\n        # 1 stage\n        stage_1_channel = int(base_channel * channel_ratio)\n        trans_dw_stride = patch_size // 4\n        self.conv_1 = ConvBlock(inplanes=64, outplanes=stage_1_channel, res_conv=True, stride=1)\n        self.trans_patch_conv = nn.Conv2d(64, embed_dim, kernel_size=trans_dw_stride, stride=trans_dw_stride, padding=0)\n        self.trans_1 = Block(dim=embed_dim, num_heads=num_heads, mlp_ratio=mlp_ratio, qkv_bias=qkv_bias,\n                             qk_scale=qk_scale, drop=drop_rate, attn_drop=attn_drop_rate, drop_path=self.trans_dpr[0],\n                             )\n\n        # 2~4 stage\n        init_stage = 2\n        fin_stage = depth // 3 + 1\n        for i in range(init_stage, fin_stage):\n            self.add_module('conv_trans_' + str(i),\n                    ConvTransBlock(\n                        stage_1_channel, stage_1_channel, False, 1, dw_stride=trans_dw_stride, embed_dim=embed_dim,\n                        num_heads=num_heads, mlp_ratio=mlp_ratio, qkv_bias=qkv_bias, qk_scale=qk_scale,\n                        drop_rate=drop_rate, attn_drop_rate=attn_drop_rate, drop_path_rate=self.trans_dpr[i-1],\n                        num_med_block=num_med_block\n                    )\n            )\n\n\n        stage_2_channel = int(base_channel * channel_ratio * 2)\n        # 5~8 stage\n        init_stage = fin_stage # 5\n        fin_stage = fin_stage + depth // 3 # 9\n        for i in range(init_stage, fin_stage):\n            s = 2 if i == init_stage else 1\n            in_channel = stage_1_channel if i == init_stage else stage_2_channel\n            res_conv = True if i == init_stage else False\n            self.add_module('conv_trans_' + str(i),\n                    ConvTransBlock(\n                        in_channel, stage_2_channel, res_conv, s, dw_stride=trans_dw_stride // 2, embed_dim=embed_dim,\n                        num_heads=num_heads, mlp_ratio=mlp_ratio, qkv_bias=qkv_bias, qk_scale=qk_scale,\n                        drop_rate=drop_rate, attn_drop_rate=attn_drop_rate, drop_path_rate=self.trans_dpr[i-1],\n                        num_med_block=num_med_block\n                    )\n            )\n\n        stage_3_channel = int(base_channel * channel_ratio * 2 * 2)\n        # 9~12 stage\n        init_stage = fin_stage  # 9\n        fin_stage = fin_stage + depth // 3  # 13\n        for i in range(init_stage, fin_stage):\n            s = 2 if i == init_stage else 1\n            in_channel = stage_2_channel if i == init_stage else stage_3_channel\n            res_conv = True if i == init_stage else False\n            last_fusion = True if i == depth else False\n            self.add_module('conv_trans_' + str(i),\n                    ConvTransBlock(\n                        in_channel, stage_3_channel, res_conv, s, dw_stride=trans_dw_stride // 4, embed_dim=embed_dim,\n                        num_heads=num_heads, mlp_ratio=mlp_ratio, qkv_bias=qkv_bias, qk_scale=qk_scale,\n                        drop_rate=drop_rate, attn_drop_rate=attn_drop_rate, drop_path_rate=self.trans_dpr[i-1],\n                        num_med_block=num_med_block, last_fusion=last_fusion\n                    )\n            )\n        self.fin_stage = fin_stage\n\n        trunc_normal_(self.cls_token, std=.02)\n\n        self.apply(self._init_weights)\n\n    def _init_weights(self, m):\n        if isinstance(m, nn.Linear):\n            trunc_normal_(m.weight, std=.02)\n            if isinstance(m, nn.Linear) and m.bias is not None:\n                nn.init.constant_(m.bias, 0)\n        elif isinstance(m, nn.LayerNorm):\n            nn.init.constant_(m.bias, 0)\n            nn.init.constant_(m.weight, 1.0)\n        elif isinstance(m, nn.Conv2d):\n            nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu')\n        elif isinstance(m, nn.BatchNorm2d):\n            nn.init.constant_(m.weight, 1.)\n            nn.init.constant_(m.bias, 0.)\n        elif isinstance(m, nn.GroupNorm):\n            nn.init.constant_(m.weight, 1.)\n            nn.init.constant_(m.bias, 0.)\n\n    @torch.jit.ignore\n    def no_weight_decay(self):\n        return {'cls_token'}\n\n\n    def forward(self, x):\n        B = x.shape[0]\n        cls_tokens = self.cls_token.expand(B, -1, -1)\n\n        # pdb.set_trace()\n        # stem stage [N, 3, 224, 224] -> [N, 64, 56, 56]\n        x_base = self.maxpool(self.act1(self.bn1(self.conv1(x))))\n\n        # 1 stage\n        x = self.conv_1(x_base, return_x_2=False)\n\n        x_t = self.trans_patch_conv(x_base).flatten(2).transpose(1, 2)\n        x_t = torch.cat([cls_tokens, x_t], dim=1)\n        x_t = self.trans_1(x_t)\n        \n        # 2 ~ final \n        for i in range(2, self.fin_stage):\n            x, x_t = eval('self.conv_trans_' + str(i))(x, x_t)\n\n        # conv classification\n        x_p = self.pooling(x).flatten(1)\n        conv_cls = self.conv_cls_head(x_p)\n\n        # trans classification\n        x_t = self.trans_norm(x_t)\n        tran_cls = self.trans_cls_head(x_t[:, 0])\n        return conv_cls + tran_cls\n","metadata":{"execution":{"iopub.status.busy":"2024-07-19T07:34:55.863624Z","iopub.execute_input":"2024-07-19T07:34:55.863919Z","iopub.status.idle":"2024-07-19T07:34:57.046976Z","shell.execute_reply.started":"2024-07-19T07:34:55.863892Z","shell.execute_reply":"2024-07-19T07:34:57.046057Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch.nn.functional as F\nfrom functools import partial\n\nfrom timm.models.layers import DropPath, trunc_normal_\n\nclass Mlp(nn.Module):\n    def __init__(self, in_features, hidden_features=None, out_features=None, act_layer=nn.GELU, drop=0.):\n        super().__init__()\n        out_features = out_features or in_features\n        hidden_features = hidden_features or in_features\n        self.fc1 = nn.Linear(in_features, hidden_features)\n        self.act = act_layer()\n        self.fc2 = nn.Linear(hidden_features, out_features)\n        self.drop = nn.Dropout(drop)\n\n    def forward(self, x):\n        x = self.fc1(x)\n        x = self.act(x)\n        x = self.drop(x)\n        x = self.fc2(x)\n        x = self.drop(x)\n        return x\n\n\nclass Attention(nn.Module):\n    def __init__(self, dim, num_heads=8, qkv_bias=False, qk_scale=None, attn_drop=0., proj_drop=0.):\n        super().__init__()\n        self.num_heads = num_heads\n        head_dim = dim // num_heads\n        # NOTE scale factor was wrong in my original version, can set manually to be compat with prev weights\n        self.scale = qk_scale or head_dim ** -0.5\n\n        self.qkv = nn.Linear(dim, dim * 3, bias=qkv_bias)\n        self.attn_drop = nn.Dropout(attn_drop)\n        self.proj = nn.Linear(dim, dim)\n        self.proj_drop = nn.Dropout(proj_drop)\n\n    def forward(self, x):\n        B, N, C = x.shape\n        qkv = self.qkv(x).reshape(B, N, 3, self.num_heads, C // self.num_heads).permute(2, 0, 3, 1, 4)\n        q, k, v = qkv[0], qkv[1], qkv[2]  # make torchscript happy (cannot use tensor as tuple)\n\n        attn = (q @ k.transpose(-2, -1)) * self.scale\n        attn = attn.softmax(dim=-1)\n        attn = self.attn_drop(attn)\n\n        x = (attn @ v).transpose(1, 2).reshape(B, N, C)\n        x = self.proj(x)\n        x = self.proj_drop(x)\n        return x\n\n\nclass Block(nn.Module):\n    \n    def __init__(self, dim, num_heads, mlp_ratio=4., qkv_bias=False, qk_scale=None, drop=0., attn_drop=0.,\n                 drop_path=0., act_layer=nn.GELU, norm_layer=partial(nn.LayerNorm, eps=1e-6)):\n        super().__init__()\n        self.norm1 = norm_layer(dim)\n        self.attn = Attention(\n            dim, num_heads=num_heads, qkv_bias=qkv_bias, qk_scale=qk_scale, attn_drop=attn_drop, proj_drop=drop)\n        # NOTE: drop path for stochastic depth, we shall see if this is better than dropout here\n        self.drop_path = DropPath(drop_path) if drop_path > 0. else nn.Identity()\n        self.norm2 = norm_layer(dim)\n        mlp_hidden_dim = int(dim * mlp_ratio)\n        self.mlp = Mlp(in_features=dim, hidden_features=mlp_hidden_dim, act_layer=act_layer, drop=drop)\n        \n    def forward(self, x):\n        x = x + self.drop_path(self.attn(self.norm1(x)))\n        x = x + self.drop_path(self.mlp(self.norm2(x)))\n        return x\n\nclass ViT(nn.Module):\n\n    def __init__(self, patch_size=16, in_chans=3, num_classes=0, base_channel=64, channel_ratio=4, num_med_block=0,\n                 embed_dim=384, depth=12, num_heads=6, mlp_ratio=4., qkv_bias=False, qk_scale=None,\n                 drop_rate=0., attn_drop_rate=0., drop_path_rate=0.):\n\n        # Transformer\n        super().__init__()\n        self.num_classes = num_classes\n        self.num_features = self.embed_dim = embed_dim  # num_features for consistency with other models\n        assert depth % 3 == 0\n\n        self.cls_token = nn.Parameter(torch.zeros(1, 1, embed_dim))\n        self.trans_dpr = [x.item() for x in torch.linspace(0, drop_path_rate, depth)]  # stochastic depth decay rule\n\n        # Classifier head\n        self.trans_norm = nn.LayerNorm(embed_dim)\n        self.trans_cls_head = nn.Linear(embed_dim, num_classes) if num_classes > 0 else nn.Identity()\n        self.pooling = nn.AdaptiveAvgPool2d(1)\n        #self.conv_cls_head = nn.Linear(int(256 * channel_ratio), num_classes)\n\n        # Stem stage: get the feature maps by conv block (copied form resnet.py)\n        self.conv1 = nn.Conv2d(in_chans, 64, kernel_size=7, stride=2, padding=3, bias=False)  # 1 / 2 [112, 112]\n        self.bn1 = nn.BatchNorm2d(64)\n        self.act1 = nn.ReLU(inplace=True)\n        self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)  # 1 / 4 [56, 56]\n\n        # 1 stage\n        stage_1_channel = int(base_channel * channel_ratio)\n        trans_dw_stride = patch_size // 4\n        self.conv_1 = ConvBlock(inplanes=64, outplanes=stage_1_channel, res_conv=True, stride=1)\n        self.trans_patch_conv = nn.Conv2d(64, embed_dim, kernel_size=trans_dw_stride, stride=trans_dw_stride, padding=0)\n        self.trans_1 = Block(dim=embed_dim, num_heads=num_heads, mlp_ratio=mlp_ratio, qkv_bias=qkv_bias,\n                             qk_scale=qk_scale, drop=drop_rate, attn_drop=attn_drop_rate, drop_path=self.trans_dpr[0],\n                             )\n\n        # 2~4 stage\n        init_stage = 2\n        fin_stage = depth // 3 + 1\n        for i in range(init_stage, fin_stage):\n            self.add_module('trans_' + str(i),\n                    Block(dim=embed_dim, num_heads=num_heads, mlp_ratio=mlp_ratio, qkv_bias=qkv_bias,\n                             qk_scale=qk_scale, drop=drop_rate, attn_drop=attn_drop_rate, drop_path=self.trans_dpr[0],\n                       )\n            )\n\n\n        stage_2_channel = int(base_channel * channel_ratio * 2)\n        # 5~8 stage\n        init_stage = fin_stage # 5\n        fin_stage = fin_stage + depth // 3 # 9\n        for i in range(init_stage, fin_stage):\n            s = 2 if i == init_stage else 1\n            in_channel = stage_1_channel if i == init_stage else stage_2_channel\n            res_conv = True if i == init_stage else False\n            self.add_module('trans_' + str(i),\n                    Block(dim=embed_dim, num_heads=num_heads, mlp_ratio=mlp_ratio, qkv_bias=qkv_bias,\n                             qk_scale=qk_scale, drop=drop_rate, attn_drop=attn_drop_rate, drop_path=self.trans_dpr[0],\n                       )\n            )\n\n        stage_3_channel = int(base_channel * channel_ratio * 2 * 2)\n        # 9~12 stage\n        init_stage = fin_stage  # 9\n        fin_stage = fin_stage + depth // 3  # 13\n        for i in range(init_stage, fin_stage):\n            s = 2 if i == init_stage else 1\n            in_channel = stage_2_channel if i == init_stage else stage_3_channel\n            res_conv = True if i == init_stage else False\n            last_fusion = True if i == depth else False\n            self.add_module('trans_' + str(i),\n                    Block(dim=embed_dim, num_heads=num_heads, mlp_ratio=mlp_ratio, qkv_bias=qkv_bias,\n                             qk_scale=qk_scale, drop=drop_rate, attn_drop=attn_drop_rate, drop_path=self.trans_dpr[0],\n                       )\n            )\n        self.fin_stage = fin_stage\n\n        trunc_normal_(self.cls_token, std=.02)\n\n        self.apply(self._init_weights)\n\n    def _init_weights(self, m):\n        if isinstance(m, nn.Linear):\n            trunc_normal_(m.weight, std=.02)\n            if isinstance(m, nn.Linear) and m.bias is not None:\n                nn.init.constant_(m.bias, 0)\n        elif isinstance(m, nn.LayerNorm):\n            nn.init.constant_(m.bias, 0)\n            nn.init.constant_(m.weight, 1.0)\n        elif isinstance(m, nn.Conv2d):\n            nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu')\n        elif isinstance(m, nn.BatchNorm2d):\n            nn.init.constant_(m.weight, 1.)\n            nn.init.constant_(m.bias, 0.)\n        elif isinstance(m, nn.GroupNorm):\n            nn.init.constant_(m.weight, 1.)\n            nn.init.constant_(m.bias, 0.)\n\n    @torch.jit.ignore\n    def no_weight_decay(self):\n        return {'cls_token'}\n\n\n    def forward(self, x):\n        B = x.shape[0]\n        cls_tokens = self.cls_token.expand(B, -1, -1)\n\n        # pdb.set_trace()\n        # stem stage [N, 3, 224, 224] -> [N, 64, 56, 56]\n        x_base = self.maxpool(self.act1(self.bn1(self.conv1(x))))\n\n        # 1 stage\n        x = self.conv_1(x_base, return_x_2=False)\n\n        x_t = self.trans_patch_conv(x_base).flatten(2).transpose(1, 2)\n        x_t = torch.cat([cls_tokens, x_t], dim=1)\n        x_t = self.trans_1(x_t)\n        \n        # 2 ~ final \n        for i in range(2, self.fin_stage):\n            x_t = eval('self.trans_' + str(i))(x_t)\n\n        # conv classification\n        #x_p = self.pooling(x).flatten(1)\n        #conv_cls = self.conv_cls_head(x_p)\n\n        # trans classification\n        x_t = self.trans_norm(x_t)\n        tran_cls = self.trans_cls_head(x_t[:, 0])\n        return tran_cls\n","metadata":{"execution":{"iopub.status.busy":"2024-07-19T07:34:57.048245Z","iopub.execute_input":"2024-07-19T07:34:57.048574Z","iopub.status.idle":"2024-07-19T07:34:57.091459Z","shell.execute_reply.started":"2024-07-19T07:34:57.048534Z","shell.execute_reply":"2024-07-19T07:34:57.090298Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# GPUが利用可能であればCUDAを使用し、そうでなければCPUを使用\ndevice = 'cuda' if torch.cuda.is_available() else 'cpu'","metadata":{"execution":{"iopub.status.busy":"2024-07-19T07:34:57.092713Z","iopub.execute_input":"2024-07-19T07:34:57.093073Z","iopub.status.idle":"2024-07-19T07:34:57.123803Z","shell.execute_reply.started":"2024-07-19T07:34:57.093033Z","shell.execute_reply":"2024-07-19T07:34:57.122883Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## ViT or Conformer　モデルの選択","metadata":{}},{"cell_type":"code","source":"model = Conformer(num_classes=10).to(device)# モデルの定義とGPU/CPUへの配置\noptimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)# オプティマイザの定義\ncriterion = nn.CrossEntropyLoss().to(device)# 損失関数の定義とGPU/CPUへの配置\nscheduler = torch.optim.lr_scheduler.StepLR(optimizer, step_size=5, gamma=0.5)# スケジューラの定義","metadata":{"execution":{"iopub.status.busy":"2024-07-19T07:34:57.127571Z","iopub.execute_input":"2024-07-19T07:34:57.127991Z","iopub.status.idle":"2024-07-19T07:34:58.101456Z","shell.execute_reply.started":"2024-07-19T07:34:57.127964Z","shell.execute_reply":"2024-07-19T07:34:58.100677Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# ベストなエポックと正解率を記録するための初期化\nbest_epoch = 0\nbest_acc = 0.0\n\n# エポックごとのループ\nfor epoch in range(1, num_epochs + 1):\n    print(\"-\" * 40)\n    print(f\"Epoch: {epoch}\")\n    \n    # 訓練フェーズ\n    epoch_loss = 0.0\n    epoch_corrects = 0\n    model.train()# モデルを訓練モードに設定\n    for batch_in, batch_out in tqdm(train_loader):\n        batch_in = batch_in.to(device)\n        batch_out = batch_out.to(device)\n        \n        # モデルによる予測と損失の計算\n        y_pred = model(batch_in)\n        preds = torch.argmax(y_pred, 1)\n        loss = criterion(y_pred, batch_out)\n        \n        # 勾配の初期化、逆伝播、パラメータの更新\n        optimizer.zero_grad()\n        loss.backward()\n        optimizer.step()\n    \n        # エポック全体の損失と正解数の累積\n        epoch_loss += loss.item() * batch_in.size(0)\n        epoch_corrects += torch.sum(preds == batch_out.data)\n    \n    # エポックの平均損失と正解率の計算\n    epoch_loss = epoch_loss / len(train_loader.dataset)\n    epoch_acc = epoch_corrects / len(train_loader.dataset)\n\n    print(f\"Train loss: {epoch_loss:.4f}\")\n    print(f\"Train acc: {epoch_acc:.4f}\")\n\n    # 検証フェーズ\n    with torch.no_grad():\n        epoch_loss = 0.0\n        epoch_corrects = 0\n        model.eval()# モデルを評価モードに設定\n        for batch_in, batch_out in tqdm(valid_loader):\n            batch_in = batch_in.to(device)\n            batch_out = batch_out.to(device)\n            \n            # モデルによる予測と損失の計算\n            y_pred = model(batch_in)\n            preds = torch.argmax(y_pred, 1)\n            loss = criterion(y_pred, batch_out)\n            \n            # 検証データセット全体の損失と正解数の累積\n            epoch_loss += loss.item() * batch_in.size(0)\n            epoch_corrects += torch.sum(preds == batch_out.data)\n    \n    # エポックの平均損失と正解率の計算\n    epoch_loss = epoch_loss / len(valid_loader.dataset)\n    epoch_acc = epoch_corrects / len(valid_loader.dataset)\n    \n    # スケジューラの更新\n    scheduler.step()\n    \n    # 検証の結果を表示\n    print(f\"Valid loss: {epoch_loss:.4f}\")\n    print(f\"Valid acc: {epoch_acc:.4f}\")\n    \n    # 最良の精度を更新した場合、モデルを保存\n    if epoch_acc >= best_acc:\n        best_epoch = epoch\n        best_acc = epoch_acc\n        torch.save(model, \"./model.pt\")\n    \n    # 現在のベストのエポックと正解率を表示\n    print(f\"Best epoch: {best_epoch}\")\n    print(f\"Best acc: {best_acc:.4f}\")\n    print(\"-\" * 40)","metadata":{"execution":{"iopub.status.busy":"2024-07-19T07:34:58.102627Z","iopub.execute_input":"2024-07-19T07:34:58.102925Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# py7zrライブラリのインストール\n!pip install py7zr\n\nimport py7zr\n\n# 7z形式のファイルを解凍する\nwith py7zr.SevenZipFile('/kaggle/input/cifar-10/test.7z', mode='r') as z:\n    z.extractall('/kaggle/working/')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Make a list of data filenames for dataset\nimport os\n\n# 指定ディレクトリ内のファイルパスを取得し、数値順にソートする\ntest_filenames = [os.path.join('/kaggle/working/test', f) for f in os.listdir('/kaggle/working/test')]\ntest_filenames.sort(key=lambda x: int(x.split('/')[-1].split('.')[0]))\n\n# test_filenamesには、'/kaggle/working/test' ディレクトリ内のファイルのパスが数値順にソートされて格納されています","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Define dataset\nclass CIFAR10(Dataset):\n    def __init__(self, filenames, transforms):\n        self.filenames = filenames# ファイルパスのリスト\n        self.transforms = transforms# 前処理パイプライン\n        \n    def __len__(self):\n        return len(self.filenames)# データセットのサイズ（データ数）\n    \n    def __getitem__(self, idx):\n        img = Image.open(self.filenames[idx])# ファイルパスから画像を読み込み\n        transformed_img = self.transforms(img)# 画像に前処理を適用\n        return transformed_img\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# CIFAR10クラスを使用してテスト用データセットを定義\ntest_dataset = CIFAR10(test_filenames, test_transforms)\n\n# データセットをバッチサイズで分割してロードするデータローダーを定義\ntest_loader = DataLoader(test_dataset, batch_size=batch_size, shuffle=False)\n\n# test_loaderには、前処理済みのテスト用データがバッチごとに分割されてロードされます","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# For one-hot encoding\nclasses = ('airplane', \n           'automobile', \n           'bird',\n           'cat',\n           'deer',\n           'dog', \n           'frog', \n           'horse', \n           'ship',\n           'truck')\n\n# CIFAR-10データセットのクラスラベルに対応するクラス名のリストです。\n# それぞれのクラスラベルは、対応する物体のカテゴリを示しています。\n# 例えば、0番目のクラスラベルは'airplane'（飛行機）、1番目のクラスラベルは'automobile'（自動車）などです。\n# これらのクラス名は、モデルが出力するクラスインデックスを対応する物体のカテゴリ名に変換する際に使用されます。","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# ベストなモデルを読み込んでGPU/CPUに配置\nmodel = torch.load('./model.pt').to(device)\n\n# './model.pt'に保存されたモデルを読み込み、GPUが利用可能であればCUDAに配置し、そうでなければCPUに配置します。","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 結果を格納する空リストを作成\nresult = []\n\n# 推論フェーズ\nwith torch.no_grad():\n    model.eval()# モデルを評価モードに設定\n    for batch_in in tqdm(test_loader):\n        batch_in = batch_in.to(device)# バッチデータをGPUに送る\n        \n        y_pred = model(batch_in)# モデルによる推論\n        preds = torch.argmax(y_pred, 1)# 推論結果のクラスインデックスを取得\n        preds = preds.cpu().numpy()# 推論結果をCPUに移動してNumPy配列に変換\n        result.extend([classes[i] for i in preds])# クラスインデックスをクラス名に変換し、結果リストに追加\n        \n# 推論結果をDataFrameに格納し、CSVファイルとして保存\nsample['label'] = result# 'label'列に推論結果を格納\nsample.to_csv('./predict.csv', index=False)# 'predict.csv'としてCSVファイルとして保存\n\n# モデルによる推論結果をCSVファイルに保存しました。","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Remove test data\nimport shutil\n\n# '/kaggle/working/data'ディレクトリを削除\nshutil.rmtree('/kaggle/working/data')\n\n# '/kaggle/working/test'ディレクトリを削除\nshutil.rmtree('/kaggle/working/test')\n\nsample.head(10)\n# テストデータを削除しました。以降、これらのディレクトリにアクセスすることはできません。","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}