Upload folder using huggingface_hub
Browse files- MyConfig.py +17 -0
- MyModel.py +29 -0
- MyPipe.py +65 -0
- README.md +34 -0
- config.json +25 -0
- model.safetensors +3 -0
MyConfig.py
ADDED
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
from transformers import PretrainedConfig
|
3 |
+
from typing import List
|
4 |
+
|
5 |
+
class MnistConfig(PretrainedConfig):
|
6 |
+
# since we have an image classification task
|
7 |
+
# we need to put a model type that is close to our task
|
8 |
+
# don't worry this will not affect our model
|
9 |
+
model_type = "MobileNetV1"
|
10 |
+
def __init__(
|
11 |
+
self,
|
12 |
+
conv1=10,
|
13 |
+
conv2=20,
|
14 |
+
**kwargs):
|
15 |
+
self.conv1 = conv1
|
16 |
+
self.conv2 = conv2
|
17 |
+
super().__init__(**kwargs)
|
MyModel.py
ADDED
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
from transformers import PreTrainedModel
|
3 |
+
from .MyConfig import MnistConfig
|
4 |
+
from torch import nn
|
5 |
+
import torch.nn.functional as F
|
6 |
+
|
7 |
+
class MnistModel(PreTrainedModel):
|
8 |
+
config_class = MnistConfig
|
9 |
+
def __init__(self, config):
|
10 |
+
super().__init__(config)
|
11 |
+
# use the config to instantiate our model
|
12 |
+
self.conv1 = nn.Conv2d(1, config.conv1, kernel_size=5)
|
13 |
+
self.conv2 = nn.Conv2d(config.conv1, config.conv2, kernel_size=5)
|
14 |
+
self.conv2_drop = nn.Dropout2d()
|
15 |
+
self.fc1 = nn.Linear(320, 50)
|
16 |
+
self.fc2 = nn.Linear(50, 10)
|
17 |
+
self.softmax = nn.Softmax(dim=-1)
|
18 |
+
def forward(self, x,labels=None):
|
19 |
+
x = F.relu(F.max_pool2d(self.conv1(x), 2))
|
20 |
+
x = F.relu(F.max_pool2d(self.conv2_drop(self.conv2(x)), 2))
|
21 |
+
x = x.view(-1, 320)
|
22 |
+
x = F.relu(self.fc1(x))
|
23 |
+
x = F.dropout(x, training=self.training)
|
24 |
+
x = self.fc2(x)
|
25 |
+
output = self.softmax(x)
|
26 |
+
if labels != None :
|
27 |
+
print("continue training script here")
|
28 |
+
|
29 |
+
return output
|
MyPipe.py
ADDED
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
from transformers import Pipeline
|
3 |
+
import requests
|
4 |
+
from PIL import Image
|
5 |
+
import torchvision.transforms as transforms
|
6 |
+
import torch
|
7 |
+
|
8 |
+
class MnistPipe(Pipeline):
|
9 |
+
def __init__(self,**kwargs):
|
10 |
+
|
11 |
+
# self.tokenizer = (...) # code if you want to instantiate more parameters
|
12 |
+
|
13 |
+
Pipeline.__init__(self,**kwargs) # self.model automatically instantiated here
|
14 |
+
|
15 |
+
self.transform = transforms.Compose(
|
16 |
+
[transforms.ToTensor(),
|
17 |
+
transforms.Resize((28,28), antialias=True)
|
18 |
+
])
|
19 |
+
|
20 |
+
def _sanitize_parameters(self, **kwargs):
|
21 |
+
# will make sure where each parameter goes
|
22 |
+
preprocess_kwargs = {}
|
23 |
+
postprocess_kwargs = {}
|
24 |
+
if "download" in kwargs:
|
25 |
+
preprocess_kwargs["download"] = kwargs["download"]
|
26 |
+
if "clean_output" in kwargs :
|
27 |
+
postprocess_kwargs["clean_output"] = kwargs["clean_output"]
|
28 |
+
return preprocess_kwargs, {}, postprocess_kwargs
|
29 |
+
|
30 |
+
def preprocess(self, inputs, download=False):
|
31 |
+
if download == True :
|
32 |
+
# call download_img method and name image as "image.png"
|
33 |
+
self.download_img(inputs)
|
34 |
+
inputs = "image.png"
|
35 |
+
|
36 |
+
# we open and process the image
|
37 |
+
img = Image.open(inputs)
|
38 |
+
gray = img.convert('L')
|
39 |
+
tensor = self.transform(gray)
|
40 |
+
tensor = tensor.unsqueeze(0)
|
41 |
+
return tensor
|
42 |
+
|
43 |
+
def _forward(self, tensor):
|
44 |
+
with torch.no_grad():
|
45 |
+
# the model has been automatically instantiated
|
46 |
+
# in the __init__ method
|
47 |
+
out = self.model(tensor)
|
48 |
+
return out
|
49 |
+
|
50 |
+
def postprocess(self, out, clean_output=True):
|
51 |
+
if clean_output ==True :
|
52 |
+
label = torch.argmax(out,axis=-1) # get class
|
53 |
+
label = label.tolist()[0]
|
54 |
+
return label
|
55 |
+
else :
|
56 |
+
return out
|
57 |
+
|
58 |
+
def download_img(self,url):
|
59 |
+
# if download = True download image and name it image.png
|
60 |
+
response = requests.get(url, stream=True)
|
61 |
+
|
62 |
+
with open("image.png", "wb") as f:
|
63 |
+
for chunk in response.iter_content(chunk_size=8192):
|
64 |
+
f.write(chunk)
|
65 |
+
print("image saved as image.png")
|
README.md
ADDED
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
---
|
3 |
+
tags:
|
4 |
+
- custom_code
|
5 |
+
---
|
6 |
+
# how to create custom architectures
|
7 |
+
you can read this [blogpost](https://huggingface.co/blog/not-lain/custom-architectures-with-huggingface) to find out more 📖
|
8 |
+
|
9 |
+
# How to use
|
10 |
+
you can the model via the command
|
11 |
+
```python
|
12 |
+
from transformers import AutoModelForImageClassification
|
13 |
+
model = AutoModelForImageClassification.from_pretrained("not-lain/MyRepo", trust_remote_code=True)
|
14 |
+
```
|
15 |
+
or you can use the pipeline
|
16 |
+
```python
|
17 |
+
from transformers import pipeline
|
18 |
+
pipe = pipeline(model="not-lain/MyRepo", trust_remote_code=True)
|
19 |
+
pipe(
|
20 |
+
"url",
|
21 |
+
download=True, # will call the download_img method
|
22 |
+
clean_output=False # will be passed as postprocess_kwargs
|
23 |
+
)
|
24 |
+
```
|
25 |
+
# parameters
|
26 |
+
the pipeline supports the following parameters :
|
27 |
+
* download
|
28 |
+
* clean_output
|
29 |
+
|
30 |
+
you can also use the following method to download images from the web
|
31 |
+
```python
|
32 |
+
pipe.download_img(url)
|
33 |
+
```
|
34 |
+
|
config.json
ADDED
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"_name_or_path": "not-lain/MyRepo",
|
3 |
+
"architectures": [
|
4 |
+
"MnistModel"
|
5 |
+
],
|
6 |
+
"auto_map": {
|
7 |
+
"AutoConfig": "MyConfig.MnistConfig",
|
8 |
+
"AutoModelForImageClassification": "MyModel.MnistModel"
|
9 |
+
},
|
10 |
+
"conv1": 10,
|
11 |
+
"conv2": 20,
|
12 |
+
"custom_pipelines": {
|
13 |
+
"image-classification": {
|
14 |
+
"impl": "MyPipe.MnistPipe",
|
15 |
+
"pt": [
|
16 |
+
"AutoModelForImageClassification"
|
17 |
+
],
|
18 |
+
"tf": [],
|
19 |
+
"type": "image"
|
20 |
+
}
|
21 |
+
},
|
22 |
+
"model_type": "MobileNetV1",
|
23 |
+
"torch_dtype": "float32",
|
24 |
+
"transformers_version": "4.35.2"
|
25 |
+
}
|
model.safetensors
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:6eee7d5198f4d57968bd06755727c4e89cd627b1b5fa7e0bfa42cf5a3bcd6697
|
3 |
+
size 87976
|