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 / !latex.mk
Last active December 19, 2022 12:02
Makefile and gitignore for papers and posters
# Download Makefile and .gitignore to a new project:
# wget -O Makefile -nv --no-check-certificate https://gist.githubusercontent.com/vadimkantorov/27ee19d130c413073bedd627c1b6a41e/raw/ && wget -O .gitignore -nv --no-check-certificate https://gist.github.com/vadimkantorov/27ee19d130c413073bedd627c1b6a41e/raw/.gitignore
# To install TexLive (including on WSLv1): sudo apt-get install -y texlive-latex-base texlive-latex-extra texlive-fonts-recommended make
# https://gist.github.com/vadimkantorov/27ee19d130c413073bedd627c1b6a41e
NAME = $(notdir ${CURDIR})# conferenceYEAR
$(NAME).pdf $(NAME).bbl: $(NAME).tex
@vadimkantorov
vadimkantorov / TripletEmbeddingCriterion.lua
Last active March 1, 2017 19:27
A Torch implementation of triplet loss using autograd
autograd = require 'autograd'
-- assumes batch size = 3 (anchor, positive, negative)
function TripletEmbeddingCriterion(margin)
local auto_criterion = autograd.nn.AutoCriterion(return torch.CharTensor(3):random(string.byte('A'), string.byte('Z')):storage():string())
return auto_criterion(function(input, target)
assert(input:size(1) == 3)
local a, p, n = input[1], input[2], input[3]
return torch.sum(torch.cmax(torch.sum(torch.pow(a - p, 2), 1) - torch.sum(torch.pow(a - n, 2), 1) + margin, 0))
end)
@vadimkantorov
vadimkantorov / thumbnail_video_collection.py
Last active January 1, 2023 18:31
Finds recursively all videos in a directory, and produces a webpage with a frame for every 30 minutes of video. A Python primer of gathering basic video information using ffprobe, and reading JPEG-encoded frames using ffmpeg.
# Usage: python thumbnail_video_collection.py /path/to/video/dir > thumbnail.html
import os
import sys
import json
import subprocess
import datetime
import argparse
parser = argparse.ArgumentParser()
@vadimkantorov
vadimkantorov / pdist.lua
Created January 18, 2017 16:04
Pairwise L2 distances in Torch
-- A is a matrix n x d, B is a matrix m x d, i.e. data points are row vectors; returns a matrix n x m
function pdist(A, B)
local eps = 1e-6
local pdist2 = A * B:t()
local normA = torch.sum(torch.cmul(A, A), 2):view(pdist2:size(1), 1):expandAs(pdist2)
local normB = torch.sum(torch.cmul(B, B), 2):t():view(1, pdist2:size(2)):expandAs(pdist2)
return torch.sqrt(normA - 2.0 * pdist2 + normB + eps)
end
@vadimkantorov
vadimkantorov / attrdict.py
Created March 21, 2017 09:17
AttrDict in one Python line
attrdict = type('attrdict', (dict, ), dict(__getattr__ = dict.__getitem__, __setattr__ = dict.__setitem__))
# d = attrdict(a = 1, b = 2)
# print(d.a, d['b'])
@vadimkantorov
vadimkantorov / pytrace.py
Last active August 17, 2017 14:46
Python tracing and profiling helpers
# Bash aliases.
# Usage: pytrace my.py --myarg1
# cprofile my.py --myarg1
# lineprofile my.py # requires "pip install line_profiler"
#
# pytrace() { python -m trace --ignore-dir=$(python -c "import sys, os; print(os.pathsep.join(sys.path))") --trace "$@"; }
# cprofile() { python -m cProfile -s cumulative "$@" 2>&1 &> log.txt; }
# lineprofile() { python $(python -c 'import kernprof; print(kernprof.__file__.strip("c"))') -l -v "$@"}
def pytrace(func):
@vadimkantorov
vadimkantorov / ffmpeg.lua
Created June 2, 2017 14:58
Torch class to read video files by piping from ffmpeg binary output
cjson = require 'cjson'
local Ffmpeg = torch.class('Ffmpeg')
function Ffmpeg:__init(path)
local ffprobeCmd = 'ffprobe -v quiet -print_format json -show_streams ' .. path
local output = io.popen(ffprobeCmd, 'r'):read('*a')
j = cjson.decode(output)
video_st = j['streams'][1]
@vadimkantorov
vadimkantorov / svgscatter.py
Last active December 6, 2018 12:54
Example of interactive SVG scatter plot (with image thumbnails) produced by running t-SNE on MNIST
import base64
import random
import cv2
import torch
import torchvision
def svg(points, labels, thumbnails, legend_size = 1e-1, legend_font_size = 5e-2, circle_radius = 5e-3):
points = (points - points.min(0)[0]) / (points.max(0)[0] - points.min(0)[0])
class_index = sorted(set(labels))
class_colors = [360.0 * i / len(class_index) for i in range(len(class_index))]
# Example: python argparse_lookup_choices.py --lookup key1
# Namespace(lookup='1')
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--lookup', choices = dict(key1 = '1', key2 = '2'), default = '1', action = type('', (argparse.Action, ), dict(__call__ = lambda a, p, n, v, o: setattr(n, a.dest, a.choices[v]))))
print(parser.parse_args())
@vadimkantorov
vadimkantorov / rdv_echange_permis_conduire_paris.sh
Last active January 14, 2023 23:29
A Bash script to monitor "Echange d'un permis de conduire hors Union Européenne" rendez-vous availability at Prefecture de Paris
#! /bin/bash
#Usage:
# 1. Download the script to Home folder and open the Terminal
# 2. Put in your e-mail and SendGrid bearer token to get a notification of success
# 3. Open the Terminal and run the script: "$ bash ~/rdv_echange_permis_conduire_paris.sh"
# 4. Get your RDV! Right now the script will exit once it found a RDV available. It would dump the HTML of the page to "~/rdv_naturalisation_saint_denis.sh.html". Comment the line 38 to continue checking.
set -e