-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbinaries.py
72 lines (48 loc) · 2.29 KB
/
binaries.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
from autoencoder.model import Sampling, VAE
import matplotlib.pyplot as plt
import tensorflow as tf
from PIL import Image
import numpy as np
import h5py
# use Pythagorean norm (maybe weighted features in future?)
def calculate_distance(features_1, features_2):
return np.linalg.norm(np.array(features_1) - np.array(features_2))
def load_training_dataset():
import h5py
global labels, images
with h5py.File('Galaxy10.h5', 'r') as F:
labels = np.array(F['ans'])
(milkywaylikes_idx,) = np.where(labels > 3)
images = np.array(F['images'][milkywaylikes_idx])
labels = np.array(F['ans'][milkywaylikes_idx]) - 4
images = images.astype(np.float32) / 255.
_, y, x, _ = images.shape
startx = x // 2 -(64 // 2)
starty = y // 2 -(64 // 2)
images = images[:, starty:starty + 64, startx:startx + 64, :]
def get_closest_neighbour(NN, reference_image):
if isinstance(reference_image, str):
reference_image = np.array(Image.open(reference_image))
reference_latent_position = NN.encode(np.array([reference_image]).reshape(1,64,64,3))[0][0]
elif isinstance(reference_image, np.ndarray):
reference_latent_position = NN.encode(np.array([reference_image]).reshape(1,64,64,3))[0][0]
try:
images == None
except NameError:
load_training_dataset()
predictions = np.array(NN.encoder.predict(images))
min_index, min_label = 0, 0
min_distance = np.inf
for i, prediction in enumerate(predictions[0, :, :]):
print(f"{i}/{len(images)} -> Minimal distance: {min_distance:.4f} for image {min_index} in cluster {min_label}", end = "\r")
assert len(reference_latent_position) == len(prediction), "Dimensionality check failed"
current_distance = calculate_distance(prediction, reference_latent_position)
if current_distance < min_distance:
min_prediction = prediction
min_distance = current_distance
min_index = i
print(f"\n Minimum distance = {min_distance} for image {min_index} found!")
return images[min_index]
if __name__ == "__main__":
Network = VAE("./trained_models/vae_encoder_epoch_24.h5", "./trained_models/vae_decoder_epoch_24.h5")
min_neighbour = get_closest_neighbour(Network, "./images/cropped/example_04.png")