{"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"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":3175,"databundleVersionId":44352,"sourceType":"competition"},{"sourceId":2530676,"sourceType":"datasetVersion","datasetId":1533506}],"dockerImageVersionId":30121,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Imports","metadata":{"id":"pXwHstOQ71Pw"}},{"cell_type":"code","source":"!pip install torchsummary\nimport os\nimport io\n\nimport copy\nimport torch\nimport random\nimport pathlib\nimport pandas as pd\nimport PIL\n\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport matplotlib.animation as animation\n\nimport torch.nn as nn\nimport torch.optim as optim\nimport torch.nn.functional as F\n\nfrom torchvision import datasets, transforms, utils\nfrom torchvision.datasets import ImageFolder\nfrom torch.optim.lr_scheduler import StepLR\nfrom torch.utils.data import Dataset, DataLoader\n\nfrom tqdm.auto import tqdm\nimport IPython.display as display","metadata":{"id":"f97C6r0K75sT","execution":{"iopub.status.busy":"2021-08-20T07:55:40.475548Z","iopub.execute_input":"2021-08-20T07:55:40.475915Z","iopub.status.idle":"2021-08-20T07:55:49.716931Z","shell.execute_reply.started":"2021-08-20T07:55:40.475833Z","shell.execute_reply":"2021-08-20T07:55:49.716061Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Hyperparameters","metadata":{"id":"5-jfyApydrnF"}},{"cell_type":"code","source":"BATCH_SIZE = 128\nNUM_EPOCHS = 6\nSPLIT = [0.7,0.2,0.1]\nLEARNING_RATE = 1e-3\nQUESTION_WEIGHTS = np.array([1.2, 1, 1.2, 2, 2.5, 1.1, 1.1, 1.4, 16, 4, 6, 8])\nCRITERION = torch.nn.MSELoss()","metadata":{"id":"VSHzBjbkdvlZ","execution":{"iopub.status.busy":"2021-08-20T11:07:19.385406Z","iopub.execute_input":"2021-08-20T11:07:19.385905Z","iopub.status.idle":"2021-08-20T11:07:19.393014Z","shell.execute_reply.started":"2021-08-20T11:07:19.385858Z","shell.execute_reply":"2021-08-20T11:07:19.392166Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Helper functions","metadata":{"id":"FxGqkc6L40kd"}},{"cell_type":"code","source":"def plot_train_val(x, train, val, train_label,\n                   val_label, title, y_label,\n                   color):\n\n  plt.plot(x, train, label=train_label, color=color)\n  plt.plot(x, val, label=val_label, color=color, linestyle='--')\n  plt.legend(loc='lower right')\n\n  plt.xlabel('epoch')\n  plt.ylabel(y_label)\n  plt.title(title)\n  plt.show()","metadata":{"id":"1y4uPtoQ5idk","execution":{"iopub.status.busy":"2021-08-20T07:55:49.729366Z","iopub.execute_input":"2021-08-20T07:55:49.729613Z","iopub.status.idle":"2021-08-20T07:55:49.735Z","shell.execute_reply.started":"2021-08-20T07:55:49.72959Z","shell.execute_reply":"2021-08-20T07:55:49.734225Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Showing images\ndef show_image(path):\n  display.Image(path)\n\n# Setting device\ndef set_device():\n  device = \"cuda\" if torch.cuda.is_available() else \"cpu\"\n  if device != \"cuda\":\n    print(\"WARNING: For this notebook to perform best, \"\n        \"if possible, in the menu under `Runtime` -> \"\n        \"`Change runtime type.`  select `GPU` \")\n  else:\n    print(\"GPU is enabled in this notebook.\")\n\n  return device","metadata":{"id":"Qfc6Huxw7iHi","execution":{"iopub.status.busy":"2021-08-20T07:55:49.73664Z","iopub.execute_input":"2021-08-20T07:55:49.737197Z","iopub.status.idle":"2021-08-20T07:55:49.749236Z","shell.execute_reply.started":"2021-08-20T07:55:49.737155Z","shell.execute_reply":"2021-08-20T07:55:49.748442Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Downloading data","metadata":{"id":"_8b02muU7xWO"}},{"cell_type":"code","source":"# Unzipping training images and solutions\n!unzip -qq ../input/galaxy-zoo-the-galaxy-challenge/images_training_rev1.zip\n!unzip -qq ../input/galaxy-zoo-the-galaxy-challenge/training_solutions_rev1.zip\n\n# !unzip -qq images_test_rev1.zip ","metadata":{"id":"KsQAg-k59bti","execution":{"iopub.status.busy":"2021-08-20T07:55:49.750651Z","iopub.execute_input":"2021-08-20T07:55:49.751047Z","iopub.status.idle":"2021-08-20T07:56:11.806578Z","shell.execute_reply.started":"2021-08-20T07:55:49.750999Z","shell.execute_reply":"2021-08-20T07:56:11.805442Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Now we have training images in the `images_training_rev1` directory and test images in the `images_test_rev1` directory.\n\nThe labels for training set is to be extracted from `training_solutions_rev1.csv` file.","metadata":{"id":"l59IzcfnTNhz"}},{"cell_type":"markdown","source":"# Transforming and preprocessing data","metadata":{"id":"O4b-Oq4M9jpe"}},{"cell_type":"code","source":"from IPython.display import Image\nImage('./images_training_rev1/999967.jpg')","metadata":{"id":"sSeYdXCzS1BT","outputId":"dccd6af2-f033-47bd-a342-9be454090c33","execution":{"iopub.status.busy":"2021-08-20T07:56:11.808138Z","iopub.execute_input":"2021-08-20T07:56:11.808495Z","iopub.status.idle":"2021-08-20T07:56:11.818319Z","shell.execute_reply.started":"2021-08-20T07:56:11.808455Z","shell.execute_reply":"2021-08-20T07:56:11.817302Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Preparing dataset and dataloaders","metadata":{"id":"thHd2hLz01ZR"}},{"cell_type":"code","source":"class GalaxyData(Dataset):\n    \"\"\"Custom image dataset with probabitity labels (Kaggle Galaxy Zoo challange).\"\"\"\n\n    def __init__(self, csv_file, root_dir, transform=None):\n        \"\"\"\n        Args:\n            csv_file (string): Path to the csv file with annotations.\n            root_dir (string): Directory with all the images.\n            transform (callable, optional): Optional transform to be applied\n                on a sample.\n        \"\"\"\n        self.landmarks_frame = pd.read_csv(csv_file)\n        self.root_dir = root_dir\n        self.transform = transform\n\n    def __len__(self):\n        return len(self.landmarks_frame)\n\n    def __getitem__(self, idx):\n        if torch.is_tensor(idx):\n            idx = idx.tolist()\n\n        img_name = str(self.root_dir) + \"/\" + str(self.landmarks_frame.loc[idx, \"GalaxyID\"]) + \".jpg\"\n        image = PIL.Image.open(img_name)\n        image = np.array(image)\n\n        if self.transform:\n            image = self.transform(image)\n\n\n        landmarks = self.landmarks_frame.loc[idx, \"Class1.1\":]\n        landmarks = np.array(landmarks)\n        landmarks = landmarks.astype('float')\n        sample = [image, landmarks]\n        return sample\n\n\ntsfm = transforms.Compose([\n                           transforms.ToTensor(),\n                          #  transforms.Normalize([0,0,0],[1,1,1]),\n                           # transforms.Resize(size=(224,224))\n                           # Add transforms here\n                           transforms.CenterCrop((224, 224)),\n                           transforms.Resize(size=(112, 112))\n])\n\ndataset1 = GalaxyData(\"./training_solutions_rev1.csv\", \"./images_training_rev1\", tsfm)\n\n","metadata":{"id":"kzMfch0RzDRA","execution":{"iopub.status.busy":"2021-08-20T07:56:11.819542Z","iopub.execute_input":"2021-08-20T07:56:11.820135Z","iopub.status.idle":"2021-08-20T07:56:12.175395Z","shell.execute_reply.started":"2021-08-20T07:56:11.820097Z","shell.execute_reply":"2021-08-20T07:56:12.173476Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# determine absolute training, val, test data amounts\nsplit = np.array(SPLIT) * len(dataset1)\n# turn into integers\nsplit = np.floor(split).astype(np.int32)\n# make total sum match dataset samples\nwhile np.sum(split) != len(dataset1):\n  split[0]+=1\n\ntrain_data, validation_data, test_data = torch.utils.data.random_split(dataset1, \n  split.tolist(), generator=torch.Generator().manual_seed(42))\n\ntrain_loader = torch.utils.data.DataLoader(train_data, \n                                           batch_size=BATCH_SIZE, shuffle=True)\n\nvalidation_loader = torch.utils.data.DataLoader(validation_data, \n                                           batch_size=BATCH_SIZE, shuffle=True)\n\ntest_loader = torch.utils.data.DataLoader(test_data, \n                                           batch_size=BATCH_SIZE, shuffle=True)","metadata":{"id":"jWaCC3534Q1O","execution":{"iopub.status.busy":"2021-08-20T07:56:12.178443Z","iopub.execute_input":"2021-08-20T07:56:12.178973Z","iopub.status.idle":"2021-08-20T07:56:12.227639Z","shell.execute_reply.started":"2021-08-20T07:56:12.178917Z","shell.execute_reply":"2021-08-20T07:56:12.225788Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Defining network class","metadata":{"id":"ysijcVyU9pyf"}},{"cell_type":"code","source":"class GalaxyNet(nn.Module):\n\n  def __init__(self):\n    super().__init__()\n    \n    self.conv1 = nn.Conv2d(in_channels=3, out_channels=32, kernel_size=7, stride=2)\n    self.conv2 = nn.Conv2d(in_channels=32, out_channels=64, kernel_size=7, stride=1, padding=3)\n    self.conv3 = nn.Conv2d(in_channels=64, out_channels=128, kernel_size=5, stride=1, padding=2)\n    self.conv4 = nn.Conv2d(in_channels=128, out_channels=256, kernel_size=3, stride=1, padding=1)\n    self.conv5 = nn.Conv2d(in_channels=256, out_channels=1024, kernel_size=3, stride=1, padding=1)\n    \n    self.conv1x1_64 = nn.Conv2d(in_channels=32, out_channels=64, kernel_size=1)\n    self.conv1x1_128 = nn.Conv2d(in_channels=64, out_channels=128, kernel_size=1)\n    self.conv1x1_256 = nn.Conv2d(in_channels=128, out_channels=256, kernel_size=1)\n    self.conv1x1_1024 = nn.Conv2d(in_channels=256, out_channels=1024, kernel_size=1)\n    \n    self.batch_norm1 = torch.nn.BatchNorm2d(64)\n    self.batch_norm2 = torch.nn.BatchNorm2d(128)\n    self.batch_norm3 = torch.nn.BatchNorm2d(256)\n    self.batch_norm4 = torch.nn.BatchNorm2d(1024)\n    \n    self.final_max_pool = nn.MaxPool2d(kernel_size = 53)\n    self.flatten = nn.Flatten()\n    \n    self.linear = nn.Linear(in_features=1024, out_features=37)\n    self.sigmoid = nn.Sigmoid()\n\n#    self.layers = nn.Sequential(\n#          nn.MaxPool2d(kernel_size = 2)\n#         nn.Conv2d(in_channels=3, out_channels=32, kernel_size=7, stride=2),\n#         nn.ReLU(),\n#         nn.Conv2d(in_channels=32, out_channels=64, kernel_size=5, stride=2),\n#         nn.ReLU(),\n#         nn.Conv2d(in_channels=64, out_channels=128, kernel_size=5, stride=1),\n#         nn.ReLU(),\n#         nn.Conv2d(in_channels=128, out_channels=256, kernel_size=3, stride=1),\n#         nn.ReLU(),\n#         nn.MaxPool2d(kernel_size=2),\n#         nn.Conv2d(in_channels=256, out_channels=512, kernel_size=3, stride=1),\n#         nn.ReLU(),\n#         nn.MaxPool2d(kernel_size=(7,7)),\n#         nn.Flatten(),\n#         nn.Linear(in_features=512, out_features=37),\n#         nn.Sigmoid()\n# #         # nn.Softmax(dim=1)\n\n# #         #nn.MaxPool2d(kernel_size=3),\n\n\n# #         # nn.Conv2d(3, out_channels=2, kernel_size=15, stride = 1, padding=0), \n# #         # nn.ReLU(),\n# #         # nn.Conv2d(2, out_channels=1, kernel_size=15, padding=0), \n# #         # # nn.AvgPool2d((416,416)),\n# #         # nn.ReLU(),\n# #         # nn.Flatten(),\n# #         # nn.Linear(in_features=38416, out_features=2)\n#        )\n\n    \n\n  def forward(self, x):\n    conv1 = F.relu(self.conv1(x))\n    \n    conv2 = self.batch_norm1(F.relu(self.conv2(conv1) + self.conv1x1_64(conv1)))\n    \n    conv3 = self.batch_norm2(F.relu(self.conv3(conv2) + self.conv1x1_128(conv2)))\n    \n    conv4 = self.batch_norm3(F.relu(self.conv4(conv3) + self.conv1x1_256(conv3)))\n    \n    conv5 = self.batch_norm4(F.relu(self.conv5(conv4) + self.conv1x1_1024(conv4)))\n    conv_output = self.flatten(self.final_max_pool(conv5))\n    \n    out = self.sigmoid(self.linear(conv_output))\n\n    return out\n\nmodel = GalaxyNet()\ndevice = set_device()\nmodel.to(device)","metadata":{"id":"YAWraw9--BiS","execution":{"iopub.status.busy":"2021-08-20T07:56:12.234061Z","iopub.execute_input":"2021-08-20T07:56:12.236357Z","iopub.status.idle":"2021-08-20T07:56:16.552605Z","shell.execute_reply.started":"2021-08-20T07:56:12.236316Z","shell.execute_reply":"2021-08-20T07:56:16.551765Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torchsummary\ntorchsummary.summary(model, (3, 112, 112))","metadata":{"id":"GA9kDKRHZ39o","outputId":"d554a7ab-f44b-49dd-8fda-f3fcda425f42","execution":{"iopub.status.busy":"2021-08-20T07:56:16.553832Z","iopub.execute_input":"2021-08-20T07:56:16.55419Z","iopub.status.idle":"2021-08-20T07:56:17.479501Z","shell.execute_reply.started":"2021-08-20T07:56:16.554162Z","shell.execute_reply":"2021-08-20T07:56:17.478653Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Training","metadata":{"id":"iC4CUUvXych1"}},{"cell_type":"code","source":"def split_into_questions(x):\n    q1 = x[:, 0:3]\n    q2 = x[:, 3:5]\n    q3 = x[:, 5:7]\n    q4 = x[:, 7:9]\n    q5 = x[:, 9:13]\n    q6 = x[:, 13:15]\n    q7 = x[:, 15:18]\n    q8 = x[:, 18:25]\n    q9 = x[:, 25:28]\n    q10 = x[:, 28:31]\n    q11 = x[:, 31:37]\n    return q1, q2, q3, q4, q5, q6, q7, q8, q9, q10, q11\n\n\n# q is short for question\ndef calculate_accuracy(output, target):\n    q_outputs = split_into_questions(output)\n    q_targets = split_into_questions(target)\n    \n    accuracy = []\n    counter = 0\n    final_acc = np.zeros(11, dtype=np.float)\n    for q_index in range(11):\n        accuracy.append([])\n        for batch_index in range(q_outputs[q_index].shape[0]):\n            \n            if (q_targets[q_index][batch_index].sum() == 0):\n                accuracy[q_index].append(0)\n                \n            else:  \n                accuracy[q_index].append(torch.sum(\n                    q_outputs[q_index][batch_index].argmax(0) == q_targets[q_index][batch_index].argmax(0)))\n                \n                counter += 1\n            \n        if (counter == 0):\n             final_acc[q_index] = 0.5\n        else:\n            final_acc[q_index] = np.sum(accuracy[q_index]) / counter\n        counter = 0\n    return final_acc\n        ","metadata":{"execution":{"iopub.status.busy":"2021-08-20T07:56:17.480755Z","iopub.execute_input":"2021-08-20T07:56:17.481236Z","iopub.status.idle":"2021-08-20T07:56:17.495833Z","shell.execute_reply.started":"2021-08-20T07:56:17.4812Z","shell.execute_reply":"2021-08-20T07:56:17.494821Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def criterion_fun(output, target):\n    q_outputs = split_into_questions(output)\n    q_targets = split_into_questions(target)\n    eps = 1e-6\n    \n    loss_sum = 0\n    for q_index in range(11):\n        loss_sum += torch.sqrt(CRITERION(q_outputs[q_index], q_targets[q_index]) + eps) * QUESTION_WEIGHTS[q_index]\n    \n    return loss_sum","metadata":{"execution":{"iopub.status.busy":"2021-08-20T07:56:17.497392Z","iopub.execute_input":"2021-08-20T07:56:17.498019Z","iopub.status.idle":"2021-08-20T07:56:17.510807Z","shell.execute_reply.started":"2021-08-20T07:56:17.497981Z","shell.execute_reply":"2021-08-20T07:56:17.509978Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def train_network(model: torch.nn.Module, data_loader: torch.utils.data.DataLoader,\n                  optimizer: torch.optim.Optimizer, device: torch.device = r'cpu') -> None:\n    \"\"\"\n    Train specified network for one epoch on specified data loader.\n\n    :param model: network to train\n    :param data_loader: data loader to be trained on\n    :param optimizer: optimizer used to train network\n    :param device: device on which to train network\n    :return: None\n    \"\"\"\n    model.train()\n    criterion = criterion_fun\n    for batch_index, (data, target) in enumerate(data_loader):\n        data, target = data.to(device), target.float().to(device)\n        optimizer.zero_grad()\n        output = model(data)\n        loss = criterion(output, target)\n        loss.backward()\n        optimizer.step()\n\ndef test_network(model: torch.nn.Module, data_loader: torch.utils.data.DataLoader,\n                 device: torch.device = r'cpu'):\n    \"\"\"\n    Test specified network on specified data loader.\n\n    :param model: network to test on\n    :param data_loader: data loader to be tested on\n    :param device: device on which to test network\n    :return: cross-entropy loss as well as accuracy\n    \"\"\"\n    model.eval()\n    loss = 0.0\n    counter = 0\n    accuracy = np.zeros(11, dtype=np.float) # 11 - number of questions\n    criterion = criterion_fun\n    with torch.no_grad():\n        for data, target in data_loader:\n            counter += 1\n            data, target = data.to(device), target.float().to(device)\n            output = model(data)\n            loss += float(criterion(output, target).item())\n            accuracy += calculate_accuracy(output, target)\n\n    return loss / len(data_loader.dataset), accuracy / counter\n\ndef train_and_evaluate(model: torch.nn.Module, optimizer: torch.optim.Optimizer,\n  device: torch.device, num_epochs: int,\n  loader_train: torch.utils.data.DataLoader,\n  loader_test: torch.utils.data.DataLoader) -> None:\n  \"\"\"\n  Auxiliary function for training and evaluating a corresponding model.\n  \n  :param model: model instance to train and evaluate\n  :param optimizer: optimizer to use for model training\n  :param device: device to use for model training and evaluation\n  :param num_epochs: amount of epochs for model training\n  :param loader_train: data loader supplying the training samples\n  :param loader_test: data loader supplying the test samples\n  :return: None\n  \"\"\"\n\n  epochs = []\n  train_accuracy = []\n  val_accuracy = []\n  train_loss = []\n  val_loss = []\n\n  model.to(device=device)\n\n  with tqdm(range(num_epochs), unit='epoch') as tepochs:\n    tepochs.set_description('Training')\n    for epoch in tepochs:\n      # Train model instance for one epoch.\n      train_network(model=model, data_loader=loader_train, device=device, optimizer=optimizer)\n      # Evaluate current model instance.\n      train_performance = test_network(model=model, data_loader=loader_train, device=device)\n      # Print result of current epoch to standard out.\n      print(f'Epoch: {str(epoch + 1).zfill(len(str(num_epochs)))} ' +\n      f'/ Train Loss * 10000: {train_performance[0] * 10000:.5f} / Train acuracy: {train_performance[1]}')\n      # Evaluate model on validation data set.\n      val_performance = test_network(model=model, data_loader=loader_test, device=device)\n      print(f'Epoch: {str(epoch + 1).zfill(len(str(num_epochs)))} ' +\n      f'/ Validation loss * 10000: {val_performance[0] * 10000:.5f} / Validation accuracy: {val_performance[1]}\\n')\n\n      epochs.append(epoch)  \n      train_accuracy.append(np.mean(train_performance[1]))\n      val_accuracy.append(np.mean(val_performance[1]))\n      train_loss.append(train_performance[0] * 10000)\n      val_loss.append(val_performance[0] * 10000)\n        \n    \n      if ((epoch+1) % 10 == 0):\n        torch.save(model.state_dict(), \"./GalaxyNetv3(L1)_ep\" + str(epoch + 1) + \".pth\")\n\n  plot_train_val(epochs, train_accuracy, val_accuracy, \n                 \"train\", \"validation\", \"Train and validation accuracy\", \"Mean accuracy\", \"blue\")\n  \n  plot_train_val(epochs, train_loss, val_loss, \n                 \"train\", \"validation\", \"Train and validation loss\", \"Loss * 10000\", \"red\")\n\n\n\ntarget_device = torch.device(r'cuda' if torch.cuda.is_available() else r'cpu')\noptimizer = torch.optim.Adam(model.parameters(), lr=LEARNING_RATE)\n\ntrain_and_evaluate(\n  model=model,\n  optimizer=optimizer,\n  device=target_device,\n  num_epochs=NUM_EPOCHS,\n  loader_train=train_loader,\n  loader_test=validation_loader)  # we need a test_loader here","metadata":{"id":"UCH2Sbh0i7o0","outputId":"c923654e-1930-45e4-b18e-10067cccca54","execution":{"iopub.status.busy":"2021-08-20T11:07:27.059887Z","iopub.execute_input":"2021-08-20T11:07:27.060244Z","iopub.status.idle":"2021-08-20T12:39:20.034618Z","shell.execute_reply.started":"2021-08-20T11:07:27.060212Z","shell.execute_reply":"2021-08-20T12:39:20.033841Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tsfm = transforms.Compose([\n                           transforms.ToTensor(),\n                          #  transforms.Normalize([0,0,0],[1,1,1]),\n                           # transforms.Resize(size=(224,224))\n                           # Add transforms here\n                           transforms.CenterCrop((224, 224)),\n                           transforms.Resize(size=(112, 112))\n])\nimage = PIL.Image.open(\"./images_training_rev1/100023.jpg\")\nimage = tsfm(image)\nimage = torch.unsqueeze(image, 0)\n","metadata":{"execution":{"iopub.status.busy":"2021-08-16T09:45:49.694308Z","iopub.execute_input":"2021-08-16T09:45:49.694692Z","iopub.status.idle":"2021-08-16T09:45:49.70676Z","shell.execute_reply.started":"2021-08-16T09:45:49.694661Z","shell.execute_reply":"2021-08-16T09:45:49.705898Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.to(\"cpu\")","metadata":{"execution":{"iopub.status.busy":"2021-08-16T09:43:15.528174Z","iopub.execute_input":"2021-08-16T09:43:15.528535Z","iopub.status.idle":"2021-08-16T09:43:15.540365Z","shell.execute_reply.started":"2021-08-16T09:43:15.528505Z","shell.execute_reply":"2021-08-16T09:43:15.539261Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(model(image))","metadata":{"execution":{"iopub.status.busy":"2021-08-16T09:45:54.700702Z","iopub.execute_input":"2021-08-16T09:45:54.701029Z","iopub.status.idle":"2021-08-16T09:45:54.718805Z","shell.execute_reply.started":"2021-08-16T09:45:54.700997Z","shell.execute_reply":"2021-08-16T09:45:54.717997Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def clearGPUcache():\n  model.to(\"cpu\")\n  del model\n  #data.to(\"cpu\")\n  #target.to(\"cpu\")\n  torch.cuda.empty_cache()\nclearGPUcache()","metadata":{"id":"bwPqzBiGvYeY","outputId":"127311bd-adba-4df7-af78-cb2843c00b59"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# dir(model)\n# model\nlist(model.parameters())[0].shape","metadata":{"id":"H_xF0Zk7YlSR","outputId":"c1bef627-8514-461b-c01e-3002d5846684"},"execution_count":null,"outputs":[]}]}