Skip to content

Instantly share code, notes, and snippets.

@ProGamerGov
ProGamerGov / remove_hooks.py
Created May 1, 2022 18:23
Remove hooks in PyTorch without using the hook handle
from collections import OrderedDict
from typing import Callable, Dict, Optional
from warnings import warn
import torch
def _remove_all_forward_hooks(
module: torch.nn.Module, hook_fn_name: Optional[str] = None
) -> None:
"""
@ProGamerGov
ProGamerGov / replace_vae.py
Last active December 26, 2023 07:15
Replace the VAE in a Stable Diffusion model with a new VAE. Tested on v1.4 & v1.5 SD models
# Script by https://github.com/ProGamerGov
import copy
import torch
# Path to model and VAE files that you want to merge
vae_file_path = "vae-ft-mse-840000-ema-pruned.ckpt"
model_file_path = "v1-5-pruned-emaonly.ckpt"
# Name to use for new model file
@ProGamerGov
ProGamerGov / convert_webp_to_png.py
Created February 12, 2023 21:09
Convert all '.webp' images in a dataset to '.png'
import os
from PIL import Image
def convert_webp_to_png(directory: str, delete_old_webp_images: bool = False):
for root, dirs, files in os.walk(directory):
for file in files:
if file.endswith(".webp"):
filepath = os.path.join(root, file)
img = Image.open(filepath)
new_filepath = os.path.splitext(filepath)[0] + ".png"
@ProGamerGov
ProGamerGov / color_transfer.py
Last active May 1, 2022 18:30
A PyTorch function that matches the histogram of one image to another image, and should hopefully be helpful for individuals with use cases like astronomy & neural style transfer
from typing import Tuple
import torch
def color_transfer(
input: torch.Tensor,
source: torch.Tensor,
mode: str = "pca",
@ProGamerGov
ProGamerGov / alternate.sh
Last active September 22, 2020 21:04
Scripts that create rotated and reflected version of a specified image
#! /bin/bash
#Usage: ./alternate.sh <input.png>
#Dependency: sudo apt-get install imagemagick
#Permission fix troubleshooting: chmod u+x ./alternate.sh
# 1. Defines the input image as a variable
input=$1
input_file=`basename $input`
clean_name="${input_file%.*}"
@ProGamerGov
ProGamerGov / image_cppn.py
Last active May 22, 2020 00:35
tensorflow/lucid CPPN (X,Y) --> (R,G,B) Differentiable Image Parameterization in PyTorch
# tensorflow/lucid CPPN (X,Y) --> (R,G,B) Differentiable Image Parameterization in PyTorch
import torch
import torch.nn as nn
import torch.optim as optim
import torchvision.transforms as transforms
from inception5h import Inception5h
from PIL import Image

neural-style-pt with tiling

Tiling

The tiling feature is based on neural-dream's tiling system.

Usage

Basic usage:

python neural_style_tile.py -style_image  -content_image  -tile_size 256 -image_size 512
@ProGamerGov
ProGamerGov / c.py
Last active February 28, 2020 20:39
This file is used to convert an MMdnn PyTorch model to a usable state dict model.
import torch
import torch.nn as nn
from collections import OrderedDict
import imp
import numpy as np
# Import the model classes that were edited. Replace 'model_class_name" with the name of the class script, and
# replace 'ModelName' with the name of the class in the script
from model_class_name import ModelName
@ProGamerGov
ProGamerGov / neural_style_hooks.py
Last active February 16, 2020 22:24
Use the '-use_hooks' parameter to use forward hooks instead of layers to compute content and style loss.
import os
import copy
import torch
import torch.nn as nn
import torch.optim as optim
import torchvision.transforms as transforms
from PIL import Image
from CaffeLoader import loadCaffemodel, ModelParallel