Skip to content

Instantly share code, notes, and snippets.

View vishal-keshav's full-sized avatar
🏠
Working from home #COVID-19

Vishal Keshav vishal-keshav

🏠
Working from home #COVID-19
View GitHub Profile
class Solution(object):
def convert(self, s, numRows):
if numRows == 1: return s
arr_of_arr = [[] for i in range(numRows)]
running_index, direction = 0, 1
for c in s:
arr_of_arr[running_index].append(c)
running_index += direction
if running_index>=numRows:
running_index, direction = numRows-2, -1
@vishal-keshav
vishal-keshav / plot_images.md
Created November 2, 2019 17:37
Plotting a bunch of images with matplotlib

This snippet I often use to output a bunch of sample images in jupyter notebook.

%matplotlib inline
from matplotlib import pyplot as plt
_, axs = plt.subplots(1, 10, figsize=(32, 32))
axs = axs.flatten()
for img, ax in zip(example_images, axs):
    ax.imshow(img)
plt.show()
print(example_labels)
@vishal-keshav
vishal-keshav / draw_me_a_cat.cpp
Created June 8, 2019 16:10
Cool map of india
#include "stdio.h"
int main (void) {
int a=10, b=0, c=10;
char* bits ="TFy!QJu ROo TNn(ROo)SLq SLq ULo+UHs UJq TNn*RPn/QPbEWS_JSWQAIJO^NBELPeHBFHT}TnALVlBLOFAkHFOuFETpHCStHAUFAgcEAelclcn^r^r\\tZvYxXyT|S~Pn SPm SOn TNn ULo0ULo#ULo-WHq!WFs XDt!";
a = bits[b];
while (a != 0) {
a = bits[b];
b++;
while (a > 64) {
a--;
@vishal-keshav
vishal-keshav / distributed_training.py
Last active May 6, 2019 15:22
distributed training example in tensorflow 1.13
def get_model(model_name, input_tensor, is_train, pretrained):
....
def optimisation(label, logits, param_dict):
....
def get_data_provider(dataset_name, dataset_path, param_dict):
....
def test_distributed_training(model, dataset, param_dict):
@vishal-keshav
vishal-keshav / tensorflow_pb_infer_script.py
Created April 9, 2019 06:07
Infer the output from a tensorflow pb model
import tensorflow as tf
import numpy as np
def preprocess(img):
# Apply any preprocessing on the input
return img
def inference_from_pb(pb_file = "model.pb", img, inputs = ['input'], outputs = ['output']):
img = preporcess(img)
with tf.gfile.GFile(pb_file, "rb") as f:
@vishal-keshav
vishal-keshav / convert_to_inference_graph.py
Created April 3, 2019 10:30
Optimizing tensorflow model for inference
import sys
import tensorflow as tf
from tensorflow.python.tools import freeze_graph
from tensorflow.python.tools import optimize_for_inference_lib
def convert_frozen_to_inference(model_path = "generated_model",
frozen_file = "generated_model.pb", inputs = ["input"],
outputs = ["output"], out_file = "generated_model_opt.pb"):
frozen_graph = tf.GraphDef()
with tf.gfile.Open(model_path + "/" + frozen_file) as f:
@vishal-keshav
vishal-keshav / imagenet_tf_datasets.py
Created April 1, 2019 11:06
ImageNet from tensorflow_datasets
"""
Testing the brand new datasets from tensorflow community for experimenting on
ImageNet2012 dataset.
We identify several problems while working with ImageNet dataset:
1. The dataset is not easy to download. Credentials (email) of some well known
organization/university is required to get the dowanload link.
2. The huge size if dataset, namely "ILSVRC2012_img_train.tar" -> 138Gb
and "ILSVRC2012_img_val.tar" -> 7Gb
@vishal-keshav
vishal-keshav / tensorflow_model_exporter.py
Created March 28, 2019 10:50
Exporting tensorflow graph to protobuf and tflite
"""
A well-documented module to export the tensorflow trained graph as protobuf and
tflite.
Background on protobuf and tflite model format:
Protocol buffer format(frozen) and tflite format contains exactly the same graph
and same weights associated to that graph, the only difference is that tflite
format is understood by tflite interpreter while protobuf format is understood
by tensorflow.
@vishal-keshav
vishal-keshav / mobilenet_pb_inference.py
Created March 19, 2019 10:32
How to infer an image classification with pre-trained frozen tensorflow pb file
import tensorflow as tf
from tensorflow.python.platform import gfile
import numpy as np
from imagenet_classes import class_names
from scipy.misc import imread, imresize
dir_name = 'mobilenet_v1_1.0_224'
with tf.Graph().as_default() as graph:
with tf.Session() as sess:
@vishal-keshav
vishal-keshav / C_plus_plus_logging.md
Created February 25, 2019 06:26
A better way to log in C++

Logging in C++

When writing a high performance C++ program, sometimes it become crucial to log what is happening while program execution is in progress. There are several logging libraries like glog (from Google) and log(from Boost), but using those for a simple program can be counter productive as it unneccesarily increases the executable size and takes more time to setup.

I use the below 5 lines code snippet to log in C++ program. Variadic templates are being used to create such a minimilistic logging system.

template <typename T>
void log_it(T t) {
 cout &lt;&lt; t &lt;&lt; endl;