nagadomi / waifu2x-caffe-ubuntu

waifu2x-caffe for Ubuntu
MIT License
38 stars 10 forks source link

waifu2x-caffe on Google Colab! #10

Open mikebilly opened 2 years ago

mikebilly commented 2 years ago

I went through so much trouble trying to make waifu2x-caffe work on Google Colab, but I'm finally done! This took me a week to code from scratch, as I don't know the first thing about Linux or Ubuntu,... I had to search everywhere for the errors. It feels awesome when it's complete and working!, so I decided to share the notebook with you! That waifu2x-caffe on Google Colab is able to utilize cuDNN and Cuda!

Most of the code was copied from many sources, so A LOT of the commands might be redundant. It would be great if you could help me remove all the unnecessary commands and modify the code so it works better and is updated.

Thank you @nagadomi for your quick support on my previous issue at https://github.com/nagadomi/waifu2x-caffe-ubuntu/issues/9. Thank you @sjscotti for your instructive comments at https://github.com/nagadomi/waifu2x-caffe-ubuntu/issues/3, and @ddouglas87 for your detailed guide at https://github.com/k4yt3x/video2x/issues/74#issuecomment-526899181!

The notebook link is: https://colab.research.google.com/drive/1b2Sv0u3eMhXah0De1zlzKPIvgq8aLUTs?usp=sharing You can just copy the blocks of code below and paste them into your new notebook!

Installation, building,...

%cd /content

!sudo apt remove cmake
!sudo apt purge --auto-remove cmake

!mkdir ~/temp
%cd ~/temp

!wget https://cmake.org/files/v3.12/cmake-3.12.3-Linux-x86_64.sh

!sudo mkdir /opt/cmake

!sudo sh cmake-3.12.3-Linux-x86_64.sh --prefix=/opt/cmake --skip-license
#!sudo sh cmake-3.12.3-Linux-x86_64.sh --prefix=/opt/cmake

!rm /usr/local/bin/cmake ## xoá /usr/local/bin/cmake
!sudo ln -s /opt/cmake/bin/cmake /usr/local/bin/cmake

!cmake --version

!sudo apt-get update # This will update the repositories list
!sudo apt-get upgrade # This will update all the necessary packages on your system
!sudo apt-get dist-upgrade # This will add/remove any needed packages
!reboot # You may need this since sometimes after a upgrade/dist-upgrade, there are some left over entries that get fixed after a reboot
!sudo apt-get install linux-headers-$(uname -r) # This should work now

!sudo apt install libboost-all-dev libgflags-dev libprotobuf-dev protobuf-compiler libgoogle-glog-dev libhdf5-dev liblmdb-dev libleveldb-dev libsnappy-dev libopencv-dev libatlas-base-dev libpython3-all-dev python3-pip python3-numpy python-numpy doxygen libmsgpack-dev ffmpeg

!sudo sudo add-apt-repository ppa:graphics-drivers/ppa -y

!apt update
!apt install nvidia-384 nvidia-384-dev

!apt-get install g++ freeglut3-dev build-essential libx11-dev libxmu-dev libxi-dev libglu1-mesa libglu1-mesa-dev

from google.colab import drive

import os

os.chdir('/content')

### installs cudnn11.1
!gdown --id 1fltYtMCo3lms5PSprT4V1r8jDFrliurA ## ok

!gdown --id 1-L4hKKztAjACa5FINdzH6BWR_qDdirF2 ##ok

!sudo dpkg -i libcudnn8-dev_8.0.5.39-1+cuda11.1_amd64.deb libcudnn8_8.0.5.39-1+cuda11.1_amd64.deb ##ok

os.chdir('/usr/local/lib/python3.7/dist-packages/ideep4py/lib')

!sudo ldconfig 
# /sbin/ldconfig.real: /usr/local/lib/python3.7/dist-packages/ideep4py/lib/libmkldnn.so.0 is not a symbolic link

!ls -lha

!sudo ln -sf libmkldnn.so.0.14.0 libmkldnn.so.0

!sudo ln -sf libmkldnn.so.0 libmkldnn.so
# it fixes that ```/sbin/ldconfig.real: /usr/local/lib/python3.7/dist-packages/ideep4py/lib/libmkldnn.so.0 is not a symbolic link```

!ls -lha

!sudo ldconfig 

os.chdir('/usr/local')

!gdown --id 10gnXfW0TOsBhsHu9My7vxzlq4XLT5Apa

### installs cudnn11.1
#we are installing the cuDNN that we dropped in our google drive
%cd /usr/local/
!tar -xzvf "cudnn-11.1-linux-x64-v8.0.5.39.tgz"

!chmod a+r /usr/local/cuda/include/cudnn.h
!cat /usr/local/cuda/include/cudnn.h | grep CUDNN_MAJOR -A 2

%cd /content/

os.chdir('/content')

!git clone --recurse-submodules https://github.com/nagadomi/waifu2x-caffe-ubuntu.git

os.chdir('/content/waifu2x-caffe-ubuntu')

!git clone https://github.com/nagadomi/caffe.git

!nvcc --version

!mkdir build

os.chdir('/content/waifu2x-caffe-ubuntu')

!git submodule update --init --recursive

!apt update

!apt install g++-5
!sudo apt-get install dialog
!sudo apt-get install libprotobuf-dev protobuf-c-compiler protobuf-compiler 
!sudo apt-get install libgflags-dev # from https://github.com/BVLC/caffe/wiki/Commonly-encountered-build-issues
!sudo apt-get install libgoogle-glog-dev # from https://github.com/BVLC/caffe/wiki/Commonly-encountered-build-issues
!sudo apt-get install liblmdb-dev # from https://github.com/BVLC/caffe/wiki/Commonly-encountered-build-issues
!sudo apt-get install libleveldb-dev # from https://gist.github.com/dustismo/6203329#file-gistfile1-sh
!sudo apt-get install libsnappy-dev # from https://github.com/BVLC/caffe/issues/4490

