Closed dannokablammo closed 2 years ago
For anyone else running into this same problem, replace the "Installation and loading of libraries and definitions" cell with the code below and it should work again:
# @title Installation and loading of libraries and definitions
# @markdown This cell will take a while because you have to download multiple libraries
print("Downloading CLIP...")
!git clone https://github.com/openai/CLIP &> /dev/null
print("Downloading Python AI libraries...")
!git clone https://github.com/CompVis/taming-transformers &> /dev/null
!pip install ftfy regex tqdm omegaconf pytorch-lightning &> /dev/null
!pip install kornia &> /dev/null
!pip install einops &> /dev/null
print("Installing libraries for handling metadata...")
!pip install stegano &> /dev/null
!apt install exempi &> /dev/null
!pip install python-xmp-toolkit &> /dev/null
!pip install imgtag &> /dev/null
!pip install pillow==7.1.2 &> /dev/null
import argparse
import math
from pathlib import Path
import sys
import os
import cv2
import pandas as pd
import numpy as np
import subprocess
sys.path.append('./taming-transformers')
# Some models include transformers, others need explicit pip install
try:
import transformers
except Exception:
!pip install transformers
import transformers
from IPython import display
from base64 import b64encode
from omegaconf import OmegaConf
from PIL import Image
from taming.models import cond_transformer, vqgan
import torch
from torch import nn, optim
from torch.nn import functional as F
from torchvision import transforms
from torchvision.transforms import functional as TF
from tqdm.notebook import tqdm
from CLIP import clip
import kornia.augmentation as K
import numpy as np
import imageio
from PIL import ImageFile, Image
from imgtag import ImgTag # metadata
from libxmp import * # metadata
import libxmp # metadata
from stegano import lsb
import json
ImageFile.LOAD_TRUNCATED_IMAGES = True
def sinc(x):
return torch.where(x != 0, torch.sin(math.pi * x) / (math.pi * x), x.new_ones([]))
def lanczos(x, a):
cond = torch.logical_and(-a < x, x < a)
out = torch.where(cond, sinc(x) * sinc(x/a), x.new_zeros([]))
return out / out.sum()
def ramp(ratio, width):
n = math.ceil(width / ratio + 1)
out = torch.empty([n])
cur = 0
for i in range(out.shape[0]):
out[i] = cur
cur += ratio
return torch.cat([-out[1:].flip([0]), out])[1:-1]
def resample(input, size, align_corners=True):
n, c, h, w = input.shape
dh, dw = size
input = input.view([n * c, 1, h, w])
if dh < h:
kernel_h = lanczos(ramp(dh / h, 2), 2).to(input.device, input.dtype)
pad_h = (kernel_h.shape[0] - 1) // 2
input = F.pad(input, (0, 0, pad_h, pad_h), 'reflect')
input = F.conv2d(input, kernel_h[None, None, :, None])
if dw < w:
kernel_w = lanczos(ramp(dw / w, 2), 2).to(input.device, input.dtype)
pad_w = (kernel_w.shape[0] - 1) // 2
input = F.pad(input, (pad_w, pad_w, 0, 0), 'reflect')
input = F.conv2d(input, kernel_w[None, None, None, :])
input = input.view([n, c, h, w])
return F.interpolate(input, size, mode='bicubic', align_corners=align_corners)
class ReplaceGrad(torch.autograd.Function):
@staticmethod
def forward(ctx, x_forward, x_backward):
ctx.shape = x_backward.shape
return x_forward
@staticmethod
def backward(ctx, grad_in):
return None, grad_in.sum_to_size(ctx.shape)
replace_grad = ReplaceGrad.apply
class ClampWithGrad(torch.autograd.Function):
@staticmethod
def forward(ctx, input, min, max):
ctx.min = min
ctx.max = max
ctx.save_for_backward(input)
return input.clamp(min, max)
@staticmethod
def backward(ctx, grad_in):
input, = ctx.saved_tensors
return grad_in * (grad_in * (input - input.clamp(ctx.min, ctx.max)) >= 0), None, None
clamp_with_grad = ClampWithGrad.apply
def vector_quantize(x, codebook):
d = x.pow(2).sum(dim=-1, keepdim=True) + codebook.pow(2).sum(dim=1) - 2 * x @ codebook.T
indices = d.argmin(-1)
x_q = F.one_hot(indices, codebook.shape[0]).to(d.dtype) @ codebook
return replace_grad(x_q, x)
class Prompt(nn.Module):
def __init__(self, embed, weight=1., stop=float('-inf')):
super().__init__()
self.register_buffer('embed', embed)
self.register_buffer('weight', torch.as_tensor(weight))
self.register_buffer('stop', torch.as_tensor(stop))
def forward(self, input):
input_normed = F.normalize(input.unsqueeze(1), dim=2)
embed_normed = F.normalize(self.embed.unsqueeze(0), dim=2)
dists = input_normed.sub(embed_normed).norm(dim=2).div(2).arcsin().pow(2).mul(2)
dists = dists * self.weight.sign()
return self.weight.abs() * replace_grad(dists, torch.maximum(dists, self.stop)).mean()
def parse_prompt(prompt):
vals = prompt.rsplit(':', 2)
vals = vals + ['', '1', '-inf'][len(vals):]
return vals[0], float(vals[1]), float(vals[2])
class MakeCutouts(nn.Module):
def __init__(self, cut_size, cutn, cut_pow=1.):
super().__init__()
self.cut_size = cut_size
self.cutn = cutn
self.cut_pow = cut_pow
self.augs = nn.Sequential(
K.RandomHorizontalFlip(p=0.5),
# K.RandomSolarize(0.01, 0.01, p=0.7),
K.RandomSharpness(0.3,p=0.4),
K.RandomAffine(degrees=30, translate=0.1, p=0.8, padding_mode='border'),
K.RandomPerspective(0.2,p=0.4),
K.ColorJitter(hue=0.01, saturation=0.01, p=0.7))
self.noise_fac = 0.1
def forward(self, input):
sideY, sideX = input.shape[2:4]
max_size = min(sideX, sideY)
min_size = min(sideX, sideY, self.cut_size)
cutouts = []
for _ in range(self.cutn):
size = int(torch.rand([])**self.cut_pow * (max_size - min_size) + min_size)
offsetx = torch.randint(0, sideX - size + 1, ())
offsety = torch.randint(0, sideY - size + 1, ())
cutout = input[:, :, offsety:offsety + size, offsetx:offsetx + size]
cutouts.append(resample(cutout, (self.cut_size, self.cut_size)))
batch = self.augs(torch.cat(cutouts, dim=0))
if self.noise_fac:
facs = batch.new_empty([self.cutn, 1, 1, 1]).uniform_(0, self.noise_fac)
batch = batch + facs * torch.randn_like(batch)
return batch
def load_vqgan_model(config_path, checkpoint_path):
config = OmegaConf.load(config_path)
if config.model.target == 'taming.models.vqgan.VQModel':
model = vqgan.VQModel(**config.model.params)
model.eval().requires_grad_(False)
model.init_from_ckpt(checkpoint_path)
elif config.model.target == 'taming.models.cond_transformer.Net2NetTransformer':
parent_model = cond_transformer.Net2NetTransformer(**config.model.params)
parent_model.eval().requires_grad_(False)
parent_model.init_from_ckpt(checkpoint_path)
model = parent_model.first_stage_model
else:
raise ValueError(f'unknown model type: {config.model.target}')
del model.loss
return model
def resize_image(image, out_size):
ratio = image.size[0] / image.size[1]
area = min(image.size[0] * image.size[1], out_size[0] * out_size[1])
size = round((area * ratio)**0.5), round((area / ratio)**0.5)
return image.resize(size, Image.LANCZOS)
I'm getting the same issue with the following notebook:
The code fix above did not work for me. I still got the error.
I'm getting the same issue with the following notebook:
The code fix above did not work for me. I still got the error.
me too
Not sure why my copy/pasted replacement code isn't working for you, but here's a direct link to the new notebook with my code revision and it's working for me: https://colab.research.google.com/drive/1e_pNkug8Yxg-tMDqqi9ZRWdoSvMjPszY?usp=sharing
I keep getting a run time error during the increase resolution cell with your notebook @dannokablammo
@ashwhite I had to strip out a few lines of code to get the "Installation and loading of libraries and definitions" cell to work at all. Probably that's what's affecting the increase resolution cell. I don't know enough to troubleshoot things any further. However, I let Chigozie Nri know about the issue with his notebook and it sounds like he'll try to fix it soon. He should be able to make a much better fix than I did.
Got you. Do you know of any other working notebooks that utilize the zoom effect? I've an urgent project and need to turn it around in the next day :/
@ashwhite You could try out this notebook available through Patreon (only $5 gets you access to it): https://www.patreon.com/sportsracer48/posts
It's a bit more intimidating and gives you control over all kinds of things. I still haven't properly tried it out myself yet.
Otherwise, if you still prefer using this Zooming VQGAN+CLIP notebook even though the upscaler is broken, I've been able to render frames set to 700x700 and then I upscale them myself in After Effects with the Detail-preserving Upscale effect. But I'm paying for Colab Pro so if you're using a free account you might not be able to make 700x700 work.
@dannokablammo - My original render was 700x700 but was unable to get it to the point where I can save out the video. I have Pro version. Are you able to get the video to save?
Does that Patreon version do the zoom effect also?
@ashwhite I just link the colab to my Google Drive (the very first cell does this) and it drops all the frames into a folder that it creates on my Google Drive called vqgan. I then download those frames to my PC and drop them into After Effects. (I never bother with having the notebook convert the frames to a video.)
These are just a few of the options you can control in that other notebook on Patreon:
translate_x: horizontal image motion as a function of time t in seconds.
translate_y: vertical image motion as a function of time t in seconds.
translate_z_3d: forward image motion as a function of time t in seconds. (only for animation_mode:3D)
rotate_3d: image rotation as a quaternion [r,x,y,z] as a function of time t in seconds. (only for animation_mode:3D)
rotate_2d: image rotation in degrees as a function of time t in seconds. (only for animation_mode:2D)
zoom_x_2d: horizontal image zoom as a function of time t in seconds. (only for animation_mode:2D)
zoom_y_2d: vertical image zoom as a function of time t in seconds. (only for animation_mode:2D)
lock_camera: check this box to prevent all scrolling or drifting. Makes for more stable 3D rotations. (only for animation_mode:3D)
field_of_view: vertical field of view in degrees. (only for animation_mode:3D)
near_plane: closest depth distance in pixels. (only for animation_mode:3D)
far_plane: farthest depth distance in pixels. (only for animation_mode:3D)
So they use Z as a forward motion to create the same zoom effect from your notebook? Thanks for the helpful list. I'm just going through that patreon notebook now. It's very in-depth.
Or maybe I just apply same count for the x & y zoom fields to create the 2d zoom. I think that would work.
One thing that patreon notebook doesn't seem to have is the ability to select which frames you want your prompts to appear in.
@dannokablammo Having played with that patreon notebook, it's actually not overly clear and a little nonsensical, although the support is quicker. Hopefully there will be a fix for this one soon. Desperate to get something rendered.
Apologies for taking a while on this. I can't actually replicate the issue on the GPUs colab is giving me, but I can try to fix it. I've pushed @dannokablammo 's fix, so you can try again, but I think whether it works or not will depend on which particular GPU you get.
@ashwhite and @dannokablammo , could you run the following 2 things for me (in an empty notebook) and tell me the outputs?
!nvidia-smi --query-gpu=gpu_name --format=noheader,nounits,csv
import torch
import torchvision
print(torch.__version__)
print(torchvision.__version__)
The other thing you could try is using a google account that doesn't have colab pro / pro+, I know that's annoying, but if you need to get something out quickly it's a workaround for now.
@dannokablammo Could you also clarify the issue you're having with upscaling? Again, it seems to be working fine for me.
However, I let Chigozie Nri know about the issue with his notebook and it sounds like he'll try to fix it soon. He should be able to make a much better fix than I did.
She/her FYI
@chigozienri Your new version is running fine for me. Thanks!
It was @ashwhite who was having an issue with the upscaler in the temporary version I had made. It's working for me in your latest version, though.
Hi. So I'm now able to run the cells except when it comes to the video cell I get this error:
FileNotFoundError Traceback (most recent call last)
It isn't saved in my google drive where the individual frames are stored as it did before.
It also doesn't appear in the cell as it did before (where I could right-click save if needed)
In response to your testing of a blank notebook
First code output: NVIDIA-SMI has failed because it couldn't communicate with the NVIDIA driver. Make sure that the latest NVIDIA driver is installed and running.
Second code output: 1.10.0+cu111 0.11.1+cu111
Getting the upscaling error again also:
filepath
manuallyby adding filepath = 'PATH_TO_THE_VIDEO'
to the beginning of this cell. If these steps do not work, please post the traceback in the github.RuntimeError Traceback (most recent call last)
I've just emptied my browser cache and reduced everything down to minimums (width + height = 400 / Frames = 10 etc) and it produced the video at the end so I'm going to try with larger settings and see if that has fixed it. Will report back when completed.
Reporting on my last attempt - I ran the following (width + height = 650 / Frames = 1000 / fps = 60) and got the upscaling error again. I then tried to run the video cell regardless of the previous cell error and it weirdly re-rendered my previous video of 10 frames so I'm not sure what is going on with this. Maybe weird caching?
I've managed to find a dirty work around - As the png's are accessible I've found an app called PNG Animator in the mac AppStore to export the full sequence. The only issue being that the file size is over 1GB for an 18 second clip so will need compressing in post. Just thought i'd share if anyone else runs into the same issue as me and needs a quick fix.
I do have one query with the settings... Is there a way of reducing the flickering? I've seen some smooth zoom effects happen by others but everything I do feels quite strobey. Is there a specific setting to overcome this?
@ashwhite Regarding the noticeable flickering/strobe between frames, I compensate for this by applying a deflicker filter in After Effects.
@dannokablammo Good shout. Not sure why I didn't think of that.
This is in regard to: https://colab.research.google.com/github/chigozienri/VQGAN-CLIP-animations/blob/main/VQGAN-CLIP-animations.ipynb
I used to have no problem running this colab (even as recently as last night) but now I'm suddenly getting an error when I run the cell called "Installation and loading of libraries and definitions".
Here's the error that keeps happening:
ImportError Traceback (most recent call last)