Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/dimension reduction #3

Merged
merged 2 commits into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/monocheck/dimension_reduction.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import numpy as np

from sklearn.decomposition import PCA

def reduce_dimension(images_feature: np.ndarray, components:int = 100):
pca = PCA(n_components=2, random_state=22)
pca.fit(images_feature)
reduced_images_feature = pca.transform(images_feature)

return reduced_images_feature

7 changes: 2 additions & 5 deletions src/monocheck/feature_extraction.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@
from keras.applications.vgg16 import VGG16


def extract_features(images_input: List[np.ndarray], model:VGG16):
images_features = []
for image_input in images_input:
feature = model.predict(image_input)
images_features.append(feature)
def extract_features(images_input: np.ndarray, model:VGG16):
images_features = model.predict(images_input)

return images_features

Expand Down
18 changes: 10 additions & 8 deletions src/monocheck/pipeline.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import numpy as np
from pathlib import Path
from typing import List

Expand All @@ -6,23 +7,24 @@

from monocheck.prepare import load_image
from monocheck.feature_extraction import extract_features

from monocheck.dimension_reduction import reduce_dimension

def pipeline(image_paths:List[Path]):
imgs_array = [load_image(image_path) for image_path in image_paths]
imgs_array = [load_image(image_path).squeeze(0) for image_path in image_paths]
imgs_array = np.stack(imgs_array, axis=0)
model = VGG16()
model = Model(inputs = model.inputs, outputs = model.layers[-2].output)

imgs_features = extract_features(imgs_array, model)
return imgs_features

imgs_features = imgs_features.reshape(-1,4096)
reduced_imgs_features = reduce_dimension(imgs_features)
return reduced_imgs_features

if __name__ == "__main__":
image_path = Path("image.jpg")
imgs_path = [image_path]
imgs_path = [Path("image.jpg"), Path("image2.jpg")]
imgs_feat = pipeline(imgs_path)
print(imgs_feat)

imgs_features = pipeline(imgs_path)
print(imgs_features)



13 changes: 8 additions & 5 deletions tests/feature_extraction/test_extract_features.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
import numpy as np
from pathlib import Path

from keras.applications.vgg16 import VGG16
from keras.models import Model

from monocheck.feature_extraction import extract_features


def generate_random_image():
""" Generate a random array with shape (1, 224, 224, 3) """
random_image = np.random.rand(1, 224, 224, 3)
return random_image

def test_extract_features():
"""5 random images array"""
imgs_array = [generate_random_image() for _ in range(5)]
num_of_imgs = 5
imgs_array = [generate_random_image().squeeze(0) for _ in range(num_of_imgs)]
imgs_array = np.stack(imgs_array, axis=0)

model = VGG16()
model = Model(inputs = model.inputs, outputs = model.layers[-2].output)

images_feature = extract_features(imgs_array, model)

for image_feature in images_feature:
assert isinstance(image_feature, np.ndarray)
assert image_feature.shape == (1, 4096)

assert isinstance(images_feature, np.ndarray)
assert images_feature.shape == (num_of_imgs, 4096)

Loading