3
3
import shutil
4
4
import warnings
5
5
from itertools import compress
6
+ import numpy as np
6
7
7
8
from rich .logging import RichHandler
8
9
from tqdm import tqdm
@@ -54,9 +55,12 @@ def read(self, folder_path):
54
55
def calculate (self ):
55
56
"""
56
57
The function calculates embeddings, performs PCA, and applies K-means clustering to the
57
- embeddings.
58
+ embeddings. It will not perform these operations if no images have been read.
58
59
"""
59
60
61
+ if not self .images :
62
+ raise ValueError ("The images list can not be empty. Please call the read method before calculating." )
63
+
60
64
self .embeddings = get_embeddings (use_gpu = self .use_gpu , images = self .images )
61
65
self .pca_embeddings = calculate_pca (self .embeddings , self .pca_dim )
62
66
self .centroid , self .labels , self .counts = calculate_kmeans (
@@ -98,3 +102,20 @@ def export(self, output_folder="./"):
98
102
create_image_grid (label_images , project_path , label_number )
99
103
100
104
logging .info (f"Exported images and grids to { project_path } " )
105
+
106
+
107
+ def export_embeddings (self , output_folder = "./" ):
108
+ """
109
+ Export the calculated embeddings to a specified output folder.
110
+
111
+ Parameters:
112
+ - output_folder (str): The directory to export the embeddings into.
113
+ """
114
+
115
+ if self .embeddings is None :
116
+ raise ValueError ("Embeddings can not be empty. Please call the calculate method first." )
117
+
118
+
119
+ embeddings_path = os .path .join (output_folder , f"{ self .project_name } _embeddings.npy" )
120
+ np .save (embeddings_path , self .embeddings )
121
+ logging .info (f"Embeddings have been saved to { embeddings_path } " )
0 commit comments