### Install KerasCV and Keras Source: https://github.com/keras-team/keras-cv/blob/master/keras_cv/src/tools/training_scipts/training_deeplab_v3_plus.ipynb Install the necessary KerasCV and Keras packages. Use the `-q` flag for quiet installation and `--upgrade` to ensure the latest versions are installed. ```python !pip install -q --upgrade keras-cv !pip install -q --upgrade keras # Upgrade to Keras 3. ``` -------------------------------- ### Install KerasCV with Custom Ops from Source Source: https://github.com/keras-team/keras-cv/blob/master/README.md Use these commands to clone the KerasCV repository, configure build dependencies, build the package with custom ops enabled, and install the resulting wheel file. Ensure Bazel is installed and configured. ```bash git clone https://github.com/keras-team/keras-cv.git cd keras-cv python3 build_deps/configure.py bazel build build_pip_pkg export BUILD_WITH_CUSTOM_OPS=true bazel-bin/build_pip_pkg wheels pip install wheels/keras_cv-*.whl ``` -------------------------------- ### Install KerasCV and Keras Core Source: https://github.com/keras-team/keras-cv/blob/master/keras_cv/src/tools/training_scipts/Training_YOLOv8.ipynb Install the KerasCV and Keras Core libraries. This is a prerequisite for using KerasCV functionalities. ```python !pip install keras-cv keras-core ``` -------------------------------- ### Install KerasCV from GitHub Source: https://github.com/keras-team/keras-cv/blob/master/keras_cv/src/tools/checkpoint_conversion/ViT_weight_conversion.ipynb Use this command to install KerasCV directly from a GitHub repository, specifying a branch. Ensure you have pip and git installed. ```bash Collecting git+https://github.com/DavidLandup0/keras-cv@vit Cloning https://github.com/DavidLandup0/keras-cv (to revision vit) to /tmp/pip-req-build-76ui5l8a Running command git clone -q https://github.com/DavidLandup0/keras-cv /tmp/pip-req-build-76ui5l8a Running command git checkout -b vit --track origin/vit Switched to a new branch 'vit' Branch 'vit' set up to track remote branch 'vit' from 'origin'. ``` -------------------------------- ### Install KerasCV and TensorFlow Source: https://github.com/keras-team/keras-cv/blob/master/keras_cv/src/tools/checkpoint_conversion/ViT_weight_conversion.ipynb Use these commands to install the latest version of KerasCV from GitHub and ensure TensorFlow is up-to-date. This is typically run in a Python environment like Google Colab. ```bash !pip install --upgrade tensorflow !pip install "git+https://github.com/DavidLandup0/keras-cv@vit" ``` -------------------------------- ### Load and Preprocess Example Image Source: https://github.com/keras-team/keras-cv/blob/master/keras_cv/src/tools/checkpoint_conversion/ViT_weight_conversion.ipynb Demonstrates loading an image from a URL, converting it to an array, preprocessing it, and adding a batch dimension for model inference. Requires `tensorflow` and the `url_to_array` and `preprocess_image` functions. ```python import tensorflow as tf cat = "https://upload.wikimedia.org/wikipedia/commons/thumb/4/4d/Cat_November_2010-1a.jpg/1200px-Cat_November_2010-1a.jpg" cat_img = url_to_array(cat) cat_img, _ = preprocess_image(cat_img, None) cat_img = tf.expand_dims(cat_img, 0) ``` -------------------------------- ### Import KerasCV and Keras Modules Source: https://github.com/keras-team/keras-cv/blob/master/keras_cv/src/tools/training_scipts/training_deeplab_v3_plus.ipynb Import core Keras modules, KerasCV, NumPy, and the Pascal VOC dataset loader. Ensure these libraries are installed and configured before running. ```python import keras from keras import ops import keras_cv import numpy as np from keras_cv.datasets.pascal_voc.segmentation import load as load_voc ``` -------------------------------- ### Link to ImageNet Training Script Source: https://github.com/keras-team/keras-cv/blob/master/CONTRIBUTING.md An example script for training models on ImageNet, useful for validating model performance. ```markdown [ImageNet training script](https://github.com/keras-team/keras-cv/blob/master/examples/training/classification/imagenet/basic_training.py) ``` -------------------------------- ### Load Image and Predict with YOLOv8 Detector Source: https://github.com/keras-team/keras-cv/blob/master/README.md Example demonstrating loading an image, resizing it, and using a pre-trained YOLOv8 model for prediction. Ensure the KERAS_BACKEND is set before importing Keras. ```python import keras_cv import keras filepath = keras.utils.get_file(origin="https://i.imgur.com/gCNcJJI.jpg") image = np.array(keras.utils.load_img(filepath)) image_resized = keras.ops.image.resize(image, (640, 640))[None, ...] model = keras_cv.models.YOLOV8Detector.from_preset( "yolo_v8_m_pascalvoc", bounding_box_format="xywh", ) predictions = model.predict(image_resized) ``` -------------------------------- ### Load and Prepare VOC Datasets Source: https://github.com/keras-team/keras-cv/blob/master/keras_cv/src/tools/training_scipts/Training_YOLOv8.ipynb Loads the VOC2007 and VOC2012 datasets, concatenates them, and preprocesses them for object detection. Ensure TensorFlow Datasets (tfds) is installed. ```python train_ds = tfds.load( "voc/2007", split="train+validation", with_info=False, shuffle_files=True, # , data_dir="gs://kerascv-dataset" ) train_ds = train_ds.concatenate( tfds.load( "voc/2012", split="train+validation", with_info=False, shuffle_files=True, # data_dir="gs://kerascv-dataset" ) ) eval_ds = tfds.load( "voc/2007", split="test", with_info=False ) # , data_dir="gs://kerascv-dataset") ``` -------------------------------- ### Install KerasCV Stable with Keras 3 Source: https://github.com/keras-team/keras-cv/blob/master/README.md Install stable versions of KerasCV and Keras 3. Ensure Keras 3 is installed after KerasCV. ```bash pip install --upgrade keras-cv tensorflow pip install --upgrade keras ``` -------------------------------- ### Install KerasCV Nightly with Keras 3 Source: https://github.com/keras-team/keras-cv/blob/master/README.md Install the latest changes for KerasCV and Keras 3 using the nightly package. ```bash pip install --upgrade keras-cv-nightly tf-nightly ``` -------------------------------- ### Install KerasCV with Keras 2 Source: https://github.com/keras-team/keras-cv/blob/master/README.md Use this command to install the latest KerasCV release when using Keras 2. ```bash pip install --upgrade keras-cv tensorflow ``` -------------------------------- ### Format Code with Shell Script Source: https://github.com/keras-team/keras-cv/blob/master/CONTRIBUTING.md Run this shell script to format your code according to project standards. Ensure you have the necessary formatting tools installed. ```shell shell/format.sh ``` -------------------------------- ### Install KerasCV from GitHub Source: https://github.com/keras-team/keras-cv/blob/master/keras_cv/src/tools/training_scipts/Training_YOLOv8.ipynb Uninstall the current version of KerasCV and install a specific version from a GitHub repository. This is useful for testing development branches or specific commits. ```python !pip uninstall -y keras-cv !pip install git+https://github.com/ianstenbit/keras-cv.git@task-aligned-assignment ``` -------------------------------- ### Image Classification Pipeline with Augmentation Source: https://context7.com/keras-team/keras-cv/llms.txt An end-to-end example for training an image classifier using a pretrained backbone and data augmentation. Requires TensorFlow Datasets and KerasCV. ```python import keras_cv import keras import tensorflow as tf import tensorflow_datasets as tfds # Configuration BATCH_SIZE = 32 NUM_CLASSES = 3 EPOCHS = 10 # Create augmentation pipeline augmenter = keras_cv.layers.Augmenter([ keras_cv.layers.RandomFlip(), keras_cv.layers.RandAugment(value_range=(0, 255), magnitude=0.5), keras_cv.layers.CutMix(), ]) def preprocess_train(images, labels): labels = tf.one_hot(labels, NUM_CLASSES) inputs = {"images": images, "labels": labels} outputs = augmenter(inputs) return outputs["images"], outputs["labels"] def preprocess_eval(images, labels): labels = tf.one_hot(labels, NUM_CLASSES) return images, labels # Load dataset train_ds, test_ds = tfds.load( "rock_paper_scissors", as_supervised=True, split=["train", "test"], ) train_ds = train_ds.batch(BATCH_SIZE).map( preprocess_train, num_parallel_calls=tf.data.AUTOTUNE ).prefetch(tf.data.AUTOTUNE) test_ds = test_ds.batch(BATCH_SIZE).map( preprocess_eval, num_parallel_calls=tf.data.AUTOTUNE ).prefetch(tf.data.AUTOTUNE) # Create model with pretrained backbone backbone = keras_cv.models.EfficientNetV2B0Backbone.from_preset( "efficientnetv2_b0_imagenet" ) model = keras_cv.models.ImageClassifier( backbone=backbone, num_classes=NUM_CLASSES, activation="softmax", ) # Compile and train model.compile( optimizer=keras.optimizers.Adam(learning_rate=1e-4), loss="categorical_crossentropy", metrics=["accuracy"], ) model.fit(train_ds, validation_data=test_ds, epochs=EPOCHS) ``` -------------------------------- ### Import necessary libraries for KerasCV Source: https://github.com/keras-team/keras-cv/blob/master/keras_cv/src/tools/training_scipts/Training_YOLOv8.ipynb Import TensorFlow, TensorFlow Datasets, and other required libraries for KerasCV development. This setup is typical for training deep learning models. ```python # Copyright 2022 The KerasCV 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 # # https://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. """ Title: Train an Object Detection Model on Pascal VOC 2007 using KerasCV Author: [lukewood](https://github.com/LukeWood), [tanzhenyu](https://github.com/tanzhenyu) Date created: 2022/09/27 Last modified: 2023/03/29 Description: Use KerasCV to train a RetinaNet on Pascal VOC 2007. """ import resource import sys import tensorflow as tf import tensorflow_datasets as tfds import tqdm from tensorflow import keras import keras_cv # Temporarily need PyCOCOCallback to verify # a 1:1 comparison with the PyMetrics version. from keras_cv.callbacks import PyCOCOCallback low, high = resource.getrlimit(resource.RLIMIT_NOFILE) resource.setrlimit(resource.RLIMIT_NOFILE, (high, high)) ``` -------------------------------- ### Create an Augmentation Pipeline with Augmenter Source: https://context7.com/keras-team/keras-cv/llms.txt Chains multiple KerasCV augmentation layers using the Augmenter class. This example includes RandomFlip, RandomBrightness, RandomContrast, and RandAugment. ```python import keras_cv import numpy as np images = np.ones((16, 256, 256, 3), dtype="float32") * 127.5 augmenter = keras_cv.layers.Augmenter([ keras_cv.layers.RandomFlip(mode="horizontal"), keras_cv.layers.RandomBrightness(factor=0.2, value_range=(0, 255)), keras_cv.layers.RandomContrast(factor=0.2, value_range=(0, 255)), keras_cv.layers.RandAugment(value_range=(0, 255), magnitude=0.3), ]) augmented_images = augmenter({"images": images}) ``` -------------------------------- ### Set Up Callbacks and Train the Model Source: https://github.com/keras-team/keras-cv/blob/master/keras_cv/src/tools/training_scipts/Training_YOLOv8.ipynb Configures callbacks for evaluation, logging, and model checkpointing, then initiates the model training process. ```python callbacks = [ keras_cv.callbacks.PyCOCOCallback(eval_ds, bounding_box_format="xywh"), keras.callbacks.TensorBoard("gs://ian-kerascv/yolov8-gpu-logs-v4"), keras.callbacks.ModelCheckpoint( "./weights.h5", save_best_only=True, save_weights_only=True ), ] history = model.fit( train_ds, validation_data=eval_ds, epochs=120, callbacks=callbacks, ) ``` -------------------------------- ### Download and Inspect Model Checkpoint Source: https://github.com/keras-team/keras-cv/blob/master/keras_cv/src/tools/checkpoint_conversion/ViT_weight_conversion.ipynb This snippet shows how to construct a cloud storage path for a model checkpoint, print its size, and download it locally using gsutil. ```python filename = best_model_i1k_checkpoint path = f"gs://vit_models/augreg/{filename}.npz" print(f"{tf.io.gfile.stat(path).length / 1024 / 1024:.1f} MiB - {path}") ``` ```python !gsutil cp {path} . ``` -------------------------------- ### Instantiate CLIP Model Source: https://github.com/keras-team/keras-cv/blob/master/keras_cv/src/tools/checkpoint_conversion/clip_weights_conversion.ipynb Instantiates the CLIP model using parameters extracted from the configuration dictionary. Ensure all required parameters are correctly set. ```python embed_dim = MODEL_CONFIGS[config_name]["embed_dim"] context_length = MODEL_CONFIGS[config_name]["context_length"] vocab_size = MODEL_CONFIGS[config_name]["vocab_size"] transformer_width = MODEL_CONFIGS[config_name]["transformer_width"] transformer_heads = MODEL_CONFIGS[config_name]["transformer_heads"] transformer_layers = MODEL_CONFIGS[config_name]["transformer_layers"] vision_layers = MODEL_CONFIGS[config_name]["vision_layers"] vision_width = MODEL_CONFIGS[config_name]["vision_width"] vision_patch_size = MODEL_CONFIGS[config_name]["vision_patch_size"] image_resolution = MODEL_CONFIGS[config_name]["image_resolution"] model = CLIP( embed_dim, image_resolution, vision_layers, vision_width, vision_patch_size, context_length, vocab_size, transformer_width, transformer_heads, transformer_layers, ) ``` -------------------------------- ### Create and Train DeepLabV3+ Model Source: https://context7.com/keras-team/keras-cv/llms.txt Demonstrates how to create a DeepLabV3+ model with a ResNet50V2 backbone, prepare data, compile, train, and perform inference. Ensure necessary imports for Keras, NumPy, and KerasCV. ```python import numpy as np import keras import keras_cv backbone = keras_cv.models.ResNet50V2Backbone(input_shape=[256, 256, 3]) model = keras_cv.models.DeepLabV3Plus( num_classes=21, # Including background backbone=backbone, projection_filters=48, ) # Prepare data images = np.ones((4, 256, 256, 3), dtype="float32") masks = np.zeros((4, 256, 256, 21), dtype="float32") masks[:, :, :, 0] = 1.0 # Background class # Compile and train model.compile( optimizer=keras.optimizers.Adam(learning_rate=1e-4), loss=keras.losses.CategoricalCrossentropy(from_logits=False), metrics=["accuracy"], ) model.fit(images, masks, epochs=5) # Inference segmentation_masks = model.predict(images) # Shape: (4, 256, 256, 21) predicted_classes = np.argmax(segmentation_masks, axis=-1) ``` -------------------------------- ### Initialize CLIP Model and Processor Source: https://github.com/keras-team/keras-cv/blob/master/keras_cv/src/tools/checkpoint_conversion/clip_weights_conversion.ipynb Loads a pre-trained CLIP model and its corresponding processor from Hugging Face. Ensure the 'config_name_hf' variable is set to a valid Hugging Face model identifier. ```python from PIL import Image import requests import torch from transformers import CLIPProcessor as CP from transformers import CLIPModel as CM ``` ```python model_hf = CM.from_pretrained(config_name_hf) processor_hf = CP.from_pretrained(config_name_hf) ``` -------------------------------- ### Configure Model Prediction Decoder and Visualize Source: https://github.com/keras-team/keras-cv/blob/master/keras_cv/src/tools/training_scipts/Training_YOLOv8.ipynb Set up the `MultiClassNonMaxSuppression` decoder for the model and then call `visualize_detections` to display the results. This involves setting confidence and IoU thresholds. ```python model.prediction_decoder = keras_cv.layers.MultiClassNonMaxSuppression( bounding_box_format="xywh", from_logits=False, confidence_threshold=0.3, iou_threshold=0.5, ) model.make_predict_function(force=True) visualize_detections(model, eval_ds.shuffle(10), "xywh", rows=2, cols=2) old_model = model ``` -------------------------------- ### Get Top 5 Predictions Source: https://github.com/keras-team/keras-cv/blob/master/keras_cv/src/tools/checkpoint_conversion/ViT_weight_conversion.ipynb Retrieves the top 5 predicted class indices and their corresponding confidence scores from the model's output. This requires TensorFlow. ```python top_5 = tf.math.top_k(predictions, k=5, sorted=False) top_5 ``` -------------------------------- ### Load and Display Training Data CSV Source: https://github.com/keras-team/keras-cv/blob/master/keras_cv/src/tools/checkpoint_conversion/ViT_weight_conversion.ipynb Loads a CSV file containing training experiment data from Google Cloud Storage using pandas. The `df.head()` command displays the first few rows of the DataFrame, useful for inspecting the loaded data. ```python with tf.io.gfile.GFile("gs://vit_models/augreg/index.csv") as f: df = pd.read_csv(f) df.head() ``` -------------------------------- ### TensorFlow Variable Output Example Source: https://github.com/keras-team/keras-cv/blob/master/keras_cv/src/tools/checkpoint_conversion/ViT_weight_conversion.ipynb This represents the output of a TensorFlow Variable, showing its shape, data type, and numpy array values. This is typically seen after an assignment operation. ```python ``` -------------------------------- ### Configure Evaluation Dataset Preprocessing Source: https://github.com/keras-team/keras-cv/blob/master/keras_cv/src/tools/training_scipts/Training_YOLOv8.ipynb Applies resizing to the evaluation dataset, batches it, pads bounding boxes, and prefetches data for efficient evaluation. ```python eval_resizing = keras_cv.layers.Resizing( 640, 640, pad_to_aspect_ratio=True, bounding_box_format="xywh" ) eval_ds = eval_ds.map( eval_resizing, num_parallel_calls=tf.data.AUTOTUNE, ) eval_ds = eval_ds.apply(tf.data.experimental.dense_to_ragged_batch(BATCH_SIZE)) eval_ds = eval_ds.map(pad_fn, num_parallel_calls=tf.data.AUTOTUNE) eval_ds = eval_ds.prefetch(tf.data.AUTOTUNE) ``` -------------------------------- ### Configure Cosine Decay Learning Rate Schedule Source: https://github.com/keras-team/keras-cv/blob/master/keras_cv/src/tools/training_scipts/training_deeplab_v3_plus.ipynb Sets up a learning rate schedule using CosineDecay. This is useful for optimizers where the learning rate needs to decrease over time. Requires BATCH_SIZE, EPOCHS, and dataset cardinality. ```python BATCH_SIZE = 4 INITIAL_LR = 0.007 * BATCH_SIZE / 16 EPOCHS = 1 NUM_CLASSES = 21 learning_rate = keras.optimizers.schedules.CosineDecay( INITIAL_LR, decay_steps=EPOCHS * 2124, ) ``` -------------------------------- ### Preprocess and Batch Dataset Source: https://github.com/keras-team/keras-cv/blob/master/keras_cv/src/tools/checkpoint_conversion/ViT_weight_conversion.ipynb Applies preprocessing functions, shuffles, batches, and prefetches the dataset for efficient model training. AUTOTUNE is used for optimal performance. ```python test_set = ( test_set[0] .shuffle(len(test_set[0])) .map(preprocess_image) .batch(32) .prefetch(tf.data.AUTOTUNE) ) ``` -------------------------------- ### Get Predicted Class Label Source: https://github.com/keras-team/keras-cv/blob/master/keras_cv/src/tools/checkpoint_conversion/ViT_weight_conversion.ipynb Determines the most likely class label for an image by finding the index with the highest prediction score and mapping it to a string label using the ImageNet labels. ```python pred = np.argmax(predictions) imagenet_int_to_str[int(pred)] ``` -------------------------------- ### Initialize and Compile YOLOv8 Detector Model Source: https://github.com/keras-team/keras-cv/blob/master/keras_cv/src/tools/training_scipts/Training_YOLOv8.ipynb Initializes a YOLOv8 detector model with a specified backbone and configuration, compiles it with an optimizer, and sets trainable layers. ```python with strategy.scope(): model = keras_cv.models.YOLOV8Detector( num_classes=20, backbone=keras_cv.models.YOLOV8Backbone.from_preset( "yolo_v8_m_backbone_coco" ), fpn_depth=2, bounding_box_format="xywh", ) lr_schedule = keras.optimizers.schedules.PolynomialDecay( initial_learning_rate=BASE_LR, decay_steps=train_ds.cardinality() * 120, ) optimizer = tf.keras.optimizers.SGD( learning_rate=lr_schedule, momentum=0.937, clipnorm=5.0, weight_decay=5e-4, use_ema=True, ema_momentum=0.9999, ) model.compile( optimizer=optimizer, box_loss="ciou", classification_loss="binary_crossentropy", ) model.backbone.trainable = True ``` -------------------------------- ### Select Model Configuration Source: https://github.com/keras-team/keras-cv/blob/master/keras_cv/src/tools/checkpoint_conversion/ViT_weight_conversion.ipynb Selects the first model configuration from a dictionary of configurations. This is typically used to determine which model to load or process next. ```python model_to_convert = list(config_names.items())[0] model_to_convert ``` -------------------------------- ### Text-to-Image Generation with StableDiffusion Source: https://context7.com/keras-team/keras-cv/llms.txt Initializes the StableDiffusion model and generates images from text prompts. Supports custom batch sizes, number of diffusion steps, guidance scale, and seeds. Requires PIL for image saving. ```python from keras_cv.models import StableDiffusion from PIL import Image # Initialize model model = StableDiffusion(img_height=512, img_width=512, jit_compile=True) # Generate image from text prompt images = model.text_to_image( prompt="A photograph of a cat wearing sunglasses on a beach", batch_size=1, num_steps=50, unconditional_guidance_scale=7.5, seed=42, ) Image.fromarray(images[0]).save("cat_beach.png") ``` -------------------------------- ### Initialize CLIP Processor Source: https://github.com/keras-team/keras-cv/blob/master/keras_cv/src/tools/checkpoint_conversion/clip_weights_conversion.ipynb Initialize the CLIP processor with vocabulary and merge paths. This processor is used to prepare text inputs for the CLIP model. ```python VOCAB_PATH = keras.utils.get_file(None, "https://storage.googleapis.com/keras-cv/models/clip/vocab.json") MERGE_PATH = keras.utils.get_file(None, "https://storage.googleapis.com/keras-cv/models/clip/merges.txt") ``` ```python processor = CLIPProcessor(224, VOCAB_PATH, MERGE_PATH) text_processed = processor(["a photo of a cat", "a photo of a dog"]) ``` -------------------------------- ### Display CLIP Model Summary Source: https://github.com/keras-team/keras-cv/blob/master/keras_cv/src/tools/checkpoint_conversion/clip_weights_conversion.ipynb Prints a summary of the CLIP model's architecture, including layer names, output shapes, and parameter counts. ```python model.summary() ``` -------------------------------- ### Instantiate DeepLabV3+ Model with ResNet50 Backbone Source: https://github.com/keras-team/keras-cv/blob/master/keras_cv/src/tools/training_scipts/training_deeplab_v3_plus.ipynb Creates a DeepLabV3+ model using a ResNet50 backbone pre-trained on ImageNet. Specify the number of classes for segmentation. This model is suitable for semantic segmentation tasks. ```python model = keras_cv.models.DeepLabV3Plus.from_preset( "resnet50_v2_imagenet", num_classes=NUM_CLASSES ) ``` -------------------------------- ### Run All KerasCV Unit Tests Source: https://github.com/keras-team/keras-cv/blob/master/CONTRIBUTING.md Execute all unit tests for the KerasCV library from the project's root directory. ```shell pytest keras_cv/ ``` -------------------------------- ### Link to Contribution Welcome Issues Source: https://github.com/keras-team/keras-cv/blob/master/CONTRIBUTING.md Find issues tagged with 'contribution-welcome' to identify areas where contributions are needed. ```markdown [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) ``` -------------------------------- ### Pad Bounding Boxes and Prepare for Training Source: https://github.com/keras-team/keras-cv/blob/master/keras_cv/src/tools/training_scipts/Training_YOLOv8.ipynb Defines a padding function to ensure bounding boxes are dense and prepares the training dataset by shuffling, mapping the padding function, and prefetching data. ```python def pad_fn(inputs): inputs["bounding_boxes"] = keras_cv.bounding_box.to_dense( inputs["bounding_boxes"], max_boxes=32 ) return inputs train_ds = train_ds.shuffle(8 * strategy.num_replicas_in_sync) train_ds = train_ds.map(pad_fn, num_parallel_calls=tf.data.AUTOTUNE) train_ds = train_ds.prefetch(tf.data.AUTOTUNE) ``` -------------------------------- ### Prepare Training Data for RetinaNet Source: https://context7.com/keras-team/keras-cv/llms.txt Define sample images and corresponding labels (boxes and classes) for training a RetinaNet model. Uses NumPy arrays. ```python images = np.ones((2, 512, 512, 3), dtype="float32") labels = { "boxes": np.array([ [[0, 0, 100, 100], [100, 100, 200, 200]], [[50, 50, 150, 150], [200, 200, 100, 100]], ], dtype="float32"), "classes": np.array([[1, 2], [0, 1]], dtype="float32"), } ``` -------------------------------- ### Load JAX Parameters from NPZ File Source: https://github.com/keras-team/keras-cv/blob/master/keras_cv/src/tools/checkpoint_conversion/ViT_weight_conversion.ipynb This snippet shows how to load JAX parameters from a downloaded .npz file into a Python dictionary. ```python with open(local_path, "rb") as f: params_jax = np.load(f) params_jax = dict(zip(params_jax.keys(), params_jax.values())) ``` -------------------------------- ### Load Pretrained DeepLabV3+ Model Source: https://github.com/keras-team/keras-cv/blob/master/keras_cv/src/tools/training_scipts/training_deeplab_v3_plus.ipynb Construct a DeepLabV3+ model pretrained on the Pascal VOC dataset. Specify the number of classes and the input image shape for the model. ```python model = keras_cv.models.DeepLabV3Plus.from_preset( "deeplab_v3_plus_resnet50_pascalvoc", num_classes=21, input_shape=[512, 512, 3], ) ``` -------------------------------- ### Link to API Design Guidelines Source: https://github.com/keras-team/keras-cv/blob/master/CONTRIBUTING.md Refer to the API Design Guidelines when making changes that introduce new API elements. ```markdown [API Design Guidelines](API_DESIGN.md) ``` -------------------------------- ### Activate Pre-commit Hooks Source: https://github.com/keras-team/keras-cv/blob/master/CONTRIBUTING.md Configure Git to use pre-commit hooks located in the `.github/.githooks` directory. This automates linting and formatting checks before each commit. ```git git config core.hooksPath .github/.githooks ``` -------------------------------- ### Apply Dataset Mapping and Augmentation Source: https://github.com/keras-team/keras-cv/blob/master/keras_cv/src/tools/training_scipts/Training_YOLOv8.ipynb Maps the unpacking function to the datasets, applies a sequential augmentation pipeline including JitteredResize and RandomFlip, and batches the data. ```python train_ds = train_ds.map( lambda inputs: unpackage_tfds_inputs(inputs, bounding_box_format="xywh"), num_parallel_calls=tf.data.AUTOTUNE, ) eval_ds = eval_ds.map( lambda inputs: unpackage_tfds_inputs(inputs, bounding_box_format="xywh"), num_parallel_calls=tf.data.AUTOTUNE, ) augmenter = keras.Sequential( layers=[ keras_cv.layers.RandomFlip( mode="horizontal", bounding_box_format="xywh" ), keras_cv.layers.JitteredResize( target_size=(640, 640), scale_factor=(0.8, 1.25), bounding_box_format="xywh", ), ] ) train_ds = train_ds.apply( tf.data.experimental.dense_to_ragged_batch(BATCH_SIZE) ) train_ds = train_ds.map(augmenter, num_parallel_calls=tf.data.AUTOTUNE) ``` -------------------------------- ### Load Other KerasCV Backbones Source: https://context7.com/keras-team/keras-cv/llms.txt Demonstrates loading various pretrained backbones like EfficientNetV2, MobileNetV3, DenseNet, and Vision Transformer variants. All follow a consistent API for feature extraction. ```python import keras_cv import tensorflow as tf images = tf.ones(shape=(8, 224, 224, 3)) # EfficientNetV2 backbone_effnet = keras_cv.models.EfficientNetV2B0Backbone.from_preset( "efficientnetv2_b0_imagenet" ) features_effnet = backbone_effnet(images) # MobileNetV3 backbone_mobilenet = keras_cv.models.MobileNetV3SmallBackbone.from_preset( "mobilenet_v3_small_imagenet" ) features_mobilenet = backbone_mobilenet(images) # DenseNet backbone_densenet = keras_cv.models.DenseNet121Backbone.from_preset( "densenet121_imagenet" ) features_densenet = backbone_densenet(images) # All backbones follow the same API # features = backbone(images) ``` -------------------------------- ### Compile DeepLabv3+ Model Source: https://github.com/keras-team/keras-cv/blob/master/keras_cv/src/tools/training_scipts/training_deeplab_v3_plus.ipynb Configures the model's training process by defining the optimizer (SGD), loss function (CategoricalCrossentropy), and evaluation metrics (MeanIoU, CategoricalAccuracy). Ensure NUM_CLASSES is set appropriately for your dataset. ```python model.compile( optimizer=keras.optimizers.SGD( learning_rate=learning_rate, weight_decay=0.0001, momentum=0.9, clipnorm=10.0, ), loss=keras.losses.CategoricalCrossentropy(from_logits=False), metrics=[ keras.metrics.MeanIoU( num_classes=NUM_CLASSES, sparse_y_true=False, sparse_y_pred=False ), keras.metrics.CategoricalAccuracy(), ], ) model.summary() ``` -------------------------------- ### Fork and Clone KerasCV Repository Source: https://github.com/keras-team/keras-cv/blob/master/CONTRIBUTING.md Commands to fork the KerasCV repository, clone it locally, and set up the development environment. Requires GitHub CLI. ```shell gh repo fork keras-team/keras-cv --clone --remote cd keras-cv pip install ".[tests]" pip install -e . ``` -------------------------------- ### Load CLIP Model Weights Source: https://github.com/keras-team/keras-cv/blob/master/keras_cv/src/tools/checkpoint_conversion/clip_weights_conversion.ipynb Load pre-trained weights for the CLIP model from a specified file path. This is a necessary step before performing inference. ```python model.load_weights("model.weights.h5") ``` -------------------------------- ### Configure Keras Backend to JAX Source: https://github.com/keras-team/keras-cv/blob/master/README.md Set the KERAS_BACKEND environment variable to 'jax' to use KerasCV with JAX. This should be done before importing Keras libraries. ```shell export KERAS_BACKEND=jax ``` -------------------------------- ### Convert URL to Image Array Source: https://github.com/keras-team/keras-cv/blob/master/keras_cv/src/tools/checkpoint_conversion/ViT_weight_conversion.ipynb Downloads an image from a URL, decodes it into a NumPy array, converts it to RGB format, and resizes it to 224x224 pixels. Requires `urllib`, `cv2`, and `numpy`. ```python import urllib import cv2 import numpy as np def url_to_array(url): req = urllib.request.urlopen(url) arr = np.array(bytearray(req.read()), dtype=np.int8) arr = cv2.imdecode(arr, -1) arr = cv2.cvtColor(arr, cv2.COLOR_BGR2RGB) arr = cv2.resize(arr, (224, 224)) return arr ``` -------------------------------- ### Download ImageNet Labels Source: https://github.com/keras-team/keras-cv/blob/master/keras_cv/src/tools/checkpoint_conversion/ViT_weight_conversion.ipynb Downloads the ImageNet wordnet lemmas file, which maps integer IDs to string labels. This is used to interpret model predictions. ```bash !wget https://storage.googleapis.com/bit_models/ilsvrc2012_wordnet_lemmas.txt -O ilsvrc2012_wordnet_lemmas.txt ``` ```python with open("ilsvrc2012_wordnet_lemmas.txt", "r") as f: lines = f.readlines() imagenet_int_to_str = [line.rstrip() for line in lines] ``` -------------------------------- ### Custom ResNetV2 Backbone Configuration Source: https://context7.com/keras-team/keras-cv/llms.txt Configures a ResNetV2 backbone with custom stackwise filters, blocks, and strides. Includes options for rescaling and input shape. Useful for fine-tuning architecture details. ```python import keras_cv # Custom backbone configuration backbone = keras_cv.models.ResNetV2Backbone( stackwise_filters=[64, 128, 256, 512], stackwise_blocks=[3, 4, 6, 3], stackwise_strides=[1, 2, 2, 2], include_rescaling=True, input_shape=(224, 224, 3), ) # Access pyramid level outputs for FPN print(backbone.pyramid_level_inputs) # {'P2': 'v2_stack_0_block2_out', 'P3': 'v2_stack_1_block3_out', ...} ``` -------------------------------- ### Iterate and Print Labels from Dataset Source: https://github.com/keras-team/keras-cv/blob/master/keras_cv/src/tools/checkpoint_conversion/ViT_weight_conversion.ipynb Takes one batch from the preprocessed dataset and prints the labels for each image in that batch. This is useful for inspecting the data. ```python for entry, label in test_set.take(1): print(label) ``` -------------------------------- ### Generate API Documentation Source: https://github.com/keras-team/keras-cv/blob/master/CONTRIBUTING.md Script to generate API documentation. This should be run when creating Pull Requests that modify public APIs exported by `keras_export`. ```shell ./shell/api_gen.sh ``` -------------------------------- ### Save CLIP Model Weights and Configuration Source: https://github.com/keras-team/keras-cv/blob/master/keras_cv/src/tools/checkpoint_conversion/clip_weights_conversion.ipynb Saves the model's weights to a file and its configuration to a JSON file. Assumes `config_name` is defined. ```python os.makedirs(config_name, exist_ok=True) model.save_weights(os.path.join(config_name, "model.weights.h5")) ``` ```python config = { "module": "keras_cv.models.feature_extractor.clip.clip_model", "class_name": "CLIP", "config": model.get_config(), "registered_name": "keras_cv>CLIP", "weights": "model.weights.h5", } with open(os.path.join(config_name, "config.json"), "w") as config_file: json.dump(config, config_file) metadata = { "keras_version": keras.__version__, "keras_cv_version": keras_cv.__version__, "parameter_count": model.count_params(), "date_saved": datetime.utcnow().strftime("%Y-%m-%d@%H:%M:%S"), } with open(os.path.join(config_name, "metadata.json"), "w") as metadata_file: json.dump(metadata, metadata_file) ``` -------------------------------- ### Generate and Visualize Predictions Source: https://github.com/keras-team/keras-cv/blob/master/keras_cv/src/tools/training_scipts/training_deeplab_v3_plus.ipynb Loads a test dataset, preprocesses inputs, and generates segmentation mask predictions on sample images. Visualizes the true masks alongside the model's predictions for qualitative evaluation. ```python test_ds = load_voc(split="sbd_eval") test_ds = preprocess_tfds_inputs(test_ds) images, masks = next(iter(train_ds.take(1))) images = ops.convert_to_tensor(images) masks = ops.convert_to_tensor(masks) preds = ops.expand_dims(ops.argmax(model(images), axis=-1), axis=-1) masks = ops.expand_dims(ops.argmax(masks, axis=-1), axis=-1) keras_cv.visualization.plot_segmentation_mask_gallery( images, value_range=(0, 255), num_classes=21, y_true=masks, y_pred=preds, scale=3, rows=1, cols=4, ) ``` -------------------------------- ### Prepare Training Data for YOLOV8Detector Source: https://context7.com/keras-team/keras-cv/llms.txt Define sample images and corresponding labels (boxes and classes) for training a YOLOV8Detector. Uses TensorFlow tensors. ```python # Prepare training data images = tf.ones(shape=(4, 640, 640, 3)) labels = { "boxes": tf.constant([ [[50, 50, 150, 150], [200, 200, 350, 350], [-1, -1, -1, -1]], [[100, 100, 200, 200], [-1, -1, -1, -1], [-1, -1, -1, -1]], [[0, 0, 100, 100], [150, 150, 300, 300], [400, 400, 500, 500]], [[250, 250, 400, 400], [-1, -1, -1, -1], [-1, -1, -1, -1]], ], dtype=tf.float32), "classes": tf.constant([[1, 2, -1], [0, -1, -1], [1, 1, 2], [0, -1, -1]], dtype=tf.int64), } ```