├── test └── test_hello.py ├── autodistill_grounded_sam ├── __init__.py ├── grounded_sam.py └── helpers.py ├── requirements.txt ├── .github └── workflows │ ├── welcome.yml │ ├── test.yml │ └── publish.yml ├── Makefile ├── setup.py ├── .gitignore ├── README.md └── LICENSE /test/test_hello.py: -------------------------------------------------------------------------------- 1 | def test_hello(): 2 | assert True == True -------------------------------------------------------------------------------- /autodistill_grounded_sam/__init__.py: -------------------------------------------------------------------------------- 1 | from autodistill_grounded_sam.grounded_sam import GroundedSAM 2 | 3 | __version__ = "0.1.2" 4 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | torch 2 | autodistill 3 | numpy>=1.20.0 4 | opencv-python>=4.6.0 5 | rf_groundingdino 6 | rf_segment_anything 7 | supervision 8 | -------------------------------------------------------------------------------- /.github/workflows/welcome.yml: -------------------------------------------------------------------------------- 1 | on: 2 | issues: 3 | types: [opened] 4 | pull_request_target: 5 | types: [opened] 6 | 7 | jobs: 8 | build: 9 | name: 👋 Welcome 10 | runs-on: ubuntu-latest 11 | steps: 12 | - uses: actions/first-interaction@v1.1.1 13 | with: 14 | repo-token: ${{ secrets.GITHUB_TOKEN }} 15 | issue-message: "Hello there, thank you for opening an Issue ! 🙏🏻 The team was notified and they will get back to you soon." 16 | pr-message: "Hello there, thank you for opening an PR ! 🙏🏻 The team was notified and they will get back to you soon." -------------------------------------------------------------------------------- /.github/workflows/test.yml: -------------------------------------------------------------------------------- 1 | name: Test WorkFlow 2 | 3 | on: 4 | pull_request: 5 | branches: [main] 6 | 7 | jobs: 8 | build: 9 | runs-on: ubuntu-latest 10 | strategy: 11 | matrix: 12 | python-version: [3.7, 3.8, 3.9] 13 | steps: 14 | - name: 🛎️ Checkout 15 | uses: actions/checkout@v3 16 | with: 17 | ref: ${{ github.head_ref }} 18 | - name: 🐍 Set up Python ${{ matrix.python-version }} 19 | uses: actions/setup-python@v2 20 | with: 21 | python-version: ${{ matrix.python-version }} 22 | - name: 🦾 Install dependencies 23 | run: | 24 | python -m pip install --upgrade pip 25 | pip install ".[dev]" 26 | - name: 🧹 Lint with flake8 27 | run: | 28 | make check_code_quality 29 | - name: 🧪 Test 30 | run: "python -m pytest ./test" -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | .PHONY: style check_code_quality 2 | 3 | export PYTHONPATH = . 4 | check_dirs := autodistill_grounded_sam 5 | 6 | style: 7 | black $(check_dirs) 8 | isort --profile black $(check_dirs) 9 | 10 | check_code_quality: 11 | black --check $(check_dirs) 12 | isort --check-only --profile black $(check_dirs) 13 | # stop the build if there are Python syntax errors or undefined names 14 | flake8 $(check_dirs) --count --select=E9,F63,F7,F82 --show-source --statistics 15 | # exit-zero treats all errors as warnings. E203 for black, E501 for docstring, W503 for line breaks before logical operators 16 | flake8 $(check_dirs) --count --max-line-length=88 --exit-zero --ignore=D --extend-ignore=E203,E501,W503 --statistics 17 | 18 | publish: 19 | python setup.py sdist bdist_wheel 20 | twine check dist/* 21 | twine upload dist/* -u ${PYPI_USERNAME} -p ${PYPI_PASSWORD} --verbose 22 | -------------------------------------------------------------------------------- /.github/workflows/publish.yml: -------------------------------------------------------------------------------- 1 | name: Publish WorkFlow 2 | 3 | on: 4 | release: 5 | types: [created] 6 | 7 | jobs: 8 | build: 9 | runs-on: ubuntu-latest 10 | strategy: 11 | matrix: 12 | python-version: [3.8] 13 | steps: 14 | - name: 🛎️ Checkout 15 | uses: actions/checkout@v3 16 | with: 17 | ref: ${{ github.head_ref }} 18 | - name: 🐍 Set up Python ${{ matrix.python-version }} 19 | uses: actions/setup-python@v2 20 | with: 21 | python-version: ${{ matrix.python-version }} 22 | - name: 🦾 Install dependencies 23 | run: | 24 | python -m pip install --upgrade pip 25 | pip install ".[dev]" 26 | - name: 🚀 Publish to PyPi 27 | env: 28 | PYPI_USERNAME: ${{ secrets.PYPI_USERNAME }} 29 | PYPI_PASSWORD: ${{ secrets.PYPI_PASSWORD }} 30 | PYPI_TEST_PASSWORD: ${{ secrets.PYPI_TEST_PASSWORD }} 31 | run: | 32 | make publish -e PYPI_USERNAME=$PYPI_USERNAME -e PYPI_PASSWORD=$PYPI_PASSWORD -e PYPI_TEST_PASSWORD=$PYPI_TEST_PASSWORD -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | import setuptools 2 | from setuptools import find_packages 3 | import subprocess 4 | import sys 5 | import re 6 | 7 | # groundingdino needs torch to be installed before it can be installed 8 | # this is a hack but couldn't find any other way to make it work 9 | try: 10 | import torch 11 | except: 12 | subprocess.check_call([sys.executable, "-m", "pip", "install", 'torch']) 13 | 14 | with open("./autodistill_grounded_sam/__init__.py", 'r') as f: 15 | content = f.read() 16 | # from https://www.py4u.net/discuss/139845 17 | version = re.search(r'__version__\s*=\s*[\'"]([^\'"]*)[\'"]', content).group(1) 18 | 19 | with open("README.md", "r") as fh: 20 | long_description = fh.read() 21 | 22 | with open("requirements.txt", "r") as fh: 23 | install_requires = fh.read().split('\n') 24 | 25 | setuptools.setup( 26 | name="autodistill_grounded_sam", 27 | version=version, 28 | author="Roboflow", 29 | author_email="autodistill@roboflow.com", 30 | description="Automatically distill large foundational models into smaller, in-domain models for deployment", 31 | long_description="Automatically distill large foundational models into smaller, in-domain models for deployment", 32 | long_description_content_type="text/markdown", 33 | url="https://github.com/autodistill/autodistill-grounded-sam", 34 | install_requires=install_requires, 35 | packages=find_packages(exclude=("tests",)), 36 | extras_require={ 37 | "dev": ["flake8", "black==22.3.0", "isort", "twine", "pytest", "wheel"], 38 | }, 39 | classifiers=[ 40 | "Programming Language :: Python :: 3", 41 | "License :: OSI Approved :: MIT License", 42 | "Operating System :: OS Independent", 43 | ], 44 | python_requires=">=3.7", 45 | ) 46 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | .DS_Store 7 | 8 | # C extensions 9 | *.so 10 | 11 | # Distribution / packaging 12 | .Python 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 | pip-wheel-metadata/ 26 | share/python-wheels/ 27 | *.egg-info/ 28 | .installed.cfg 29 | *.egg 30 | MANIFEST 31 | 32 | # PyInstaller 33 | # Usually these files are written by a python script from a template 34 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 35 | *.manifest 36 | *.spec 37 | 38 | # Installer logs 39 | pip-log.txt 40 | pip-delete-this-directory.txt 41 | 42 | # Unit test / coverage reports 43 | htmlcov/ 44 | .tox/ 45 | .nox/ 46 | .coverage 47 | .coverage.* 48 | .cache 49 | nosetests.xml 50 | coverage.xml 51 | *.cover 52 | *.py,cover 53 | .hypothesis/ 54 | .pytest_cache/ 55 | 56 | # Translations 57 | *.mo 58 | *.pot 59 | 60 | # Django stuff: 61 | *.log 62 | local_settings.py 63 | db.sqlite3 64 | db.sqlite3-journal 65 | 66 | # Flask stuff: 67 | instance/ 68 | .webassets-cache 69 | 70 | # Scrapy stuff: 71 | .scrapy 72 | 73 | # Sphinx documentation 74 | docs/_build/ 75 | 76 | # PyBuilder 77 | target/ 78 | 79 | # Jupyter Notebook 80 | .ipynb_checkpoints 81 | 82 | # IPython 83 | profile_default/ 84 | ipython_config.py 85 | 86 | # pyenv 87 | .python-version 88 | 89 | # pipenv 90 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 91 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 92 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 93 | # install all needed dependencies. 94 | #Pipfile.lock 95 | 96 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow 97 | __pypackages__/ 98 | 99 | # Celery stuff 100 | celerybeat-schedule 101 | celerybeat.pid 102 | 103 | # SageMath parsed files 104 | *.sage.py 105 | 106 | # Environments 107 | .env 108 | .venv 109 | env/ 110 | venv/ 111 | ENV/ 112 | env.bak/ 113 | venv.bak/ 114 | 115 | # Spyder project settings 116 | .spyderproject 117 | .spyproject 118 | 119 | # Rope project settings 120 | .ropeproject 121 | 122 | # mkdocs documentation 123 | /site 124 | 125 | # mypy 126 | .mypy_cache/ 127 | .dmypy.json 128 | dmypy.json 129 | 130 | *.jpeg 131 | *.xml 132 | 133 | # Pyre type checker 134 | .pyre/ 135 | -------------------------------------------------------------------------------- /autodistill_grounded_sam/grounded_sam.py: -------------------------------------------------------------------------------- 1 | import os 2 | from dataclasses import dataclass 3 | 4 | os.environ["CUBLAS_WORKSPACE_CONFIG"] = ":16:8" 5 | os.environ["TOKENIZERS_PARALLELISM"] = "false" 6 | 7 | import torch 8 | 9 | torch.use_deterministic_algorithms(False) 10 | 11 | from typing import Any 12 | 13 | import numpy as np 14 | import supervision as sv 15 | from autodistill_grounded_sam.helpers import (combine_detections, 16 | load_grounding_dino, 17 | load_SAM) 18 | from autodistill.helpers import load_image 19 | from groundingdino.util.inference import Model 20 | from segment_anything import SamPredictor 21 | 22 | from autodistill.detection import CaptionOntology, DetectionBaseModel 23 | 24 | HOME = os.path.expanduser("~") 25 | DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu") 26 | 27 | 28 | @dataclass 29 | class GroundedSAM(DetectionBaseModel): 30 | ontology: CaptionOntology 31 | grounding_dino_model: Model 32 | sam_predictor: SamPredictor 33 | box_threshold: float 34 | text_threshold: float 35 | 36 | def __init__( 37 | self, ontology: CaptionOntology, box_threshold=0.35, text_threshold=0.25 38 | ): 39 | self.ontology = ontology 40 | self.grounding_dino_model = load_grounding_dino() 41 | self.sam_predictor = load_SAM() 42 | self.box_threshold = box_threshold 43 | self.text_threshold = text_threshold 44 | 45 | def predict(self, input: Any) -> sv.Detections: 46 | image = load_image(input, return_format="cv2") 47 | 48 | # GroundingDINO predictions 49 | detections_list = [] 50 | 51 | for i, description in enumerate(self.ontology.prompts()): 52 | # detect objects 53 | detections = self.grounding_dino_model.predict_with_classes( 54 | image=image, 55 | classes=[description], 56 | box_threshold=self.box_threshold, 57 | text_threshold=self.text_threshold, 58 | ) 59 | 60 | detections_list.append(detections) 61 | 62 | detections = combine_detections( 63 | detections_list, overwrite_class_ids=range(len(detections_list)) 64 | ) 65 | 66 | # SAM Predictions 67 | xyxy = detections.xyxy 68 | 69 | self.sam_predictor.set_image(image) 70 | result_masks = [] 71 | for box in xyxy: 72 | masks, scores, logits = self.sam_predictor.predict( 73 | box=box, multimask_output=False 74 | ) 75 | index = np.argmax(scores) 76 | result_masks.append(masks[index]) 77 | 78 | detections.mask = np.array(result_masks) 79 | 80 | # separate in supervision to combine detections and override class_ids 81 | return detections 82 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |
2 |

