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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cells": [
|
| 3 |
+
{
|
| 4 |
+
"cell_type": "code",
|
| 5 |
+
"execution_count": 1,
|
| 6 |
+
"id": "pVAyqh-hccNc",
|
| 7 |
+
"metadata": {
|
| 8 |
+
"colab": {
|
| 9 |
+
"base_uri": "https://localhost:8080/"
|
| 10 |
+
},
|
| 11 |
+
"executionInfo": {
|
| 12 |
+
"elapsed": 22150,
|
| 13 |
+
"status": "ok",
|
| 14 |
+
"timestamp": 1726361588423,
|
| 15 |
+
"user": {
|
| 16 |
+
"displayName": "Darshil Parekh",
|
| 17 |
+
"userId": "08764169128860999444"
|
| 18 |
+
},
|
| 19 |
+
"user_tz": -330
|
| 20 |
+
},
|
| 21 |
+
"id": "pVAyqh-hccNc",
|
| 22 |
+
"outputId": "c5eb1f4a-c3d1-4491-f677-6e4f87bd6a17"
|
| 23 |
+
},
|
| 24 |
+
"outputs": [
|
| 25 |
+
{
|
| 26 |
+
"name": "stdout",
|
| 27 |
+
"output_type": "stream",
|
| 28 |
+
"text": [
|
| 29 |
+
"Collecting datasets\n",
|
| 30 |
+
" Downloading datasets-3.0.0-py3-none-any.whl.metadata (19 kB)\n",
|
| 31 |
+
"Requirement already satisfied: filelock in /usr/local/lib/python3.10/dist-packages (from datasets) (3.16.0)\n",
|
| 32 |
+
"Requirement already satisfied: numpy>=1.17 in /usr/local/lib/python3.10/dist-packages (from datasets) (1.26.4)\n",
|
| 33 |
+
"Collecting pyarrow>=15.0.0 (from datasets)\n",
|
| 34 |
+
" Downloading pyarrow-17.0.0-cp310-cp310-manylinux_2_28_x86_64.whl.metadata (3.3 kB)\n",
|
| 35 |
+
"Collecting dill<0.3.9,>=0.3.0 (from datasets)\n",
|
| 36 |
+
" Downloading dill-0.3.8-py3-none-any.whl.metadata (10 kB)\n",
|
| 37 |
+
"Requirement already satisfied: pandas in /usr/local/lib/python3.10/dist-packages (from datasets) (2.1.4)\n",
|
| 38 |
+
"Requirement already satisfied: requests>=2.32.2 in /usr/local/lib/python3.10/dist-packages (from datasets) (2.32.3)\n",
|
| 39 |
+
"Requirement already satisfied: tqdm>=4.66.3 in /usr/local/lib/python3.10/dist-packages (from datasets) (4.66.5)\n",
|
| 40 |
+
"Collecting xxhash (from datasets)\n",
|
| 41 |
+
" Downloading xxhash-3.5.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (12 kB)\n",
|
| 42 |
+
"Collecting multiprocess (from datasets)\n",
|
| 43 |
+
" Downloading multiprocess-0.70.16-py310-none-any.whl.metadata (7.2 kB)\n",
|
| 44 |
+
"Requirement already satisfied: fsspec<=2024.6.1,>=2023.1.0 in /usr/local/lib/python3.10/dist-packages (from fsspec[http]<=2024.6.1,>=2023.1.0->datasets) (2024.6.1)\n",
|
| 45 |
+
"Requirement already satisfied: aiohttp in /usr/local/lib/python3.10/dist-packages (from datasets) (3.10.5)\n",
|
| 46 |
+
"Requirement already satisfied: huggingface-hub>=0.22.0 in /usr/local/lib/python3.10/dist-packages (from datasets) (0.24.6)\n",
|
| 47 |
+
"Requirement already satisfied: packaging in /usr/local/lib/python3.10/dist-packages (from datasets) (24.1)\n",
|
| 48 |
+
"Requirement already satisfied: pyyaml>=5.1 in /usr/local/lib/python3.10/dist-packages (from datasets) (6.0.2)\n",
|
| 49 |
+
"Requirement already satisfied: aiohappyeyeballs>=2.3.0 in /usr/local/lib/python3.10/dist-packages (from aiohttp->datasets) (2.4.0)\n",
|
| 50 |
+
"Requirement already satisfied: aiosignal>=1.1.2 in /usr/local/lib/python3.10/dist-packages (from aiohttp->datasets) (1.3.1)\n",
|
| 51 |
+
"Requirement already satisfied: attrs>=17.3.0 in /usr/local/lib/python3.10/dist-packages (from aiohttp->datasets) (24.2.0)\n",
|
| 52 |
+
"Requirement already satisfied: frozenlist>=1.1.1 in /usr/local/lib/python3.10/dist-packages (from aiohttp->datasets) (1.4.1)\n",
|
| 53 |
+
"Requirement already satisfied: multidict<7.0,>=4.5 in /usr/local/lib/python3.10/dist-packages (from aiohttp->datasets) (6.1.0)\n",
|
| 54 |
+
"Requirement already satisfied: yarl<2.0,>=1.0 in /usr/local/lib/python3.10/dist-packages (from aiohttp->datasets) (1.11.1)\n",
|
| 55 |
+
"Requirement already satisfied: async-timeout<5.0,>=4.0 in /usr/local/lib/python3.10/dist-packages (from aiohttp->datasets) (4.0.3)\n",
|
| 56 |
+
"Requirement already satisfied: typing-extensions>=3.7.4.3 in /usr/local/lib/python3.10/dist-packages (from huggingface-hub>=0.22.0->datasets) (4.12.2)\n",
|
| 57 |
+
"Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.10/dist-packages (from requests>=2.32.2->datasets) (3.3.2)\n",
|
| 58 |
+
"Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/dist-packages (from requests>=2.32.2->datasets) (3.8)\n",
|
| 59 |
+
"Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.10/dist-packages (from requests>=2.32.2->datasets) (2.0.7)\n",
|
| 60 |
+
"Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.10/dist-packages (from requests>=2.32.2->datasets) (2024.8.30)\n",
|
| 61 |
+
"Requirement already satisfied: python-dateutil>=2.8.2 in /usr/local/lib/python3.10/dist-packages (from pandas->datasets) (2.8.2)\n",
|
| 62 |
+
"Requirement already satisfied: pytz>=2020.1 in /usr/local/lib/python3.10/dist-packages (from pandas->datasets) (2024.2)\n",
|
| 63 |
+
"Requirement already satisfied: tzdata>=2022.1 in /usr/local/lib/python3.10/dist-packages (from pandas->datasets) (2024.1)\n",
|
| 64 |
+
"Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.10/dist-packages (from python-dateutil>=2.8.2->pandas->datasets) (1.16.0)\n",
|
| 65 |
+
"Downloading datasets-3.0.0-py3-none-any.whl (474 kB)\n",
|
| 66 |
+
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m474.3/474.3 kB\u001b[0m \u001b[31m6.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
| 67 |
+
"\u001b[?25hDownloading dill-0.3.8-py3-none-any.whl (116 kB)\n",
|
| 68 |
+
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m116.3/116.3 kB\u001b[0m \u001b[31m2.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
| 69 |
+
"\u001b[?25hDownloading pyarrow-17.0.0-cp310-cp310-manylinux_2_28_x86_64.whl (39.9 MB)\n",
|
| 70 |
+
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m39.9/39.9 MB\u001b[0m \u001b[31m10.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
| 71 |
+
"\u001b[?25hDownloading multiprocess-0.70.16-py310-none-any.whl (134 kB)\n",
|
| 72 |
+
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m134.8/134.8 kB\u001b[0m \u001b[31m4.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
| 73 |
+
"\u001b[?25hDownloading xxhash-3.5.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (194 kB)\n",
|
| 74 |
+
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m194.1/194.1 kB\u001b[0m \u001b[31m9.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
| 75 |
+
"\u001b[?25hInstalling collected packages: xxhash, pyarrow, dill, multiprocess, datasets\n",
|
| 76 |
+
" Attempting uninstall: pyarrow\n",
|
| 77 |
+
" Found existing installation: pyarrow 14.0.2\n",
|
| 78 |
+
" Uninstalling pyarrow-14.0.2:\n",
|
| 79 |
+
" Successfully uninstalled pyarrow-14.0.2\n",
|
| 80 |
+
"\u001b[31mERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.\n",
|
| 81 |
+
"cudf-cu12 24.4.1 requires pyarrow<15.0.0a0,>=14.0.1, but you have pyarrow 17.0.0 which is incompatible.\n",
|
| 82 |
+
"ibis-framework 8.0.0 requires pyarrow<16,>=2, but you have pyarrow 17.0.0 which is incompatible.\u001b[0m\u001b[31m\n",
|
| 83 |
+
"\u001b[0mSuccessfully installed datasets-3.0.0 dill-0.3.8 multiprocess-0.70.16 pyarrow-17.0.0 xxhash-3.5.0\n"
|
| 84 |
+
]
|
| 85 |
+
}
|
| 86 |
+
],
|
| 87 |
+
"source": [
|
| 88 |
+
"!pip install datasets"
|
| 89 |
+
]
|
| 90 |
+
},
|
| 91 |
+
{
|
| 92 |
+
"cell_type": "code",
|
| 93 |
+
"execution_count": 3,
|
| 94 |
+
"id": "2KMucD1rKwTz",
|
| 95 |
+
"metadata": {
|
| 96 |
+
"executionInfo": {
|
| 97 |
+
"elapsed": 2060,
|
| 98 |
+
"status": "ok",
|
| 99 |
+
"timestamp": 1726361622660,
|
| 100 |
+
"user": {
|
| 101 |
+
"displayName": "Darshil Parekh",
|
| 102 |
+
"userId": "08764169128860999444"
|
| 103 |
+
},
|
| 104 |
+
"user_tz": -330
|
| 105 |
+
},
|
| 106 |
+
"id": "2KMucD1rKwTz"
|
| 107 |
+
},
|
| 108 |
+
"outputs": [],
|
| 109 |
+
"source": [
|
| 110 |
+
"!pip freeze > requirements.txt"
|
| 111 |
+
]
|
| 112 |
+
},
|
| 113 |
+
{
|
| 114 |
+
"cell_type": "code",
|
| 115 |
+
"execution_count": null,
|
| 116 |
+
"id": "Z8RYK2ZSK42y",
|
| 117 |
+
"metadata": {
|
| 118 |
+
"id": "Z8RYK2ZSK42y"
|
| 119 |
+
},
|
| 120 |
+
"outputs": [],
|
| 121 |
+
"source": []
|
| 122 |
+
},
|
| 123 |
+
{
|
| 124 |
+
"cell_type": "code",
|
| 125 |
+
"execution_count": null,
|
| 126 |
+
"id": "tVGMu6bhcdoz",
|
| 127 |
+
"metadata": {
|
| 128 |
+
"colab": {
|
| 129 |
+
"base_uri": "https://localhost:8080/"
|
| 130 |
+
},
|
| 131 |
+
"executionInfo": {
|
| 132 |
+
"elapsed": 4076,
|
| 133 |
+
"status": "ok",
|
| 134 |
+
"timestamp": 1726321470958,
|
| 135 |
+
"user": {
|
| 136 |
+
"displayName": "Darshil Parekh",
|
| 137 |
+
"userId": "08764169128860999444"
|
| 138 |
+
},
|
| 139 |
+
"user_tz": -330
|
| 140 |
+
},
|
| 141 |
+
"id": "tVGMu6bhcdoz",
|
| 142 |
+
"outputId": "436e5f2d-46e8-4813-86d0-945606f3431b"
|
| 143 |
+
},
|
| 144 |
+
"outputs": [
|
| 145 |
+
{
|
| 146 |
+
"name": "stdout",
|
| 147 |
+
"output_type": "stream",
|
| 148 |
+
"text": [
|
| 149 |
+
"Drive already mounted at /content/drive; to attempt to forcibly remount, call drive.mount(\"/content/drive\", force_remount=True).\n"
|
| 150 |
+
]
|
| 151 |
+
}
|
| 152 |
+
],
|
| 153 |
+
"source": [
|
| 154 |
+
"from google.colab import drive\n",
|
| 155 |
+
"drive.mount('/content/drive')"
|
| 156 |
+
]
|
| 157 |
+
},
|
| 158 |
+
{
|
| 159 |
+
"cell_type": "code",
|
| 160 |
+
"execution_count": null,
|
| 161 |
+
"id": "ddb93c12-776c-43f3-87f2-566a61510042",
|
| 162 |
+
"metadata": {
|
| 163 |
+
"id": "ddb93c12-776c-43f3-87f2-566a61510042"
|
| 164 |
+
},
|
| 165 |
+
"outputs": [],
|
| 166 |
+
"source": [
|
| 167 |
+
"from datasets import load_from_disk\n",
|
| 168 |
+
"\n",
|
| 169 |
+
"import os\n",
|
| 170 |
+
"import torch\n",
|
| 171 |
+
"import torch.nn as nn\n",
|
| 172 |
+
"import torch.optim as optim\n",
|
| 173 |
+
"from torch.autograd import Variable\n",
|
| 174 |
+
"from torch.utils.data import Dataset, DataLoader\n",
|
| 175 |
+
"\n",
|
| 176 |
+
"import torchvision\n",
|
| 177 |
+
"from torchvision import datasets, transforms\n",
|
| 178 |
+
"\n",
|
| 179 |
+
"import matplotlib.pyplot as plt\n",
|
| 180 |
+
"import numpy as np\n",
|
| 181 |
+
"\n",
|
| 182 |
+
"\n",
|
| 183 |
+
"import utils\n",
|
| 184 |
+
"\n",
|
| 185 |
+
"from utils import Utils, Logger"
|
| 186 |
+
]
|
| 187 |
+
},
|
| 188 |
+
{
|
| 189 |
+
"cell_type": "code",
|
| 190 |
+
"execution_count": null,
|
| 191 |
+
"id": "a2b42c1f-13d2-423b-a8c0-c1631d93b3fe",
|
| 192 |
+
"metadata": {
|
| 193 |
+
"colab": {
|
| 194 |
+
"base_uri": "https://localhost:8080/"
|
| 195 |
+
},
|
| 196 |
+
"executionInfo": {
|
| 197 |
+
"elapsed": 9,
|
| 198 |
+
"status": "ok",
|
| 199 |
+
"timestamp": 1726321476954,
|
| 200 |
+
"user": {
|
| 201 |
+
"displayName": "Darshil Parekh",
|
| 202 |
+
"userId": "08764169128860999444"
|
| 203 |
+
},
|
| 204 |
+
"user_tz": -330
|
| 205 |
+
},
|
| 206 |
+
"id": "a2b42c1f-13d2-423b-a8c0-c1631d93b3fe",
|
| 207 |
+
"outputId": "b5f7a35f-201d-47fb-8d67-8f22e568e89c"
|
| 208 |
+
},
|
| 209 |
+
"outputs": [
|
| 210 |
+
{
|
| 211 |
+
"data": {
|
| 212 |
+
"text/plain": [
|
| 213 |
+
"<torch._C.Generator at 0x7988a01888d0>"
|
| 214 |
+
]
|
| 215 |
+
},
|
| 216 |
+
"execution_count": 4,
|
| 217 |
+
"metadata": {},
|
| 218 |
+
"output_type": "execute_result"
|
| 219 |
+
}
|
| 220 |
+
],
|
| 221 |
+
"source": [
|
| 222 |
+
"random_seed = 42\n",
|
| 223 |
+
"torch.manual_seed(random_seed)"
|
| 224 |
+
]
|
| 225 |
+
},
|
| 226 |
+
{
|
| 227 |
+
"cell_type": "code",
|
| 228 |
+
"execution_count": null,
|
| 229 |
+
"id": "05efff77-160f-41e3-939a-0755f6986de0",
|
| 230 |
+
"metadata": {
|
| 231 |
+
"colab": {
|
| 232 |
+
"base_uri": "https://localhost:8080/"
|
| 233 |
+
},
|
| 234 |
+
"executionInfo": {
|
| 235 |
+
"elapsed": 9,
|
| 236 |
+
"status": "ok",
|
| 237 |
+
"timestamp": 1726321476955,
|
| 238 |
+
"user": {
|
| 239 |
+
"displayName": "Darshil Parekh",
|
| 240 |
+
"userId": "08764169128860999444"
|
| 241 |
+
},
|
| 242 |
+
"user_tz": -330
|
| 243 |
+
},
|
| 244 |
+
"id": "05efff77-160f-41e3-939a-0755f6986de0",
|
| 245 |
+
"outputId": "a74d9600-0cc4-4c6a-e56b-cace164a82b7"
|
| 246 |
+
},
|
| 247 |
+
"outputs": [
|
| 248 |
+
{
|
| 249 |
+
"data": {
|
| 250 |
+
"text/plain": [
|
| 251 |
+
"(1, 1)"
|
| 252 |
+
]
|
| 253 |
+
},
|
| 254 |
+
"execution_count": 5,
|
| 255 |
+
"metadata": {},
|
| 256 |
+
"output_type": "execute_result"
|
| 257 |
+
}
|
| 258 |
+
],
|
| 259 |
+
"source": [
|
| 260 |
+
"AVAIL_GPUS = min(1, torch.cuda.device_count())\n",
|
| 261 |
+
"NUM_WORKERS=int(os.cpu_count() / 2)\n",
|
| 262 |
+
"AVAIL_GPUS,NUM_WORKERS"
|
| 263 |
+
]
|
| 264 |
+
},
|
| 265 |
+
{
|
| 266 |
+
"cell_type": "code",
|
| 267 |
+
"execution_count": null,
|
| 268 |
+
"id": "5fecca51-0f62-4060-9df1-ee94c54ebcaf",
|
| 269 |
+
"metadata": {
|
| 270 |
+
"colab": {
|
| 271 |
+
"base_uri": "https://localhost:8080/"
|
| 272 |
+
},
|
| 273 |
+
"executionInfo": {
|
| 274 |
+
"elapsed": 8,
|
| 275 |
+
"status": "ok",
|
| 276 |
+
"timestamp": 1726321476955,
|
| 277 |
+
"user": {
|
| 278 |
+
"displayName": "Darshil Parekh",
|
| 279 |
+
"userId": "08764169128860999444"
|
| 280 |
+
},
|
| 281 |
+
"user_tz": -330
|
| 282 |
+
},
|
| 283 |
+
"id": "5fecca51-0f62-4060-9df1-ee94c54ebcaf",
|
| 284 |
+
"outputId": "6ad227e6-6d27-4ac5-bff1-56f7831b17ae"
|
| 285 |
+
},
|
| 286 |
+
"outputs": [
|
| 287 |
+
{
|
| 288 |
+
"data": {
|
| 289 |
+
"text/plain": [
|
| 290 |
+
"DatasetDict({\n",
|
| 291 |
+
" train: Dataset({\n",
|
| 292 |
+
" features: ['image', 'company', 'content', 'description', 'fulltext', 'fulltext_vector'],\n",
|
| 293 |
+
" num_rows: 33034\n",
|
| 294 |
+
" })\n",
|
| 295 |
+
" test: Dataset({\n",
|
| 296 |
+
" features: ['image', 'company', 'content', 'description', 'fulltext', 'fulltext_vector'],\n",
|
| 297 |
+
" num_rows: 14158\n",
|
| 298 |
+
" })\n",
|
| 299 |
+
"})"
|
| 300 |
+
]
|
| 301 |
+
},
|
| 302 |
+
"execution_count": 6,
|
| 303 |
+
"metadata": {},
|
| 304 |
+
"output_type": "execute_result"
|
| 305 |
+
}
|
| 306 |
+
],
|
| 307 |
+
"source": [
|
| 308 |
+
"reloaded_dataset = load_from_disk(\"/content/drive/MyDrive/PreProcessedDataWithEmb\")\n",
|
| 309 |
+
"reloaded_dataset"
|
| 310 |
+
]
|
| 311 |
+
},
|
| 312 |
+
{
|
| 313 |
+
"cell_type": "code",
|
| 314 |
+
"execution_count": null,
|
| 315 |
+
"id": "c4b75140-eb3d-425a-9769-b7a838b66a1d",
|
| 316 |
+
"metadata": {
|
| 317 |
+
"colab": {
|
| 318 |
+
"base_uri": "https://localhost:8080/"
|
| 319 |
+
},
|
| 320 |
+
"executionInfo": {
|
| 321 |
+
"elapsed": 7,
|
| 322 |
+
"status": "ok",
|
| 323 |
+
"timestamp": 1726321476955,
|
| 324 |
+
"user": {
|
| 325 |
+
"displayName": "Darshil Parekh",
|
| 326 |
+
"userId": "08764169128860999444"
|
| 327 |
+
},
|
| 328 |
+
"user_tz": -330
|
| 329 |
+
},
|
| 330 |
+
"id": "c4b75140-eb3d-425a-9769-b7a838b66a1d",
|
| 331 |
+
"outputId": "cc744cf8-215a-481e-c041-142de4ee429d"
|
| 332 |
+
},
|
| 333 |
+
"outputs": [
|
| 334 |
+
{
|
| 335 |
+
"data": {
|
| 336 |
+
"text/plain": [
|
| 337 |
+
"device(type='cuda')"
|
| 338 |
+
]
|
| 339 |
+
},
|
| 340 |
+
"execution_count": 7,
|
| 341 |
+
"metadata": {},
|
| 342 |
+
"output_type": "execute_result"
|
| 343 |
+
}
|
| 344 |
+
],
|
| 345 |
+
"source": [
|
| 346 |
+
"# Set device\n",
|
| 347 |
+
"device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n",
|
| 348 |
+
"device"
|
| 349 |
+
]
|
| 350 |
+
},
|
| 351 |
+
{
|
| 352 |
+
"cell_type": "code",
|
| 353 |
+
"execution_count": null,
|
| 354 |
+
"id": "a8808490-4734-470b-b330-470accf429b3",
|
| 355 |
+
"metadata": {
|
| 356 |
+
"colab": {
|
| 357 |
+
"base_uri": "https://localhost:8080/",
|
| 358 |
+
"height": 36
|
| 359 |
+
},
|
| 360 |
+
"executionInfo": {
|
| 361 |
+
"elapsed": 6,
|
| 362 |
+
"status": "ok",
|
| 363 |
+
"timestamp": 1726321476955,
|
| 364 |
+
"user": {
|
| 365 |
+
"displayName": "Darshil Parekh",
|
| 366 |
+
"userId": "08764169128860999444"
|
| 367 |
+
},
|
| 368 |
+
"user_tz": -330
|
| 369 |
+
},
|
| 370 |
+
"id": "a8808490-4734-470b-b330-470accf429b3",
|
| 371 |
+
"outputId": "eccf8d8e-e9bc-4bc9-ee97-b01219332110"
|
| 372 |
+
},
|
| 373 |
+
"outputs": [
|
| 374 |
+
{
|
| 375 |
+
"data": {
|
| 376 |
+
"application/vnd.google.colaboratory.intrinsic+json": {
|
| 377 |
+
"type": "string"
|
| 378 |
+
},
|
| 379 |
+
"text/plain": [
|
| 380 |
+
"'Tesla T4'"
|
| 381 |
+
]
|
| 382 |
+
},
|
| 383 |
+
"execution_count": 8,
|
| 384 |
+
"metadata": {},
|
| 385 |
+
"output_type": "execute_result"
|
| 386 |
+
}
|
| 387 |
+
],
|
| 388 |
+
"source": [
|
| 389 |
+
"torch.cuda.get_device_name(0)"
|
| 390 |
+
]
|
| 391 |
+
},
|
| 392 |
+
{
|
| 393 |
+
"cell_type": "code",
|
| 394 |
+
"execution_count": null,
|
| 395 |
+
"id": "K_3r1z0op4Ky",
|
| 396 |
+
"metadata": {
|
| 397 |
+
"id": "K_3r1z0op4Ky"
|
| 398 |
+
},
|
| 399 |
+
"outputs": [],
|
| 400 |
+
"source": [
|
| 401 |
+
"def get_wrong_image(dataset,correct_class):\n",
|
| 402 |
+
" for data in dataset:\n",
|
| 403 |
+
" if data['company'] != correct_class:\n",
|
| 404 |
+
" return data['image']"
|
| 405 |
+
]
|
| 406 |
+
},
|
| 407 |
+
{
|
| 408 |
+
"cell_type": "code",
|
| 409 |
+
"execution_count": null,
|
| 410 |
+
"id": "FOoGc_spmduX",
|
| 411 |
+
"metadata": {
|
| 412 |
+
"id": "FOoGc_spmduX"
|
| 413 |
+
},
|
| 414 |
+
"outputs": [],
|
| 415 |
+
"source": [
|
| 416 |
+
"# prompt: transform PIL Image to Tensor\\\n",
|
| 417 |
+
"\n",
|
| 418 |
+
"from torchvision import transforms\n",
|
| 419 |
+
"\n",
|
| 420 |
+
"transform = transforms.PILToTensor()\n",
|
| 421 |
+
"\n",
|
| 422 |
+
"TrainTransformedImage = []\n",
|
| 423 |
+
"WorngTrainTransformedImage = []\n",
|
| 424 |
+
"TrainTextVector = []\n",
|
| 425 |
+
"for data in reloaded_dataset[\"train\"]:\n",
|
| 426 |
+
" image_tensor = transform(data['image'].convert(\"RGB\"))\n",
|
| 427 |
+
" wrong_image_tensor = transform(get_wrong_image(reloaded_dataset[\"train\"],data['company']).convert(\"RGB\"))\n",
|
| 428 |
+
" TrainTransformedImage.append(image_tensor)\n",
|
| 429 |
+
" WorngTrainTransformedImage.append(wrong_image_tensor)\n",
|
| 430 |
+
" TrainTextVector.append(np.array(data['fulltext_vector'], dtype=\"float32\"))\n",
|
| 431 |
+
"\n"
|
| 432 |
+
]
|
| 433 |
+
},
|
| 434 |
+
{
|
| 435 |
+
"cell_type": "code",
|
| 436 |
+
"execution_count": null,
|
| 437 |
+
"id": "MxB4YVQKm8OV",
|
| 438 |
+
"metadata": {
|
| 439 |
+
"id": "MxB4YVQKm8OV"
|
| 440 |
+
},
|
| 441 |
+
"outputs": [],
|
| 442 |
+
"source": [
|
| 443 |
+
"# prompt: transform PIL Image to Tensor\\\n",
|
| 444 |
+
"\n",
|
| 445 |
+
"from torchvision import transforms\n",
|
| 446 |
+
"\n",
|
| 447 |
+
"transform = transforms.PILToTensor()\n",
|
| 448 |
+
"\n",
|
| 449 |
+
"TestTransformedImage = []\n",
|
| 450 |
+
"WorngTestTransformedImage = []\n",
|
| 451 |
+
"TestTextVector = []\n",
|
| 452 |
+
"for data in reloaded_dataset[\"test\"]:\n",
|
| 453 |
+
" image_tensor = transform(data['image'].convert(\"RGB\"))\n",
|
| 454 |
+
" wrong_image_tensor = transform(get_wrong_image(reloaded_dataset[\"test\"],data['company']).convert(\"RGB\"))\n",
|
| 455 |
+
" TestTransformedImage.append(image_tensor)\n",
|
| 456 |
+
" WorngTestTransformedImage.append(wrong_image_tensor)\n",
|
| 457 |
+
" TestTextVector.append(np.array(data['fulltext_vector'], dtype=\"float32\"))"
|
| 458 |
+
]
|
| 459 |
+
},
|
| 460 |
+
{
|
| 461 |
+
"cell_type": "code",
|
| 462 |
+
"execution_count": null,
|
| 463 |
+
"id": "zVPu-CLLlEo5",
|
| 464 |
+
"metadata": {
|
| 465 |
+
"id": "zVPu-CLLlEo5"
|
| 466 |
+
},
|
| 467 |
+
"outputs": [],
|
| 468 |
+
"source": [
|
| 469 |
+
"from torch.utils.data import Dataset\n",
|
| 470 |
+
"import numpy as np\n",
|
| 471 |
+
"\n",
|
| 472 |
+
"class EmojiDataset(Dataset):\n",
|
| 473 |
+
" def __init__(self,transformed_image,wrong_transformed_image,text_vector):\n",
|
| 474 |
+
" self.image_transform = transformed_image\n",
|
| 475 |
+
" self.wrong_image_transform = wrong_transformed_image\n",
|
| 476 |
+
" self.text_vector = text_vector\n",
|
| 477 |
+
"\n",
|
| 478 |
+
" def __len__(self):\n",
|
| 479 |
+
" return len(self.image_transform)\n",
|
| 480 |
+
"\n",
|
| 481 |
+
" def __getitem__(self, idx):\n",
|
| 482 |
+
" image = self.image_transform[idx]\n",
|
| 483 |
+
" wrong_image = self.wrong_image_transform[idx]\n",
|
| 484 |
+
" fulltext_vector = self.text_vector[idx]\n",
|
| 485 |
+
" return image.float(), fulltext_vector, wrong_image\n"
|
| 486 |
+
]
|
| 487 |
+
},
|
| 488 |
+
{
|
| 489 |
+
"cell_type": "code",
|
| 490 |
+
"execution_count": null,
|
| 491 |
+
"id": "E2dcO4FIg4Pj",
|
| 492 |
+
"metadata": {
|
| 493 |
+
"id": "E2dcO4FIg4Pj"
|
| 494 |
+
},
|
| 495 |
+
"outputs": [],
|
| 496 |
+
"source": [
|
| 497 |
+
"train_data = EmojiDataset(TrainTransformedImage,WorngTrainTransformedImage,TrainTextVector)\n",
|
| 498 |
+
"train_dataloader = DataLoader(train_data, batch_size=64, shuffle=True)\n",
|
| 499 |
+
"test_data = EmojiDataset(TestTransformedImage,WorngTestTransformedImage,TestTextVector)\n",
|
| 500 |
+
"test_dataloader = DataLoader(test_data, batch_size=64, shuffle=True)"
|
| 501 |
+
]
|
| 502 |
+
},
|
| 503 |
+
{
|
| 504 |
+
"cell_type": "markdown",
|
| 505 |
+
"id": "e6-pRimNwMnl",
|
| 506 |
+
"metadata": {
|
| 507 |
+
"id": "e6-pRimNwMnl"
|
| 508 |
+
},
|
| 509 |
+
"source": []
|
| 510 |
+
},
|
| 511 |
+
{
|
| 512 |
+
"cell_type": "code",
|
| 513 |
+
"execution_count": null,
|
| 514 |
+
"id": "O_oCUV-4f5I9",
|
| 515 |
+
"metadata": {
|
| 516 |
+
"colab": {
|
| 517 |
+
"base_uri": "https://localhost:8080/",
|
| 518 |
+
"height": 356
|
| 519 |
+
},
|
| 520 |
+
"executionInfo": {
|
| 521 |
+
"elapsed": 17115,
|
| 522 |
+
"status": "error",
|
| 523 |
+
"timestamp": 1726322100858,
|
| 524 |
+
"user": {
|
| 525 |
+
"displayName": "Darshil Parekh",
|
| 526 |
+
"userId": "08764169128860999444"
|
| 527 |
+
},
|
| 528 |
+
"user_tz": -330
|
| 529 |
+
},
|
| 530 |
+
"id": "O_oCUV-4f5I9",
|
| 531 |
+
"outputId": "eb0d861c-1d9b-416e-b85c-c6475cdaf246"
|
| 532 |
+
},
|
| 533 |
+
"outputs": [
|
| 534 |
+
{
|
| 535 |
+
"ename": "RuntimeError",
|
| 536 |
+
"evalue": "mat1 and mat2 shapes cannot be multiplied (12288x64 and 786432x1024)",
|
| 537 |
+
"output_type": "error",
|
| 538 |
+
"traceback": [
|
| 539 |
+
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
|
| 540 |
+
"\u001b[0;31mRuntimeError\u001b[0m Traceback (most recent call last)",
|
| 541 |
+
"\u001b[0;32m<ipython-input-21-32273bdaa5b7>\u001b[0m in \u001b[0;36m<cell line: 58>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 77\u001b[0m \u001b[0;31m# real_images = batch # Replace with your real image data\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 78\u001b[0m \u001b[0;31m# real_images = real_images.view(batch_size, -1) # flatten the real_images tensor\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 79\u001b[0;31m \u001b[0mreal_outputs\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mdiscriminator\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mreal_images\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 80\u001b[0m \u001b[0md_loss_real\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mcriterion\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mreal_outputs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mreal_labels\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 81\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
|
| 542 |
+
"\u001b[0;32m/usr/local/lib/python3.10/dist-packages/torch/nn/modules/module.py\u001b[0m in \u001b[0;36m_wrapped_call_impl\u001b[0;34m(self, *args, **kwargs)\u001b[0m\n\u001b[1;32m 1530\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_compiled_call_impl\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m# type: ignore[misc]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1531\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1532\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_call_impl\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 1533\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1534\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0m_call_impl\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
|
| 543 |
+
"\u001b[0;32m/usr/local/lib/python3.10/dist-packages/torch/nn/modules/module.py\u001b[0m in \u001b[0;36m_call_impl\u001b[0;34m(self, *args, **kwargs)\u001b[0m\n\u001b[1;32m 1539\u001b[0m \u001b[0;32mor\u001b[0m \u001b[0m_global_backward_pre_hooks\u001b[0m \u001b[0;32mor\u001b[0m \u001b[0m_global_backward_hooks\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1540\u001b[0m or _global_forward_hooks or _global_forward_pre_hooks):\n\u001b[0;32m-> 1541\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mforward_call\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 1542\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1543\u001b[0m \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
|
| 544 |
+
"\u001b[0;32m<ipython-input-21-32273bdaa5b7>\u001b[0m in \u001b[0;36mforward\u001b[0;34m(self, x)\u001b[0m\n\u001b[1;32m 37\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 38\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mforward\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mx\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 39\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmodel\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 40\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 41\u001b[0m \u001b[0;31m# Hyperparameters\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
|
| 545 |
+
"\u001b[0;32m/usr/local/lib/python3.10/dist-packages/torch/nn/modules/module.py\u001b[0m in \u001b[0;36m_wrapped_call_impl\u001b[0;34m(self, *args, **kwargs)\u001b[0m\n\u001b[1;32m 1530\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_compiled_call_impl\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m# type: ignore[misc]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1531\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1532\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_call_impl\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 1533\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1534\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0m_call_impl\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
|
| 546 |
+
"\u001b[0;32m/usr/local/lib/python3.10/dist-packages/torch/nn/modules/module.py\u001b[0m in \u001b[0;36m_call_impl\u001b[0;34m(self, *args, **kwargs)\u001b[0m\n\u001b[1;32m 1539\u001b[0m \u001b[0;32mor\u001b[0m \u001b[0m_global_backward_pre_hooks\u001b[0m \u001b[0;32mor\u001b[0m \u001b[0m_global_backward_hooks\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1540\u001b[0m or _global_forward_hooks or _global_forward_pre_hooks):\n\u001b[0;32m-> 1541\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mforward_call\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 1542\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1543\u001b[0m \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
|
| 547 |
+
"\u001b[0;32m/usr/local/lib/python3.10/dist-packages/torch/nn/modules/container.py\u001b[0m in \u001b[0;36mforward\u001b[0;34m(self, input)\u001b[0m\n\u001b[1;32m 215\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mforward\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0minput\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 216\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mmodule\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 217\u001b[0;31m \u001b[0minput\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mmodule\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 218\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0minput\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 219\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
|
| 548 |
+
"\u001b[0;32m/usr/local/lib/python3.10/dist-packages/torch/nn/modules/module.py\u001b[0m in \u001b[0;36m_wrapped_call_impl\u001b[0;34m(self, *args, **kwargs)\u001b[0m\n\u001b[1;32m 1530\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_compiled_call_impl\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m# type: ignore[misc]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1531\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1532\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_call_impl\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 1533\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1534\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0m_call_impl\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
|
| 549 |
+
"\u001b[0;32m/usr/local/lib/python3.10/dist-packages/torch/nn/modules/module.py\u001b[0m in \u001b[0;36m_call_impl\u001b[0;34m(self, *args, **kwargs)\u001b[0m\n\u001b[1;32m 1539\u001b[0m \u001b[0;32mor\u001b[0m \u001b[0m_global_backward_pre_hooks\u001b[0m \u001b[0;32mor\u001b[0m \u001b[0m_global_backward_hooks\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1540\u001b[0m or _global_forward_hooks or _global_forward_pre_hooks):\n\u001b[0;32m-> 1541\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mforward_call\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 1542\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1543\u001b[0m \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
|
| 550 |
+
"\u001b[0;32m/usr/local/lib/python3.10/dist-packages/torch/nn/modules/linear.py\u001b[0m in \u001b[0;36mforward\u001b[0;34m(self, input)\u001b[0m\n\u001b[1;32m 114\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 115\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mforward\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0minput\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mTensor\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m->\u001b[0m \u001b[0mTensor\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 116\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mF\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mlinear\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mweight\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mbias\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 117\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 118\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mextra_repr\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m->\u001b[0m \u001b[0mstr\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
|
| 551 |
+
"\u001b[0;31mRuntimeError\u001b[0m: mat1 and mat2 shapes cannot be multiplied (12288x64 and 786432x1024)"
|
| 552 |
+
]
|
| 553 |
+
}
|
| 554 |
+
],
|
| 555 |
+
"source": [
|
| 556 |
+
"# prompt: Write a python code to train a stackGan model to generate a image of size 64x64x4 using a fulltext_vector of size 100\n",
|
| 557 |
+
"\n",
|
| 558 |
+
"import torch\n",
|
| 559 |
+
"import torch.nn as nn\n",
|
| 560 |
+
"from torch.utils.data import DataLoader\n",
|
| 561 |
+
"\n",
|
| 562 |
+
"class Generator(nn.Module):\n",
|
| 563 |
+
" def __init__(self, input_size, output_size):\n",
|
| 564 |
+
" super(Generator, self).__init__()\n",
|
| 565 |
+
" self.model = nn.Sequential(\n",
|
| 566 |
+
" nn.Linear(input_size, 256),\n",
|
| 567 |
+
" nn.ReLU(),\n",
|
| 568 |
+
" nn.Linear(256, 512),\n",
|
| 569 |
+
" nn.ReLU(),\n",
|
| 570 |
+
" nn.Linear(512, 1024),\n",
|
| 571 |
+
" nn.ReLU(),\n",
|
| 572 |
+
" nn.Linear(1024, output_size),\n",
|
| 573 |
+
" nn.Tanh()\n",
|
| 574 |
+
" )\n",
|
| 575 |
+
"\n",
|
| 576 |
+
" def forward(self, x):\n",
|
| 577 |
+
" return self.model(x)\n",
|
| 578 |
+
"\n",
|
| 579 |
+
"class Discriminator(nn.Module):\n",
|
| 580 |
+
" def __init__(self, input_size):\n",
|
| 581 |
+
" super(Discriminator, self).__init__()\n",
|
| 582 |
+
" self.model = nn.Sequential(\n",
|
| 583 |
+
" nn.Linear(input_size, 1024),\n",
|
| 584 |
+
" nn.LeakyReLU(0.2),\n",
|
| 585 |
+
" nn.Linear(1024, 512),\n",
|
| 586 |
+
" nn.LeakyReLU(0.2),\n",
|
| 587 |
+
" nn.Linear(512, 256),\n",
|
| 588 |
+
" nn.LeakyReLU(0.2),\n",
|
| 589 |
+
" nn.Linear(256, 1),\n",
|
| 590 |
+
" nn.Sigmoid()\n",
|
| 591 |
+
" )\n",
|
| 592 |
+
"\n",
|
| 593 |
+
" def forward(self, x):\n",
|
| 594 |
+
" return self.model(x)\n",
|
| 595 |
+
"\n",
|
| 596 |
+
"# Hyperparameters\n",
|
| 597 |
+
"input_size = 100 # Size of the fulltext_vector\n",
|
| 598 |
+
"output_size = 64* 64 * 64 * 3 # Size of the generated image (64x64x4)\n",
|
| 599 |
+
"batch_size = 64\n",
|
| 600 |
+
"learning_rate = 0.0002\n",
|
| 601 |
+
"num_epochs = 100\n",
|
| 602 |
+
"\n",
|
| 603 |
+
"# Create generator and discriminator\n",
|
| 604 |
+
"generator = Generator(input_size, output_size).to(device)\n",
|
| 605 |
+
"discriminator = Discriminator(output_size).to(device)\n",
|
| 606 |
+
"\n",
|
| 607 |
+
"# Define loss function and optimizers\n",
|
| 608 |
+
"criterion = nn.BCELoss()\n",
|
| 609 |
+
"optimizer_G = optim.Adam(generator.parameters(), lr=learning_rate)\n",
|
| 610 |
+
"optimizer_D = optim.Adam(discriminator.parameters(), lr=learning_rate)\n",
|
| 611 |
+
"\n",
|
| 612 |
+
"# Training loop\n",
|
| 613 |
+
"for epoch in range(num_epochs):\n",
|
| 614 |
+
" #for batch in test_dataloader:\n",
|
| 615 |
+
" for i, batch in enumerate(test_dataloader, 0):\n",
|
| 616 |
+
"\n",
|
| 617 |
+
" real_images = batch[0].to(device)\n",
|
| 618 |
+
" right_embed = batch[1].to(device)\n",
|
| 619 |
+
" wrong_images = batch[2].to(device)\n",
|
| 620 |
+
" # Generate random noise\n",
|
| 621 |
+
" noise = torch.randn(batch_size, input_size).to(device)\n",
|
| 622 |
+
"\n",
|
| 623 |
+
" # Generate fake images\n",
|
| 624 |
+
" fake_images = generator(right_embed)\n",
|
| 625 |
+
"\n",
|
| 626 |
+
" # Train discriminator\n",
|
| 627 |
+
" optimizer_D.zero_grad()\n",
|
| 628 |
+
" real_labels = torch.ones(batch_size, 1).to(device)\n",
|
| 629 |
+
" fake_labels = torch.zeros(batch_size, 1).to(device)\n",
|
| 630 |
+
"\n",
|
| 631 |
+
" # Real images (assuming you have real images in your dataset)\n",
|
| 632 |
+
" # real_images = batch # Replace with your real image data\n",
|
| 633 |
+
" # real_images = real_images.view(batch_size, -1) # flatten the real_images tensor\n",
|
| 634 |
+
" real_outputs = discriminator(real_images)\n",
|
| 635 |
+
" d_loss_real = criterion(real_outputs, real_labels)\n",
|
| 636 |
+
"\n",
|
| 637 |
+
" fake_outputs = discriminator(fake_images.detach())\n",
|
| 638 |
+
" d_loss_fake = criterion(fake_outputs, fake_labels)\n",
|
| 639 |
+
" d_loss = d_loss_real + d_loss_fake\n",
|
| 640 |
+
" d_loss = d_loss_fake\n",
|
| 641 |
+
" d_loss.backward()\n",
|
| 642 |
+
" optimizer_D.step()\n",
|
| 643 |
+
"\n",
|
| 644 |
+
" # Train generator\n",
|
| 645 |
+
" optimizer_G.zero_grad()\n",
|
| 646 |
+
" fake_outputs = discriminator(fake_images)\n",
|
| 647 |
+
" g_loss = criterion(fake_outputs, real_labels)\n",
|
| 648 |
+
" g_loss.backward()\n",
|
| 649 |
+
" optimizer_G.step()\n",
|
| 650 |
+
"\n",
|
| 651 |
+
" print(f\"Epoch [{epoch+1}/{num_epochs}], D Loss: {d_loss.item():.4f}, G Loss: {g_loss.item():.4f}\")\n",
|
| 652 |
+
"\n",
|
| 653 |
+
"print(\"Training finished!\")"
|
| 654 |
+
]
|
| 655 |
+
},
|
| 656 |
+
{
|
| 657 |
+
"cell_type": "code",
|
| 658 |
+
"execution_count": null,
|
| 659 |
+
"id": "408f4137-3658-4b46-8034-2591773e70eb",
|
| 660 |
+
"metadata": {
|
| 661 |
+
"editable": true,
|
| 662 |
+
"id": "408f4137-3658-4b46-8034-2591773e70eb",
|
| 663 |
+
"tags": []
|
| 664 |
+
},
|
| 665 |
+
"outputs": [],
|
| 666 |
+
"source": [
|
| 667 |
+
"class generator(nn.Module):\n",
|
| 668 |
+
" def __init__(self):\n",
|
| 669 |
+
" super(generator, self).__init__()\n",
|
| 670 |
+
" self.image_size = 64\n",
|
| 671 |
+
" self.num_channels = 3\n",
|
| 672 |
+
" self.noise_dim = 100\n",
|
| 673 |
+
" self.embed_dim = 100\n",
|
| 674 |
+
" self.projected_embed_dim = 128\n",
|
| 675 |
+
" self.latent_dim = self.noise_dim + self.projected_embed_dim\n",
|
| 676 |
+
" self.ngf = 64\n",
|
| 677 |
+
"\n",
|
| 678 |
+
" self.projection = nn.Sequential(\n",
|
| 679 |
+
" nn.Linear(in_features=self.embed_dim, out_features=self.projected_embed_dim),\n",
|
| 680 |
+
" nn.BatchNorm1d(num_features=self.projected_embed_dim),\n",
|
| 681 |
+
" nn.LeakyReLU(negative_slope=0.2, inplace=True)\n",
|
| 682 |
+
" )\n",
|
| 683 |
+
"\n",
|
| 684 |
+
" self.netG = nn.Sequential(\n",
|
| 685 |
+
" nn.ConvTranspose2d(self.latent_dim, self.ngf * 8, 4, 1, 0, bias=False),\n",
|
| 686 |
+
" nn.BatchNorm2d(self.ngf * 8),\n",
|
| 687 |
+
" nn.ReLU(True),\n",
|
| 688 |
+
" # state size. (ngf*8) x 4 x 4\n",
|
| 689 |
+
" nn.ConvTranspose2d(self.ngf * 8, self.ngf * 4, 4, 2, 1, bias=False),\n",
|
| 690 |
+
" nn.BatchNorm2d(self.ngf * 4),\n",
|
| 691 |
+
" nn.ReLU(True),\n",
|
| 692 |
+
" # state size. (ngf*4) x 8 x 8\n",
|
| 693 |
+
" nn.ConvTranspose2d(self.ngf * 4, self.ngf * 2, 4, 2, 1, bias=False),\n",
|
| 694 |
+
" nn.BatchNorm2d(self.ngf * 2),\n",
|
| 695 |
+
" nn.ReLU(True),\n",
|
| 696 |
+
" # state size. (ngf*2) x 16 x 16\n",
|
| 697 |
+
" nn.ConvTranspose2d(self.ngf * 2,self.ngf, 4, 2, 1, bias=False),\n",
|
| 698 |
+
" nn.BatchNorm2d(self.ngf),\n",
|
| 699 |
+
" nn.ReLU(True),\n",
|
| 700 |
+
" # state size. (ngf) x 32 x 32\n",
|
| 701 |
+
" nn.ConvTranspose2d(self.ngf, self.num_channels, 4, 2, 1, bias=False),\n",
|
| 702 |
+
" nn.Tanh()\n",
|
| 703 |
+
" # state size. (num_channels) x 64 x 64\n",
|
| 704 |
+
" )\n",
|
| 705 |
+
"\n",
|
| 706 |
+
"\n",
|
| 707 |
+
" def forward(self, embed_vector, z):\n",
|
| 708 |
+
"\n",
|
| 709 |
+
" projected_embed = self.projection(embed_vector).unsqueeze(2).unsqueeze(3)\n",
|
| 710 |
+
" latent_vector = torch.cat([projected_embed, z], 1)\n",
|
| 711 |
+
" output = self.netG(latent_vector)\n",
|
| 712 |
+
"\n",
|
| 713 |
+
" return output"
|
| 714 |
+
]
|
| 715 |
+
},
|
| 716 |
+
{
|
| 717 |
+
"cell_type": "code",
|
| 718 |
+
"execution_count": null,
|
| 719 |
+
"id": "e2ce6d2d-87c7-4cde-a7e2-e382d8b334b0",
|
| 720 |
+
"metadata": {
|
| 721 |
+
"colab": {
|
| 722 |
+
"base_uri": "https://localhost:8080/"
|
| 723 |
+
},
|
| 724 |
+
"executionInfo": {
|
| 725 |
+
"elapsed": 582,
|
| 726 |
+
"status": "ok",
|
| 727 |
+
"timestamp": 1726322184976,
|
| 728 |
+
"user": {
|
| 729 |
+
"displayName": "Darshil Parekh",
|
| 730 |
+
"userId": "08764169128860999444"
|
| 731 |
+
},
|
| 732 |
+
"user_tz": -330
|
| 733 |
+
},
|
| 734 |
+
"id": "e2ce6d2d-87c7-4cde-a7e2-e382d8b334b0",
|
| 735 |
+
"outputId": "e79717ad-f16d-4e61-f988-102ac801f50d"
|
| 736 |
+
},
|
| 737 |
+
"outputs": [
|
| 738 |
+
{
|
| 739 |
+
"name": "stdout",
|
| 740 |
+
"output_type": "stream",
|
| 741 |
+
"text": [
|
| 742 |
+
"generator(\n",
|
| 743 |
+
" (projection): Sequential(\n",
|
| 744 |
+
" (0): Linear(in_features=100, out_features=128, bias=True)\n",
|
| 745 |
+
" (1): BatchNorm1d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
|
| 746 |
+
" (2): LeakyReLU(negative_slope=0.2, inplace=True)\n",
|
| 747 |
+
" )\n",
|
| 748 |
+
" (netG): Sequential(\n",
|
| 749 |
+
" (0): ConvTranspose2d(228, 512, kernel_size=(4, 4), stride=(1, 1), bias=False)\n",
|
| 750 |
+
" (1): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
|
| 751 |
+
" (2): ReLU(inplace=True)\n",
|
| 752 |
+
" (3): ConvTranspose2d(512, 256, kernel_size=(4, 4), stride=(2, 2), padding=(1, 1), bias=False)\n",
|
| 753 |
+
" (4): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
|
| 754 |
+
" (5): ReLU(inplace=True)\n",
|
| 755 |
+
" (6): ConvTranspose2d(256, 128, kernel_size=(4, 4), stride=(2, 2), padding=(1, 1), bias=False)\n",
|
| 756 |
+
" (7): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
|
| 757 |
+
" (8): ReLU(inplace=True)\n",
|
| 758 |
+
" (9): ConvTranspose2d(128, 64, kernel_size=(4, 4), stride=(2, 2), padding=(1, 1), bias=False)\n",
|
| 759 |
+
" (10): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
|
| 760 |
+
" (11): ReLU(inplace=True)\n",
|
| 761 |
+
" (12): ConvTranspose2d(64, 3, kernel_size=(4, 4), stride=(2, 2), padding=(1, 1), bias=False)\n",
|
| 762 |
+
" (13): Tanh()\n",
|
| 763 |
+
" )\n",
|
| 764 |
+
")\n"
|
| 765 |
+
]
|
| 766 |
+
}
|
| 767 |
+
],
|
| 768 |
+
"source": [
|
| 769 |
+
"netG = generator().to(device)\n",
|
| 770 |
+
"# Handle multi-gpu if desired\n",
|
| 771 |
+
"netG.apply(Utils.weights_init)\n",
|
| 772 |
+
"# Print the model\n",
|
| 773 |
+
"print(netG)"
|
| 774 |
+
]
|
| 775 |
+
},
|
| 776 |
+
{
|
| 777 |
+
"cell_type": "code",
|
| 778 |
+
"execution_count": null,
|
| 779 |
+
"id": "8ca1cf01-943e-47a8-acf7-9576243d5119",
|
| 780 |
+
"metadata": {
|
| 781 |
+
"id": "8ca1cf01-943e-47a8-acf7-9576243d5119"
|
| 782 |
+
},
|
| 783 |
+
"outputs": [],
|
| 784 |
+
"source": [
|
| 785 |
+
"class discriminator(nn.Module):\n",
|
| 786 |
+
" def __init__(self):\n",
|
| 787 |
+
" super(discriminator, self).__init__()\n",
|
| 788 |
+
" self.image_size = 64\n",
|
| 789 |
+
" self.num_channels = 3\n",
|
| 790 |
+
" self.embed_dim = 100\n",
|
| 791 |
+
" self.projected_embed_dim = 128\n",
|
| 792 |
+
" self.ndf = 64\n",
|
| 793 |
+
" self.B_dim = 128\n",
|
| 794 |
+
" self.C_dim = 16\n",
|
| 795 |
+
"\n",
|
| 796 |
+
" self.netD_1 = nn.Sequential(\n",
|
| 797 |
+
" # input is (nc) x 64 x 64\n",
|
| 798 |
+
" nn.Conv2d(self.num_channels, self.ndf, 4, 2, 1, bias=False),\n",
|
| 799 |
+
" nn.LeakyReLU(0.2, inplace=True),\n",
|
| 800 |
+
" # state size. (ndf) x 32 x 32\n",
|
| 801 |
+
" nn.Conv2d(self.ndf, self.ndf * 2, 4, 2, 1, bias=False),\n",
|
| 802 |
+
" nn.BatchNorm2d(self.ndf * 2),\n",
|
| 803 |
+
" nn.LeakyReLU(0.2, inplace=True),\n",
|
| 804 |
+
" # state size. (ndf*2) x 16 x 16\n",
|
| 805 |
+
" nn.Conv2d(self.ndf * 2, self.ndf * 4, 4, 2, 1, bias=False),\n",
|
| 806 |
+
" nn.BatchNorm2d(self.ndf * 4),\n",
|
| 807 |
+
" nn.LeakyReLU(0.2, inplace=True),\n",
|
| 808 |
+
" # state size. (ndf*4) x 8 x 8\n",
|
| 809 |
+
" nn.Conv2d(self.ndf * 4, self.ndf * 8, 4, 2, 1, bias=False),\n",
|
| 810 |
+
" nn.BatchNorm2d(self.ndf * 8),\n",
|
| 811 |
+
" nn.LeakyReLU(0.2, inplace=True),\n",
|
| 812 |
+
" )\n",
|
| 813 |
+
"\n",
|
| 814 |
+
" self.projector = utils.Concat_embed(self.embed_dim, self.projected_embed_dim)\n",
|
| 815 |
+
"\n",
|
| 816 |
+
" self.netD_2 = nn.Sequential(\n",
|
| 817 |
+
" # state size. (ndf*8) x 4 x 4\n",
|
| 818 |
+
" nn.Conv2d(self.ndf * 8 + self.projected_embed_dim, 1, 4, 1, 0, bias=False),\n",
|
| 819 |
+
" nn.Sigmoid()\n",
|
| 820 |
+
" )\n",
|
| 821 |
+
"\n",
|
| 822 |
+
" def forward(self, inp, embed):\n",
|
| 823 |
+
" x_intermediate = self.netD_1(inp)\n",
|
| 824 |
+
" x = self.projector(x_intermediate, embed)\n",
|
| 825 |
+
" x = self.netD_2(x)\n",
|
| 826 |
+
"\n",
|
| 827 |
+
" return x.view(-1, 1).squeeze(1) , x_intermediate"
|
| 828 |
+
]
|
| 829 |
+
},
|
| 830 |
+
{
|
| 831 |
+
"cell_type": "code",
|
| 832 |
+
"execution_count": null,
|
| 833 |
+
"id": "88574088-bcca-49dd-91c2-e9f6b40e4b8f",
|
| 834 |
+
"metadata": {
|
| 835 |
+
"colab": {
|
| 836 |
+
"base_uri": "https://localhost:8080/"
|
| 837 |
+
},
|
| 838 |
+
"executionInfo": {
|
| 839 |
+
"elapsed": 3,
|
| 840 |
+
"status": "ok",
|
| 841 |
+
"timestamp": 1726322190144,
|
| 842 |
+
"user": {
|
| 843 |
+
"displayName": "Darshil Parekh",
|
| 844 |
+
"userId": "08764169128860999444"
|
| 845 |
+
},
|
| 846 |
+
"user_tz": -330
|
| 847 |
+
},
|
| 848 |
+
"id": "88574088-bcca-49dd-91c2-e9f6b40e4b8f",
|
| 849 |
+
"outputId": "d59ce15c-979b-4b9d-d4db-0bdc3526b7a2"
|
| 850 |
+
},
|
| 851 |
+
"outputs": [
|
| 852 |
+
{
|
| 853 |
+
"name": "stdout",
|
| 854 |
+
"output_type": "stream",
|
| 855 |
+
"text": [
|
| 856 |
+
"discriminator(\n",
|
| 857 |
+
" (netD_1): Sequential(\n",
|
| 858 |
+
" (0): Conv2d(3, 64, kernel_size=(4, 4), stride=(2, 2), padding=(1, 1), bias=False)\n",
|
| 859 |
+
" (1): LeakyReLU(negative_slope=0.2, inplace=True)\n",
|
| 860 |
+
" (2): Conv2d(64, 128, kernel_size=(4, 4), stride=(2, 2), padding=(1, 1), bias=False)\n",
|
| 861 |
+
" (3): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
|
| 862 |
+
" (4): LeakyReLU(negative_slope=0.2, inplace=True)\n",
|
| 863 |
+
" (5): Conv2d(128, 256, kernel_size=(4, 4), stride=(2, 2), padding=(1, 1), bias=False)\n",
|
| 864 |
+
" (6): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
|
| 865 |
+
" (7): LeakyReLU(negative_slope=0.2, inplace=True)\n",
|
| 866 |
+
" (8): Conv2d(256, 512, kernel_size=(4, 4), stride=(2, 2), padding=(1, 1), bias=False)\n",
|
| 867 |
+
" (9): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
|
| 868 |
+
" (10): LeakyReLU(negative_slope=0.2, inplace=True)\n",
|
| 869 |
+
" )\n",
|
| 870 |
+
" (projector): Concat_embed(\n",
|
| 871 |
+
" (projection): Sequential(\n",
|
| 872 |
+
" (0): Linear(in_features=100, out_features=128, bias=True)\n",
|
| 873 |
+
" (1): BatchNorm1d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
|
| 874 |
+
" (2): LeakyReLU(negative_slope=0.2, inplace=True)\n",
|
| 875 |
+
" )\n",
|
| 876 |
+
" )\n",
|
| 877 |
+
" (netD_2): Sequential(\n",
|
| 878 |
+
" (0): Conv2d(640, 1, kernel_size=(4, 4), stride=(1, 1), bias=False)\n",
|
| 879 |
+
" (1): Sigmoid()\n",
|
| 880 |
+
" )\n",
|
| 881 |
+
")\n"
|
| 882 |
+
]
|
| 883 |
+
}
|
| 884 |
+
],
|
| 885 |
+
"source": [
|
| 886 |
+
"netD_1 = discriminator().to(device)\n",
|
| 887 |
+
"# Handle multi-gpu if desired\n",
|
| 888 |
+
"netD_1.apply(Utils.weights_init)\n",
|
| 889 |
+
"# Print the model\n",
|
| 890 |
+
"print(netD_1)"
|
| 891 |
+
]
|
| 892 |
+
},
|
| 893 |
+
{
|
| 894 |
+
"cell_type": "code",
|
| 895 |
+
"execution_count": null,
|
| 896 |
+
"id": "118be16d-27aa-41c1-a514-ca1fc95c54df",
|
| 897 |
+
"metadata": {
|
| 898 |
+
"id": "118be16d-27aa-41c1-a514-ca1fc95c54df"
|
| 899 |
+
},
|
| 900 |
+
"outputs": [],
|
| 901 |
+
"source": [
|
| 902 |
+
"class gan_factory(object):\n",
|
| 903 |
+
"\n",
|
| 904 |
+
" @staticmethod\n",
|
| 905 |
+
" def generator_factory(type):\n",
|
| 906 |
+
" if type == 'gan':\n",
|
| 907 |
+
" return generator()\n",
|
| 908 |
+
"\n",
|
| 909 |
+
" @staticmethod\n",
|
| 910 |
+
" def discriminator_factory(type):\n",
|
| 911 |
+
" if type == 'gan':\n",
|
| 912 |
+
" return discriminator()"
|
| 913 |
+
]
|
| 914 |
+
},
|
| 915 |
+
{
|
| 916 |
+
"cell_type": "code",
|
| 917 |
+
"execution_count": null,
|
| 918 |
+
"id": "16c6ac96-644c-4269-8731-ab447ad478fd",
|
| 919 |
+
"metadata": {
|
| 920 |
+
"editable": true,
|
| 921 |
+
"id": "16c6ac96-644c-4269-8731-ab447ad478fd",
|
| 922 |
+
"tags": []
|
| 923 |
+
},
|
| 924 |
+
"outputs": [],
|
| 925 |
+
"source": [
|
| 926 |
+
"import numpy as np\n",
|
| 927 |
+
"import torch\n",
|
| 928 |
+
"import yaml\n",
|
| 929 |
+
"from torch import nn\n",
|
| 930 |
+
"from torch.autograd import Variable\n",
|
| 931 |
+
"from torch.utils.data import DataLoader\n",
|
| 932 |
+
"\n",
|
| 933 |
+
"from utils import Utils, Logger\n",
|
| 934 |
+
"from PIL import Image\n",
|
| 935 |
+
"import os\n",
|
| 936 |
+
"\n",
|
| 937 |
+
"class Trainer(object):\n",
|
| 938 |
+
" def __init__(self, type, dataset, split, lr,\n",
|
| 939 |
+
" save_path, l1_coef, l2_coef, batch_size, num_workers, epochs):\n",
|
| 940 |
+
"\n",
|
| 941 |
+
" self.generator = torch.nn.DataParallel(gan_factory.generator_factory(type).cuda())\n",
|
| 942 |
+
" self.discriminator = torch.nn.DataParallel(gan_factory.discriminator_factory(type).cuda())\n",
|
| 943 |
+
"\n",
|
| 944 |
+
" self.discriminator.apply(Utils.weights_init)\n",
|
| 945 |
+
"\n",
|
| 946 |
+
" self.generator.apply(Utils.weights_init)\n",
|
| 947 |
+
"\n",
|
| 948 |
+
" self.dataset = dataset\n",
|
| 949 |
+
"\n",
|
| 950 |
+
" #print \"Image = \",len(self.dataset)\n",
|
| 951 |
+
" self.noise_dim = 100\n",
|
| 952 |
+
" self.batch_size = batch_size\n",
|
| 953 |
+
" self.num_workers = num_workers\n",
|
| 954 |
+
" self.lr = lr\n",
|
| 955 |
+
" self.beta1 = 0.5\n",
|
| 956 |
+
" self.num_epochs = epochs\n",
|
| 957 |
+
"\n",
|
| 958 |
+
"\n",
|
| 959 |
+
" self.l1_coef = l1_coef\n",
|
| 960 |
+
" self.l2_coef = l2_coef\n",
|
| 961 |
+
"\n",
|
| 962 |
+
" self.data_loader = DataLoader(self.dataset, batch_size=self.batch_size, shuffle=True, num_workers=self.num_workers)\n",
|
| 963 |
+
"\n",
|
| 964 |
+
" self.optimD = torch.optim.Adam(self.discriminator.parameters(), lr=self.lr, betas=(self.beta1, 0.999))\n",
|
| 965 |
+
" self.optimG = torch.optim.Adam(self.generator.parameters(), lr=self.lr, betas=(self.beta1, 0.999))\n",
|
| 966 |
+
"\n",
|
| 967 |
+
" self.logger = Logger()\n",
|
| 968 |
+
" self.checkpoints_path = 'checkpoints'\n",
|
| 969 |
+
" self.save_path = save_path\n",
|
| 970 |
+
" self.type = type\n",
|
| 971 |
+
"\n",
|
| 972 |
+
" def train(self, cls):\n",
|
| 973 |
+
"\n",
|
| 974 |
+
" if self.type == 'gan':\n",
|
| 975 |
+
" self._train_gan(cls)\n",
|
| 976 |
+
"\n",
|
| 977 |
+
"\n",
|
| 978 |
+
" def _train_gan(self, cls):\n",
|
| 979 |
+
" criterion = nn.BCELoss()\n",
|
| 980 |
+
" l2_loss = nn.MSELoss()\n",
|
| 981 |
+
" l1_loss = nn.L1Loss()\n",
|
| 982 |
+
" #print(\"Started Training\")\n",
|
| 983 |
+
" for epoch in range(self.num_epochs):\n",
|
| 984 |
+
" iteration = 0\n",
|
| 985 |
+
" #print(\"Starting Iter :\",iteration)\n",
|
| 986 |
+
" for sample in self.data_loader:\n",
|
| 987 |
+
" #print('Inside Dataloader loop is')\n",
|
| 988 |
+
" iteration += 1\n",
|
| 989 |
+
" right_images = sample[0]\n",
|
| 990 |
+
" right_embed = sample[1]\n",
|
| 991 |
+
" wrong_images = sample[2]\n",
|
| 992 |
+
"\n",
|
| 993 |
+
"\n",
|
| 994 |
+
"\n",
|
| 995 |
+
" right_images = Variable(right_images.float()).cuda()\n",
|
| 996 |
+
" right_embed = Variable(right_embed.float()).cuda()\n",
|
| 997 |
+
" wrong_images = Variable(wrong_images.float()).cuda()\n",
|
| 998 |
+
"\n",
|
| 999 |
+
" #print(\"Data Loaded\")\n",
|
| 1000 |
+
"\n",
|
| 1001 |
+
" real_labels = torch.ones(right_images.size(0))\n",
|
| 1002 |
+
" fake_labels = torch.zeros(right_images.size(0))\n",
|
| 1003 |
+
"\n",
|
| 1004 |
+
" smoothed_real_labels = torch.FloatTensor(Utils.smooth_label(real_labels.numpy(), -0.1))\n",
|
| 1005 |
+
"\n",
|
| 1006 |
+
" real_labels = Variable(real_labels).cuda()\n",
|
| 1007 |
+
" smoothed_real_labels = Variable(smoothed_real_labels).cuda()\n",
|
| 1008 |
+
" fake_labels = Variable(fake_labels).cuda()\n",
|
| 1009 |
+
"\n",
|
| 1010 |
+
" # Train the discriminator\n",
|
| 1011 |
+
" self.discriminator.zero_grad()\n",
|
| 1012 |
+
" outputs, activation_real = self.discriminator(right_images, right_embed)\n",
|
| 1013 |
+
" real_loss = criterion(outputs, smoothed_real_labels)\n",
|
| 1014 |
+
" real_score = outputs\n",
|
| 1015 |
+
"\n",
|
| 1016 |
+
" if cls:\n",
|
| 1017 |
+
" outputs, _ = self.discriminator(wrong_images, right_embed)\n",
|
| 1018 |
+
" wrong_loss = criterion(outputs, fake_labels)\n",
|
| 1019 |
+
" wrong_score = outputs\n",
|
| 1020 |
+
"\n",
|
| 1021 |
+
" noise = Variable(torch.randn(right_images.size(0), 100)).cuda()\n",
|
| 1022 |
+
" noise = noise.view(noise.size(0), 100, 1, 1)\n",
|
| 1023 |
+
" fake_images = self.generator(right_embed, noise)\n",
|
| 1024 |
+
" outputs, _ = self.discriminator(fake_images, right_embed)\n",
|
| 1025 |
+
" fake_loss = criterion(outputs, fake_labels)\n",
|
| 1026 |
+
" fake_score = outputs\n",
|
| 1027 |
+
"\n",
|
| 1028 |
+
" d_loss = real_loss + fake_loss\n",
|
| 1029 |
+
"\n",
|
| 1030 |
+
" if cls:\n",
|
| 1031 |
+
" d_loss = d_loss + wrong_loss\n",
|
| 1032 |
+
"\n",
|
| 1033 |
+
" d_loss.backward()\n",
|
| 1034 |
+
" self.optimD.step()\n",
|
| 1035 |
+
"\n",
|
| 1036 |
+
" # Train the generator\n",
|
| 1037 |
+
" self.generator.zero_grad()\n",
|
| 1038 |
+
" noise = Variable(torch.randn(right_images.size(0), 100)).cuda()\n",
|
| 1039 |
+
" noise = noise.view(noise.size(0), 100, 1, 1)\n",
|
| 1040 |
+
" fake_images = self.generator(right_embed, noise)\n",
|
| 1041 |
+
" outputs, activation_fake = self.discriminator(fake_images, right_embed)\n",
|
| 1042 |
+
" _, activation_real = self.discriminator(right_images, right_embed)\n",
|
| 1043 |
+
"\n",
|
| 1044 |
+
" activation_fake = torch.mean(activation_fake, 0) #try with median and check if it converges\n",
|
| 1045 |
+
" activation_real = torch.mean(activation_real, 0) #try with median and check if it converges\n",
|
| 1046 |
+
"\n",
|
| 1047 |
+
"\n",
|
| 1048 |
+
" g_loss = criterion(outputs, real_labels) + self.l2_coef * l2_loss(activation_fake, activation_real.detach()) + self.l1_coef * l1_loss(fake_images, right_images)\n",
|
| 1049 |
+
"\n",
|
| 1050 |
+
" g_loss.backward()\n",
|
| 1051 |
+
" self.optimG.step()\n",
|
| 1052 |
+
"\n",
|
| 1053 |
+
" #print('Completed Iter:', iteration)\n",
|
| 1054 |
+
"\n",
|
| 1055 |
+
" self.logger.log_iteration_gan(epoch, iteration, d_loss, g_loss, real_score, fake_score)\n",
|
| 1056 |
+
"\n",
|
| 1057 |
+
"\n",
|
| 1058 |
+
" if (epoch) % 10 == 0:\n",
|
| 1059 |
+
" print('epoch', epoch, 'complete')\n",
|
| 1060 |
+
" Utils.save_checkpoint(self.discriminator, self.generator, self.checkpoints_path, self.save_path, epoch)"
|
| 1061 |
+
]
|
| 1062 |
+
},
|
| 1063 |
+
{
|
| 1064 |
+
"cell_type": "code",
|
| 1065 |
+
"execution_count": null,
|
| 1066 |
+
"id": "0d815894-238b-473d-8c29-cd2ebd948c59",
|
| 1067 |
+
"metadata": {
|
| 1068 |
+
"colab": {
|
| 1069 |
+
"base_uri": "https://localhost:8080/",
|
| 1070 |
+
"height": 1000
|
| 1071 |
+
},
|
| 1072 |
+
"editable": true,
|
| 1073 |
+
"executionInfo": {
|
| 1074 |
+
"elapsed": 2890277,
|
| 1075 |
+
"status": "error",
|
| 1076 |
+
"timestamp": 1726326443418,
|
| 1077 |
+
"user": {
|
| 1078 |
+
"displayName": "Darshil Parekh",
|
| 1079 |
+
"userId": "08764169128860999444"
|
| 1080 |
+
},
|
| 1081 |
+
"user_tz": -330
|
| 1082 |
+
},
|
| 1083 |
+
"id": "0d815894-238b-473d-8c29-cd2ebd948c59",
|
| 1084 |
+
"outputId": "868608c0-e04e-4de7-ff06-f18455e12b59",
|
| 1085 |
+
"tags": []
|
| 1086 |
+
},
|
| 1087 |
+
"outputs": [
|
| 1088 |
+
{
|
| 1089 |
+
"name": "stdout",
|
| 1090 |
+
"output_type": "stream",
|
| 1091 |
+
"text": [
|
| 1092 |
+
"Epoch: 0, Iter: 222, d_loss= 0.701056, g_loss= 3538.156250, D(X)= 0.579759, D(G(X))= 0.064360\n",
|
| 1093 |
+
"epoch 0 complete\n",
|
| 1094 |
+
"Epoch: 1, Iter: 222, d_loss= 0.679630, g_loss= 3676.295898, D(X)= 0.604906, D(G(X))= 0.025995\n",
|
| 1095 |
+
"Epoch: 2, Iter: 222, d_loss= 0.839086, g_loss= 3861.553955, D(X)= 0.576425, D(G(X))= 0.096675\n",
|
| 1096 |
+
"Epoch: 3, Iter: 222, d_loss= 0.979223, g_loss= 3977.661377, D(X)= 0.955430, D(G(X))= 0.406845\n",
|
| 1097 |
+
"Epoch: 4, Iter: 222, d_loss= 0.474595, g_loss= 3004.067383, D(X)= 0.799094, D(G(X))= 0.075874\n",
|
| 1098 |
+
"Epoch: 5, Iter: 222, d_loss= 0.804994, g_loss= 3905.889160, D(X)= 0.574385, D(G(X))= 0.079843\n",
|
| 1099 |
+
"Epoch: 6, Iter: 222, d_loss= 0.828463, g_loss= 3658.190430, D(X)= 0.960235, D(G(X))= 0.308461\n",
|
| 1100 |
+
"Epoch: 7, Iter: 222, d_loss= 0.587771, g_loss= 2689.952148, D(X)= 0.787618, D(G(X))= 0.161049\n",
|
| 1101 |
+
"Epoch: 8, Iter: 222, d_loss= 0.427244, g_loss= 3475.554688, D(X)= 0.849420, D(G(X))= 0.053325\n",
|
| 1102 |
+
"Epoch: 9, Iter: 222, d_loss= 0.975016, g_loss= 2920.777100, D(X)= 0.410495, D(G(X))= 0.030045\n",
|
| 1103 |
+
"Epoch: 10, Iter: 222, d_loss= 0.646374, g_loss= 2579.601074, D(X)= 0.710434, D(G(X))= 0.144055\n",
|
| 1104 |
+
"epoch 10 complete\n",
|
| 1105 |
+
"Epoch: 11, Iter: 222, d_loss= 0.468959, g_loss= 3792.114258, D(X)= 0.747990, D(G(X))= 0.037221\n",
|
| 1106 |
+
"Epoch: 12, Iter: 222, d_loss= 0.467665, g_loss= 3418.384277, D(X)= 0.910842, D(G(X))= 0.091576\n",
|
| 1107 |
+
"Epoch: 13, Iter: 222, d_loss= 0.612966, g_loss= 3741.182861, D(X)= 0.638289, D(G(X))= 0.025249\n",
|
| 1108 |
+
"Epoch: 14, Iter: 222, d_loss= 1.720957, g_loss= 3652.041016, D(X)= 0.281481, D(G(X))= 0.001660\n",
|
| 1109 |
+
"Epoch: 15, Iter: 222, d_loss= 0.531606, g_loss= 4409.900391, D(X)= 0.962485, D(G(X))= 0.101459\n",
|
| 1110 |
+
"Epoch: 16, Iter: 222, d_loss= 0.491479, g_loss= 4025.999756, D(X)= 0.745472, D(G(X))= 0.061522\n",
|
| 1111 |
+
"Epoch: 17, Iter: 222, d_loss= 0.766251, g_loss= 3645.494873, D(X)= 0.532194, D(G(X))= 0.012785\n",
|
| 1112 |
+
"Epoch: 18, Iter: 222, d_loss= 0.695738, g_loss= 3654.352783, D(X)= 0.944553, D(G(X))= 0.253696\n",
|
| 1113 |
+
"Epoch: 19, Iter: 222, d_loss= 0.867143, g_loss= 2578.100342, D(X)= 0.975738, D(G(X))= 0.324007\n",
|
| 1114 |
+
"Epoch: 20, Iter: 222, d_loss= 0.509606, g_loss= 4646.008301, D(X)= 0.733054, D(G(X))= 0.066041\n",
|
| 1115 |
+
"epoch 20 complete\n",
|
| 1116 |
+
"Epoch: 21, Iter: 222, d_loss= 0.466746, g_loss= 3191.031250, D(X)= 0.721822, D(G(X))= 0.008881\n",
|
| 1117 |
+
"Epoch: 22, Iter: 222, d_loss= 0.461730, g_loss= 4163.461914, D(X)= 0.811961, D(G(X))= 0.034651\n",
|
| 1118 |
+
"Epoch: 23, Iter: 222, d_loss= 0.385916, g_loss= 3554.304443, D(X)= 0.799397, D(G(X))= 0.006166\n",
|
| 1119 |
+
"Epoch: 24, Iter: 222, d_loss= 0.774094, g_loss= 4240.382812, D(X)= 0.922192, D(G(X))= 0.276380\n",
|
| 1120 |
+
"Epoch: 25, Iter: 222, d_loss= 0.373230, g_loss= 3827.503174, D(X)= 0.847593, D(G(X))= 0.004507\n",
|
| 1121 |
+
"Epoch: 26, Iter: 222, d_loss= 0.985485, g_loss= 4279.049805, D(X)= 0.439561, D(G(X))= 0.013861\n",
|
| 1122 |
+
"Epoch: 27, Iter: 222, d_loss= 0.626296, g_loss= 3783.946533, D(X)= 0.593357, D(G(X))= 0.016181\n",
|
| 1123 |
+
"Epoch: 28, Iter: 222, d_loss= 0.531090, g_loss= 3176.968018, D(X)= 0.680971, D(G(X))= 0.004957\n",
|
| 1124 |
+
"Epoch: 29, Iter: 222, d_loss= 0.353129, g_loss= 2946.677979, D(X)= 0.880036, D(G(X))= 0.011091\n",
|
| 1125 |
+
"Epoch: 30, Iter: 222, d_loss= 0.334022, g_loss= 3002.093018, D(X)= 0.919297, D(G(X))= 0.001554\n",
|
| 1126 |
+
"epoch 30 complete\n",
|
| 1127 |
+
"Epoch: 31, Iter: 222, d_loss= 0.413073, g_loss= 4168.980469, D(X)= 0.798591, D(G(X))= 0.026550\n",
|
| 1128 |
+
"Epoch: 32, Iter: 222, d_loss= 0.430312, g_loss= 3704.318359, D(X)= 0.877181, D(G(X))= 0.043618\n",
|
| 1129 |
+
"Epoch: 33, Iter: 222, d_loss= 0.540595, g_loss= 3159.942383, D(X)= 0.654681, D(G(X))= 0.013845\n",
|
| 1130 |
+
"Epoch: 34, Iter: 222, d_loss= 0.613879, g_loss= 3588.398438, D(X)= 0.586779, D(G(X))= 0.002503\n",
|
| 1131 |
+
"Epoch: 35, Iter: 222, d_loss= 0.419698, g_loss= 4234.351562, D(X)= 0.834483, D(G(X))= 0.048954\n",
|
| 1132 |
+
"Epoch: 36, Iter: 222, d_loss= 0.397612, g_loss= 2739.781982, D(X)= 0.831707, D(G(X))= 0.021257\n",
|
| 1133 |
+
"Epoch: 37, Iter: 222, d_loss= 0.965550, g_loss= 4611.081055, D(X)= 0.460998, D(G(X))= 0.003786\n",
|
| 1134 |
+
"Epoch: 38, Iter: 222, d_loss= 0.394915, g_loss= 3028.919922, D(X)= 0.805663, D(G(X))= 0.004134\n",
|
| 1135 |
+
"Epoch: 39, Iter: 222, d_loss= 0.448785, g_loss= 2665.940674, D(X)= 0.757539, D(G(X))= 0.001606\n",
|
| 1136 |
+
"Epoch: 40, Iter: 222, d_loss= 0.546468, g_loss= 3599.658691, D(X)= 0.638130, D(G(X))= 0.007002\n",
|
| 1137 |
+
"epoch 40 complete\n",
|
| 1138 |
+
"Epoch: 41, Iter: 222, d_loss= 0.536460, g_loss= 3580.464111, D(X)= 0.779603, D(G(X))= 0.043807\n",
|
| 1139 |
+
"Epoch: 42, Iter: 222, d_loss= 0.360860, g_loss= 4108.963379, D(X)= 0.906363, D(G(X))= 0.006131\n",
|
| 1140 |
+
"Epoch: 43, Iter: 222, d_loss= 0.360820, g_loss= 3276.907959, D(X)= 0.858369, D(G(X))= 0.003254\n",
|
| 1141 |
+
"Epoch: 44, Iter: 222, d_loss= 1.295341, g_loss= 4108.103516, D(X)= 0.266473, D(G(X))= 0.000534\n",
|
| 1142 |
+
"Epoch: 45, Iter: 222, d_loss= 0.387217, g_loss= 4203.428711, D(X)= 0.788921, D(G(X))= 0.004543\n",
|
| 1143 |
+
"Epoch: 46, Iter: 222, d_loss= 0.422805, g_loss= 3600.573242, D(X)= 0.745221, D(G(X))= 0.005458\n",
|
| 1144 |
+
"Epoch: 47, Iter: 222, d_loss= 0.383310, g_loss= 3377.399170, D(X)= 0.967776, D(G(X))= 0.000569\n",
|
| 1145 |
+
"Epoch: 48, Iter: 222, d_loss= 0.364424, g_loss= 2696.472900, D(X)= 0.817939, D(G(X))= 0.000297\n",
|
| 1146 |
+
"Epoch: 49, Iter: 222, d_loss= 0.360125, g_loss= 3540.071045, D(X)= 0.881340, D(G(X))= 0.003572\n",
|
| 1147 |
+
"Epoch: 50, Iter: 222, d_loss= 0.339098, g_loss= 3244.859131, D(X)= 0.919540, D(G(X))= 0.004547\n",
|
| 1148 |
+
"epoch 50 complete\n",
|
| 1149 |
+
"Epoch: 51, Iter: 222, d_loss= 0.363068, g_loss= 3698.898193, D(X)= 0.937415, D(G(X))= 0.007880\n",
|
| 1150 |
+
"Epoch: 52, Iter: 222, d_loss= 0.375254, g_loss= 3719.618652, D(X)= 0.957973, D(G(X))= 0.005729\n",
|
| 1151 |
+
"Epoch: 53, Iter: 222, d_loss= 0.438091, g_loss= 4305.036621, D(X)= 0.715028, D(G(X))= 0.000790\n",
|
| 1152 |
+
"Epoch: 54, Iter: 222, d_loss= 0.354891, g_loss= 3700.000732, D(X)= 0.937225, D(G(X))= 0.000967\n",
|
| 1153 |
+
"Epoch: 55, Iter: 222, d_loss= 0.364136, g_loss= 3272.887451, D(X)= 0.958542, D(G(X))= 0.001034\n",
|
| 1154 |
+
"Epoch: 56, Iter: 222, d_loss= 0.666980, g_loss= 3945.071045, D(X)= 0.924580, D(G(X))= 0.263233\n",
|
| 1155 |
+
"Epoch: 57, Iter: 222, d_loss= 0.356129, g_loss= 4415.549316, D(X)= 0.903652, D(G(X))= 0.003884\n",
|
| 1156 |
+
"Epoch: 58, Iter: 222, d_loss= 0.355318, g_loss= 3713.228271, D(X)= 0.828642, D(G(X))= 0.003223\n",
|
| 1157 |
+
"Epoch: 59, Iter: 222, d_loss= 0.333158, g_loss= 3780.786621, D(X)= 0.922390, D(G(X))= 0.000811\n",
|
| 1158 |
+
"Epoch: 60, Iter: 222, d_loss= 0.331645, g_loss= 3918.236816, D(X)= 0.900606, D(G(X))= 0.001038\n",
|
| 1159 |
+
"epoch 60 complete\n",
|
| 1160 |
+
"Epoch: 61, Iter: 222, d_loss= 0.342588, g_loss= 3963.584961, D(X)= 0.866488, D(G(X))= 0.000639\n",
|
| 1161 |
+
"Epoch: 62, Iter: 222, d_loss= 0.335670, g_loss= 3823.017090, D(X)= 0.909363, D(G(X))= 0.000514\n",
|
| 1162 |
+
"Epoch: 63, Iter: 222, d_loss= 0.332328, g_loss= 3192.711914, D(X)= 0.871337, D(G(X))= 0.000163\n",
|
| 1163 |
+
"Epoch: 64, Iter: 222, d_loss= 0.352667, g_loss= 4211.349121, D(X)= 0.830729, D(G(X))= 0.000308\n",
|
| 1164 |
+
"Epoch: 65, Iter: 222, d_loss= 0.375499, g_loss= 3618.913574, D(X)= 0.785201, D(G(X))= 0.001551\n",
|
| 1165 |
+
"Epoch: 66, Iter: 222, d_loss= 0.422351, g_loss= 2822.109131, D(X)= 0.796270, D(G(X))= 0.030600\n",
|
| 1166 |
+
"Epoch: 67, Iter: 222, d_loss= 0.532200, g_loss= 3551.816895, D(X)= 0.684085, D(G(X))= 0.037150\n",
|
| 1167 |
+
"Epoch: 68, Iter: 222, d_loss= 0.468477, g_loss= 3234.406982, D(X)= 0.808089, D(G(X))= 0.020581\n",
|
| 1168 |
+
"Epoch: 69, Iter: 222, d_loss= 0.357905, g_loss= 3858.374023, D(X)= 0.955218, D(G(X))= 0.000775\n",
|
| 1169 |
+
"Epoch: 70, Iter: 222, d_loss= 0.377006, g_loss= 3424.719727, D(X)= 0.787619, D(G(X))= 0.000361\n",
|
| 1170 |
+
"epoch 70 complete\n",
|
| 1171 |
+
"Epoch: 71, Iter: 222, d_loss= 0.332974, g_loss= 2816.813477, D(X)= 0.919339, D(G(X))= 0.000862\n",
|
| 1172 |
+
"Epoch: 72, Iter: 222, d_loss= 0.337460, g_loss= 2565.474121, D(X)= 0.928413, D(G(X))= 0.000817\n",
|
| 1173 |
+
"Epoch: 73, Iter: 222, d_loss= 0.328905, g_loss= 4435.673340, D(X)= 0.912533, D(G(X))= 0.000075\n",
|
| 1174 |
+
"Epoch: 74, Iter: 222, d_loss= 0.353846, g_loss= 4069.165283, D(X)= 0.819689, D(G(X))= 0.000205\n",
|
| 1175 |
+
"Epoch: 75, Iter: 222, d_loss= 0.456906, g_loss= 4124.631836, D(X)= 0.978655, D(G(X))= 0.027646\n",
|
| 1176 |
+
"Epoch: 76, Iter: 222, d_loss= 0.351983, g_loss= 3220.302490, D(X)= 0.854669, D(G(X))= 0.000532\n",
|
| 1177 |
+
"Epoch: 77, Iter: 222, d_loss= 0.331386, g_loss= 3644.930908, D(X)= 0.883734, D(G(X))= 0.000370\n",
|
| 1178 |
+
"Epoch: 78, Iter: 222, d_loss= 0.335561, g_loss= 2944.252441, D(X)= 0.914969, D(G(X))= 0.000500\n",
|
| 1179 |
+
"Epoch: 79, Iter: 222, d_loss= 0.329391, g_loss= 3497.903320, D(X)= 0.882267, D(G(X))= 0.000115\n",
|
| 1180 |
+
"Epoch: 80, Iter: 222, d_loss= 0.342888, g_loss= 3254.711914, D(X)= 0.845431, D(G(X))= 0.000475\n",
|
| 1181 |
+
"epoch 80 complete\n"
|
| 1182 |
+
]
|
| 1183 |
+
},
|
| 1184 |
+
{
|
| 1185 |
+
"ename": "KeyboardInterrupt",
|
| 1186 |
+
"evalue": "",
|
| 1187 |
+
"output_type": "error",
|
| 1188 |
+
"traceback": [
|
| 1189 |
+
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
|
| 1190 |
+
"\u001b[0;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)",
|
| 1191 |
+
"\u001b[0;32m<ipython-input-43-d0d8e74db11c>\u001b[0m in \u001b[0;36m<cell line: 31>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 29\u001b[0m )\n\u001b[1;32m 30\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 31\u001b[0;31m \u001b[0mtrainer\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtrain\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcls\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 32\u001b[0m \u001b[0;31m#trainer.predict()\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
|
| 1192 |
+
"\u001b[0;32m<ipython-input-41-1bfb2b9620b8>\u001b[0m in \u001b[0;36mtrain\u001b[0;34m(self, cls)\u001b[0m\n\u001b[1;32m 48\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 49\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtype\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;34m'gan'\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 50\u001b[0;31m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_train_gan\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mcls\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 51\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 52\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
|
| 1193 |
+
"\u001b[0;32m<ipython-input-41-1bfb2b9620b8>\u001b[0m in \u001b[0;36m_train_gan\u001b[0;34m(self, cls)\u001b[0m\n\u001b[1;32m 106\u001b[0m \u001b[0md_loss\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0md_loss\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mwrong_loss\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 107\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 108\u001b[0;31m \u001b[0md_loss\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mbackward\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 109\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0moptimD\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mstep\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 110\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
|
| 1194 |
+
"\u001b[0;32m/usr/local/lib/python3.10/dist-packages/torch/_tensor.py\u001b[0m in \u001b[0;36mbackward\u001b[0;34m(self, gradient, retain_graph, create_graph, inputs)\u001b[0m\n\u001b[1;32m 523\u001b[0m \u001b[0minputs\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0minputs\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 524\u001b[0m )\n\u001b[0;32m--> 525\u001b[0;31m torch.autograd.backward(\n\u001b[0m\u001b[1;32m 526\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mgradient\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mretain_graph\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcreate_graph\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0minputs\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0minputs\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 527\u001b[0m )\n",
|
| 1195 |
+
"\u001b[0;32m/usr/local/lib/python3.10/dist-packages/torch/autograd/__init__.py\u001b[0m in \u001b[0;36mbackward\u001b[0;34m(tensors, grad_tensors, retain_graph, create_graph, grad_variables, inputs)\u001b[0m\n\u001b[1;32m 265\u001b[0m \u001b[0;31m# some Python versions print out the first line of a multi-line function\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 266\u001b[0m \u001b[0;31m# calls in the traceback and some print out the last line\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 267\u001b[0;31m _engine_run_backward(\n\u001b[0m\u001b[1;32m 268\u001b[0m \u001b[0mtensors\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 269\u001b[0m \u001b[0mgrad_tensors_\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
|
| 1196 |
+
"\u001b[0;32m/usr/local/lib/python3.10/dist-packages/torch/autograd/graph.py\u001b[0m in \u001b[0;36m_engine_run_backward\u001b[0;34m(t_outputs, *args, **kwargs)\u001b[0m\n\u001b[1;32m 742\u001b[0m \u001b[0munregister_hooks\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0m_register_logging_hooks_on_whole_graph\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mt_outputs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 743\u001b[0m \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 744\u001b[0;31m return Variable._execution_engine.run_backward( # Calls into the C++ engine to run the backward pass\n\u001b[0m\u001b[1;32m 745\u001b[0m \u001b[0mt_outputs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 746\u001b[0m ) # Calls into the C++ engine to run the backward pass\n",
|
| 1197 |
+
"\u001b[0;31mKeyboardInterrupt\u001b[0m: "
|
| 1198 |
+
]
|
| 1199 |
+
}
|
| 1200 |
+
],
|
| 1201 |
+
"source": [
|
| 1202 |
+
"import argparse\n",
|
| 1203 |
+
"from PIL import Image #This may not be used\n",
|
| 1204 |
+
"import os ##This may not be used\n",
|
| 1205 |
+
"import easydict\n",
|
| 1206 |
+
"\n",
|
| 1207 |
+
"args = easydict.EasyDict({'type': 'gan',\n",
|
| 1208 |
+
" 'lr': 0.0002,\n",
|
| 1209 |
+
" 'l1_coef': 50,\n",
|
| 1210 |
+
" 'l2_coef': 100,\n",
|
| 1211 |
+
" 'cls': True,\n",
|
| 1212 |
+
" 'save_path':'Result',\n",
|
| 1213 |
+
" 'inference': True,\n",
|
| 1214 |
+
" 'dataset': test_data,\n",
|
| 1215 |
+
" 'split': 2,\n",
|
| 1216 |
+
" 'batch_size':64,\n",
|
| 1217 |
+
" 'num_workers':1,\n",
|
| 1218 |
+
" 'epochs':600})\n",
|
| 1219 |
+
"\n",
|
| 1220 |
+
"trainer = Trainer(type=args.type,\n",
|
| 1221 |
+
" dataset=args.dataset,\n",
|
| 1222 |
+
" split=args.split,\n",
|
| 1223 |
+
" lr=args.lr,\n",
|
| 1224 |
+
" save_path=args.save_path,\n",
|
| 1225 |
+
" l1_coef=args.l1_coef,\n",
|
| 1226 |
+
" l2_coef=args.l2_coef,\n",
|
| 1227 |
+
" batch_size=args.batch_size,\n",
|
| 1228 |
+
" num_workers=args.num_workers,\n",
|
| 1229 |
+
" epochs=args.epochs\n",
|
| 1230 |
+
" )\n",
|
| 1231 |
+
"\n",
|
| 1232 |
+
"trainer.train(args.cls)\n",
|
| 1233 |
+
"#trainer.predict()"
|
| 1234 |
+
]
|
| 1235 |
+
},
|
| 1236 |
+
{
|
| 1237 |
+
"cell_type": "code",
|
| 1238 |
+
"execution_count": null,
|
| 1239 |
+
"id": "242ee9a9-47df-4c50-b7d9-ae181543f75f",
|
| 1240 |
+
"metadata": {
|
| 1241 |
+
"colab": {
|
| 1242 |
+
"base_uri": "https://localhost:8080/",
|
| 1243 |
+
"height": 211
|
| 1244 |
+
},
|
| 1245 |
+
"executionInfo": {
|
| 1246 |
+
"elapsed": 759,
|
| 1247 |
+
"status": "error",
|
| 1248 |
+
"timestamp": 1726326479692,
|
| 1249 |
+
"user": {
|
| 1250 |
+
"displayName": "Darshil Parekh",
|
| 1251 |
+
"userId": "08764169128860999444"
|
| 1252 |
+
},
|
| 1253 |
+
"user_tz": -330
|
| 1254 |
+
},
|
| 1255 |
+
"id": "242ee9a9-47df-4c50-b7d9-ae181543f75f",
|
| 1256 |
+
"outputId": "1c0df61d-a9a3-461f-9ea1-a0eb63c92be1"
|
| 1257 |
+
},
|
| 1258 |
+
"outputs": [
|
| 1259 |
+
{
|
| 1260 |
+
"ename": "AttributeError",
|
| 1261 |
+
"evalue": "'Logger' object has no attribute 'd_loss_list'",
|
| 1262 |
+
"output_type": "error",
|
| 1263 |
+
"traceback": [
|
| 1264 |
+
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
|
| 1265 |
+
"\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)",
|
| 1266 |
+
"\u001b[0;32m<ipython-input-45-b7e01a63074d>\u001b[0m in \u001b[0;36m<cell line: 9>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 7\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 8\u001b[0m \u001b[0;31m# Extract the data from the logger\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 9\u001b[0;31m \u001b[0md_loss_list\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mtrainer\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mlogger\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0md_loss_list\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 10\u001b[0m \u001b[0mg_loss_list\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mtrainer\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mlogger\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mg_loss_list\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 11\u001b[0m \u001b[0md_x_list\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mtrainer\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mlogger\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0md_x_list\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
|
| 1267 |
+
"\u001b[0;31mAttributeError\u001b[0m: 'Logger' object has no attribute 'd_loss_list'"
|
| 1268 |
+
]
|
| 1269 |
+
}
|
| 1270 |
+
],
|
| 1271 |
+
"source": [
|
| 1272 |
+
"# prompt: draw a graph for the d_loss, g_loss, d(X) and d(g(x))\n",
|
| 1273 |
+
"\n",
|
| 1274 |
+
"import matplotlib.pyplot as plt\n",
|
| 1275 |
+
"\n",
|
| 1276 |
+
"# Assuming you have lists or arrays named d_loss_list, g_loss_list, d_x_list, d_gx_list\n",
|
| 1277 |
+
"# containing the values for each metric over the training epochs.\n",
|
| 1278 |
+
"\n",
|
| 1279 |
+
"# Extract the data from the logger\n",
|
| 1280 |
+
"d_loss_list = trainer.logger.d_loss_list\n",
|
| 1281 |
+
"g_loss_list = trainer.logger.g_loss_list\n",
|
| 1282 |
+
"d_x_list = trainer.logger.d_x_list\n",
|
| 1283 |
+
"d_gx_list = trainer.logger.d_gx_list\n",
|
| 1284 |
+
"\n",
|
| 1285 |
+
"# Create the figure and axes\n",
|
| 1286 |
+
"fig, axs = plt.subplots(2, 2, figsize=(12, 8))\n",
|
| 1287 |
+
"\n",
|
| 1288 |
+
"# Plot D Loss\n",
|
| 1289 |
+
"axs[0, 0].plot(d_loss_list)\n",
|
| 1290 |
+
"axs[0, 0].set_title(\"Discriminator Loss\")\n",
|
| 1291 |
+
"axs[0, 0].set_xlabel(\"Epoch\")\n",
|
| 1292 |
+
"axs[0, 0].set_ylabel(\"Loss\")\n",
|
| 1293 |
+
"\n",
|
| 1294 |
+
"# Plot G Loss\n",
|
| 1295 |
+
"axs[0, 1].plot(g_loss_list)\n",
|
| 1296 |
+
"axs[0, 1].set_title(\"Generator Loss\")\n",
|
| 1297 |
+
"axs[0, 1].set_xlabel(\"Epoch\")\n",
|
| 1298 |
+
"axs[0, 1].set_ylabel(\"Loss\")\n",
|
| 1299 |
+
"\n",
|
| 1300 |
+
"# Plot D(X)\n",
|
| 1301 |
+
"axs[1, 0].plot(d_x_list)\n",
|
| 1302 |
+
"axs[1, 0].set_title(\"Discriminator Output for Real Images (D(X))\")\n",
|
| 1303 |
+
"axs[1, 0].set_xlabel(\"Epoch\")\n",
|
| 1304 |
+
"axs[1, 0].set_ylabel(\"Score\")\n",
|
| 1305 |
+
"\n",
|
| 1306 |
+
"# Plot D(G(X))\n",
|
| 1307 |
+
"axs[1, 1].plot(d_gx_list)\n",
|
| 1308 |
+
"axs[1, 1].set_title(\"Discriminator Output for Fake Images (D(G(X)))\")\n",
|
| 1309 |
+
"axs[1, 1].set_xlabel(\"Epoch\")\n",
|
| 1310 |
+
"axs[1, 1].set_ylabel(\"Score\")\n",
|
| 1311 |
+
"\n",
|
| 1312 |
+
"# Adjust layout and display the plot\n",
|
| 1313 |
+
"plt.tight_layout()\n",
|
| 1314 |
+
"plt.show()\n"
|
| 1315 |
+
]
|
| 1316 |
+
},
|
| 1317 |
+
{
|
| 1318 |
+
"cell_type": "code",
|
| 1319 |
+
"execution_count": null,
|
| 1320 |
+
"id": "1e2bf862-35fe-433e-b1e2-012cbd5ee47d",
|
| 1321 |
+
"metadata": {
|
| 1322 |
+
"editable": true,
|
| 1323 |
+
"id": "1e2bf862-35fe-433e-b1e2-012cbd5ee47d",
|
| 1324 |
+
"tags": []
|
| 1325 |
+
},
|
| 1326 |
+
"outputs": [],
|
| 1327 |
+
"source": [
|
| 1328 |
+
"!cp -r checkpoints /content/drive/MyDrive"
|
| 1329 |
+
]
|
| 1330 |
+
},
|
| 1331 |
+
{
|
| 1332 |
+
"cell_type": "code",
|
| 1333 |
+
"execution_count": null,
|
| 1334 |
+
"id": "JDN71GvsFTyn",
|
| 1335 |
+
"metadata": {
|
| 1336 |
+
"id": "JDN71GvsFTyn"
|
| 1337 |
+
},
|
| 1338 |
+
"outputs": [],
|
| 1339 |
+
"source": []
|
| 1340 |
+
}
|
| 1341 |
+
],
|
| 1342 |
+
"metadata": {
|
| 1343 |
+
"colab": {
|
| 1344 |
+
"provenance": []
|
| 1345 |
+
},
|
| 1346 |
+
"kernelspec": {
|
| 1347 |
+
"display_name": "Python 3 (ipykernel)",
|
| 1348 |
+
"language": "python",
|
| 1349 |
+
"name": "python3"
|
| 1350 |
+
},
|
| 1351 |
+
"language_info": {
|
| 1352 |
+
"codemirror_mode": {
|
| 1353 |
+
"name": "ipython",
|
| 1354 |
+
"version": 3
|
| 1355 |
+
},
|
| 1356 |
+
"file_extension": ".py",
|
| 1357 |
+
"mimetype": "text/x-python",
|
| 1358 |
+
"name": "python",
|
| 1359 |
+
"nbconvert_exporter": "python",
|
| 1360 |
+
"pygments_lexer": "ipython3",
|
| 1361 |
+
"version": "3.11.9"
|
| 1362 |
+
}
|
| 1363 |
+
},
|
| 1364 |
+
"nbformat": 4,
|
| 1365 |
+
"nbformat_minor": 5
|
| 1366 |
+
}
|
03_GAN_1.ipynb
ADDED
|
@@ -0,0 +1,921 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cells": [
|
| 3 |
+
{
|
| 4 |
+
"cell_type": "code",
|
| 5 |
+
"execution_count": null,
|
| 6 |
+
"id": "pVAyqh-hccNc",
|
| 7 |
+
"metadata": {
|
| 8 |
+
"id": "pVAyqh-hccNc"
|
| 9 |
+
},
|
| 10 |
+
"outputs": [],
|
| 11 |
+
"source": [
|
| 12 |
+
"#!pip install datasets"
|
| 13 |
+
]
|
| 14 |
+
},
|
| 15 |
+
{
|
| 16 |
+
"cell_type": "code",
|
| 17 |
+
"execution_count": null,
|
| 18 |
+
"id": "jxrRXwMPRZkB",
|
| 19 |
+
"metadata": {
|
| 20 |
+
"id": "jxrRXwMPRZkB"
|
| 21 |
+
},
|
| 22 |
+
"outputs": [],
|
| 23 |
+
"source": [
|
| 24 |
+
"#!pip install google-colab"
|
| 25 |
+
]
|
| 26 |
+
},
|
| 27 |
+
{
|
| 28 |
+
"cell_type": "code",
|
| 29 |
+
"execution_count": null,
|
| 30 |
+
"id": "tVGMu6bhcdoz",
|
| 31 |
+
"metadata": {
|
| 32 |
+
"id": "tVGMu6bhcdoz"
|
| 33 |
+
},
|
| 34 |
+
"outputs": [],
|
| 35 |
+
"source": [
|
| 36 |
+
"#from google.colab import drive\n",
|
| 37 |
+
"#drive.mount('/content/drive')"
|
| 38 |
+
]
|
| 39 |
+
},
|
| 40 |
+
{
|
| 41 |
+
"cell_type": "code",
|
| 42 |
+
"execution_count": 1,
|
| 43 |
+
"id": "ddb93c12-776c-43f3-87f2-566a61510042",
|
| 44 |
+
"metadata": {
|
| 45 |
+
"id": "ddb93c12-776c-43f3-87f2-566a61510042"
|
| 46 |
+
},
|
| 47 |
+
"outputs": [],
|
| 48 |
+
"source": [
|
| 49 |
+
"from datasets import load_from_disk\n",
|
| 50 |
+
"\n",
|
| 51 |
+
"import os\n",
|
| 52 |
+
"import torch\n",
|
| 53 |
+
"import torch.nn as nn\n",
|
| 54 |
+
"import torch.optim as optim\n",
|
| 55 |
+
"from torch.autograd import Variable\n",
|
| 56 |
+
"from torch.utils.data import Dataset, DataLoader\n",
|
| 57 |
+
"\n",
|
| 58 |
+
"import torchvision\n",
|
| 59 |
+
"from torchvision import datasets, transforms\n",
|
| 60 |
+
"\n",
|
| 61 |
+
"import matplotlib.pyplot as plt\n",
|
| 62 |
+
"import numpy as np\n",
|
| 63 |
+
"\n",
|
| 64 |
+
"\n",
|
| 65 |
+
"import utils\n",
|
| 66 |
+
"\n",
|
| 67 |
+
"from utils import Utils, Logger"
|
| 68 |
+
]
|
| 69 |
+
},
|
| 70 |
+
{
|
| 71 |
+
"cell_type": "code",
|
| 72 |
+
"execution_count": 2,
|
| 73 |
+
"id": "a2b42c1f-13d2-423b-a8c0-c1631d93b3fe",
|
| 74 |
+
"metadata": {
|
| 75 |
+
"colab": {
|
| 76 |
+
"base_uri": "https://localhost:8080/"
|
| 77 |
+
},
|
| 78 |
+
"id": "a2b42c1f-13d2-423b-a8c0-c1631d93b3fe",
|
| 79 |
+
"outputId": "48c61bf7-c6f0-4999-be41-9343ece1c42d"
|
| 80 |
+
},
|
| 81 |
+
"outputs": [
|
| 82 |
+
{
|
| 83 |
+
"data": {
|
| 84 |
+
"text/plain": [
|
| 85 |
+
"<torch._C.Generator at 0x269e8d33c10>"
|
| 86 |
+
]
|
| 87 |
+
},
|
| 88 |
+
"execution_count": 2,
|
| 89 |
+
"metadata": {},
|
| 90 |
+
"output_type": "execute_result"
|
| 91 |
+
}
|
| 92 |
+
],
|
| 93 |
+
"source": [
|
| 94 |
+
"random_seed = 42\n",
|
| 95 |
+
"torch.manual_seed(random_seed)"
|
| 96 |
+
]
|
| 97 |
+
},
|
| 98 |
+
{
|
| 99 |
+
"cell_type": "code",
|
| 100 |
+
"execution_count": 3,
|
| 101 |
+
"id": "05efff77-160f-41e3-939a-0755f6986de0",
|
| 102 |
+
"metadata": {
|
| 103 |
+
"colab": {
|
| 104 |
+
"base_uri": "https://localhost:8080/"
|
| 105 |
+
},
|
| 106 |
+
"id": "05efff77-160f-41e3-939a-0755f6986de0",
|
| 107 |
+
"outputId": "8a37fe4f-0e64-454e-fa11-7fda2854ab4a"
|
| 108 |
+
},
|
| 109 |
+
"outputs": [
|
| 110 |
+
{
|
| 111 |
+
"data": {
|
| 112 |
+
"text/plain": [
|
| 113 |
+
"(1, 11)"
|
| 114 |
+
]
|
| 115 |
+
},
|
| 116 |
+
"execution_count": 3,
|
| 117 |
+
"metadata": {},
|
| 118 |
+
"output_type": "execute_result"
|
| 119 |
+
}
|
| 120 |
+
],
|
| 121 |
+
"source": [
|
| 122 |
+
"AVAIL_GPUS = min(1, torch.cuda.device_count())\n",
|
| 123 |
+
"NUM_WORKERS=int(os.cpu_count() / 2)\n",
|
| 124 |
+
"AVAIL_GPUS,NUM_WORKERS"
|
| 125 |
+
]
|
| 126 |
+
},
|
| 127 |
+
{
|
| 128 |
+
"cell_type": "code",
|
| 129 |
+
"execution_count": 4,
|
| 130 |
+
"id": "Dt3BJgH_RkaV",
|
| 131 |
+
"metadata": {
|
| 132 |
+
"id": "Dt3BJgH_RkaV"
|
| 133 |
+
},
|
| 134 |
+
"outputs": [],
|
| 135 |
+
"source": [
|
| 136 |
+
"colab = False\n",
|
| 137 |
+
"colabUrl = \"/content/drive/MyDrive/PreProcessedDataWithEmb\"\n",
|
| 138 |
+
"\n",
|
| 139 |
+
"localUrl = \"C:\\\\Users\\\\daparekh\\\\OneDrive - OpenText\\\\Desktop\\\\Final Thesis\\\\Code\\\\PreProcessedDataWithEmb\"\n",
|
| 140 |
+
"\n",
|
| 141 |
+
"if colab:\n",
|
| 142 |
+
" data_path = colabUrl\n",
|
| 143 |
+
"else:\n",
|
| 144 |
+
" data_path = localUrl"
|
| 145 |
+
]
|
| 146 |
+
},
|
| 147 |
+
{
|
| 148 |
+
"cell_type": "code",
|
| 149 |
+
"execution_count": 5,
|
| 150 |
+
"id": "5fecca51-0f62-4060-9df1-ee94c54ebcaf",
|
| 151 |
+
"metadata": {
|
| 152 |
+
"colab": {
|
| 153 |
+
"base_uri": "https://localhost:8080/"
|
| 154 |
+
},
|
| 155 |
+
"id": "5fecca51-0f62-4060-9df1-ee94c54ebcaf",
|
| 156 |
+
"outputId": "47be9553-c7cb-4c31-e231-9f761e338a44"
|
| 157 |
+
},
|
| 158 |
+
"outputs": [
|
| 159 |
+
{
|
| 160 |
+
"data": {
|
| 161 |
+
"text/plain": [
|
| 162 |
+
"DatasetDict({\n",
|
| 163 |
+
" train: Dataset({\n",
|
| 164 |
+
" features: ['image', 'company', 'content', 'description', 'fulltext', 'fulltext_vector'],\n",
|
| 165 |
+
" num_rows: 33034\n",
|
| 166 |
+
" })\n",
|
| 167 |
+
" test: Dataset({\n",
|
| 168 |
+
" features: ['image', 'company', 'content', 'description', 'fulltext', 'fulltext_vector'],\n",
|
| 169 |
+
" num_rows: 14158\n",
|
| 170 |
+
" })\n",
|
| 171 |
+
"})"
|
| 172 |
+
]
|
| 173 |
+
},
|
| 174 |
+
"execution_count": 5,
|
| 175 |
+
"metadata": {},
|
| 176 |
+
"output_type": "execute_result"
|
| 177 |
+
}
|
| 178 |
+
],
|
| 179 |
+
"source": [
|
| 180 |
+
"reloaded_dataset = load_from_disk(data_path)\n",
|
| 181 |
+
"reloaded_dataset"
|
| 182 |
+
]
|
| 183 |
+
},
|
| 184 |
+
{
|
| 185 |
+
"cell_type": "code",
|
| 186 |
+
"execution_count": 6,
|
| 187 |
+
"id": "c4b75140-eb3d-425a-9769-b7a838b66a1d",
|
| 188 |
+
"metadata": {
|
| 189 |
+
"colab": {
|
| 190 |
+
"base_uri": "https://localhost:8080/"
|
| 191 |
+
},
|
| 192 |
+
"id": "c4b75140-eb3d-425a-9769-b7a838b66a1d",
|
| 193 |
+
"outputId": "ccf919fd-c5e9-4e1d-9c7a-f49eecbac827"
|
| 194 |
+
},
|
| 195 |
+
"outputs": [
|
| 196 |
+
{
|
| 197 |
+
"data": {
|
| 198 |
+
"text/plain": [
|
| 199 |
+
"device(type='cuda')"
|
| 200 |
+
]
|
| 201 |
+
},
|
| 202 |
+
"execution_count": 6,
|
| 203 |
+
"metadata": {},
|
| 204 |
+
"output_type": "execute_result"
|
| 205 |
+
}
|
| 206 |
+
],
|
| 207 |
+
"source": [
|
| 208 |
+
"# Set device\n",
|
| 209 |
+
"device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n",
|
| 210 |
+
"device"
|
| 211 |
+
]
|
| 212 |
+
},
|
| 213 |
+
{
|
| 214 |
+
"cell_type": "code",
|
| 215 |
+
"execution_count": 7,
|
| 216 |
+
"id": "a8808490-4734-470b-b330-470accf429b3",
|
| 217 |
+
"metadata": {
|
| 218 |
+
"colab": {
|
| 219 |
+
"base_uri": "https://localhost:8080/"
|
| 220 |
+
},
|
| 221 |
+
"id": "a8808490-4734-470b-b330-470accf429b3",
|
| 222 |
+
"outputId": "47144284-4221-4def-87fd-fb05fb375345"
|
| 223 |
+
},
|
| 224 |
+
"outputs": [
|
| 225 |
+
{
|
| 226 |
+
"data": {
|
| 227 |
+
"text/plain": [
|
| 228 |
+
"'NVIDIA RTX 500 Ada Generation Laptop GPU'"
|
| 229 |
+
]
|
| 230 |
+
},
|
| 231 |
+
"execution_count": 7,
|
| 232 |
+
"metadata": {},
|
| 233 |
+
"output_type": "execute_result"
|
| 234 |
+
}
|
| 235 |
+
],
|
| 236 |
+
"source": [
|
| 237 |
+
"torch.cuda.get_device_name(0)"
|
| 238 |
+
]
|
| 239 |
+
},
|
| 240 |
+
{
|
| 241 |
+
"cell_type": "code",
|
| 242 |
+
"execution_count": 8,
|
| 243 |
+
"id": "K_3r1z0op4Ky",
|
| 244 |
+
"metadata": {
|
| 245 |
+
"id": "K_3r1z0op4Ky"
|
| 246 |
+
},
|
| 247 |
+
"outputs": [],
|
| 248 |
+
"source": [
|
| 249 |
+
"def get_wrong_image(dataset,correct_class):\n",
|
| 250 |
+
" for data in dataset:\n",
|
| 251 |
+
" if data['company'] != correct_class:\n",
|
| 252 |
+
" return data['image']"
|
| 253 |
+
]
|
| 254 |
+
},
|
| 255 |
+
{
|
| 256 |
+
"cell_type": "code",
|
| 257 |
+
"execution_count": 9,
|
| 258 |
+
"id": "FOoGc_spmduX",
|
| 259 |
+
"metadata": {
|
| 260 |
+
"id": "FOoGc_spmduX"
|
| 261 |
+
},
|
| 262 |
+
"outputs": [],
|
| 263 |
+
"source": [
|
| 264 |
+
"# prompt: transform PIL Image to Tensor\\\n",
|
| 265 |
+
"\n",
|
| 266 |
+
"from torchvision import transforms\n",
|
| 267 |
+
"\n",
|
| 268 |
+
"transform = transforms.PILToTensor()\n",
|
| 269 |
+
"\n",
|
| 270 |
+
"TrainTransformedImage = []\n",
|
| 271 |
+
"WorngTrainTransformedImage = []\n",
|
| 272 |
+
"TrainTextVector = []\n",
|
| 273 |
+
"for data in reloaded_dataset[\"train\"]:\n",
|
| 274 |
+
" image_tensor = transform(data['image'].convert(\"RGB\"))\n",
|
| 275 |
+
" wrong_image_tensor = transform(get_wrong_image(reloaded_dataset[\"train\"],data['company']).convert(\"RGB\"))\n",
|
| 276 |
+
" TrainTransformedImage.append(image_tensor)\n",
|
| 277 |
+
" WorngTrainTransformedImage.append(wrong_image_tensor)\n",
|
| 278 |
+
" TrainTextVector.append(np.array(data['fulltext_vector'], dtype=\"float32\"))\n",
|
| 279 |
+
"\n"
|
| 280 |
+
]
|
| 281 |
+
},
|
| 282 |
+
{
|
| 283 |
+
"cell_type": "code",
|
| 284 |
+
"execution_count": 10,
|
| 285 |
+
"id": "MxB4YVQKm8OV",
|
| 286 |
+
"metadata": {
|
| 287 |
+
"id": "MxB4YVQKm8OV"
|
| 288 |
+
},
|
| 289 |
+
"outputs": [],
|
| 290 |
+
"source": [
|
| 291 |
+
"# prompt: transform PIL Image to Tensor\\\n",
|
| 292 |
+
"\n",
|
| 293 |
+
"from torchvision import transforms\n",
|
| 294 |
+
"\n",
|
| 295 |
+
"transform = transforms.PILToTensor()\n",
|
| 296 |
+
"\n",
|
| 297 |
+
"TestTransformedImage = []\n",
|
| 298 |
+
"WorngTestTransformedImage = []\n",
|
| 299 |
+
"TestTextVector = []\n",
|
| 300 |
+
"for data in reloaded_dataset[\"test\"]:\n",
|
| 301 |
+
" image_tensor = transform(data['image'].convert(\"RGB\"))\n",
|
| 302 |
+
" wrong_image_tensor = transform(get_wrong_image(reloaded_dataset[\"test\"],data['company']).convert(\"RGB\"))\n",
|
| 303 |
+
" TestTransformedImage.append(image_tensor)\n",
|
| 304 |
+
" WorngTestTransformedImage.append(wrong_image_tensor)\n",
|
| 305 |
+
" TestTextVector.append(np.array(data['fulltext_vector'], dtype=\"float32\"))"
|
| 306 |
+
]
|
| 307 |
+
},
|
| 308 |
+
{
|
| 309 |
+
"cell_type": "code",
|
| 310 |
+
"execution_count": 11,
|
| 311 |
+
"id": "zVPu-CLLlEo5",
|
| 312 |
+
"metadata": {
|
| 313 |
+
"id": "zVPu-CLLlEo5"
|
| 314 |
+
},
|
| 315 |
+
"outputs": [],
|
| 316 |
+
"source": [
|
| 317 |
+
"from torch.utils.data import Dataset\n",
|
| 318 |
+
"import numpy as np\n",
|
| 319 |
+
"\n",
|
| 320 |
+
"class EmojiDataset(Dataset):\n",
|
| 321 |
+
" def __init__(self,transformed_image,wrong_transformed_image,text_vector):\n",
|
| 322 |
+
" self.image_transform = transformed_image\n",
|
| 323 |
+
" self.wrong_image_transform = wrong_transformed_image\n",
|
| 324 |
+
" self.text_vector = text_vector\n",
|
| 325 |
+
"\n",
|
| 326 |
+
" def __len__(self):\n",
|
| 327 |
+
" return len(self.image_transform)\n",
|
| 328 |
+
"\n",
|
| 329 |
+
" def __getitem__(self, idx):\n",
|
| 330 |
+
" image = self.image_transform[idx]\n",
|
| 331 |
+
" wrong_image = self.wrong_image_transform[idx]\n",
|
| 332 |
+
" fulltext_vector = self.text_vector[idx]\n",
|
| 333 |
+
" return image.float(), fulltext_vector, wrong_image\n"
|
| 334 |
+
]
|
| 335 |
+
},
|
| 336 |
+
{
|
| 337 |
+
"cell_type": "code",
|
| 338 |
+
"execution_count": 12,
|
| 339 |
+
"id": "HSAg0I6jaaSW",
|
| 340 |
+
"metadata": {
|
| 341 |
+
"id": "HSAg0I6jaaSW"
|
| 342 |
+
},
|
| 343 |
+
"outputs": [],
|
| 344 |
+
"source": [
|
| 345 |
+
"batch_size = 64"
|
| 346 |
+
]
|
| 347 |
+
},
|
| 348 |
+
{
|
| 349 |
+
"cell_type": "code",
|
| 350 |
+
"execution_count": 13,
|
| 351 |
+
"id": "E2dcO4FIg4Pj",
|
| 352 |
+
"metadata": {
|
| 353 |
+
"id": "E2dcO4FIg4Pj"
|
| 354 |
+
},
|
| 355 |
+
"outputs": [],
|
| 356 |
+
"source": [
|
| 357 |
+
"train_data = EmojiDataset(TrainTransformedImage,WorngTrainTransformedImage,TrainTextVector)\n",
|
| 358 |
+
"train_dataloader = DataLoader(train_data, batch_size=batch_size, shuffle=True)\n",
|
| 359 |
+
"test_data = EmojiDataset(TestTransformedImage,WorngTestTransformedImage,TestTextVector)\n",
|
| 360 |
+
"test_dataloader = DataLoader(test_data, batch_size=batch_size, shuffle=True)"
|
| 361 |
+
]
|
| 362 |
+
},
|
| 363 |
+
{
|
| 364 |
+
"cell_type": "code",
|
| 365 |
+
"execution_count": 14,
|
| 366 |
+
"id": "408f4137-3658-4b46-8034-2591773e70eb",
|
| 367 |
+
"metadata": {
|
| 368 |
+
"editable": true,
|
| 369 |
+
"id": "408f4137-3658-4b46-8034-2591773e70eb",
|
| 370 |
+
"tags": []
|
| 371 |
+
},
|
| 372 |
+
"outputs": [],
|
| 373 |
+
"source": [
|
| 374 |
+
"class generator(nn.Module):\n",
|
| 375 |
+
" def __init__(self):\n",
|
| 376 |
+
" super(generator, self).__init__()\n",
|
| 377 |
+
" self.image_size = 64\n",
|
| 378 |
+
" self.num_channels = 3\n",
|
| 379 |
+
" self.noise_dim = 100\n",
|
| 380 |
+
" self.embed_dim = 100\n",
|
| 381 |
+
" self.projected_embed_dim = 128\n",
|
| 382 |
+
" self.latent_dim = self.noise_dim + self.projected_embed_dim\n",
|
| 383 |
+
" self.ngf = 64\n",
|
| 384 |
+
"\n",
|
| 385 |
+
" self.projection = nn.Sequential(\n",
|
| 386 |
+
" nn.Linear(in_features=self.embed_dim, out_features=self.projected_embed_dim),\n",
|
| 387 |
+
" nn.BatchNorm1d(num_features=self.projected_embed_dim),\n",
|
| 388 |
+
" nn.LeakyReLU(negative_slope=0.2, inplace=True)\n",
|
| 389 |
+
" )\n",
|
| 390 |
+
"\n",
|
| 391 |
+
" self.netG = nn.Sequential(\n",
|
| 392 |
+
" nn.ConvTranspose2d(self.latent_dim, self.ngf * 8, 4, 1, 0, bias=False),\n",
|
| 393 |
+
" nn.BatchNorm2d(self.ngf * 8),\n",
|
| 394 |
+
" nn.ReLU(True),\n",
|
| 395 |
+
" # state size. (ngf*8) x 4 x 4\n",
|
| 396 |
+
" nn.ConvTranspose2d(self.ngf * 8, self.ngf * 4, 4, 2, 1, bias=False),\n",
|
| 397 |
+
" nn.BatchNorm2d(self.ngf * 4),\n",
|
| 398 |
+
" nn.ReLU(True),\n",
|
| 399 |
+
" # state size. (ngf*4) x 8 x 8\n",
|
| 400 |
+
" nn.ConvTranspose2d(self.ngf * 4, self.ngf * 2, 4, 2, 1, bias=False),\n",
|
| 401 |
+
" nn.BatchNorm2d(self.ngf * 2),\n",
|
| 402 |
+
" nn.ReLU(True),\n",
|
| 403 |
+
" # state size. (ngf*2) x 16 x 16\n",
|
| 404 |
+
" nn.ConvTranspose2d(self.ngf * 2,self.ngf, 4, 2, 1, bias=False),\n",
|
| 405 |
+
" nn.BatchNorm2d(self.ngf),\n",
|
| 406 |
+
" nn.ReLU(True),\n",
|
| 407 |
+
" # state size. (ngf) x 32 x 32\n",
|
| 408 |
+
" nn.ConvTranspose2d(self.ngf, self.num_channels, 4, 2, 1, bias=False),\n",
|
| 409 |
+
" nn.Tanh()\n",
|
| 410 |
+
" # state size. (num_channels) x 64 x 64\n",
|
| 411 |
+
" )\n",
|
| 412 |
+
"\n",
|
| 413 |
+
"\n",
|
| 414 |
+
" def forward(self, embed_vector, z):\n",
|
| 415 |
+
"\n",
|
| 416 |
+
" projected_embed = self.projection(embed_vector).unsqueeze(2).unsqueeze(3)\n",
|
| 417 |
+
" latent_vector = torch.cat([projected_embed, z], 1)\n",
|
| 418 |
+
" output = self.netG(latent_vector)\n",
|
| 419 |
+
"\n",
|
| 420 |
+
" return output"
|
| 421 |
+
]
|
| 422 |
+
},
|
| 423 |
+
{
|
| 424 |
+
"cell_type": "code",
|
| 425 |
+
"execution_count": 15,
|
| 426 |
+
"id": "e2ce6d2d-87c7-4cde-a7e2-e382d8b334b0",
|
| 427 |
+
"metadata": {
|
| 428 |
+
"colab": {
|
| 429 |
+
"base_uri": "https://localhost:8080/"
|
| 430 |
+
},
|
| 431 |
+
"id": "e2ce6d2d-87c7-4cde-a7e2-e382d8b334b0",
|
| 432 |
+
"outputId": "94a5adf9-6d0b-4f55-c61b-2ff189d4c7d9"
|
| 433 |
+
},
|
| 434 |
+
"outputs": [
|
| 435 |
+
{
|
| 436 |
+
"name": "stdout",
|
| 437 |
+
"output_type": "stream",
|
| 438 |
+
"text": [
|
| 439 |
+
"generator(\n",
|
| 440 |
+
" (projection): Sequential(\n",
|
| 441 |
+
" (0): Linear(in_features=100, out_features=128, bias=True)\n",
|
| 442 |
+
" (1): BatchNorm1d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
|
| 443 |
+
" (2): LeakyReLU(negative_slope=0.2, inplace=True)\n",
|
| 444 |
+
" )\n",
|
| 445 |
+
" (netG): Sequential(\n",
|
| 446 |
+
" (0): ConvTranspose2d(228, 512, kernel_size=(4, 4), stride=(1, 1), bias=False)\n",
|
| 447 |
+
" (1): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
|
| 448 |
+
" (2): ReLU(inplace=True)\n",
|
| 449 |
+
" (3): ConvTranspose2d(512, 256, kernel_size=(4, 4), stride=(2, 2), padding=(1, 1), bias=False)\n",
|
| 450 |
+
" (4): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
|
| 451 |
+
" (5): ReLU(inplace=True)\n",
|
| 452 |
+
" (6): ConvTranspose2d(256, 128, kernel_size=(4, 4), stride=(2, 2), padding=(1, 1), bias=False)\n",
|
| 453 |
+
" (7): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
|
| 454 |
+
" (8): ReLU(inplace=True)\n",
|
| 455 |
+
" (9): ConvTranspose2d(128, 64, kernel_size=(4, 4), stride=(2, 2), padding=(1, 1), bias=False)\n",
|
| 456 |
+
" (10): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
|
| 457 |
+
" (11): ReLU(inplace=True)\n",
|
| 458 |
+
" (12): ConvTranspose2d(64, 3, kernel_size=(4, 4), stride=(2, 2), padding=(1, 1), bias=False)\n",
|
| 459 |
+
" (13): Tanh()\n",
|
| 460 |
+
" )\n",
|
| 461 |
+
")\n"
|
| 462 |
+
]
|
| 463 |
+
}
|
| 464 |
+
],
|
| 465 |
+
"source": [
|
| 466 |
+
"netG = generator().to(device)\n",
|
| 467 |
+
"# Handle multi-gpu if desired\n",
|
| 468 |
+
"netG.apply(Utils.weights_init)\n",
|
| 469 |
+
"# Print the model\n",
|
| 470 |
+
"print(netG)"
|
| 471 |
+
]
|
| 472 |
+
},
|
| 473 |
+
{
|
| 474 |
+
"cell_type": "code",
|
| 475 |
+
"execution_count": 16,
|
| 476 |
+
"id": "8ca1cf01-943e-47a8-acf7-9576243d5119",
|
| 477 |
+
"metadata": {
|
| 478 |
+
"id": "8ca1cf01-943e-47a8-acf7-9576243d5119"
|
| 479 |
+
},
|
| 480 |
+
"outputs": [],
|
| 481 |
+
"source": [
|
| 482 |
+
"class discriminator(nn.Module):\n",
|
| 483 |
+
" def __init__(self):\n",
|
| 484 |
+
" super(discriminator, self).__init__()\n",
|
| 485 |
+
" self.image_size = 64\n",
|
| 486 |
+
" self.num_channels = 3\n",
|
| 487 |
+
" self.embed_dim = 100\n",
|
| 488 |
+
" self.projected_embed_dim = 128\n",
|
| 489 |
+
" self.ndf = 64\n",
|
| 490 |
+
" self.B_dim = 128\n",
|
| 491 |
+
" self.C_dim = 16\n",
|
| 492 |
+
"\n",
|
| 493 |
+
" self.netD_1 = nn.Sequential(\n",
|
| 494 |
+
" # input is (nc) x 64 x 64\n",
|
| 495 |
+
" nn.Conv2d(self.num_channels, self.ndf, 4, 2, 1, bias=False),\n",
|
| 496 |
+
" nn.LeakyReLU(0.2, inplace=True),\n",
|
| 497 |
+
" # state size. (ndf) x 32 x 32\n",
|
| 498 |
+
" nn.Conv2d(self.ndf, self.ndf * 2, 4, 2, 1, bias=False),\n",
|
| 499 |
+
" nn.BatchNorm2d(self.ndf * 2),\n",
|
| 500 |
+
" nn.LeakyReLU(0.2, inplace=True),\n",
|
| 501 |
+
" # state size. (ndf*2) x 16 x 16\n",
|
| 502 |
+
" nn.Conv2d(self.ndf * 2, self.ndf * 4, 4, 2, 1, bias=False),\n",
|
| 503 |
+
" nn.BatchNorm2d(self.ndf * 4),\n",
|
| 504 |
+
" nn.LeakyReLU(0.2, inplace=True),\n",
|
| 505 |
+
" # state size. (ndf*4) x 8 x 8\n",
|
| 506 |
+
" nn.Conv2d(self.ndf * 4, self.ndf * 8, 4, 2, 1, bias=False),\n",
|
| 507 |
+
" nn.BatchNorm2d(self.ndf * 8),\n",
|
| 508 |
+
" nn.LeakyReLU(0.2, inplace=True),\n",
|
| 509 |
+
" )\n",
|
| 510 |
+
"\n",
|
| 511 |
+
" self.projector = utils.Concat_embed(self.embed_dim, self.projected_embed_dim)\n",
|
| 512 |
+
"\n",
|
| 513 |
+
" self.netD_2 = nn.Sequential(\n",
|
| 514 |
+
" # state size. (ndf*8) x 4 x 4\n",
|
| 515 |
+
" nn.Conv2d(self.ndf * 8 + self.projected_embed_dim, 1, 4, 1, 0, bias=False),\n",
|
| 516 |
+
" nn.Sigmoid()\n",
|
| 517 |
+
" )\n",
|
| 518 |
+
"\n",
|
| 519 |
+
" def forward(self, inp, embed):\n",
|
| 520 |
+
" x_intermediate = self.netD_1(inp)\n",
|
| 521 |
+
" x = self.projector(x_intermediate, embed)\n",
|
| 522 |
+
" x = self.netD_2(x)\n",
|
| 523 |
+
"\n",
|
| 524 |
+
" return x.view(-1, 1).squeeze(1) , x_intermediate"
|
| 525 |
+
]
|
| 526 |
+
},
|
| 527 |
+
{
|
| 528 |
+
"cell_type": "code",
|
| 529 |
+
"execution_count": 17,
|
| 530 |
+
"id": "88574088-bcca-49dd-91c2-e9f6b40e4b8f",
|
| 531 |
+
"metadata": {
|
| 532 |
+
"colab": {
|
| 533 |
+
"base_uri": "https://localhost:8080/"
|
| 534 |
+
},
|
| 535 |
+
"id": "88574088-bcca-49dd-91c2-e9f6b40e4b8f",
|
| 536 |
+
"outputId": "8d364989-a60f-4606-c934-f559b0c7fe87"
|
| 537 |
+
},
|
| 538 |
+
"outputs": [
|
| 539 |
+
{
|
| 540 |
+
"name": "stdout",
|
| 541 |
+
"output_type": "stream",
|
| 542 |
+
"text": [
|
| 543 |
+
"discriminator(\n",
|
| 544 |
+
" (netD_1): Sequential(\n",
|
| 545 |
+
" (0): Conv2d(3, 64, kernel_size=(4, 4), stride=(2, 2), padding=(1, 1), bias=False)\n",
|
| 546 |
+
" (1): LeakyReLU(negative_slope=0.2, inplace=True)\n",
|
| 547 |
+
" (2): Conv2d(64, 128, kernel_size=(4, 4), stride=(2, 2), padding=(1, 1), bias=False)\n",
|
| 548 |
+
" (3): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
|
| 549 |
+
" (4): LeakyReLU(negative_slope=0.2, inplace=True)\n",
|
| 550 |
+
" (5): Conv2d(128, 256, kernel_size=(4, 4), stride=(2, 2), padding=(1, 1), bias=False)\n",
|
| 551 |
+
" (6): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
|
| 552 |
+
" (7): LeakyReLU(negative_slope=0.2, inplace=True)\n",
|
| 553 |
+
" (8): Conv2d(256, 512, kernel_size=(4, 4), stride=(2, 2), padding=(1, 1), bias=False)\n",
|
| 554 |
+
" (9): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
|
| 555 |
+
" (10): LeakyReLU(negative_slope=0.2, inplace=True)\n",
|
| 556 |
+
" )\n",
|
| 557 |
+
" (projector): Concat_embed(\n",
|
| 558 |
+
" (projection): Sequential(\n",
|
| 559 |
+
" (0): Linear(in_features=100, out_features=128, bias=True)\n",
|
| 560 |
+
" (1): BatchNorm1d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
|
| 561 |
+
" (2): LeakyReLU(negative_slope=0.2, inplace=True)\n",
|
| 562 |
+
" )\n",
|
| 563 |
+
" )\n",
|
| 564 |
+
" (netD_2): Sequential(\n",
|
| 565 |
+
" (0): Conv2d(640, 1, kernel_size=(4, 4), stride=(1, 1), bias=False)\n",
|
| 566 |
+
" (1): Sigmoid()\n",
|
| 567 |
+
" )\n",
|
| 568 |
+
")\n"
|
| 569 |
+
]
|
| 570 |
+
}
|
| 571 |
+
],
|
| 572 |
+
"source": [
|
| 573 |
+
"netD_1 = discriminator().to(device)\n",
|
| 574 |
+
"# Handle multi-gpu if desired\n",
|
| 575 |
+
"netD_1.apply(Utils.weights_init)\n",
|
| 576 |
+
"# Print the model\n",
|
| 577 |
+
"print(netD_1)"
|
| 578 |
+
]
|
| 579 |
+
},
|
| 580 |
+
{
|
| 581 |
+
"cell_type": "code",
|
| 582 |
+
"execution_count": 18,
|
| 583 |
+
"id": "118be16d-27aa-41c1-a514-ca1fc95c54df",
|
| 584 |
+
"metadata": {
|
| 585 |
+
"id": "118be16d-27aa-41c1-a514-ca1fc95c54df"
|
| 586 |
+
},
|
| 587 |
+
"outputs": [],
|
| 588 |
+
"source": [
|
| 589 |
+
"class gan_factory(object):\n",
|
| 590 |
+
"\n",
|
| 591 |
+
" @staticmethod\n",
|
| 592 |
+
" def generator_factory(type):\n",
|
| 593 |
+
" if type == 'gan':\n",
|
| 594 |
+
" return generator()\n",
|
| 595 |
+
"\n",
|
| 596 |
+
" @staticmethod\n",
|
| 597 |
+
" def discriminator_factory(type):\n",
|
| 598 |
+
" if type == 'gan':\n",
|
| 599 |
+
" return discriminator()"
|
| 600 |
+
]
|
| 601 |
+
},
|
| 602 |
+
{
|
| 603 |
+
"cell_type": "code",
|
| 604 |
+
"execution_count": 19,
|
| 605 |
+
"id": "16c6ac96-644c-4269-8731-ab447ad478fd",
|
| 606 |
+
"metadata": {
|
| 607 |
+
"editable": true,
|
| 608 |
+
"id": "16c6ac96-644c-4269-8731-ab447ad478fd",
|
| 609 |
+
"tags": []
|
| 610 |
+
},
|
| 611 |
+
"outputs": [],
|
| 612 |
+
"source": [
|
| 613 |
+
"import numpy as np\n",
|
| 614 |
+
"import torch\n",
|
| 615 |
+
"import yaml\n",
|
| 616 |
+
"from torch import nn\n",
|
| 617 |
+
"from torch.autograd import Variable\n",
|
| 618 |
+
"from torch.utils.data import DataLoader\n",
|
| 619 |
+
"\n",
|
| 620 |
+
"from utils import Utils, Logger\n",
|
| 621 |
+
"from PIL import Image\n",
|
| 622 |
+
"import os\n",
|
| 623 |
+
"\n",
|
| 624 |
+
"class Trainer(object):\n",
|
| 625 |
+
" def __init__(self, type, dataset, lr, save_path, l1_coef, l2_coef, batch_size, num_workers, epochs):\n",
|
| 626 |
+
"\n",
|
| 627 |
+
" self.generator = torch.nn.DataParallel(gan_factory.generator_factory(type).cuda())\n",
|
| 628 |
+
" self.discriminator = torch.nn.DataParallel(gan_factory.discriminator_factory(type).cuda())\n",
|
| 629 |
+
"\n",
|
| 630 |
+
" self.discriminator.apply(Utils.weights_init)\n",
|
| 631 |
+
"\n",
|
| 632 |
+
" self.generator.apply(Utils.weights_init)\n",
|
| 633 |
+
"\n",
|
| 634 |
+
" self.dataset = dataset\n",
|
| 635 |
+
"\n",
|
| 636 |
+
" #print \"Image = \",len(self.dataset)\n",
|
| 637 |
+
" self.noise_dim = 100\n",
|
| 638 |
+
" self.batch_size = batch_size\n",
|
| 639 |
+
" self.num_workers = num_workers\n",
|
| 640 |
+
" self.lr = lr\n",
|
| 641 |
+
" self.beta1 = 0.5\n",
|
| 642 |
+
" self.num_epochs = epochs\n",
|
| 643 |
+
"\n",
|
| 644 |
+
"\n",
|
| 645 |
+
" self.l1_coef = l1_coef\n",
|
| 646 |
+
" self.l2_coef = l2_coef\n",
|
| 647 |
+
"\n",
|
| 648 |
+
" self.data_loader = DataLoader(self.dataset, batch_size=self.batch_size, shuffle=True, num_workers=self.num_workers)\n",
|
| 649 |
+
"\n",
|
| 650 |
+
" self.optimD = torch.optim.Adam(self.discriminator.parameters(), lr=self.lr, betas=(self.beta1, 0.999))\n",
|
| 651 |
+
" self.optimG = torch.optim.Adam(self.generator.parameters(), lr=self.lr, betas=(self.beta1, 0.999))\n",
|
| 652 |
+
"\n",
|
| 653 |
+
" self.logger = Logger()\n",
|
| 654 |
+
" self.checkpoints_path = 'checkpoints'\n",
|
| 655 |
+
" self.save_path = save_path\n",
|
| 656 |
+
" self.type = type\n",
|
| 657 |
+
"\n",
|
| 658 |
+
" def train(self, cls):\n",
|
| 659 |
+
"\n",
|
| 660 |
+
" if self.type == 'gan':\n",
|
| 661 |
+
" self._train_gan(cls)\n",
|
| 662 |
+
"\n",
|
| 663 |
+
"\n",
|
| 664 |
+
" def _train_gan(self, cls):\n",
|
| 665 |
+
" criterion = nn.BCELoss()\n",
|
| 666 |
+
" l2_loss = nn.MSELoss()\n",
|
| 667 |
+
" l1_loss = nn.L1Loss()\n",
|
| 668 |
+
" #print(\"Started Training\")\n",
|
| 669 |
+
" for epoch in range(self.num_epochs):\n",
|
| 670 |
+
" iteration = 0\n",
|
| 671 |
+
" #print(\"Starting Iter :\",iteration)\n",
|
| 672 |
+
" for sample in self.data_loader:\n",
|
| 673 |
+
" #print('Inside Dataloader loop is')\n",
|
| 674 |
+
" iteration += 1\n",
|
| 675 |
+
" right_images = sample[0]\n",
|
| 676 |
+
" right_embed = sample[1]\n",
|
| 677 |
+
" wrong_images = sample[2]\n",
|
| 678 |
+
"\n",
|
| 679 |
+
"\n",
|
| 680 |
+
"\n",
|
| 681 |
+
" right_images = Variable(right_images.float()).cuda()\n",
|
| 682 |
+
" right_embed = Variable(right_embed.float()).cuda()\n",
|
| 683 |
+
" wrong_images = Variable(wrong_images.float()).cuda()\n",
|
| 684 |
+
"\n",
|
| 685 |
+
" #print(\"Data Loaded\")\n",
|
| 686 |
+
"\n",
|
| 687 |
+
" real_labels = torch.ones(right_images.size(0))\n",
|
| 688 |
+
" fake_labels = torch.zeros(right_images.size(0))\n",
|
| 689 |
+
"\n",
|
| 690 |
+
" smoothed_real_labels = torch.FloatTensor(Utils.smooth_label(real_labels.numpy(), -0.1))\n",
|
| 691 |
+
"\n",
|
| 692 |
+
" real_labels = Variable(real_labels).cuda()\n",
|
| 693 |
+
" smoothed_real_labels = Variable(smoothed_real_labels).cuda()\n",
|
| 694 |
+
" fake_labels = Variable(fake_labels).cuda()\n",
|
| 695 |
+
"\n",
|
| 696 |
+
" # Train the discriminator\n",
|
| 697 |
+
" self.discriminator.zero_grad()\n",
|
| 698 |
+
" outputs, activation_real = self.discriminator(right_images, right_embed)\n",
|
| 699 |
+
" real_loss = criterion(outputs, smoothed_real_labels)\n",
|
| 700 |
+
" real_score = outputs\n",
|
| 701 |
+
"\n",
|
| 702 |
+
" if cls:\n",
|
| 703 |
+
" outputs, _ = self.discriminator(wrong_images, right_embed)\n",
|
| 704 |
+
" wrong_loss = criterion(outputs, fake_labels)\n",
|
| 705 |
+
" wrong_score = outputs\n",
|
| 706 |
+
"\n",
|
| 707 |
+
" noise = Variable(torch.randn(right_images.size(0), 100)).cuda()\n",
|
| 708 |
+
" noise = noise.view(noise.size(0), 100, 1, 1)\n",
|
| 709 |
+
" fake_images = self.generator(right_embed, noise)\n",
|
| 710 |
+
" outputs, _ = self.discriminator(fake_images, right_embed)\n",
|
| 711 |
+
" fake_loss = criterion(outputs, fake_labels)\n",
|
| 712 |
+
" fake_score = outputs\n",
|
| 713 |
+
"\n",
|
| 714 |
+
" d_loss = real_loss + fake_loss\n",
|
| 715 |
+
"\n",
|
| 716 |
+
" if cls:\n",
|
| 717 |
+
" d_loss = d_loss + wrong_loss\n",
|
| 718 |
+
"\n",
|
| 719 |
+
" d_loss.backward()\n",
|
| 720 |
+
" self.optimD.step()\n",
|
| 721 |
+
"\n",
|
| 722 |
+
" # Train the generator\n",
|
| 723 |
+
" self.generator.zero_grad()\n",
|
| 724 |
+
" noise = Variable(torch.randn(right_images.size(0), 100)).cuda()\n",
|
| 725 |
+
" noise = noise.view(noise.size(0), 100, 1, 1)\n",
|
| 726 |
+
" fake_images = self.generator(right_embed, noise)\n",
|
| 727 |
+
" outputs, activation_fake = self.discriminator(fake_images, right_embed)\n",
|
| 728 |
+
" _, activation_real = self.discriminator(right_images, right_embed)\n",
|
| 729 |
+
"\n",
|
| 730 |
+
" activation_fake = torch.mean(activation_fake, 0) #try with median and check if it converges\n",
|
| 731 |
+
" activation_real = torch.mean(activation_real, 0) #try with median and check if it converges\n",
|
| 732 |
+
"\n",
|
| 733 |
+
"\n",
|
| 734 |
+
" g_loss = criterion(outputs, real_labels) + self.l2_coef * l2_loss(activation_fake, activation_real.detach()) + self.l1_coef * l1_loss(fake_images, right_images)\n",
|
| 735 |
+
"\n",
|
| 736 |
+
" g_loss.backward()\n",
|
| 737 |
+
" self.optimG.step()\n",
|
| 738 |
+
"\n",
|
| 739 |
+
" #print('Completed Iter:', iteration)\n",
|
| 740 |
+
"\n",
|
| 741 |
+
" self.logger.log_iteration_gan(epoch, iteration, d_loss, g_loss, real_score, fake_score)\n",
|
| 742 |
+
"\n",
|
| 743 |
+
"\n",
|
| 744 |
+
" if (epoch) % 10 == 0:\n",
|
| 745 |
+
" print('epoch', epoch, 'complete')\n",
|
| 746 |
+
" Utils.save_checkpoint(self.discriminator, self.generator, self.checkpoints_path, self.save_path, epoch)"
|
| 747 |
+
]
|
| 748 |
+
},
|
| 749 |
+
{
|
| 750 |
+
"cell_type": "code",
|
| 751 |
+
"execution_count": 18,
|
| 752 |
+
"id": "0d815894-238b-473d-8c29-cd2ebd948c59",
|
| 753 |
+
"metadata": {
|
| 754 |
+
"editable": true,
|
| 755 |
+
"id": "0d815894-238b-473d-8c29-cd2ebd948c59",
|
| 756 |
+
"tags": []
|
| 757 |
+
},
|
| 758 |
+
"outputs": [
|
| 759 |
+
{
|
| 760 |
+
"ename": "NameError",
|
| 761 |
+
"evalue": "name 'Trainer' is not defined",
|
| 762 |
+
"output_type": "error",
|
| 763 |
+
"traceback": [
|
| 764 |
+
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
|
| 765 |
+
"\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)",
|
| 766 |
+
"Cell \u001b[1;32mIn[18], line 15\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01measydict\u001b[39;00m\n\u001b[0;32m 4\u001b[0m args \u001b[38;5;241m=\u001b[39m easydict\u001b[38;5;241m.\u001b[39mEasyDict({\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mtype\u001b[39m\u001b[38;5;124m'\u001b[39m: \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mgan\u001b[39m\u001b[38;5;124m'\u001b[39m,\n\u001b[0;32m 5\u001b[0m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mlr\u001b[39m\u001b[38;5;124m'\u001b[39m: \u001b[38;5;241m0.001\u001b[39m,\n\u001b[0;32m 6\u001b[0m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124ml1_coef\u001b[39m\u001b[38;5;124m'\u001b[39m: \u001b[38;5;241m50\u001b[39m,\n\u001b[1;32m (...)\u001b[0m\n\u001b[0;32m 12\u001b[0m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mnum_workers\u001b[39m\u001b[38;5;124m'\u001b[39m:\u001b[38;5;241m6\u001b[39m,\n\u001b[0;32m 13\u001b[0m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mepochs\u001b[39m\u001b[38;5;124m'\u001b[39m: \u001b[38;5;241m100\u001b[39m})\n\u001b[1;32m---> 15\u001b[0m trainer \u001b[38;5;241m=\u001b[39m Trainer(\u001b[38;5;28mtype\u001b[39m\u001b[38;5;241m=\u001b[39margs\u001b[38;5;241m.\u001b[39mtype,\n\u001b[0;32m 16\u001b[0m dataset\u001b[38;5;241m=\u001b[39margs\u001b[38;5;241m.\u001b[39mdataset,\n\u001b[0;32m 17\u001b[0m lr\u001b[38;5;241m=\u001b[39margs\u001b[38;5;241m.\u001b[39mlr,\n\u001b[0;32m 18\u001b[0m save_path\u001b[38;5;241m=\u001b[39margs\u001b[38;5;241m.\u001b[39msave_path,\n\u001b[0;32m 19\u001b[0m l1_coef\u001b[38;5;241m=\u001b[39margs\u001b[38;5;241m.\u001b[39ml1_coef,\n\u001b[0;32m 20\u001b[0m l2_coef\u001b[38;5;241m=\u001b[39margs\u001b[38;5;241m.\u001b[39ml2_coef,\n\u001b[0;32m 21\u001b[0m batch_size\u001b[38;5;241m=\u001b[39margs\u001b[38;5;241m.\u001b[39mbatch_size,\n\u001b[0;32m 22\u001b[0m num_workers\u001b[38;5;241m=\u001b[39margs\u001b[38;5;241m.\u001b[39mnum_workers,\n\u001b[0;32m 23\u001b[0m epochs\u001b[38;5;241m=\u001b[39margs\u001b[38;5;241m.\u001b[39mepochs\n\u001b[0;32m 24\u001b[0m )\n\u001b[0;32m 26\u001b[0m trainer\u001b[38;5;241m.\u001b[39mtrain(args\u001b[38;5;241m.\u001b[39mcls)\n",
|
| 767 |
+
"\u001b[1;31mNameError\u001b[0m: name 'Trainer' is not defined"
|
| 768 |
+
]
|
| 769 |
+
}
|
| 770 |
+
],
|
| 771 |
+
"source": [
|
| 772 |
+
"import argparse\n",
|
| 773 |
+
"import easydict\n",
|
| 774 |
+
"\n",
|
| 775 |
+
"args = easydict.EasyDict({'type': 'gan',\n",
|
| 776 |
+
" 'lr': 0.001,\n",
|
| 777 |
+
" 'l1_coef': 50,\n",
|
| 778 |
+
" 'l2_coef': 100,\n",
|
| 779 |
+
" 'cls': True,\n",
|
| 780 |
+
" 'save_path':'Result',\n",
|
| 781 |
+
" 'dataset': test_data,\n",
|
| 782 |
+
" 'batch_size': batch_size,\n",
|
| 783 |
+
" 'num_workers':6,\n",
|
| 784 |
+
" 'epochs': 100})\n",
|
| 785 |
+
"\n",
|
| 786 |
+
"trainer = Trainer(type=args.type,\n",
|
| 787 |
+
" dataset=args.dataset,\n",
|
| 788 |
+
" lr=args.lr,\n",
|
| 789 |
+
" save_path=args.save_path,\n",
|
| 790 |
+
" l1_coef=args.l1_coef,\n",
|
| 791 |
+
" l2_coef=args.l2_coef,\n",
|
| 792 |
+
" batch_size=args.batch_size,\n",
|
| 793 |
+
" num_workers=args.num_workers,\n",
|
| 794 |
+
" epochs=args.epochs\n",
|
| 795 |
+
" )\n",
|
| 796 |
+
"\n",
|
| 797 |
+
"trainer.train(args.cls)\n",
|
| 798 |
+
"#trainer.predict()"
|
| 799 |
+
]
|
| 800 |
+
},
|
| 801 |
+
{
|
| 802 |
+
"cell_type": "code",
|
| 803 |
+
"execution_count": null,
|
| 804 |
+
"id": "242ee9a9-47df-4c50-b7d9-ae181543f75f",
|
| 805 |
+
"metadata": {
|
| 806 |
+
"colab": {
|
| 807 |
+
"base_uri": "https://localhost:8080/",
|
| 808 |
+
"height": 211
|
| 809 |
+
},
|
| 810 |
+
"id": "242ee9a9-47df-4c50-b7d9-ae181543f75f",
|
| 811 |
+
"outputId": "1c0df61d-a9a3-461f-9ea1-a0eb63c92be1"
|
| 812 |
+
},
|
| 813 |
+
"outputs": [
|
| 814 |
+
{
|
| 815 |
+
"ename": "AttributeError",
|
| 816 |
+
"evalue": "'Logger' object has no attribute 'd_loss_list'",
|
| 817 |
+
"output_type": "error",
|
| 818 |
+
"traceback": [
|
| 819 |
+
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
|
| 820 |
+
"\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)",
|
| 821 |
+
"\u001b[0;32m<ipython-input-45-b7e01a63074d>\u001b[0m in \u001b[0;36m<cell line: 9>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 7\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 8\u001b[0m \u001b[0;31m# Extract the data from the logger\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 9\u001b[0;31m \u001b[0md_loss_list\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mtrainer\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mlogger\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0md_loss_list\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 10\u001b[0m \u001b[0mg_loss_list\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mtrainer\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mlogger\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mg_loss_list\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 11\u001b[0m \u001b[0md_x_list\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mtrainer\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mlogger\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0md_x_list\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
|
| 822 |
+
"\u001b[0;31mAttributeError\u001b[0m: 'Logger' object has no attribute 'd_loss_list'"
|
| 823 |
+
]
|
| 824 |
+
}
|
| 825 |
+
],
|
| 826 |
+
"source": [
|
| 827 |
+
"# prompt: draw a graph for the d_loss, g_loss, d(X) and d(g(x))\n",
|
| 828 |
+
"\n",
|
| 829 |
+
"import matplotlib.pyplot as plt\n",
|
| 830 |
+
"\n",
|
| 831 |
+
"# Assuming you have lists or arrays named d_loss_list, g_loss_list, d_x_list, d_gx_list\n",
|
| 832 |
+
"# containing the values for each metric over the training epochs.\n",
|
| 833 |
+
"\n",
|
| 834 |
+
"# Extract the data from the logger\n",
|
| 835 |
+
"d_loss_list = trainer.logger.d_loss_list\n",
|
| 836 |
+
"g_loss_list = trainer.logger.g_loss_list\n",
|
| 837 |
+
"d_x_list = trainer.logger.d_x_list\n",
|
| 838 |
+
"d_gx_list = trainer.logger.d_gx_list\n",
|
| 839 |
+
"\n",
|
| 840 |
+
"# Create the figure and axes\n",
|
| 841 |
+
"fig, axs = plt.subplots(2, 2, figsize=(12, 8))\n",
|
| 842 |
+
"\n",
|
| 843 |
+
"# Plot D Loss\n",
|
| 844 |
+
"axs[0, 0].plot(d_loss_list)\n",
|
| 845 |
+
"axs[0, 0].set_title(\"Discriminator Loss\")\n",
|
| 846 |
+
"axs[0, 0].set_xlabel(\"Epoch\")\n",
|
| 847 |
+
"axs[0, 0].set_ylabel(\"Loss\")\n",
|
| 848 |
+
"\n",
|
| 849 |
+
"# Plot G Loss\n",
|
| 850 |
+
"axs[0, 1].plot(g_loss_list)\n",
|
| 851 |
+
"axs[0, 1].set_title(\"Generator Loss\")\n",
|
| 852 |
+
"axs[0, 1].set_xlabel(\"Epoch\")\n",
|
| 853 |
+
"axs[0, 1].set_ylabel(\"Loss\")\n",
|
| 854 |
+
"\n",
|
| 855 |
+
"# Plot D(X)\n",
|
| 856 |
+
"axs[1, 0].plot(d_x_list)\n",
|
| 857 |
+
"axs[1, 0].set_title(\"Discriminator Output for Real Images (D(X))\")\n",
|
| 858 |
+
"axs[1, 0].set_xlabel(\"Epoch\")\n",
|
| 859 |
+
"axs[1, 0].set_ylabel(\"Score\")\n",
|
| 860 |
+
"\n",
|
| 861 |
+
"# Plot D(G(X))\n",
|
| 862 |
+
"axs[1, 1].plot(d_gx_list)\n",
|
| 863 |
+
"axs[1, 1].set_title(\"Discriminator Output for Fake Images (D(G(X)))\")\n",
|
| 864 |
+
"axs[1, 1].set_xlabel(\"Epoch\")\n",
|
| 865 |
+
"axs[1, 1].set_ylabel(\"Score\")\n",
|
| 866 |
+
"\n",
|
| 867 |
+
"# Adjust layout and display the plot\n",
|
| 868 |
+
"plt.tight_layout()\n",
|
| 869 |
+
"plt.show()\n"
|
| 870 |
+
]
|
| 871 |
+
},
|
| 872 |
+
{
|
| 873 |
+
"cell_type": "code",
|
| 874 |
+
"execution_count": null,
|
| 875 |
+
"id": "1e2bf862-35fe-433e-b1e2-012cbd5ee47d",
|
| 876 |
+
"metadata": {
|
| 877 |
+
"editable": true,
|
| 878 |
+
"id": "1e2bf862-35fe-433e-b1e2-012cbd5ee47d",
|
| 879 |
+
"tags": []
|
| 880 |
+
},
|
| 881 |
+
"outputs": [],
|
| 882 |
+
"source": [
|
| 883 |
+
"!cp -r checkpoints /content/drive/MyDrive"
|
| 884 |
+
]
|
| 885 |
+
},
|
| 886 |
+
{
|
| 887 |
+
"cell_type": "code",
|
| 888 |
+
"execution_count": null,
|
| 889 |
+
"id": "JDN71GvsFTyn",
|
| 890 |
+
"metadata": {
|
| 891 |
+
"id": "JDN71GvsFTyn"
|
| 892 |
+
},
|
| 893 |
+
"outputs": [],
|
| 894 |
+
"source": []
|
| 895 |
+
}
|
| 896 |
+
],
|
| 897 |
+
"metadata": {
|
| 898 |
+
"colab": {
|
| 899 |
+
"provenance": []
|
| 900 |
+
},
|
| 901 |
+
"kernelspec": {
|
| 902 |
+
"display_name": "Python 3 (ipykernel)",
|
| 903 |
+
"language": "python",
|
| 904 |
+
"name": "python3"
|
| 905 |
+
},
|
| 906 |
+
"language_info": {
|
| 907 |
+
"codemirror_mode": {
|
| 908 |
+
"name": "ipython",
|
| 909 |
+
"version": 3
|
| 910 |
+
},
|
| 911 |
+
"file_extension": ".py",
|
| 912 |
+
"mimetype": "text/x-python",
|
| 913 |
+
"name": "python",
|
| 914 |
+
"nbconvert_exporter": "python",
|
| 915 |
+
"pygments_lexer": "ipython3",
|
| 916 |
+
"version": "3.11.9"
|
| 917 |
+
}
|
| 918 |
+
},
|
| 919 |
+
"nbformat": 4,
|
| 920 |
+
"nbformat_minor": 5
|
| 921 |
+
}
|