Skip to content

Instantly share code, notes, and snippets.

@rahulvigneswaran
rahulvigneswaran / print_nonzeros_table.py
Created October 1, 2019 06:17
The following function displays the count of zero and non-zero weights in a Pytorch model.
def print_nonzeros(model):
nonzero = total = 0
for name, p in model.named_parameters():
tensor = p.data.cpu().numpy()
nz_count = np.count_nonzero(tensor)
total_params = np.prod(tensor.shape)
nonzero += nz_count
total += total_params
print(f'{name:20} | nonzeros = {nz_count:7} / {total_params:7} ({100 * nz_count / total_params:6.2f}%) | total_pruned = {total_params - nz_count :7} | shape = {tensor.shape}')
print(f'alive: {nonzero}, pruned : {total - nonzero}, total: {total}, Compression rate : {total/nonzero:10.2f}x ({100 * (total-nonzero) / total:6.2f}% pruned)')
@rahulvigneswaran
rahulvigneswaran / GPU_selector.py
Created November 8, 2019 10:45
Select GPU in Pytorch
import os
parser = argparse.ArgumentParser()
parser.add_argument("--gpu", default="0", type=str)
args = parser.parse_args()
@rahulvigneswaran
rahulvigneswaran / gpu_with_username.txt
Created November 17, 2019 16:45
Gives you a list of GPUs on a SSH server with its Users' names.
Check this repo :
https://github.com/mseitzer/gpu-monitor
import os
import numpy as np
from scipy.stats import wasserstein_distance, energy_distance
from matplotlib import pyplot as plt
epoch_name = "epoch_1_"
epoch = 1
a = os.getcwd()
root, dirs, files = next(os.walk(a))
import os
import numpy as np
from scipy.stats import wasserstein_distance, energy_distance
from matplotlib import pyplot as plt
epoch_name = "epoch_1_"
epoch = 1
a = os.getcwd()
root, dirs, files = next(os.walk(a))
@rahulvigneswaran
rahulvigneswaran / custom_subset-pytorchforum.ipynb
Last active March 13, 2021 13:18
Custom_Subset [PyTorchForum].ipynb
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@rahulvigneswaran
rahulvigneswaran / save_and_resume_pytorch_boilerplate.py
Created June 2, 2021 23:39
A boilerplate code to save models and resume from it incase the run crashes.
def save_latest(epoch, model_dir, model, optimizer, scheduler=None, wandb_id=None):
"""Saves latest epoch's weights and other necessary things to resume
"""
model_states = {
"epoch": epoch,
"state_dict": model.state_dict(),
"opt_state_dict": optimizer.state_dict(),
"sch_state_dict": scheduler.state_dict() if scheduler != None else None,
"wandb_id_save": wandb_id, #----> Remove this if you don't use wandb for logging
}
@rahulvigneswaran
rahulvigneswaran / ImageNet2012_WordNet_SynsetID_To_ClassNames.txt
Created October 11, 2021 22:04
Instruction to get class names for the corresponding WordNet ID of ImageNet 2012 AKA ILSVRC2012 data set
# First run the following things
echo "Downloading..."
wget -c http://dl.caffe.berkeleyvision.org/caffe_ilsvrc12.tar.gz
echo "Unzipping..."
tar -xf caffe_ilsvrc12.tar.gz && rm -f caffe_ilsvrc12.tar.gz
echo "Done."
#!/bin/bash
#
# script to download and extract ImageNet dataset
# ILSVRC2012_img_train.tar (about 138 GB)
# ILSVRC2012_img_val.tar (about 6.3 GB)
#
# make sure ILSVRC2012_img_train.tar & ILSVRC2012_img_val.tar in your current directory
#
# https://github.com/facebook/fb.resnet.torch/blob/master/INSTALL.md
#