Spaces:
Sleeping
Sleeping
Annas Dev
commited on
Commit
·
0f98d7f
1
Parent(s):
cf69c91
add new model
Browse files- app.py +1 -1
- src/similarity/model_implements/mobilenet_v3.py +12 -2
- src/similarity/model_implements/vit_base.py +16 -0
- src/similarity/similarity.py +14 -2
- src/util/image.py +1 -1
- src/util/matrix.py +5 -0
app.py
CHANGED
@@ -21,7 +21,7 @@ def check(img_main, img_1, img_2, model_idx):
|
|
21 |
for i in range(3)
|
22 |
]
|
23 |
similarity.check_similarity([img_main, img_1, img_2], models[model_idx])
|
24 |
-
return
|
25 |
|
26 |
# def greet(name):
|
27 |
# return "Hello " + name + "!!"
|
|
|
21 |
for i in range(3)
|
22 |
]
|
23 |
similarity.check_similarity([img_main, img_1, img_2], models[model_idx])
|
24 |
+
return []
|
25 |
|
26 |
# def greet(name):
|
27 |
# return "Hello " + name + "!!"
|
src/similarity/model_implements/mobilenet_v3.py
CHANGED
@@ -1,4 +1,14 @@
|
|
|
|
|
|
|
|
1 |
class ModelnetV3():
|
2 |
-
def
|
|
|
|
|
|
|
|
|
3 |
print('getting with ModelnetV3...')
|
4 |
-
|
|
|
|
|
|
|
|
1 |
+
import tensorflow_hub as hub
|
2 |
+
import numpy as np
|
3 |
+
|
4 |
class ModelnetV3():
|
5 |
+
def __init__(self):
|
6 |
+
module_handle = "https://tfhub.dev/google/imagenet/mobilenet_v3_large_100_224/feature_vector/5"
|
7 |
+
self.module = hub.load(module_handle)
|
8 |
+
|
9 |
+
def extract_feature(self, imgs):
|
10 |
print('getting with ModelnetV3...')
|
11 |
+
features = []
|
12 |
+
for img in imgs:
|
13 |
+
features.append(np.squeeze(self.module(img)))
|
14 |
+
return features
|
src/similarity/model_implements/vit_base.py
ADDED
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import ViTFeatureExtractor, ViTForImageClassification
|
2 |
+
from PIL import Image
|
3 |
+
|
4 |
+
class VitBase():
|
5 |
+
|
6 |
+
def __init__(self):
|
7 |
+
self.model = ViTFeatureExtractor.from_pretrained('google/vit-base-patch16-224')
|
8 |
+
|
9 |
+
def extract_feature(self, imgs):
|
10 |
+
features = []
|
11 |
+
for img in imgs:
|
12 |
+
feature = self.model(images=img, return_tensors="np")
|
13 |
+
print('type::', type(**feature))
|
14 |
+
features.append(feature)
|
15 |
+
print(features[0].shape)
|
16 |
+
return features
|
src/similarity/similarity.py
CHANGED
@@ -1,10 +1,16 @@
|
|
1 |
from src.model import simlarity_model as model
|
2 |
from src.util import image as image_util
|
|
|
3 |
from .model_implements.mobilenet_v3 import ModelnetV3
|
|
|
|
|
4 |
|
5 |
class Similarity:
|
6 |
def get_models(self):
|
7 |
-
return [
|
|
|
|
|
|
|
8 |
|
9 |
def check_similarity(self, img_urls, model):
|
10 |
# model = self.get_models()[model_idx]
|
@@ -12,7 +18,13 @@ class Similarity:
|
|
12 |
for url in img_urls:
|
13 |
if url == "": continue
|
14 |
imgs.append(image_util.load_image_url(url, required_size=(model.image_size, model.image_size)))
|
15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
return 'oke'
|
17 |
|
18 |
|
|
|
1 |
from src.model import simlarity_model as model
|
2 |
from src.util import image as image_util
|
3 |
+
from src.util import matrix
|
4 |
from .model_implements.mobilenet_v3 import ModelnetV3
|
5 |
+
from .model_implements.vit_base import VitBase
|
6 |
+
|
7 |
|
8 |
class Similarity:
|
9 |
def get_models(self):
|
10 |
+
return [
|
11 |
+
model.SimilarityModel(name= 'Mobilenet V3', image_size= 224, model_cls = ModelnetV3()),
|
12 |
+
model.SimilarityModel(name= 'Vision Transformer', image_size= 224, model_cls = VitBase()),
|
13 |
+
]
|
14 |
|
15 |
def check_similarity(self, img_urls, model):
|
16 |
# model = self.get_models()[model_idx]
|
|
|
18 |
for url in img_urls:
|
19 |
if url == "": continue
|
20 |
imgs.append(image_util.load_image_url(url, required_size=(model.image_size, model.image_size)))
|
21 |
+
|
22 |
+
features = model.model_cls.extract_feature(imgs)
|
23 |
+
for i, v in enumerate(features):
|
24 |
+
if i == 0: continue
|
25 |
+
dist = matrix.cosine(features[0], v)
|
26 |
+
print(f'distance: {dist}')
|
27 |
+
|
28 |
return 'oke'
|
29 |
|
30 |
|
src/util/image.py
CHANGED
@@ -6,5 +6,5 @@ def load_image_url(url, required_size = (224,224)):
|
|
6 |
img = Image.open(requests.get(url, stream=True).raw)
|
7 |
img = Image.fromarray(np.array(img))
|
8 |
img = img.resize(required_size)
|
9 |
-
img = (np.expand_dims(np.array(img), 0)/255).astype(np.float32)
|
10 |
return img
|
|
|
6 |
img = Image.open(requests.get(url, stream=True).raw)
|
7 |
img = Image.fromarray(np.array(img))
|
8 |
img = img.resize(required_size)
|
9 |
+
#img = (np.expand_dims(np.array(img), 0)/255).astype(np.float32)
|
10 |
return img
|
src/util/matrix.py
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from numpy.linalg import norm
|
2 |
+
import numpy as np
|
3 |
+
|
4 |
+
def cosine(x, y):
|
5 |
+
return np.dot(x,y)/(norm(x)*norm(y))
|