### not sure what these are for
!ln -s /usr/bin ./caffe
!ln -s /usr/lib/x86_64-linux-gnu ./libcaffe ### để ý xem tồn tại ko
### /content/waifu2x-caffe-ubuntu/build/libcaffe

os.chdir('/content')

### ko đc đâu nhá, phải lấy config file của lltcggie, edit: ko rõ nữa. Chuẩn r
!cp /content/waifu2x-caffe-ubuntu/caffe/Makefile.config.example-ubuntu Makefile.config

os.chdir('/content/waifu2x-caffe-ubuntu/build')

!sudo apt-get install libboost1.62-dev ## có thể

!apt -y install libboost-tools-dev libboost-thread1.62-dev magics++ ## có thể

!sudo apt-get install libboost-all-dev
### fixes this warning
### Could not find the following Boost libraries:

###          boost_filesystem

!sudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libboost-all-dev libhdf5-serial-dev libgflags-dev libgoogle-glog-dev liblmdb-dev protobuf-compiler libatlas-base-dev
!sudo apt-get install python-dev python-pip gfortran

!sudo apt-get update -y ## probably unnessary
!sudo apt-get install -y fftw3 ## probably unnessary

!sudo apt-get install doxygen ## probably unnessary

!gcc -DCHECK_FUNCTION_EXISTS=pthread_create -rdynamic -lpthreads  ## probably unnessary

!ld -lpthreads --verbose ## probably unnessary

os.chdir('/content/waifu2x-caffe-ubuntu/build')

!cmake ..

### you may have to run it a second time
!cmake ..

!make

### create sym link after building (build xong thì tạo sym link để waifu2x-caffe xuất hiện trong /content/waifu2x-caffe-ubuntu/bin)
!ln -s /content/waifu2x-caffe-ubuntu/build/waifu2x-caffe /content/waifu2x-caffe-ubuntu/bin/waifu2x-caffe

!sudo apt-get update
!sudo apt-get install nkf

Done!, the following code upscales your picture (please modify accordingly).

os.chdir('/content')

!wget https://github.com/mikebilly/hdm/raw/main/pic.png

os.chdir('/content/waifu2x-caffe-ubuntu/bin')
!./waifu2x-caffe --model_dir ./models/cunet -p cudnn -m noise_scale -n 3 -s 2 -t 1 -i /content/waifu2x-caffe-ubuntu/bin/pic.png -o pic_2x.png | nkf

If you want to upload your upscaled picture to your Google Drive, add the following code:

from google.colab import drive
drive.mount("/content/gdrive")

!cp /content/waifu2x-caffe-ubuntu/bin/pic_2x.png "/content/gdrive/MyDrive"
mikebilly commented 2 years ago

Hi @nagadomi , I tried to upscale a 2720x1920 picture with these output settings -p cudnn -m noise_scale -n 3 -s 8 -t 1 -i but I got this output:

tcmalloc: large alloc 4010811392 bytes == 0x55db15804000 @  0x7f28ceaf11e7 0x7f28cc497382 0x7f28cc59621b 0x55da05ce6afb 0x55da05ce688c 0x55da05ce51cf 0x55da05d1ef73 0x55da05d1eb47 0x55da05d1e016 0x55da05d1dbf5 0x55da05d1caf7 0x55da05cc5e2e 0x7f28cb75abf7 0x55da05cc13ca
tcmalloc: large alloc 4010811392 bytes == 0x55dc097ba000 @  0x7f28ceaf11e7 0x7f28cc497382 0x7f28cc59621b 0x55da05ce6afb 0x55da05ce688c 0x55da05ce51cf 0x55da05d1ef73 0x55da05d1eb47 0x55da05d1e016 0x55da05d1dbf5 0x55da05d1caf7 0x55da05cc5e2e 0x7f28cb75abf7 0x55da05cc13ca
tcmalloc: large alloc 4010811392 bytes == 0x55dcf88bc000 @  0x7f28ceaf11e7 0x7f28cc497382 0x7f28cc59621b 0x7f28cc5b46e5 0x7f28cc5b6113 0x55da05d1e799 0x55da05d1e8b6 0x55da05d1ec07 0x55da05d1e016 0x55da05d1dbf5 0x55da05d1caf7 0x55da05cc5e2e 0x7f28cb75abf7 0x55da05cc13ca

and it didn't create output picture. However, waifu2x-ncnn-vulkan managed to upscale 2080x1318 and 2130x1500 pictures without any error, using the same output settings. (Both waifu2x-caffe and waifu2x-ncnn-vulkan ran on Google Colab with GPU accelerator)

Edit: image this is before it stops upscaling I will try to upscale that 2720x1920 picture with waifu2x-ncnn-vulkan to see if it will succeed.

nagadomi commented 2 years ago

I am not the author of waifu2x-caffe. It depends on the manner in which the resources are used in the program. 8x 2720x1920 is 21760x15360x3channels, minimum memory requirements = 21760*15360*3*sizeof(float) = 4010803200 = about 4GB If the program uses three copies of the temporary image, it will result in an out of memory error. I think it's not designed to handle such huge images with 12GB RAM.

mikebilly commented 2 years ago

it's because I used tta mode right? is there a way to make it keep only 1 copy of the temporary image. And by the way is it possible to use waifu2x on google colab?, not waifu2x-caffe, but waifu2x? Update: waifu2x-ncnn-vulkan successfully upscaled that 2720x1920, 8x, with those output settings, taking around 1 hour and 10 minutes and using up only 2.8gb ram. I'm a bit disappointed in waifu2x-caffe