Skip to content
This repository has been archived by the owner on Apr 20, 2020. It is now read-only.

Commit

Permalink
SCRATCH - image KNN with Xception network
Browse files Browse the repository at this point in the history
  • Loading branch information
alexklibisz committed Apr 22, 2018
1 parent c0369c2 commit 917c824
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 80 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
.ipynb_checkpoints
91 changes: 50 additions & 41 deletions scratch/es-lsh-images/get_imagenet_vectors_labels.py
@@ -1,5 +1,5 @@
from glob import glob
from keras.applications import MobileNet
from keras.applications import Xception
from keras.applications.imagenet_utils import preprocess_input, decode_predictions
from keras.models import Model
from scipy.misc import imread, imresize, imshow
Expand All @@ -8,25 +8,9 @@
import pdb


imgs_dir = "/home/alex/Downloads/ILSVRC/Data/DET/test/"
vecs_path = 'imagenet_vectors.npy'
labels_path = 'imagenet_labels.txt'

img_paths = sorted(glob('%s/*.JPEG' % imgs_dir))
fp_labels = open(labels_path, 'w')
def get_img(img_path, dim=224):

batch_size = 500
imgs_batch = np.zeros((batch_size, 224, 224, 3))

# Instantiate model and chop off some layers.
vector_layer = "conv_preds"
model = MobileNet()
model2 = Model(inputs=model.input, outputs=model.get_layer(vector_layer).output)
model.summary()

vecs = np.zeros((len(img_paths), model.output_shape[-1]))

for i, img_path in enumerate(img_paths):
assert dim % 2 == 0

img = imread(img_path)

Expand All @@ -38,40 +22,65 @@
tmp[:, :, 2] = img
img = tmp

# Resize and crop to fit network.
# Resize image.
h0, w0, d = img.shape
h1, w1 = h0, w0
if h0 < w0:
h1 = 224
w1 = int(224 * w0 / h0)
h1 = dim
w1 = int(dim * w0 / h0)
else:
w1 = 224
h1 = int(224 * h0 / w0)
w1 = dim
h1 = int(dim * h0 / w0)
assert abs((h0 / w0) - (h1 / w1)) <= 0.1, "%d %d %d %d" % (h0, w0, h1, w1)

img = imresize(img, (h1, w1, d))

# imgs_batch = img[np.newaxis, :224, :224, :].astype(np.float32)
# imgs_batch = preprocess_input(imgs_batch, mode='tf')
# prds_batch = model.predict(imgs_batch)
# print(decode_predictions(prds_batch))
# imshow(img)
# Crop image at the center.
# Width > height.
if w1 > h1:
c = int(w1 / 2)
o = int(dim / 2)
img = img[:, c - o: c + o, :]
# Height > width.
elif h1 > w1:
c = int(h1 / 2)
o = int(dim / 2)
img = img[c - o: c + o, :, :]

assert img.shape == (dim, dim, 3), '%s, %s' % (img_path, img.shape)

return img


imgs_dir = "/home/alex/Downloads/ILSVRC/Data/DET/test/"
vecs_path = 'imagenet_vectors.npy'

img_paths = sorted(glob('%s/*.JPEG' % imgs_dir))
fp_paths = open('imagenet_paths.txt', 'w')

dim = 224
batch_size = 500
imgs_batch = np.zeros((batch_size, dim, dim, 3))

# Instantiate model and chop off some layers.
vector_layer = "avg_pool"
m1 = Xception()
m2 = Model(inputs=m1.input, outputs=m1.get_layer(vector_layer).output)

imgs_batch[i % batch_size] = img[:224, :224, :]
vecs = np.zeros((len(img_paths), m2.output_shape[-1]))

if (i % batch_size == 0 and i > 0):
imgs_batch = preprocess_input(imgs_batch, mode='tf')
for i in range(0, len(img_paths), batch_size):

labels = decode_predictions(model.predict(imgs_batch))
s = ""
for img_path, line in zip(img_paths[i - batch_size:i], labels):
s += img_path.split('/')[-1] + ' '
s += ','.join(map(lambda x: x[1], line)) + '\n'
fp_labels.write(s)
for j in range(batch_size):
imgs_batch[j] = get_img(img_paths[i + j], dim)

prd = model2.predict(imgs_batch)
vecs[i - batch_size:i] = prd.reshape(prd.shape[0], prd.shape[-1])
print('%d %.3lf %.3lf %.3lf' % (i, prd.min(), prd.mean(), prd.max()))
imgs_batch = preprocess_input(imgs_batch, mode='tf')
prds_batch = m2.predict(imgs_batch)
vecs[i:i + batch_size] = prds_batch
fp_paths.write('\n'.join(img_paths[i:i + batch_size]) + '\n')

print('%d-%d %.3lf %.3lf %.3lf' % (
i, i + batch_size, prds_batch.min(),
np.median(prds_batch), prds_batch.max()))

np.save(vecs_path, vecs)
79 changes: 40 additions & 39 deletions scratch/es-lsh-images/imagenet_knn_exact.ipynb

Large diffs are not rendered by default.

0 comments on commit 917c824

Please sign in to comment.