<jupyter_start><jupyter_text>Load test image<jupyter_code>img_path = "../dataset_test/brad.jpg"<jupyter_output><empty_output><jupyter_text>Load labels  ImageNet: 1001<jupyter_code>with open("../dataset_test/ImageNetLabels.txt") as f:
    labels_imagenet = np.asarray(f.readlines())
    labels_imagenet = [x.replace("\n", "") for x in labels_imagenet]<jupyter_output><empty_output><jupyter_text>imagenet21k_wordnet_lemmas<jupyter_code>with open("../dataset_test/imagenet21k_wordnet_lemmas.txt") as f:
    labels_imagenet21k = np.asarray(f.readlines())
    labels_imagenet21k = [x.replace("\n", "") for x in labels_imagenet21k]<jupyter_output><empty_output><jupyter_text>Test Models **Path to downloaded models:** /tmp/tfhub_modules/  mobilenet_v2<jupyter_code>m = tf.keras.Sequential([
    hub.KerasLayer("https://tfhub.dev/google/tf2-preview/mobilenet_v2/classification/4", output_shape=[1001])
])
m.build([None, 224, 224, 3])  # Batch input shape.

normalization_layer = tf.keras.layers.experimental.preprocessing.Rescaling(1./255)
# Transform the image
#image = tf.keras.preprocessing.image.load_img(img_path)
image_pil = Image.open(img_path)
image = np.array(image_pil)
image = tf.image.resize(image, [224, 224])
image = normalization_layer(image)
image = tf.expand_dims(image, axis=0)
# Predict
prediction = m(image).numpy()[0]
# Map with labels
labels_mapped = np.column_stack([labels_imagenet, prediction])
labels_mapped_high = [[x, y] for (x, y) in labels_mapped if np.float(y) > 5]
# 
labels_mapped_high
prediction = m(image).numpy()
prediction[0]
from keras.applications import imagenet_utils
P = imagenet_utils.decode_predictions(prediction[:,:1000], 10)
P
model = tf.keras.applications.vgg16.VGG16(weights='imagenet', include_top=True)
image_pil = Image.open(img_path)
image = np.array(image_pil)
image = tf.image.resize(image, [224, 224])

x = np.expand_dims(np.copy(image), axis=0)
x = tf.keras.applications.vgg16.preprocess_input(x)

features = model.predict(x)

features.shape
tf.keras.applications.vgg16.decode_predictions(features)
from tensorflow.keras.applications.resnet50 import ResNet50
from tensorflow.keras.preprocessing import image
from tensorflow.keras.applications.resnet50 import preprocess_input, decode_predictions
import numpy as np

model = ResNet50(weights='imagenet')

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:', decode_predictions(preds, top=3)[0])
from tensorflow.keras.applications.xception import Xception
from tensorflow.keras.preprocessing import image
from tensorflow.keras.applications.xception import preprocess_input, decode_predictions
import numpy as np

model = Xception(weights='imagenet')

img = image.load_img(img_path, target_size=(299, 299))
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:', decode_predictions(preds, top=3)[0])
from tensorflow.keras.applications import NASNetLarge
from tensorflow.keras.preprocessing import image
from tensorflow.keras.applications.xception import preprocess_input, decode_predictions
import numpy as np

model = NASNetLarge(weights='imagenet')

img = image.load_img(img_path, target_size=(331, 331))
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)
pred = decode_predictions(preds, top=3)[0]
print('Predicted:', pred)
np.asarray(pred)[:,1:3]<jupyter_output><empty_output><jupyter_text>Big Transfer (BiT) - m-r152x4 https://tfhub.dev/google/bit/m-r152x4/imagenet21k_classification/1<jupyter_code>module = hub.KerasLayer("https://tfhub.dev/google/bit/m-r152x4/imagenet21k_classification/1")
image_pil = Image.open(img_path)
image = np.array(image_pil)

image = normalization_layer(image)
images = tf.expand_dims(image, axis=0)

logits = module(images)[0]  # Logits with shape [batch_size, 21843].
probabilities = tf.nn.sigmoid(logits)
labels_mapped = np.column_stack([labels_imagenet21k, probabilities[0]])
labels_mapped
labels_mapped[:,1]
probabilities_no_batch = probabilities[0]
np.argmax(probabilities_no_batch)
labels_mapped[10135]
from keras.applications import imagenet_utils
P = imagenet_utils.decode_predictions(probabilities)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12404255319148937, 'fraction_numerical': 0.02893617021276596, 'mean_word_length': 7.049657534246576, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 22, 'https://': 3, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '187460', 'license': 'Apache-2.0', 'n_tokens_mistral': 1767, 'n_tokens_neox': 1687, 'n_words': 324, 'path': 'tests/notebooks/Tensorflow Hub - Classification.ipynb', 'repo_name': 'houseofai/readyml', 'size': '4700'}