Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 56 additions & 9 deletions Real-CUGAN/colab-demo.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,19 @@
"PendingPath=ROOTPATH+\"Real-CUGAN/pending/\" # input dir\n",
"FinishPath=ROOTPATH+\"Real-CUGAN/finish/\" # output dir\n",
"ModelName=\"up2x-latest-no-denoise.pth\" # default model\n",
"Tile=4 #{0,1,2,3,4,auto}; the larger the number, the smaller the memory consumption\n",
"Tile=5 #{0,1,2,3,4,auto}; the larger the number, the smaller the memory consumption\n",
"Scale=2\n",
"NT=2 #线程数\n",
"CacheModel=0\n",
"RunModel=0 #运行模式 0=image 1=video\n",
"DeviceBase=\"cuda:0\"\n",
"Half=True\n",
"\n",
"\n",
"# initialize environment\n",
"!pip install torch opencv-python\n",
"!pip install imageio-ffmpeg\n",
"!pip install imageio==2.4.1\n",
"!git clone https://github.com/bilibili/ailab.git\n",
"from google.colab import drive\n",
"drive.mount('/content/gdrive')"
Expand Down Expand Up @@ -55,9 +64,13 @@
"outputs": [],
"source": [
"import os\n",
"os.mkdir(ModelPath)\n",
"os.mkdir(PendingPath)\n",
"os.mkdir(FinishPath)\n",
"if not os.path.exists(ModelPath):\n",
" os.mkdir(ModelPath)\n",
"if not os.path.exists(PendingPath):\n",
" os.mkdir(PendingPath)\n",
"if not os.path.exists(FinishPath):\n",
" os.mkdir(FinishPath)\n",
"\n",
"!cp -r /content/gdrive/MyDrive/updated_weights/* /content/ailab/Real-CUGAN/model/\n",
"fileNames = os.listdir(PendingPath)\n",
"print(\"Pending images:\")\n",
Expand Down Expand Up @@ -102,25 +115,59 @@
"import os,sys,cv2\n",
"import numpy as np\n",
"from upcunet_v3 import RealWaifuUpScaler\n",
"from inference_video import VideoRealWaifuUpScaler\n",
"from inference_video import UpScalerMT\n",
"from multiprocessing import Queue\n",
"from time import time as ttime \n",
"fileNames = os.listdir(PendingPath)\n",
"print(f\"using model {ModelPath+ModelName}\")\n",
"upscaler = RealWaifuUpScaler(2, ModelPath+ModelName, half=True, device=\"cuda:0\")\n",
"\n",
"upscaler = RealWaifuUpScaler(Scale, ModelPath+ModelName, half=Half, device=DeviceBase)\n",
"\n",
"# Overwrite VideoRealWaifuUpScaler\n",
"class VideoRealWaifuUpScaler_kai(VideoRealWaifuUpScaler):\n",
" def __init__(self):\n",
" self.nt = NT\n",
" self.n_gpu=1 # 每块GPU开nt个进程\n",
" self.scale=Scale\n",
" self.encode_params = ['-crf', '21', '-preset', 'medium']\n",
" self.p_sleep=(0.005,0.012)\n",
" self.decode_sleep=0.002\n",
" self.half=Half\n",
" self.tile=Tile\n",
" self.cache_mode=CacheModel\n",
" self.alpha=1\n",
" self.device=DeviceBase\n",
"\n",
" video_model = RealWaifuUpScaler(Scale, ModelPath+ModelName, Half, DeviceBase)\n",
" self.inp_q = Queue(NT * 2)\n",
" self.res_q = Queue(NT * 2)\n",
" for _ in range(NT):\n",
" upscaler = UpScalerMT(self.inp_q, self.res_q, self.device, video_model, self.p_sleep, self.nt, self.tile, self.cache_mode, self.alpha)\n",
" upscaler.start()\n",
"\n",
"video_upscaler=VideoRealWaifuUpScaler_kai()\n",
"\n",
"t0 = ttime()\n",
"for i in fileNames:\n",
" torch.cuda.empty_cache()\n",
" try:\n",
" img = cv2.imread(PendingPath+i)[:, :, [2, 1, 0]]\n",
" result = upscaler(img,tile_mode=5,cache_mode=2,alpha=1)\n",
" cv2.imwrite(FinishPath+i,result[:, :, ::-1])\n",
" if RunModel == 0:\n",
" img = cv2.imread(PendingPath+i)[:, :, [2, 1, 0]]\n",
" result = upscaler(img,tile_mode=5,cache_mode=2,alpha=1)\n",
" cv2.imwrite(FinishPath+i,result[:, :, ::-1])\n",
" elif RunModel == 1:\n",
" video_upscaler(PendingPath+i, FinishPath+i)\n",
" else:\n",
" print(\"Warning: Error RunModel Input\")\n",
" except RuntimeError as e:\n",
" print (i+\" FAILED\")\n",
" print (e)\n",
" else:\n",
" print(i+\" DONE\")\n",
" os.remove(PendingPath+i)\n",
"t1 = ttime()\n",
"print(\"time_spent\", t1 - t0)"
"print(\"time_spent\", t1 - t0)\n"
]
},
{
Expand Down