Skip to content

Instantly share code, notes, and snippets.

View davidbau's full-sized avatar

David Bau davidbau

View GitHub Profile
@davidbau
davidbau / index.js
Created May 14, 2015 14:42
requirebin sketch
// require() some stuff from npm (like you were using browserify)
// and then hit Run Code to run it on the right
var seedrandom = require('seedrandom');
var rng = seedrandom.xor4096('hello.');
console.log(rng());
@davidbau
davidbau / index.js
Created May 14, 2015 17:36
requirebin sketch
// require() some stuff from npm (like you were using browserify)
// and then hit Run Code to run it on the right
var $ = require('jquery');
var seedrandom = require('seedrandom');
var rng = seedrandom.xor4096('hello.');
for (var j = 0; j < 20; ++j) {
$('body').append('<p>' + rng() + '</p>');
}
@davidbau
davidbau / index.js
Last active August 29, 2015 14:22
requirebin sketch
var Parser = require('parse5').Parser;
//Instantiate parser
var parser = new Parser(null, {locationInfo: true});
//Then feed it with an HTML document
var d = parser.parseFragment('<p>hello<p>hello')
var info = ['<pre>', 'fragment has ' + d.childNodes.length + ' top-level nodes'];
for (var j = 0; j < d.childNodes.length; ++j) {
@davidbau
davidbau / .block
Last active March 25, 2017 13:23
3D Scatter Plot Using three.js
license: mit
@davidbau
davidbau / download_mp.py
Last active February 21, 2018 15:19
Script for downloading and formatting miniplaces in pytorch ImageFolder format
#!/usr/bin/env python2.7
# Script to create simple flat pytorch ImageFolder folder hierarchy
# of training and validation images for miniplaces. Each category
# name is just a folder name (numbered in alphabetical order as in
# the original miniplaces), and both train and val images are places
# directly inside a single level of folders with the flat cateogry names.
import shutil, os, tarfile
@davidbau
davidbau / autoimport.py
Created August 26, 2018 08:50
Used to eval python expressions containing arbitrary fully-qualified package names, automatically importing packages as needed.
from collections import defaultdict
from importlib import import_module
def autoimport_eval(term):
'''
Used to eval any expression containing fully-qualified names, such as
'torchvision.models.alexnet(pretrained=True)' with automatic import of
global module names.
'''
import os
import torch.utils.data as data
from torchvision.datasets.folder import default_loader, is_image_file
from PIL import Image
def grayscale_loader(path):
with open(path, 'rb') as f:
return Image.open(f).convert('L')
class FeatureFolder(data.Dataset):
@davidbau
davidbau / fooling.html
Created February 14, 2019 06:07
an example vue HTML file that displays images and metadata
<!DOCTYPE html>
<html>
<!-- an example vue HTML file that displays images and metadata -->
<head>
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.16/dist/vue.js"
integrity="sha256-CMMTrj5gGwOAXBeFi7kNokqowkzbeL8ydAJy39ewjkQ="
crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.10/lodash.js"
integrity="sha256-qwbDmNVLiCqkqRBpF46q5bjYH11j5cd+K+Y6D3/ja28="
crossorigin="anonymous"></script>
@davidbau
davidbau / explorefeaturebyclick.ipynb
Created October 20, 2019 22:14
ExploreFeatureByClick.ipynb
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@davidbau
davidbau / nethook.py
Last active April 1, 2022 19:38
Utilities for instrumenting a pytorch model: Trace, TraceDict, subsequence, get_module, replace_module, get_parameter, set_requires_grad.
"""
Utilities for instrumenting a torch model. (David Bau)
Trace will hook one layer at a time.
TraceDict will hook multiple layers at once.
subsequence slices intervals from Sequential modules.
get_module, replace_module, get_parameter resolve dotted names.
set_requires_grad recursively sets requires_grad in module parameters.
"""