Advertisement
Guest User

Untitled

a guest
Apr 28th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.86 KB | None | 0 0
  1. from keras.applications.resnet50 import ResNet50, preprocess_input
  2. from keras.preprocessing.image import load_img, img_to_array
  3. import numpy as np
  4. from os import listdir
  5. from os.path import join
  6. import pandas as pd
  7.  
  8.  
  9. def featurize(fname, model, img_size):
  10. img = load_img(fname, target_size=img_size)
  11. raw_arr = img_to_array(img)
  12. raw_arr = np.expand_dims(raw_arr, axis=0)
  13. raw_arr = preprocess_input(raw_arr)
  14. features = model.predict(raw_arr).flatten()
  15. return features
  16.  
  17. if __name__ == "__main__":
  18. data_dir = 'raw'
  19. fnames = listdir(data_dir)
  20. fpaths = (join(data_dir, f) for f in fnames)
  21.  
  22. model = ResNet50(weights='imagenet', include_top=False)
  23. img_size = (224, 224) # image size expected by ResNet model
  24. x = np.vstack([featurize(fpath, model, img_size) for fpath in image_paths])
  25. y = np.array(['dog' in fname for fname in fnames])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement