Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Super-Resolution Google Colab + colab folder #148

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions colab-notebooks/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Google Colab Notebooks
230 changes: 230 additions & 0 deletions colab-notebooks/Super_deep_daze.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,230 @@
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "Super-deep-daze.ipynb",
"private_outputs": true,
"provenance": [],
"collapsed_sections": []
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "yNomPmYphEf6"
},
"source": [
"# Super deep-daze\n",
"## deep-daze & Super-Resolution GAN\n",
"\n",
"## https://github.com/lucidrains/deep-daze\n",
"\n",
"## https://github.com/krasserm/super-resolution"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "G2PhQt5gi2ag"
},
"source": [
"# deep-daze"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "Vy_VWKIFhB34"
},
"source": [
"**Restart after running this cell!**\n",
"\n",
"You must run this cell and then restart and rerun everything for the PyTorch version to be correct. Otherwise the model will run but not produce any meaningful output."
]
},
{
"cell_type": "code",
"metadata": {
"id": "qs1XUMEYg7B_"
},
"source": [
"!nvidia-smi\n",
"\n",
"import subprocess\n",
"\n",
"CUDA_version = [s for s in subprocess.check_output([\"nvcc\", \"--version\"]).decode(\"UTF-8\").split(\", \") if s.startswith(\"release\")][0].split(\" \")[-1]\n",
"print(\"CUDA version:\", CUDA_version)\n",
"\n",
"if CUDA_version == \"10.0\":\n",
" torch_version_suffix = \"+cu100\"\n",
"elif CUDA_version == \"10.1\":\n",
" torch_version_suffix = \"+cu101\"\n",
"elif CUDA_version == \"10.2\":\n",
" torch_version_suffix = \"\"\n",
"else:\n",
" torch_version_suffix = \"+cu110\"\n",
"\n",
"!pip install torch==1.7.1{torch_version_suffix} torchvision==0.8.2{torch_version_suffix} -f https://download.pytorch.org/whl/torch_stable.html ftfy regex\n",
"!pip install deep-daze --upgrade"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"cellView": "form",
"id": "sl-7OtJHhVvF"
},
"source": [
"from tqdm import trange\n",
"from IPython.display import Image, display\n",
"import os\n",
"from deep_daze import Imagine\n",
"\n",
"TEXT = \"Super Deep Daze\" #@param {type:\"string\"}\n",
"NUM_LAYERS = 42 #@param {type:\"number\"}\n",
"SAVE_EVERY = 10#@param {type:\"number\"}\n",
"IMAGE_WIDTH = 512 #@param {type:\"number\"}\n",
"SAVE_PROGRESS = True #@param {type:\"boolean\"}\n",
"LEARNING_RATE = 1e-5 #@param {type:\"number\"}\n",
"ITERATIONS = 10000 #@param {type:\"number\"}\n",
"\n",
"os.chdir('/content/')\n",
"\n",
"model = Imagine(\n",
" text = TEXT,\n",
" num_layers = NUM_LAYERS,\n",
" save_every = SAVE_EVERY,\n",
" image_width = IMAGE_WIDTH,\n",
" batch_size=64,\n",
" gradient_accumulate_every=1,\n",
" lr = LEARNING_RATE,\n",
" iterations = ITERATIONS,\n",
" save_progress = SAVE_PROGRESS,\n",
" save_date_time = True).cuda()\n",
"\n",
"for epoch in trange(20, desc = 'epochs'):\n",
" for i in trange(ITERATIONS, desc = 'iteration'):\n",
" model.train_step(epoch, i)\n",
"\n",
" if i % model.save_every != 0:\n",
" continue\n",
"\n",
" filename = TEXT.replace(' ', '_')\n",
" image = Image(filename + '.png')\n",
" display(image)"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "Ny7IiA3PicJK"
},
"source": [
"# Super-Resolution GAN"
]
},
{
"cell_type": "code",
"metadata": {
"id": "HKMvpfahiXjR"
},
"source": [
"%cd /content/\n",
"!git clone https://github.com/krasserm/super-resolution\n",
"%cd /content/super-resolution/"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "kiYb41o1if7I"
},
"source": [
"import os\n",
"import matplotlib.pyplot as plt\n",
"\n",
"from data import DIV2K\n",
"from model.srgan import generator, discriminator\n",
"\n",
"from model import resolve_single\n",
"from utils import load_image\n",
"\n",
"%matplotlib inline"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "tA20tQQvihmO"
},
"source": [
"# Location of model weights (needed for demo)\n",
"weights_dir = 'weights/srgan'\n",
"weights_file = lambda filename: os.path.join(weights_dir, filename)\n",
"\n",
"os.makedirs(weights_dir, exist_ok=True)"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "vZfxW83Uimoc"
},
"source": [
"%cd /content/super-resolution/\n",
"!wget https://martin-krasser.de/sisr/weights-srgan.tar.gz\n",
"!tar -xpf weights-srgan.tar.gz\n",
"%cd /content/super-resolution"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "nHILuxRkippo"
},
"source": [
"gan_generator = generator()\n",
"gan_generator.load_weights(weights_file('gan_generator.h5'))\n",
"\n",
"lr = load_image('/content/Super_Deep_Daze.jpg')\n",
"image = resolve_single(gan_generator, lr)"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "YHxF1BLAiwZ0"
},
"source": [
"plt.figimage(image, resize=True)\n",
"#plt.savefig('/content/TEST')\n",
"plt.imshow(image)"
],
"execution_count": null,
"outputs": []
}
]
}
4 changes: 2 additions & 2 deletions deep_daze/deep_daze.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ def replace_current_img(self):
if os.path.isfile(always_current_img) or os.path.islink(always_current_img):
os.remove(always_current_img) # remove the file

copy(str(self.filename), always_current_img)
#copy(str(self.filename), str(self.filename))

def generate_and_save_image(self, custom_filename: Path = None, current_iteration: int = None):
"""
Expand All @@ -279,7 +279,7 @@ def generate_and_save_image(self, custom_filename: Path = None, current_iteratio
img.clamp_(0., 1.)
self.filename = custom_filename if custom_filename else self.image_output_path(current_iteration=current_iteration)
save_image(img, self.filename)
self.replace_current_image()
self.replace_current_img()
tqdm.write(f'image updated at "./{str(self.filename)}"')


Expand Down