|
|
@ -257,6 +257,8 @@ |
|
|
|
"\n", |
|
|
|
"\n", |
|
|
|
" Remove Super Resolution\n", |
|
|
|
" Remove Super Resolution\n", |
|
|
|
"\n", |
|
|
|
"\n", |
|
|
|
|
|
|
|
" Remove SLIP Models\n", |
|
|
|
|
|
|
|
"\n", |
|
|
|
" '''\n", |
|
|
|
" '''\n", |
|
|
|
" )" |
|
|
|
" )" |
|
|
|
], |
|
|
|
], |
|
|
@ -366,9 +368,7 @@ |
|
|
|
}, |
|
|
|
}, |
|
|
|
"source": [ |
|
|
|
"source": [ |
|
|
|
"#@title 1.2 Prepare Folders\n", |
|
|
|
"#@title 1.2 Prepare Folders\n", |
|
|
|
"import subprocess\n", |
|
|
|
"import subprocess, os, sys, ipykernel\n", |
|
|
|
"import sys\n", |
|
|
|
|
|
|
|
"import ipykernel\n", |
|
|
|
|
|
|
|
"\n", |
|
|
|
"\n", |
|
|
|
"def gitclone(url):\n", |
|
|
|
"def gitclone(url):\n", |
|
|
|
" res = subprocess.run(['git', 'clone', url], stdout=subprocess.PIPE).stdout.decode('utf-8')\n", |
|
|
|
" res = subprocess.run(['git', 'clone', url], stdout=subprocess.PIPE).stdout.decode('utf-8')\n", |
|
|
@ -407,7 +407,7 @@ |
|
|
|
" else:\n", |
|
|
|
" else:\n", |
|
|
|
" root_path = '/content'\n", |
|
|
|
" root_path = '/content'\n", |
|
|
|
"else:\n", |
|
|
|
"else:\n", |
|
|
|
" root_path = '.'\n", |
|
|
|
" root_path = os.getcwd()\n", |
|
|
|
"\n", |
|
|
|
"\n", |
|
|
|
"import os\n", |
|
|
|
"import os\n", |
|
|
|
"def createPath(filepath):\n", |
|
|
|
"def createPath(filepath):\n", |
|
|
@ -420,13 +420,13 @@ |
|
|
|
"\n", |
|
|
|
"\n", |
|
|
|
"if is_colab:\n", |
|
|
|
"if is_colab:\n", |
|
|
|
" if google_drive and not save_models_to_google_drive or not google_drive:\n", |
|
|
|
" if google_drive and not save_models_to_google_drive or not google_drive:\n", |
|
|
|
" model_path = '/content/model'\n", |
|
|
|
" model_path = '/content/models'\n", |
|
|
|
" createPath(model_path)\n", |
|
|
|
" createPath(model_path)\n", |
|
|
|
" if google_drive and save_models_to_google_drive:\n", |
|
|
|
" if google_drive and save_models_to_google_drive:\n", |
|
|
|
" model_path = f'{root_path}/model'\n", |
|
|
|
" model_path = f'{root_path}/models'\n", |
|
|
|
" createPath(model_path)\n", |
|
|
|
" createPath(model_path)\n", |
|
|
|
"else:\n", |
|
|
|
"else:\n", |
|
|
|
" model_path = f'{root_path}/model'\n", |
|
|
|
" model_path = f'{root_path}/models'\n", |
|
|
|
" createPath(model_path)\n", |
|
|
|
" createPath(model_path)\n", |
|
|
|
"\n", |
|
|
|
"\n", |
|
|
|
"# libraries = f'{root_path}/libraries'\n", |
|
|
|
"# libraries = f'{root_path}/libraries'\n", |
|
|
@ -444,7 +444,7 @@ |
|
|
|
"source": [ |
|
|
|
"source": [ |
|
|
|
"#@title ### 1.3 Install and import dependencies\n", |
|
|
|
"#@title ### 1.3 Install and import dependencies\n", |
|
|
|
"\n", |
|
|
|
"\n", |
|
|
|
"import pathlib, shutil\n", |
|
|
|
"import pathlib, shutil, os, sys\n", |
|
|
|
"\n", |
|
|
|
"\n", |
|
|
|
"if not is_colab:\n", |
|
|
|
"if not is_colab:\n", |
|
|
|
" # If running locally, there's a good chance your env will need this in order to not crash upon np.matmul() or similar operations.\n", |
|
|
|
" # If running locally, there's a good chance your env will need this in order to not crash upon np.matmul() or similar operations.\n", |
|
|
@ -458,48 +458,70 @@ |
|
|
|
" root_path = f'/content'\n", |
|
|
|
" root_path = f'/content'\n", |
|
|
|
" model_path = '/content/models' \n", |
|
|
|
" model_path = '/content/models' \n", |
|
|
|
"else:\n", |
|
|
|
"else:\n", |
|
|
|
" root_path = f'.'\n", |
|
|
|
" root_path = os.getcwd()\n", |
|
|
|
" model_path = f'{root_path}/model'\n", |
|
|
|
" model_path = f'{root_path}/models'\n", |
|
|
|
"\n", |
|
|
|
"\n", |
|
|
|
"model_256_downloaded = False\n", |
|
|
|
"model_256_downloaded = False\n", |
|
|
|
"model_512_downloaded = False\n", |
|
|
|
"model_512_downloaded = False\n", |
|
|
|
"model_secondary_downloaded = False\n", |
|
|
|
"model_secondary_downloaded = False\n", |
|
|
|
"\n", |
|
|
|
"\n", |
|
|
|
|
|
|
|
"multipip_res = subprocess.run(['pip', 'install', 'lpips', 'datetime', 'timm', 'ftfy', 'einops', 'pytorch-lightning', 'omegaconf'], stdout=subprocess.PIPE).stdout.decode('utf-8')\n", |
|
|
|
|
|
|
|
"print(multipip_res)\n", |
|
|
|
|
|
|
|
"\n", |
|
|
|
"if is_colab:\n", |
|
|
|
"if is_colab:\n", |
|
|
|
" gitclone(\"https://github.com/openai/CLIP\")\n", |
|
|
|
|
|
|
|
" #gitclone(\"https://github.com/facebookresearch/SLIP.git\")\n", |
|
|
|
|
|
|
|
" gitclone(\"https://github.com/crowsonkb/guided-diffusion\")\n", |
|
|
|
|
|
|
|
" gitclone(\"https://github.com/assafshocher/ResizeRight.git\")\n", |
|
|
|
|
|
|
|
" gitclone(\"https://github.com/MSFTserver/pytorch3d-lite.git\")\n", |
|
|
|
|
|
|
|
" pipie(\"./CLIP\")\n", |
|
|
|
|
|
|
|
" pipie(\"./guided-diffusion\")\n", |
|
|
|
|
|
|
|
" multipip_res = subprocess.run(['pip', 'install', 'lpips', 'datetime', 'timm', 'ftfy'], stdout=subprocess.PIPE).stdout.decode('utf-8')\n", |
|
|
|
|
|
|
|
" print(multipip_res)\n", |
|
|
|
|
|
|
|
" subprocess.run(['apt', 'install', 'imagemagick'], stdout=subprocess.PIPE).stdout.decode('utf-8')\n", |
|
|
|
" subprocess.run(['apt', 'install', 'imagemagick'], stdout=subprocess.PIPE).stdout.decode('utf-8')\n", |
|
|
|
" gitclone(\"https://github.com/isl-org/MiDaS.git\")\n", |
|
|
|
|
|
|
|
" gitclone(\"https://github.com/alembics/disco-diffusion.git\")\n", |
|
|
|
|
|
|
|
" pipi(\"pytorch-lightning\")\n", |
|
|
|
|
|
|
|
" pipi(\"omegaconf\")\n", |
|
|
|
|
|
|
|
" pipi(\"einops\")\n", |
|
|
|
|
|
|
|
" # Rename a file to avoid a name conflict..\n", |
|
|
|
|
|
|
|
" try:\n", |
|
|
|
|
|
|
|
" os.rename(\"MiDaS/utils.py\", \"MiDaS/midas_utils.py\")\n", |
|
|
|
|
|
|
|
" shutil.copyfile(\"disco-diffusion/disco_xform_utils.py\", \"disco_xform_utils.py\")\n", |
|
|
|
|
|
|
|
" except:\n", |
|
|
|
|
|
|
|
" pass\n", |
|
|
|
|
|
|
|
"\n", |
|
|
|
"\n", |
|
|
|
"if not os.path.exists(f'{model_path}'):\n", |
|
|
|
"try:\n", |
|
|
|
" pathlib.Path(model_path).mkdir(parents=True, exist_ok=True)\n", |
|
|
|
" import clip\n", |
|
|
|
"if not os.path.exists(f'{model_path}/dpt_large-midas-2f21e586.pt'):\n", |
|
|
|
"except:\n", |
|
|
|
" wget(\"https://github.com/intel-isl/DPT/releases/download/1_0/dpt_large-midas-2f21e586.pt\", model_path)\n", |
|
|
|
" if os.path.exists(\"CLIP\") is not True:\n", |
|
|
|
|
|
|
|
" gitclone(\"https://github.com/openai/CLIP\")\n", |
|
|
|
|
|
|
|
" sys.path.append(f'{root_path}/CLIP')\n", |
|
|
|
"\n", |
|
|
|
"\n", |
|
|
|
"import sys\n", |
|
|
|
"try:\n", |
|
|
|
"import torch\n", |
|
|
|
" from guided_diffusion.script_util import create_model_and_diffusion\n", |
|
|
|
|
|
|
|
"except:\n", |
|
|
|
|
|
|
|
" if os.path.exists(\"guided-diffusion\") is not True:\n", |
|
|
|
|
|
|
|
" gitclone(\"https://github.com/crowsonkb/guided-diffusion\")\n", |
|
|
|
|
|
|
|
" sys.path.append(f'{PROJECT_DIR}/guided-diffusion')\n", |
|
|
|
|
|
|
|
"\n", |
|
|
|
|
|
|
|
"try:\n", |
|
|
|
|
|
|
|
" from resize_right import resize\n", |
|
|
|
|
|
|
|
"except:\n", |
|
|
|
|
|
|
|
" if os.path.exists(\"resize_right\") is not True:\n", |
|
|
|
|
|
|
|
" gitclone(\"https://github.com/assafshocher/ResizeRight.git\")\n", |
|
|
|
|
|
|
|
" sys.path.append(f'{PROJECT_DIR}/ResizeRight')\n", |
|
|
|
"\n", |
|
|
|
"\n", |
|
|
|
"# sys.path.append('./SLIP')\n", |
|
|
|
"try:\n", |
|
|
|
"sys.path.append('./pytorch3d-lite')\n", |
|
|
|
" import py3d_tools\n", |
|
|
|
"sys.path.append('./ResizeRight')\n", |
|
|
|
"except:\n", |
|
|
|
"sys.path.append('./MiDaS')\n", |
|
|
|
" if os.path.exists('pytorch3d-lite') is not True:\n", |
|
|
|
|
|
|
|
" gitclone(\"https://github.com/MSFTserver/pytorch3d-lite.git\")\n", |
|
|
|
|
|
|
|
" sys.path.append(f'{PROJECT_DIR}/pytorch3d-lite')\n", |
|
|
|
|
|
|
|
"\n", |
|
|
|
|
|
|
|
"try:\n", |
|
|
|
|
|
|
|
" from midas.dpt_depth import DPTDepthModel\n", |
|
|
|
|
|
|
|
"except:\n", |
|
|
|
|
|
|
|
" if os.path.exists('MiDaS') is not True:\n", |
|
|
|
|
|
|
|
" gitclone(\"https://github.com/isl-org/MiDaS.git\")\n", |
|
|
|
|
|
|
|
" if os.path.exists('MiDaS/midas_utils.py') is not True:\n", |
|
|
|
|
|
|
|
" shutil.move('MiDaS/utils.py', 'MiDaS/midas_utils.py')\n", |
|
|
|
|
|
|
|
" if not os.path.exists(f'{model_path}/dpt_large-midas-2f21e586.pt'):\n", |
|
|
|
|
|
|
|
" wget(\"https://github.com/intel-isl/DPT/releases/download/1_0/dpt_large-midas-2f21e586.pt\", model_path)\n", |
|
|
|
|
|
|
|
" sys.path.append(f'{PROJECT_DIR}/MiDaS')\n", |
|
|
|
|
|
|
|
"\n", |
|
|
|
|
|
|
|
"try:\n", |
|
|
|
|
|
|
|
" sys.path.append(PROJECT_DIR)\n", |
|
|
|
|
|
|
|
" import disco_xform_utils as dxf\n", |
|
|
|
|
|
|
|
"except:\n", |
|
|
|
|
|
|
|
" if os.path.exists(\"disco-diffusion\") is not True:\n", |
|
|
|
|
|
|
|
" gitclone(\"https://github.com/alembics/disco-diffusion.git\")\n", |
|
|
|
|
|
|
|
" # Rename a file to avoid a name conflict..\n", |
|
|
|
|
|
|
|
" if os.path.exists('disco_xform_utils.py') is not True:\n", |
|
|
|
|
|
|
|
" shutil.move('disco-diffusion/disco_xform_utils.py', 'disco_xform_utils.py')\n", |
|
|
|
|
|
|
|
" sys.path.append(PROJECT_DIR)\n", |
|
|
|
|
|
|
|
"\n", |
|
|
|
|
|
|
|
"import torch\n", |
|
|
|
"from dataclasses import dataclass\n", |
|
|
|
"from dataclasses import dataclass\n", |
|
|
|
"from functools import partial\n", |
|
|
|
"from functools import partial\n", |
|
|
|
"import cv2\n", |
|
|
|
"import cv2\n", |
|
|
@ -520,11 +542,8 @@ |
|
|
|
"import torchvision.transforms as T\n", |
|
|
|
"import torchvision.transforms as T\n", |
|
|
|
"import torchvision.transforms.functional as TF\n", |
|
|
|
"import torchvision.transforms.functional as TF\n", |
|
|
|
"from tqdm.notebook import tqdm\n", |
|
|
|
"from tqdm.notebook import tqdm\n", |
|
|
|
"sys.path.append('./CLIP')\n", |
|
|
|
|
|
|
|
"sys.path.append('./guided-diffusion')\n", |
|
|
|
|
|
|
|
"import clip\n", |
|
|
|
"import clip\n", |
|
|
|
"from resize_right import resize\n", |
|
|
|
"from resize_right import resize\n", |
|
|
|
"# from models import SLIP_VITB16, SLIP, SLIP_VITL16\n", |
|
|
|
|
|
|
|
"from guided_diffusion.script_util import create_model_and_diffusion, model_and_diffusion_defaults\n", |
|
|
|
"from guided_diffusion.script_util import create_model_and_diffusion, model_and_diffusion_defaults\n", |
|
|
|
"from datetime import datetime\n", |
|
|
|
"from datetime import datetime\n", |
|
|
|
"import numpy as np\n", |
|
|
|
"import numpy as np\n", |
|
|
@ -549,13 +568,15 @@ |
|
|
|
"\n", |
|
|
|
"\n", |
|
|
|
"# AdaBins stuff\n", |
|
|
|
"# AdaBins stuff\n", |
|
|
|
"if USE_ADABINS:\n", |
|
|
|
"if USE_ADABINS:\n", |
|
|
|
" if is_colab:\n", |
|
|
|
" try:\n", |
|
|
|
" gitclone(\"https://github.com/shariqfarooq123/AdaBins.git\")\n", |
|
|
|
" from infer import InferenceHelper\n", |
|
|
|
" if not os.path.exists(f'{model_path}/AdaBins_nyu.pt'):\n", |
|
|
|
" except:\n", |
|
|
|
" wget(\"https://cloudflare-ipfs.com/ipfs/Qmd2mMnDLWePKmgfS8m6ntAg4nhV5VkUyAydYBp8cWWeB7/AdaBins_nyu.pt\", model_path)\n", |
|
|
|
" if os.path.exists(\"AdaBins\") is not True:\n", |
|
|
|
" pathlib.Path(\"pretrained\").mkdir(parents=True, exist_ok=True)\n", |
|
|
|
" gitclone(\"https://github.com/shariqfarooq123/AdaBins.git\")\n", |
|
|
|
" shutil.copyfile(f\"{model_path}/AdaBins_nyu.pt\", \"pretrained/AdaBins_nyu.pt\")\n", |
|
|
|
" if not path_exists(f'{model_path}/pretrained/AdaBins_nyu.pt'):\n", |
|
|
|
" sys.path.append('./AdaBins')\n", |
|
|
|
" os.makedirs(f'{model_path}/pretrained')\n", |
|
|
|
|
|
|
|
" wget(\"https://cloudflare-ipfs.com/ipfs/Qmd2mMnDLWePKmgfS8m6ntAg4nhV5VkUyAydYBp8cWWeB7/AdaBins_nyu.pt\", f'{model_path}/pretrained')\n", |
|
|
|
|
|
|
|
" sys.path.append(f'{os.getcwd()}/AdaBins')\n", |
|
|
|
" from infer import InferenceHelper\n", |
|
|
|
" from infer import InferenceHelper\n", |
|
|
|
" MAX_ADABINS_AREA = 500000\n", |
|
|
|
" MAX_ADABINS_AREA = 500000\n", |
|
|
|
"\n", |
|
|
|
"\n", |
|
|
@ -1684,8 +1705,6 @@ |
|
|
|
"RN50x4 = False #@param{type:\"boolean\"}\n", |
|
|
|
"RN50x4 = False #@param{type:\"boolean\"}\n", |
|
|
|
"RN50x16 = False #@param{type:\"boolean\"}\n", |
|
|
|
"RN50x16 = False #@param{type:\"boolean\"}\n", |
|
|
|
"RN50x64 = False #@param{type:\"boolean\"}\n", |
|
|
|
"RN50x64 = False #@param{type:\"boolean\"}\n", |
|
|
|
"SLIPB16 = False #@param{type:\"boolean\"}\n", |
|
|
|
|
|
|
|
"SLIPL16 = False #@param{type:\"boolean\"}\n", |
|
|
|
|
|
|
|
"\n", |
|
|
|
"\n", |
|
|
|
"#@markdown If you're having issues with model downloads, check this to compare SHA's:\n", |
|
|
|
"#@markdown If you're having issues with model downloads, check this to compare SHA's:\n", |
|
|
|
"check_model_SHA = False #@param{type:\"boolean\"}\n", |
|
|
|
"check_model_SHA = False #@param{type:\"boolean\"}\n", |
|
|
@ -1819,20 +1838,6 @@ |
|
|
|
"if RN50x64 is True: clip_models.append(clip.load('RN50x64', jit=False)[0].eval().requires_grad_(False).to(device)) \n", |
|
|
|
"if RN50x64 is True: clip_models.append(clip.load('RN50x64', jit=False)[0].eval().requires_grad_(False).to(device)) \n", |
|
|
|
"if RN101 is True: clip_models.append(clip.load('RN101', jit=False)[0].eval().requires_grad_(False).to(device)) \n", |
|
|
|
"if RN101 is True: clip_models.append(clip.load('RN101', jit=False)[0].eval().requires_grad_(False).to(device)) \n", |
|
|
|
"\n", |
|
|
|
"\n", |
|
|
|
"if SLIPB16:\n", |
|
|
|
|
|
|
|
" SLIPB16model = SLIP_VITB16(ssl_mlp_dim=4096, ssl_emb_dim=256)\n", |
|
|
|
|
|
|
|
" if not os.path.exists(f'{model_path}/slip_base_100ep.pt'):\n", |
|
|
|
|
|
|
|
" wget(\"https://dl.fbaipublicfiles.com/slip/slip_base_100ep.pt\", model_path)\n", |
|
|
|
|
|
|
|
" sd = torch.load(f'{model_path}/slip_base_100ep.pt')\n", |
|
|
|
|
|
|
|
" real_sd = {}\n", |
|
|
|
|
|
|
|
" for k, v in sd['state_dict'].items():\n", |
|
|
|
|
|
|
|
" real_sd['.'.join(k.split('.')[1:])] = v\n", |
|
|
|
|
|
|
|
" del sd\n", |
|
|
|
|
|
|
|
" SLIPB16model.load_state_dict(real_sd)\n", |
|
|
|
|
|
|
|
" SLIPB16model.requires_grad_(False).eval().to(device)\n", |
|
|
|
|
|
|
|
"\n", |
|
|
|
|
|
|
|
" clip_models.append(SLIPL16model)\n", |
|
|
|
|
|
|
|
"\n", |
|
|
|
|
|
|
|
"normalize = T.Normalize(mean=[0.48145466, 0.4578275, 0.40821073], std=[0.26862954, 0.26130258, 0.27577711])\n", |
|
|
|
"normalize = T.Normalize(mean=[0.48145466, 0.4578275, 0.40821073], std=[0.26862954, 0.26130258, 0.27577711])\n", |
|
|
|
"lpips_model = lpips.LPIPS(net='vgg').to(device)" |
|
|
|
"lpips_model = lpips.LPIPS(net='vgg').to(device)" |
|
|
|
], |
|
|
|
], |
|
|
|