Skip to content

Instantly share code, notes, and snippets.

@vadimkantorov
vadimkantorov / svgheatmap.py
Last active December 21, 2021 21:10
Python script for drawing a heat map in SVG, with a jet colorbar
import base64
import numpy as np
import cv2
def svg(tensor, K = 10, font_size = 0.03):
min_score, max_score = tensor.min(), tensor.max()
labels = np.arange(min_score, max_score, float(max_score - min_score) / K)
bgr_image = cv2.applyColorMap((tensor - min_score) * 255. / (max_score - min_score), dtype = np.uint8), cv2.COLORMAP_JET)
bgr_jet = cv2.applyColorMap(np.arange(255, dtype = np.uint8), cv2.COLORMAP_JET).squeeze(1)
@vadimkantorov
vadimkantorov / ffmpeg.py
Last active May 3, 2022 16:40
Simple python FFmpeg pipe wrapper for reading videos
# print(next(iter(FFmpeg('video.mp4')))[0].shape)
# (480, 640, 3)
# print(next(iter(FFmpeg('video.mp4')))[1])
# 0.0799333
import subprocess
import json
import re
import numpy as np
@vadimkantorov
vadimkantorov / sitecustomize.py
Last active January 3, 2018 23:12
A trick to make certain Python3 modules available in Python2 without modifying the original Python3 code. It suffices to place sitecustomize.py along with original code.
import sys
import urllib
import urlparse
sys.modules['urllib.request'] = type('', (object, ), dict(urlretrieve = staticmethod(urllib.urlretrieve)))
sys.modules['urllib.parse'] = type('', (object, ), dict(urlparse = staticmethod(urlparse.urlparse)))
@vadimkantorov
vadimkantorov / compact_bilinear_pooling.py
Last active September 22, 2021 07:51
Compact Bilinear Pooling in PyTorch using the new FFT support
# References:
# [1] Multimodal Compact Bilinear Pooling for Visual Question Answering and Visual Grounding, Fukui et al., https://arxiv.org/abs/1606.01847
# [2] Compact Bilinear Pooling, Gao et al., https://arxiv.org/abs/1511.06062
# [3] Fast and Scalable Polynomial Kernels via Explicit Feature Maps, Pham and Pagh, https://chbrown.github.io/kdd-2013-usb/kdd/p239.pdf
# [4] Fastfood — Approximating Kernel Expansions in Loglinear Time, Le et al., https://arxiv.org/abs/1408.3060
# [5] Original implementation in Caffe: https://github.com/gy20073/compact_bilinear_pooling
# TODO: migrate to use of new native complex64 types
# TODO: change strided x coo matmul to torch.matmul(): M[sparse_coo] @ M[strided] -> M[strided]
@vadimkantorov
vadimkantorov / image2urijpeg.sh
Last active June 1, 2023 17:52
Convert an image to Base64 data-uri format using ImageMagick and OpenSSL
# Usage:
# bash image2urijpeg.sh image.jpg 320x240 > base64.txt # will resize before conversion
# bash image2urijpeg.sh image.jpg > base64.txt # will keep original size
magick "$1" -resize ${2:--} jpeg:- | openssl enc -base64 -A | sed -e 's/^/data:image\/jpeg;base64,/'
@vadimkantorov
vadimkantorov / gifcast.cmd
Last active June 16, 2018 00:53
Record a GIF screencast on Windows using FFmpeg
ffmpeg -y -video_size 800x600 -offset_x 10 -offset_y 120 -framerate 5 -show_region 1 -f gdigrab -an -i desktop -vf scale=320:240 output.gif
@vadimkantorov
vadimkantorov / genpass.sh
Last active June 23, 2020 12:04
Generate a random password
echo $(< /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c10)
alias genpass='echo $(< /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c10)'
@vadimkantorov
vadimkantorov / env.sh
Last active May 21, 2019 13:51
Install PyTorch for CUDA 9.0
PREFIX=$PWD/env
export CUDA_HOME=/usr/local/cuda-9.0
export CMAKE_PREFIX_PATH=$PREFIX
export LD_LIBRARY_PATH=$PREFIX/lib:$LD_LIBRARY_PATH
export CPATH=$PREFIX/include:$CPATH
export PATH=$PREFIX/bin:$CUDA_HOME/bin:${PATH//"/home/cvlab/anaconda3/bin"/}
export CUDNN_LIB_DIR=$PREFIX/lib
export CUDNN_INCLUDE_DIR=$PREFIX/include
@vadimkantorov
vadimkantorov / jlabnopass.sh
Last active December 7, 2020 09:18
Start JupyterLab server without password
jupyter lab --NotebookApp.token=''
alias runjupyter="jupyter lab --NotebookApp.token='' --port 6006 --no-browser --ip=\"0.0.0.0\" --allow-root --NotebookApp.iopub_msg_rate_limit=1000000.0 --NotebookApp.iopub_data_rate_limit=100000000.0 --NotebookApp.notebook_dir=/"
alias mapports='ssh -L 7006:localhost:7006'
@vadimkantorov
vadimkantorov / prettifyjson.sh
Created August 13, 2018 18:23
Prettify json files in Python3 processing correctly UTF-8
# prettifyjson input.json > output.json
alias prettifyjson='python3 -c "import json, sys; json.dump(json.load(open(sys.argv[1])), sys.stdout, ensure_ascii = False, indent = 2)"'