Spaces:
Runtime error
Runtime error
add model
Browse filesFiles changed (13)
- DockerfileAdded+18
- app.pyAdded+78
- build_image.shAdded+8
- config_parser.pyAdded+86
- configtest.jsonAdded+100
- demo_full_yvesrocher.jsonAdded+100
- demo_yvesrocher.jsonAdded+55
- docker-compose.yamlAdded+13
- inferencer.pyAdded+136
- liccheck.iniAdded+43
- poetry.lockAdded
- pyproject.tomlAdded+66
- requirements.txtAdded
123456789101112131415161718FROM python:3.8.13-bullseye
LABEL author="Bastien GUILLAUME"LABEL version="0.0.2"
RUN apt-get update \ && apt-get upgrade -y \ && apt-get install ffmpeg libsm6 libxext6 -y
COPY requirements.txt /opt/app/WORKDIR /opt/appRUN pip install -r requirements.txt
COPY inference.py /opt/app/
CMD ["python", "inference.py"]EXPOSE 80123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778# pylint: disable=no-name-in-module# pylint: disable=no-member
"""Author : Bastien GUILLAUMEVersion : 0.0.1Date : 2023-03-16
Title : Inference With Gradio running an onnxruntime backend"""
import os
import gradio as grfrom config_parser import *from inferencer import *
gr.close_all()
def infertace_builder(): interface_list = [] logging.log(level=logging.INFO, msg=f"Building Interfaces") for task_number in range(0, len(tasks)): logging.log(level=logging.INFO, msg=f"Building Interface n° {task_number}") interface_list.append( gr.Interface( title=tasks[task_number], description=tasks_description[task_number], fn=analysis, # if tasks_name[task_number] == "Étiquetage" # else corck_screwing_analysis, allow_flagging="never", inputs=[ gr.Textbox( value=f"task{task_number+1}", visible=False, interactive=False ), gr.Dropdown( tasks_products[task_number], type="value", value=tasks_products[task_number][0], label="Choix", visible=True if len(tasks_products[task_number]) > 1 else False, info="Sur quel type de produit, voulez vous lancer l'analyse ?", ), gr.Image( label="Image à analyser", shape=None, image_mode="RGB", invert_colors=False, source="upload", tool="editor", type="numpy", ), ], outputs=gr.Label( label="Résultats", ), ) ) logging.log(level=logging.INFO, msg=f"Interfaces ready\n") return interface_list
generated_interface = infertace_builder()
iface_generated = gr.TabbedInterface( interface_list=generated_interface, tab_names=tasks_name, title=title,)
if __name__ == "__main__": logging.log(level=logging.INFO, msg="Starting the Gradio server...") iface_generated.launch( server_name="0.0.0.0", server_port=int(os.getenv("PORT", "8150")) ) logging.log(level=logging.INFO, msg="Stopping the Gradio server...")12345678#!/bin/bash -eimage_name=gcr.io/tough-variety-310920/openvino_inferenceimage_tag=1.0.1full_image_name=${image_name}:${image_tag}
cd "$(dirname "$0")" docker build -t "${full_image_name}" .docker push "$full_image_name"1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586# pylint: disable=no-name-in-module# pylint: disable=no-member
"""Author : Bastien GUILLAUMEVersion : 0.0.1Date : 2023-03-16
Title : Inference With Gradio running an onnxruntime backend"""
import jsonimport loggingimport osfrom functools import reduce
logging.basicConfig( format="%(asctime)s.%(msecs)03d %(levelname)-8s %(message)s", level=logging.INFO, datefmt="%Y%m%d-%H%M%S",)
with open("demo_yvesrocher.json") as config_file: config = json.load(config_file)
logging.log(level=logging.DEBUG, msg=f"Loaded config file : {json.dumps(config)}")
def deep_get(dictionary, keys, default=None): return reduce( lambda d, key: d.get(key, default) if isinstance(d, dict) else default, keys.split("."), dictionary, )
def get_tasks(): tasks = [] for task in config["tasks"].keys(): tasks.append(config["tasks"][task]["name"]["fr"]) return tasks
def get_tasks_name(): tasks_name = [] for task in config["tasks"].keys(): tasks_name.append(config["tasks"][task]["shortname"]) return tasks_name
def get_tasks_description(): tasks_descripion = [] for task in config["tasks"].keys(): tasks_descripion.append(config["tasks"][task]["description"]["fr"]) return tasks_descripion
def get_tasks_products(): tasks_products = [] for task in config["tasks"].keys(): tasks_products.append(config["tasks"][task]["products"]) return tasks_products
title = ( os.getenv("GRADIO_TITLE") if "GRADIO_TITLE" in os.environ else config.get("title", "TITLE neither set in config file nor in ENV"))description = ( os.getenv("GRADIO_DESCRIPTION") if "GRADIO_DESCRIPTION" in os.environ else config.get("description", "DESCRIPTION neither set in config file nor in ENV"))tasks = get_tasks()tasks_name = get_tasks_name()tasks_description = get_tasks_description()tasks_products = get_tasks_products()logging.log(level=logging.INFO, msg=f"Parsed Data :")logging.log(level=logging.INFO, msg=f"Title : {title}")logging.log(level=logging.INFO, msg=f"Description : {description}")logging.log(level=logging.INFO, msg=f"Tasks : {tasks}")logging.log(level=logging.INFO, msg=f"Tasks name : {tasks_name}")logging.log(level=logging.INFO, msg=f"Tasks descrption : {tasks_description}")logging.log(level=logging.INFO, msg=f"Tasks products : {tasks_products}")logging.log(level=logging.INFO, msg=f"End of Parsed Data\n")123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100{ "title": "YVES ROCHER", "description": "Démonstration des algos de reconnaissance d'étiquetage/bouchage correct", "tasks": { "task1": { "shortname": "Étiquetage", "name": { "en": "Quality Control of Labels", "fr": "Contrôle de l'Étiquetage" }, "description": { "en": "", "fr": "Est-ce que l'étiquette est bien positionnée" }, "products":[ "497 Pure Algue 200ml", "505 Pure Calmille 200ml", "614 Eco Douche 100ml", "648 Hair Care 300ml" ], "models": { "497 Pure Algue 200ml": { "type": "classification", "path": "https://automi-test-models.s3.eu-west-3.amazonaws.com/inference-pipeline/po2xfZzK0KtWZUpXrFjZ/model.onnx", "input_shape": 224, "mean": [0.485, 0.456, 0.406], "std": [0.229, 0.224, 0.225], "class_names": ["Étiquetage incorrect", "Étiquetage correct"] }, "505 Pure Calmille 200ml": { "type": "classification", "path": "https://automi-test-models.s3.eu-west-3.amazonaws.com/inference-pipeline/po2xfZzK0KtWZUpXrFjZ/model.onnx", "input_shape": 224, "mean": [0.485, 0.456, 0.406], "std": [0.229, 0.224, 0.225], "class_names": ["Étiquetage incorrect", "Étiquetage correct"] }, "614 Eco Douche 100ml": { "type": "classification", "path": "https://automi-test-models.s3.eu-west-3.amazonaws.com/inference-pipeline/po2xfZzK0KtWZUpXrFjZ/model.onnx", "input_shape": 224, "mean": [0.485, 0.456, 0.406], "std": [0.229, 0.224, 0.225], "class_names": ["Étiquetage incorrect", "Étiquetage correct"] }, "648 Hair Care 300ml": { "type": "classification", "path": "https://automi-test-models.s3.eu-west-3.amazonaws.com/inference-pipeline/po2xfZzK0KtWZUpXrFjZ/model.onnx", "input_shape": 224, "mean": [0.485, 0.456, 0.406], "std": [0.229, 0.224, 0.225], "class_names": ["Étiquetage incorrect", "Étiquetage correct"] } } }, "task2": { "shortname": "Bouchage", "name": { "en": "Quality Control of Corck Screwing", "fr": "Contrôle du Bouchage" }, "description": { "en": "", "fr": "Est-ce que le bouchon est bien positionné et entièrement vissé" }, "products":[ "187 Hamamelis 300ml", "550 SVC 300ml", "600 PN 500 ml" ], "models": { "187 Hamamelis 300ml":{ "type": "classification", "path": "/Users/bastien/Downloads/model_corck_screwing.onnx", "input_shape": 256, "mean": [0.485, 0.456, 0.406], "std": [0.229, 0.224, 0.225], "class_names": ["Bouchage incorrect", "Bouchage correct"] }, "550 SVC 300ml": { "type": "classification", "path": "/Users/bastien/Downloads/model_corck_screwing.onnx", "input_shape": 256, "mean": [0.485, 0.456, 0.406], "std": [0.229, 0.224, 0.225], "class_names": ["Bouchage incorrect", "Bouchage correct"] }, "600 PN 500 ml": { "type": "classification", "path": "/Users/bastien/Downloads/model_corck_screwing.onnx", "input_shape": 256, "mean": [0.485, 0.456, 0.406], "std": [0.229, 0.224, 0.225], "class_names": ["Bouchage incorrect", "Bouchage correct"] } } } }}
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100{ "title": "YVES ROCHER", "description": "Démonstration des algos de reconnaissance d'étiquetage/bouchage correct", "tasks": { "task1": { "shortname": "Étiquetage", "name": { "en": "Quality Control of Labels", "fr": "Contrôle de l'Étiquetage" }, "description": { "en": "Is the label in the right position ?", "fr": "Est-ce que l'étiquette est bien positionnée ?" }, "products":[ "497 Pure Algue 200ml", "505 Pure Calmille 200ml", "614 Eco Douche 100ml", "648 Hair Care 300ml" ], "models": { "497 Pure Algue 200ml": { "type": "classification", "path": "https://automi-test-models.s3.eu-west-3.amazonaws.com/inference-pipeline/po2xfZzK0KtWZUpXrFjZ/model.onnx", "input_shape": 224, "mean": [0.485, 0.456, 0.406], "std": [0.229, 0.224, 0.225], "class_names": ["Étiquetage correct", "Étiquetage incorrect"] }, "505 Pure Calmille 200ml": { "type": "classification", "path": "https://automi-test-models.s3.eu-west-3.amazonaws.com/inference-pipeline/po2xfZzK0KtWZUpXrFjZ/model.onnx", "input_shape": 224, "mean": [0.485, 0.456, 0.406], "std": [0.229, 0.224, 0.225], "class_names": ["Étiquetage correct", "Étiquetage incorrect"] }, "614 Eco Douche 100ml": { "type": "classification", "path": "https://automi-test-models.s3.eu-west-3.amazonaws.com/inference-pipeline/po2xfZzK0KtWZUpXrFjZ/model.onnx", "input_shape": 224, "mean": [0.485, 0.456, 0.406], "std": [0.229, 0.224, 0.225], "class_names": ["Étiquetage correct", "Étiquetage incorrect"] }, "648 Hair Care 300ml": { "type": "classification", "path": "https://automi-test-models.s3.eu-west-3.amazonaws.com/inference-pipeline/po2xfZzK0KtWZUpXrFjZ/model.onnx", "input_shape": 224, "mean": [0.485, 0.456, 0.406], "std": [0.229, 0.224, 0.225], "class_names": ["Étiquetage correct", "Étiquetage incorrect"] } } }, "task2": { "shortname": "Bouchage", "name": { "en": "Quality Control of Corck Screwing", "fr": "Contrôle du Bouchage" }, "description": { "en": "Is the corck in the right position ?", "fr": "Est-ce que le bouchon est bien positionné et entièrement vissé ?" }, "products":[ "187 Hamamelis 300ml", "550 SVC 300ml", "600 PN 500 ml" ], "models": { "187 Hamamelis 300ml":{ "type": "classification", "path": "https://automi-test-models.s3.eu-west-3.amazonaws.com/inference-pipeline/Qp6BRHBcLq7KKxqCWqmV/model.onnx", "input_shape": 224, "mean": [0.485, 0.456, 0.406], "std": [0.229, 0.224, 0.225], "class_names": ["Bouchage correct", "Bouchage incorrect"] }, "550 SVC 300ml": { "type": "classification", "path": "/Users/bastien/Downloads/model_corck_screwing.onnx", "input_shape": 256, "mean": [0.485, 0.456, 0.406], "std": [0.229, 0.224, 0.225], "class_names": ["Bouchage correct", "Bouchage incorrect"] }, "600 PN 500 ml": { "type": "classification", "path": "/Users/bastien/Downloads/model_corck_screwing.onnx", "input_shape": 256, "mean": [0.485, 0.456, 0.406], "std": [0.229, 0.224, 0.225], "class_names": ["Bouchage correct", "Bouchage incorrect"] } } } }}
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455{ "title": "YVES ROCHER", "description": "Démonstration des algos de reconnaissance d'étiquetage/bouchage correct", "tasks": { "task1": { "shortname": "Étiquetage", "name": { "en": "Quality Control of Labels", "fr": "Contrôle de l'Étiquetage" }, "description": { "en": "Is the label in the right position ?", "fr": "Est-ce que l'étiquette est bien positionnée ?" }, "products":[ "505 Pure Calmille 200ml" ], "models": { "505 Pure Calmille 200ml": { "type": "classification", "path": "https://automi-test-models.s3.eu-west-3.amazonaws.com/inference-pipeline/dmeQ6Mae0HKDwkHBTdh2/model.onnx", "input_shape": 224, "mean": [0.485, 0.456, 0.406], "std": [0.229, 0.224, 0.225], "class_names": ["Étiquetage correct", "Étiquetage incorrect"] } } }, "task2": { "shortname": "Bouchage", "name": { "en": "Quality Control of Corck Screwing", "fr": "Contrôle du Bouchage" }, "description": { "en": "Is the corck in the right position ?", "fr": "Est-ce que le bouchon est bien positionné et entièrement vissé ?" }, "products":[ "187 Hamamelis 300ml" ], "models": { "187 Hamamelis 300ml":{ "type": "classification", "path": "https://automi-test-models.s3.eu-west-3.amazonaws.com/inference-pipeline/3nkoTiBxDNBzqyGtE5w8/model.onnx", "input_shape": 224, "mean": [0.485, 0.456, 0.406], "std": [0.229, 0.224, 0.225], "class_names": ["Bouchage correct", "Bouchage incorrect"] } } } }}
12345678910111213version: '3.8'
services: magnetoscopie: image: openvino_inference:1.0.1 container_name: gradio_showcase_onnx_model volumes: - ./models:/opt/app/models ports: - "8150:80" env_file: - .env restart: unless-stopped123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136# pylint: disable=no-name-in-module# pylint: disable=no-member
"""Author : Bastien GUILLAUMEVersion : 0.0.1Date : 2023-03-16
Title : Inference With Gradio running an onnxruntime backend"""
import numpy as npimport onnxruntime as ortimport requestsfrom config_parser import *from torchvision import transforms
corck_screwing_metadata = { "image_threshold": 0.9247307181358337, "pixel_threshold": 0.9247307181358337, "min": 4.034907666260021e-26, "max": 0.998478353023529,}
inferencer_arr = {}logging.log(level=logging.INFO, msg="Loading models...")for task in config["tasks"].keys(): inferencer_arr[task] = {} r = None for model in config["tasks"][task]["models"]: product_name = model model_path = config["tasks"][task]["models"][product_name]["path"] logging.log( level=logging.INFO, msg=f"Loading labeling model for product {product_name}", ) inferencer_arr[task][product_name] = {} if model_path.startswith("http"): r = requests.get(model_path, stream=True).content inferencer_arr[task][product_name][product_name] = ort.InferenceSession( r if model_path.startswith("http") else model_path ) inferencer_arr[task][product_name]["input_name"] = ( inferencer_arr[task][product_name][product_name].get_inputs()[0].name ) inferencer_arr[task][product_name]["output_name"] = ( inferencer_arr[task][product_name][product_name].get_outputs()[0].name ) logging.log(level=logging.INFO, msg=f"Model for {product_name} loaded\n")logging.log(level=logging.INFO, msg="All models loaded...\n\n")logging.log(level=logging.DEBUG, msg=f"Inferencer Array : {inferencer_arr}")
def softmax(x): e_x = np.exp(x - np.max(x)) return e_x / e_x.sum(axis=0)
def is_anomalous_classification(prediction, meta_data): pred_label = None pred_score = prediction.reshape(-1).max() if "image_threshold" in meta_data: pred_label = ( config["tasks"][task]["models"][model]["class_names"][0] if (pred_score >= meta_data["image_threshold"]) else config["tasks"][task]["models"][model]["class_names"][1] ) return pred_label, pred_score
def analysis(task, use_case, image): """ Main function that process inference and return results strings Args: - task - use case - image Returns: - String including label and confidence of the model """
input_image = pre_process_all(task, use_case, image) result = inference(task, use_case, input_image) logging.log(level=logging.DEBUG, msg=result) return result
def pre_process_all(task, use_case, image): preprocessed_image = [] input_shape = config["tasks"][task]["models"][use_case]["input_shape"] mean = config["tasks"][task]["models"][use_case]["mean"] std = config["tasks"][task]["models"][use_case]["std"] logging.log(level=logging.DEBUG, msg=f"Shape {input_shape}") logging.log(level=logging.DEBUG, msg=f"Mean {mean}") logging.log(level=logging.DEBUG, msg=f"Std {std}") data_transforms = transforms.Compose( [ transforms.ToPILImage(), transforms.Resize(input_shape), transforms.CenterCrop(input_shape), transforms.ToTensor(), transforms.Normalize(mean, std), ] ) preprocessed_image = data_transforms(image).detach().numpy() preprocessed_image = np.expand_dims(preprocessed_image, axis=0) logging.log(level=logging.DEBUG, msg=preprocessed_image) return preprocessed_image
def inference(task, model, input_image): """ Process inference for bottle labels Args: - task - use_case (product) - image Returns: - String including label and confidence of the model """
prediction = inferencer_arr[task][model][model].run( [inferencer_arr[task][model]["output_name"]], {inferencer_arr[task][model]["input_name"]: input_image}, ) prediction = prediction[0].squeeze() logging.log(level=logging.INFO, msg=f"Softmaxed prediction {softmax(prediction)}") if config["tasks"][task]["models"][model]["type"] == "classification": result = f"{config['tasks'][task]['models'][model]['class_names'][np.argmax(prediction)]} avec une confiance de {str(round(softmax(prediction)[np.argmax(prediction)]*100))} %" elif task == "task2": label, score = is_anomalous_classification(prediction, corck_screwing_metadata) result = f"{label} avec une confiance de {str(round(score*100))} %" else: result = "Houston, we've got a problem" logging.log(level=logging.DEBUG, msg=result) return result12345678910111213141516171819202122232425262728293031323334353637383940414243# Authorized and unauthorized licenses in LOWER CASE[Licenses]authorized_licenses: bsd new bsd bsd license new bsd license simplified bsd apache apache 2.0 apache software license gnu lgpl lgpl with exceptions or zpl isc license isc license (iscl) mit mit license python software foundation python software foundation license zpl 2.1 historical permission notice and disclaimer
unauthorized_licenses: gpl v3
[Authorized Packages]aiohttp: 3.8.1aiosignal: 1.2.0async-timeout: 4.0.2bcrypt: 3.2.0certifi: 2021.10.8frozenlist: 1.3.0gradio: 2.9.1monotonic: 1.6multidict: 6.0.2openvino: 2022.1.0paramiko: 2.10.3Pillow: 9.1.0PyNaCl: 1.5.0python-multipart: 0.0.5requests: 2.27.1yarl: 1.7.2
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566[tool.poetry]name = "Model Demo with Gradio"version = "0.0.1"description = ""authors = ["bastien@automi.ai"]
[tool.poetry.dependencies]python = "^3.8, <3.11"gradio = "^3.23.0"logging = "^0.4.9.6"numpy = "^1.24.2"onnxruntime = "^1.14.1"requests = "^2.28.2"torchvision = "^0.15.1"
[tool.poetry.group.dev.dependencies]black = "^23.1.0"liccheck = "^0.8.3"pylint = "^2.17.0"pytest = "^7.2.2"
[build-system]requires = ["poetry-core>=1.0.0"]build-backend = "poetry.core.masonry.api"
#[tool.liccheck]#authorized_licenses = [# "bsd",# "new bsd",# "bsd license",# "new bsd license",# "simplified bsd",# "apache",# "apache 2.0",# "apache software license",# "gnu lgpl",# "lgpl with exceptions or zpl",# "isc license",# "isc license (iscl)",# "mit",# "mit license",# "python software foundation",# "python software foundation license",# "zpl 2.1",# "historical permission notice and disclaimer",# "gpl v3"#]#unauthorized_licenses = [""]#authorized_packages = [# "aiohttp: *",# "aiosignal: *",# "async-timeout: *",# "bcrypt: *",# "certifi: *",# "frozenlist: *",# "gradio: *",# "monotonic: *",# "multidict: *",# "openvino: *",# "paramiko: *",# "Pillow: *",# "PyNaCl: *",# "python-multipart: *",# "requests: *",# "yarl: *"#]DockerfileAdded+18
app.pyAdded+78
build_image.shAdded+8
config_parser.pyAdded+86
configtest.jsonAdded+100
demo_full_yvesrocher.jsonAdded+100
demo_yvesrocher.jsonAdded+55
docker-compose.yamlAdded+13
inferencer.pyAdded+136
liccheck.iniAdded+43
poetry.lockAdded
pyproject.tomlAdded+66
requirements.txtAdded