Skip to content

Instantly share code, notes, and snippets.

@hushell
hushell / feature_average.py
Created March 5, 2021 09:36
Superpixel feature
import torch
F = torch.rand(5, 10, 10) # C, H, W
M = torch.randint(8, (10, 10)) # 8 superpixels
M1hot = torch.nn.functional.one_hot(M, 8) # H, W, K
counts = M1hot.sum(dim=(0, 1)) # counts of pixels per superpixel
Favg = M1hot.view(-1, 8).T.float() @ F0.view(5, -1).T # K, C
@hushell
hushell / gumbel_softmax.py
Last active December 19, 2020 00:46
Gumbel softmax
import torch
from torch.distributions.utils import clamp_probs
from torch.distributions import RelaxedOneHotCategorical
class QuantizeCategorical(torch.autograd.Function):
@staticmethod
def forward(ctx, soft_value):
argmax = soft_value.max(-1)[1]
hard_value = torch.zeros_like(soft_value)
hard_value._unquantize = soft_value
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
wget https://repo.continuum.io/archive/Anaconda3-5.1.0-Linux-x86_64.sh
bash Anaconda3-5.1.0-Linux-x86_64.sh
conda install pytorch torchvision -c pytorch
conda install -c menpo opencv3
pip install dominate tqdm
pip install git+https://github.com/pytorch/tnt.git@master
from joblib import Parallel, delayed
import Queue
import os
# Define number of GPUs available
N_GPU = 4
# Put indices in queue
q = Queue.Queue(maxsize=N_GPU)
for i in range(N_GPU):
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@hushell
hushell / plot_iou.ipynb
Created January 25, 2018 03:54
StageF -> Stage GD -> Stage F2 -> Stage FGD
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
function ww = approx_int_func_solve(xy_pairs, xmin, xmax, intval, w0, f, lbda)
% example:
% xy_pairs = [0, 1.32; 33.9, .55];
% xmin = 0;
% xmax = 33.9;
% intval = 38.985;
% w0 = [0.1,-2,2]
% ww = approx_int_func_solve(xy_pairs, xmin, xmax, intval, w0);
if nargin < 6