Skip to content

Instantly share code, notes, and snippets.

View vadimkantorov's full-sized avatar
💭
looking for an internship for summer/fall 2021

Vadim Kantorov vadimkantorov

💭
looking for an internship for summer/fall 2021
View GitHub Profile
@vadimkantorov
vadimkantorov / sequential_backprop.py
Last active November 12, 2021 16:34
Mini-batching within the model in PyTorch
# Instance-Aware, Context-Focused, and Memory-Efficient Weakly Supervised Object Detection, https://arxiv.org/abs/2004.04725
# https://github.com/NVlabs/wetectron/issues/72
# https://discuss.pytorch.org/t/mini-batching-gradient-accumulation-within-the-model/136460
import torch
import torch.nn as nn
class SequentialBackprop(nn.Module):
def __init__(self, module, batch_size = 1):
super().__init__()
@vadimkantorov
vadimkantorov / torchutils.py
Last active November 4, 2021 14:38
PyTorch useful functions
import torch
import torch.nn.functional as F
import torchvision
import base64
gather_incomplete = lambda tensor, I: tensor.gather(I.ndim, I[(...,) + (None,) * (tensor.ndim - I.ndim)].expand((-1,) * (I.ndim + 1) + tensor.shape[I.ndim + 1:])).squeeze(I.ndim)
expand_dim = lambda tensor, expand, dim: tensor.unsqueeze(dim).expand((-1, ) * (dim if dim >= 0 else tensor.ndim + dim + 1) + (expand, ) + (-1, ) * (tensor.ndim - (dim if dim >= 0 else tensor.ndim + dim + 1)))
encode_image_as_html = lambda img, height, width: '<img height="{height}" width="{width}" src="data:image/jpeg;base64,{encoded}" />'.format(height = height, width = width, encoded = bytes(base64.b64encode(torchvision.io.encode_jpeg(F.interpolate(img[None], (height, width)).squeeze(0)))).decode())
# Blender 2.93 python script for producing images like in https://github.com/facebookresearch/meshrcnn/issues/22
# blender -noaudio --background --python meshrcnn_overlay_mesh.py -- --object-path test.obj --background-path test.png
#
# Sample ouptut:
# https://github.com/facebookresearch/meshrcnn/issues/100
#
# References:
# https://github.com/yuki-koyama/blender-cli-rendering/blob/master/02_suzanne.py
# https://github.com/weiaicunzai/blender_shapenet_render/blob/master/render_rgb.py
# https://towardsdatascience.com/blender-2-8-grease-pencil-scripting-and-generative-art-cbbfd3967590
@vadimkantorov
vadimkantorov / imagefilters.py
Last active August 18, 2021 10:07
Image gradient filters in PyTorch
# https://scikit-image.org/docs/dev/api/skimage.filters.html
import torch
import torch.nn.functional as F
def sobel_filter() -> '2133':
flipped_sobel_x = torch.tensor([
[-1, 0, 1],
[-2, 0, 2],
[-1, 0, 1]
@vadimkantorov
vadimkantorov / mts.txt
Created June 26, 2021 21:41
MTS cheatsheet
https://media.mts.ru/technologies/124401/
Calls redirect: **21*+NUM#
import os |
import shutil
@vadimkantorov
vadimkantorov / gpuscan.py
Last active May 17, 2021 23:55
ssh to check nvidia-smi and aggregate by user / machine
#! /usr/bin/python3
# authors: Theophile Dalens, Vadim Kantorov
# first version: https://github.com/willowsierra/office/blob/master/scripts/gpuscan.py
# TODO: resolve usernames with "ldapsearch -x -h ildap -b 'ou=people,dc=inria,dc=fr' -s sub 'inrialogin=kantorov' -LLL mail"
# TODO: implement thresholds
# TODO: prepare suggested e-mail text
import itertools
import argparse
@vadimkantorov
vadimkantorov / downloadgdrive.sh
Last active October 27, 2021 10:21
Bash script for downloading a file from Google Drive by its ID using wget
# Method from https://gist.github.com/vladalive/535cc2aff8a9527f1d9443b036320672
# Usage (download UP-DETR checkpoint published at https://github.com/dddzg/up-detr, https://drive.google.com/file/d/1_YNtzKKaQbgFfd6m2ZUCO6LWpKqd7o7X/view?usp=sharing)
# downloadgdrive 1_YNtzKKaQbgFfd6m2ZUCO6LWpKqd7o7X # -> ./1_YNtzKKaQbgFfd6m2ZUCO6LWpKqd7o7X
# downloadgdrive 1_YNtzKKaQbgFfd6m2ZUCO6LWpKqd7o7X abc # -> ./abc
function downloadgdrive() { wget --load-cookies googlecookies.txt "https://docs.google.com/uc?export=download&confirm="$(wget --quiet --save-cookies googlecookies.txt --keep-session-cookies --no-check-certificate "https://docs.google.com/uc?export=download&id=$1" -O- | sed -rn "s/.*confirm=([0-9A-Za-z_]+).*/\1\n/p")"&id=$1" -O ${2:-$1}; }