Skip to content

Instantly share code, notes, and snippets.

View himanshurawlani's full-sized avatar

Himanshu Rawlani himanshurawlani

View GitHub Profile
@himanshurawlani
himanshurawlani / download_inceptionv3_model.py
Created October 19, 2018 21:47
Script to download InceptionV3 model in Keras
from keras.applications.inception_v3 import InceptionV3
from keras.layers import Input
inception_model = InceptionV3(weights='imagenet', input_tensor=Input(shape=(224, 224, 3)))
inception_model.save('inception.h5')
@himanshurawlani
himanshurawlani / export_saved_model.py
Created October 19, 2018 21:54
Script to convert Keras models to TensorFlow Serving SavedModel format
import tensorflow as tf
# The export path contains the name and the version of the model
tf.keras.backend.set_learning_phase(0) # Ignore dropout at inference
model = tf.keras.models.load_model('./inception.h5')
export_path = '../my_image_classifier/1'
# Fetch the Keras session and save the model
# The signature definition is defined by the input and output tensors
# And stored with the default serving key
@himanshurawlani
himanshurawlani / serving_sample_request.py
Created October 19, 2018 22:35
Script to make a POST request to TensorFlow Serving server with input image given as argument
import argparse
import json
import numpy as np
import requests
from keras.applications import inception_v3
from keras.preprocessing import image
# Argument parser for giving input image_path from command line
ap = argparse.ArgumentParser()
@himanshurawlani
himanshurawlani / app.py
Last active October 19, 2018 23:05
Flask server to be used on top of TensorFlow Serving server
import base64
import json
from io import BytesIO
import numpy as np
import requests
from flask import Flask, request, jsonify
from keras.applications import inception_v3
from keras.preprocessing import image
@himanshurawlani
himanshurawlani / auto_cmd.py
Last active February 6, 2021 01:03
Script to automate starting and stopping of Flask and TF_Serving servers
import os
import signal
import subprocess
# Making sure to use virtual environment libraries
activate_this = "/home/ubuntu/tensorflow/bin/activate_this.py"
exec(open(activate_this).read(), dict(__file__=activate_this))
# Change directory to where your Flask's app.py is present
os.chdir("/home/ubuntu/Desktop/Medium/keras-and-tensorflow-serving/flask_server")
@himanshurawlani
himanshurawlani / flask_sample_request.py
Last active October 19, 2018 23:28
Script to make a request to Flask server (mimics frontend request)
# importing the requests library
import argparse
import base64
import requests
# defining the api-endpoint
API_ENDPOINT = "http://localhost:5000/imageclassifier/predict/"
# taking input image via command line
@himanshurawlani
himanshurawlani / using_tfds_load.py
Last active March 21, 2019 22:12
This gist shows how to use tfds.load() from TensorFlow Datasets
import tensorflow_datasets as tfds
SPLIT_WEIGHTS = (8, 1, 1)
splits = tfds.Split.TRAIN.subsplit(weighted=SPLIT_WEIGHTS)
(raw_train, raw_validation, raw_test), metadata = tfds.load(name="tf_flowers",
with_info=True,
split=list(splits),
# specifying batch_size=-1 will load full dataset in the memory
# batch_size=-1,
# as_supervised: `bool`, if `True`, the returned `tf.data.Dataset`
@himanshurawlani
himanshurawlani / preprocess_dataset.py
Last active December 31, 2023 00:14
Pre-processing the dataset download using tfds.load()
def format_example(image, label):
image = tf.cast(image, tf.float32)
# Normalize the pixel values
image = image / 255.0
# Resize the image
image = tf.image.resize(image, (IMG_SIZE, IMG_SIZE))
return image, label
train = raw_train.map(format_example)
validation = raw_validation.map(format_example)
@himanshurawlani
himanshurawlani / fetching_batches.py
Last active March 21, 2019 22:09
This gist shows how to fetch batches of data downloaded using tfds.load()
plt.figure(figsize=(12,12))
for batch in train.take(1):
for i in range(9):
image, label = batch[0][i], batch[1][i]
plt.subplot(3, 3, i+1)
plt.imshow(image.numpy())
plt.title(get_label_name(label.numpy()))
plt.grid(False)
# OR
@himanshurawlani
himanshurawlani / simple_cnn.py
Created March 17, 2019 13:15
Building a simple CNN using tf.keras functional API
from tensorflow import keras
# Creating a simple CNN model in keras using functional API
def create_model():
img_inputs = keras.Input(shape=IMG_SHAPE)
conv_1 = keras.layers.Conv2D(32, (3, 3), activation='relu')(img_inputs)
maxpool_1 = keras.layers.MaxPooling2D((2, 2))(conv_1)
conv_2 = keras.layers.Conv2D(64, (3, 3), activation='relu')(maxpool_1)
maxpool_2 = keras.layers.MaxPooling2D((2, 2))(conv_2)
conv_3 = keras.layers.Conv2D(64, (3, 3), activation='relu')(maxpool_2)