2022-08-21 23:57:48 +00:00
|
|
|
#!/usr/bin/env python3
|
2022-08-24 13:22:04 +00:00
|
|
|
# Copyright (c) 2022 Lincoln D. Stein (https://github.com/lstein)
|
2022-08-17 16:00:00 +00:00
|
|
|
# Before running stable-diffusion on an internet-isolated machine,
|
|
|
|
# run this script from one with internet connectivity. The
|
|
|
|
# two machines must share a common .cache directory.
|
2022-08-28 20:14:29 +00:00
|
|
|
from transformers import CLIPTokenizer, CLIPTextModel
|
|
|
|
import clip
|
|
|
|
from transformers import BertTokenizerFast
|
2022-08-22 19:33:27 +00:00
|
|
|
import sys
|
|
|
|
import transformers
|
2022-08-26 05:20:01 +00:00
|
|
|
import os
|
|
|
|
import warnings
|
2022-09-17 05:32:31 +00:00
|
|
|
import urllib.request
|
2022-08-22 19:33:27 +00:00
|
|
|
|
|
|
|
transformers.logging.set_verbosity_error()
|
2022-08-17 16:00:00 +00:00
|
|
|
|
|
|
|
# this will preload the Bert tokenizer fles
|
2022-08-26 07:15:42 +00:00
|
|
|
print('preloading bert tokenizer...')
|
|
|
|
|
|
|
|
tokenizer = BertTokenizerFast.from_pretrained('bert-base-uncased')
|
|
|
|
print('...success')
|
2022-08-17 16:00:00 +00:00
|
|
|
|
|
|
|
# this will download requirements for Kornia
|
2022-08-26 07:15:42 +00:00
|
|
|
print('preloading Kornia requirements (ignore the deprecation warnings)...')
|
2022-08-26 05:20:01 +00:00
|
|
|
with warnings.catch_warnings():
|
2022-08-26 07:15:42 +00:00
|
|
|
warnings.filterwarnings('ignore', category=DeprecationWarning)
|
2022-08-26 05:20:01 +00:00
|
|
|
import kornia
|
2022-08-26 07:15:42 +00:00
|
|
|
print('...success')
|
2022-08-17 16:00:00 +00:00
|
|
|
|
2022-08-26 07:15:42 +00:00
|
|
|
version = 'openai/clip-vit-large-patch14'
|
2022-08-22 19:33:27 +00:00
|
|
|
|
2022-08-26 05:20:01 +00:00
|
|
|
print('preloading CLIP model (Ignore the deprecation warnings)...')
|
2022-08-22 19:33:27 +00:00
|
|
|
sys.stdout.flush()
|
2022-08-26 07:15:42 +00:00
|
|
|
|
|
|
|
tokenizer = CLIPTokenizer.from_pretrained(version)
|
|
|
|
transformer = CLIPTextModel.from_pretrained(version)
|
2022-08-22 19:33:27 +00:00
|
|
|
print('\n\n...success')
|
|
|
|
|
2022-08-26 05:20:01 +00:00
|
|
|
# In the event that the user has installed GFPGAN and also elected to use
|
|
|
|
# RealESRGAN, this will attempt to download the model needed by RealESRGANer
|
|
|
|
gfpgan = False
|
|
|
|
try:
|
|
|
|
from realesrgan import RealESRGANer
|
2022-08-26 07:15:42 +00:00
|
|
|
|
2022-08-26 05:20:01 +00:00
|
|
|
gfpgan = True
|
|
|
|
except ModuleNotFoundError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
if gfpgan:
|
2022-08-26 07:15:42 +00:00
|
|
|
print('Loading models from RealESRGAN and facexlib')
|
2022-08-26 05:20:01 +00:00
|
|
|
try:
|
|
|
|
from basicsr.archs.rrdbnet_arch import RRDBNet
|
|
|
|
from facexlib.utils.face_restoration_helper import FaceRestoreHelper
|
2022-08-26 07:15:42 +00:00
|
|
|
|
|
|
|
RealESRGANer(
|
|
|
|
scale=2,
|
|
|
|
model_path='https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.1/RealESRGAN_x2plus.pth',
|
|
|
|
model=RRDBNet(
|
|
|
|
num_in_ch=3,
|
|
|
|
num_out_ch=3,
|
|
|
|
num_feat=64,
|
|
|
|
num_block=23,
|
|
|
|
num_grow_ch=32,
|
|
|
|
scale=2,
|
|
|
|
),
|
|
|
|
)
|
2022-08-28 20:14:29 +00:00
|
|
|
|
|
|
|
RealESRGANer(
|
|
|
|
scale=4,
|
|
|
|
model_path='https://github.com/xinntao/Real-ESRGAN/releases/download/v0.1.0/RealESRGAN_x4plus.pth',
|
|
|
|
model=RRDBNet(
|
|
|
|
num_in_ch=3,
|
|
|
|
num_out_ch=3,
|
|
|
|
num_feat=64,
|
|
|
|
num_block=23,
|
|
|
|
num_grow_ch=32,
|
|
|
|
scale=4,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
2022-08-26 07:15:42 +00:00
|
|
|
FaceRestoreHelper(1, det_model='retinaface_resnet50')
|
|
|
|
print('...success')
|
2022-08-26 05:20:01 +00:00
|
|
|
except Exception:
|
|
|
|
import traceback
|
2022-09-17 05:32:31 +00:00
|
|
|
print('Error loading ESRGAN:')
|
|
|
|
print(traceback.format_exc())
|
2022-08-24 13:22:04 +00:00
|
|
|
|
2022-09-17 05:32:31 +00:00
|
|
|
try:
|
|
|
|
import urllib.request
|
2022-09-18 19:01:05 +00:00
|
|
|
model_url = 'https://github.com/TencentARC/GFPGAN/releases/download/v1.3.0/GFPGANv1.3.pth'
|
2022-09-17 05:32:31 +00:00
|
|
|
model_dest = 'src/gfpgan/experiments/pretrained_models/GFPGANv1.3.pth'
|
2022-09-18 19:01:05 +00:00
|
|
|
|
2022-09-18 18:42:43 +00:00
|
|
|
if not os.path.exists(model_dest):
|
|
|
|
print('downloading gfpgan model file...')
|
2022-09-20 00:04:44 +00:00
|
|
|
urllib.request.urlretrieve(model_url,model_dest)
|
2022-09-17 05:32:31 +00:00
|
|
|
except Exception:
|
|
|
|
import traceback
|
2022-08-26 07:15:42 +00:00
|
|
|
print('Error loading GFPGAN:')
|
|
|
|
print(traceback.format_exc())
|
2022-09-18 19:01:05 +00:00
|
|
|
print('...success')
|
|
|
|
|
|
|
|
print('preloading CodeFormer model file...')
|
|
|
|
try:
|
|
|
|
import urllib.request
|
|
|
|
model_url = 'https://github.com/sczhou/CodeFormer/releases/download/v0.1.0/codeformer.pth'
|
2022-09-19 17:59:43 +00:00
|
|
|
model_dest = 'ldm/dream/restoration/codeformer/weights/codeformer.pth'
|
2022-09-18 19:01:05 +00:00
|
|
|
if not os.path.exists(model_dest):
|
|
|
|
print('downloading codeformer model file...')
|
|
|
|
os.makedirs(os.path.dirname(model_dest), exist_ok=True)
|
2022-09-20 00:04:44 +00:00
|
|
|
urllib.request.urlretrieve(model_url,model_dest)
|
2022-09-18 19:01:05 +00:00
|
|
|
except Exception:
|
|
|
|
import traceback
|
|
|
|
print('Error loading CodeFormer:')
|
|
|
|
print(traceback.format_exc())
|
|
|
|
print('...success')
|