hjxdl 0.1.64__py3-none-any.whl → 0.1.66__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
hdl/_version.py CHANGED
@@ -12,5 +12,5 @@ __version__: str
12
12
  __version_tuple__: VERSION_TUPLE
13
13
  version_tuple: VERSION_TUPLE
14
14
 
15
- __version__ = version = '0.1.64'
16
- __version_tuple__ = version_tuple = (0, 1, 64)
15
+ __version__ = version = '0.1.66'
16
+ __version_tuple__ = version_tuple = (0, 1, 66)
hdl/utils/llm/vis.py CHANGED
@@ -6,6 +6,7 @@ import numpy as np
6
6
  from PIL import Image
7
7
  # from transformers import ChineseCLIPProcessor, ChineseCLIPModel
8
8
  import open_clip
9
+ import natsort
9
10
 
10
11
  from ..database_tools.connect import conn_redis
11
12
 
@@ -20,27 +21,43 @@ class ImgHandler:
20
21
  def __init__(
21
22
  self,
22
23
  model_path,
23
- redis_host,
24
- redis_port,
24
+ db_host,
25
+ db_port,
25
26
  model_name: str = None,
26
- device: str = None
27
+ device: str = "cpu",
28
+ num_vec_dim: int = None,
29
+ load_model: bool = True,
27
30
  ) -> None:
28
- if device is None:
29
- self.device = torch.device("cuda") \
30
- if torch.cuda.is_available() \
31
- else torch.device("cpu")
32
- else:
33
- self.device = device
31
+
32
+ self.device = torch.device(device)
33
+ self.model_path = model_path
34
+ self.model_name = model_name
35
+
36
+ self.db_host = db_host
37
+ self.db_port = db_port
38
+ self._db_conn = None
39
+ self.num_vec_dim = num_vec_dim
40
+ if load_model:
41
+ self.load_model()
42
+
43
+ def load_model(self):
44
+ """Load the OpenCLIP model and related configurations.
45
+
46
+ This function loads the OpenCLIP model from the specified model path
47
+ and initializes the necessary components such as the model,
48
+ preprocessors for training and validation data, tokenizer, etc.
49
+
50
+ Returns:
51
+ None
52
+ """
34
53
  ckpt_file = (
35
- Path(model_path) / Path("open_clip_pytorch_model.bin")
54
+ Path(self.model_path) / Path("open_clip_pytorch_model.bin")
36
55
  ).as_posix()
37
-
38
56
  self.open_clip_cfg = json.load(
39
- open(Path(model_path) / Path("open_clip_config.json"))
57
+ open(Path(self.model_path) / Path("open_clip_config.json"))
40
58
  )
41
- if model_name is not None:
42
- self.model_name = model_name
43
- else:
59
+
60
+ if self.model_name is None:
44
61
  self.model_name = (
45
62
  self.open_clip_cfg['model_cfg']['text_cfg']['hf_tokenizer_name']
46
63
  .split('/')[-1]
@@ -54,27 +71,22 @@ class ImgHandler:
54
71
  # precision=precision
55
72
  )
56
73
  )
57
- self.vector_dimension = self.open_clip_cfg["model_cfg"]["embed_dim"]
74
+ if self.num_vec_dim is None:
75
+ self.num_vec_dim = self.open_clip_cfg["model_cfg"]["embed_dim"]
58
76
  self.tokenizer = open_clip.get_tokenizer(
59
- HF_HUB_PREFIX + model_path
77
+ HF_HUB_PREFIX + self.model_path
60
78
  )
61
- # self.model = ChineseCLIPModel.from_pretrained(model_path).to(self.device)
62
- # self.processor = ChineseCLIPProcessor.from_pretrained(model_path)
63
- self.redis_host = redis_host
64
- self.redis_port = redis_port
65
- self._redis_conn = None
66
-
67
79
 
68
80
  @property
69
- def redis_conn(self):
81
+ def db_conn(self):
70
82
  """Establishes a connection to Redis server if not already connected.
71
83
 
72
84
  Returns:
73
85
  Redis connection object: A connection to the Redis server.
74
86
  """
75
- if self._redis_conn is None:
76
- self._redis_conn = conn_redis(self.redis_host, self.redis_port)
77
- return self._redis_conn
87
+ if self._db_conn is None:
88
+ self._db_conn = conn_redis(self.db_host, self.db_port)
89
+ return self._db_conn
78
90
 
