Saturday, 16 January 2021

keras 11 mobileNet 2


mobileNet prediction
[[('n02510455', 'giant_panda', 0.9997156), 
('n02509815', 'lesser_panda', 0.00016485745), 
('n02447366', 'badger', 8.6640284e-05), 
('n02445715', 'skunk', 1.2499775e-05),
 ('n02443114', 'polecat', 1.1246146e-05)]]
#mobileNet.py
import  tensorflow as tf
from tensorflow import keras
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Activation, Dense, Flatten, Conv2D, MaxPool2D, Dropout
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.metrics import categorical_crossentropy
from tensorflow.keras.preprocessing import image
from tensorflow.keras.applications import imagenet_utils
from sklearn.metrics import confusion_matrix
import itertools
import shutil
import random
import os
import glob
import matplotlib.pyplot as plt
from tensorflow.keras.models import load_model
import numpy as np

#train on GPU
pysical_devices = tf.config.experimental.list_physical_devices('GPU')
#print("Num GPUs Available: ", len(pysical_devices))
tf.config.experimental.set_memory_growth(pysical_devices[0], True)

mobile = tf.keras.applications.mobilenet.MobileNet()

def prepare_image(file):
    img = image.load_img('data/' + file, target_size=(224, 224))
    img_array = image.img_to_array(img)
    img_array_expanded_dims = np.expand_dims(img_array, axis=0)
    return tf.keras.applications.mobilenet.preprocess_input(img_array_expanded_dims)

preprocessed_image = prepare_image('panda.jpg')
predictions = mobile.predict(preprocessed_image)
results = imagenet_utils.decode_predictions(predictions)
print(results)

reference:

No comments:

Post a Comment