Skip to content

Commit 6af76fb

Browse files
authored
Merge pull request #1 from yunusstalha/main
Pre-calculation Checks and Embedding Exports
2 parents e7fdafc + 03930c5 commit 6af76fb

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

tasnif/tasnif.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import shutil
44
import warnings
55
from itertools import compress
6+
import numpy as np
67

78
from rich.logging import RichHandler
89
from tqdm import tqdm
@@ -54,9 +55,12 @@ def read(self, folder_path):
5455
def calculate(self):
5556
"""
5657
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.
5859
"""
5960

61+
if not self.images:
62+
raise ValueError("The images list can not be empty. Please call the read method before calculating.")
63+
6064
self.embeddings = get_embeddings(use_gpu=self.use_gpu, images=self.images)
6165
self.pca_embeddings = calculate_pca(self.embeddings, self.pca_dim)
6266
self.centroid, self.labels, self.counts = calculate_kmeans(
@@ -98,3 +102,20 @@ def export(self, output_folder="./"):
98102
create_image_grid(label_images, project_path, label_number)
99103

100104
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

Comments
 (0)