Skip to content

Commit

Permalink
fix: Error in model prediction due to non-existent folder (#649)
Browse files Browse the repository at this point in the history
  • Loading branch information
CVHub520 committed Sep 25, 2024
1 parent 301991b commit f538bd9
Show file tree
Hide file tree
Showing 22 changed files with 78 additions and 61 deletions.
7 changes: 1 addition & 6 deletions anylabeling/config.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import os.path as osp
import shutil
import yaml

try:
import importlib.resources as pkg_resources
except ImportError:
# Try backported to PY<37 `importlib_resources`.
import importlib_resources as pkg_resources
import importlib.resources as pkg_resources

from anylabeling import configs as anylabeling_configs
from anylabeling.views.labeling.logger import logger
Expand Down
11 changes: 8 additions & 3 deletions anylabeling/services/auto_labeling/__base__/clip.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from functools import lru_cache

from ..engines import OnnxBaseModel
from anylabeling.views.labeling.logger import logger


_MODEL_INFO = {
Expand Down Expand Up @@ -155,9 +156,13 @@ def tokenize(self, texts, context_length=52):

@lru_cache()
def default_vocab():
current_dir = os.path.dirname(__file__)
return os.path.join(current_dir, "..", "configs", "clip_vocab.txt")

import importlib.resources
from anylabeling.services.auto_labeling import configs
try:
with importlib.resources.path(configs.clip, 'clip_vocab.txt') as p:
return str(p)
except Exception as e:
logger.error(f"Error loading default vocab: {e}")

def convert_to_unicode(text):
"""Converts `text` to Unicode (if it's not already), assuming utf-8 input."""
Expand Down
18 changes: 7 additions & 11 deletions anylabeling/services/auto_labeling/__base__/ram.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,19 +121,15 @@ def predict_shapes(self, image, image_path=None):

@staticmethod
def load_tag_list():
current_dir = os.path.dirname(__file__)
tag_list_file = os.path.join(
current_dir, "..", "configs", "ram_tag_list.txt"
)
tag_list_chinese_file = os.path.join(
current_dir, "..", "configs", "ram_tag_list_chinese.txt"
)
import importlib.resources as pkg_resources
from anylabeling.services.auto_labeling.configs import ram

with open(tag_list_file, "r", encoding="utf-8") as f:
tag_list = f.read().splitlines()
with pkg_resources.path(ram, 'ram_tag_list.txt') as p:
tag_list = p.read_text().splitlines()
tag_list = np.array(tag_list)
with open(tag_list_chinese_file, "r", encoding="utf-8") as f:
tag_list_chinese = f.read().splitlines()

with pkg_resources.path(ram, 'ram_tag_list_chinese.txt') as p:
tag_list_chinese = p.read_text().splitlines()
tag_list_chinese = np.array(tag_list_chinese)

return tag_list, tag_list_chinese
Expand Down
4 changes: 4 additions & 0 deletions anylabeling/services/auto_labeling/configs/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from . import bert
from . import clip
from . import ppocr
from . import ram
Empty file.
Empty file.
Empty file.
Empty file.
6 changes: 0 additions & 6 deletions anylabeling/services/auto_labeling/edge_sam.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,6 @@ def post_process(self, masks, image=None):
shape.add_point(QtCore.QPointF(point[0], point[1]))
shape.shape_type = "polygon"
shape.closed = True
shape.fill_color = "#000000"
shape.line_color = "#000000"
shape.line_width = 1
shape.label = "AUTOLABEL_OBJECT"
shape.selected = False
shapes.append(shape)
Expand Down Expand Up @@ -222,9 +219,6 @@ def post_process(self, masks, image=None):
"rectangle" if self.output_mode == "rectangle" else "rotation"
)
shape.closed = True
shape.fill_color = "#000000"
shape.line_color = "#000000"
shape.line_width = 1
if self.clip_net is not None and self.classes:
img = image[y_min:y_max, x_min:x_max]
out = self.clip_net(img, self.classes)
Expand Down
13 changes: 9 additions & 4 deletions anylabeling/services/auto_labeling/grounding_dino.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,11 +357,16 @@ def get_caption(text_prompt):

@staticmethod
def get_tokenlizer(text_encoder_type):
current_dir = os.path.dirname(__file__)
import importlib.resources
from anylabeling.services.auto_labeling import configs
cfg_name = text_encoder_type.replace("-", "_") + "_tokenizer.json"
cfg_file = os.path.join(current_dir, "configs", cfg_name)
tokenizer = Tokenizer.from_file(cfg_file)
return tokenizer
try:
with importlib.resources.path(configs.bert, cfg_name) as p:
tokenizer = Tokenizer.from_file(str(p))
return tokenizer
except Exception as e:
logger.error(f"Error loading tokenizer: {e}")
return None

@staticmethod
def get_phrases_from_posmap(
Expand Down
13 changes: 9 additions & 4 deletions anylabeling/services/auto_labeling/grounding_sam.py
Original file line number Diff line number Diff line change
Expand Up @@ -771,11 +771,16 @@ def get_caption(text_prompt):

@staticmethod
def get_tokenlizer(text_encoder_type):
current_dir = os.path.dirname(__file__)
import importlib.resources
from anylabeling.services.auto_labeling import configs
cfg_name = text_encoder_type.replace("-", "_") + "_tokenizer.json"
cfg_file = os.path.join(current_dir, "configs", cfg_name)
tokenizer = Tokenizer.from_file(cfg_file)
return tokenizer
try:
with importlib.resources.path(configs.bert, cfg_name) as p:
tokenizer = Tokenizer.from_file(str(p))
return tokenizer
except Exception as e:
logger.error(f"Error loading tokenizer: {e}")
return None

@staticmethod
def get_phrases_from_posmap(
Expand Down
13 changes: 9 additions & 4 deletions anylabeling/services/auto_labeling/grounding_sam2.py
Original file line number Diff line number Diff line change
Expand Up @@ -551,11 +551,16 @@ def get_caption(text_prompt):

@staticmethod
def get_tokenlizer(text_encoder_type):
current_dir = os.path.dirname(__file__)
import importlib.resources
from anylabeling.services.auto_labeling import configs
cfg_name = text_encoder_type.replace("-", "_") + "_tokenizer.json"
cfg_file = os.path.join(current_dir, "configs", cfg_name)
tokenizer = Tokenizer.from_file(cfg_file)
return tokenizer
try:
with importlib.resources.path(configs.bert, cfg_name) as p:
tokenizer = Tokenizer.from_file(str(p))
return tokenizer
except Exception as e:
logger.error(f"Error loading tokenizer: {e}")
return None

@staticmethod
def get_phrases_from_posmap(
Expand Down
24 changes: 11 additions & 13 deletions anylabeling/services/auto_labeling/yolov5_ram.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,20 +177,18 @@ def predict_shapes(self, image, image_path=None):

@staticmethod
def load_tag_list():
current_dir = os.path.dirname(__file__)
tag_list_file = os.path.join(
current_dir, "configs", "ram_tag_list.txt"
)
tag_list_chinese_file = os.path.join(
current_dir, "configs", "ram_tag_list_chinese.txt"
)
import importlib.resources as pkg_resources
from anylabeling.services.auto_labeling import configs

with open(tag_list_file, "r", encoding="utf-8") as f:
tag_list = f.read().splitlines()
tag_list = np.array(tag_list)
with open(tag_list_chinese_file, "r", encoding="utf-8") as f:
tag_list_chinese = f.read().splitlines()
tag_list_chinese = np.array(tag_list_chinese)
try:
tag_list = pkg_resources.read_text(configs.ram, 'ram_tag_list.txt').splitlines()
tag_list = np.array(tag_list)

tag_list_chinese = pkg_resources.read_text(configs.ram, 'ram_tag_list_chinese.txt').splitlines()
tag_list_chinese = np.array(tag_list_chinese)
except Exception as e:
logger.error(f"Error loading tag list: {e}")
return np.array([]), np.array([])

return tag_list, tag_list_chinese

Expand Down
6 changes: 4 additions & 2 deletions x-anylabeling-linux-cpu.spec
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ a = Analysis(
('anylabeling/configs/auto_labeling/*.yaml', 'anylabeling/configs/auto_labeling'),
('anylabeling/configs/*.yaml', 'anylabeling/configs'),
('anylabeling/views/labeling/widgets/auto_labeling/auto_labeling.ui', 'anylabeling/views/labeling/widgets/auto_labeling'),
('anylabeling/services/auto_labeling/configs/*.json', 'anylabeling/services/auto_labeling/configs'),
('anylabeling/services/auto_labeling/configs/*.txt', 'anylabeling/services/auto_labeling/configs')
('anylabeling/services/auto_labeling/configs/bert/*', 'anylabeling/services/auto_labeling/configs/bert'),
('anylabeling/services/auto_labeling/configs/clip/*', 'anylabeling/services/auto_labeling/configs/clip'),
('anylabeling/services/auto_labeling/configs/ppocr/*', 'anylabeling/services/auto_labeling/configs/ppocr'),
('anylabeling/services/auto_labeling/configs/ram/*', 'anylabeling/services/auto_labeling/configs/ram')
],
hiddenimports=[],
hookspath=[],
Expand Down
6 changes: 4 additions & 2 deletions x-anylabeling-linux-gpu.spec
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ a = Analysis(
('anylabeling/configs/auto_labeling/*.yaml', 'anylabeling/configs/auto_labeling'),
('anylabeling/configs/*.yaml', 'anylabeling/configs'),
('anylabeling/views/labeling/widgets/auto_labeling/auto_labeling.ui', 'anylabeling/views/labeling/widgets/auto_labeling'),
('anylabeling/services/auto_labeling/configs/*.json', 'anylabeling/services/auto_labeling/configs'),
('anylabeling/services/auto_labeling/configs/*.txt', 'anylabeling/services/auto_labeling/configs'),
('anylabeling/services/auto_labeling/configs/bert/*', 'anylabeling/services/auto_labeling/configs/bert'),
('anylabeling/services/auto_labeling/configs/clip/*', 'anylabeling/services/auto_labeling/configs/clip'),
('anylabeling/services/auto_labeling/configs/ppocr/*', 'anylabeling/services/auto_labeling/configs/ppocr'),
('anylabeling/services/auto_labeling/configs/ram/*', 'anylabeling/services/auto_labeling/configs/ram'),
('/home/cvhub/miniconda3/envs/x-anylabeling-gpu/lib/python3.8/site-packages/onnxruntime/capi/libonnxruntime_providers_cuda.so', 'onnxruntime/capi'),
('/home/cvhub/miniconda3/envs/x-anylabeling-gpu/lib/python3.8/site-packages/onnxruntime/capi/libonnxruntime_providers_shared.so', 'onnxruntime/capi'),
],
Expand Down
6 changes: 4 additions & 2 deletions x-anylabeling-macos.spec
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ a = Analysis(
('anylabeling/configs/auto_labeling/*.yaml', 'anylabeling/configs/auto_labeling'),
('anylabeling/configs/*.yaml', 'anylabeling/configs'),
('anylabeling/views/labeling/widgets/auto_labeling/auto_labeling.ui', 'anylabeling/views/labeling/widgets/auto_labeling'),
('anylabeling/services/auto_labeling/configs/*.json', 'anylabeling/services/auto_labeling/configs'),
('anylabeling/services/auto_labeling/configs/*.txt', 'anylabeling/services/auto_labeling/configs')
('anylabeling/services/auto_labeling/configs/bert/*', 'anylabeling/services/auto_labeling/configs/bert'),
('anylabeling/services/auto_labeling/configs/clip/*', 'anylabeling/services/auto_labeling/configs/clip'),
('anylabeling/services/auto_labeling/configs/ppocr/*', 'anylabeling/services/auto_labeling/configs/ppocr'),
('anylabeling/services/auto_labeling/configs/ram/*', 'anylabeling/services/auto_labeling/configs/ram')
],
hiddenimports=[],
hookspath=[],
Expand Down
6 changes: 4 additions & 2 deletions x-anylabeling-win-cpu.spec
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ a = Analysis(
('anylabeling/configs/auto_labeling/*.yaml', 'anylabeling/configs/auto_labeling'),
('anylabeling/configs/*.yaml', 'anylabeling/configs'),
('anylabeling/views/labeling/widgets/auto_labeling/auto_labeling.ui', 'anylabeling/views/labeling/widgets/auto_labeling'),
('anylabeling/services/auto_labeling/configs/*.json', 'anylabeling/services/auto_labeling/configs'),
('anylabeling/services/auto_labeling/configs/*.txt', 'anylabeling/services/auto_labeling/configs')
('anylabeling/services/auto_labeling/configs/bert/*', 'anylabeling/services/auto_labeling/configs/bert'),
('anylabeling/services/auto_labeling/configs/clip/*', 'anylabeling/services/auto_labeling/configs/clip'),
('anylabeling/services/auto_labeling/configs/ppocr/*', 'anylabeling/services/auto_labeling/configs/ppocr'),
('anylabeling/services/auto_labeling/configs/ram/*', 'anylabeling/services/auto_labeling/configs/ram')
],
hiddenimports=[],
hookspath=[],
Expand Down
6 changes: 4 additions & 2 deletions x-anylabeling-win-gpu.spec
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ a = Analysis(
('anylabeling/configs/auto_labeling/*.yaml', 'anylabeling/configs/auto_labeling'),
('anylabeling/configs/*.yaml', 'anylabeling/configs'),
('anylabeling/views/labeling/widgets/auto_labeling/auto_labeling.ui', 'anylabeling/views/labeling/widgets/auto_labeling'),
('anylabeling/services/auto_labeling/configs/*.json', 'anylabeling/services/auto_labeling/configs'),
('anylabeling/services/auto_labeling/configs/*.txt', 'anylabeling/services/auto_labeling/configs'),
('anylabeling/services/auto_labeling/configs/bert/*', 'anylabeling/services/auto_labeling/configs/bert'),
('anylabeling/services/auto_labeling/configs/clip/*', 'anylabeling/services/auto_labeling/configs/clip'),
('anylabeling/services/auto_labeling/configs/ppocr/*', 'anylabeling/services/auto_labeling/configs/ppocr'),
('anylabeling/services/auto_labeling/configs/ram/*', 'anylabeling/services/auto_labeling/configs/ram'),
('C:/Users/18102/.conda/envs/x-anylabeling-gpu/Lib/site-packages/onnxruntime/capi/onnxruntime_providers_cuda.dll', 'onnxruntime/capi'),
('C:/Users/18102/.conda/envs/x-anylabeling-gpu/Lib/site-packages/onnxruntime/capi/onnxruntime_providers_shared.dll', 'onnxruntime/capi')
],
Expand Down

0 comments on commit f538bd9

Please sign in to comment.