Last active
May 31, 2020 06:58
-
-
Save oak-tree/e6930d857072ae345daa9a5acf66a1d6 to your computer and use it in GitHub Desktop.
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 | |
import os | |
import time | |
import numpy as np | |
import matplotlib.pyplot as plt | |
from tensorflow import keras | |
from tensorflow.keras.preprocessing import image | |
from tensorflow.keras.applications.resnet50 import preprocess_input, decode_predictions | |
def run(): | |
MAIN_PATH = "./" | |
import urllib | |
from pathlib import Path | |
Path(MAIN_PATH).mkdir(parents=True, exist_ok=True) | |
opener = urllib.request.URLopener() | |
opener.addheader('User-Agent', 'whatever') | |
opener.retrieve( | |
"https://d17fnq9dkz9hgj.cloudfront.net/breed-uploads/2018/08/siberian-husky-detail.jpg?bust=1535566590&width=630", "{}/img0.JPG".format(MAIN_PATH)) | |
opener.retrieve( | |
"https://www.hakaimagazine.com/wp-content/uploads/header-gulf-birds.jpg", "{}/img1.JPG".format(MAIN_PATH)) | |
opener.retrieve("https://www.artis.nl/media/filer_public_thumbnails/filer_public/00/f1/00f1b6db-fbed-4fef-9ab0-84e944ff11f8/chimpansee_amber_r_1920x1080.jpg__1920x1080_q85_subject_location-923%2C365_subsampling-2.jpg", "{}/img2.JPG".format(MAIN_PATH)) | |
opener.retrieve( | |
"https://www.familyhandyman.com/wp-content/uploads/2018/09/How-to-Avoid-Snakes-Slithering-Up-Your-Toilet-shutterstock_780480850.jpg", "{}/img3.JPG".format(MAIN_PATH)) | |
model = tf.keras.models.load_model('resnet50_saved_model') | |
img_path = '{}/img0.JPG'.format(MAIN_PATH) # Siberian_husky | |
img = image.load_img(img_path, target_size=(224, 224)) | |
x = image.img_to_array(img) | |
x = np.expand_dims(x, axis=0) | |
x = preprocess_input(x) | |
preds = model.predict(x) | |
# decode the results into a list of tuples (class, description, probability) | |
# (one such list for each sample in the batch) | |
print('{} - Predicted: {}'.format(img_path, | |
decode_predictions(preds, top=3)[0])) | |
plt.subplot(2, 2, 1) | |
plt.imshow(img) | |
plt.axis('off') | |
plt.title(decode_predictions(preds, top=3)[0][0][1]) | |
batch_size = 8 | |
batched_input = np.zeros((batch_size, 224, 224, 3), dtype=np.float32) | |
for i in range(batch_size): | |
img_path = '{}/img%d.JPG'.format(MAIN_PATH) % (i % 4) | |
img = image.load_img(img_path, target_size=(224, 224)) | |
x = image.img_to_array(img) | |
x = np.expand_dims(x, axis=0) | |
x = preprocess_input(x) | |
batched_input[i, :] = x | |
batched_input = tf.constant(batched_input) | |
print('batched_input shape: ', batched_input.shape) | |
# Benchmarking throughput | |
N_warmup_run = 50 | |
N_run = 1000 | |
elapsed_time = [] | |
for i in range(N_warmup_run): | |
preds = model.predict(batched_input) | |
for i in range(N_run): | |
start_time = time.time() | |
preds = model.predict(batched_input) | |
end_time = time.time() | |
elapsed_time = np.append(elapsed_time, end_time - start_time) | |
if i % 50 == 0: | |
print('Step {}: {:4.1f}ms'.format( | |
i, (elapsed_time[-50:].mean()) * 1000)) | |
print('Throughput: {:.0f} images/s'.format(N_run * | |
batch_size / elapsed_time.sum())) | |
if __name__ == "__main__": | |
run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment