Skip to content

Commit

Permalink
Improve KerasHub doc content
Browse files Browse the repository at this point in the history
  • Loading branch information
mattdangerw committed Nov 25, 2024
1 parent 378a0cb commit 68fdde7
Show file tree
Hide file tree
Showing 13 changed files with 106 additions and 67 deletions.
44 changes: 25 additions & 19 deletions scripts/hub_master.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
BASE_CLASSES = {
"path": "base_classes/",
"title": "Model Base Classes",
"title": "Modeling API",
"toc": True,
"children": [
{
Expand Down Expand Up @@ -209,7 +209,7 @@

MODELS_MASTER = {
"path": "models/",
"title": "Pretrained Models",
"title": "Model Architectures",
"toc": True,
"children": [
{
Expand Down Expand Up @@ -491,7 +491,7 @@
},
{
"path": "deeplab_v3/",
"title": "DeepLabV3 and DeepLabV3Plus",
"title": "DeepLabV3",
"toc": True,
"children": [
{
Expand Down Expand Up @@ -1725,12 +1725,18 @@
{
"path": "audio_converter",
"title": "AudioConverter layer",
"generate": ["keras_hub.layers.AudioConverter"],
"generate": [
"keras_hub.layers.AudioConverter",
"keras_hub.layers.AudioConverter.from_preset",
],
},
{
"path": "image_converter",
"title": "ImageConverter layer",
"generate": ["keras_hub.layers.ImageConverter"],
"generate": [
"keras_hub.layers.ImageConverter",
"keras_hub.layers.ImageConverter.from_preset",
],
},
{
"path": "start_end_packer",
Expand Down Expand Up @@ -1848,8 +1854,8 @@
"title": "API documentation",
"toc": True,
"children": [
MODELS_MASTER,
BASE_CLASSES,
MODELS_MASTER,
TOKENIZERS_MASTER,
PREPROCESSING_LAYERS_MASTER,
MODELING_LAYERS_MASTER,
Expand All @@ -1860,32 +1866,32 @@

HUB_GUIDES_MASTER = {
"path": "guides/",
"title": "Guides",
"title": "Developer guides",
"toc": True,
"children": [
{
"path": "semantic_segmentation_deeplab_v3",
"title": "Semantic Segmentation with KerasHub",
"path": "upload",
"title": "Uploading Models",
},
{
"path": "transformer_pretraining",
"title": "Pretraining a Transformer from scratch with KerasHub",
"path": "stable_diffusion_3_in_keras_hub",
"title": "Stable Diffusion 3",
},
{
"path": "upload",
"title": "Uploading Models with KerasHub",
"path": "segment_anything_in_keras_hub",
"title": "Segment Anything",
},
{
"path": "classification_with_keras_hub",
"title": "Classification with KerasHub",
"title": "Image Classification",
},
{
"path": "segment_anything_in_keras_hub",
"title": "Segment Anything in KerasHub",
"path": "semantic_segmentation_deeplab_v3",
"title": "Semantic Segmentation",
},
{
"path": "stable_diffusion_3_in_keras_hub",
"title": "Stable Diffusion 3 in KerasHub",
"path": "transformer_pretraining",
"title": "Pretraining a Transformer from scratch",
},
],
}
Expand All @@ -1902,7 +1908,7 @@
HUB_API_MASTER,
{
"path": "presets/",
"title": "Presets",
"title": "Pretrained models list",
},
],
}
4 changes: 3 additions & 1 deletion scripts/render_presets.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,10 @@ def render_table(symbol):
return None
for preset in symbol.presets:
metadata = symbol.presets[preset]["metadata"]
url = symbol.presets[preset]["kaggle_handle"]
url = url.replace("kaggle://", "https://www.kaggle.com/models/")
table += (
f"{preset} | "
f"[{preset}]({url}) | "
f"{format_param_count(metadata)} | "
f"{metadata['description']} \n"
)
Expand Down
2 changes: 1 addition & 1 deletion scripts/tuner_master.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@

TUNER_GUIDES_MASTER = {
"path": "guides/",
"title": "Guides",
"title": "Developer guides",
"toc": True,
"children": [
{
Expand Down
12 changes: 12 additions & 0 deletions templates/keras_hub/api/base_classes/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# KerasHub Modeling API

The following base classes form the API for working with pretrained models
through KerasHub. These base classes can be used with the `from_preset()`
constructor to automatically instantiate a subclass with the correct model
architecture, e.g.
`keras_hub.models.TextClassifier.from_preset("bert_base_en", num_classes=2)`.

For the full list of available pretrained model presets shipped directly by the
Keras team, see the [Pretrained Models](/keras_hub/presets/) page.

{{toc}}
5 changes: 1 addition & 4 deletions templates/keras_hub/api/index.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
# KerasHub API documentation

KerasHub is a toolbox of modular building blocks ranging from pretrained
state-of-the-art models, to low-level Transformer Encoder layers. For an
introduction to the library see the [KerasHub home page](/keras_hub/). For a
high-level introduction to the API see our
[getting started guide](/keras_hub/getting_started/).
state-of-the-art models, to low-level Transformer Encoder layers.

{{toc}}
3 changes: 2 additions & 1 deletion templates/keras_hub/api/metrics/index.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# KerasHub Metrics

KerasHub metrics are `keras.Metric` subclasses for NLP-specific use cases.
KerasHub metrics are `keras.Metric` subclasses that are common to computer
vision and natural language processing workflows.

{{toc}}
7 changes: 7 additions & 0 deletions templates/keras_hub/api/modeling_layers/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# KerasHub Modeling Layers

KerasHub modeling layers are give `keras.layers.Layer` implementations for
building blocks common to pretrained models. They can be used to create a new
model from scratch, or to extend a pretrained model.

{{toc}}
14 changes: 6 additions & 8 deletions templates/keras_hub/api/models/index.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
# KerasHub Models
# KerasHub Model Architectures

KerasHub contains end-to-end implementations of popular model architectures.
These models can be created in two ways:
The following is a list of model architectures supported by KerasHub. These
models can be created in two ways:

- Through the `from_preset()` constructor, which instantiates an object with
a pre-trained configurations, vocabularies, and (optionally) weights.
a pre-trained configurations, vocabularies, and weights.
- Through custom configuration controlled by the user.

For the full list of available presets shipped directly by the Keras team, see
the [Presets page](/keras_hub/presets/)

## API Documentation
For the full list of available pretrained model presets shipped directly by the
Keras team, see the [Pretrained Models](/keras_hub/presets/) page.

{{toc}}
7 changes: 7 additions & 0 deletions templates/keras_hub/api/preprocessing_layers/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# KerasHub Preprocessing Layers

KerasHub preprocessing layers can be used to create custom preprocessing
pipelines for pretrained models. Preprocessing layers are all compatible with
`tf.data`, even when running on the `jax` and `torch` backends.

{{toc}}
7 changes: 7 additions & 0 deletions templates/keras_hub/api/samplers/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# KerasHub Samplers

KerasHub samplers are used to control the sampling process when generating text
with generative models. See `keras_hub.models.CausalLM` for the high-level
generative API.

{{toc}}
59 changes: 29 additions & 30 deletions templates/keras_hub/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,19 @@
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 use for both training and inference, on any of the TensorFlow,
Models can be used for both training and inference, on any of the TensorFlow,
Jax, and Torch backends.

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

See our [Getting Started guide](/guides/keras_hub/getting_started)
to start learning our API. We welcome
[contributions](https://github.com/keras-team/keras-hub/issues/1835).

---
## Quick links

* [Getting started with KerasHub](/keras_hub/getting_started/)
* [Guides](/keras_hub/guides)
* [API reference](/keras_hub/api/)
* [Developer guides](/keras_hub/guides)
* [API documentation](/keras_hub/api/)
* [KerasHub on GitHub](https://github.com/keras-team/keras-hub)
* [KerasHub models on Kaggle](https://www.kaggle.com/organizations/keras/models)
* [Pretrained model list](/keras_hub/presets/)
Expand All @@ -43,62 +39,65 @@ our nightly package.
pip install --upgrade keras-hub-nightly
```

Note that currently, installing KerasHub will always pull in TensorFlow for use
of the `tf.data` API for preprocessing. Even when pre-processing with `tf.data`,
training can still happen on any backend. Read
[Getting started with Keras](https://keras.io/getting_started/) for more
information on installing Keras 3 and compatibility with different frameworks.
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.

**Note:** We recommend using KerasHub with TensorFlow 2.16 or later, as TF 2.16
packages Keras 3 by default.
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.

---
## Quickstart

Below is a quick example using ResNet to predict an image, and BERT to train a
classifier:
Choose a backend:

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

Import KerasHub and other libraries:

```python
# Import libraries.
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:

# Load a ResNet model.
```python
classifier = keras_hub.models.ImageClassifier.from_preset(
"resnet_50_imagenet",
activation="softmax",
)
# Predict a label for a single image.
image_url = "https://upload.wikimedia.org/wikipedia/commons/a/aa/California_quail.jpg"
image_path = keras.utils.get_file(origin=image_url)
image = keras.utils.load_img(image_path)
batch = np.array([image])
preds = classifier.predict(batch)
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:

# Load a BERT model.
```python
classifier = keras_hub.models.BertClassifier.from_preset(
"bert_base_en_uncased",
activation="softmax",
num_classes=2,
)

# Fine-tune on IMDb movie reviews.
imdb_train, imdb_test = tfds.load(
"imdb_reviews",
split=["train", "test"],
as_supervised=True,
batch_size=16,
)
classifier.fit(imdb_train, validation_data=imdb_test)
# Predict two new examples.
preds = 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)
```

Expand Down
2 changes: 2 additions & 0 deletions templates/keras_hub/presets/index.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# KerasHub pretrained models

Below, we list all presets available in the KerasHub library. For more detailed
usage, browse the docstring for a particular class. For an in depth introduction
to our API, see the [getting started guide](/keras_hub/getting_started/).
Expand Down
7 changes: 4 additions & 3 deletions templates/keras_tuner/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ experiment with new search algorithms.
## Quick links

* [Getting started with KerasTuner](/keras_tuner/getting_started/)
* [Guides](/keras_tuner/guides)
* [API reference](/keras_tuner/api/)
* [Developer guides](/keras_tuner/guides)
* [API documentation](/keras_tuner/api/)
* [KerasTuner on GitHub](https://github.com/keras-team/keras-tuner)


Expand Down Expand Up @@ -74,7 +74,8 @@ tuner.search(x_train, y_train, epochs=5, validation_data=(x_val, y_val))
best_model = tuner.get_best_models()[0]
```

To learn more about KerasTuner, check out [this starter guide](https://keras.io/guides/keras_tuner/getting_started/).
To learn more about KerasTuner, check out the
[getting stated guide](https://keras.io/guides/keras_tuner/getting_started/).


---
Expand Down

0 comments on commit 68fdde7

Please sign in to comment.