Spaces:
Sleeping
Sleeping
Update clip_model.py
Browse files- clip_model.py +18 -0
clip_model.py
CHANGED
|
@@ -20,11 +20,27 @@ class ClipModel:
|
|
| 20 |
self.processor = ChineseCLIPProcessor.from_pretrained(model_name)
|
| 21 |
|
| 22 |
print("***** Clip Model LOAD DONE *****")
|
|
|
|
| 23 |
|
| 24 |
# Load Chinese vocabulary
|
| 25 |
with open(vocab_path, 'r', encoding='utf-8') as f:
|
| 26 |
self.vocab = [line.strip() for line in f.readlines()]
|
| 27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
def clip_result(self, image_path, top_k=3):
|
| 29 |
"""
|
| 30 |
給定圖片路徑,返回最接近的 top_k 詞彙
|
|
@@ -33,6 +49,8 @@ class ClipModel:
|
|
| 33 |
image = Image.open(image_path)
|
| 34 |
|
| 35 |
print(f"===== Clip Model_clip_result : {image_path} ===== ")
|
|
|
|
|
|
|
| 36 |
|
| 37 |
# Process images and texts
|
| 38 |
batch_size = 16 # Process 16 vocab at a time
|
|
|
|
| 20 |
self.processor = ChineseCLIPProcessor.from_pretrained(model_name)
|
| 21 |
|
| 22 |
print("***** Clip Model LOAD DONE *****")
|
| 23 |
+
check_memory_usage()
|
| 24 |
|
| 25 |
# Load Chinese vocabulary
|
| 26 |
with open(vocab_path, 'r', encoding='utf-8') as f:
|
| 27 |
self.vocab = [line.strip() for line in f.readlines()]
|
| 28 |
|
| 29 |
+
def check_memory_usage():
|
| 30 |
+
# Get memory details
|
| 31 |
+
memory_info = psutil.virtual_memory()
|
| 32 |
+
|
| 33 |
+
total_memory = memory_info.total / (1024 * 1024) # Convert bytes to MB
|
| 34 |
+
available_memory = memory_info.available / (1024 * 1024)
|
| 35 |
+
used_memory = memory_info.used / (1024 * 1024)
|
| 36 |
+
memory_usage_percent = memory_info.percent
|
| 37 |
+
|
| 38 |
+
print(f"^^^^^^ Total Memory: {total_memory:.2f} MB ^^^^^^")
|
| 39 |
+
print(f"^^^^^^ Available Memory: {available_memory:.2f} MB ^^^^^^")
|
| 40 |
+
print(f"^^^^^^ Used Memory: {used_memory:.2f} MB ^^^^^^")
|
| 41 |
+
print(f"^^^^^^ Memory Usage (%): {memory_usage_percent}% ^^^^^^")
|
| 42 |
+
|
| 43 |
+
|
| 44 |
def clip_result(self, image_path, top_k=3):
|
| 45 |
"""
|
| 46 |
給定圖片路徑,返回最接近的 top_k 詞彙
|
|
|
|
| 49 |
image = Image.open(image_path)
|
| 50 |
|
| 51 |
print(f"===== Clip Model_clip_result : {image_path} ===== ")
|
| 52 |
+
# Run the function
|
| 53 |
+
check_memory_usage()
|
| 54 |
|
| 55 |
# Process images and texts
|
| 56 |
batch_size = 16 # Process 16 vocab at a time
|