From bcc6f174a6379419ae64245c54f6e296743dfaa0 Mon Sep 17 00:00:00 2001 From: MSFTserver Date: Sun, 3 Apr 2022 21:56:41 -0700 Subject: [PATCH] update install to be crossplatform --- Disco_Diffusion.ipynb | 137 ++++++++++++++++++++++-------------------- README.md | 3 +- disco.py | 116 ++++++++++++++++++++--------------- 3 files changed, 142 insertions(+), 114 deletions(-) diff --git a/Disco_Diffusion.ipynb b/Disco_Diffusion.ipynb index 97fea4a..01e6e25 100644 --- a/Disco_Diffusion.ipynb +++ b/Disco_Diffusion.ipynb @@ -257,6 +257,8 @@ "\n", " Remove Super Resolution\n", "\n", + " Remove SLIP Models\n", + "\n", " '''\n", " )" ], @@ -366,9 +368,7 @@ }, "source": [ "#@title 1.2 Prepare Folders\n", - "import subprocess\n", - "import sys\n", - "import ipykernel\n", + "import subprocess, os, sys, ipykernel\n", "\n", "def gitclone(url):\n", " res = subprocess.run(['git', 'clone', url], stdout=subprocess.PIPE).stdout.decode('utf-8')\n", @@ -407,7 +407,7 @@ " else:\n", " root_path = '/content'\n", "else:\n", - " root_path = '.'\n", + " root_path = os.getcwd()\n", "\n", "import os\n", "def createPath(filepath):\n", @@ -420,13 +420,13 @@ "\n", "if is_colab:\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", " 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", "else:\n", - " model_path = f'{root_path}/model'\n", + " model_path = f'{root_path}/models'\n", " createPath(model_path)\n", "\n", "# libraries = f'{root_path}/libraries'\n", @@ -444,7 +444,7 @@ "source": [ "#@title ### 1.3 Install and import dependencies\n", "\n", - "import pathlib, shutil\n", + "import pathlib, shutil, os, sys\n", "\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", @@ -458,48 +458,70 @@ " root_path = f'/content'\n", " model_path = '/content/models' \n", "else:\n", - " root_path = f'.'\n", - " model_path = f'{root_path}/model'\n", + " root_path = os.getcwd()\n", + " model_path = f'{root_path}/models'\n", "\n", "model_256_downloaded = False\n", "model_512_downloaded = False\n", "model_secondary_downloaded = False\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", - " 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", - " 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", - "if not os.path.exists(f'{model_path}'):\n", - " pathlib.Path(model_path).mkdir(parents=True, exist_ok=True)\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", + "try:\n", + " import clip\n", + "except:\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", - "import sys\n", - "import torch\n", + "try:\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", - "# sys.path.append('./SLIP')\n", - "sys.path.append('./pytorch3d-lite')\n", - "sys.path.append('./ResizeRight')\n", - "sys.path.append('./MiDaS')\n", + "try:\n", + " import py3d_tools\n", + "except:\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 functools import partial\n", "import cv2\n", @@ -520,11 +542,8 @@ "import torchvision.transforms as T\n", "import torchvision.transforms.functional as TF\n", "from tqdm.notebook import tqdm\n", - "sys.path.append('./CLIP')\n", - "sys.path.append('./guided-diffusion')\n", "import clip\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 datetime import datetime\n", "import numpy as np\n", @@ -549,13 +568,15 @@ "\n", "# AdaBins stuff\n", "if USE_ADABINS:\n", - " if is_colab:\n", - " gitclone(\"https://github.com/shariqfarooq123/AdaBins.git\")\n", - " if not os.path.exists(f'{model_path}/AdaBins_nyu.pt'):\n", - " wget(\"https://cloudflare-ipfs.com/ipfs/Qmd2mMnDLWePKmgfS8m6ntAg4nhV5VkUyAydYBp8cWWeB7/AdaBins_nyu.pt\", model_path)\n", - " pathlib.Path(\"pretrained\").mkdir(parents=True, exist_ok=True)\n", - " shutil.copyfile(f\"{model_path}/AdaBins_nyu.pt\", \"pretrained/AdaBins_nyu.pt\")\n", - " sys.path.append('./AdaBins')\n", + " try:\n", + " from infer import InferenceHelper\n", + " except:\n", + " if os.path.exists(\"AdaBins\") is not True:\n", + " gitclone(\"https://github.com/shariqfarooq123/AdaBins.git\")\n", + " if not path_exists(f'{model_path}/pretrained/AdaBins_nyu.pt'):\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", " MAX_ADABINS_AREA = 500000\n", "\n", @@ -1684,8 +1705,6 @@ "RN50x4 = False #@param{type:\"boolean\"}\n", "RN50x16 = False #@param{type:\"boolean\"}\n", "RN50x64 = False #@param{type:\"boolean\"}\n", - "SLIPB16 = False #@param{type:\"boolean\"}\n", - "SLIPL16 = False #@param{type:\"boolean\"}\n", "\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", @@ -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 RN101 is True: clip_models.append(clip.load('RN101', jit=False)[0].eval().requires_grad_(False).to(device)) \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", "lpips_model = lpips.LPIPS(net='vgg').to(device)" ], diff --git a/README.md b/README.md index a35472b..ca0ee58 100644 --- a/README.md +++ b/README.md @@ -54,7 +54,8 @@ A frankensteinian amalgamation of notebooks, models and techniques for the gener * Added video_init_seed_continuity option to make init video animations more continuous * Removed pytorch3d from needing to be compiled with a lite version specifically made for Disco Diffusion * Remove Super Resolution - +* Remove Slip Models +* Update for crossplatform support ## Notebook Provenance diff --git a/disco.py b/disco.py index 3e4a56a..c7d5b42 100644 --- a/disco.py +++ b/disco.py @@ -345,9 +345,7 @@ else: # !! "id": "PrepFolders" # !! }} #@title 1.2 Prepare Folders -import subprocess -import sys -import ipykernel +import subprocess, os, sys, ipykernel def gitclone(url): res = subprocess.run(['git', 'clone', url], stdout=subprocess.PIPE).stdout.decode('utf-8') @@ -386,7 +384,7 @@ if is_colab: else: root_path = '/content' else: - root_path = '.' + root_path = os.getcwd() import os def createPath(filepath): @@ -399,13 +397,13 @@ createPath(outDirPath) if is_colab: if google_drive and not save_models_to_google_drive or not google_drive: - model_path = '/content/model' + model_path = '/content/models' createPath(model_path) if google_drive and save_models_to_google_drive: - model_path = f'{root_path}/model' + model_path = f'{root_path}/models' createPath(model_path) else: - model_path = f'{root_path}/model' + model_path = f'{root_path}/models' createPath(model_path) # libraries = f'{root_path}/libraries' @@ -418,7 +416,7 @@ else: # !! }} #@title ### 1.3 Install and import dependencies -import pathlib, shutil +import pathlib, shutil, os, sys if not is_colab: # If running locally, there's a good chance your env will need this in order to not crash upon np.matmul() or similar operations. @@ -432,46 +430,70 @@ if is_colab: root_path = f'/content' model_path = '/content/models' else: - root_path = f'.' - model_path = f'{root_path}/model' + root_path = os.getcwd() + model_path = f'{root_path}/models' model_256_downloaded = False model_512_downloaded = False model_secondary_downloaded = False +multipip_res = subprocess.run(['pip', 'install', 'lpips', 'datetime', 'timm', 'ftfy', 'einops', 'pytorch-lightning', 'omegaconf'], stdout=subprocess.PIPE).stdout.decode('utf-8') +print(multipip_res) + if is_colab: - gitclone("https://github.com/openai/CLIP") - gitclone("https://github.com/crowsonkb/guided-diffusion") - gitclone("https://github.com/assafshocher/ResizeRight.git") - gitclone("https://github.com/MSFTserver/pytorch3d-lite.git") - pipie("./CLIP") - pipie("./guided-diffusion") - multipip_res = subprocess.run(['pip', 'install', 'lpips', 'datetime', 'timm', 'ftfy'], stdout=subprocess.PIPE).stdout.decode('utf-8') - print(multipip_res) subprocess.run(['apt', 'install', 'imagemagick'], stdout=subprocess.PIPE).stdout.decode('utf-8') - gitclone("https://github.com/isl-org/MiDaS.git") - gitclone("https://github.com/alembics/disco-diffusion.git") - pipi("pytorch-lightning") - pipi("omegaconf") - pipi("einops") - # Rename a file to avoid a name conflict.. - try: - os.rename("MiDaS/utils.py", "MiDaS/midas_utils.py") - shutil.copyfile("disco-diffusion/disco_xform_utils.py", "disco_xform_utils.py") - except: - pass -if not os.path.exists(f'{model_path}'): - pathlib.Path(model_path).mkdir(parents=True, exist_ok=True) -if not os.path.exists(f'{model_path}/dpt_large-midas-2f21e586.pt'): - wget("https://github.com/intel-isl/DPT/releases/download/1_0/dpt_large-midas-2f21e586.pt", model_path) +try: + import clip +except: + if os.path.exists("CLIP") is not True: + gitclone("https://github.com/openai/CLIP") + sys.path.append(f'{root_path}/CLIP') + +try: + from guided_diffusion.script_util import create_model_and_diffusion +except: + if os.path.exists("guided-diffusion") is not True: + gitclone("https://github.com/crowsonkb/guided-diffusion") + sys.path.append(f'{PROJECT_DIR}/guided-diffusion') -import sys -import torch +try: + from resize_right import resize +except: + if os.path.exists("resize_right") is not True: + gitclone("https://github.com/assafshocher/ResizeRight.git") + sys.path.append(f'{PROJECT_DIR}/ResizeRight') -sys.path.append('./pytorch3d-lite') -sys.path.append('./ResizeRight') -sys.path.append('./MiDaS') +try: + import py3d_tools +except: + if os.path.exists('pytorch3d-lite') is not True: + gitclone("https://github.com/MSFTserver/pytorch3d-lite.git") + sys.path.append(f'{PROJECT_DIR}/pytorch3d-lite') + +try: + from midas.dpt_depth import DPTDepthModel +except: + if os.path.exists('MiDaS') is not True: + gitclone("https://github.com/isl-org/MiDaS.git") + if os.path.exists('MiDaS/midas_utils.py') is not True: + shutil.move('MiDaS/utils.py', 'MiDaS/midas_utils.py') + if not os.path.exists(f'{model_path}/dpt_large-midas-2f21e586.pt'): + wget("https://github.com/intel-isl/DPT/releases/download/1_0/dpt_large-midas-2f21e586.pt", model_path) + sys.path.append(f'{PROJECT_DIR}/MiDaS') + +try: + sys.path.append(PROJECT_DIR) + import disco_xform_utils as dxf +except: + if os.path.exists("disco-diffusion") is not True: + gitclone("https://github.com/alembics/disco-diffusion.git") + # Rename a file to avoid a name conflict.. + if os.path.exists('disco_xform_utils.py') is not True: + shutil.move('disco-diffusion/disco_xform_utils.py', 'disco_xform_utils.py') + sys.path.append(PROJECT_DIR) + +import torch from dataclasses import dataclass from functools import partial import cv2 @@ -492,8 +514,6 @@ from torch.nn import functional as F import torchvision.transforms as T import torchvision.transforms.functional as TF from tqdm.notebook import tqdm -sys.path.append('./CLIP') -sys.path.append('./guided-diffusion') import clip from resize_right import resize from guided_diffusion.script_util import create_model_and_diffusion, model_and_diffusion_defaults @@ -520,13 +540,15 @@ warnings.filterwarnings("ignore", category=UserWarning) # AdaBins stuff if USE_ADABINS: - if is_colab: - gitclone("https://github.com/shariqfarooq123/AdaBins.git") - if not os.path.exists(f'{model_path}/AdaBins_nyu.pt'): - wget("https://cloudflare-ipfs.com/ipfs/Qmd2mMnDLWePKmgfS8m6ntAg4nhV5VkUyAydYBp8cWWeB7/AdaBins_nyu.pt", model_path) - pathlib.Path("pretrained").mkdir(parents=True, exist_ok=True) - shutil.copyfile(f"{model_path}/AdaBins_nyu.pt", "pretrained/AdaBins_nyu.pt") - sys.path.append('./AdaBins') + try: + from infer import InferenceHelper + except: + if os.path.exists("AdaBins") is not True: + gitclone("https://github.com/shariqfarooq123/AdaBins.git") + if not path_exists(f'{model_path}/pretrained/AdaBins_nyu.pt'): + os.makedirs(f'{model_path}/pretrained') + wget("https://cloudflare-ipfs.com/ipfs/Qmd2mMnDLWePKmgfS8m6ntAg4nhV5VkUyAydYBp8cWWeB7/AdaBins_nyu.pt", f'{model_path}/pretrained') + sys.path.append(f'{os.getcwd()}/AdaBins') from infer import InferenceHelper MAX_ADABINS_AREA = 500000