Upload model
Browse files- config.json +20 -0
- configuration_spice_cnn.py +48 -0
- modeling_spice_cnn.py +44 -0
- pytorch_model.bin +3 -0
config.json
ADDED
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"architectures": [
|
3 |
+
"SpiceCNNModelForImageClassification"
|
4 |
+
],
|
5 |
+
"auto_map": {
|
6 |
+
"AutoConfig": "configuration_spice_cnn.SpiceCNNConfig",
|
7 |
+
"AutoModelForImageClassification": "modeling_spice_cnn.SpiceCNNModelForImageClassification"
|
8 |
+
},
|
9 |
+
"dropout_rate": 0.2,
|
10 |
+
"hidden_size": 128,
|
11 |
+
"kernel_size": 3,
|
12 |
+
"model_type": "spicecnn",
|
13 |
+
"num_classes": 10,
|
14 |
+
"num_filters": 16,
|
15 |
+
"padding": 1,
|
16 |
+
"pooling_size": 2,
|
17 |
+
"stride": 1,
|
18 |
+
"torch_dtype": "float32",
|
19 |
+
"transformers_version": "4.29.2"
|
20 |
+
}
|
configuration_spice_cnn.py
ADDED
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import PretrainedConfig
|
2 |
+
|
3 |
+
"""Spice CNN model configuration"""
|
4 |
+
|
5 |
+
SPICE_CNN_PRETRAINED_CONFIG_ARCHIVE_MAP = {
|
6 |
+
"spicecloud/spice-cnn-base": "https://huggingface.co/spice-cnn-base/resolve/main/config.json"
|
7 |
+
}
|
8 |
+
|
9 |
+
|
10 |
+
# Define custom convnet configuration
|
11 |
+
class SpiceCNNConfig(PretrainedConfig):
|
12 |
+
"""
|
13 |
+
This is the configuration class to store the configuration of a [`SpiceCNNModel`].
|
14 |
+
It is used to instantiate an SpiceCNN model according to the specified arguments,
|
15 |
+
defining the model architecture. Instantiating a configuration with the defaults
|
16 |
+
will yield a similar configuration to that of the SpiceCNN
|
17 |
+
[spicecloud/spice-cnn-base](https://huggingface.co/spicecloud/spice-cnn-base)
|
18 |
+
architecture.
|
19 |
+
|
20 |
+
Configuration objects inherit from [`PretrainedConfig`] and can be used to control
|
21 |
+
the model outputs. Read the documentation from [`PretrainedConfig`] for more
|
22 |
+
information.
|
23 |
+
"""
|
24 |
+
|
25 |
+
model_type = "spicecnn"
|
26 |
+
|
27 |
+
def __init__(
|
28 |
+
self,
|
29 |
+
num_classes: int = 10,
|
30 |
+
dropout_rate: float = 0.2,
|
31 |
+
hidden_size: int = 128,
|
32 |
+
num_filters: int = 16,
|
33 |
+
kernel_size: int = 3,
|
34 |
+
stride: int = 1,
|
35 |
+
padding: int = 1,
|
36 |
+
pooling_size: int = 2,
|
37 |
+
**kwargs
|
38 |
+
):
|
39 |
+
super().__init__(**kwargs)
|
40 |
+
|
41 |
+
self.num_classes = num_classes
|
42 |
+
self.dropout_rate = dropout_rate
|
43 |
+
self.hidden_size = hidden_size
|
44 |
+
self.num_filters = num_filters
|
45 |
+
self.kernel_size = kernel_size
|
46 |
+
self.stride = stride
|
47 |
+
self.padding = padding
|
48 |
+
self.pooling_size = pooling_size
|
modeling_spice_cnn.py
ADDED
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch.nn as nn
|
2 |
+
|
3 |
+
from transformers import PreTrainedModel
|
4 |
+
|
5 |
+
from .configuration_spice_cnn import SpiceCNNConfig
|
6 |
+
|
7 |
+
|
8 |
+
class SpiceCNNModelForImageClassification(PreTrainedModel):
|
9 |
+
config_class = SpiceCNNConfig
|
10 |
+
|
11 |
+
def __init__(self, config: SpiceCNNConfig):
|
12 |
+
super().__init__(config)
|
13 |
+
layers = [
|
14 |
+
nn.Conv2d(
|
15 |
+
1,
|
16 |
+
32,
|
17 |
+
kernel_size=config.kernel_size,
|
18 |
+
stride=config.stride,
|
19 |
+
padding=config.padding,
|
20 |
+
),
|
21 |
+
nn.ReLU(),
|
22 |
+
nn.MaxPool2d(kernel_size=config.pooling_size),
|
23 |
+
nn.Conv2d(
|
24 |
+
32,
|
25 |
+
64,
|
26 |
+
kernel_size=config.kernel_size,
|
27 |
+
stride=config.stride,
|
28 |
+
padding=config.padding,
|
29 |
+
),
|
30 |
+
nn.ReLU(),
|
31 |
+
nn.MaxPool2d(kernel_size=config.pooling_size),
|
32 |
+
nn.Flatten(),
|
33 |
+
nn.Linear(7 * 7 * 64, 128),
|
34 |
+
nn.ReLU(),
|
35 |
+
nn.Linear(128, config.num_classes),
|
36 |
+
]
|
37 |
+
self.model = nn.Sequential(*layers)
|
38 |
+
|
39 |
+
def forward(self, tensor, labels=None):
|
40 |
+
logits = self.model(tensor)
|
41 |
+
if labels is not None:
|
42 |
+
loss = nn.CrossEntropyLoss(logits, labels)
|
43 |
+
return {"loss": loss, "logits": logits}
|
44 |
+
return {"logits": logits}
|
pytorch_model.bin
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:cb3719d131ffeda1fe4d66a097fad200329816c955394cadbecc1f849f7438e7
|
3 |
+
size 1689227
|