Skip to content

Commit

Permalink
Merge pull request #3 from sinanerdinc/feature/log-management
Browse files Browse the repository at this point in the history
A logger file has been added for log management.
  • Loading branch information
cobanov authored Mar 13, 2024
2 parents dc0ca5c + 2c9bbc8 commit 0d69d97
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 44 deletions.
18 changes: 5 additions & 13 deletions tasnif/calculations.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,8 @@
import logging

import numpy as np
from img2vec_pytorch import Img2Vec
from rich.logging import RichHandler
from scipy.cluster.vq import kmeans2
from sklearn.decomposition import PCA

# Configure logging
LOG_FORMAT = "%(asctime)s - %(levelname)s - %(message)s"
logging.basicConfig(
level="INFO", format=LOG_FORMAT, datefmt="[%X]", handlers=[RichHandler()]
)
from .logger import info


def get_embeddings(use_gpu=False, images=None):
Expand All @@ -19,7 +11,7 @@ def get_embeddings(use_gpu=False, images=None):
image embeddings.
"""

logging.info(f"Img2Vec is running on {'GPU' if use_gpu else 'CPU'}...")
info(f"Img2Vec is running on {'GPU' if use_gpu else 'CPU'}...")
img2vec = Img2Vec(cuda=use_gpu)

embeddings = img2vec.get_vec(images, tensor=False)
Expand All @@ -44,7 +36,7 @@ def calculate_pca(embeddings, pca_dim):
n_samples, _ = embeddings.shape
if n_samples < pca_dim:
n_components = min(n_samples, pca_dim)
logging.info(
info(
f"Number of samples is less than the desired dimension. Setting n_components to {n_components}"
)

Expand All @@ -53,7 +45,7 @@ def calculate_pca(embeddings, pca_dim):

pca = PCA(n_components=n_components)
pca_embeddings = pca.fit_transform(embeddings.squeeze())
logging.info("PCA calculated.")
info("PCA calculated.")
return pca_embeddings


Expand All @@ -74,7 +66,7 @@ def calculate_kmeans(pca_embeddings, num_classes):
try:
centroid, labels = kmeans2(data=pca_embeddings, k=num_classes, minit="points")
counts = np.bincount(labels)
logging.info("KMeans calculated.")
info("KMeans calculated.")
return centroid, labels, counts

except Exception as e:
Expand Down
20 changes: 20 additions & 0 deletions tasnif/logger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import logging
from rich.logging import RichHandler

log_format = "%(asctime)s - %(levelname)s - %(message)s"
logging.basicConfig(
level="INFO", format=log_format, datefmt="[%X]", handlers=[RichHandler(show_time=False, show_level=False)]
)


def info(msg):
logging.info(msg)


def error(msg):
logging.error(msg)


if __name__ == '__main__':
info("info message")
error("error message")
26 changes: 6 additions & 20 deletions tasnif/tasnif.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,21 @@
import logging
import os
import shutil
import warnings
from itertools import compress
import numpy as np

from rich.logging import RichHandler
from tqdm import tqdm

from .calculations import calculate_kmeans, calculate_pca, get_embeddings
from .utils import (
create_dir,
create_image_grid,
read_images_from_directory,
read_with_pil,
)
from .logger import info, error

warnings.filterwarnings("ignore")


# Configure logging
LOG_FORMAT = "%(asctime)s - %(levelname)s - %(message)s"
logging.basicConfig(
level="INFO", format=LOG_FORMAT, datefmt="[%X]", handlers=[RichHandler()]
)


class Tasnif:
def __init__(self, num_classes, pca_dim=16, use_gpu=False):
self.num_classes = num_classes
Expand Down Expand Up @@ -93,16 +83,13 @@ def export(self, output_folder="./"):
try:
shutil.copy2(img_path, target_directory)
except Exception as e:
logging.error(
f"Error copying {img_path} to {target_directory}: {e}"
)
error(f"Error copying {img_path} to {target_directory}: {e}")

# Create an image grid for the current label
label_images = list(compress(self.images, label_mask))
create_image_grid(label_images, project_path, label_number)

logging.info(f"Exported images and grids to {project_path}")

info(f"Exported images and grids to {project_path}")

def export_embeddings(self, output_folder="./"):
"""
Expand All @@ -111,11 +98,10 @@ def export_embeddings(self, output_folder="./"):
Parameters:
- output_folder (str): The directory to export the embeddings into.
"""

if self.embeddings is None:
raise ValueError("Embeddings can not be empty. Please call the calculate method first.")



embeddings_path = os.path.join(output_folder, f"{self.project_name}_embeddings.npy")
np.save(embeddings_path, self.embeddings)
logging.info(f"Embeddings have been saved to {embeddings_path}")
info(f"Embeddings have been saved to {embeddings_path}")
14 changes: 3 additions & 11 deletions tasnif/utils.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
import glob
import logging
import os
from pathlib import Path

import matplotlib.pyplot as plt
from PIL import Image
from rich.logging import RichHandler
from tqdm import tqdm

# Configure logging
LOG_FORMAT = "%(asctime)s - %(levelname)s - %(message)s"
logging.basicConfig(
level="INFO", format=LOG_FORMAT, datefmt="[%X]", handlers=[RichHandler()]
)
from .logger import info


def read_images_from_directory(image_directory: str) -> list:
Expand All @@ -25,7 +17,7 @@ def read_images_from_directory(image_directory: str) -> list:
image_extensions = ("*.gif", "*.png", "*.jpg", "*.jpeg")
for ext in image_extensions:
list_of_images.extend(glob.glob(os.path.join(image_directory, ext)))
logging.info(f"Images found: {len(list_of_images)}")
info(f"Images found: {len(list_of_images)}")
return list_of_images


Expand All @@ -42,7 +34,7 @@ def read_with_pil(list_of_images: list, resize=True) -> list:
if resize:
img.thumbnail((512, 512))
pil_images.append(img)
logging.info("Image reading done!")
info("Image reading done!")
return pil_images


Expand Down

0 comments on commit 0d69d97

Please sign in to comment.