hjxdl 0.1.61__py3-none-any.whl → 0.1.63__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.61'
16
- __version_tuple__ = version_tuple = (0, 1, 61)
15
+ __version__ = version = '0.1.63'
16
+ __version_tuple__ = version_tuple = (0, 1, 63)
hdl/utils/llm/vis.py CHANGED
@@ -66,6 +66,11 @@ class ImgHandler:
66
66
 
67
67
  @property
68
68
  def redis_conn(self):
69
+ """Establishes a connection to Redis server if not already connected.
70
+
71
+ Returns:
72
+ Redis connection object: A connection to the Redis server.
73
+ """
69
74
  if self._redis_conn is None:
70
75
  self._redis_conn = conn_redis(self.redis_host, self.redis_port)
71
76
  return self._redis_conn
@@ -76,14 +81,25 @@ class ImgHandler:
76
81
  to_numpy = False,
77
82
  **kwargs
78
83
  ):
79
- imgs = torch.stack([
80
- self.preprocess_val(Image.open(image)).to(self.device)
81
- for image in images
82
- ])
83
- img_features = self.model.encode_image(imgs, **kwargs)
84
- img_features /= img_features.norm(dim=-1, keepdim=True)
85
- if to_numpy:
86
- img_features = img_features.cpu().numpy()
84
+ """Get image features using a pretrained model.
85
+
86
+ Args:
87
+ images (list): List of image paths.
88
+ to_numpy (bool, optional): Whether to convert the image features to numpy array. Defaults to False.
89
+ **kwargs: Additional keyword arguments to pass to the model.
90
+
91
+ Returns:
92
+ torch.Tensor or numpy.ndarray: Image features extracted from the model.
93
+ """
94
+ with torch.no_grad(), torch.amp.autocast("cuda"):
95
+ imgs = torch.stack([
96
+ self.preprocess_val(Image.open(image)).to(self.device)
97
+ for image in images
98
+ ])
99
+ img_features = self.model.encode_image(imgs, **kwargs)
100
+ img_features /= img_features.norm(dim=-1, keepdim=True)
101
+ if to_numpy:
102
+ img_features = img_features.cpu().numpy()
87
103
  return img_features
88
104
 
89
105
  def get_text_features(
@@ -92,14 +108,28 @@ class ImgHandler:
92
108
  to_numpy = False,
93
109
  **kwargs
94
110
  ):
95
- txts = self.tokenizer(
96
- texts,
97
- context_length=self.model.context_length
98
- ).to(self.device)
99
- txt_features = self.model.encode_text(txts, **kwargs)
100
- txt_features /= txt_features.norm(dim=-1, keepdim=True)
101
- if to_numpy:
102
- txt_features = txt_features.cpu().numpy()
111
+ """Get text features from the input texts.
112
+
113
+ Args:
114
+ texts (list): List of input texts to extract features from.
115
+ to_numpy (bool, optional): Whether to convert the output features to a numpy array. Defaults to False.
116
+ **kwargs: Additional keyword arguments to pass to the model for encoding text.
117
+
118
+ Returns:
119
+ torch.Tensor or numpy.ndarray: Extracted text features.
120
+
121
+ Example:
122
+ get_text_features(["text1", "text2"], to_numpy=True)
123
+ """
124
+ with torch.no_grad(), torch.amp.autocast("cuda"):
125
+ txts = self.tokenizer(
126
+ texts,
127
+ context_length=self.model.context_length
128
+ ).to(self.device)
129
+ txt_features = self.model.encode_text(txts, **kwargs)
130
+ txt_features /= txt_features.norm(dim=-1, keepdim=True)
131
+ if to_numpy:
132
+ txt_features = txt_features.cpu().numpy()
103
133
  return txt_features
104
134
 
105
135
 
@@ -111,13 +141,54 @@ class ImgHandler:
111
141
  to_numpy: bool = False,
112
142
  **kwargs
113
143
  ):
