Skip to content

Instantly share code, notes, and snippets.

@ProGamerGov
ProGamerGov / remove_hooks.py
Created May 1, 2022
Remove hooks in PyTorch without using the hook handle
View remove_hooks.py
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 / color_transfer.py
Last active May 1, 2022
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
View color_transfer.py
from typing import Tuple
import torch
def color_transfer(
input: torch.Tensor,
source: torch.Tensor,
mode: str = "pca",
@ProGamerGov
ProGamerGov / image_cppn.py
Last active May 22, 2020
tensorflow/lucid CPPN (X,Y) --> (R,G,B) Differentiable Image Parameterization in PyTorch
View image_cppn.py
# 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
View README.md

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 Feb 28, 2020
This file is used to convert an MMdnn PyTorch model to a usable state dict model.
View c.py
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 Feb 16, 2020
Use the '-use_hooks' parameter to use forward hooks instead of layers to compute content and style loss.
View neural_style_hooks.py
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
View README.md

neural-style-pt with mean loss and histogram transfer

Histogram Transfer

Users can specify an image for which the histogram will be transfered from, and what images the histogram will be transfered to; either the content image, style image(s), or both.

Mean Loss

A new loss layer type has been added that uses image means. Currently it only uses the first style image specified.

View README_HLOSS.md
View neural_style_test_loss.py
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
View download_gdrive_models.sh
#!/bin/bash
fileid="1qWjJd-mVNxvHhzc9XsU6LjC0pQXIwW4t"
filename="models/VGG16-Stylized-ImageNet.pth"
curl -c ./cookie -s -L "https://drive.google.com/uc?export=download&id=${fileid}" > /dev/null
curl -Lb ./cookie "https://drive.google.com/uc?export=download&confirm=`awk '/download/ {print $NF}' ./cookie`&id=${fileid}" -o ${filename}
fileid="1EU-F9ugeIeTO9ay4PinzsBXgEuCYBu0Z"
filename="models/VGG16_SOD_finetune.pth"
curl -c ./cookie -s -L "https://drive.google.com/uc?export=download&id=${fileid}" > /dev/null
curl -Lb ./cookie "https://drive.google.com/uc?export=download&confirm=`awk '/download/ {print $NF}' ./cookie`&id=${fileid}" -o ${filename}