forked from LC1332/Suzumiya-Diffusion-Learning
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCLIPExtractor.py
More file actions
156 lines (114 loc) · 4.79 KB
/
Copy pathCLIPExtractor.py
File metadata and controls
156 lines (114 loc) · 4.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
from transformers import CLIPProcessor, CLIPModel
from PIL import Image
import torch
from tqdm import tqdm
import numpy as np
import matplotlib.pyplot as plt
from config import MODEL_PATH
class CLIPExtractor:
def __init__(self, model_name=MODEL_PATH, processor_name=MODEL_PATH):
# Initialize the model and processor with default or specified values
self.model = CLIPModel.from_pretrained(model_name)
self.processor = CLIPProcessor.from_pretrained(processor_name)
self.device = "cuda" if torch.cuda.is_available() else "cpu"
self.model.to(self.device)
def extract(self, file_names, batch_size=16):
num_images = len(file_names)
all_features = []
for start_idx in tqdm(range(0, num_images, batch_size)):
batch_files = file_names[start_idx:start_idx + batch_size]
images = [Image.open(file_name).convert("RGB") for file_name in batch_files]
inputs = self.processor(images=images, return_tensors="pt", padding=True).to(self.device)
with torch.no_grad():
outputs = self.model.get_image_features(**inputs)
all_features.extend(outputs.cpu().numpy())
return all_features
def visualize(self, features, save_name):
features_np = np.array(features)
plt.figure(figsize=(10, 8))
plt.imshow(features_np, aspect='auto', cmap='viridis')
plt.colorbar()
plt.title('Feature Heatmap')
plt.xlabel('Feature Index')
plt.ylabel('Image Index')
plt.savefig("save_namenump")
plt.close()
if __name__ == "__main__":
extractor = CLIPExtractor()
import os
# 遍历解压后的文件夹,获取所有.jpg文件名
jpg_files = []
folder_path = "./output/temp_crop"
for root, dirs, files in os.walk(folder_path):
for file in files:
if file.endswith(".jpg"):
jpg_files.append(os.path.join(root, file))
file_names = jpg_files
features = extractor.extract(file_names)
extractor.visualize(features, 'output/feature_heatmap.png')
# Example usage:
# extractor = CLIPExtractor()
# file_names = ['path/to/your/image1.jpg', 'path/to/your/image2.jpg'] # Replace these paths with your actual file paths
# features = extractor.extract(file_names)
# extractor.visualize(features, 'heatmap.png')
# 我需要把下面代码重构成 CLIPExtractor类
# 已知下面这段代码
# ```python
# from transformers import CLIPProcessor, CLIPModel
# from PIL import Image
# import torch
# from tqdm import tqdm
# # Define the batch size
# batch_size = 16
# model_name = "Green-Sky/FaRL-Base-Patch16-LAIONFace20M-ep64"
# processor_name = "openai/clip-vit-base-patch16"
# # Initialize the model and processor
# model = CLIPModel.from_pretrained(model_name)
# processor = CLIPProcessor.from_pretrained(processor_name)
# # model = CLIPModel.from_pretrained("h94/IP-Adapter")
# # processor = CLIPProcessor.from_pretrained("h94/IP-Adapter")
# device = "cuda" if torch.cuda.is_available() else "cpu"
# model.to(device)
# # Function to process images in batches and extract features
# def batch_process_images(file_names, batch_size, model, processor):
# num_images = len(file_names)
# all_features = []
# for start_idx in tqdm(range(0, num_images, batch_size)):
# batch_files = file_names[start_idx:start_idx + batch_size]
# images = [Image.open(file_name).convert("RGB") for file_name in batch_files]
# inputs = processor(images=images, return_tensors="pt", padding=True).to(device)
# with torch.no_grad():
# outputs = model.get_image_features(**inputs)
# all_features.extend(outputs.cpu().numpy())
# return all_features
# # Example usage
# batch_size = 16
# file_names = jpg_files
# features = batch_process_images(file_names, batch_size, model, processor)
# ```
# 能够正常运行
# 以及可视化代码
# ```python
# import numpy as np
# import matplotlib.pyplot as plt
# # 假设`features`是一个二维数组,其中每一行是一个图像的特征向量
# # 例如: features = np.random.rand(5, 2048) # 使用随机数据作为示例
# # 将features转换为NumPy数组,以便更容易地处理
# features_np = np.array(features)
# # 创建热图
# plt.figure(figsize=(10, 8))
# plt.imshow(features_np, aspect='auto', cmap='viridis')
# plt.colorbar()
# plt.title('Feature Heatmap')
# plt.xlabel('Feature Index')
# plt.ylabel('Image Index')
# plt.show()
# ```
# 我希望实现一个CLIPExtractor类
# 这个类可以默认初始化(无参数)
# extractor = CLIPExtractor()
# 也可以指定model_name和process_name进行初始化
# 然后使用features = extractor.extract( file_names )
# 进行特征的抽取
# 以及一个visualize函数extractor.visualize( features, save_name )
# 可以将可视化的热图保存到save_name中