Upload 2 files
Browse files- interfere.py +123 -0
- interfere_cpu.py +116 -0
interfere.py
ADDED
@@ -0,0 +1,123 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# This file is provided by DOF Studio on huggingface.co
|
2 |
+
# with model animeGender-dvgg-0.7.
|
3 |
+
# Link: https://huggingface.co/DOFOFFICIAL/animeGender-dvgg-0.7
|
4 |
+
|
5 |
+
import cv2
|
6 |
+
import torch
|
7 |
+
import torchvision
|
8 |
+
import numpy as np
|
9 |
+
from torchvision import transforms
|
10 |
+
from PIL import Image
|
11 |
+
|
12 |
+
from transformers import undefined
|
13 |
+
|
14 |
+
num_cls = 2
|
15 |
+
classes = ['female', 'male']
|
16 |
+
|
17 |
+
#############################
|
18 |
+
# model struct
|
19 |
+
def model_struct():
|
20 |
+
model = torchvision.models.vgg16(pretrained=True)
|
21 |
+
|
22 |
+
last_dim = len(model.classifier) - 1
|
23 |
+
num_fc = model.classifier[last_dim].in_features
|
24 |
+
|
25 |
+
model.classifier[last_dim] = torch.nn.Linear(num_fc, num_cls)
|
26 |
+
|
27 |
+
model.classifier.append(torch.nn.Softmax())
|
28 |
+
|
29 |
+
for param in model.parameters():
|
30 |
+
param.requires_grad = False
|
31 |
+
|
32 |
+
for param in model.classifier[last_dim].parameters():
|
33 |
+
param.requires_grad = True
|
34 |
+
for param in model.classifier[last_dim + 1].parameters():
|
35 |
+
param.requires_grad = True
|
36 |
+
|
37 |
+
model = model.cuda() #GPU
|
38 |
+
|
39 |
+
return model
|
40 |
+
|
41 |
+
|
42 |
+
#############################
|
43 |
+
# graphic lib
|
44 |
+
def dim(imgpath):
|
45 |
+
img = cv2.imread(imgpath, 1)
|
46 |
+
height, width, channels = img.shape
|
47 |
+
return height, width, channels
|
48 |
+
|
49 |
+
def crop(imgfrom, imgto, x = 0, w = 64, y = 0, h = 64):
|
50 |
+
img = cv2.imread(imgfrom, 1)
|
51 |
+
img2 = img[y:y+h, x:x+w]
|
52 |
+
return cv2.imwrite(imgto, img2)
|
53 |
+
|
54 |
+
def resize(imgfrom, imgto, width, height):
|
55 |
+
img = cv2.imread(imgfrom, 1)
|
56 |
+
img2 = cv2.resize(img, (width, height))
|
57 |
+
return cv2.imwrite(imgto, img2)
|
58 |
+
|
59 |
+
def rgb32to24(imgfrom, imgto):
|
60 |
+
img = cv2.imread(imgfrom, 1)
|
61 |
+
img2 = cv2.cvtColor(img, cv2.COLOR_BGRA2BGR)
|
62 |
+
return cv2.imwrite(imgto, img2)
|
63 |
+
|
64 |
+
def cmpgraph_64x64(imgfrom, imgto):
|
65 |
+
height, width, channels = dim(imgfrom)
|
66 |
+
img = cv2.imread(imgfrom, 1)
|
67 |
+
img2 = []
|
68 |
+
if height > width:
|
69 |
+
hnew = int(np.round(64 / width * height))
|
70 |
+
wnew = 64
|
71 |
+
img2 = cv2.resize(img, (wnew, hnew))
|
72 |
+
img2 = img2[0:64, 0:64]
|
73 |
+
elif width > height:
|
74 |
+
wnew = int(np.round(64 / height * width))
|
75 |
+
hnew = 64
|
76 |
+
img2 = cv2.resize(img, (wnew, hnew))
|
77 |
+
img2 = img2[0:64, 0:64]
|
78 |
+
else:
|
79 |
+
img2 = cv2.resize(img, (64,64))
|
80 |
+
img3 = cv2.cvtColor(img2, cv2.COLOR_BGRA2BGR)
|
81 |
+
return cv2.imwrite(imgto, img3)
|
82 |
+
|
83 |
+
|
84 |
+
#############################
|
85 |
+
# model usage
|
86 |
+
def predict_class(img_path, model):
|
87 |
+
img = Image.open(img_path)
|
88 |
+
transform = transforms.Compose([transforms.ToTensor()])
|
89 |
+
img = transform(img).cuda()
|
90 |
+
img = torch.unsqueeze(img, dim=0)
|
91 |
+
out = model(img)
|
92 |
+
max = torch.max(out).item()
|
93 |
+
pmax = torch.max(out, 1)[1].item()
|
94 |
+
cls = classes[pmax]
|
95 |
+
print('This is ' + cls + ' with confidence of ' + str(np.round(max, 3)))
|
96 |
+
|
97 |
+
def modelload(modelpath):
|
98 |
+
device = torch.device('cuda')
|
99 |
+
model = model_struct()
|
100 |
+
model.to(device)
|
101 |
+
model.eval()
|
102 |
+
save = torch.load(modelpath)
|
103 |
+
model.load_state_dict(save)
|
104 |
+
return model
|
105 |
+
|
106 |
+
def predictmain(model, filepath):
|
107 |
+
img = filepath
|
108 |
+
predict_class(img, model)
|
109 |
+
|
110 |
+
|
111 |
+
if __name__ == '__main__':
|
112 |
+
# transfomer usage
|
113 |
+
model = undefined.from_pretrained("undefined")
|
114 |
+
model.load_adapter("DOFOFFICIAL/animeGender-dvgg-0.7", source="hf")
|
115 |
+
model.to("cuda")
|
116 |
+
model.eval()
|
117 |
+
|
118 |
+
# local usage
|
119 |
+
model = modelload("model_animeGender-dvgg-0.7.pth")
|
120 |
+
|
121 |
+
# use your picture to interfere
|
122 |
+
cmpgraph_64x64("path.png", "path(1).png")
|
123 |
+
predictmain(model, "path(1).png")
|
interfere_cpu.py
ADDED
@@ -0,0 +1,116 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# This file is provided by DOF Studio on huggingface.co
|
2 |
+
# with model animeGender-dvgg-0.7.
|
3 |
+
# Link: https://huggingface.co/DOFOFFICIAL/animeGender-dvgg-0.7
|
4 |
+
|
5 |
+
import cv2
|
6 |
+
import torch
|
7 |
+
import torchvision
|
8 |
+
import numpy as np
|
9 |
+
from torchvision import transforms
|
10 |
+
from PIL import Image
|
11 |
+
|
12 |
+
from transformers import undefined
|
13 |
+
|
14 |
+
num_cls = 2
|
15 |
+
classes = ['female', 'male']
|
16 |
+
|
17 |
+
#############################
|
18 |
+
# model struct
|
19 |
+
def model_struct():
|
20 |
+
model = torchvision.models.vgg16(pretrained=True)
|
21 |
+
|
22 |
+
last_dim = len(model.classifier) - 1
|
23 |
+
num_fc = model.classifier[last_dim].in_features
|
24 |
+
|
25 |
+
model.classifier[last_dim] = torch.nn.Linear(num_fc, num_cls)
|
26 |
+
|
27 |
+
model.classifier.append(torch.nn.Softmax())
|
28 |
+
|
29 |
+
for param in model.parameters():
|
30 |
+
param.requires_grad = False
|
31 |
+
|
32 |
+
for param in model.classifier[last_dim].parameters():
|
33 |
+
param.requires_grad = True
|
34 |
+
for param in model.classifier[last_dim + 1].parameters():
|
35 |
+
param.requires_grad = True
|
36 |
+
|
37 |
+
return model
|
38 |
+
|
39 |
+
|
40 |
+
#############################
|
41 |
+
# graphic lib
|
42 |
+
def dim(imgpath):
|
43 |
+
img = cv2.imread(imgpath, 1)
|
44 |
+
height, width, channels = img.shape
|
45 |
+
return height, width, channels
|
46 |
+
|
47 |
+
def crop(imgfrom, imgto, x = 0, w = 64, y = 0, h = 64):
|
48 |
+
img = cv2.imread(imgfrom, 1)
|
49 |
+
img2 = img[y:y+h, x:x+w]
|
50 |
+
return cv2.imwrite(imgto, img2)
|
51 |
+
|
52 |
+
def resize(imgfrom, imgto, width, height):
|
53 |
+
img = cv2.imread(imgfrom, 1)
|
54 |
+
img2 = cv2.resize(img, (width, height))
|
55 |
+
return cv2.imwrite(imgto, img2)
|
56 |
+
|
57 |
+
def rgb32to24(imgfrom, imgto):
|
58 |
+
img = cv2.imread(imgfrom, 1)
|
59 |
+
img2 = cv2.cvtColor(img, cv2.COLOR_BGRA2BGR)
|
60 |
+
return cv2.imwrite(imgto, img2)
|
61 |
+
|
62 |
+
def cmpgraph_64x64(imgfrom, imgto):
|
63 |
+
height, width, channels = dim(imgfrom)
|
64 |
+
img = cv2.imread(imgfrom, 1)
|
65 |
+
img2 = []
|
66 |
+
if height > width:
|
67 |
+
hnew = int(np.round(64 / width * height))
|
68 |
+
wnew = 64
|
69 |
+
img2 = cv2.resize(img, (wnew, hnew))
|
70 |
+
img2 = img2[0:64, 0:64]
|
71 |
+
elif width > height:
|
72 |
+
wnew = int(np.round(64 / height * width))
|
73 |
+
hnew = 64
|
74 |
+
img2 = cv2.resize(img, (wnew, hnew))
|
75 |
+
img2 = img2[0:64, 0:64]
|
76 |
+
else:
|
77 |
+
img2 = cv2.resize(img, (64,64))
|
78 |
+
img3 = cv2.cvtColor(img2, cv2.COLOR_BGRA2BGR)
|
79 |
+
return cv2.imwrite(imgto, img3)
|
80 |
+
|
81 |
+
|
82 |
+
#############################
|
83 |
+
# model usage
|
84 |
+
def predict_class(img_path, model):
|
85 |
+
img = Image.open(img_path)
|
86 |
+
transform = transforms.Compose([transforms.ToTensor()])
|
87 |
+
img = transform(img)
|
88 |
+
img = torch.unsqueeze(img, dim=0)
|
89 |
+
out = model(img)
|
90 |
+
max = torch.max(out).item()
|
91 |
+
pmax = torch.max(out, 1)[1].item()
|
92 |
+
cls = classes[pmax]
|
93 |
+
print('This is ' + cls + ' with confidence of ' + str(np.round(max, 3)))
|
94 |
+
|
95 |
+
def modelload(modelpath):
|
96 |
+
model = model_struct()
|
97 |
+
save = torch.load(modelpath)
|
98 |
+
model.load_state_dict(save)
|
99 |
+
return model
|
100 |
+
|
101 |
+
def predictmain(model, filepath):
|
102 |
+
img = filepath
|
103 |
+
predict_class(img, model)
|
104 |
+
|
105 |
+
|
106 |
+
if __name__ == '__main__':
|
107 |
+
# transfomer usage
|
108 |
+
model = undefined.from_pretrained("undefined")
|
109 |
+
model.load_adapter("DOFOFFICIAL/animeGender-dvgg-0.7", source="hf")
|
110 |
+
|
111 |
+
# local usage
|
112 |
+
model = modelload("model_animeGender-dvgg-0.7.pth")
|
113 |
+
|
114 |
+
# use your picture to interfere
|
115 |
+
cmpgraph_64x64("path.png", "path(1).png")
|
116 |
+
predictmain(model, "path(1).png")
|