Guest User

Untitled

a guest
Jul 18th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. from keras.applications.vgg16 import VGG16, preprocess_input
  2. from keras.preprocessing import image
  3. from hashlib import sha1
  4. from datasketch.weighted_minhash import WeightedMinHashGenerator
  5. from datasketch.lsh import MinHashLSH
  6.  
  7. import numpy as np
  8. import os
  9.  
  10.  
  11. VGG16_FEATURE_SHAPE = (1, 25088)
  12.  
  13.  
  14. def process_image(path):
  15. img = image.load_img(path, target_size=(224, 224))
  16. x = image.img_to_array(img)
  17. x = np.expand_dims(x, axis=0)
  18. return preprocess_input(x)
  19.  
  20.  
  21. def create_lsh_base(folder_path, model, hash_generator, lsh):
  22. for root_folder, _, files in os.walk(folder_path):
  23. for f in files:
  24. filename = f.split('.')[0]
  25.  
  26. x = process_image(root_folder+"/"+f)
  27. features = model.predict(x)
  28. lsh.insert(filename, mg.minhash(features.flatten()))
  29. return lsh
  30.  
  31.  
  32. if __name__ == "__main__":
  33. model = VGG16(include_top=False, weights="imagenet")
  34. mg = WeightedMinHashGenerator(VGG16_FEATURE_SHAPE[1], 2)
  35. lsh = MinHashLSH(threshold=0.5, num_perm=2)
  36.  
  37. lsh = create_lsh_base("images", model, mg, lsh)
Add Comment
Please, Sign In to add comment