# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# # Work in Progress
#
# ### End-to-end a pretrained/prebuilt ImageNet model from a github repo into `scivision`
#
# Provides an example of end-to-end pipeling from the following test repo as a scivision Model:
# https://github.com/quantumjot/scivision-test-plugin
#    
# The code calls the repo from the catalog and run end-to-end example using ImageNet pretrained/prebuilt models.

# ## Load scivision

from scivision import catalog
from scivision.io import load_pretrained_model

# ## Catalog

my_query = {
    "institution": "alan-turing-institute", 
    "domain": "computer-vision", 
    "task": "object-detection",
    "model": 'https://github.com/quantumjot/scivision-test-plugin'
}

queryR = catalog.query(my_query)

target = queryR[0]

target

# ## Model settings and config

# settings
config_path = target['model'] + "/" + target['scivision_yaml_filename']
inputs = target['datasource']

model = load_pretrained_model(config_path, allow_install=True)

model

# ## Load input image

# +
# libraries
from skimage.io import imread
from skimage.transform import resize
from tensorflow.keras.applications.imagenet_utils import decode_predictions

import matplotlib.pyplot as plt
# -

x = imread(inputs)
X = resize(x, (224, 224)) * 255  # cast back to 0-255 range

# ## Model predictions

y = model.predict(X)


def get_imagenet_label(probs):
    return decode_predictions(probs, top=1)[0][0]


plt.figure()
plt.imshow(x)
_, image_class, class_confidence = get_imagenet_label(y)
plt.title("{} : {:.2f}%".format(image_class, class_confidence * 100))
plt.show()



StarcoderdataJupyterScriptsDedupFiltered