Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add sdv3 code #2468

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions benchmarks/vectorized_randomly_zoomed_crop.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,10 +249,10 @@ def from_config(cls, config):
config["zoom_factor"]
)
if isinstance(config["aspect_ratio_factor"], dict):
config["aspect_ratio_factor"] = (
keras.utils.deserialize_keras_object(
config["aspect_ratio_factor"]
)
config[
"aspect_ratio_factor"
] = keras.utils.deserialize_keras_object(
config["aspect_ratio_factor"]
)
return cls(**config)

Expand Down
1 change: 1 addition & 0 deletions keras_cv/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
since your modifications would be overwritten.
"""


import os

# Import everything from /api/ into keras.
Expand Down
1 change: 1 addition & 0 deletions keras_cv/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
since your modifications would be overwritten.
"""


from keras_cv.api import bounding_box
from keras_cv.api import callbacks
from keras_cv.api import core
Expand Down
1 change: 1 addition & 0 deletions keras_cv/api/bounding_box/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
since your modifications would be overwritten.
"""


from keras_cv.src.bounding_box.converters import convert_format
from keras_cv.src.bounding_box.ensure_tensor import ensure_tensor
from keras_cv.src.bounding_box.formats import CENTER_XYWH
Expand Down
1 change: 1 addition & 0 deletions keras_cv/api/callbacks/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
since your modifications would be overwritten.
"""


from keras_cv.src.callbacks.pycoco_callback import PyCOCOCallback
from keras_cv.src.callbacks.waymo_evaluation_callback import (
WaymoEvaluationCallback,
Expand Down
1 change: 1 addition & 0 deletions keras_cv/api/core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
since your modifications would be overwritten.
"""


from keras_cv.src.core.factor_sampler.constant_factor_sampler import (
ConstantFactorSampler,
)
Expand Down
1 change: 1 addition & 0 deletions keras_cv/api/datasets/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
since your modifications would be overwritten.
"""


from keras_cv.api.datasets import imagenet
from keras_cv.api.datasets import pascal_voc
from keras_cv.api.datasets import waymo
1 change: 1 addition & 0 deletions keras_cv/api/datasets/imagenet/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
since your modifications would be overwritten.
"""


from keras_cv.src.datasets.imagenet.load import load
1 change: 1 addition & 0 deletions keras_cv/api/datasets/pascal_voc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
since your modifications would be overwritten.
"""


from keras_cv.api.datasets.pascal_voc import segmentation
from keras_cv.src.datasets.pascal_voc.load import load
1 change: 1 addition & 0 deletions keras_cv/api/datasets/pascal_voc/segmentation/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
since your modifications would be overwritten.
"""


from keras_cv.src.datasets.pascal_voc.segmentation import load
1 change: 1 addition & 0 deletions keras_cv/api/datasets/waymo/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
since your modifications would be overwritten.
"""


from keras_cv.src.datasets.waymo.load import load
from keras_cv.src.datasets.waymo.transformer import (
build_tensors_for_augmentation,
Expand Down
1 change: 1 addition & 0 deletions keras_cv/api/keypoint/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
since your modifications would be overwritten.
"""


from keras_cv.src.keypoint.converters import convert_format
from keras_cv.src.keypoint.formats import REL_XY
from keras_cv.src.keypoint.formats import XY
Expand Down
2 changes: 2 additions & 0 deletions keras_cv/api/layers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
since your modifications would be overwritten.
"""


from keras_cv.src.layers.augmenter import Augmenter
from keras_cv.src.layers.feature_pyramid import FeaturePyramid
from keras_cv.src.layers.fusedmbconv import FusedMBConvBlock
Expand Down Expand Up @@ -152,6 +153,7 @@
from keras_cv.src.layers.vit_det_layers import WindowedTransformerEncoder
from keras_cv.src.layers.vit_det_layers import WindowPartitioning
from keras_cv.src.layers.vit_layers import PatchingAndEmbedding
from keras_cv.src.layers.vit_layers import Unpatching
from keras_cv.src.models.segmentation.segment_anything.sam_layers import (
MultiHeadAttentionWithDownsampling,
)
Expand Down
1 change: 1 addition & 0 deletions keras_cv/api/losses/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
since your modifications would be overwritten.
"""


from keras_cv.src.losses.centernet_box_loss import CenterNetBoxLoss
from keras_cv.src.losses.ciou_loss import CIoULoss
from keras_cv.src.losses.focal import FocalLoss
Expand Down
1 change: 1 addition & 0 deletions keras_cv/api/metrics/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
since your modifications would be overwritten.
"""


from keras_cv.src.metrics.object_detection.box_coco_metrics import (
BoxCOCOMetrics,
)
1 change: 1 addition & 0 deletions keras_cv/api/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
since your modifications would be overwritten.
"""


from keras_cv.api.models import classification
from keras_cv.api.models import feature_extractor
from keras_cv.api.models import object_detection
Expand Down
1 change: 1 addition & 0 deletions keras_cv/api/models/classification/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
since your modifications would be overwritten.
"""


from keras_cv.src.models.classification.image_classifier import ImageClassifier
from keras_cv.src.models.classification.video_classifier import VideoClassifier
1 change: 1 addition & 0 deletions keras_cv/api/models/feature_extractor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
since your modifications would be overwritten.
"""


from keras_cv.src.models.feature_extractor.clip.clip_encoder import (
CLIPAttention,
)
Expand Down
1 change: 1 addition & 0 deletions keras_cv/api/models/object_detection/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
since your modifications would be overwritten.
"""


from keras_cv.src.models.object_detection.retinanet.retinanet import RetinaNet
from keras_cv.src.models.object_detection.yolo_v8.yolo_v8_detector import (
YOLOV8Detector,
Expand Down
1 change: 1 addition & 0 deletions keras_cv/api/models/retinanet/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
since your modifications would be overwritten.
"""


from keras_cv.src.models.object_detection.retinanet.feature_pyramid import (
FeaturePyramid,
)
Expand Down
1 change: 1 addition & 0 deletions keras_cv/api/models/segmentation/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
since your modifications would be overwritten.
"""


from keras_cv.src.models.segmentation.basnet.basnet import BASNet
from keras_cv.src.models.segmentation.deeplab_v3_plus.deeplab_v3_plus import (
DeepLabV3Plus,
Expand Down
1 change: 1 addition & 0 deletions keras_cv/api/models/stable_diffusion/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
since your modifications would be overwritten.
"""


from keras_cv.src.models.stable_diffusion.clip_tokenizer import SimpleTokenizer
from keras_cv.src.models.stable_diffusion.decoder import Decoder
from keras_cv.src.models.stable_diffusion.diffusion_model import DiffusionModel
Expand Down
1 change: 1 addition & 0 deletions keras_cv/api/models/yolov8/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
since your modifications would be overwritten.
"""


from keras_cv.src.models.object_detection.yolo_v8.yolo_v8_label_encoder import (
YOLOV8LabelEncoder as LabelEncoder,
)
1 change: 1 addition & 0 deletions keras_cv/api/visualization/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
since your modifications would be overwritten.
"""


from keras_cv.src.visualization.draw_bounding_boxes import draw_bounding_boxes
from keras_cv.src.visualization.plot_bounding_box_gallery import (
plot_bounding_box_gallery,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,15 +236,15 @@ def _compute_output_signature(self, inputs):
bounding_boxes = inputs.get(BOUNDING_BOXES, None)

if bounding_boxes is not None:
fn_output_signature[BOUNDING_BOXES] = (
self._compute_bounding_box_signature(bounding_boxes)
)
fn_output_signature[
BOUNDING_BOXES
] = self._compute_bounding_box_signature(bounding_boxes)

segmentation_masks = inputs.get(SEGMENTATION_MASKS, None)
if segmentation_masks is not None:
fn_output_signature[SEGMENTATION_MASKS] = (
self.compute_image_signature(segmentation_masks)
)
fn_output_signature[
SEGMENTATION_MASKS
] = self.compute_image_signature(segmentation_masks)

keypoints = inputs.get(KEYPOINTS, None)
if keypoints is not None:
Expand Down
8 changes: 4 additions & 4 deletions keras_cv/src/layers/preprocessing/random_crop_and_resize.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,10 +272,10 @@ def from_config(cls, config):
config["crop_area_factor"]
)
if isinstance(config["aspect_ratio_factor"], dict):
config["aspect_ratio_factor"] = (
keras.utils.deserialize_keras_object(
config["aspect_ratio_factor"]
)
config[
"aspect_ratio_factor"
] = keras.utils.deserialize_keras_object(
config["aspect_ratio_factor"]
)
return cls(**config)

Expand Down
8 changes: 4 additions & 4 deletions keras_cv/src/layers/regularization/squeeze_excite.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,10 @@ def get_config(self):
@classmethod
def from_config(cls, config):
if isinstance(config["squeeze_activation"], dict):
config["squeeze_activation"] = (
keras.saving.deserialize_keras_object(
config["squeeze_activation"]
)
config[
"squeeze_activation"
] = keras.saving.deserialize_keras_object(
config["squeeze_activation"]
)
if isinstance(config["excite_activation"], dict):
config["excite_activation"] = keras.saving.deserialize_keras_object(
Expand Down
86 changes: 81 additions & 5 deletions keras_cv/src/layers/vit_layers.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@
import math

import tensorflow as tf
from tensorflow.keras import layers

from keras_cv.src.api_export import keras_cv_export
from keras_cv.src.backend import keras
from keras_cv.src.backend import ops


@keras_cv_export("keras_cv.layers.PatchingAndEmbedding")
class PatchingAndEmbedding(layers.Layer):
class PatchingAndEmbedding(keras.layers.Layer):
"""
Layer to patchify images, prepend a class token, positionally embed and
create a projection of patches for Vision Transformers
Expand All @@ -44,7 +45,7 @@ class PatchingAndEmbedding(layers.Layer):
Patchified and linearly projected input images, including a prepended
learnable class token with shape (batch, num_patches+1, project_dim)

Example:
Basic usage:

```
images = #... batch of images
Expand All @@ -70,7 +71,7 @@ def __init__(self, project_dim, patch_size, padding="VALID", **kwargs):
f"Padding must be either 'SAME' or 'VALID', but {padding} was "
"passed."
)
self.projection = layers.Conv2D(
self.projection = keras.layers.Conv2D(
filters=self.project_dim,
kernel_size=self.patch_size,
strides=self.patch_size,
Expand All @@ -87,7 +88,7 @@ def build(self, input_shape):
* input_shape[2]
// self.patch_size
)
self.position_embedding = layers.Embedding(
self.position_embedding = keras.layers.Embedding(
input_dim=self.num_patches + 1, output_dim=self.project_dim
)

Expand Down Expand Up @@ -221,3 +222,78 @@ def get_config(self):
}
base_config = super().get_config()
return dict(list(base_config.items()) + list(config.items()))


@keras_cv_export("keras_cv.layers.Unpatching")
class Unpatching(keras.layers.Layer):
"""
Layer to unpatchify image data.

This layer expects patches sorted by column and reorganizes the patches
such that they will each be positioned as a 2D shape with some
number of channels.

Any necessary padding or truncation will be applied to reach the target
shape.

Args:
target_shape: The target image shape after unpatching,
of form [height, width]
"""

def __init__(self, target_shape):
self.target_shape = target_shape

def call(self, patches):
"""
Reconstructs an unpatched image from the sequence of column sequence
patches.

If there are insufficient patches to construct the image of requested
dimensions, additional zero-patches will
be appended. If excessive patches are provided, unnecessary patches
will be truncated from the end.

Args:
patches: Patches of images in column sequence (i.e. each patch
is vertically oriented relative to the
previous patch). Expected shape of [batch_size, patch_num,
patch_height, patch_width, channels].

Returns:
Unpatched image: Image reconstructed from the patches,
of expected shape [batch_size, height, width, channels].
"""
(
batch_size,
patch_count,
patch_height,
patch_width,
channels,
) = ops.shape(patches)

target_height, target_width = self.target_shape

patches_per_column = target_height // patch_height
patches_per_row = target_width // patch_width
required_patches = patches_per_column * patches_per_row

if required_patches > patch_count:
corrected_patches = ops.concatenate(
[
patches,
ops.zeros(
(
required_patches - patch_count,
patch_height,
patch_height,
channels,
)
),
],
axis=1,
)
else:
corrected_patches = patches[:, :required_patches]

return ops.split(corrected_patches, patches_per_column, axis=1)
6 changes: 3 additions & 3 deletions keras_cv/src/metrics/object_detection/box_coco_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,9 +212,9 @@ def result_fn(self, force=False):
)
result = {}
for i, key in enumerate(METRIC_NAMES):
result[self.name_prefix() + METRIC_MAPPING[key]] = (
py_func_result[i]
)
result[
self.name_prefix() + METRIC_MAPPING[key]
] = py_func_result[i]
return result

obj.result = types.MethodType(result_fn, obj)
Expand Down
Loading
Loading