Zihua-Liu-CVer
commited on
Upload model
Browse files- config.json +13 -0
- configuration_convnet.py +19 -0
- modeling_convnet.py +49 -0
- pytorch_model.bin +3 -0
config.json
ADDED
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"architectures": [
|
3 |
+
"ConvNetModel"
|
4 |
+
],
|
5 |
+
"auto_map": {
|
6 |
+
"AutoConfig": "configuration_convnet.ConNetConfig",
|
7 |
+
"AutoModel": "modeling_convnet.ConvNetModel"
|
8 |
+
},
|
9 |
+
"model_type": "convnet",
|
10 |
+
"num_classes": 10,
|
11 |
+
"torch_dtype": "float32",
|
12 |
+
"transformers_version": "4.30.2"
|
13 |
+
}
|
configuration_convnet.py
ADDED
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import PretrainedConfig
|
2 |
+
|
3 |
+
class ConNetConfig(PretrainedConfig):
|
4 |
+
model_type = "convnet"
|
5 |
+
|
6 |
+
def __init__(
|
7 |
+
self,
|
8 |
+
num_classes=10,
|
9 |
+
**kwargs,
|
10 |
+
):
|
11 |
+
self.num_classes = num_classes
|
12 |
+
super().__init__(**kwargs)
|
13 |
+
|
14 |
+
|
15 |
+
if __name__=="__main__":
|
16 |
+
convnet_config = ConNetConfig(num_classes=10)
|
17 |
+
convnet_config.save_pretrained("custom-convnet")
|
18 |
+
|
19 |
+
pass
|
modeling_convnet.py
ADDED
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import PreTrainedModel
|
2 |
+
import torch
|
3 |
+
import torch.nn as nn
|
4 |
+
from .configuration_convnet import ConNetConfig
|
5 |
+
|
6 |
+
# Convolutional neural network (two convolutional layers)
|
7 |
+
class ConvNet(nn.Module):
|
8 |
+
def __init__(self, num_classes=10):
|
9 |
+
super(ConvNet, self).__init__()
|
10 |
+
self.layer1 = nn.Sequential(
|
11 |
+
nn.Conv2d(1, 16, kernel_size=5, stride=1, padding=2),
|
12 |
+
nn.BatchNorm2d(16),
|
13 |
+
nn.ReLU(),
|
14 |
+
nn.MaxPool2d(kernel_size=2, stride=2))
|
15 |
+
self.layer2 = nn.Sequential(
|
16 |
+
nn.Conv2d(16, 32, kernel_size=5, stride=1, padding=2),
|
17 |
+
nn.BatchNorm2d(32),
|
18 |
+
nn.ReLU(),
|
19 |
+
nn.MaxPool2d(kernel_size=2, stride=2))
|
20 |
+
self.fc = nn.Linear(7*7*32, num_classes)
|
21 |
+
|
22 |
+
def forward(self, x):
|
23 |
+
out = self.layer1(x)
|
24 |
+
out = self.layer2(out)
|
25 |
+
out = out.reshape(out.size(0), -1)
|
26 |
+
out = self.fc(out)
|
27 |
+
return out
|
28 |
+
|
29 |
+
|
30 |
+
|
31 |
+
|
32 |
+
class ConvNetModel(PreTrainedModel):
|
33 |
+
config_class = ConNetConfig
|
34 |
+
|
35 |
+
def __init__(self, config):
|
36 |
+
super().__init__(config)
|
37 |
+
self.model = ConvNet(num_classes=config.num_classes)
|
38 |
+
|
39 |
+
def forward(self, x):
|
40 |
+
out = self.model(x)
|
41 |
+
|
42 |
+
return out
|
43 |
+
|
44 |
+
|
45 |
+
if __name__=="__main__":
|
46 |
+
resnet50d_config = ConNetConfig(num_classes=10)
|
47 |
+
resnet50d = ConvNetModel(resnet50d_config)
|
48 |
+
resnet50d.save_pretrained("my_models")
|
49 |
+
pass
|
pytorch_model.bin
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:1b50fcbf2d4c2d9559fc3e165d9fe8c282ca44106a7d731b03d01bc768e58c7f
|
3 |
+
size 120885
|