Skip to content

Instantly share code, notes, and snippets.

View davidbau's full-sized avatar

David Bau davidbau

View GitHub Profile
@davidbau
davidbau / runningstats.py
Last active April 18, 2024 11:15
Running stats objects for pytorch: mean, variance, covariance, second-moment, quantiles, topk, and combinations.
'''
To use a runningstats object,
1. Create the the desired stat object, e.g., `m = Mean()`
2. Feed it batches via the add method, e.g., `m.add(batch)`
3. Repeat step 2 any number of times.
4. Read out the statistic of interest, e.g., `m.mean()`
Built-in runningstats objects include:
@davidbau
davidbau / googlesender.py
Last active February 25, 2023 03:25
Python 2 script for generating a JavaScript/jQuery function for submitting to a Google Form
# Google form submit-maker.
#
# Usage: python googlesender.py https://docs.google.com/forms/d/e/1.../viewform
#
# Point this python file at a live Google forms URL, and it will generate
# code for a Javascript function that submits to that form cross-domain.
#
# Notes:
# - The form should be created with "short answer text" questions.
# - The viewform URL to scrape is the link shared when the form is sent.
@davidbau
davidbau / +lightbox.html
Created January 14, 2023 21:07
lightbox.html, a page for automatically showing all images in a directory on an Apache server.
<!DOCTYPE html>
<html>
<!--
+lightbox.html, a page for automatically showing all images in a
directory on an Apache server. Just copy it into the directory.
Works by scraping the default directory HTML at "./" - David Bau.
-->
<head>
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.16/dist/vue.js"
integrity="sha256-CMMTrj5gGwOAXBeFi7kNokqowkzbeL8ydAJy39ewjkQ="
@davidbau
davidbau / seeing_images.ipynb
Created October 17, 2019 08:03
seeing_images.ipynb
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@davidbau
davidbau / simpleganpaint.ipynb
Last active July 14, 2022 19:34
SimpleGanPaint.ipynb
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@davidbau
davidbau / npycat
Last active May 1, 2022 14:30
npycat: cat utility and swiss army knife for npy and npz files
#!/usr/bin/env python
"""
npycat: cat utility and swiss army knife for numpy and pytorch files.
"""
import numpy, argparse
args = None
def main():
@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.
"""
@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 / 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>
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):