Skip to content

Commit

Permalink
Merge branch 'master' into port_mobilenet
Browse files Browse the repository at this point in the history
  • Loading branch information
pkgoogle authored Jan 17, 2025
2 parents a5a0bb3 + 2ac95a9 commit 2413f1e
Show file tree
Hide file tree
Showing 311 changed files with 11,489 additions and 2,528 deletions.
11 changes: 3 additions & 8 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,21 @@
"customizations": {
"vscode": {
"settings": {
"python.linting.enabled": true,
"python.linting.flake8Enabled": true,
"python.linting.pylintEnabled": false,
"python.testing.pytestEnabled": true,
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.organizeImports": true
},
"[python]": {
"editor.defaultFormatter": "ms-python.black-formatter"
"editor.defaultFormatter": "charliermarsh.ruff"
},
"editor.rulers": [
80
]
},
"extensions": [
"ms-python.python",
"ms-python.isort",
"ms-python.flake8",
"ms-python.black-formatter"
"charliermarsh.ruff",
"ms-python.python"
]
}
},
Expand Down
8 changes: 4 additions & 4 deletions .github/workflows/actions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
version: [keras-stable]
include:
- backend: jax
version: keras-3.1
version: keras-3.5
- backend: jax
version: keras-nightly
runs-on: ubuntu-latest
Expand Down Expand Up @@ -48,11 +48,11 @@ jobs:
run: |
pip install -r requirements.txt --progress-bar off
pip install --no-deps -e "." --progress-bar off
- name: Pin Keras 3.1
if: ${{ matrix.version == 'keras-3.1'}}
- name: Pin Keras 3.5
if: ${{ matrix.version == 'keras-3.5'}}
run: |
pip uninstall -y keras
pip install keras==3.1.0 --progress-bar off
pip install keras==3.5.0 --progress-bar off
- name: Pin Keras Nightly
if: ${{ matrix.version == 'keras-nightly'}}
run: |
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ build/
.idea/

venv/

.DS_Store
9 changes: 4 additions & 5 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -216,12 +216,11 @@ so you aren't waiting around forever!

