Image Classification
Keras
LiteRT
TF-Keras
Safetensors
English
efficientnetv2-s
efficientnetv2
fgic
transfer-learning
gem-pooling
focal-loss
swa
grad-cam
calibration
temperature-scaling
computer-vision
tensorflow.js
Eval Results (legacy)
Instructions to use 0xgr3y/Arch-Building-Image-Classification with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Keras
How to use 0xgr3y/Arch-Building-Image-Classification with Keras:
# Available backend options are: "jax", "torch", "tensorflow". import os os.environ["KERAS_BACKEND"] = "jax" import keras model = keras.saving.load_model("hf://0xgr3y/Arch-Building-Image-Classification") - Notebooks
- Google Colab
- Kaggle
Update model README
Browse files
README.md
CHANGED
|
@@ -114,9 +114,9 @@ Output (6 classes)
|
|
| 114 |
|
| 115 |
| Metric | Value |
|
| 116 |
|--------|-------|
|
| 117 |
-
| Test Accuracy |
|
| 118 |
-
| Validation Accuracy (SWA) |
|
| 119 |
-
| Test-Time Augmentation |
|
| 120 |
| Test Loss | 0.3974 |
|
| 121 |
| Overfitting Gap (Train β Test) | 3.22% |
|
| 122 |
| Macro Avg Precision | 96.29% |
|
|
@@ -135,7 +135,7 @@ Output (6 classes)
|
|
| 135 |
| Temple | 97.55% | 94.64% | 96.07% | 168 |
|
| 136 |
| **Macro Avg** | **96.29%** | **96.23%** | **96.21%** | **1,008** |
|
| 137 |
|
| 138 |
-
**Highest performing classes:** Skyscraper (recall=99.40%), Castle (F1=97.92%)
|
| 139 |
**Most challenging classes:** Stadium (recall=90.48%), Bridge (precision=94.19%)
|
| 140 |
|
| 141 |
### Model Selection
|
|
@@ -174,8 +174,8 @@ Two-phase progressive training with SWA post-processing:
|
|
| 174 |
|
| 175 |
| Phase | Description | Backbone | Optimizer | LR | Max Epochs | Actual Epochs | CutMix+Mixup | FocalLoss LS |
|
| 176 |
|-------|-------------|----------|-----------|-----|-----------|---------------|---------------|-------------|
|
| 177 |
-
| **Phase 1** β Feature Extraction | Train custom head only | Frozen (all) | AdamW (wd=2e-5) | 0.001 + CosineDecay + Warmup 3ep | 25 |
|
| 178 |
-
| **Phase 2** β Selective Fine-Tuning | Load best_phase1 β fine-tune | conv4_block + conv5_block unfrozen (BN frozen) | DiscriminativeAdamW (conv4=0.1Γ) | 3e-4 + CosineDecay + Warmup 5ep | 50 |
|
| 179 |
|
| 180 |
> ΒΉ Phase 1 stopped at epoch 1 because `val_accuracy = 86.71% β₯ 85%` threshold (myCallback). This demonstrates the effectiveness of ImageNet transfer learning β a single epoch of head training exceeds the target.
|
| 181 |
|
|
@@ -209,19 +209,19 @@ Two-phase progressive training with SWA post-processing:
|
|
| 209 |
|-----------|---------------|-----------|
|
| 210 |
| Transfer Learning | DenseNet121 backbone frozen in Phase 1 | Yosinski et al., NeurIPS 2014 |
|
| 211 |
| Selective Fine-Tuning | Unfreeze conv4+conv5 only, BN stays frozen | Howard & Ruder, ACL 2018 |
|
| 212 |
-
| Discriminative LR | conv4=0.1Γ, conv5+head=1.0Γ |
|
| 213 |
| CutMix + Mixup | Alternation per batch (50/50), Phase 1 only | Yun et al., ICCV 2019; Zhang et al., ICLR 2018 |
|
| 214 |
| Focal Loss | gamma=2.0, down-weights easy examples | Lin et al., ICCV 2017 |
|
| 215 |
| Label Smoothing | 0.1 (Phase 1) β 0.05 (Phase 2) | Szegedy et al., CVPR 2016 |
|
| 216 |
| GeM Pooling | p=3.0 learnable, replaces GAP | Radenovic et al., CVPR 2018 |
|
| 217 |
-
| Dropout | 0.4 after Dense(256)+BN |
|
| 218 |
-
| Batch Normalization | After Conv2D and Dense; frozen during fine-tuning |
|
| 219 |
-
| EMA | Shadow weights, decay=0.999 |
|
| 220 |
| SWA | 5-epoch post-training, constant LR 1e-4 | Izmailov et al., UAI 2018 |
|
| 221 |
-
| Data Augmentation | Rotation Β±15Β°, shift Β±10%, zoom Β±20%, brightness 0.75β1.15, horizontal flip |
|
| 222 |
-
| Test-Time Augmentation | 6 augmentation variants, averaged |
|
| 223 |
-
| WarmupCosineDecay | Linear warmup + cosine annealing |
|
| 224 |
-
| Early Stopping | Patience 7 (Phase 1) / 12 (Phase 2) |
|
| 225 |
|
| 226 |
### Dataset
|
| 227 |
|
|
@@ -238,7 +238,7 @@ Two-phase progressive training with SWA post-processing:
|
|
| 238 |
- **Normalization:** `preprocess_input` from `tf.keras.applications.densenet` (ImageNet distribution)
|
| 239 |
- **Input resolution:** 320Γ320 (higher than ImageNet default 224Γ224 to capture fine-grained architectural details β textures, ornaments, facade patterns)
|
| 240 |
- **Augmentation:** Applied to training set only; validation and test sets use clean preprocessing
|
| 241 |
-
- **Split method:** `splitfolders.ratio` from `dataset
|
| 242 |
|
| 243 |
## Files
|
| 244 |
|
|
@@ -270,7 +270,7 @@ from tensorflow.keras.layers import Layer
|
|
| 270 |
from PIL import Image
|
| 271 |
import numpy as np
|
| 272 |
|
| 273 |
-
# ---
|
| 274 |
|
| 275 |
class GeMPooling(Layer):
|
| 276 |
def __init__(self, p=3.0, eps=1e-6, **kwargs):
|
|
@@ -345,7 +345,7 @@ class DiscriminativeAdamW(tf.keras.optimizers.AdamW):
|
|
| 345 |
return {**super().get_config(), "lr_multipliers": self.lr_multipliers,
|
| 346 |
"backbone_layer_idx": self.backbone_layer_idx}
|
| 347 |
|
| 348 |
-
# ---
|
| 349 |
|
| 350 |
LABELS = ["bridge", "castle", "mosque", "skyscraper", "stadium", "temple"]
|
| 351 |
custom_objects = {
|
|
@@ -357,7 +357,7 @@ custom_objects = {
|
|
| 357 |
model_path = hf_hub_download("0xgr3y/Arch-Building-Image-Classification", "best_phase2_swa.keras")
|
| 358 |
model = tf.keras.models.load_model(model_path, custom_objects=custom_objects, compile=False)
|
| 359 |
|
| 360 |
-
# ---
|
| 361 |
|
| 362 |
img = Image.open("building.jpg").convert("RGB").resize((320, 320))
|
| 363 |
arr = np.expand_dims(preprocess_input(np.array(img, dtype=np.float32)), axis=0)
|
|
@@ -365,7 +365,7 @@ preds = model.predict(arr, verbose=0)[0]
|
|
| 365 |
print(f"Predicted: {LABELS[np.argmax(preds)]} ({np.max(preds)*100:.1f}%)")
|
| 366 |
```
|
| 367 |
|
| 368 |
-
### Python β
|
| 369 |
|
| 370 |
Download the model first:
|
| 371 |
```python
|
|
@@ -448,7 +448,7 @@ print(f"Predicted: {LABELS[np.argmax(preds)]} ({np.max(preds)*100:.1f}%)")
|
|
| 448 |
|
| 449 |
## Ethical Considerations
|
| 450 |
|
| 451 |
-
- All training images sourced from [Pexels](https://www.pexels.com) under the Pexels License (free for commercial use, no attribution required). No copyrighted or personally identifiable images were used.
|
| 452 |
- The dataset contains only photographs of buildings and structures β no people, faces, or private property are the subject of classification.
|
| 453 |
- The model reflects the visual distribution of Pexels stock photography, which may over-represent Western and iconic architectural styles and under-represent vernacular or regional architecture.
|
| 454 |
- The 6 class categories are broad and do not capture the full diversity of world architecture. Results should not be used to make definitive claims about architectural categorization.
|
|
@@ -471,6 +471,13 @@ print(f"Predicted: {LABELS[np.argmax(preds)]} ({np.max(preds)*100:.1f}%)")
|
|
| 471 |
7. Szegedy, C., Vanhoucke, V., Ioffe, S., Shlens, J., & Wojna, Z. (2016). Rethinking the Inception Architecture for Computer Vision. *CVPR 2016*. [arXiv:1512.00567](https://arxiv.org/abs/1512.00567)
|
| 472 |
8. Yosinski, J., Clune, J., Bengio, Y., & Lipson, H. (2014). How Transferable Are Features in Deep Neural Networks? *NeurIPS 2014*. [arXiv:1411.1792](https://arxiv.org/abs/1411.1792)
|
| 473 |
9. Howard, J., & Ruder, S. (2018). Universal Language Model Fine-tuning for Text Classification. *ACL 2018*. [arXiv:1801.06146](https://arxiv.org/abs/1801.06146)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 474 |
|
| 475 |
## Citation
|
| 476 |
|
|
|
|
| 114 |
|
| 115 |
| Metric | Value |
|
| 116 |
|--------|-------|
|
| 117 |
+
| Test Accuracy | 96.23% (970/1,008) |
|
| 118 |
+
| Validation Accuracy (SWA) | 95.93% |
|
| 119 |
+
| Test-Time Augmentation | 96.33% (+0.10%) |
|
| 120 |
| Test Loss | 0.3974 |
|
| 121 |
| Overfitting Gap (Train β Test) | 3.22% |
|
| 122 |
| Macro Avg Precision | 96.29% |
|
|
|
|
| 135 |
| Temple | 97.55% | 94.64% | 96.07% | 168 |
|
| 136 |
| **Macro Avg** | **96.29%** | **96.23%** | **96.21%** | **1,008** |
|
| 137 |
|
| 138 |
+
**Highest performing classes:** Skyscraper (recall=99.40%), Castle (F1=97.92%)
|
| 139 |
**Most challenging classes:** Stadium (recall=90.48%), Bridge (precision=94.19%)
|
| 140 |
|
| 141 |
### Model Selection
|
|
|
|
| 174 |
|
| 175 |
| Phase | Description | Backbone | Optimizer | LR | Max Epochs | Actual Epochs | CutMix+Mixup | FocalLoss LS |
|
| 176 |
|-------|-------------|----------|-----------|-----|-----------|---------------|---------------|-------------|
|
| 177 |
+
| **Phase 1** β Feature Extraction | Train custom head only | Frozen (all) | AdamW (wd=2e-5) | 0.001 + CosineDecay + Warmup 3ep | 25 | 1 ΒΉ | Yes (50/50 alternation) | 0.1 |
|
| 178 |
+
| **Phase 2** β Selective Fine-Tuning | Load best_phase1 β fine-tune | conv4_block + conv5_block unfrozen (BN frozen) | DiscriminativeAdamW (conv4=0.1Γ) | 3e-4 + CosineDecay + Warmup 5ep | 50 | 6 + 5 SWA Β² | No | 0.05 |
|
| 179 |
|
| 180 |
> ΒΉ Phase 1 stopped at epoch 1 because `val_accuracy = 86.71% β₯ 85%` threshold (myCallback). This demonstrates the effectiveness of ImageNet transfer learning β a single epoch of head training exceeds the target.
|
| 181 |
|
|
|
|
| 209 |
|-----------|---------------|-----------|
|
| 210 |
| Transfer Learning | DenseNet121 backbone frozen in Phase 1 | Yosinski et al., NeurIPS 2014 |
|
| 211 |
| Selective Fine-Tuning | Unfreeze conv4+conv5 only, BN stays frozen | Howard & Ruder, ACL 2018 |
|
| 212 |
+
| Discriminative LR | conv4=0.1Γ, conv5+head=1.0Γ | Howard & Ruder, ACL 2018 |
|
| 213 |
| CutMix + Mixup | Alternation per batch (50/50), Phase 1 only | Yun et al., ICCV 2019; Zhang et al., ICLR 2018 |
|
| 214 |
| Focal Loss | gamma=2.0, down-weights easy examples | Lin et al., ICCV 2017 |
|
| 215 |
| Label Smoothing | 0.1 (Phase 1) β 0.05 (Phase 2) | Szegedy et al., CVPR 2016 |
|
| 216 |
| GeM Pooling | p=3.0 learnable, replaces GAP | Radenovic et al., CVPR 2018 |
|
| 217 |
+
| Dropout | 0.4 after Dense(256)+BN | Srivastava et al., JMLR 2014 |
|
| 218 |
+
| Batch Normalization | After Conv2D and Dense; frozen during fine-tuning | Ioffe & Szegedy, arXiv 2015 |
|
| 219 |
+
| EMA | Shadow weights, decay=0.999 | Tarvainen & Valpola, NeurIPS 2017 |
|
| 220 |
| SWA | 5-epoch post-training, constant LR 1e-4 | Izmailov et al., UAI 2018 |
|
| 221 |
+
| Data Augmentation | Rotation Β±15Β°, shift Β±10%, zoom Β±20%, brightness 0.75β1.15, horizontal flip | Perez & Wang, arXiv 2017 |
|
| 222 |
+
| Test-Time Augmentation | 6 augmentation variants, averaged | Shanmugam et al., ICML 2020 |
|
| 223 |
+
| WarmupCosineDecay | Linear warmup + cosine annealing | Loshchilov & Hutter, ICLR 2017 (SGDR) |
|
| 224 |
+
| Early Stopping | Patience 7 (Phase 1) / 12 (Phase 2) | Prechelt, Neural Networks 1998 |
|
| 225 |
|
| 226 |
### Dataset
|
| 227 |
|
|
|
|
| 238 |
- **Normalization:** `preprocess_input` from `tf.keras.applications.densenet` (ImageNet distribution)
|
| 239 |
- **Input resolution:** 320Γ320 (higher than ImageNet default 224Γ224 to capture fine-grained architectural details β textures, ornaments, facade patterns)
|
| 240 |
- **Augmentation:** Applied to training set only; validation and test sets use clean preprocessing
|
| 241 |
+
- **Split method:** `splitfolders.ratio` from `dataset/`, seed=42
|
| 242 |
|
| 243 |
## Files
|
| 244 |
|
|
|
|
| 270 |
from PIL import Image
|
| 271 |
import numpy as np
|
| 272 |
|
| 273 |
+
# ========---Custom Layers (must match training definition)---========
|
| 274 |
|
| 275 |
class GeMPooling(Layer):
|
| 276 |
def __init__(self, p=3.0, eps=1e-6, **kwargs):
|
|
|
|
| 345 |
return {**super().get_config(), "lr_multipliers": self.lr_multipliers,
|
| 346 |
"backbone_layer_idx": self.backbone_layer_idx}
|
| 347 |
|
| 348 |
+
# =====================---Load Model---==========================
|
| 349 |
|
| 350 |
LABELS = ["bridge", "castle", "mosque", "skyscraper", "stadium", "temple"]
|
| 351 |
custom_objects = {
|
|
|
|
| 357 |
model_path = hf_hub_download("0xgr3y/Arch-Building-Image-Classification", "best_phase2_swa.keras")
|
| 358 |
model = tf.keras.models.load_model(model_path, custom_objects=custom_objects, compile=False)
|
| 359 |
|
| 360 |
+
# =======================---Inference---==========================
|
| 361 |
|
| 362 |
img = Image.open("building.jpg").convert("RGB").resize((320, 320))
|
| 363 |
arr = np.expand_dims(preprocess_input(np.array(img, dtype=np.float32)), axis=0)
|
|
|
|
| 365 |
print(f"Predicted: {LABELS[np.argmax(preds)]} ({np.max(preds)*100:.1f}%)")
|
| 366 |
```
|
| 367 |
|
| 368 |
+
### Python β TensorFlow Lite
|
| 369 |
|
| 370 |
Download the model first:
|
| 371 |
```python
|
|
|
|
| 448 |
|
| 449 |
## Ethical Considerations
|
| 450 |
|
| 451 |
+
- All training images sourced from [Pexels.com](https://www.pexels.com) under the Pexels License (free for commercial use, no attribution required). No copyrighted or personally identifiable images were used.
|
| 452 |
- The dataset contains only photographs of buildings and structures β no people, faces, or private property are the subject of classification.
|
| 453 |
- The model reflects the visual distribution of Pexels stock photography, which may over-represent Western and iconic architectural styles and under-represent vernacular or regional architecture.
|
| 454 |
- The 6 class categories are broad and do not capture the full diversity of world architecture. Results should not be used to make definitive claims about architectural categorization.
|
|
|
|
| 471 |
7. Szegedy, C., Vanhoucke, V., Ioffe, S., Shlens, J., & Wojna, Z. (2016). Rethinking the Inception Architecture for Computer Vision. *CVPR 2016*. [arXiv:1512.00567](https://arxiv.org/abs/1512.00567)
|
| 472 |
8. Yosinski, J., Clune, J., Bengio, Y., & Lipson, H. (2014). How Transferable Are Features in Deep Neural Networks? *NeurIPS 2014*. [arXiv:1411.1792](https://arxiv.org/abs/1411.1792)
|
| 473 |
9. Howard, J., & Ruder, S. (2018). Universal Language Model Fine-tuning for Text Classification. *ACL 2018*. [arXiv:1801.06146](https://arxiv.org/abs/1801.06146)
|
| 474 |
+
10. Srivastava, N., Hinton, G., Krizhevsky, A., Sutskever, I., & Salakhutdinov, R. (2014). Dropout: A Simple Way to Prevent Neural Networks from Overfitting. *JMLR*, 15(56), 1929β1958. [http://jmlr.org/papers/v15/srivastava14a.html](http://jmlr.org/papers/v15/srivastava14a.html)
|
| 475 |
+
11. Ioffe, S., & Szegedy, C. (2015). Batch Normalization: Accelerating Deep Network Training by Reducing Internal Covariate Shift. *arXiv preprint*. [arXiv:1502.03167](https://arxiv.org/abs/1502.03167)
|
| 476 |
+
12. Tarvainen, A., & Valpola, H. (2017). Mean Teachers are Better Role Models: Weight-averaged Consistency Targets Improve Semi-supervised Deep Learning Results. *NeurIPS 2017*. [arXiv:1703.01780](https://arxiv.org/abs/1703.01780)
|
| 477 |
+
13. Perez, L., & Wang, J. (2017). The Effectiveness of Data Augmentation in Image Classification using Deep Learning. *arXiv preprint*. [arXiv:1712.04621](https://arxiv.org/abs/1712.04621)
|
| 478 |
+
14. Shanmugam, D., Blalock, D., Balakrishnan, G., Guttag, J., & Sarma, A. (2020). Towards Principled Test-Time Augmentation. *ICML 2020*. [PDF](https://dmshanmugam.github.io/pdfs/icml_2020_testaug.pdf)
|
| 479 |
+
15. Loshchilov, I., & Hutter, F. (2017). SGDR: Stochastic Gradient Descent with Warm Restarts. *ICLR 2017*. [arXiv:1608.03983](https://arxiv.org/abs/1608.03983)
|
| 480 |
+
16. Prechelt, L. (1998). Automatic Early Stopping Using Cross Validation: Quantifying the Criteria. *Neural Networks*, 11(4), 761β767. [https://doi.org/10.1016/S0893-6080(98)00010-0](https://doi.org/10.1016/S0893-6080(98)00010-0)
|
| 481 |
|
| 482 |
## Citation
|
| 483 |
|