Skip to content

Instantly share code, notes, and snippets.

View CleanPegasus's full-sized avatar
🐍
Focusing

Arun CleanPegasus

🐍
Focusing
View GitHub Profile
@CleanPegasus
CleanPegasus / tfx.sh
Last active September 27, 2020 17:25
Installing TensorFlow Serving
# Run this code on the terminal
# Make sure you have curl installed. If not use "sudo apt install curl" to install it
echo "deb [arch=amd64] http://storage.googleapis.com/tensorflow-serving-apt stable tensorflow-model-server tensorflow-model-server-universal" | sudo tee /etc/apt/sources.list.d/tensorflow-serving.list && \
curl https://storage.googleapis.com/tensorflow-serving-apt/tensorflow-serving.release.pub.gpg | sudo apt-key add -
sudo apt update
sudo apt-get install tensorflow-model-server
# Or alternatively you can use pip to install tensorflow serving
# If you don't have pip installed use "sudo apt install python3-pip" to install it in your linux environment
pip3 install tensorflow-serving-api
@CleanPegasus
CleanPegasus / tfx_save.py
Last active September 28, 2020 07:22
Saving model for serving
import tempfile
model_dir = tempfile.gettempdir() # gets the path of the temporary folder
version = 1 # version of the specific model
export_path = os.path.join(model_dir, str(version))
if os.path.isdir(export_path):
print("Found an existing version of the model. Deleting the previous version\n")
!rm -r {export_path} # terminal command to remove a directory
# Saving the model
@CleanPegasus
CleanPegasus / model.py
Created September 27, 2020 18:09
Emotion recognition model
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import tensorflow as tf
from tensorflow.keras.layers import Conv2D, GlobalAveragePooling2D, MaxPooling2D, SeparableConv2D
from tensorflow.keras.layers import Dense, Input, Dropout, BatchNormalization, Activation
from tensorflow.keras.models import Model
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.regularizers import l2
@CleanPegasus
CleanPegasus / add_env.py
Created September 28, 2020 05:31
Adding model directory to environment
os.environ['MODEL_DIR'] = model_dir
@CleanPegasus
CleanPegasus / serve.sh
Created September 28, 2020 05:39
Model Serving
# The model will be accessible as an API through the port 8501
bash --bg
nohup tensorflow_model_server \
--rest_api_port = 8501 \
--model_name = emotion_recognizer \
--model_base_path = "${MODEL_DIR}" >server.log 2>&1
@CleanPegasus
CleanPegasus / emotion_img_preprocess.py
Created September 28, 2020 07:53
Preprocessing the image before predicting
img = cv2.imread('img.jpg') # reading the image
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # converting the image to grayscale
img = cv2.resize(img, (48, 48)) # resizing the image
img = img/255 # normalizing the image
img = np.reshape(img, [-1, 48, 48, 1]) # reshaping the image to be suitable for serving
@CleanPegasus
CleanPegasus / predict_server.py
Created September 28, 2020 07:57
Get the prediction from the served model
import requests
import json
EMOTIONS = ["angry", "disgust", "scared", "happy", "sad", "surprised", "neutral"] # Emotions corresponding to the output
data = json.dumps({"signature_name": "serving_default", "instances": img.tolist()}) # making the input in the format required for serving
headers = {"content-type": "application/json"} # specifying that the input will be in json format
json_response = requests.post('http://localhost:8501/v1/models/emotion_model:predict', data=data, headers=headers) # post request to the served model
predictions = json.loads(json_response.text)['predictions'] # getting the predictions
import tensorflow as tf
def sample(x, y, z):
return tf.reduce_sum(x + y * z)
from tensordash.tensordash import Tensordash
histories = Tensordash(
ModelName = '<YOUR_MODEL_NAME>',
email = '<YOUR_EMAIL_ID>',
password = '<YOUR PASSWORD>')
try:
model.fit(
X_train,
from tensordash.torchdash import Torchdash
histories = Torchdash(
ModelName = '<YOUR_MODEL_NAME>',
email = '<YOUR_EMAIL_ID>',
password = '<YOUR PASSWORD>')
try:
for epoch in range(epochs):
losses = []