From d91c6fee893a6278d592b921e42874cf1e1b8fed Mon Sep 17 00:00:00 2001 From: rwschubert Date: Wed, 18 Mar 2020 18:38:20 +0100 Subject: [PATCH 1/2] Clean code. --- docker/dockerfile | 11 ++++++++--- install_prerequisites.sh | 10 ++++++---- src/main/deep_learning_service.py | 6 +++--- src/main/inference/base_error.py | 6 +++--- src/main/inference/base_inference_engine.py | 6 +++--- src/main/inference/errors.py | 6 +++--- src/main/inference/exceptions.py | 9 ++------- .../inference/inference_engines_factory.py | 4 ++-- .../inference/yolov3_opencv_cpu_detection.py | 6 +++--- src/main/models.py | 4 ++-- src/main/start.py | 19 ++++++------------- 11 files changed, 41 insertions(+), 46 deletions(-) diff --git a/docker/dockerfile b/docker/dockerfile index 7ac8796..dfda33e 100644 --- a/docker/dockerfile +++ b/docker/dockerfile @@ -1,13 +1,18 @@ +# Use Python 3 base image FROM python:3.6 +# Add maintainer information LABEL maintainer="antoine.charbel@inmind.ai" -COPY docker/requirements.txt . - +# Add project COPY src/main /main +# Install requirements +COPY docker/requirements.txt . RUN pip install -r requirements.txt +# Set work dir WORKDIR /main -CMD ["uvicorn", "start:app", "--host", "0.0.0.0", "--port", "7770"] +# Set init command +CMD ["uvicorn", "start:app", "--host", "0.0.0.0", "--port", "7770"] \ No newline at end of file diff --git a/install_prerequisites.sh b/install_prerequisites.sh index fc7493d..3733cd1 100644 --- a/install_prerequisites.sh +++ b/install_prerequisites.sh @@ -1,26 +1,28 @@ #!/bin/bash -# This will install docker following [https://docs.docker.com/install/linux/docker-ce/ubuntu/] +# Install Docker as described in https://docs.docker.com/install/linux/docker-ce/ubuntu/ +# Prepare environment sudo apt-get remove docker docker-engine docker.io sudo apt-get update +# Add further dependencies sudo apt-get install \ apt-transport-https \ ca-certificates \ curl \ software-properties-common +# Add Docker package repository curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add - sudo apt-key fingerprint 0EBFCD88 - sudo add-apt-repository \ "deb [arch=amd64] https://download.docker.com/linux/ubuntu \ $(lsb_release -cs) \ stable" +# Install Docker and add user sudo apt-get update sudo apt-get install -y docker-ce sudo groupadd docker sudo usermod -aG docker ${USER} -docker run hello-world - +docker run hello-world \ No newline at end of file diff --git a/src/main/deep_learning_service.py b/src/main/deep_learning_service.py index 0459d56..a025d4a 100644 --- a/src/main/deep_learning_service.py +++ b/src/main/deep_learning_service.py @@ -1,3 +1,4 @@ +# Import dependencies import os import json import uuid @@ -6,9 +7,8 @@ from inference.exceptions import ModelNotFound, InvalidModelConfiguration, ModelNotLoaded, InferenceEngineNotFound, \ InvalidInputData, ApplicationError - +# Deep learning service class class DeepLearningService: - def __init__(self): """ Sets the models base directory, and initializes some dictionaries. @@ -164,4 +164,4 @@ def get_config(self, model_name): """ if not self.model_loaded(model_name): self.load_model(model_name) - return self.models_dict[model_name].configuration + return self.models_dict[model_name].configuration \ No newline at end of file diff --git a/src/main/inference/base_error.py b/src/main/inference/base_error.py index b59f57a..f97fa8d 100644 --- a/src/main/inference/base_error.py +++ b/src/main/inference/base_error.py @@ -1,10 +1,10 @@ +# Import dependencies import logging from datetime import datetime from abc import ABC, abstractmethod - +# Abstract error class class AbstractError(ABC): - def __init__(self): """ Sets the logger file, level, and format. @@ -43,4 +43,4 @@ def error(self, message): :param message: Containing the request status and the model response :return: """ - pass + pass \ No newline at end of file diff --git a/src/main/inference/base_inference_engine.py b/src/main/inference/base_inference_engine.py index 4238c11..e04bbfe 100644 --- a/src/main/inference/base_inference_engine.py +++ b/src/main/inference/base_inference_engine.py @@ -1,9 +1,9 @@ +# Import dependencies from abc import ABC, abstractmethod from inference.exceptions import InvalidModelConfiguration, ModelNotLoaded, ApplicationError - +# Abstract inference engine class class AbstractInferenceEngine(ABC): - def __init__(self, model_path): """ Takes a model path and calls the load function. @@ -89,4 +89,4 @@ def validate_json_configuration(self, data): pass def __del__(self): - self.free() + self.free() \ No newline at end of file diff --git a/src/main/inference/errors.py b/src/main/inference/errors.py index 704b5f1..123a7d3 100644 --- a/src/main/inference/errors.py +++ b/src/main/inference/errors.py @@ -1,11 +1,11 @@ +# Import dependencies import os import logging from datetime import datetime, date from inference.base_error import AbstractError - +# Abstract error class class Error(AbstractError): - def __init__(self): if 'logs' not in os.listdir(): os.mkdir('logs') @@ -44,4 +44,4 @@ def check_date(self): b = datetime.strptime(oldest_date, '%Y-%m-%d') delta = a - b if delta.days > 365: - os.remove('logs/' + oldest_log_file) + os.remove('logs/' + oldest_log_file) \ No newline at end of file diff --git a/src/main/inference/exceptions.py b/src/main/inference/exceptions.py index 1dc4f4a..120ea63 100644 --- a/src/main/inference/exceptions.py +++ b/src/main/inference/exceptions.py @@ -1,9 +1,9 @@ +# Define meta class __metaclass__ = type class ApplicationError(Exception): """Base class for other exceptions""" - def __init__(self, default_message, additional_message=''): self.default_message = default_message self.additional_message = additional_message @@ -18,7 +18,6 @@ def get_message(self): class InvalidModelConfiguration(ApplicationError): """Raised when the model's configuration is corrupted""" - def __init__(self, additional_message=''): # super('Invalid model configuration', additional_message) super().__init__('Invalid model configuration', additional_message) @@ -26,7 +25,6 @@ def __init__(self, additional_message=''): class ModelNotFound(ApplicationError): """Raised when the model is not found""" - def __init__(self, additional_message=''): # super('Model not found', additional_message) super().__init__('Model not found', additional_message) @@ -34,7 +32,6 @@ def __init__(self, additional_message=''): class ModelNotLoaded(ApplicationError): """Raised when the model is not loaded""" - def __init__(self, additional_message=''): # super('Error loading model', additional_message) super().__init__('Error loading model', additional_message) @@ -42,7 +39,6 @@ def __init__(self, additional_message=''): class InvalidInputData(ApplicationError): """Raised when the input data is corrupted""" - def __init__(self, additional_message=''): # super('Invalid input data', additional_message) super().__init__('Invalid input data', additional_message) @@ -50,7 +46,6 @@ def __init__(self, additional_message=''): class InferenceEngineNotFound(ApplicationError): """Raised when the Inference Engine is not found""" - def __init__(self, additional_message=''): # super('Inference engine not found', additional_message) - super().__init__('Inference engine not found', additional_message) + super().__init__('Inference engine not found', additional_message) \ No newline at end of file diff --git a/src/main/inference/inference_engines_factory.py b/src/main/inference/inference_engines_factory.py index e09e114..30a8e9e 100644 --- a/src/main/inference/inference_engines_factory.py +++ b/src/main/inference/inference_engines_factory.py @@ -1,10 +1,10 @@ +# Import dependencies import os import json from inference.exceptions import ModelNotFound, ApplicationError, InvalidModelConfiguration, InferenceEngineNotFound, ModelNotLoaded - +# Inference engine factory class class InferenceEngineFactory: - @staticmethod def get_engine(path_to_model): """ diff --git a/src/main/inference/yolov3_opencv_cpu_detection.py b/src/main/inference/yolov3_opencv_cpu_detection.py index 6459a71..847b77c 100644 --- a/src/main/inference/yolov3_opencv_cpu_detection.py +++ b/src/main/inference/yolov3_opencv_cpu_detection.py @@ -1,3 +1,4 @@ +# Import dependencies import os import cv2 import uuid @@ -9,9 +10,8 @@ from inference.base_inference_engine import AbstractInferenceEngine from inference.exceptions import InvalidModelConfiguration, InvalidInputData, ApplicationError - +# Inference engine class class InferenceEngine(AbstractInferenceEngine): - def __init__(self, model_path): self.net = None self.scale = None @@ -234,4 +234,4 @@ def validate_json_configuration(self, data): try: jsonschema.validate(data, schema) except Exception as e: - raise InvalidModelConfiguration(e) + raise InvalidModelConfiguration(e) \ No newline at end of file diff --git a/src/main/models.py b/src/main/models.py index cc58851..2ca163a 100644 --- a/src/main/models.py +++ b/src/main/models.py @@ -1,5 +1,5 @@ +# API response class class ApiResponse: - def __init__(self, success=True, data=None, error=None): """ Defines the response shape @@ -9,4 +9,4 @@ def __init__(self, success=True, data=None, error=None): """ self.data = data self.error = error.get_message() if error is not None else '' - self.success = success + self.success = success \ No newline at end of file diff --git a/src/main/start.py b/src/main/start.py index 48bf7e0..2e9c2a8 100644 --- a/src/main/start.py +++ b/src/main/start.py @@ -1,3 +1,4 @@ +# Import dependencies import sys from starlette.responses import FileResponse from models import ApiResponse @@ -10,28 +11,20 @@ InferenceEngineNotFound, InvalidInputData from inference.errors import Error +# Append path sys.path.append('./inference') +# Init deep learning service dl_service = DeepLearningService() error_logging = Error() -app = FastAPI(version='3.1.0', title='BMW InnovationLab YOLOv3 opencv inference Automation', +app = FastAPI(version='3.1.0', title='BMW InnovationLab YOLOv3 OpenCV Inference Automation', description="API for performing YOLOv3 opencv inference

