Update README.md
Browse files
README.md
CHANGED
|
@@ -16,4 +16,55 @@ The steps to create the embeddings can be described as:
|
|
| 16 |
1. Resize the images to 512x512.
|
| 17 |
2. Transform the images into their Fourier image.
|
| 18 |
3. Input the images into the model using predict.
|
| 19 |
-
4. The output will be a 128-length vector for use in classification models.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
1. Resize the images to 512x512.
|
| 17 |
2. Transform the images into their Fourier image.
|
| 18 |
3. Input the images into the model using predict.
|
| 19 |
+
4. The output will be a 128-length vector for use in classification models.
|
| 20 |
+
|
| 21 |
+
The preprocessing code along with the predict can calculate the embeddings for classification.
|
| 22 |
+
|
| 23 |
+
```
|
| 24 |
+
|
| 25 |
+
# load an image and apply the fourier transform
|
| 26 |
+
|
| 27 |
+
import numpy as np
|
| 28 |
+
from PIL import Image
|
| 29 |
+
from scipy.fftpack import fft2
|
| 30 |
+
from tensorflow.keras.models import load_model, Model
|
| 31 |
+
|
| 32 |
+
# Function to apply Fourier transform
|
| 33 |
+
def apply_fourier_transform(image):
|
| 34 |
+
image = np.array(image)
|
| 35 |
+
fft_image = fft2(image)
|
| 36 |
+
return np.abs(fft_image)
|
| 37 |
+
|
| 38 |
+
# Function to preprocess image
|
| 39 |
+
def preprocess_image(image_path):
|
| 40 |
+
try:
|
| 41 |
+
image = Image.open(image_path).convert('L')
|
| 42 |
+
image = image.resize((512, 512))
|
| 43 |
+
image = apply_fourier_transform(image)
|
| 44 |
+
image = np.expand_dims(image, axis=-1) # Expand dimensions to match model input shape
|
| 45 |
+
image = np.expand_dims(image, axis=0) # Expand to add batch dimension
|
| 46 |
+
return image
|
| 47 |
+
except Exception as e:
|
| 48 |
+
print(f"Error processing image {image_path}: {e}")
|
| 49 |
+
return None
|
| 50 |
+
|
| 51 |
+
# Function to load embedding model and calculate embeddings
|
| 52 |
+
def calculate_embeddings(image_path, model_path='embedding_model.keras'):
|
| 53 |
+
# Load the trained model
|
| 54 |
+
model = load_model(model_path)
|
| 55 |
+
|
| 56 |
+
# Remove the final classification layer to get embeddings
|
| 57 |
+
embedding_model = Model(inputs=model.input, outputs=model.output)
|
| 58 |
+
|
| 59 |
+
# Preprocess the image
|
| 60 |
+
preprocessed_image = preprocess_image(image_path)
|
| 61 |
+
|
| 62 |
+
# Calculate embeddings
|
| 63 |
+
embeddings = embedding_model.predict(preprocessed_image)
|
| 64 |
+
|
| 65 |
+
return embeddings
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
calculate_embeddings('filename.jpg')
|
| 70 |
+
```
|