{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nfrom sklearn.model_selection import train_test_split \nfrom sklearn.preprocessing import OneHotEncoder\nimport torch\nfrom torch import nn\nimport torchvision\nfrom PIL import Image\nimport matplotlib.pyplot as plt\nfrom torch.utils.data import DataLoader, Dataset\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-02-10T16:31:32.051173Z","iopub.execute_input":"2023-02-10T16:31:32.051626Z","iopub.status.idle":"2023-02-10T16:31:32.066398Z","shell.execute_reply.started":"2023-02-10T16:31:32.051581Z","shell.execute_reply":"2023-02-10T16:31:32.065358Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')","metadata":{"execution":{"iopub.status.busy":"2023-02-10T16:31:33.054259Z","iopub.execute_input":"2023-02-10T16:31:33.054662Z","iopub.status.idle":"2023-02-10T16:31:33.059939Z","shell.execute_reply.started":"2023-02-10T16:31:33.054629Z","shell.execute_reply":"2023-02-10T16:31:33.058498Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pip install torch-summary","metadata":{"execution":{"iopub.status.busy":"2023-02-09T18:10:45.505822Z","iopub.execute_input":"2023-02-09T18:10:45.506981Z","iopub.status.idle":"2023-02-09T18:11:00.968569Z","shell.execute_reply.started":"2023-02-09T18:10:45.506938Z","shell.execute_reply":"2023-02-09T18:11:00.967319Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from torchsummary import summary #packet fo testing model","metadata":{"execution":{"iopub.status.busy":"2023-02-09T18:11:00.974100Z","iopub.execute_input":"2023-02-09T18:11:00.974819Z","iopub.status.idle":"2023-02-09T18:11:00.992710Z","shell.execute_reply.started":"2023-02-09T18:11:00.974775Z","shell.execute_reply":"2023-02-09T18:11:00.991489Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Load and preprocessing data","metadata":{}},{"cell_type":"code","source":"train_data = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ntest_data = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')","metadata":{"execution":{"iopub.status.busy":"2023-02-10T16:31:44.689010Z","iopub.execute_input":"2023-02-10T16:31:44.689796Z","iopub.status.idle":"2023-02-10T16:31:48.052829Z","shell.execute_reply.started":"2023-02-10T16:31:44.689756Z","shell.execute_reply":"2023-02-10T16:31:48.051820Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class MNIST_Dataset(Dataset):\n    def __init__(self, digits, labels, transforms):\n        self.digits = digits\n        self.labels = labels\n        self.transforms = transforms\n        \n    def __getitem__(self, idx):\n        label = self.labels[idx]\n        digit = self.digits[idx].reshape(1, 28, 28) \n        digit = torch.Tensor(digit)\n        digit = torch.cat([digit, digit, digit], dim=0)\n        digit = torchvision.transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225))(digit)\n        return (digit, label)\n    def __len__(self):\n        return len(self.labels)","metadata":{"execution":{"iopub.status.busy":"2023-02-10T16:31:48.054852Z","iopub.execute_input":"2023-02-10T16:31:48.055237Z","iopub.status.idle":"2023-02-10T16:31:48.064190Z","shell.execute_reply.started":"2023-02-10T16:31:48.055200Z","shell.execute_reply":"2023-02-10T16:31:48.062172Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def data_split_and_encode(df, transforms=None,  ohe=False):\n    X_train, X_test, y_train, y_test = train_test_split(train_data.drop(labels='label', axis=1).to_numpy(), \n                                                    train_data['label'], test_size=0.15)\n    dim_2d = X_train.shape[1]**0.5\n    if int(dim_2d) == dim_2d:\n        X_train = X_train.reshape(-1, 28, 28).astype(np.float32)\n        X_test = X_test.reshape(-1, 28, 28).astype(np.float32)\n    if ohe:\n        y_train = OneHotEncoder(sparse=False).fit_transform(y_train.to_numpy().reshape(-1,1))\n        y_test = OneHotEncoder(sparse=False).fit_transform(y_test.to_numpy().reshape(-1,1))\n\n    train_dataset = MNIST_Dataset(X_train, y_train, transforms)\n    test_dataset = MNIST_Dataset(X_test, y_test, transforms)\n    return train_dataset, test_dataset","metadata":{"execution":{"iopub.status.busy":"2023-02-10T16:31:48.829768Z","iopub.execute_input":"2023-02-10T16:31:48.830185Z","iopub.status.idle":"2023-02-10T16:31:48.838835Z","shell.execute_reply.started":"2023-02-10T16:31:48.830107Z","shell.execute_reply":"2023-02-10T16:31:48.837790Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset,test_dataset = data_split_and_encode(train_data, ohe=True)","metadata":{"execution":{"iopub.status.busy":"2023-02-10T16:31:50.246905Z","iopub.execute_input":"2023-02-10T16:31:50.247320Z","iopub.status.idle":"2023-02-10T16:31:50.762158Z","shell.execute_reply.started":"2023-02-10T16:31:50.247286Z","shell.execute_reply":"2023-02-10T16:31:50.760770Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_loader = DataLoader(train_dataset,batch_size=32, )\ntest_loader = DataLoader(test_dataset,batch_size=32)","metadata":{"execution":{"iopub.status.busy":"2023-02-10T16:31:52.683441Z","iopub.execute_input":"2023-02-10T16:31:52.683802Z","iopub.status.idle":"2023-02-10T16:31:52.691094Z","shell.execute_reply.started":"2023-02-10T16:31:52.683771Z","shell.execute_reply":"2023-02-10T16:31:52.689508Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# ResNet18","metadata":{}},{"cell_type":"code","source":"resnet = torchvision.models.resnet18(pretrained=True)","metadata":{"execution":{"iopub.status.busy":"2023-02-10T16:31:55.368206Z","iopub.execute_input":"2023-02-10T16:31:55.369137Z","iopub.status.idle":"2023-02-10T16:31:55.578992Z","shell.execute_reply.started":"2023-02-10T16:31:55.369070Z","shell.execute_reply":"2023-02-10T16:31:55.578044Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"resnet.fc = nn.Sequential(\n    nn.Linear(in_features=512, out_features=10, bias=True),\n    nn.Softmax(dim=1)\n)","metadata":{"execution":{"iopub.status.busy":"2023-02-10T16:31:58.656924Z","iopub.execute_input":"2023-02-10T16:31:58.657360Z","iopub.status.idle":"2023-02-10T16:31:58.664707Z","shell.execute_reply.started":"2023-02-10T16:31:58.657322Z","shell.execute_reply":"2023-02-10T16:31:58.663532Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"freezing = [resnet.conv1, resnet.layer1, resnet.layer2] #not freezing last 2 layers\nfor el in freezing:\n    for param in el.parameters():\n        param.requires_grad = False","metadata":{"execution":{"iopub.status.busy":"2023-02-10T16:32:04.914278Z","iopub.execute_input":"2023-02-10T16:32:04.915189Z","iopub.status.idle":"2023-02-10T16:32:04.921557Z","shell.execute_reply.started":"2023-02-10T16:32:04.915140Z","shell.execute_reply":"2023-02-10T16:32:04.920071Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def train_model(train_loader, test_loader, model, loss, opt, epochs=15):\n    model.history =  {\n            'score': {\n                'train': [],\n                'test': []\n                }\n            }\n    for epoch in range(epochs):\n        print(f'Start epoch - {epoch+1}')\n        model.train()\n        i = 0\n        score_train = []\n        score_test = []\n        \n        for X_train, y_train in train_loader:\n            X_train = X_train.type(torch.float).to(device)\n            y_train = y_train.to(device)\n            opt.zero_grad()\n            pred = model(X_train)\n            _loss = loss(y_train, pred)\n            score = [torch.argmax(target) == torch.argmax(pred_d) for target, pred_d in zip(y_train, pred)]\n            score = [el.cpu().numpy() for el in score]\n            _loss.backward()\n            opt.step()\n            score_train.append(sum(score)/len(score))\n\n        with torch.no_grad():\n            model.eval()\n            for X_test, y_test in test_loader:\n                i += 1\n                X_test = X_test.type(torch.float).to(device)\n                y_test = y_test.to(device)\n                pred_test = model(X_test)\n                score = [torch.argmax(target) == torch.argmax(pred_d) for target, pred_d in zip(y_test, pred_test)]\n                score = [el.cpu().numpy() for el in score]\n                score_test.append(sum(score)/len(score))\n\n                if i%100 == 0:\n                    random_digit_1 = np.random.randint(0, X_test.shape[0])\n                    random_digit_2 = np.random.randint(0, X_test.shape[0])\n                    plt.subplot(1,2,1)\n                    plt.imshow(np.rollaxis(X_test[random_digit_1].cpu().numpy(),0,3))\n                    plt.title(f'Predict {torch.argmax(pred_test[random_digit_1])}')\n                    plt.axis('off')\n                    plt.subplot(1,2,2)\n                    plt.imshow(np.rollaxis(X_test[random_digit_2].cpu().numpy(),0,3))\n                    plt.title(f'Predict {torch.argmax(pred_test[random_digit_2])}')\n                    plt.axis('off')\n                    plt.show()\n            \n        model.history['score']['train'].append(sum(score_train)/len(score_train))\n        model.history['score']['test'].append(sum(score_test)/len(score_test))\n        \n        print(f\"Accuracy train: {model.history['score']['train']} -- Accuracy test: {model.history['score']['test']}\")","metadata":{"execution":{"iopub.status.busy":"2023-02-10T16:32:05.837024Z","iopub.execute_input":"2023-02-10T16:32:05.837803Z","iopub.status.idle":"2023-02-10T16:32:05.853428Z","shell.execute_reply.started":"2023-02-10T16:32:05.837763Z","shell.execute_reply":"2023-02-10T16:32:05.852133Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"loss = nn.CrossEntropyLoss()\nopt = torch.optim.Adam(params=resnet.parameters(), lr=0.0004)","metadata":{"execution":{"iopub.status.busy":"2023-02-10T16:32:08.380916Z","iopub.execute_input":"2023-02-10T16:32:08.381316Z","iopub.status.idle":"2023-02-10T16:32:08.387576Z","shell.execute_reply.started":"2023-02-10T16:32:08.381286Z","shell.execute_reply":"2023-02-10T16:32:08.386173Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_model(train_loader, test_loader, resnet.to(device), loss, opt, epochs=20) #Training process","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(resnet.history['score']['train'])\nplt.plot(resnet.history['score']['test'])","metadata":{"execution":{"iopub.status.busy":"2023-02-10T17:44:40.641213Z","iopub.execute_input":"2023-02-10T17:44:40.641646Z","iopub.status.idle":"2023-02-10T17:44:40.872822Z","shell.execute_reply.started":"2023-02-10T17:44:40.641611Z","shell.execute_reply":"2023-02-10T17:44:40.872003Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Result 0.989","metadata":{}},{"cell_type":"markdown","source":"# ResNet50","metadata":{}},{"cell_type":"code","source":"resnet50 = torchvision.models.resnet50(pretrained=True)","metadata":{"execution":{"iopub.status.busy":"2023-02-10T17:05:20.234128Z","iopub.execute_input":"2023-02-10T17:05:20.234525Z","iopub.status.idle":"2023-02-10T17:05:20.701675Z","shell.execute_reply.started":"2023-02-10T17:05:20.234492Z","shell.execute_reply":"2023-02-10T17:05:20.700510Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"resnet50.fc = nn.Sequential(\n    nn.Linear(in_features=2048, out_features=10, bias=True),\n    nn.Softmax(dim=1)\n)","metadata":{"execution":{"iopub.status.busy":"2023-02-10T17:05:20.703579Z","iopub.execute_input":"2023-02-10T17:05:20.703982Z","iopub.status.idle":"2023-02-10T17:05:20.710230Z","shell.execute_reply.started":"2023-02-10T17:05:20.703951Z","shell.execute_reply":"2023-02-10T17:05:20.709236Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"freezing = [resnet50.conv1, resnet50.layer1, resnet50.layer2] #not freezing last 2 layers\nfor el in freezing:\n    for param in el.parameters():\n        param.requires_grad = False","metadata":{"execution":{"iopub.status.busy":"2023-02-10T17:05:21.693290Z","iopub.execute_input":"2023-02-10T17:05:21.693697Z","iopub.status.idle":"2023-02-10T17:05:21.700313Z","shell.execute_reply.started":"2023-02-10T17:05:21.693664Z","shell.execute_reply":"2023-02-10T17:05:21.699154Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"loss = nn.CrossEntropyLoss()\nopt = torch.optim.Adam(params=resnet50.parameters(), lr=0.0004)","metadata":{"execution":{"iopub.status.busy":"2023-02-10T17:05:24.211690Z","iopub.execute_input":"2023-02-10T17:05:24.212303Z","iopub.status.idle":"2023-02-10T17:05:24.219039Z","shell.execute_reply.started":"2023-02-10T17:05:24.212267Z","shell.execute_reply":"2023-02-10T17:05:24.217705Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_model(train_loader, test_loader, resnet50.to(device), loss, opt, epochs=10) #Training process","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(resnet50.history['score']['train'])\nplt.plot(resnet50.history['score']['test'])","metadata":{"execution":{"iopub.status.busy":"2023-02-10T17:44:01.256518Z","iopub.execute_input":"2023-02-10T17:44:01.256881Z","iopub.status.idle":"2023-02-10T17:44:01.450154Z","shell.execute_reply.started":"2023-02-10T17:44:01.256849Z","shell.execute_reply":"2023-02-10T17:44:01.449006Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Result -0.982","metadata":{}},{"cell_type":"code","source":"resnet50_2 = torchvision.models.resnet50(pretrained=True)","metadata":{"execution":{"iopub.status.busy":"2023-02-10T17:15:13.312723Z","iopub.execute_input":"2023-02-10T17:15:13.313088Z","iopub.status.idle":"2023-02-10T17:15:13.773851Z","shell.execute_reply.started":"2023-02-10T17:15:13.313057Z","shell.execute_reply":"2023-02-10T17:15:13.772881Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"resnet50_2.fc = nn.Sequential(\n    nn.Linear(in_features=2048, out_features=10, bias=True),\n    nn.Softmax(dim=1)\n)","metadata":{"execution":{"iopub.status.busy":"2023-02-10T17:15:14.558726Z","iopub.execute_input":"2023-02-10T17:15:14.559074Z","iopub.status.idle":"2023-02-10T17:15:14.565184Z","shell.execute_reply.started":"2023-02-10T17:15:14.559043Z","shell.execute_reply":"2023-02-10T17:15:14.564040Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"freezing = [resnet50_2.conv1, resnet50_2.layer1] #not freezing last 3 layers\nfor el in freezing:\n    for param in el.parameters():\n        param.requires_grad = False","metadata":{"execution":{"iopub.status.busy":"2023-02-10T17:15:16.888897Z","iopub.execute_input":"2023-02-10T17:15:16.889648Z","iopub.status.idle":"2023-02-10T17:15:16.895106Z","shell.execute_reply.started":"2023-02-10T17:15:16.889601Z","shell.execute_reply":"2023-02-10T17:15:16.894107Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"loss = nn.CrossEntropyLoss()\nopt = torch.optim.Adam(params=resnet50_2.parameters(), lr=0.0004)","metadata":{"execution":{"iopub.status.busy":"2023-02-10T17:15:19.927898Z","iopub.execute_input":"2023-02-10T17:15:19.928578Z","iopub.status.idle":"2023-02-10T17:15:19.935071Z","shell.execute_reply.started":"2023-02-10T17:15:19.928540Z","shell.execute_reply":"2023-02-10T17:15:19.933998Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_model(train_loader, test_loader, resnet50_2.to(device), loss, opt, epochs=10) #Training process","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(resnet50_2.history['score']['train'])\nplt.plot(resnet50_2.history['score']['test'])","metadata":{"execution":{"iopub.status.busy":"2023-02-10T17:44:18.011429Z","iopub.execute_input":"2023-02-10T17:44:18.011794Z","iopub.status.idle":"2023-02-10T17:44:18.201166Z","shell.execute_reply.started":"2023-02-10T17:44:18.011762Z","shell.execute_reply":"2023-02-10T17:44:18.200081Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Result - 0.976","metadata":{}},{"cell_type":"markdown","source":"# ResNet101","metadata":{}},{"cell_type":"code","source":"resnet101 = torchvision.models.resnet101(pretrained=True)","metadata":{"execution":{"iopub.status.busy":"2023-02-10T17:31:23.357607Z","iopub.execute_input":"2023-02-10T17:31:23.358147Z","iopub.status.idle":"2023-02-10T17:31:24.347355Z","shell.execute_reply.started":"2023-02-10T17:31:23.358092Z","shell.execute_reply":"2023-02-10T17:31:24.345705Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"resnet101.fc = nn.Sequential(\n    nn.Linear(in_features=2048, out_features=10, bias=True),\n    nn.Softmax(dim=1)\n)","metadata":{"execution":{"iopub.status.busy":"2023-02-10T17:31:25.046259Z","iopub.execute_input":"2023-02-10T17:31:25.047203Z","iopub.status.idle":"2023-02-10T17:31:25.065158Z","shell.execute_reply.started":"2023-02-10T17:31:25.047156Z","shell.execute_reply":"2023-02-10T17:31:25.064157Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"freezing = [resnet101.conv1, resnet101.layer1, resnet101.layer2, resnet101.layer3] #not freezing last 1 layer\nfor el in freezing:\n    for param in el.parameters():\n        param.requires_grad = False","metadata":{"execution":{"iopub.status.busy":"2023-02-10T17:31:28.188809Z","iopub.execute_input":"2023-02-10T17:31:28.189213Z","iopub.status.idle":"2023-02-10T17:31:28.196535Z","shell.execute_reply.started":"2023-02-10T17:31:28.189178Z","shell.execute_reply":"2023-02-10T17:31:28.195218Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"loss = nn.CrossEntropyLoss()\nopt = torch.optim.Adam(params=resnet101.parameters(), lr=0.0004)","metadata":{"execution":{"iopub.status.busy":"2023-02-10T17:31:29.752910Z","iopub.execute_input":"2023-02-10T17:31:29.754026Z","iopub.status.idle":"2023-02-10T17:31:29.761165Z","shell.execute_reply.started":"2023-02-10T17:31:29.753974Z","shell.execute_reply":"2023-02-10T17:31:29.760178Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_model(train_loader, test_loader, resnet101.to(device), loss, opt, epochs=10) #Training process","metadata":{"execution":{"iopub.status.busy":"2023-02-10T17:31:32.009975Z","iopub.execute_input":"2023-02-10T17:31:32.010729Z","iopub.status.idle":"2023-02-10T17:41:28.917025Z","shell.execute_reply.started":"2023-02-10T17:31:32.010691Z","shell.execute_reply":"2023-02-10T17:41:28.915913Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(resnet101.history['score']['train'])\nplt.plot(resnet101.history['score']['test'])","metadata":{"execution":{"iopub.status.busy":"2023-02-10T17:42:53.265834Z","iopub.execute_input":"2023-02-10T17:42:53.266217Z","iopub.status.idle":"2023-02-10T17:42:53.463283Z","shell.execute_reply.started":"2023-02-10T17:42:53.266185Z","shell.execute_reply":"2023-02-10T17:42:53.462621Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Result 0.95","metadata":{}},{"cell_type":"code","source":"resnet101_2 = torchvision.models.resnet101(pretrained=True)\nresnet101_2.fc = nn.Sequential(\n    nn.Linear(in_features=2048, out_features=10, bias=False),\n    nn.Softmax(dim=1)\n)\n","metadata":{"execution":{"iopub.status.busy":"2023-02-10T17:48:19.167166Z","iopub.execute_input":"2023-02-10T17:48:19.167836Z","iopub.status.idle":"2023-02-10T17:48:20.111965Z","shell.execute_reply.started":"2023-02-10T17:48:19.167800Z","shell.execute_reply":"2023-02-10T17:48:20.110890Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"freezing = [resnet101_2.conv1, resnet101_2.layer1, resnet101_2.layer2] #not freezing last 2 layers\nfor el in freezing:\n    for param in el.parameters():\n        param.requires_grad = False","metadata":{"execution":{"iopub.status.busy":"2023-02-10T17:48:44.110100Z","iopub.execute_input":"2023-02-10T17:48:44.110484Z","iopub.status.idle":"2023-02-10T17:48:44.115917Z","shell.execute_reply.started":"2023-02-10T17:48:44.110454Z","shell.execute_reply":"2023-02-10T17:48:44.114848Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"loss = nn.CrossEntropyLoss()\nopt = torch.optim.Adam(params=resnet101_2.parameters(), lr=0.0004)","metadata":{"execution":{"iopub.status.busy":"2023-02-10T17:48:49.342414Z","iopub.execute_input":"2023-02-10T17:48:49.342828Z","iopub.status.idle":"2023-02-10T17:48:49.352427Z","shell.execute_reply.started":"2023-02-10T17:48:49.342794Z","shell.execute_reply":"2023-02-10T17:48:49.351392Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_model(train_loader, test_loader, resnet101_2.to(device), loss, opt, epochs=10) #Training process","metadata":{"execution":{"iopub.status.busy":"2023-02-10T17:48:52.739845Z","iopub.execute_input":"2023-02-10T17:48:52.740219Z","iopub.status.idle":"2023-02-10T18:01:57.436195Z","shell.execute_reply.started":"2023-02-10T17:48:52.740185Z","shell.execute_reply":"2023-02-10T18:01:57.435168Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(resnet101.history['score']['train'])\nplt.plot(resnet101.history['score']['test'])","metadata":{"execution":{"iopub.status.busy":"2023-02-10T18:02:21.341865Z","iopub.execute_input":"2023-02-10T18:02:21.342267Z","iopub.status.idle":"2023-02-10T18:02:21.530057Z","shell.execute_reply.started":"2023-02-10T18:02:21.342233Z","shell.execute_reply":"2023-02-10T18:02:21.529166Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Result 0.984","metadata":{}},{"cell_type":"markdown","source":"**All ResNet models have same results. The best results were obtained with two frozen layers.**","metadata":{}}]}