This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Helper script to compute BPD of images in a directory. | |
The script will never modify any files. | |
All conversions are done in memory on a copy. | |
Usage: python compute_bpd.py your_glob_pattern [extension] [colorspace] [psnr-check] | |
Args: | |
- extension: any valid PIL image extension (e.g., PNG, WebP, JPEG). | |
- colorspace: any valid PIL colorspace plus the lossless YCoCg. | |
- psnr-check (flag): if set, will compute PSNR of saved image with respect ot the original colorspace. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
Usage: python patchify_padded.py 'my_dir/*.png' output_dir 32 | |
This script: | |
1) Loads all images matching the glob 'my_dir/*.png' | |
2) Padds them with zeros such that the height and width are divisible by the specified patch size (in this case, 32) | |
3) Saves them to disk in parallel to the output directory (output_dir) | |
''' | |
from PIL import Image |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def push(state, symbol, cdf_func, prec): | |
cdf_low, cdf_high = cdf_func(symbol) | |
freq = cdf_high - cdf_low | |
return prec*(state // freq) + (state % freq) + cdf_low | |
def pop(state, icdf_func, cdf_func, prec): | |
cdf_value = state % prec | |
symbol, cdf_low, cdf_high = icdf_func(cdf_value) | |
freq = cdf_high - cdf_low | |
return symbol, freq*(state // prec) + cdf_value - cdf_low |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
def encode(v): | |
changes = np.flatnonzero(np.diff(v)) + 1 | |
values = np.r_[v[0], v[changes]] | |
runlengths = np.diff(np.r_[0, changes, len(v)]) | |
return runlengths, values | |
def decode(runlengths, values): | |
return np.repeat(values, runlengths) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# We can produce samples from an un-normalized distribution by adding | |
# iid Gumbel noise together with the argmax operator; which is denoted as the Gumbel-Max Trick. | |
# However, argmax doesn't produce meaningful gradient signals, so we replace argmax | |
# by softmax, with a temperature parameter (Gumbel-Softmax Trick). | |
# | |
# I didn't invent any of this, all credit goes to the following papers: | |
# - https://arxiv.org/abs/1611.00712 | |
# - https://arxiv.org/abs/1611.01144 | |
import numpy as np |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from time import time | |
from contextlib import contextmanager | |
import json | |
import torch | |
import logging | |
logging.basicConfig(stream=sys.stdout, | |
level=logging.INFO, format='%(asctime)s %(name)s %(levelname)s:%(message)s') | |
logger = logging.getLogger() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
This file is heavily inspired by https://github.com/j-towns/ans-notes/blob/master/rans.py | |
We describe a variant of bits-back coding called BB-Huffman. This file is meant | |
purely as an educational tool and is in no way optimized. The goals are to | |
1. illustrate how bits-back coding can be used with Huffman-like codecs; | |
2. and how the cost of using a selector to toggle between codebooks can be greatly reduced. | |
- Symbols are integers between 0 and 2; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
call plug#begin() | |
Plug 'lervag/vimtex' | |
Plug 'manicmaniac/coconut.vim' | |
Plug 'goerz/jupytext.vim' | |
Plug 'skanehira/preview-markdown.vim' | |
call plug#end() | |
tnoremap <Esc> <C-\><C-n> | |
tnoremap <C-A> pwd\|xclip -selection clipboard<CR><C-\><C-n>:cd <C-r>+<CR>i | |
vnoremap <C-c> "+y |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
from hashlib import sha256 | |
from pandas.util import hash_pandas_object | |
from functools import lru_cache | |
class HashableDataFrame(pd.DataFrame): | |
def __init__(self, obj): | |
super().__init__(obj) |