KerasHub
prasadsachin commited on
Commit
8be5c1b
·
verified ·
1 Parent(s): d9a3cb8

Update README.md with new model card content

Browse files
Files changed (1) hide show
  1. README.md +106 -22
README.md CHANGED
@@ -1,25 +1,109 @@
1
  ---
2
  library_name: keras-hub
3
  ---
4
- This is a [`SwinTransformer` model](https://keras.io/api/keras_hub/models/swin_transformer) uploaded using the KerasHub library and can be used with JAX, TensorFlow, and PyTorch backends.
5
- This model is related to a `ImageClassifier` task.
6
-
7
- Model config:
8
- * **name:** swin_transformer_backbone
9
- * **trainable:** True
10
- * **dtype:** {'module': 'keras', 'class_name': 'DTypePolicy', 'config': {'name': 'float32'}, 'registered_name': None}
11
- * **image_shape:** [384, 384, 3]
12
- * **patch_size:** 4
13
- * **embed_dim:** 128
14
- * **depths:** [2, 2, 18, 2]
15
- * **num_heads:** [4, 8, 16, 32]
16
- * **window_size:** 12
17
- * **mlp_ratio:** 4.0
18
- * **qkv_bias:** True
19
- * **dropout_rate:** 0.0
20
- * **attention_dropout:** 0.0
21
- * **drop_path:** 0.1
22
- * **patch_norm:** True
23
- * **data_format:** channels_last
24
-
25
- This model card has been generated automatically and should be completed by the model author. See [Model Cards documentation](https://huggingface.co/docs/hub/model-cards) for more information.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
  library_name: keras-hub
3
  ---
4
+ ### Model Overview
5
+ # Swin Transformer
6
+
7
+ Instantiates the Swin Transformer architecture.
8
+
9
+ ## Model Details
10
+
11
+ The Swin Transformer (Shifted Window Transformer) is a hierarchical vision transformer whose representation is computed with shifted windows. The shifted windowing scheme brings greater efficiency by limiting self-attention computation to non-overlapping local windows while also allowing for cross-window connection.
12
+
13
+ This hierarchical architecture has the flexibility to model at various scales and has linear computational complexity with respect to image size. These qualities make Swin Transformer compatible with a broad range of vision tasks, including image classification, object detection, and semantic segmentation.
14
+
15
+ ### Reference
16
+ * [Swin Transformer: Hierarchical Vision Transformer using Shifted Windows](https://arxiv.org/abs/2103.14030)
17
+
18
+ Unlike traditional Vision Transformers (ViT), which compute attention globally across all patches (resulting in quadratic complexity relative to image size), Swin Transformer computes self-attention within local non-overlapping windows. By shifting the window partition between consecutive layers, the model achieves cross-window connections, maintaining linear computational complexity while enabling robust global context modeling.
19
+
20
+ ### Links
21
+ * [Swin Transformer Quickstart Notebook](https://www.kaggle.com/code/prasadsachin/swin-transformer-quickstart-keras-hub)
22
+ * [Swin Transformer API Documentation](https://keras.io/keras_hub/api/models/swin_transformer/)
23
+ * [KerasHub Beginner Guide](https://keras.io/guides/keras_hub/getting_started/)
24
+ * [KerasHub Model Publishing Guide](https://keras.io/guides/keras_hub/upload/)
25
+
26
+ ## Installation
27
+
28
+ Keras and KerasHub can be installed with:
29
+ ```bash
30
+ pip install -U -q keras-hub
31
+ pip install -U -q keras
32
+ ```
33
+
34
+ JAX, TensorFlow, and PyTorch come preinstalled in Kaggle Notebooks. For instructions on installing them in another environment, see the [Keras Getting Started](https://keras.io/getting_started/) page.
35
+
36
+ ## Presets
37
+
38
+ The following model checkpoints are provided by the Keras team. Weights have been ported from [Hugging Face Hub](https://huggingface.co/microsoft).
39
+
40
+ | Preset name | Parameters | Description |
41
+ | :--- | :--- | :--- |
42
+ | **swin_tiny_patch4_window7_224** | 28.29M | Tiny Swin Transformer model pre-trained on ImageNet-1k at a 224x224 resolution |
43
+ | **swin_small_patch4_window7_224** | 49.61M | Small Swin Transformer model pre-trained on ImageNet-1k at a 224x224 resolution |
44
+ | **swin_base_patch4_window7_224** | 87.77M | Base Swin Transformer model pre-trained on ImageNet-1k at a 224x224 resolution |
45
+ | **swin_base_patch4_window12_384** | 87.90M | Base Swin Transformer model pre-trained on ImageNet-1k at a 384x384 resolution |
46
+ | **swin_large_patch4_window7_224** | 196.53M | Large Swin Transformer model pre-trained ImageNet-1k at a 224x224 resolution |
47
+ | **swin_large_patch4_window12_384** | 196.74M | Large Swin Transformer model pre-trained on ImageNet-1k at a 384x384 resolution |
48
+
49
+ ## Example Use
50
+
51
+ ```python
52
+ import numpy as np
53
+ import keras_hub
54
+
55
+ # Pretrained Swin Transformer backbone
56
+ model = keras_hub.models.SwinTransformerBackbone.from_preset("swin_tiny_patch4_window7_224")
57
+ input_data = np.random.uniform(0, 1, size=(2, 224, 224, 3))
58
+ model(input_data)
59
+
60
+ # Randomly initialized Swin Transformer backbone with custom config
61
+ model = keras_hub.models.SwinTransformerBackbone(
62
+ image_shape=(224, 224, 3),
63
+ embed_dim=96,
64
+ depths=(2, 2, 6, 2),
65
+ num_heads=(3, 6, 12, 24),
66
+ window_size=7,
67
+ )
68
+ model(input_data)
69
+
70
+ # Use Swin Transformer for image classification task
71
+ classifier = keras_hub.models.SwinTransformerImageClassifier.from_preset(
72
+ "swin_tiny_patch4_window7_224",
73
+ num_classes=1000,
74
+ )
75
+
76
+ # Use Hugging Face presets directly for on-the-fly conversion
77
+ classifier = keras_hub.models.SwinTransformerImageClassifier.from_preset(
78
+ "hf://microsoft/swin-tiny-patch4-window7-224"
79
+ )
80
+ ```
81
+
82
+ ## Example Usage
83
+ ```
84
+ import numpy as np
85
+ import keras_hub
86
+
87
+ # Top-5 ImageNet class decoding.
88
+ model = keras_hub.models.SwinTransformerImageClassifier.from_preset(
89
+ "swin_base_patch4_window12_384"
90
+ )
91
+ images = np.random.randint(0, 256, size=(1, 384, 384, 3), dtype="uint8")
92
+ logits = model.predict(images, verbose=0)
93
+ print(keras_hub.utils.decode_imagenet_predictions(logits, top=5)[0])
94
+ ```
95
+
96
+ ## Example Usage with Hugging Face URI
97
+
98
+ ```
99
+ import numpy as np
100
+ import keras_hub
101
+
102
+ # Top-5 ImageNet class decoding.
103
+ model = keras_hub.models.SwinTransformerImageClassifier.from_preset(
104
+ "hf://keras/swin_base_patch4_window12_384"
105
+ )
106
+ images = np.random.randint(0, 256, size=(1, 384, 384, 3), dtype="uint8")
107
+ logits = model.predict(images, verbose=0)
108
+ print(keras_hub.utils.decode_imagenet_predictions(logits, top=5)[0])
109
+ ```