|
| 1 | +import tensorflow_datasets as tfds |
| 2 | + |
| 3 | +import numpy as np |
| 4 | +import matplotlib.pyplot as plt |
| 5 | + |
| 6 | +import tensorflow as tf |
| 7 | +from tensorflow import keras |
| 8 | +from keras import layers |
| 9 | + |
| 10 | +import functools |
| 11 | + |
| 12 | +from sklearn.neighbors import NearestNeighbors # similarity |
| 13 | + |
| 14 | +import ipywidgets as w # interface |
| 15 | + |
| 16 | + |
| 17 | +ds = tfds.load("imagenette/160px") |
| 18 | + |
| 19 | + |
| 20 | +def extract_image(example): |
| 21 | + image = example['image'] |
| 22 | + return image |
| 23 | + |
| 24 | +def preprocess_image(image, height, width): |
| 25 | + image = tf.image.resize(image, (height, width)) |
| 26 | + image = tf.cast(image, tf.float32) / 255.0 |
| 27 | + return image |
| 28 | + |
| 29 | +def get_image_batches(batch_size = 128, height = 256, width = 256): |
| 30 | + partial_preprocess_image = functools.partial(preprocess_image, height=height, width=width) |
| 31 | + train_ds = ds['train'] |
| 32 | + train_ds = ( train_ds.map(extract_image) |
| 33 | + .map(partial_preprocess_image) |
| 34 | + .cache() |
| 35 | + .shuffle(buffer_size=1000) |
| 36 | + .batch(batch_size) |
| 37 | + .prefetch(tf.data.AUTOTUNE) |
| 38 | + ) |
| 39 | + return train_ds |
| 40 | + |
| 41 | + |
| 42 | +BATCH_SIZE = 32 |
| 43 | +IMG_WIDTH = IMG_HEIGHT = 32 |
| 44 | +train_ds = get_image_batches(batch_size = BATCH_SIZE, height = IMG_HEIGHT, width = IMG_WIDTH) |
| 45 | + |
| 46 | + |
| 47 | +images = np.array([img for batch in train_ds.take(300) for img in batch]) |
| 48 | +print(images.shape) |
| 49 | + |
| 50 | + |
| 51 | +encoder = keras.models.load_model("saved_models/encoder.keras") |
| 52 | +encoder.summary() |
| 53 | + |
| 54 | + |
| 55 | +features = encoder.predict(images, batch_size = BATCH_SIZE) |
| 56 | + |
| 57 | +knn = NearestNeighbors(n_neighbors=5, metric="cosine") |
| 58 | +knn.fit(features) |
| 59 | + |
| 60 | + |
| 61 | +def show_similar_images(start_image_idx, n_neighbors=10): |
| 62 | + input_image = images[start_image_idx] |
| 63 | + input_image = np.expand_dims(input_image, 0) |
| 64 | + fig, axes = plt.subplots(1, n_neighbors+1, figsize=(n_neighbors * 2, 5)) |
| 65 | + |
| 66 | + feature = encoder.predict(input_image) |
| 67 | + distances, nbors = knn.kneighbors(feature, n_neighbors=n_neighbors) |
| 68 | + distances, nbors = distances[0], nbors[0] |
| 69 | + nbor_images = [images[i] for i in nbors] |
| 70 | + |
| 71 | + for i in range(len(nbor_images)+1): |
| 72 | + ax = axes[i] |
| 73 | + ax.set_axis_off() |
| 74 | + if i == 0: |
| 75 | + ax.imshow(input_image.squeeze(0)) |
| 76 | + ax.set_title("Input") |
| 77 | + else: |
| 78 | + ax.imshow(nbor_images[i-1]) |
| 79 | + ax.set_title(f"Sim: {1 - distances[i-1]:.2f}") |
| 80 | + plt.show() |
| 81 | + |
| 82 | +w.interact(show_similar_images, |
| 83 | + start_image_idx=w.IntSlider(max=len(images)-1, continuous_update=False), |
| 84 | + n_neighbors=w.IntSlider(min=2, value=5, max=10, continuous_update=False), |
| 85 | +) |
0 commit comments