This file contains hidden or 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 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') |
This file contains hidden or 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 | |
# 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 |
This file contains hidden or 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 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() |
This file contains hidden or 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 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 |
This file contains hidden or 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 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") |
This file contains hidden or 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
# 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 |
This file contains hidden or 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_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` |
This file contains hidden or 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
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) |
This file contains hidden or 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
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 |
This file contains hidden or 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 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) |
OlderNewer