114
- image_features = self.get_img_features(images, **kwargs)
115
- text_features = self.get_text_features(texts, **kwargs)
116
- text_probs = (100.0 * image_features @ text_features.T)
117
- if probs:
118
- text_probs = text_probs.softmax(dim=-1)
119
- if to_numpy:
120
- text_probs = text_probs.cpu().numpy()
144
+ """Get the probabilities of text-image associations.
145
+
146
+ Args:
147
+ texts (list): List of text inputs.
148
+ images (list): List of image inputs.
149
+ probs (bool, optional): Whether to return probabilities. Defaults to False.
150
+ to_numpy (bool, optional): Whether to convert the output to a numpy array. Defaults to False.
151
+ **kwargs: Additional keyword arguments for feature extraction.
152
+
153
+ Returns:
154
+ torch.Tensor or numpy.ndarray: Text-image association probabilities.
155
+ """
156
+ with torch.no_grad(), torch.amp.autocast("cuda"):
157
+ image_features = self.get_img_features(images, **kwargs)
158
+ text_features = self.get_text_features(texts, **kwargs)
159
+ text_probs = (100.0 * image_features @ text_features.T)
160
+ # >3 有关联
161
+ if probs:
162
+ text_probs = text_probs.softmax(dim=-1)
163
+ if to_numpy:
164
+ text_probs = text_probs.cpu().numpy()
121
165
  return text_probs
122
166
 
167
+ def get_pics_sims(
168
+ self,
169
+ images1,
170
+ images2,
171
+ to_numpy: bool = False,
172
+ **kwargs
173
+ ):
174
+ """Calculate similarity scores between two sets of images.
175
+
176
+ Args:
177
+ images1: First set of images.
178
+ images2: Second set of images.
179
+ to_numpy: Whether to convert the similarity scores to a numpy array (default is False).
180
+ **kwargs: Additional keyword arguments to pass to the get_img_features method.
181
+
182
+ Returns:
183
+ torch.Tensor or numpy.ndarray: Similarity scores between the two sets of images.
184
+ """
185
+ with torch.no_grad(), torch.amp.autocast("cuda"):
186
+ img1_feats = self.get_img_features(images1, **kwargs)
187
+ img2_feats = self.get_img_features(images2, **kwargs)
188
+ sims = img1_feats @ img2_feats.T
189
+ if to_numpy:
190
+ sims = sims.cpu().numpy()
191
+ # > 0.9 很相似
192
+ return sims
193
+
123
194
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: hjxdl
3
- Version: 0.1.61
3
+ Version: 0.1.63
4
4
  Summary: A collection of functions for Jupyter notebooks
5
5
  Home-page: https://github.com/huluxiaohuowa/hdl
6
6
  Author: Jianxing Hu
@@ -1,5 +1,5 @@
1
1
  hdl/__init__.py,sha256=GffnD0jLJdhkd-vo989v40N90sQbofkayRBwxc6TVhQ,72
2
- hdl/_version.py,sha256=iuq68v789XjbJHhRQlyaD2e2yTTa7FKtdYjoKlGAJmY,413
2
+ hdl/_version.py,sha256=mjLtl9xPbny0ZTEF6CvbbjoK1aXIg4Io5ZPI-Nfqo_k,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=zVgy3MepIEKk9oeXuADjloBaWjlshjZB6g2uHbtcPEg,3471
134
+ hdl/utils/llm/vis.py,sha256=6CpV9lXvHw5TRSsu2XKLDNYJIP3QsKYEDVY7pKx9r1A,6439
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.61.dist-info/METADATA,sha256=Ud4NYOjhC49-QV63fDwSy0AltmVxPtFz_HcEr5qi9Bs,880
140
- hjxdl-0.1.61.dist-info/WHEEL,sha256=cVxcB9AmuTcXqmwrtPhNK88dr7IR_b6qagTj0UvIEbY,91
141
- hjxdl-0.1.61.dist-info/top_level.txt,sha256=-kxwTM5JPhylp06z3zAVO3w6_h7wtBfBo2zgM6YZoTk,4
142
- hjxdl-0.1.61.dist-info/RECORD,,
139
+ hjxdl-0.1.63.dist-info/METADATA,sha256=LzK9HNry28LVlCSbab50Gp1hoIw-vQFDuCzwCO1Zyjk,880
140
+ hjxdl-0.1.63.dist-info/WHEEL,sha256=cVxcB9AmuTcXqmwrtPhNK88dr7IR_b6qagTj0UvIEbY,91
141
+ hjxdl-0.1.63.dist-info/top_level.txt,sha256=-kxwTM5JPhylp06z3zAVO3w6_h7wtBfBo2zgM6YZoTk,4
142
+ hjxdl-0.1.63.dist-info/RECORD,,
File without changes