3 | 4 | 8 | 9 |

10 |
11 | 12 | # Autodistill: GroundedSAM Base Model 13 | 14 | This repository contains the code implementing [GroundedSAM](https://github.com/IDEA-Research/Grounded-Segment-Anything) as a Base Model for use with [`autodistill`](https://github.com/autodistill/autodistill). 15 | 16 | GroundedSAM combines [GroundingDINO](https://github.com/IDEA-Research/GroundingDINO) with the [Segment Anything Model](https://github.com/facebookresearch/segment-anything) to identify and segment objects in an image given text captions. 17 | 18 | Read the full [Autodistill documentation](https://autodistill.github.io/autodistill/). 19 | 20 | Read the [GroundedSAM Autodistill documentation](https://autodistill.github.io/autodistill/base_models/groundedsam/). 21 | 22 | > [!TIP] 23 | > You can use Autodistill Grounded SAM on your own hardware using the instructions below, or use the [Roboflow hosted version of Autodistill](https://blog.roboflow.com/launch-auto-label/) to label images in the cloud. 24 | 25 | ## Installation 26 | 27 | To use the GroundedSAM Base Model, simply install it along with a Target Model supporting the `detection` task: 28 | 29 | ```bash 30 | pip3 install autodistill-grounded-sam autodistill-yolov8 31 | ``` 32 | 33 | You can find a full list of `detection` Target Models on [the main autodistill repo](https://github.com/autodistill/autodistill). 34 | 35 | ## Quickstart 36 | 37 | ```python 38 | from autodistill_grounded_sam import GroundedSAM 39 | from autodistill.detection import CaptionOntology 40 | from autodistill.utils import plot 41 | import cv2 42 | 43 | # define an ontology to map class names to our GroundedSAM prompt 44 | # the ontology dictionary has the format {caption: class} 45 | # where caption is the prompt sent to the base model, and class is the label that will 46 | # be saved for that caption in the generated annotations 47 | # then, load the model 48 | base_model = GroundedSAM( 49 | ontology=CaptionOntology( 50 | { 51 | "person": "person", 52 | "shipping container": "shipping container", 53 | } 54 | ) 55 | ) 56 | 57 | # run inference on a single image 58 | results = base_model.predict("logistics.jpeg") 59 | 60 | plot( 61 | image=cv2.imread("logistics.jpeg"), 62 | classes=base_model.ontology.classes(), 63 | detections=results 64 | ) 65 | # label all images in a folder called `context_images` 66 | base_model.label("./context_images", extension=".jpeg") 67 | ``` 68 | 69 | ## License 70 | 71 | The code in this repository is licensed under an [Apache 2.0 license](LICENSE). 72 | 73 | ## 🏆 Contributing 74 | 75 | We love your input! Please see the core Autodistill [contributing guide](https://github.com/autodistill/autodistill/blob/main/CONTRIBUTING.md) to get started. Thank you 🙏 to all our contributors! 76 | -------------------------------------------------------------------------------- /autodistill_grounded_sam/helpers.py: -------------------------------------------------------------------------------- 1 | import os 2 | import urllib.request 3 | 4 | import numpy as np 5 | import supervision as sv 6 | import torch 7 | from groundingdino.util.inference import Model 8 | from segment_anything import SamPredictor, sam_model_registry 9 | 10 | DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu") 11 | 12 | if not torch.cuda.is_available(): 13 | print("WARNING: CUDA not available. GroundingDINO will run very slowly.") 14 | 15 | 16 | def combine_detections(detections_list, overwrite_class_ids): 17 | if len(detections_list) == 0: 18 | return sv.Detections.empty() 19 | 20 | if overwrite_class_ids is not None and len(overwrite_class_ids) != len( 21 | detections_list 22 | ): 23 | raise ValueError( 24 | "Length of overwrite_class_ids must match the length of detections_list." 25 | ) 26 | 27 | xyxy = [] 28 | mask = [] 29 | confidence = [] 30 | class_id = [] 31 | tracker_id = [] 32 | 33 | for idx, detection in enumerate(detections_list): 34 | xyxy.append(detection.xyxy) 35 | 36 | if detection.mask is not None: 37 | mask.append(detection.mask) 38 | 39 | if detection.confidence is not None: 40 | confidence.append(detection.confidence) 41 | 42 | if detection.class_id is not None: 43 | if overwrite_class_ids is not None: 44 | # Overwrite the class IDs for the current Detections object 45 | class_id.append( 46 | np.full_like( 47 | detection.class_id, overwrite_class_ids[idx], dtype=np.int64 48 | ) 49 | ) 50 | else: 51 | class_id.append(detection.class_id) 52 | 53 | if detection.tracker_id is not None: 54 | tracker_id.append(detection.tracker_id) 55 | 56 | xyxy = np.vstack(xyxy) 57 | mask = np.vstack(mask) if mask else None 58 | confidence = np.hstack(confidence) if confidence else None 59 | class_id = np.hstack(class_id) if class_id else None 60 | tracker_id = np.hstack(tracker_id) if tracker_id else None 61 | 62 | return sv.Detections( 63 | xyxy=xyxy, 64 | mask=mask, 65 | confidence=confidence, 66 | class_id=class_id, 67 | tracker_id=tracker_id, 68 | ) 69 | 70 | 71 | def load_grounding_dino(): 72 | AUTODISTILL_CACHE_DIR = os.path.expanduser("~/.cache/autodistill") 73 | 74 | GROUDNING_DINO_CACHE_DIR = os.path.join(AUTODISTILL_CACHE_DIR, "groundingdino") 75 | 76 | GROUNDING_DINO_CONFIG_PATH = os.path.join( 77 | GROUDNING_DINO_CACHE_DIR, "GroundingDINO_SwinT_OGC.py" 78 | ) 79 | GROUNDING_DINO_CHECKPOINT_PATH = os.path.join( 80 | GROUDNING_DINO_CACHE_DIR, "groundingdino_swint_ogc.pth" 81 | ) 82 | 83 | try: 84 | print("trying to load grounding dino directly") 85 | grounding_dino_model = Model( 86 | model_config_path=GROUNDING_DINO_CONFIG_PATH, 87 | model_checkpoint_path=GROUNDING_DINO_CHECKPOINT_PATH, 88 | device=DEVICE, 89 | ) 90 | return grounding_dino_model 91 | except Exception: 92 | print("downloading dino model weights") 93 | if not os.path.exists(GROUDNING_DINO_CACHE_DIR): 94 | os.makedirs(GROUDNING_DINO_CACHE_DIR) 95 | 96 | if not os.path.exists(GROUNDING_DINO_CHECKPOINT_PATH): 97 | url = "https://github.com/IDEA-Research/GroundingDINO/releases/download/v0.1.0-alpha/groundingdino_swint_ogc.pth" 98 | urllib.request.urlretrieve(url, GROUNDING_DINO_CHECKPOINT_PATH) 99 | 100 | if not os.path.exists(GROUNDING_DINO_CONFIG_PATH): 101 | url = "https://raw.githubusercontent.com/roboflow/GroundingDINO/main/groundingdino/config/GroundingDINO_SwinT_OGC.py" 102 | urllib.request.urlretrieve(url, GROUNDING_DINO_CONFIG_PATH) 103 | 104 | grounding_dino_model = Model( 105 | model_config_path=GROUNDING_DINO_CONFIG_PATH, 106 | model_checkpoint_path=GROUNDING_DINO_CHECKPOINT_PATH, 107 | device=DEVICE, 108 | ) 109 | 110 | # grounding_dino_model.to(DEVICE) 111 | 112 | return grounding_dino_model 113 | 114 | 115 | def load_SAM(): 116 | # Check if segment-anything library is already installed 117 | 118 | AUTODISTILL_CACHE_DIR = os.path.expanduser("~/.cache/autodistill") 119 | SAM_CACHE_DIR = os.path.join(AUTODISTILL_CACHE_DIR, "segment_anything") 120 | SAM_CHECKPOINT_PATH = os.path.join(SAM_CACHE_DIR, "sam_vit_h_4b8939.pth") 121 | 122 | url = "https://dl.fbaipublicfiles.com/segment_anything/sam_vit_h_4b8939.pth" 123 | 124 | # Create the destination directory if it doesn't exist 125 | os.makedirs(os.path.dirname(SAM_CHECKPOINT_PATH), exist_ok=True) 126 | 127 | # Download the file if it doesn't exist 128 | if not os.path.isfile(SAM_CHECKPOINT_PATH): 129 | urllib.request.urlretrieve(url, SAM_CHECKPOINT_PATH) 130 | 131 | SAM_ENCODER_VERSION = "vit_h" 132 | 133 | sam = sam_model_registry[SAM_ENCODER_VERSION](checkpoint=SAM_CHECKPOINT_PATH).to( 134 | device=DEVICE 135 | ) 136 | sam_predictor = SamPredictor(sam) 137 | 138 | return sam_predictor 139 | -------------------------------------------------------------------------------- /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. --------------------------------------------------------------------------------