Skip to content

Instantly share code, notes, and snippets.

View hzitoun's full-sized avatar

Hamed ZITOUN hzitoun

View GitHub Profile
@hzitoun
hzitoun / load_model.py
Created May 18, 2020 17:07
Code for my medium story Serve a Deep Learning Image Classification Model Written in TensorFlow 2 as a REST API and Dockerize it! (with GPU support)
loaded_model = tf.keras.models.load_model('../models/saved_image_classifier/')
loss, acc = loaded_model.evaluate(X_test, y_test, verbose=0)
print('Accuracy: {:5.2f}%'.format(100*acc))
@hzitoun
hzitoun / save_model.py
Created May 18, 2020 17:03
Source code for medium story 🚀Serve a TensorFlow 2 Deep Learning Image Classifier as a REST API and Dockerize it! (with GPU support)
model.save('../models/saved_image_classifier/')
@hzitoun
hzitoun / make_test_inference.py
Created May 18, 2020 16:56
Source code for medium story 🚀Serve a TensorFlow 2 Deep Learning Image Classifier as a REST API and Dockerize it! (with GPU support)
test_image = read_image("../data/raw/test/82_automobile.png", image_size)
plt.imshow(test_image)
plt.title('test image')
plt.show()
pred_softmax = model.predict(test_image.reshape(1, image_size[0], image_size[1], 3))
pred_class_index = np.argmax(pred_softmax, axis=1)
target_names = ['airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck']
print("CNN sees", target_names[pred_class_index.item()])
@hzitoun
hzitoun / viz_image.py
Created May 18, 2020 16:08
Code for my medium story Serve a Deep Learning Image Classification Model Written in TensorFlow 2 as a REST API and Dockerize it! (with GPU support)
deer = read_image("../data/raw/train/3_deer.png", (32, 32))
car = read_image("../data/raw/train/4_automobile.png", (32, 32))
bird = read_image("../data/raw/train/18_bird.png", (32, 32))
plt.subplot(131)
plt.imshow(deer)
plt.title('Deer')
plt.subplot(132)
plt.imshow(car)
plt.title('Car')
@hzitoun
hzitoun / environment.yml
Last active May 18, 2020 17:30
Code for my medium story Serve a Deep Learning Image Classification Model Written in TensorFlow 2 as a REST API and Dockerize it! (with GPU support)
name: tf2-image-classifier-api
channels:
- anaconda
- conda-forge
- defaults
dependencies:
- pip==20.1
- numpy==1.18.4
- flask==1.1.2
- matplotlib==3.2.1
@hzitoun
hzitoun / read_image.py
Last active May 18, 2020 12:58
Code for my medium story Serve a Deep Learning Image Classification Model Written in TensorFlow 2 as a REST API and Dockerize it! (with GPU support)
def read_image(image_path, image_size):
""" read an image using opencv """
image = cv2.imread(image_path) #BGR
# convert to RGB
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# resize using cubic interpolation, a way to deal with missing data (upsize or downsize)
image = cv2.resize(image, image_size, cv2.INTER_CUBIC)
return image