chigozienri / VQGAN-CLIP-animations

133 stars 28 forks source link

Can no longer successfully run the cell "Installation and loading of libraries and definitions" #30

Closed dannokablammo closed 2 years ago

dannokablammo commented 2 years ago

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)

in () 52 from omegaconf import OmegaConf 53 from PIL import Image ---> 54 from taming.models import cond_transformer, vqgan 55 import torch 56 from torch import nn, optim 10 frames /content/taming-transformers/taming/models/cond_transformer.py in () 2 import torch 3 import torch.nn.functional as F ----> 4 import pytorch_lightning as pl 5 6 from main import instantiate_from_config /usr/local/lib/python3.7/dist-packages/pytorch_lightning/__init__.py in () 18 _PROJECT_ROOT = os.path.dirname(_PACKAGE_ROOT) 19 ---> 20 from pytorch_lightning.callbacks import Callback # noqa: E402 21 from pytorch_lightning.core import LightningDataModule, LightningModule # noqa: E402 22 from pytorch_lightning.trainer import Trainer # noqa: E402 /usr/local/lib/python3.7/dist-packages/pytorch_lightning/callbacks/__init__.py in () 12 # See the License for the specific language governing permissions and 13 # limitations under the License. ---> 14 from pytorch_lightning.callbacks.base import Callback 15 from pytorch_lightning.callbacks.device_stats_monitor import DeviceStatsMonitor 16 from pytorch_lightning.callbacks.early_stopping import EarlyStopping /usr/local/lib/python3.7/dist-packages/pytorch_lightning/callbacks/base.py in () 24 25 import pytorch_lightning as pl ---> 26 from pytorch_lightning.utilities.types import STEP_OUTPUT 27 28 /usr/local/lib/python3.7/dist-packages/pytorch_lightning/utilities/__init__.py in () 16 import numpy 17 ---> 18 from pytorch_lightning.utilities.apply_func import move_data_to_device # noqa: F401 19 from pytorch_lightning.utilities.distributed import AllGatherGrad, rank_zero_info, rank_zero_only # noqa: F401 20 from pytorch_lightning.utilities.enums import ( # noqa: F401 /usr/local/lib/python3.7/dist-packages/pytorch_lightning/utilities/apply_func.py in () 27 28 if _TORCHTEXT_AVAILABLE: ---> 29 if _compare_version("torchtext", operator.ge, "0.9.0"): 30 from torchtext.legacy.data import Batch 31 else: /usr/local/lib/python3.7/dist-packages/pytorch_lightning/utilities/imports.py in _compare_version(package, op, version, use_base_version) 52 """ 53 try: ---> 54 pkg = importlib.import_module(package) 55 except (ModuleNotFoundError, DistributionNotFound): 56 return False /usr/lib/python3.7/importlib/__init__.py in import_module(name, package) 125 break 126 level += 1 --> 127 return _bootstrap._gcd_import(name[level:], package, level) 128 129 /usr/local/lib/python3.7/dist-packages/torchtext/__init__.py in () 3 from . import datasets 4 from . import utils ----> 5 from . import vocab 6 from . import legacy 7 from ._extension import _init_extension /usr/local/lib/python3.7/dist-packages/torchtext/vocab/__init__.py in () 9 ) 10 ---> 11 from .vocab_factory import ( 12 vocab, 13 build_vocab_from_iterator, /usr/local/lib/python3.7/dist-packages/torchtext/vocab/vocab_factory.py in () 2 from typing import Dict, Iterable, Optional, List 3 from collections import Counter, OrderedDict ----> 4 from torchtext._torchtext import ( 5 Vocab as VocabPybind, 6 ) ImportError: /usr/local/lib/python3.7/dist-packages/torchtext/_torchtext.so: undefined symbol: _ZTVN5torch3jit6MethodE --------------------------------------------------------------------------- NOTE: If your import is failing due to a missing package, you can manually install dependencies using either !pip or !apt. To view examples of installing some common dependencies, click the "Open Examples" button below. ---------------------------------------------------------------------------
dannokablammo commented 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)
ashwhite commented 2 years ago

I'm getting the same issue with the following notebook:

https://colab.research.google.com/github/chigozienri/VQGAN-CLIP-animations/blob/main/VQGAN-CLIP-animations.ipynb#scrollTo=ZdlpRFL8UAlW

The code fix above did not work for me. I still got the error.

HalcyonForest commented 2 years ago

I'm getting the same issue with the following notebook:

https://colab.research.google.com/github/chigozienri/VQGAN-CLIP-animations/blob/main/VQGAN-CLIP-animations.ipynb#scrollTo=ZdlpRFL8UAlW

The code fix above did not work for me. I still got the error.

me too

dannokablammo commented 2 years ago

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

ashwhite commented 2 years ago

I keep getting a run time error during the increase resolution cell with your notebook @dannokablammo

dannokablammo commented 2 years ago

@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.

ashwhite commented 2 years ago

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 :/

dannokablammo commented 2 years ago

@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.

ashwhite commented 2 years ago

@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?

dannokablammo commented 2 years ago

@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)

ashwhite commented 2 years ago

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.

ashwhite commented 2 years ago

Or maybe I just apply same count for the x & y zoom fields to create the 2d zoom. I think that would work.

ashwhite commented 2 years ago

One thing that patreon notebook doesn't seem to have is the ability to select which frames you want your prompts to appear in.

ashwhite commented 2 years ago

@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.

chigozienri commented 2 years ago

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.

chigozienri commented 2 years ago

@dannokablammo Could you also clarify the issue you're having with upscaling? Again, it seems to be working fine for me.

chigozienri commented 2 years ago

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

dannokablammo commented 2 years ago

@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.

ashwhite commented 2 years ago

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)

in () 1 # @title Download video 2 from google.colab import files ----> 3 files.download(filepath) /usr/local/lib/python3.7/dist-packages/google/colab/files.py in download(filename) 141 raise OSError(msg) 142 else: --> 143 raise FileNotFoundError(msg) # pylint: disable=undefined-variable 144 145 comm_manager = _IPython.get_ipython().kernel.comm_manager FileNotFoundError: Cannot find file: /content/gdrive/MyDrive/vqgan/video.mp4
ashwhite commented 2 years ago

It isn't saved in my google drive where the individual frames are stored as it did before.

ashwhite commented 2 years ago

It also doesn't appear in the cell as it did before (where I could right-click save if needed)

ashwhite commented 2 years ago

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

ashwhite commented 2 years ago

Getting the upscaling error again also:

Upscaling frame 1 None You may be able to avoid this error by backing up the frames,restarting the notebook, and running only the video synthesis cells,or by decreasing the resolution of the image generation steps. If you restart the notebook, you will have to define the 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)

in () 36 "If these steps do not work, please post the traceback in the github." 37 ) ---> 38 raise RuntimeError(stderr) RuntimeError: None
ashwhite commented 2 years ago

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.

ashwhite commented 2 years ago

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?

ashwhite commented 2 years ago

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.

ashwhite commented 2 years ago

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?

dannokablammo commented 2 years ago

@ashwhite Regarding the noticeable flickering/strobe between frames, I compensate for this by applying a deflicker filter in After Effects.

ashwhite commented 2 years ago

@dannokablammo Good shout. Not sure why I didn't think of that.