|
| 1 | +# Copyright 2024 The KerasCV Authors |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import copy |
| 16 | + |
| 17 | +from keras_cv.models.backbones.video_swin.video_swin_backbone import ( |
| 18 | + VideoSwinBackbone, |
| 19 | +) |
| 20 | +from keras_cv.models.backbones.video_swin.video_swin_backbone_presets import ( |
| 21 | + backbone_presets, |
| 22 | +) |
| 23 | +from keras_cv.utils.python_utils import classproperty |
| 24 | + |
| 25 | +ALIAS_DOCSTRING = """VideoSwin{size}Backbone model. |
| 26 | +
|
| 27 | + Reference: |
| 28 | + - [Video Swin Transformer](https://arxiv.org/abs/2106.13230) |
| 29 | + - [Video Swin Transformer GitHub](https://github.com/SwinTransformer/Video-Swin-Transformer) |
| 30 | +
|
| 31 | + For transfer learning use cases, make sure to read the |
| 32 | + [guide to transfer learning & fine-tuning](https://keras.io/guides/transfer_learning/). |
| 33 | +
|
| 34 | + Examples: |
| 35 | + ```python |
| 36 | + input_data = np.ones(shape=(1, 32, 224, 224, 3)) |
| 37 | +
|
| 38 | + # Randomly initialized backbone |
| 39 | + model = VideoSwin{size}Backbone() |
| 40 | + output = model(input_data) |
| 41 | + ``` |
| 42 | +""" # noqa: E501 |
| 43 | + |
| 44 | + |
| 45 | +class VideoSwinTBackbone(VideoSwinBackbone): |
| 46 | + def __new__( |
| 47 | + cls, |
| 48 | + embed_dim=96, |
| 49 | + depths=[2, 2, 6, 2], |
| 50 | + num_heads=[3, 6, 12, 24], |
| 51 | + window_size=[8, 7, 7], |
| 52 | + include_rescaling=True, |
| 53 | + **kwargs, |
| 54 | + ): |
| 55 | + kwargs.update( |
| 56 | + { |
| 57 | + "embed_dim": embed_dim, |
| 58 | + "depths": depths, |
| 59 | + "num_heads": num_heads, |
| 60 | + "window_size": window_size, |
| 61 | + "include_rescaling": include_rescaling, |
| 62 | + } |
| 63 | + ) |
| 64 | + return VideoSwinBackbone.from_preset("videoswin_tiny", **kwargs) |
| 65 | + |
| 66 | + @classproperty |
| 67 | + def presets(cls): |
| 68 | + """Dictionary of preset names and configurations.""" |
| 69 | + return { |
| 70 | + "videoswin_tiny_kinetics400": copy.deepcopy( |
| 71 | + backbone_presets["videoswin_tiny_kinetics400"] |
| 72 | + ), |
| 73 | + } |
| 74 | + |
| 75 | + @classproperty |
| 76 | + def presets_with_weights(cls): |
| 77 | + """Dictionary of preset names and configurations that include |
| 78 | + weights.""" |
| 79 | + return cls.presets |
| 80 | + |
| 81 | + |
| 82 | +class VideoSwinSBackbone(VideoSwinBackbone): |
| 83 | + def __new__( |
| 84 | + cls, |
| 85 | + embed_dim=96, |
| 86 | + depths=[2, 2, 18, 2], |
| 87 | + num_heads=[3, 6, 12, 24], |
| 88 | + window_size=[8, 7, 7], |
| 89 | + include_rescaling=True, |
| 90 | + **kwargs, |
| 91 | + ): |
| 92 | + kwargs.update( |
| 93 | + { |
| 94 | + "embed_dim": embed_dim, |
| 95 | + "depths": depths, |
| 96 | + "num_heads": num_heads, |
| 97 | + "window_size": window_size, |
| 98 | + "include_rescaling": include_rescaling, |
| 99 | + } |
| 100 | + ) |
| 101 | + return VideoSwinBackbone.from_preset("videoswin_small", **kwargs) |
| 102 | + |
| 103 | + @classproperty |
| 104 | + def presets(cls): |
| 105 | + """Dictionary of preset names and configurations.""" |
| 106 | + return { |
| 107 | + "videoswin_small_kinetics400": copy.deepcopy( |
| 108 | + backbone_presets["videoswin_small_kinetics400"] |
| 109 | + ), |
| 110 | + } |
| 111 | + |
| 112 | + @classproperty |
| 113 | + def presets_with_weights(cls): |
| 114 | + """Dictionary of preset names and configurations that include |
| 115 | + weights.""" |
| 116 | + return cls.presets |
| 117 | + |
| 118 | + |
| 119 | +class VideoSwinBBackbone(VideoSwinBackbone): |
| 120 | + def __new__( |
| 121 | + cls, |
| 122 | + embed_dim=128, |
| 123 | + depths=[2, 2, 18, 2], |
| 124 | + num_heads=[4, 8, 16, 32], |
| 125 | + window_size=[8, 7, 7], |
| 126 | + include_rescaling=True, |
| 127 | + **kwargs, |
| 128 | + ): |
| 129 | + kwargs.update( |
| 130 | + { |
| 131 | + "embed_dim": embed_dim, |
| 132 | + "depths": depths, |
| 133 | + "num_heads": num_heads, |
| 134 | + "window_size": window_size, |
| 135 | + "include_rescaling": include_rescaling, |
| 136 | + } |
| 137 | + ) |
| 138 | + return VideoSwinBackbone.from_preset("videoswin_base", **kwargs) |
| 139 | + |
| 140 | + @classproperty |
| 141 | + def presets(cls): |
| 142 | + """Dictionary of preset names and configurations.""" |
| 143 | + return { |
| 144 | + "videoswin_base_kinetics400": copy.deepcopy( |
| 145 | + backbone_presets["videoswin_base_kinetics400"] |
| 146 | + ), |
| 147 | + } |
| 148 | + |
| 149 | + @classproperty |
| 150 | + def presets_with_weights(cls): |
| 151 | + """Dictionary of preset names and configurations that include |
| 152 | + weights.""" |
| 153 | + return cls.presets |
| 154 | + |
| 155 | + |
| 156 | +setattr(VideoSwinTBackbone, "__doc__", ALIAS_DOCSTRING.format(size="T")) |
| 157 | +setattr(VideoSwinSBackbone, "__doc__", ALIAS_DOCSTRING.format(size="S")) |
| 158 | +setattr(VideoSwinBBackbone, "__doc__", ALIAS_DOCSTRING.format(size="B")) |
0 commit comments