Skip to content

Instantly share code, notes, and snippets.

@Tushar-N
Tushar-N / hook_activations.py
Created August 3, 2018 00:06
Pytorch code to save activations for specific layers over an entire dataset
import torch
import torch.nn as nn
import torch.nn.functional as F
import torchvision.models as tmodels
from functools import partial
import collections
# dummy data: 10 batches of images with batch size 16
dataset = [torch.rand(16,3,224,224).cuda() for _ in range(10)]
@Tushar-N
Tushar-N / pad_packed_demo.py
Last active December 27, 2022 06:35
How to use pad_packed_sequence in pytorch<1.1.0
import torch
import torch.nn as nn
from torch.nn.utils.rnn import pack_padded_sequence, pad_packed_sequence
seqs = ['gigantic_string','tiny_str','medium_str']
# make <pad> idx 0
vocab = ['<pad>'] + sorted(set(''.join(seqs)))
# make model
@Tushar-N
Tushar-N / copy_google_drive_folders.js
Created December 19, 2022 08:26
Google apps script to copy folders in Google Drive
function getDriveFolderFromPath (path) {
return (path || "/").split("/").reduce ( function(prev,current) {
if (prev && current) {
var fldrs = prev.getFoldersByName(current);
return fldrs.hasNext() ? fldrs.next() : null;
}
else {
return current ? null : prev;
}
},DriveApp.getRootFolder());
@Tushar-N
Tushar-N / livechat-video-playback
Last active April 19, 2022 21:35
Video player with live chat playback
Video player with live chat playback
@Tushar-N
Tushar-N / click_heatmap.py
Last active March 24, 2022 14:11
Click on an image to superimpose a heatmap
import cv2
import numpy as np
import argparse
'''
usage: python click_heatmap.py <image file>
left-click: add point to heatmap
s: save image (000.png, 001.png, ...)
q: quit
r: reset
@Tushar-N
Tushar-N / csub.sh
Last active August 28, 2019 22:51
Condor submit script: use csub <cmd> from a submit node
cmd=$@
cv_dir=`expr "$cmd" : '.*--cv_dir \([^ ]*\).*'`
mkdir -p $cv_dir
# temporarily create two files: submit.sh and run.sh
cat > $cv_dir/submit.sh << EOF
universe = vanilla
Executable = /lusr/bin/bash
Arguments = $cv_dir/run.sh
@Tushar-N
Tushar-N / jpeg_to_h5.py
Created April 26, 2019 19:08
Save jpeg images as compressed binary data, instead of a dense (C, H, W) uint8 tensor.
import torch
import io
from PIL import Image
import numpy as np
# Dataset class for extracting binary data from images to store
class ImageDataset:
def __init__(self):
super(ImageDataset, self).__init__()
self.images = [] # some list of PIL images