Skip to content

Instantly share code, notes, and snippets.

@adgaudio
adgaudio / guided_filter_nd.py
Created March 17, 2021 10:18
Guided Filter supporting multi-channel guide image and 1 channel source image
"""
PyTorch Guided Filter for multi-channel (color) guide image and 1 channel
(grayscale) source image
"""
import torch as T
import torch.nn as nn
def box_filter_1d(tensor, dim, r):
cs = tensor.cumsum(dim).transpose(dim, 0)
@adgaudio
adgaudio / download_all_pretrained_pytorch_models.ipy
Created August 15, 2019 21:29
A quick hack script that uses ipython to download all pretrained pytorch models
# this is a quick ipython script to download all pre-trained pytorch models.
# run like this:
# ipython THIS_SCRIPT.ipy
import torchvision as tv, types, os.path
# get list of urls ... in a brittle way.
x = {k: v for dct in [getattr(y, 'model_urls') for y in (getattr(tv.models, x) for x in dir(tv.models)) if isinstance(y, types.ModuleType)] for k, v in dct.items()}
// these double forward slashes are comments. you can write whatever you
// want in the text after the double slashes.
x = 10;
y = 20;
z = 5;
cube([x,y,z], center=true);
cube([x,y,z], center=false);
@adgaudio
adgaudio / tesseract_git_install.sh
Created February 21, 2018 21:31
Install tesseract and leptonica from source
#!/usr/bin/env bash
# This script installs leptonica and tesseract from source
# it does not install other pre-requisites to a custom location.
# side note: install prefix is defined once per library.
# side note: it clones git repositories in the current directory.
set -e
set -u
@adgaudio
adgaudio / DecayCounter.java
Last active December 19, 2017 20:16
A weighted counter that remembers the most frequent and recent pairs on a 2-color graph.
import java.util.HashMap;
/* A weighted counter that remembers most frequent and recent pairs on a 2-color graph, where:
* - any pair (a_i, b_i) contains elements a_i from set A and elements b_i are from set B. A and B are disjoint.
*
* This counter basically implements a recurrence relation to maintain scores for each pair:
* score = memory * prev_score + (1-memory) * (+/-)1
*
* "memory" is a value between 0 and 1 chooses how much history history to take into account.
*
@adgaudio
adgaudio / spark_serialization_demo.py
Last active October 24, 2016 23:25
This gist demonstrates that spark 0.9.1 (and I'm guessing also 1.0.0) don't serialize a logger instance properly when code runs on workers
"""
This gist demonstrates that spark 1.0.0 and 0.9.1
don't serialize a logger instance properly when code runs on workers.
run this code via:
spark-submit spark_serialization_demo.py
- or -
pyspark spark_serialization_demo.py
"""
import pyspark
@adgaudio
adgaudio / ssh_tunnel.py
Last active December 15, 2015 07:18
SSH tunnel through a gateway to another machine. I know there are plenty of implementations, but none I found just worked and returned a "localhost:port" string like this does. I've been using this successfully for several months with no problems. However, I have noticed that sometimes, --encrypted is required (this may have something to do with…
"""SSH tunnel through a gateway to another machine.
USAGE:
python ./ssh_tunnel.py -h
or
>>> import ssh_tunnel
>>> ssh_tunnel.main('gateway_username', 'dest_host_addr')
@adgaudio
adgaudio / add_to_security_group.py
Last active December 15, 2015 05:19 — forked from robbyt/secgroup.py
Inspired by https://gist.github.com/robbyt/2493423 This StarCluster plugin grants all tcp, udp and icmp privileges for 10.0.0.0/8 between the current cluster's security group and the given security group, in both directions for cidr block 10.0.0.0/8. This would be particularly useful for using StarCluster within Amazon VPC.
"""
Based on https://gist.github.com/robbyt/2493423
This StarCluster plugin grants all tcp, udp and icmp privileges for 10.0.0.0/8
between the current cluster's security group and the given security group,
in both directions
"""
from starcluster.clustersetup import ClusterSetup
from starcluster.logger import log
# Tmux Configuration
# By Alex Gaudio
# April 15, 2012
# HACKS
#######
#######
## Unfortunately, the osx system clipboard integration sucks. But there's a well documented workaround.
# OS X: pbcopy and pbpaste workaround from ChrisJohnsen
# code available at: https://github.com/ChrisJohnsen/tmux-MacOSX-pasteboard.git
@adgaudio
adgaudio / distributed_percentile_algorithm.py
Last active August 29, 2015 14:05
Distributed Percentile and Distributed Median - a proof of concept and example
"""
This example demonstrates a distributed algorithm to identify the
percentile of a distributed data set.
Because this is a toy implementation, the data isn't actually
distributed across multiple machines.
"""
import numpy as np