Commit 1a3c83d6 authored by zhanggzh's avatar zhanggzh
Browse files

增加keras-cv模型及训练代码

parent 9846958a
#!/bin/sh
./shell/lint.sh $(git diff HEAD --diff-filter=d --cached --name-only '*.py')
# API Design Guidelines
In general, KerasCV abides to the
[API design guidelines of Keras](https://github.com/keras-team/governance/blob/master/keras_api_design_guidelines.md).
There are a few API guidelines that apply only to KerasCV. These are discussed
in this document.
# Label Names
When working with `bounding_box` and `segmentation_map` labels the
abbreviations `bbox` and `segm` are often used. In KerasCV, we will *not* be
using these abbreviations. This is done to ensure full consistency in our
naming convention. While the team is fond of the abbreviation `bbox`, we are
less fond of `segm`. In order to ensure full consistency, we have decided to
use the full names for label types in our code base.
# Preprocessing Layers
## Strength Parameters
Many augmentation layers take a parameter representing a strength, often called
`factor`. When possible, factor values must conform to a the range: `[0, 1]`, with
1 representing the strongest transformation and 0 representing a no-op transform.
The strength of an augmentation should scale linearly with this factor. If needed,
a transformation can be performed to map to a large value range internally. If
this is done, please provide a thorough explanation of the value range semantics in
the docstring.
Additionally, factors should support both float and tuples as inputs. If a float is
passed, such as `factor=0.5`, the layer should default to the range `[0, factor]`.
## BaseImageAugmentationLayer
When implementing preprocessing, we encourage users to subclass the
`keras_cv.layers.preprocessing.BaseImageAugmentationLayer`. This layer provides
a common `call()` method, auto vectorization, and more.
When subclassing `BaseImageAugmentationLayer`, several methods can overridden:
- `BaseImageAugmentationLayer.augment_image()` must be overridden
- `augment_label()` allows updates to be made to labels
- `augment_bounding_box()` allows updates to bounding boxes to be made
[`RandomShear` serves as a canonical example of how to subclass `BaseImageAugmentationLayer`](https://github.com/keras-team/keras-cv/blob/master/keras_cv/layers/preprocessing/random_shear.py)
## Vectorization
`BaseImageAugmentationLayer` requires you to implement augmentations in an
image-wise basis instead of using a vectorized approach. This design choice
was based made on the results found in the
[vectorization\_strategy\_benchmark.py](../benchmarks/vectorization_strategy_benchmark.py)
benchmark.
In short, the benchmark shows that making use of `tf.vectorized_map()` performs
almost identically to a manually vectorized implementation. As such, we have
decided to rely on `tf.vectorized_map()` for performance.
![Results of vectorization strategy benchmark](images/runtime-plot.png)
## Color Based Preprocessing Layers
Some preprocessing layers in KerasCV perform color based transformations. This
includes `RandomBrightness`, `Equalize`, `Solarization`, and more.
Preprocessing layers that perform color based transformations make the
following assumptions:
- these layers must accept a `value_range`, which is a tuple of numbers.
- `value_range` must default to `(0, 255)`
- input images may be of any `dtype`
The decision to support inputs of any `dtype` is made based on the nuance that
some Keras layers cast user inputs without the user knowing. For example, if
`Solarization` expected user inputs to be of type `int`, and a custom layer
was accidentally casting inputs to `float32`, it would be a bad user experience
to raise an error asserting that all inputs must be of type `int`.
New preprocessing layers should be consistent with these decisions.
# Codesamples
- Import symbols from top level namespaces in code samples (usage docstring for example).
Prefer:
```python
import keras_cv.layers.StochasticDepth
```
to:
```python
keras_cv.layers.regularization.stochastic_depth.StochasticDepth
```
# Call For Contributions
Contributors looking for a task can look at the following list to find an item
to work on. Should you decide to contribute a component, please comment on the
corresponding GitHub issue that you will be working on the component. A team
member will then follow up by assigning the issue to you.
[There is a contributions welcome label available here](https://github.com/keras-team/keras-cv/issues?page=2&q=is%3Aissue+is%3Aopen+label%3Acontribution-welcome)
## How to contribute code
Follow these steps to submit your code contribution.
[You can find a list of issues that we are looking for contributors on here!](https://github.com/keras-team/keras-cv/labels/contribution-welcome)
### Step 1. Open an issue
Before making any changes, we recommend opening an issue (if one doesn't already
exist) and discussing your proposed changes. This way, we can give you feedback
and validate the proposed changes.
If your code change involves the fixing of a bug, please include a
[Colab](https://colab.research.google.com/) notebook that shows
how to reproduce the broken behavior.
If the changes are minor (simple bug fix or documentation fix), then feel free
to open a PR without discussion.
### Step 2. Make code changes
To make code changes, you need to fork the repository. You will need to setup a
development environment and run the unit tests. This is covered in section
"Setup environment".
If your code change involves introducing a new API change, please see our
[API Design Guidelines](API_DESIGN.md).
**Notes**
- Make sure to add a new entry to [serialization tests](https://github.com/keras-team/keras-cv/blob/master/keras_cv/layers/serialization_test.py#L37) for new layers.
### Step 3. Create a pull request
Once the change is ready, open a pull request from your branch in your fork to
the master branch in [keras-team/keras-cv](https://github.com/keras-team/keras-cv).
### Step 4. Sign the Contributor License Agreement
After creating the pull request, you will need to sign the Google CLA agreement.
The agreement can be found at [https://cla.developers.google.com/clas](https://cla.developers.google.com/clas).
### Step 5. Code review
CI tests will automatically be run directly on your pull request. Their
status will be reported back via GitHub actions.
There may be
several rounds of comments and code changes before the pull request gets
approved by the reviewer.
![Approval from reviewer](https://i.imgur.com/zgRziTt.png)
### Step 6. Merging
Once the pull request is approved, a team member will take care of merging.
## Contributing models
When contributing new models, please validate model performance by providing training results. You can do this using our existing [ImageNet training script](https://github.com/keras-team/keras-cv/blob/master/examples/training/classification/imagenet/basic_training.py) or by contributing a custom training script of your own (see "Contributing training scripts" below). Training results can be added to the training history log with [this script](https://github.com/keras-team/keras-cv/blob/master/shell/weights/update_training_history.py), or shared with the team via Google Drive (we'll need TensorBoard logs as well as weights). Either way, the KerasCV team will need to upload the weights to our GCS bucket for distribution.
For an initial submission, trained weights do not need to exactly match paper-claimed results. As a baseline, let's shoot for 90% of the paper-claimed ImageNet top-1 accuracy. However, we should strive to improve these weights quickly to at least match paper-claimed results.
## Contributing training scripts
KerasCV is working to include a catalog of high-performing model training scripts for the models included in KerasCV.models and is welcoming contributions for these scripts. These training scripts serve as documentation of good training techniques and will be used to train weights that will be offered in KerasCV models through the package.
The KerasCV team will run submitted training scripts to produce weights for KerasCV, and will attribute strong weights to contributors via a training script ranking system. Stay tuned for more details about that.
Incremental improvements to existing training scripts are welcome, provided that they come with evidence of improved validation performance.
You can also open an issue to add weights for a specific model using a pre-existing script! In your issue, provide your training logs and resulting weights. Specify the arguments that were used to run the script, and provide support for those choices. If your weights beat our current weights, they'll become our default pre-trained weights for your model/task in KerasCV.models!
To contribute a new script, start by opening an issue and tagging @ianjjohnson and @LukeWood to discuss the task, dataset, and/or model for which you'd like to add a script. Once they've taken a look, you can prepare a PR to introduce the new training script.
See [this example script](https://github.com/keras-team/keras-cv/blob/master/examples/training/classification/imagenet/basic_training.py) for training ImageNet classification. Please follow the structure of this training script in contributing your own script. New scripts should either:
- Train a task for which we don't have a training script already
- Include a meaningfully different training approach for a given task
- Introduce a custom training method for a specific model or dataset, based on empirical evidence of efficacy.
When contributing training scripts or proposing runs, please include documentation to support decisions about training including hyperparameter choices. Examples of good documentation would be recent literature or a reference to a hyperparameter search.
Our default training scripts train using ImageNet. Because we cannot distribute this dataset, you will need to modify your dataloading step to load the dataset on your system if you wish to run training yourself. You are also welcome to locally train against a different dataset, provided that you include documentation in your PR supporting the claim that your script will still perform well against ImageNet.
We look forward to delivering great pre-trained models in KerasCV with the help of your contributions!
## Contributing custom ops
We do not plan to accept contributed custom ops due to the maintenance burden that they introduce. If there is a clear need for a specific custom op that should live in KerasCV, please consult the KerasCV team before implementing it, as we expect to reject contributions of custom ops by default.
We currently support only a small handful of ops that run on CPU and are not used at inference time.
If you are updating existing custom ops, you can re-compile the binaries from source using the instructions in the `Tests that require custom ops` section below.
## Setup environment
Setting up your KerasCV development environment requires you to fork the KerasCV repository,
clone the repository, install dependencies, and execute `python setup.py develop`.
You can achieve this by running the following commands:
```shell
gh repo fork keras-team/keras-cv --clone --remote
cd keras-cv
pip install ".[tests]"
python setup.py develop
```
The first line relies on having an installation of [the GitHub CLI](https://github.com/cli/cli).
Following these commands you should be able to run the tests using `pytest keras_cv`.
Please report any issues running tests following these steps.
Note that this will _not_ install custom ops. If you'd like to install custom ops from source, you can compile the binaries and add them to your local environment manually (requires Bazel):
```shell
python build_deps/configure.py
bazel build keras_cv/custom_ops:all
mv bazel-bin/keras_cv/custom_ops/*.so keras_cv/custom_ops
```
## Run tests
KerasCV is tested using [PyTest](https://docs.pytest.org/en/6.2.x/).
### Run a test file
To run a test file, run `pytest path/to/file` from the root directory of keras\_cv.
### Run a single test case
To run a single test, you can use `-k=<your_regex>`
to use regular expression to match the test you want to run. For example, you
can use the following command to run all the tests in `cut_mix_test.py`,
whose names contain `label`,
```
pytest keras_cv/layers/preprocessing/cut_mix_test.py -k="label"
```
### Run all tests
You can run the unit tests for KerasCV by running:
```
pytest keras_cv/
```
### Tests that require custom ops
For tests that require custom ops, you'll have to compile the custom ops and make them available to your local Python code:
```shell
python build_deps/configure.py
bazel build keras_cv/custom_ops:all
cp bazel-bin/keras_cv/custom_ops/*.so keras_cv/custom_ops/
```
Tests which use custom ops are disabled by default, but can be run by setting the environment variable `TEST_CUSTOM_OPS=true`.
## Formatting the Code
We use `flake8`, `isort` and `black` for code formatting. You can run
the following commands manually every time you want to format your code:
- Run `shell/format.sh` to format your code
- Run `shell/lint.sh` to check the result.
If after running these the CI flow is still failing, try updating `flake8`, `isort` and `black`.
This can be done by running `pip install --upgrade black`, `pip install --upgrade flake8`, and
`pip install --upgrade isort`.
Note: The linting checks could be automated activating
pre-commit hooks with `git config core.hooksPath .github/.githooks`
## Community Guidelines
This project follows [Google's Open Source Community Guidelines](https://opensource.google/conduct/).
---
name: Feature request
about: Suggest an idea for this project
title: ''
labels: ''
assignees: ''
---
**Short Description**
<!---
A clear and concise description of what the feature is.
-->
**Papers**
<!---
Include citation counts if possible.
-->
**Existing Implementations**
<!---
Link to existing implementations. TensorFlow implementations are preferred.
-->
**Other Information**
---
name: Other issue template
about: Describe this issue template's purpose here.
title: ''
labels: ''
assignees: ''
---
# What does this PR do?
<!--
Congratulations! You've made it this far! You're not quite done yet though.
Once merged, your PR is going to appear in the release notes with the title you set, so make sure it's a great title that fully reflects the extent of your awesome contribution.
Then, please replace this with a description of the change and which issue is fixed (if applicable). Please also include relevant motivation and context. List any dependencies (if any) that are required for this change.
Once you're done, someone will review your PR shortly (see the section "Who can review?" below to tag some potential reviewers). They may suggest changes to make the code even better. If no one reviewed your PR after a week has passed, don't hesitate to post a new comment @-mentioning the same persons --sometimes notifications get lost.
-->
<!-- Remove if not applicable -->
Fixes # (issue)
## Before submitting
- [ ] This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
- [ ] Did you read the [contributor guideline](https://github.com/keras-team/keras-cv/blob/master/.github/CONTRIBUTING.md),
Pull Request section?
- [ ] Was this discussed/approved via a Github issue? Please add a link
to it if that's the case.
- [ ] Did you write any new necessary tests?
- [ ] If this adds a new model, can you run a few training steps on TPU in Colab to ensure that no XLA incompatible OP are used?
## Who can review?
Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.
<!--
Feel free to tag @LukeWood and @tanzhenyu in your reviews.
-->
<!--
This PR template is copied and modified from here:
https://github.com/huggingface/transformers/blob/main/.github/PULL_REQUEST_TEMPLATE.md
-->
# Roadmap
The team will release 2 quarters of roadmap in advance so contributors will know
what we are working on and be better aligned when creating PRs.
As an exception, widely used backbones are always welcome. Contributors can search for `contribution-welcome` label on github.
The team will release one minor version upgrade each quarter, or whenever a new task is officially supported.
2023 Q1:
- 3D Object Detection task, the team focuses on providing a point cloud based model (specific model TBD)
- OCR task, the team opens up contribution.
- Keypoint and Action Detection task, the team opens up contribution for Simple Pose, i3d etc.
- Open Vocabulary Object Detection task, built on top of 2D Object Detection.
2022 Q4:
- 2D Semantic Segmentation task, the team focuses on DeepLabV3 and opens up contribution
for UNet/FCN.
- 3D Object Detection task, the team focuses on providing point cloud data pipeline.
- 2D Instance Segmentation task, the team focuses on MaskRCNN meta arch built on top of FasterRCNN.
- Text-to-image task, more specifically built on top of StableDiffusion for image impainting and text-to-video.
- Refactoring data augmentation layers:
- refactoring bounding box input format
- `RaggedTensor` support for data augmentation layers, contribution welcome.
2022 Q3:
- 2D Object Detection task, the team focuses on RetinaNet and FasterRCNN meta arch, and opens
up contribution for YOLOv3, YOLOx and CenterNet.
- StableDiffusion model with pretrained weights
name: Tests
on:
push:
pull_request:
release:
types: [created]
jobs:
test:
name: Test the code
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [windows-latest, ubuntu-latest]
steps:
- uses: actions/checkout@v2
- name: Set up Python 3.7
uses: actions/setup-python@v1
with:
python-version: 3.7
- name: Get pip cache dir
id: pip-cache
run: |
python -m pip install --upgrade pip setuptools
echo "::set-output name=dir::$(pip cache dir)"
- name: pip cache
uses: actions/cache@v2
with:
path: ${{ steps.pip-cache.outputs.dir }}
key: ${{ runner.os }}-pip-${{ hashFiles('setup.py') }}
restore-keys: |
${{ runner.os }}-pip-
- name: Install dependencies
run: |
pip install tensorflow-cpu==2.10.0
pip install -e ".[tests]" --progress-bar off --upgrade
- name: Build custom ops for tests
run: |
python build_deps/configure.py
bazel build keras_cv/custom_ops:all
cp bazel-bin/keras_cv/custom_ops/*.so keras_cv/custom_ops/
- name: Test with pytest
env:
TEST_CUSTOM_OPS: true
run: |
pytest keras_cv/ --ignore keras_cv/models
format:
name: Check the code format
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up Python 3.7
uses: actions/setup-python@v1
with:
python-version: 3.7
- name: Get pip cache dir
id: pip-cache
run: |
python -m pip install --upgrade pip setuptools
echo "::set-output name=dir::$(pip cache dir)"
- name: pip cache
uses: actions/cache@v2
with:
path: ${{ steps.pip-cache.outputs.dir }}
key: ${{ runner.os }}-pip-${{ hashFiles('setup.py') }}
restore-keys: |
${{ runner.os }}-pip-
- name: Install dependencies
run: |
pip install tensorflow==2.10.0
pip install -e ".[tests]" --progress-bar off --upgrade
- name: Lint
run: bash shell/lint.sh
deploy:
needs: [test, format]
if: github.event_name == 'release' && github.event.action == 'created'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up Python
uses: actions/setup-python@v1
with:
python-version: 3.7
- name: Install dependencies
run: |
pip install tensorflow==2.10.0
python -m pip install --upgrade pip setuptools wheel twine
- name: Build and publish
env:
TWINE_USERNAME: ${{ secrets.PYPI_USERNAME }}
TWINE_PASSWORD: ${{ secrets.PYPI_PASSWORD }}
run: |
python setup.py sdist bdist_wheel
twine upload dist/*
name: 'devcontainer'
on: # rebuild any PRs and main branch changes
pull_request:
paths:
- 'setup.*'
- '.devcontainer/**'
push:
paths:
- 'setup.*'
- '.devcontainer/**'
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout (GitHub)
uses: actions/checkout@v2
- name: Build and run dev container task
uses: devcontainers/ci@v0.2
with:
runCmd: pytest keras_cv/ --ignore keras_cv/models
keras_cv.egg-info/
wandb/
logs/
dist/
.DS_Store
build/
*.swp
.idea
*.pyc
.pytest_cache
*.egg-info
__pycache__/
#VS Code files and container
.vscode/
.devcontainer/
.coverage
sh_binary(
name = "build_pip_pkg",
srcs = ["build_deps/build_pip_pkg.sh"],
data = [
"LICENSE",
"MANIFEST.in",
"README.md",
"setup.py",
"//keras_cv",
],
)
Copyright 2022 The KerasCV Authors. All rights reserved.
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "[]"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.
Copyright 2015, The TensorFlow Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
recursive-include keras_cv/custom_ops *.so
Introduction:
Keras-CV源码及训练代码示例,TensorFlow版本为2.9,python版本支持3.7、3.8、3.9.
支持的网络有 ResNet101、ResNet50V2、RegNetX064、DarkNet53、DenseNet121、EfficientNetV2S
网络模型代码位于keras-cv/models.训练代码basic_training.py位于examples/training/classification/imagenet/
使用tfrecord格式的Imagenet数据集.相关网络的训练情况及参数设置位于examples/training/classification/imagenet/
rain_history.json
Install
安装DTK版本的TensorFlow2.9 pip install tensorflow-2.9.0+git28e158bd.dtk22042-cp37-cp37m-linux_x86_64.whl
执行python setup.py build
python setup.py install
Train
训练脚本位于examples/training/classification/imagenet/trian_keras_cv.sh 注意basic_training.py中的分布式设置为
默认使用所有检测到的加速卡设备,若要指定几张加速卡参与训练使用export HIP_VISIBLE_DEVICES=x1,x2,x3来指定.
\ No newline at end of file
import math
import random
import time
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns
import tensorflow as tf
import keras_cv
from keras_cv.metrics import coco
def produce_random_data(include_confidence=False, num_images=128, classes=20):
"""Generates a fake list of bounding boxes for use in this test.
Returns:
a tensor list of size [128, 25, 5/6]. This represents 128 images, 25 bboxes
and 5/6 dimensions to represent each bbox depending on if confidence is
set.
"""
images = []
for _ in range(num_images):
num_boxes = math.floor(25 * random.uniform(0, 1))
classes_in_image = np.floor(np.random.rand(num_boxes, 1) * classes)
bboxes = np.random.rand(num_boxes, 4)
boxes = np.concatenate([bboxes, classes_in_image], axis=-1)
if include_confidence:
confidence = np.random.rand(num_boxes, 1)
boxes = np.concatenate([boxes, confidence], axis=-1)
images.append(
keras_cv.utils.bounding_box.xywh_to_corners(
tf.constant(boxes, dtype=tf.float32)
)
)
images = [
keras_cv.bounding_box.pad_batch_to_shape(x, [25, images[0].shape[1]])
for x in images
]
return tf.stack(images, axis=0)
y_true = produce_random_data()
y_pred = produce_random_data(include_confidence=True)
class_ids = list(range(20))
bucket_values = [500, 1000, 2000, 3500, 5000, 7500, 10000]
update_state_runtimes = []
result_runtimes = []
end_to_end_runtimes = []
for buckets in bucket_values:
metric = coco.COCOMeanAveragePrecision(class_ids, num_buckets=buckets)
# warm up
metric.update_state(y_true, y_pred)
metric.result()
start = time.time()
metric.update_state(y_true, y_pred)
update_state_done = time.time()
r = metric.result()
end = time.time()
update_state_runtimes.append(update_state_done - start)
result_runtimes.append(end - update_state_done)
end_to_end_runtimes.append(end - start)
print("end_to_end_runtimes", end_to_end_runtimes)
data = pd.DataFrame(
{
"bucket_values": bucket_values,
"update_state_runtimes": update_state_runtimes,
"result_runtimes": result_runtimes,
"end_to_end_runtimes": end_to_end_runtimes,
}
)
sns.lineplot(data=data, x="bucket_values", y="update_state_runtimes")
plt.xlabel("Number of Confidence Buckets")
plt.ylabel("update_state() runtime (seconds)")
plt.title("Runtime of update_state()")
plt.show()
sns.lineplot(data=data, x="bucket_values", y="result_runtimes")
plt.xlabel("Number of Confidence Buckets")
plt.ylabel("result() runtime (seconds)")
plt.title("Runtime of result()")
plt.show()
sns.lineplot(data=data, x="bucket_values", y="end_to_end_runtimes")
plt.xlabel("Number of Confidence Buckets")
plt.ylabel("End to end runtime (seconds)")
plt.title("Runtimes of update_state() followed by result()")
plt.show()
import math
import random
import time
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns
import tensorflow as tf
import keras_cv
from keras_cv.metrics import coco
def produce_random_data(include_confidence=False, num_images=128, classes=20):
"""Generates a fake list of bounding boxes for use in this test.
Returns:
a tensor list of size [128, 25, 5/6]. This represents 128 images, 25 bboxes
and 5/6 dimensions to represent each bbox depending on if confidence is
set.
"""
images = []
for _ in range(num_images):
num_boxes = math.floor(25 * random.uniform(0, 1))
classes_in_image = np.floor(np.random.rand(num_boxes, 1) * classes)
bboxes = np.random.rand(num_boxes, 4)
boxes = np.concatenate([bboxes, classes_in_image], axis=-1)
if include_confidence:
confidence = np.random.rand(num_boxes, 1)
boxes = np.concatenate([boxes, confidence], axis=-1)
images.append(
keras_cv.utils.bounding_box.xywh_to_corners(
tf.constant(boxes, dtype=tf.float32)
)
)
images = [
keras_cv.bounding_box.pad_batch_to_shape(x, [25, images[0].shape[1]])
for x in images
]
return tf.stack(images, axis=0)
y_true = produce_random_data()
y_pred = produce_random_data(include_confidence=True)
class_ids = list(range(20))
n_images = [128, 256, 512, 512 + 256, 1024]
update_state_runtimes = []
result_runtimes = []
end_to_end_runtimes = []
for images in n_images:
y_true = produce_random_data(num_images=images)
y_pred = produce_random_data(num_images=images, include_confidence=True)
metric = coco.COCOMeanAveragePrecision(class_ids)
# warm up
metric.update_state(y_true, y_pred)
metric.result()
start = time.time()
metric.update_state(y_true, y_pred)
update_state_done = time.time()
r = metric.result()
end = time.time()
update_state_runtimes.append(update_state_done - start)
result_runtimes.append(end - update_state_done)
end_to_end_runtimes.append(end - start)
print("end_to_end_runtimes", end_to_end_runtimes)
data = pd.DataFrame(
{
"n_images": n_images,
"update_state_runtimes": update_state_runtimes,
"result_runtimes": result_runtimes,
"end_to_end_runtimes": end_to_end_runtimes,
}
)
sns.lineplot(data=data, x="n_images", y="update_state_runtimes")
plt.xlabel("Number of Images")
plt.ylabel("update_state() runtime (seconds)")
plt.title("Runtime of update_state()")
plt.show()
sns.lineplot(data=data, x="n_images", y="result_runtimes")
plt.xlabel("Number of Images")
plt.ylabel("result() runtime (seconds)")
plt.title("Runtime of result()")
plt.show()
sns.lineplot(data=data, x="n_images", y="end_to_end_runtimes")
plt.xlabel("Number of Images")
plt.ylabel("End to end runtime (seconds)")
plt.title("Runtimes of update_state() followed by result()")
plt.show()
import math
import random
import time
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns
import tensorflow as tf
import keras_cv
from keras_cv.metrics import coco
def produce_random_data(include_confidence=False, num_images=128, classes=20):
"""Generates a fake list of bounding boxes for use in this test.
Returns:
a tensor list of size [128, 25, 5/6]. This represents 128 images, 25 bboxes
and 5/6 dimensions to represent each bbox depending on if confidence is
set.
"""
images = []
for _ in range(num_images):
num_boxes = math.floor(25 * random.uniform(0, 1))
classes_in_image = np.floor(np.random.rand(num_boxes, 1) * classes)
bboxes = np.random.rand(num_boxes, 4)
boxes = np.concatenate([bboxes, classes_in_image], axis=-1)
if include_confidence:
confidence = np.random.rand(num_boxes, 1)
boxes = np.concatenate([boxes, confidence], axis=-1)
images.append(
keras_cv.utils.bounding_box.xywh_to_corners(
tf.constant(boxes, dtype=tf.float32)
)
)
images = [
keras_cv.bounding_box.pad_batch_to_shape(x, [25, images[0].shape[1]])
for x in images
]
return tf.stack(images, axis=0)
y_true = produce_random_data()
y_pred = produce_random_data(include_confidence=True)
class_ids = list(range(20))
n_images = [128, 256, 512, 512 + 256, 1024]
update_state_runtimes = []
result_runtimes = []
end_to_end_runtimes = []
for images in n_images:
y_true = produce_random_data(num_images=images)
y_pred = produce_random_data(num_images=images, include_confidence=True)
metric = coco.COCORecall(class_ids)
# warm up
metric.update_state(y_true, y_pred)
metric.result()
start = time.time()
metric.update_state(y_true, y_pred)
update_state_done = time.time()
r = metric.result()
end = time.time()
update_state_runtimes.append(update_state_done - start)
result_runtimes.append(end - update_state_done)
end_to_end_runtimes.append(end - start)
print("end_to_end_runtimes", end_to_end_runtimes)
data = pd.DataFrame(
{
"n_images": n_images,
"update_state_runtimes": update_state_runtimes,
"result_runtimes": result_runtimes,
"end_to_end_runtimes": end_to_end_runtimes,
}
)
sns.lineplot(data=data, x="n_images", y="update_state_runtimes")
plt.xlabel("Number of Images")
plt.ylabel("update_state() runtime (seconds)")
plt.title("Runtime of update_state()")
plt.show()
sns.lineplot(data=data, x="n_images", y="result_runtimes")
plt.xlabel("Number of Images")
plt.ylabel("result() runtime (seconds)")
plt.title("Runtime of result()")
plt.show()
sns.lineplot(data=data, x="n_images", y="end_to_end_runtimes")
plt.xlabel("Number of Images")
plt.ylabel("End to end runtime (seconds)")
plt.title("Runtimes of update_state() followed by result()")
plt.show()
This diff is collapsed.
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment