FredZhang7
commited on
Commit
•
70a3c4c
1
Parent(s):
d237e30
Upload model
Browse files- config.json +11 -2
- model.py +62 -0
- pytorch_model.bin +2 -2
config.json
CHANGED
@@ -1,4 +1,11 @@
|
|
1 |
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
"classes": {
|
3 |
"0": "nsfw_gore",
|
4 |
"1": "nsfw_suggestive",
|
@@ -6,6 +13,7 @@
|
|
6 |
},
|
7 |
"classifier": "default",
|
8 |
"crop_pct": 0.875,
|
|
|
9 |
"first_conv": "conv2d",
|
10 |
"has_aux": false,
|
11 |
"input_channels": 3,
|
@@ -34,5 +42,6 @@
|
|
34 |
0.5,
|
35 |
0.5
|
36 |
],
|
37 |
-
"
|
38 |
-
|
|
|
|
1 |
{
|
2 |
+
"architectures": [
|
3 |
+
"SafeSearchModel"
|
4 |
+
],
|
5 |
+
"auto_map": {
|
6 |
+
"AutoConfig": "model.SafeSearchConfig",
|
7 |
+
"AutoModelForImageClassification": "model.SafeSearchModel"
|
8 |
+
},
|
9 |
"classes": {
|
10 |
"0": "nsfw_gore",
|
11 |
"1": "nsfw_suggestive",
|
|
|
13 |
},
|
14 |
"classifier": "default",
|
15 |
"crop_pct": 0.875,
|
16 |
+
"device": "cpu",
|
17 |
"first_conv": "conv2d",
|
18 |
"has_aux": false,
|
19 |
"input_channels": 3,
|
|
|
42 |
0.5,
|
43 |
0.5
|
44 |
],
|
45 |
+
"torch_dtype": "float32",
|
46 |
+
"transformers_version": "4.25.1"
|
47 |
+
}
|
model.py
ADDED
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
classes = { '0': 'nsfw_gore', '1': 'nsfw_suggestive', '2': 'safe' }
|
2 |
+
model_path = "safesearch_mini_v2.bin"
|
3 |
+
|
4 |
+
from transformers import PretrainedConfig, PreTrainedModel
|
5 |
+
|
6 |
+
class SafeSearchConfig(PretrainedConfig):
|
7 |
+
model_type = "safesearch_mini_v2"
|
8 |
+
def __init__(self,
|
9 |
+
model_name: str = "safesearch_mini_v2",
|
10 |
+
input_channels: int = 3,
|
11 |
+
num_classes: int = 3,
|
12 |
+
input_size: list[int] = [3, 299, 299],
|
13 |
+
pool_size: list[int] = [8, 8],
|
14 |
+
crop_pct: float = 0.875,
|
15 |
+
interpolation: str = "bicubic",
|
16 |
+
mean: list[float] = [0.5, 0.5, 0.5],
|
17 |
+
std: list[float] = [0.5, 0.5, 0.5],
|
18 |
+
first_conv: str = "conv2d",
|
19 |
+
classifier: str = "default",
|
20 |
+
has_aux: bool = False,
|
21 |
+
label_offset: int = 0,
|
22 |
+
classes: list[str] = classes,
|
23 |
+
output_channels: int = 1536,
|
24 |
+
device: str = "cpu",
|
25 |
+
**kwargs):
|
26 |
+
self.model_name = model_name
|
27 |
+
self.input_channels = input_channels
|
28 |
+
self.num_classes = num_classes
|
29 |
+
self.input_size = input_size
|
30 |
+
self.pool_size = pool_size
|
31 |
+
self.crop_pct = crop_pct
|
32 |
+
self.interpolation = interpolation
|
33 |
+
self.mean = mean
|
34 |
+
self.std = std
|
35 |
+
self.first_conv = first_conv
|
36 |
+
self.classifier = classifier
|
37 |
+
self.has_aux = has_aux
|
38 |
+
self.label_offset = label_offset
|
39 |
+
self.classes = classes
|
40 |
+
self.output_channels = output_channels
|
41 |
+
self.device = device
|
42 |
+
super().__init__(**kwargs)
|
43 |
+
|
44 |
+
"""
|
45 |
+
safesearch_config = SafeSearchConfig()
|
46 |
+
safesearch_config.save_pretrained("safesearch_config")
|
47 |
+
"""
|
48 |
+
|
49 |
+
import torch, os, timm
|
50 |
+
|
51 |
+
class SafeSearchModel(PreTrainedModel):
|
52 |
+
config_class = SafeSearchConfig
|
53 |
+
def __init__(self, config: SafeSearchConfig):
|
54 |
+
super().__init__(config)
|
55 |
+
if not os.path.exists(model_path):
|
56 |
+
from urllib.request import urlretrieve
|
57 |
+
urlretrieve(f"https://huggingface.co/FredZhang7/google-safesearch-mini-v2/resolve/main/pytorch_model.bin", model_path)
|
58 |
+
self.model = timm.create_model("inception_resnet_v2", pretrained=False, num_classes=3)
|
59 |
+
self.model.load_state_dict(torch.load(model_path, map_location=torch.device(config.device)))
|
60 |
+
|
61 |
+
def forward(self, input_ids: torch.Tensor):
|
62 |
+
return self.model(input_ids)
|
pytorch_model.bin
CHANGED
@@ -1,3 +1,3 @@
|
|
1 |
version https://git-lfs.github.com/spec/v1
|
2 |
-
oid sha256:
|
3 |
-
size
|
|
|
1 |
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:55829a7fa31ff8f643b9c7158c449573c52ee3f4ab52b87a669b920a503348ee
|
3 |
+
size 217901159
|