" "Contact the developers:
" "Antoine Charbel: antoine.charbel@inmind.ai
" "BMW Innovation Lab: innovation-lab@bmw.de") -# app.mount("/public", StaticFiles(directory="/main/public"), name="public") - -# app.add_middleware( -# CORSMiddleware, -# allow_origins=["*"], -# allow_credentials=True, -# allow_methods=["*"], -# allow_headers=["*"], -# ) - - +# Load app @app.get('/load') def load_custom(): """ @@ -189,4 +182,4 @@ async def list_model_config(model_name: str): :return: List of model's configuration """ config = dl_service.get_config(model_name) - return ApiResponse(data=config) + return ApiResponse(data=config) \ No newline at end of file From 283683b4105a28e32c4eaf687a6df0da4b8a0c17 Mon Sep 17 00:00:00 2001 From: rwschubert Date: Wed, 18 Mar 2020 18:48:40 +0100 Subject: [PATCH 2/2] Change Python base image. --- docker/{dockerfile => Dockerfile} | 2 +- src/main/start.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) rename docker/{dockerfile => Dockerfile} (95%) diff --git a/docker/dockerfile b/docker/Dockerfile similarity index 95% rename from docker/dockerfile rename to docker/Dockerfile index dfda33e..0c41fa3 100644 --- a/docker/dockerfile +++ b/docker/Dockerfile @@ -1,5 +1,5 @@ # Use Python 3 base image -FROM python:3.6 +FROM python:3 # Add maintainer information LABEL maintainer="antoine.charbel@inmind.ai" diff --git a/src/main/start.py b/src/main/start.py index 2e9c2a8..a8b67ed 100644 --- a/src/main/start.py +++ b/src/main/start.py @@ -18,7 +18,7 @@ dl_service = DeepLearningService() error_logging = Error() app = FastAPI(version='3.1.0', title='BMW InnovationLab YOLOv3 OpenCV Inference Automation', - description="API for performing YOLOv3 opencv inference

" + description="API for YOLOv3 OpenCV Inference

" "Contact the developers:
" "Antoine Charbel: antoine.charbel@inmind.ai
" "BMW Innovation Lab: innovation-lab@bmw.de")