├── .bandit ├── .dockerignore ├── .gitignore ├── .travis.yml ├── Dockerfile ├── LICENSE ├── README.md ├── api ├── __init__.py ├── metadata.py └── predict.py ├── app.py ├── config.py ├── core ├── __init__.py └── model.py ├── docs ├── deploy-max-to-ibm-cloud-with-kubernetes-button.png └── swagger-screenshot.png ├── max-scene-classifier.yaml ├── requirements-test.txt ├── requirements.txt ├── samples ├── README.md ├── aquarium.jpg ├── bakery.jpg ├── city.jpg ├── paris.jpg └── ruins.jpg ├── sha512sums.txt └── tests ├── bakery.jpg ├── bakery.png └── test.py /.bandit: -------------------------------------------------------------------------------- 1 | [bandit] 2 | exclude: /tests,/training 3 | -------------------------------------------------------------------------------- /.dockerignore: -------------------------------------------------------------------------------- 1 | README.* 2 | .idea/ 3 | .git/ 4 | .gitignore 5 | tests/ 6 | .pytest_cache 7 | samples/ 8 | docs/ 9 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | .idea/ 3 | __pycache__/ 4 | *.py[cod] 5 | *$py.class 6 | 7 | # C extensions 8 | *.so 9 | 10 | # Distribution / packaging 11 | .Python 12 | env/ 13 | build/ 14 | develop-eggs/ 15 | dist/ 16 | downloads/ 17 | eggs/ 18 | .eggs/ 19 | lib/ 20 | lib64/ 21 | parts/ 22 | sdist/ 23 | var/ 24 | wheels/ 25 | *.egg-info/ 26 | .installed.cfg 27 | *.egg 28 | 29 | # PyInstaller 30 | # Usually these files are written by a python script from a template 31 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 32 | *.manifest 33 | *.spec 34 | 35 | # Installer logs 36 | pip-log.txt 37 | pip-delete-this-directory.txt 38 | 39 | # Unit test / coverage reports 40 | htmlcov/ 41 | .tox/ 42 | .coverage 43 | .coverage.* 44 | .cache 45 | nosetests.xml 46 | coverage.xml 47 | *.cover 48 | .hypothesis/ 49 | 50 | # Translations 51 | *.mo 52 | *.pot 53 | 54 | # Django stuff: 55 | *.log 56 | local_settings.py 57 | 58 | # Flask stuff: 59 | instance/ 60 | .webassets-cache 61 | 62 | # Scrapy stuff: 63 | .scrapy 64 | 65 | # Sphinx documentation 66 | docs/_build/ 67 | 68 | # PyBuilder 69 | target/ 70 | 71 | # Jupyter Notebook 72 | .ipynb_checkpoints 73 | 74 | # pyenv 75 | .python-version 76 | 77 | # celery beat schedule file 78 | celerybeat-schedule 79 | 80 | # SageMath parsed files 81 | *.sage.py 82 | 83 | # dotenv 84 | .env 85 | 86 | # virtualenv 87 | .venv 88 | venv/ 89 | ENV/ 90 | 91 | # Spyder project settings 92 | .spyderproject 93 | .spyproject 94 | 95 | # Rope project settings 96 | .ropeproject 97 | 98 | # mkdocs documentation 99 | /site 100 | 101 | # mypy 102 | .mypy_cache/ 103 | /assets/vggish_pca_params.npz 104 | /assets/vggish_model.ckpt 105 | 106 | /deeplabv3_pascal_trainval_2018_01_04.tar.gz 107 | /.pytest_cache/ 108 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: python 2 | python: 3 | - 3.6 4 | 5 | services: 6 | - docker 7 | 8 | install: 9 | - docker build -t max-scene-classifier . 10 | - docker run -it -d -p 5000:5000 max-scene-classifier 11 | 12 | before_script: 13 | - pip install -r requirements-test.txt 14 | - bandit -r . 15 | - sleep 30 16 | 17 | script: 18 | - flake8 . --max-line-length=127 19 | - pytest tests/test.py 20 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2018-2019 IBM Corp. All Rights Reserved. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # 16 | 17 | FROM quay.io/codait/max-base:v1.1.3 18 | 19 | # Upgrade packages to meet security criteria 20 | RUN apt-get update && apt-get upgrade -y && rm -rf /var/lib/apt/lists/* 21 | 22 | ARG model_bucket=https://max-cdn.cdn.appdomain.cloud/max-scene-classifier/1.0.1 23 | ARG model_file=assets.tar.gz 24 | 25 | RUN useradd --create-home max 26 | RUN chown -R max:max /opt/conda 27 | USER max 28 | WORKDIR /home/max 29 | RUN mkdir assets 30 | 31 | RUN wget -nv --show-progress --progress=bar:force:noscroll ${model_bucket}/${model_file} --output-document=assets/${model_file} && \ 32 | tar -x -C assets/ -f assets/${model_file} -v && rm assets/${model_file} 33 | 34 | # Conda is the preferred way to install Pytorch, but the Anaconda install pulls 35 | # in non-OSS libraries with customized license terms, specifically CUDA and MKL. 36 | #RUN conda update -n base conda 37 | #RUN conda install -y pytorch-cpu torchvision -c pytorch 38 | 39 | # pip install pytorch to avoid dependencies on MKL or CUDA 40 | COPY requirements.txt . 41 | RUN pip install -r requirements.txt 42 | 43 | COPY . . 44 | # check file integrity 45 | RUN sha512sum -c sha512sums.txt 46 | 47 | EXPOSE 5000 48 | 49 | CMD python app.py 50 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Build Status](https://travis-ci.org/IBM/MAX-Scene-Classifier.svg?branch=master)](https://travis-ci.org/IBM/MAX-Scene-Classifier) [![Website Status](https://img.shields.io/website/http/max-scene-classifier.codait-prod-41208c73af8fca213512856c7a09db52-0000.us-east.containers.appdomain.cloud/swagger.json.svg?label=api+demo)](http://max-scene-classifier.codait-prod-41208c73af8fca213512856c7a09db52-0000.us-east.containers.appdomain.cloud) 2 | 3 | [](http://ibm.biz/max-to-ibm-cloud-tutorial) 4 | 5 | # IBM Code Model Asset Exchange: Scene Classifier 6 | 7 | This repository contains code to instantiate and deploy an image classification model. This model recognizes the 365 different classes of scene/location in the [Places365-Standard subset of the Places2 Dataset](http://places2.csail.mit.edu/). The model is based on the [Places365-CNN Model](https://github.com/CSAILVision/places365) and consists of a pre-trained deep convolutional net using the ResNet architecture, trained on the [ImageNet-2012](http://www.image-net.org/challenges/LSVRC/2012/) data set. The pre-trained model is then fine-tuned on the Places365-Standard dataset. The input to the model is a 224x224 image, and the output is a list of estimated class probabilities. 8 | 9 | The specific model variant used in this repository is the [PyTorch Places365 ResNet18 Model](https://github.com/CSAILVision/places365#pre-trained-cnn-models-on-places365-standard). The model files are hosted on [IBM Cloud Object Storage](https://max-cdn.cdn.appdomain.cloud/max-scene-classifier/1.0.1/assets.tar.gz). The code in this repository deploys the model as a web service in a Docker container. This repository was developed as part of the [IBM Code Model Asset Exchange](https://developer.ibm.com/code/exchanges/models/) and the public API is powered by [IBM Cloud](https://ibm.biz/Bdz2XM). 10 | 11 | ## Model Metadata 12 | | Domain | Application | Industry | Framework | Training Data | Input Data Format | 13 | | ------------- | -------- | -------- | --------- | --------- | -------------- | 14 | | Vision | Image Classification | General | Pytorch | [Places365](http://places2.csail.mit.edu/download.html) | Image (RGB/HWC)| 15 | 16 | ## References 17 | 18 | * _B. Zhou, A. Lapedriza, A. Khosla, A. Oliva, and A. Torralba_, ["Places: A 10 million Image Database for Scene Recognition"](http://places2.csail.mit.edu/PAMI_places.pdf), IEEE Transactions on Pattern Analysis and Machine Intelligence, 2017. 19 | * _B. Zhou, A. Lapedriza, J. Xiao, A. Torralba and A. Oliva_, ["Learning Deep Features for Scene Recognition 20 | using Places Database"](http://places.csail.mit.edu/places_NIPS14.pdf), Advances in Neural Information Processing Systems 27, 2014. 21 | * _K. He, X. Zhang, S. Ren and J. Sun_, ["Deep Residual Learning for Image Recognition"](https://arxiv.org/pdf/1512.03385), CoRR (abs/1512.03385), 2015. 22 | * [Places2 Project Page](http://places2.csail.mit.edu/) 23 | * [Places365-CNN GitHub Page](https://github.com/CSAILVision/places365) 24 | 25 | ## Licenses 26 | 27 | | Component | License | Link | 28 | | ------------- | -------- | -------- | 29 | | This repository | [Apache 2.0](https://www.apache.org/licenses/LICENSE-2.0) | [LICENSE](LICENSE) | 30 | | Model Weights | [CC BY License](https://creativecommons.org/licenses/by/4.0/) | [Places365-CNN](https://github.com/CSAILVision/places365)| 31 | | Model Code (3rd party) | [MIT](https://opensource.org/licenses/MIT) | [Places365-CNN](https://github.com/CSAILVision/places365)| 32 | | Test assets | Various | [Asset README](samples/README.md) | 33 | 34 | ## Pre-requisites: 35 | 36 | * `docker`: The [Docker](https://www.docker.com/) command-line interface. Follow the [installation instructions](https://docs.docker.com/install/) for your system. 37 | * The minimum recommended resources for this model is 2GB Memory and 2 CPUs. 38 | 39 | # Deployment options 40 | 41 | * [Deploy from Quay](#deploy-from-quay) 42 | * [Deploy on Red Hat OpenShift](#deploy-on-red-hat-openshift) 43 | * [Deploy on Kubernetes](#deploy-on-kubernetes) 44 | * [Run Locally](#run-locally) 45 | 46 | ## Deploy from Quay 47 | 48 | To run the docker image, which automatically starts the model serving API, run: 49 | 50 | ``` 51 | $ docker run -it -p 5000:5000 quay.io/codait/max-scene-classifier 52 | ``` 53 | 54 | This will pull a pre-built image from the Quay.io container registry (or use an existing image if already cached locally) and run it. 55 | If you'd rather checkout and build the model locally you can follow the [run locally](#run-locally) steps below. 56 | 57 | ## Deploy on Red Hat OpenShift 58 | 59 | You can deploy the model-serving microservice on Red Hat OpenShift by following the instructions for the OpenShift web console or the OpenShift Container Platform CLI [in this tutorial](https://developer.ibm.com/tutorials/deploy-a-model-asset-exchange-microservice-on-red-hat-openshift/), specifying `quay.io/codait/max-scene-classifier` as the image name. 60 | 61 | ## Deploy on Kubernetes 62 | 63 | You can also deploy the model on Kubernetes using the latest docker image on Quay. 64 | 65 | On your Kubernetes cluster, run the following commands: 66 | 67 | ``` 68 | $ kubectl apply -f https://raw.githubusercontent.com/IBM/MAX-Scene-Classifier/master/max-scene-classifier.yaml 69 | ``` 70 | 71 | The model will be available internally at port `5000`, but can also be accessed externally through the `NodePort`. 72 | 73 | A more elaborate tutorial on how to deploy this MAX model to production on [IBM Cloud](https://ibm.biz/Bdz2XM) can be found [here](http://ibm.biz/max-to-ibm-cloud-tutorial). 74 | 75 | ## Run Locally 76 | 77 | 1. [Build the Model](#1-build-the-model) 78 | 2. [Deploy the Model](#2-deploy-the-model) 79 | 3. [Use the Model](#3-use-the-model) 80 | 4. [Development](#4-development) 81 | 5. [Cleanup](#5-cleanup) 82 | 83 | ### 1. Build the Model 84 | 85 | Clone this repository locally. In a terminal, run the following command: 86 | 87 | ``` 88 | $ git clone https://github.com/IBM/MAX-Scene-Classifier.git 89 | ``` 90 | 91 | Change directory into the repository base folder: 92 | 93 | ``` 94 | $ cd MAX-Scene-Classifier 95 | ``` 96 | 97 | To build the docker image locally, run: 98 | 99 | ``` 100 | $ docker build -t max-scene-classifier . 101 | ``` 102 | 103 | All required model assets will be downloaded during the build process. _Note_ that currently this docker image is CPU only (we will add support for GPU images later). 104 | 105 | ### 2. Deploy the Model 106 | 107 | To run the docker image, which automatically starts the model serving API, run: 108 | 109 | ``` 110 | $ docker run -it -p 5000:5000 max-scene-classifier 111 | ``` 112 | 113 | ### 3. Use the Model 114 | 115 | The API server automatically generates an interactive Swagger documentation page. Go to `http://localhost:5000` to load it. From there you can explore the API and also create test requests. 116 | 117 | Use the `model/predict` endpoint to load a test image (you can use one of the test images from the `samples` folder) and get predicted labels for the image from the API. 118 | 119 | ![Swagger Doc Screenshot](docs/swagger-screenshot.png) 120 | 121 | You can also test it on the command line, for example: 122 | 123 | ```bash 124 | $ curl -F "image=@samples/aquarium.jpg" -XPOST http://localhost:5000/model/predict 125 | ``` 126 | 127 | You should see a JSON response like that below: 128 | 129 | ```json 130 | { 131 | "status": "ok", 132 | "predictions": [ 133 | { 134 | "label_id": "9", 135 | "label": "aquarium", 136 | "probability": 0.97350615262985 137 | }, 138 | { 139 | "label_id": "342", 140 | "label": "underwater\/ocean_deep", 141 | "probability": 0.0062678409740329 142 | }, 143 | { 144 | "label_id": "297", 145 | "label": "science_museum", 146 | "probability": 0.005441018845886 147 | }, 148 | { 149 | "label_id": "239", 150 | "label": "natural_history_museum", 151 | "probability": 0.00413528829813 152 | }, 153 | { 154 | "label_id": "167", 155 | "label": "grotto", 156 | "probability": 0.0024146677460521 157 | } 158 | ] 159 | } 160 | ``` 161 | 162 | ### 4. Development 163 | 164 | To run the Flask API app in debug mode, edit `config.py` to set `DEBUG = True` under the application settings. You will then need to rebuild the docker image (see [step 1](#1-build-the-model)). 165 | 166 | ### 5. Cleanup 167 | 168 | To stop the Docker container, type `CTRL` + `C` in your terminal. 169 | 170 | ## Resources and Contributions 171 | 172 | If you are interested in contributing to the Model Asset Exchange project or have any queries, please follow the instructions [here](https://github.com/CODAIT/max-central-repo). 173 | -------------------------------------------------------------------------------- /api/__init__.py: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2018-2019 IBM Corp. All Rights Reserved. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # 16 | from .metadata import ModelMetadataAPI # noqa 17 | from .predict import ModelPredictAPI, ModelLabelsAPI # noqa 18 | -------------------------------------------------------------------------------- /api/metadata.py: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2018-2019 IBM Corp. All Rights Reserved. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # 16 | 17 | from core.model import ModelWrapper 18 | 19 | from maxfw.core import MAX_API, MetadataAPI, METADATA_SCHEMA 20 | 21 | 22 | class ModelMetadataAPI(MetadataAPI): 23 | 24 | @MAX_API.marshal_with(METADATA_SCHEMA) 25 | def get(self): 26 | """Return the metadata associated with the model""" 27 | return ModelWrapper.MODEL_META_DATA 28 | -------------------------------------------------------------------------------- /api/predict.py: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2018-2019 IBM Corp. All Rights Reserved. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # 16 | 17 | from core.model import ModelWrapper, read_image 18 | 19 | from maxfw.core import MAX_API, PredictAPI, MetadataAPI 20 | 21 | from flask_restplus import fields 22 | from werkzeug.datastructures import FileStorage 23 | 24 | 25 | model_wrapper = ModelWrapper() 26 | 27 | # === Labels API 28 | 29 | model_label = MAX_API.model('ModelLabel', { 30 | 'id': fields.String(required=True, description='Label identifier'), 31 | 'name': fields.String(required=True, description='Class label') 32 | }) 33 | 34 | labels_response = MAX_API.model('LabelsResponse', { 35 | 'count': fields.Integer(required=True, description='Number of labels returned'), 36 | 'labels': fields.List(fields.Nested(model_label), description='Class labels that can be predicted by the model') 37 | }) 38 | 39 | 40 | class ModelLabelsAPI(MetadataAPI): 41 | '''API for getting information about available class labels''' 42 | 43 | id_to_class = {i: c for i, c in enumerate(model_wrapper.classes)} 44 | 45 | @MAX_API.doc('get_labels') 46 | @MAX_API.marshal_with(labels_response) 47 | def get(self): 48 | '''Return the list of labels that can be predicted by the model''' 49 | result = {} 50 | result['labels'] = [{'id': x[0], 'name': x[1]} for x in self.id_to_class.items()] 51 | result['count'] = len(self.id_to_class) 52 | return result 53 | 54 | 55 | # === Predict API 56 | 57 | label_prediction = MAX_API.model('LabelPrediction', { 58 | 'label_id': fields.String(required=False, description='Label identifier'), 59 | 'label': fields.String(required=True, description='Class label'), 60 | 'probability': fields.Float(required=True, description='Predicted probability for the class label') 61 | }) 62 | 63 | predict_response = MAX_API.model('ModelPredictResponse', { 64 | 'status': fields.String(required=True, description='Response status message'), 65 | 'predictions': fields.List(fields.Nested(label_prediction), description='Predicted labels and probabilities') 66 | }) 67 | 68 | # set up parser for image input data 69 | image_parser = MAX_API.parser() 70 | image_parser.add_argument('image', type=FileStorage, location='files', required=True, 71 | help='An image file (encoded as PNG or JPG/JPEG)') 72 | 73 | 74 | class ModelPredictAPI(PredictAPI): 75 | 76 | @MAX_API.doc('predict') 77 | @MAX_API.expect(image_parser) 78 | @MAX_API.marshal_with(predict_response) 79 | def post(self): 80 | '''Make a prediction given input data''' 81 | result = {'status': 'error'} 82 | 83 | args = image_parser.parse_args() 84 | image_data = args['image'].read() 85 | image = read_image(image_data) 86 | preds = model_wrapper.predict(image) 87 | 88 | label_preds = [{'label_id': p[0], 'label': p[1], 'probability': p[2]} for p in [x for x in preds]] 89 | result['predictions'] = label_preds 90 | result['status'] = 'ok' 91 | 92 | return result 93 | -------------------------------------------------------------------------------- /app.py: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2018-2019 IBM Corp. All Rights Reserved. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # 16 | 17 | from maxfw.core import MAXApp 18 | from api import ModelMetadataAPI, ModelPredictAPI, ModelLabelsAPI 19 | from config import API_TITLE, API_DESC, API_VERSION 20 | 21 | max_app = MAXApp(API_TITLE, API_DESC, API_VERSION) 22 | max_app.add_api(ModelMetadataAPI, '/metadata') 23 | max_app.add_api(ModelLabelsAPI, '/labels') 24 | max_app.add_api(ModelPredictAPI, '/predict') 25 | max_app.run() 26 | -------------------------------------------------------------------------------- /config.py: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2018-2019 IBM Corp. All Rights Reserved. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # 16 | 17 | # Application settings 18 | 19 | # Flask settings 20 | DEBUG = False 21 | 22 | # Flask-restplus settings 23 | RESTPLUS_MASK_SWAGGER = False 24 | SWAGGER_UI_DOC_EXPANSION = 'none' 25 | 26 | # API metadata 27 | API_TITLE = 'MAX Scene Classifier' 28 | API_DESC = 'Classify images according to the place/location labels in the Places365 data set.' 29 | API_VERSION = '1.2.0' 30 | MODEL_ID = API_TITLE.lower().replace(' ', '-') 31 | 32 | # default model 33 | MODEL_NAME = 'resnet18_places365' 34 | DEFAULT_MODEL_PATH = 'assets' 35 | DEFAULT_MODEL_FILE = 'whole_resnet18_places365_python36.pth' 36 | # for image models, may not be required 37 | MODEL_INPUT_IMG_SIZE = (256, 256) 38 | MODEL_LICENSE = 'CC BY' 39 | -------------------------------------------------------------------------------- /core/__init__.py: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2018-2019 IBM Corp. All Rights Reserved. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # 16 | -------------------------------------------------------------------------------- /core/model.py: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2018-2019 IBM Corp. All Rights Reserved. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # 16 | 17 | from config import MODEL_ID, MODEL_LICENSE, API_TITLE,\ 18 | MODEL_INPUT_IMG_SIZE, DEFAULT_MODEL_PATH, DEFAULT_MODEL_FILE 19 | from maxfw.model import MAXModelWrapper 20 | 21 | import torch 22 | from torch.autograd import Variable as V 23 | from torchvision import transforms as trn 24 | from torch.nn import functional as F 25 | 26 | import io 27 | from PIL import Image 28 | import logging 29 | 30 | logger = logging.getLogger() 31 | 32 | 33 | def read_image(image_data): 34 | try: 35 | image = Image.open(io.BytesIO(image_data)).convert('RGB') 36 | except Exception as e: 37 | logger.warn(str(e)) 38 | from flask import abort 39 | abort(400, "The provided input is not a valid image (PNG or JPG required).") 40 | 41 | return image 42 | 43 | 44 | def preprocess_image(image, target): 45 | # load the image transformer 46 | centre_crop = trn.Compose([ 47 | trn.Resize(target), 48 | trn.CenterCrop(224), 49 | trn.ToTensor(), 50 | trn.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]) 51 | ]) 52 | return V(centre_crop(image).unsqueeze(0), volatile=True) 53 | 54 | 55 | def post_process_result(probs, idxs, classes): 56 | return [(idxs[i], classes[idxs[i]], probs[i]) for i in range(len(idxs))] 57 | 58 | 59 | class ModelWrapper(MAXModelWrapper): 60 | 61 | MODEL_META_DATA = { 62 | 'id': MODEL_ID, 63 | 'name': API_TITLE, 64 | 'description': 'Pytorch ResNet18 model trained on Places365 dataset', 65 | 'license': '{}'.format(MODEL_LICENSE), 66 | 'type': 'Image Classification', 67 | 'source': 'https://developer.ibm.com/exchanges/models/all/{}/'.format(MODEL_ID) 68 | } 69 | 70 | def __init__(self, path=DEFAULT_MODEL_PATH, model_file=DEFAULT_MODEL_FILE): 71 | logger.info('Loading model from: {}...'.format(path)) 72 | model_path = '{}/{}'.format(path, model_file) 73 | self.model = torch.load(model_path, map_location=lambda storage, loc: storage) # cpu only for now ... 74 | logger.info('Loaded model') 75 | self._load_assets(path) 76 | 77 | def _load_assets(self, path): 78 | file_name = '{}/categories_places365.txt'.format(path) 79 | classes = list() 80 | with open(file_name) as class_file: 81 | for line in class_file: 82 | classes.append(line.strip().split(' ')[0][3:]) 83 | self.classes = tuple(classes) 84 | 85 | def _pre_process(self, x): 86 | return preprocess_image(x, MODEL_INPUT_IMG_SIZE) 87 | 88 | def _post_process(self, x): 89 | probs, idxs = x.topk(5) 90 | return post_process_result(probs, idxs, self.classes) 91 | 92 | def _predict(self, x): 93 | logit = self.model.forward(x) 94 | probs = F.softmax(logit, 1).data.squeeze() 95 | return probs 96 | -------------------------------------------------------------------------------- /docs/deploy-max-to-ibm-cloud-with-kubernetes-button.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/MAX-Scene-Classifier/7c5d12a0834787909fdbab16f13ebe5eb69ccb31/docs/deploy-max-to-ibm-cloud-with-kubernetes-button.png -------------------------------------------------------------------------------- /docs/swagger-screenshot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/MAX-Scene-Classifier/7c5d12a0834787909fdbab16f13ebe5eb69ccb31/docs/swagger-screenshot.png -------------------------------------------------------------------------------- /max-scene-classifier.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: v1 2 | kind: Service 3 | metadata: 4 | name: max-scene-classifier 5 | spec: 6 | selector: 7 | app: max-scene-classifier 8 | ports: 9 | - port: 5000 10 | type: NodePort 11 | --- 12 | apiVersion: apps/v1 13 | kind: Deployment 14 | metadata: 15 | name: max-scene-classifier 16 | labels: 17 | app: max-scene-classifier 18 | spec: 19 | selector: 20 | matchLabels: 21 | app: max-scene-classifier 22 | replicas: 1 23 | template: 24 | metadata: 25 | labels: 26 | app: max-scene-classifier 27 | spec: 28 | containers: 29 | - name: max-scene-classifier 30 | image: quay.io/codait/max-scene-classifier:latest 31 | ports: 32 | - containerPort: 5000 33 | -------------------------------------------------------------------------------- /requirements-test.txt: -------------------------------------------------------------------------------- 1 | flake8==3.8.4 2 | pytest==6.1.2 3 | requests==2.25.0 4 | bandit==1.6.2 5 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | torchvision==0.2.1 2 | http://download.pytorch.org/whl/cpu/torch-0.3.1-cp36-cp36m-linux_x86_64.whl 3 | pillow==8.2.0 4 | -------------------------------------------------------------------------------- /samples/README.md: -------------------------------------------------------------------------------- 1 | # Sample assets 2 | 3 | ## Images 4 | 5 | All sample images are from [Pexels](https://www.pexels.com) and licensed under a [CC0 License](https://creativecommons.org/publicdomain/zero/1.0/). 6 | 7 | * [`aquarium.jpg`](https://www.pexels.com/photo/black-and-gray-fish-889848/) 8 | * [`paris.jpg`](https://www.pexels.com/photo/picture-of-eiffel-tower-338515/) 9 | * [`bakery.jpg`](https://www.pexels.com/photo/bacon-rosemart-192933/) 10 | * [`ruins.jpg`](https://www.pexels.com/photo/odeon-of-herodes-atticus-772686/) 11 | * [`city.jpg`](https://www.pexels.com/photo/high-angle-view-of-cityscape-against-cloudy-sky-313782/) 12 | -------------------------------------------------------------------------------- /samples/aquarium.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/MAX-Scene-Classifier/7c5d12a0834787909fdbab16f13ebe5eb69ccb31/samples/aquarium.jpg -------------------------------------------------------------------------------- /samples/bakery.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/MAX-Scene-Classifier/7c5d12a0834787909fdbab16f13ebe5eb69ccb31/samples/bakery.jpg -------------------------------------------------------------------------------- /samples/city.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/MAX-Scene-Classifier/7c5d12a0834787909fdbab16f13ebe5eb69ccb31/samples/city.jpg -------------------------------------------------------------------------------- /samples/paris.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/MAX-Scene-Classifier/7c5d12a0834787909fdbab16f13ebe5eb69ccb31/samples/paris.jpg -------------------------------------------------------------------------------- /samples/ruins.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/MAX-Scene-Classifier/7c5d12a0834787909fdbab16f13ebe5eb69ccb31/samples/ruins.jpg -------------------------------------------------------------------------------- /sha512sums.txt: -------------------------------------------------------------------------------- 1 | 113729704693e94b30e2a40ddef1ebda30a622b5bba4e89472b685ab8b5d7f3ef2226c031d3b7d8c95a77684826408a93c7522543ab26bb8068dcd4004844579 assets/categories_places365.txt 2 | db19eaca5d1b9577e5da0981fd84c8421bb7e90181c7a727b2a795fc0e721c7f1f2ad2f3e7ec10c1fffc23cb19734b1af512bd892eebffe25553b6fbbc02975b assets/whole_resnet18_places365_python36.pth 3 | -------------------------------------------------------------------------------- /tests/bakery.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/MAX-Scene-Classifier/7c5d12a0834787909fdbab16f13ebe5eb69ccb31/tests/bakery.jpg -------------------------------------------------------------------------------- /tests/bakery.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/MAX-Scene-Classifier/7c5d12a0834787909fdbab16f13ebe5eb69ccb31/tests/bakery.png -------------------------------------------------------------------------------- /tests/test.py: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2018-2019 IBM Corp. All Rights Reserved. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # 16 | 17 | import pytest 18 | import requests 19 | 20 | 21 | def test_swagger(): 22 | 23 | model_endpoint = 'http://localhost:5000/swagger.json' 24 | 25 | r = requests.get(url=model_endpoint) 26 | assert r.status_code == 200 27 | assert r.headers['Content-Type'] == 'application/json' 28 | 29 | json = r.json() 30 | assert 'swagger' in json 31 | assert json.get('info') and json.get('info').get('title') == 'MAX Scene Classifier' 32 | 33 | 34 | def test_metadata(): 35 | 36 | model_endpoint = 'http://localhost:5000/model/metadata' 37 | 38 | r = requests.get(url=model_endpoint) 39 | assert r.status_code == 200 40 | 41 | metadata = r.json() 42 | assert metadata['id'] == 'max-scene-classifier' 43 | assert metadata['name'] == 'MAX Scene Classifier' 44 | assert metadata['description'] == 'Pytorch ResNet18 model trained on Places365 dataset' 45 | assert metadata['license'] == 'CC BY' 46 | assert metadata['type'] == 'Image Classification' 47 | assert 'max-scene-classifier' in metadata['source'] 48 | 49 | 50 | def test_labels(): 51 | 52 | model_endpoint = 'http://localhost:5000/model/labels' 53 | 54 | r = requests.get(url=model_endpoint) 55 | assert r.status_code == 200 56 | labels = r.json() 57 | 58 | assert labels['count'] == 365 59 | assert len(labels['labels']) == 365 60 | assert labels['labels'][0]['id'] == '0' 61 | assert labels['labels'][0]['name'] == 'airfield' 62 | assert labels['labels'][-1]['id'] == '364' 63 | assert labels['labels'][-1]['name'] == 'zen_garden' 64 | 65 | 66 | def _check_predict(r): 67 | assert r.status_code == 200 68 | response = r.json() 69 | assert response['status'] == 'ok' 70 | 71 | assert response['predictions'][0]['label_id'] == '31' 72 | assert response['predictions'][0]['label'] == 'bakery/shop' 73 | assert response['predictions'][0]['probability'] > 0.7 74 | 75 | 76 | def test_predict(): 77 | 78 | model_endpoint = 'http://localhost:5000/model/predict' 79 | formats = ['jpg', 'png'] 80 | file_path = 'tests/bakery.{}' 81 | 82 | for f in formats: 83 | p = file_path.format(f) 84 | with open(p, 'rb') as file: 85 | file_form = {'image': (p, file, 'image/{}'.format(f))} 86 | r = requests.post(url=model_endpoint, files=file_form) 87 | _check_predict(r) 88 | 89 | 90 | def test_invalid_input(): 91 | 92 | model_endpoint = 'http://localhost:5000/model/predict' 93 | file_path = 'README.md' 94 | 95 | with open(file_path, 'rb') as file: 96 | file_form = {'image': (file_path, file, 'image/jpeg')} 97 | r = requests.post(url=model_endpoint, files=file_form) 98 | 99 | assert r.status_code == 400 100 | response = r.json() 101 | assert 'input is not a valid image' in response['message'] 102 | 103 | 104 | if __name__ == '__main__': 105 | pytest.main([__file__]) 106 | --------------------------------------------------------------------------------