79
91
  def get_img_features(
80
92
  self,
@@ -192,4 +204,27 @@ class ImgHandler:
192
204
  # > 0.9 很相似
193
205
  return sims
194
206
 
207
+ def vec_pics_todb(self, images):
208
+ """Save image features to a Redis database.
209
+
210
+ Args:
211
+ images (list): A list of image file paths.
212
+
213
+ Returns:
214
+ None
215
+
216
+ Example:
217
+ vec_pics_todb(images=['image1.jpg', 'image2.jpg'])
218
+ """
219
+ sorted_imgs = natsort.natsorted(images)
220
+ img_feats = self.get_img_features(sorted_imgs, to_numpy=True)
221
+ pipeline = self.ih.db_conn.pipeline()
222
+ for img_file, emb in zip(sorted_imgs, img_feats):
223
+ # 初始化 Redis,先使用 img 文件名作为 Key 和 Value,后续再更新为图片特征向量
224
+ pipeline.json().set(img_file, "$", img_file)
225
+ emb = emb.astype(np.float32).tolist()
226
+ pipeline.json().set(img_file, "$", emb)
227
+ res = pipeline.execute()
228
+ print('redis set:', res)
229
+
195
230
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: hjxdl
3
- Version: 0.1.64
3
+ Version: 0.1.66
4
4
  Summary: A collection of functions for Jupyter notebooks
5
5
  Home-page: https://github.com/huluxiaohuowa/hdl
6
6
  Author: Jianxing Hu
@@ -23,6 +23,7 @@ Requires-Dist: redis[hiredis]
23
23
  Requires-Dist: psycopg[binary]
24
24
  Requires-Dist: Pillow
25
25
  Requires-Dist: open-clip-torch
26
+ Requires-Dist: natsort
26
27
 
27
28
  # DL framework by Jianxing
28
29
 
@@ -1,5 +1,5 @@
1
1
  hdl/__init__.py,sha256=GffnD0jLJdhkd-vo989v40N90sQbofkayRBwxc6TVhQ,72
2
- hdl/_version.py,sha256=jeNdt9VGQNw1iIiYHVNvXMaEDEn4mkNvXMSVcAGw4fI,413
2
+ hdl/_version.py,sha256=tr9Pq2sozjd5Ty_4lPM_A-i6XowDPOaB6nT_SLKjIE4,413
3
3
  hdl/args/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
4
4
  hdl/args/loss_args.py,sha256=s7YzSdd7IjD24rZvvOrxLLFqMZQb9YylxKeyelSdrTk,70
5
5
  hdl/controllers/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
@@ -131,12 +131,12 @@ hdl/utils/llm/chat.py,sha256=sk7Lw5Oa30k-l2fnJknkMmTc5zkBeEKsR981aeFhH5s,11907
131
131
  hdl/utils/llm/embs.py,sha256=Tf0FOYrOFZp7qQpEPiSCXzlgyHH0X9HVTUtsup74a9E,7174
132
132
  hdl/utils/llm/extract.py,sha256=2sK_WJzmYIc8iuWaM9DA6Nw3_6q1O4lJ5pKpcZo-bBA,6512
133
133
  hdl/utils/llm/llama_chat.py,sha256=watcHGOaz-bv3x-yDucYlGk5f8FiqfFhwWogrl334fk,4387
134
- hdl/utils/llm/vis.py,sha256=z1NNh1Xni3l7c8i9VZLphEDGlfQSn8g5wIA7wudUgps,6516
134
+ hdl/utils/llm/vis.py,sha256=KVRM5iy5Br_ldr0H-QkppSHpPGjKlKzPHEamF7v94do,7609
135
135
  hdl/utils/schedulers/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
136
136
  hdl/utils/schedulers/norm_lr.py,sha256=bDwCmdEK-WkgxQMFBiMuchv8Mm7C0-GZJ6usm-PQk14,4461
137
137
  hdl/utils/weather/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
138
138
  hdl/utils/weather/weather.py,sha256=k11o6wM15kF8b9NMlEfrg68ak-SfSYLN3nOOflFUv-I,4381
139
- hjxdl-0.1.64.dist-info/METADATA,sha256=0Oy9QnqLsIOqprs7XFD3nG0CMopv4OIhbdcQaV19RX4,880
140
- hjxdl-0.1.64.dist-info/WHEEL,sha256=cVxcB9AmuTcXqmwrtPhNK88dr7IR_b6qagTj0UvIEbY,91
141
- hjxdl-0.1.64.dist-info/top_level.txt,sha256=-kxwTM5JPhylp06z3zAVO3w6_h7wtBfBo2zgM6YZoTk,4
142
- hjxdl-0.1.64.dist-info/RECORD,,
139
+ hjxdl-0.1.66.dist-info/METADATA,sha256=6iHh5yVtaR5Mw6WXZYT_iYuW6SYOxj0-JLuKeKS2ChU,903
140
+ hjxdl-0.1.66.dist-info/WHEEL,sha256=cVxcB9AmuTcXqmwrtPhNK88dr7IR_b6qagTj0UvIEbY,91
141
+ hjxdl-0.1.66.dist-info/top_level.txt,sha256=-kxwTM5JPhylp06z3zAVO3w6_h7wtBfBo2zgM6YZoTk,4
142
+ hjxdl-0.1.66.dist-info/RECORD,,
File without changes