## Formatting 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:
KerasHub uses [Ruff](https://docs.astral.sh/ruff/) to format the code. 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`.
If after running these the CI flow is still failing, try updating `ruff`
with `pip install --upgrade ruff`.
120 changes: 67 additions & 53 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,107 +1,121 @@
# KerasHub: Multi-framework Models
# KerasHub: Multi-framework Pretrained Models
[![](https://github.com/keras-team/keras-hub/workflows/Tests/badge.svg?branch=master)](https://github.com/keras-team/keras-hub/actions?query=workflow%3ATests+branch%3Amaster)
![Python](https://img.shields.io/badge/python-v3.9.0+-success.svg)
[![contributions welcome](https://img.shields.io/badge/contributions-welcome-brightgreen.svg?style=flat)](https://github.com/keras-team/keras-hub/issues)

> [!IMPORTANT]
> 📢 KerasNLP is now KerasHub! 📢 Read
> [the announcement](https://github.com/keras-team/keras-hub/issues/1831).
>
> We have renamed the repo to KerasHub in preparation for the release, but have not yet
> released the new package. Follow the announcement for news.
KerasHub is a library that supports natural language processing, computer
vision, audio, and multimodal backbones and task models, working natively with
TensorFlow, JAX, or PyTorch. KerasHub provides a repository of pre-trained
models and a collection of lower-level building blocks for these tasks. Built
on Keras 3, models can be trained and serialized in any framework and re-used
in another without costly migrations.

This library is an extension of the core Keras API; all high-level modules are
Layers and Models that receive that same level of polish as core Keras.
If you are familiar with Keras, congratulations! You already understand most of
KerasHub.
**KerasHub** is a pretrained modeling library that aims to be simple, flexible,
and fast. The library provides [Keras 3](https://keras.io/keras_3/)
implementations of popular model architectures, paired with a collection of
pretrained checkpoints available on [Kaggle Models](https://kaggle.com/models/).
Models can be used with text, image, and audio data for generation, classification,
and many other built in tasks.

KerasHub is an extension of the core Keras API; KerasHub components are provided
as `Layer` and `Model` implementations. If you are familiar with Keras,
congratulations! You already understand most of KerasHub.

All models support JAX, TensorFlow, and PyTorch from a single model
definition and can be fine-tuned on GPUs and TPUs out of the box. Models can
be trained on individual accelerators with built-in PEFT techniques, or
fine-tuned at scale with model and data parallel training. See our
[Getting Started guide](https://keras.io/guides/keras_hub/getting_started)
to start learning our API. Browse our models on
[Kaggle](https://www.kaggle.com/organizations/keras/models).
We welcome contributions.
to start learning our API.

## Quick Links

### For everyone

- [Home Page](https://keras.io/keras_hub)
- [Developer Guides](https://keras.io/guides/keras_hub)
- [API Reference](https://keras.io/api/keras_hub)
- [Pre-trained Models](https://www.kaggle.com/organizations/keras/models)
- [Home page](https://keras.io/keras_hub)
- [Getting started](https://keras.io/keras_hub/getting_started)
- [Guides](https://keras.io/keras_hub/guides)
- [API documentation](https://keras.io/keras_hub/api)
- [Pre-trained models](https://keras.io/keras_hub/presets/)

### For contributors

- [Call for Contributions](https://github.com/keras-team/keras-hub/issues/1835)
- [Roadmap](https://github.com/keras-team/keras-hub/issues/1836)
- [Contributing Guide](CONTRIBUTING.md)
- [Roadmap](ROADMAP.md)
- [Style Guide](STYLE_GUIDE.md)
- [API Design Guide](API_DESIGN_GUIDE.md)
- [Call for Contributions](https://github.com/keras-team/keras-hub/issues?q=is%3Aissue+is%3Aopen+label%3A%22contributions+welcome%22)

## Quickstart

Fine-tune a BERT classifier on IMDb movie reviews:
Choose a backend:

```python
import os
os.environ["KERAS_BACKEND"] = "jax" # Or "tensorflow" or "torch"!
```

Import KerasHub and other libraries:

```python
import keras
import keras_hub
import numpy as np
import tensorflow_datasets as tfds
```

Load a resnet model and use it to predict a label for an image:

```python
classifier = keras_hub.models.ImageClassifier.from_preset(
"resnet_50_imagenet",
activation="softmax",
)
url = "https://upload.wikimedia.org/wikipedia/commons/a/aa/California_quail.jpg"
path = keras.utils.get_file(origin=url)
image = keras.utils.load_img(path)
preds = classifier.predict(np.array([image]))
print(keras_hub.utils.decode_imagenet_predictions(preds))
```

Load a Bert model and fine-tune it on IMDb movie reviews:

```python
classifier = keras_hub.models.BertClassifier.from_preset(
"bert_base_en_uncased",
activation="softmax",
num_classes=2,
)
imdb_train, imdb_test = tfds.load(
"imdb_reviews",
split=["train", "test"],
as_supervised=True,
batch_size=16,
)

# Load a BERT model.
classifier = keras_hub.models.Classifier.from_preset(
"bert_base_en",
num_classes=2,
activation="softmax",
)

# Fine-tune on IMDb movie reviews.
classifier.fit(imdb_train, validation_data=imdb_test)
# Predict two new examples.
classifier.predict(["What an amazing movie!", "A total waste of my time."])
preds = classifier.predict(["What an amazing movie!", "A total waste of time."])
print(preds)
```

Try it out [in a colab](https://colab.research.google.com/drive/1gSWkh3yOLwmKAaNh2dQQ6kQIlnGte7P2?usp=sharing).
For more in depth guides and examples, visit
[keras.io/keras_hub](https://keras.io/keras_hub/).

## Installation

To try out the latest version of KerasHub, you can use
our nightly package:
To install the latest KerasHub release with Keras 3, simply run:

```bash
pip install keras-hub
```
pip install --upgrade keras-hub
```

KerasHub currently requires TensorFlow to be installed for use of the
`tf.data` API for preprocessing. Even when pre-processing with `tf.data`,
training can still happen on any backend.
To install the latest nightly changes for both KerasHub and Keras, you can use
our nightly package.

Read [Getting started with Keras](https://keras.io/getting_started/) for more
information on installing Keras 3 and compatibility with different frameworks.
```
pip install --upgrade keras-hub-nightly
```

> [!IMPORTANT]
> We recommend using KerasHub with TensorFlow 2.16 or later, as TF 2.16 packages
> Keras 3 by default.
Currently, installing KerasHub will always pull in TensorFlow for use of the
`tf.data` API for preprocessing. When pre-processing with `tf.data`, training
can still happen on any backend.

Visit the [core Keras getting started page](https://keras.io/getting_started/)
for more information on installing Keras 3, accelerator support, and
compatibility with different frameworks.

## Configuring your backend

Expand Down
2 changes: 1 addition & 1 deletion benchmarks/text_generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ def next(prompt, state, index):
)
print("Time taken: ", time_taken)
res_handler.write(
f"{sampler},{execution_method}," f"{time_taken}\n"
f"{sampler},{execution_method},{time_taken}\n"
)
print()
print("*************************************")
Expand Down
13 changes: 13 additions & 0 deletions keras_hub/api/layers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from keras_hub.src.layers.modeling.reversible_embedding import (
ReversibleEmbedding,
)
from keras_hub.src.layers.modeling.rms_normalization import RMSNormalization
from keras_hub.src.layers.modeling.rotary_embedding import RotaryEmbedding
from keras_hub.src.layers.modeling.sine_position_encoding import (
SinePositionEncoding,
Expand All @@ -34,12 +35,19 @@
from keras_hub.src.layers.preprocessing.random_deletion import RandomDeletion
from keras_hub.src.layers.preprocessing.random_swap import RandomSwap
from keras_hub.src.layers.preprocessing.start_end_packer import StartEndPacker
from keras_hub.src.models.basnet.basnet_image_converter import (
BASNetImageConverter,
)
from keras_hub.src.models.clip.clip_image_converter import CLIPImageConverter
from keras_hub.src.models.deeplab_v3.deeplab_v3_image_converter import (
DeepLabV3ImageConverter,
)
from keras_hub.src.models.densenet.densenet_image_converter import (
DenseNetImageConverter,
)
from keras_hub.src.models.efficientnet.efficientnet_image_converter import (
EfficientNetImageConverter,
)
from keras_hub.src.models.mit.mit_image_converter import MiTImageConverter
from keras_hub.src.models.mobilenet.mobilenet_image_converter import (
MobileNetImageConverter,
Expand All @@ -50,13 +58,18 @@
from keras_hub.src.models.resnet.resnet_image_converter import (
ResNetImageConverter,
)
from keras_hub.src.models.retinanet.anchor_generator import AnchorGenerator
from keras_hub.src.models.retinanet.retinanet_image_converter import (
RetinaNetImageConverter,
)
from keras_hub.src.models.sam.sam_image_converter import SAMImageConverter
from keras_hub.src.models.sam.sam_mask_decoder import SAMMaskDecoder
from keras_hub.src.models.sam.sam_prompt_encoder import SAMPromptEncoder
from keras_hub.src.models.segformer.segformer_image_converter import (
SegFormerImageConverter,
)
from keras_hub.src.models.vgg.vgg_image_converter import VGGImageConverter
from keras_hub.src.models.vit.vit_image_converter import ViTImageConverter
from keras_hub.src.models.whisper.whisper_audio_converter import (
WhisperAudioConverter,
)
Loading

0 comments on commit 2413f1e

Please sign in to comment.