Skip to content

Instantly share code, notes, and snippets.

View ankitshekhawat's full-sized avatar

Ankit Shekhawat ankitshekhawat

  • Bangalore, India
View GitHub Profile
@ankitshekhawat
ankitshekhawat / get_colors.py
Last active April 22, 2019 12:45
extract colors from 2d array; GPU seeded with kmc2; sorted according to number of samples
import kmc2
from sklearn.cluster import MiniBatchKMeans
from skimge.color import lab2rgb, rgb2lab
def get_colors(array, clusters=3):
labs = rgb2lab([array]).squeeze()
seeding = kmc2.kmc2(labs, clusters)
kmeans = MiniBatchKMeans(n_clusters = clusters, init=seeding)
kmeans.fit(np.squeeze(labs))
LABELS = kmeans.labels_
@ankitshekhawat
ankitshekhawat / clip2superpixel.py
Last active May 9, 2019 19:28
Clip a binary segmentation map to super pixel using IOU
from scipy.ndimage.morphology import binary_erosion
from keras.utils import to_categorical
from fast_slic.avx2 import SlicAvx2
def clip_to_superpixel(image, z, num_segments=100, erosion=12, threshold=0.5, iou_thresh=0.5):
fast_slic = SlicAvx2(num_components=num_segments, compactness=10, quantize_level=1)
segments = fast_slic.iterate(image)
onehot = to_categorical(segments, num_classes=num_segments, dtype='uint8')
threshold = (np.max(z)+np.min(z))*threshold
@ankitshekhawat
ankitshekhawat / metric_loss.py
Created April 27, 2019 01:09
tensorflow.contrib's metric losss, saved for posterity as tf.contrib is going away
# Copyright 2017 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
@ankitshekhawat
ankitshekhawat / recall_top_k.py
Last active May 8, 2019 16:42
accuracy metric for online triplet
def recall_top_k(y_true, y_pred):
# get batch size
batch_size = y_pred.shape[0]
# get pairwise distances
dists = _pairwise_distances(y_pred)
# get indexes of the closest item in the batch predictions
# (item itself is always the top_k, so hence getting the second top K)
# top_k function gives indexes for the highest values so subtracting from 2(or a high enough number) to invert the matrix
@ankitshekhawat
ankitshekhawat / install_pillow_simd.sh
Created May 16, 2019 17:52 — forked from ciscorn/install_pillow_simd.sh
Install pillow-simd on Ubuntu
#!/bin/env bash
pip3 uninstall pillow
apt install \
libjpeg-turbo8-dev \
zlib1g-dev \
libtiff5-dev \
liblcms2-dev \
libfreetype6-dev \
{
"lines": {
"blueline": {
"name": "Blue Line",
"color": "blue",
"type": "subway"
},
"redline": {
"name": "Red Line",
"color": "red",
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@ankitshekhawat
ankitshekhawat / download_via_aria2p.py
Last active March 12, 2020 05:47
download using an aria deamon
import aria2p
# parellel run:
# aria2c --enable-rpc --rpc-listen-all
# Spawn aria server
import subprocess
subprocess.call(['gnome-terminal', '-e', 'aria2c --enable-rpc --rpc-listen-all'])
import thinplate as tps
from PIL import Image
import morphops
import cv2
# helper function for https://github.com/cheind/py-thin-plate-spline
import thinplate as tps
from PIL import Image
import cv2