This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
os.environ['MODEL_DIR'] = model_dir |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import tensorflow as tf | |
def sample(x, y, z): | |
return tf.reduce_sum(x + y * z) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from tensordash.tensordash import Tensordash | |
histories = Tensordash( | |
ModelName = '<YOUR_MODEL_NAME>', | |
email = '<YOUR_EMAIL_ID>', | |
password = '<YOUR PASSWORD>') | |
try: | |
model.fit( | |
X_train, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = [] |
OlderNewer