Upload mindall-e-final.ipynb
Browse files- mindall-e-final.ipynb +176 -0
mindall-e-final.ipynb
ADDED
|
@@ -0,0 +1,176 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cells": [
|
| 3 |
+
{
|
| 4 |
+
"cell_type": "code",
|
| 5 |
+
"execution_count": 1,
|
| 6 |
+
"metadata": {
|
| 7 |
+
"_cell_guid": "b1076dfc-b9ad-4769-8c92-a6c4dae69d19",
|
| 8 |
+
"_uuid": "8f2839f25d086af736a60e9eeb907d3b93b6e0e5",
|
| 9 |
+
"execution": {
|
| 10 |
+
"iopub.execute_input": "2021-12-22T15:46:09.354280Z",
|
| 11 |
+
"iopub.status.busy": "2021-12-22T15:46:09.353419Z"
|
| 12 |
+
}
|
| 13 |
+
},
|
| 14 |
+
"outputs": [
|
| 15 |
+
{
|
| 16 |
+
"name": "stdout",
|
| 17 |
+
"output_type": "stream",
|
| 18 |
+
"text": [
|
| 19 |
+
"Downloading model...\n"
|
| 20 |
+
]
|
| 21 |
+
},
|
| 22 |
+
{
|
| 23 |
+
"ename": "KeyboardInterrupt",
|
| 24 |
+
"evalue": "",
|
| 25 |
+
"output_type": "error",
|
| 26 |
+
"traceback": [
|
| 27 |
+
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
|
| 28 |
+
"\u001b[0;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)",
|
| 29 |
+
"\u001b[0;32m/tmp/ipykernel_4175/2765481374.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 21\u001b[0m \u001b[0mdevice\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m'cuda:0'\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 22\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 23\u001b[0;31m \u001b[0;32mfrom\u001b[0m \u001b[0mdalle\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mutils\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mutils\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mset_seed\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 24\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0mdalle\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmodels\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mDalle\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 25\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mclip\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
|
| 30 |
+
"\u001b[0;32m~/Downloads/minDALL-E/dalle/utils/__init__.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;32mfrom\u001b[0m \u001b[0;34m.\u001b[0m\u001b[0mutils\u001b[0m \u001b[0;32mimport\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 2\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0;34m.\u001b[0m\u001b[0mconfig\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0;34m*\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0;34m.\u001b[0m\u001b[0msampling\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0;34m*\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
|
| 31 |
+
"\u001b[0;32m~/Downloads/minDALL-E/dalle/utils/utils.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 10\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mhashlib\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 11\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mtarfile\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 12\u001b[0;31m \u001b[0;32mimport\u001b[0m \u001b[0mtorch\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 13\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mclip\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 14\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mnumpy\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
|
| 32 |
+
"\u001b[0;32m~/.local/lib/python3.8/site-packages/torch/__init__.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 611\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 612\u001b[0m \u001b[0;31m# Shared memory manager needs to know the exact location of manager executable\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 613\u001b[0;31m \u001b[0m_C\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_initExtension\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmanager_path\u001b[0m\u001b[0;34m(\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 614\u001b[0m \u001b[0;32mdel\u001b[0m \u001b[0mmanager_path\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 615\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
|
| 33 |
+
"\u001b[0;32m~/.local/lib/python3.8/site-packages/torch/cuda/__init__.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 683\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0;34m.\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mprofiler\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 684\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0;34m.\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mnvtx\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 685\u001b[0;31m \u001b[0;32mfrom\u001b[0m \u001b[0;34m.\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mamp\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
|
| 34 |
+
"\u001b[0;32m~/.local/lib/python3.8/site-packages/torch/cuda/amp/__init__.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;32mfrom\u001b[0m \u001b[0;34m.\u001b[0m\u001b[0mautocast_mode\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mautocast\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcustom_fwd\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcustom_bwd\u001b[0m \u001b[0;31m# noqa: F401\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0;34m.\u001b[0m\u001b[0mgrad_scaler\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mGradScaler\u001b[0m \u001b[0;31m# noqa: F401\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
|
| 35 |
+
"\u001b[0;32m~/.local/lib/python3.8/site-packages/torch/cuda/amp/autocast_mode.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mcollections\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\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----> 5\u001b[0;31m \u001b[0;32mimport\u001b[0m \u001b[0mnumpy\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 6\u001b[0m \u001b[0mHAS_NUMPY\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;32mTrue\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 7\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mModuleNotFoundError\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
|
| 36 |
+
"\u001b[0;32m/usr/local/lib/python3.8/dist-packages/numpy/__init__.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 151\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0;34m.\u001b[0m\u001b[0mcore\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0;34m*\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 152\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0;34m.\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mcompat\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 153\u001b[0;31m \u001b[0;32mfrom\u001b[0m \u001b[0;34m.\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mlib\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 154\u001b[0m \u001b[0;31m# NOTE: to be revisited following future namespace cleanup.\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 155\u001b[0m \u001b[0;31m# See gh-14454 and gh-15672 for discussion.\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
|
| 37 |
+
"\u001b[0;32m/usr/local/lib/python3.8/dist-packages/numpy/lib/__init__.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 23\u001b[0m \u001b[0;31m# Private submodules\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 24\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0;34m.\u001b[0m\u001b[0mtype_check\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0;34m*\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 25\u001b[0;31m \u001b[0;32mfrom\u001b[0m \u001b[0;34m.\u001b[0m\u001b[0mindex_tricks\u001b[0m \u001b[0;32mimport\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 26\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0;34m.\u001b[0m\u001b[0mfunction_base\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0;34m*\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 27\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0;34m.\u001b[0m\u001b[0mnanfunctions\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0;34m*\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
|
| 38 |
+
"\u001b[0;32m/usr/local/lib/python3.8/dist-packages/numpy/lib/index_tricks.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 11\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 12\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mnumpy\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmatrixlib\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0mmatrixlib\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 13\u001b[0;31m \u001b[0;32mfrom\u001b[0m \u001b[0;34m.\u001b[0m\u001b[0mfunction_base\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mdiff\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 14\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0mnumpy\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcore\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmultiarray\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mravel_multi_index\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0munravel_index\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 15\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0mnumpy\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcore\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0moverrides\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mset_module\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
|
| 39 |
+
"\u001b[0;32m/usr/lib/python3.8/importlib/_bootstrap.py\u001b[0m in \u001b[0;36m_find_and_load\u001b[0;34m(name, import_)\u001b[0m\n",
|
| 40 |
+
"\u001b[0;32m/usr/lib/python3.8/importlib/_bootstrap.py\u001b[0m in \u001b[0;36m_find_and_load_unlocked\u001b[0;34m(name, import_)\u001b[0m\n",
|
| 41 |
+
"\u001b[0;32m/usr/lib/python3.8/importlib/_bootstrap.py\u001b[0m in \u001b[0;36m_load_unlocked\u001b[0;34m(spec)\u001b[0m\n",
|
| 42 |
+
"\u001b[0;32m/usr/lib/python3.8/importlib/_bootstrap_external.py\u001b[0m in \u001b[0;36mexec_module\u001b[0;34m(self, module)\u001b[0m\n",
|
| 43 |
+
"\u001b[0;32m/usr/lib/python3.8/importlib/_bootstrap_external.py\u001b[0m in \u001b[0;36mget_code\u001b[0;34m(self, fullname)\u001b[0m\n",
|
| 44 |
+
"\u001b[0;32m/usr/lib/python3.8/importlib/_bootstrap_external.py\u001b[0m in \u001b[0;36mget_data\u001b[0;34m(self, path)\u001b[0m\n",
|
| 45 |
+
"\u001b[0;31mKeyboardInterrupt\u001b[0m: "
|
| 46 |
+
]
|
| 47 |
+
}
|
| 48 |
+
],
|
| 49 |
+
"source": [
|
| 50 |
+
"#@title # [minDALL-E](https://github.com/kakaobrain/minDALL-E) Inferencing\n",
|
| 51 |
+
"#modified version of mega b notebook https://colab.research.google.com/drive/1Gg7-c7LrUTNfQ-Fk-BVNCe9kvedZZsAh?usp=sharing\n",
|
| 52 |
+
"\n",
|
| 53 |
+
"import os\n",
|
| 54 |
+
"from IPython.display import clear_output \n",
|
| 55 |
+
"\n",
|
| 56 |
+
"!git clone -q https://github.com/kakaobrain/minDALL-E.git\n",
|
| 57 |
+
"\n",
|
| 58 |
+
"%cd minDALL-E/\n",
|
| 59 |
+
"\n",
|
| 60 |
+
"!pip install -q tokenizers>=0.10.2\n",
|
| 61 |
+
"!pip install -q pyflakes>=2.2.0\n",
|
| 62 |
+
"!pip install -q tqdm>=4.46.0\n",
|
| 63 |
+
"!pip install -q pytorch-lightning>=1.5\n",
|
| 64 |
+
"!pip install -q einops\n",
|
| 65 |
+
"!pip install -q omegaconf\n",
|
| 66 |
+
"!pip install -q git+https://github.com/openai/CLIP.git\n",
|
| 67 |
+
"\n",
|
| 68 |
+
"clear_output()\n",
|
| 69 |
+
"print(\"Downloading model...\")\n",
|
| 70 |
+
"device = 'cuda:0'\n",
|
| 71 |
+
"\n",
|
| 72 |
+
"from dalle.utils.utils import set_seed\n",
|
| 73 |
+
"from dalle.models import Dalle\n",
|
| 74 |
+
"import clip\n",
|
| 75 |
+
"import math\n",
|
| 76 |
+
"\n",
|
| 77 |
+
"%matplotlib inline\n",
|
| 78 |
+
"\n",
|
| 79 |
+
"model = Dalle.from_pretrained('minDALL-E/1.3B') # This will automatically download the pretrained model.\n",
|
| 80 |
+
"model.to(device=device)\n",
|
| 81 |
+
"model_clip, preprocess_clip = clip.load(\"ViT-B/32\", device=device)\n",
|
| 82 |
+
"model_clip.to(device=device)\n",
|
| 83 |
+
"clear_output()"
|
| 84 |
+
]
|
| 85 |
+
},
|
| 86 |
+
{
|
| 87 |
+
"cell_type": "code",
|
| 88 |
+
"execution_count": null,
|
| 89 |
+
"metadata": {
|
| 90 |
+
"execution": {
|
| 91 |
+
"iopub.execute_input": "2021-12-22T03:40:27.709632Z",
|
| 92 |
+
"iopub.status.busy": "2021-12-22T03:40:27.709269Z",
|
| 93 |
+
"iopub.status.idle": "2021-12-22T03:40:27.800815Z",
|
| 94 |
+
"shell.execute_reply": "2021-12-22T03:40:27.799634Z",
|
| 95 |
+
"shell.execute_reply.started": "2021-12-22T03:40:27.709509Z"
|
| 96 |
+
}
|
| 97 |
+
},
|
| 98 |
+
"outputs": [],
|
| 99 |
+
"source": [
|
| 100 |
+
"from matplotlib import pyplot as plt\n",
|
| 101 |
+
"import clip\n",
|
| 102 |
+
"import torch\n",
|
| 103 |
+
"import numpy as np\n",
|
| 104 |
+
"from dalle.models import Dalle\n",
|
| 105 |
+
"from dalle.utils.utils import clip_score\n",
|
| 106 |
+
"\n",
|
| 107 |
+
"\n",
|
| 108 |
+
"prompt = \"A large bedroom with three armchairs next to a painting of the sun\"\n",
|
| 109 |
+
"print(prompt)\n",
|
| 110 |
+
"\n",
|
| 111 |
+
"num_candidates = 256\n",
|
| 112 |
+
"images = []\n",
|
| 113 |
+
"\n",
|
| 114 |
+
"\n",
|
| 115 |
+
"torch.cuda.empty_cache()\n",
|
| 116 |
+
"\n",
|
| 117 |
+
"for i in range(int(num_candidates / 32)):\n",
|
| 118 |
+
" with torch.no_grad():\n",
|
| 119 |
+
" images.append(model.sampling(prompt=prompt,\n",
|
| 120 |
+
" top_k=128,\n",
|
| 121 |
+
" top_p=None,\n",
|
| 122 |
+
" softmax_temperature=0.7,\n",
|
| 123 |
+
" num_candidates=32,\n",
|
| 124 |
+
" device=device).cpu().numpy())\n",
|
| 125 |
+
" \n",
|
| 126 |
+
" torch.cuda.empty_cache()\n",
|
| 127 |
+
"\n",
|
| 128 |
+
"images = np.concatenate(images)\n",
|
| 129 |
+
"images = np.transpose(images, (0, 2, 3, 1))\n",
|
| 130 |
+
"\n",
|
| 131 |
+
"with torch.no_grad():\n",
|
| 132 |
+
" rank = clip_score(prompt=prompt,\n",
|
| 133 |
+
" images=images,\n",
|
| 134 |
+
" model_clip=model_clip,\n",
|
| 135 |
+
" preprocess_clip=preprocess_clip,\n",
|
| 136 |
+
" device=device)\n",
|
| 137 |
+
"\n",
|
| 138 |
+
"torch.cuda.empty_cache()\n",
|
| 139 |
+
"\n",
|
| 140 |
+
"images = images[rank]\n",
|
| 141 |
+
"\n",
|
| 142 |
+
"n = num_candidates\n",
|
| 143 |
+
"\n",
|
| 144 |
+
"fig = plt.figure(figsize=(6*int(math.sqrt(n)), 6*int(math.sqrt(n))))\n",
|
| 145 |
+
"for i in range(n):\n",
|
| 146 |
+
" ax = fig.add_subplot(int(math.sqrt(n)), int(math.sqrt(n)), i+1)\n",
|
| 147 |
+
" ax.imshow(images[i])\n",
|
| 148 |
+
" ax.set_axis_off()\n",
|
| 149 |
+
"\n",
|
| 150 |
+
"plt.tight_layout()\n",
|
| 151 |
+
"plt.show() "
|
| 152 |
+
]
|
| 153 |
+
}
|
| 154 |
+
],
|
| 155 |
+
"metadata": {
|
| 156 |
+
"kernelspec": {
|
| 157 |
+
"display_name": "Python 3 (ipykernel)",
|
| 158 |
+
"language": "python",
|
| 159 |
+
"name": "python3"
|
| 160 |
+
},
|
| 161 |
+
"language_info": {
|
| 162 |
+
"codemirror_mode": {
|
| 163 |
+
"name": "ipython",
|
| 164 |
+
"version": 3
|
| 165 |
+
},
|
| 166 |
+
"file_extension": ".py",
|
| 167 |
+
"mimetype": "text/x-python",
|
| 168 |
+
"name": "python",
|
| 169 |
+
"nbconvert_exporter": "python",
|
| 170 |
+
"pygments_lexer": "ipython3",
|
| 171 |
+
"version": "3.8.10"
|
| 172 |
+
}
|
| 173 |
+
},
|
| 174 |
+
"nbformat": 4,
|
| 175 |
+
"nbformat_minor": 4
|
| 176 |
+
}
|