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 / 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 / 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 / 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 / letterbox.py
Last active May 7, 2019 12:24
Resize and place an image in a square with padding.
#PIL method
from PIL import Image
def letterbox(image, size=299, bg=(0,0,0)):
(w,h) = image.size
ratio = size / max([w,h])
image = image.resize((int(ratio*w),int(ratio*h)))
background = Image.new('RGB', (size, size), bg)
background.paste(image, [int((size-s)/2) for s in image.size])
return background
@ankitshekhawat
ankitshekhawat / GIF2MP4.swift
Last active August 16, 2018 06:22 — forked from powhu/GIF2MP4.swift
Swift 3.0 GIF to MP4 (MacOS)
//
// GIF2MP4.swift
//
// Created by PowHu Yang on 2017/1/24.
// Copyright © 2017 PowHu Yang. All rights reserved.
//
@ankitshekhawat
ankitshekhawat / PIL_to_datauri.py
Last active December 6, 2021 05:53
Convert PIL image to DataURI
#python3
def pil2datauri(img):
#converts PIL image to datauri
data = BytesIO()
img.save(data, "JPEG")
data64 = base64.b64encode(data.getvalue())
return u'data:img/jpeg;base64,'+data64.decode('utf-8')

TensorFlow Serving in 10 minutes!

TensorFlow SERVING is Googles' recommended way to deploy TensorFlow models. Without proper computer engineering background, it can be quite intimidating, even for people who feel comfortable with TensorFlow itself. Few things that I've found particularly hard were:

  • Tutorial examples have C++ code (which I don't know)
  • Tutorials have Kubernetes, gRPG, Bezel (some of which I saw for the first time)
  • It needs to be compiled. That process takes forever!

After all, it worked just fine. Here I present an easiest possible way to deploy your models with TensorFlow Serving. You will have your self-built model running inside TF-Serving by the end of this tutorial. It will be scalable, and you will be able to query it via REST.

Verifying that "ankitshekhawat.id" is my Blockstack ID. https://onename.com/ankitshekhawat
@ankitshekhawat
ankitshekhawat / moonraider.js
Last active December 11, 2017 17:56
Change gmail logo user script
// ==UserScript==
// @name Moonraider
// @namespace http://moonraft.com/
// @version 0.2
// @description Chanege the mail logo to one you want
// @author You
// @match https://mail.google.com/mail/*
// @grant none
// ==/UserScript==
@ankitshekhawat
ankitshekhawat / wifiscanner.py
Last active February 2, 2017 09:02
A simple python wifi scanner for OSX using OSX's airport command line utility. Returns Mac Addresses and RSSI
from subprocess import check_output
import re
def scan_networks():
airport = '/System/Library/PrivateFrameworks/Apple80211.framework/Versions/A/Resources/airport -s'
rc = check_output(airport, shell=True).split('\n')[1:-1]
networks =[]
for i in rc:
i = i.lstrip().rstrip()