hjxdl 0.1.62__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 +2 -2
- hdl/utils/llm/vis.py +71 -3
- {hjxdl-0.1.62.dist-info → hjxdl-0.1.63.dist-info}/METADATA +1 -1
- {hjxdl-0.1.62.dist-info → hjxdl-0.1.63.dist-info}/RECORD +6 -6
- {hjxdl-0.1.62.dist-info → hjxdl-0.1.63.dist-info}/WHEEL +0 -0
- {hjxdl-0.1.62.dist-info → hjxdl-0.1.63.dist-info}/top_level.txt +0 -0
hdl/_version.py
CHANGED
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,7 +81,17 @@ class ImgHandler:
|
|
76
81
|
to_numpy = False,
|
77
82
|
**kwargs
|
78
83
|
):
|
79
|
-
|
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"):
|
80
95
|
imgs = torch.stack([
|
81
96
|
self.preprocess_val(Image.open(image)).to(self.device)
|
82
97
|
for image in images
|
@@ -93,7 +108,20 @@ class ImgHandler:
|
|
93
108
|
to_numpy = False,
|
94
109
|
**kwargs
|
95
110
|
):
|
96
|
-
|
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"):
|
97
125
|
txts = self.tokenizer(
|
98
126
|
texts,
|
99
127
|
context_length=self.model.context_length
|
@@ -113,14 +141,54 @@ class ImgHandler:
|
|
113
141
|
to_numpy: bool = False,
|
114
142
|
**kwargs
|
115
143
|
):
|
116
|
-
|
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"):
|
117
157
|
image_features = self.get_img_features(images, **kwargs)
|
118
158
|
text_features = self.get_text_features(texts, **kwargs)
|
119
159
|
text_probs = (100.0 * image_features @ text_features.T)
|
160
|
+
# >3 有关联
|
120
161
|
if probs:
|
121
162
|
text_probs = text_probs.softmax(dim=-1)
|
122
163
|
if to_numpy:
|
123
164
|
text_probs = text_probs.cpu().numpy()
|
124
165
|
return text_probs
|
125
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
|
+
|
126
194
|
|
@@ -1,5 +1,5 @@
|
|
1
1
|
hdl/__init__.py,sha256=GffnD0jLJdhkd-vo989v40N90sQbofkayRBwxc6TVhQ,72
|
2
|
-
hdl/_version.py,sha256=
|
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=
|
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.
|
140
|
-
hjxdl-0.1.
|
141
|
-
hjxdl-0.1.
|
142
|
-
hjxdl-0.1.
|
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
|
File without changes
|