Image Classification
KerasHub
File size: 4,281 Bytes
f265b5c
 
dfca48d
 
 
28f0992
f265b5c
79514d3
310f762
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f34253a
310f762
 
819402c
310f762
 
 
 
 
 
 
 
 
 
 
 
 
 
 
819402c
310f762
 
 
 
 
 
 
 
 
819402c
310f762
 
 
 
 
 
 
 
 
 
 
 
 
 
 
819402c
310f762
 
 
79514d3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
---
library_name: keras-hub
license: apache-2.0
tags:
- image-classification
pipeline_tag: image-classification
---
### Model Overview
Instantiates the ResNet architecture. This model is supported in both KerasCV and KerasHub. KerasCV will no longer be actively developed, so please try to use KerasHub.

**Reference**

- [Deep Residual Learning for Image Recognition](https://arxiv.org/abs/1512.03385)

The difference in ResNetV1 and ResNetV2 rests in the structure of their
individual building blocks. In ResNetV2, the batch normalization and
ReLU activation precede the convolution layers, as opposed to ResNetV1 where
the batch normalization and ReLU activation are applied after the
convolution layers.

## Links

coming soon


## Installation

Keras and KerasHub can be installed with:

```
pip install -U -q keras-hub
pip install -U -q keras
```

Jax, TensorFlow, and Torch come preinstalled in Kaggle Notebooks. For instructions on installing them in another environment see the [Keras Getting Started](https://keras.io/getting_started/) page.

## Presets

The following model checkpoints are provided by the Keras team. Weights have been ported from: https://huggingface.co/timm. Full code examples for each are available below.

| Preset name                        | Parameters | Description                                                                                                                                                                                                                                                                                               |
|------------------------------------|------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| resnet_18_imagenet | 11.19M      | 18-layer ResNet model pre-trained on the ImageNet 1k dataset at a 224x224 resolution |
| resnet_50_imagenet | 23.56M      | 50-layer ResNet model pre-trained on the ImageNet 1k dataset at a 224x224 resolution |
| resnet_101_imagenet | 42.61M      | 101-layer ResNet model pre-trained on the ImageNet 1k dataset at a 224x224 resolution |
| resnet_152_imagenet | 58.30M      | 52-layer ResNet model pre-trained on the ImageNet 1k dataset at a 224x224 resolution |

## Example Usage
```python
    # Pretrained ResNet backbone.
    model = keras_hub.models.ResNetBackbone.from_preset("resnet_18_imagenet")
    input_data = np.random.uniform(0, 1, size=(2, 224, 224, 3))
    model(input_data)

    # Randomly initialized ResNetV2 backbone with a custom config.
    model = keras_hub.models.ResNetBackbone(
        input_conv_filters=[64],
        input_conv_kernel_sizes=[7],
        stackwise_num_filters=[64, 64, 64],
        stackwise_num_blocks=[2, 2, 2],
        stackwise_num_strides=[1, 2, 2],
        block_type="basic_block",
        use_pre_activation=True,
    )
    model(input_data)
    # Use resnet for image classification task
    model = keras_hub.models.ImageClassifier.from_preset("resnet_18_imagenet")

    # User timm presets directly from hugingface
    model = keras_hub.models.ImageClassifier.from_preset('hf://timm/resnet101.a1_in1k')
```

## Example Usage with Hugging Face URI

```python
    # Pretrained ResNet backbone.
    model = keras_hub.models.ResNetBackbone.from_preset("hf://keras/resnet_18_imagenet")
    input_data = np.random.uniform(0, 1, size=(2, 224, 224, 3))
    model(input_data)

    # Randomly initialized ResNetV2 backbone with a custom config.
    model = keras_hub.models.ResNetBackbone(
        input_conv_filters=[64],
        input_conv_kernel_sizes=[7],
        stackwise_num_filters=[64, 64, 64],
        stackwise_num_blocks=[2, 2, 2],
        stackwise_num_strides=[1, 2, 2],
        block_type="basic_block",
        use_pre_activation=True,
    )
    model(input_data)
    # Use resnet for image classification task
    model = keras_hub.models.ImageClassifier.from_preset("hf://keras/resnet_18_imagenet")

    # User timm presets directly from hugingface
    model = keras_hub.models.ImageClassifier.from_preset('hf://timm/resnet101.a1_in1k')
```