Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from keras.applications.resnet50 import ResNet50, preprocess_input
- from keras.preprocessing.image import load_img, img_to_array
- import numpy as np
- from os import listdir
- from os.path import join
- import pandas as pd
- def featurize(fname, model, img_size):
- img = load_img(fname, target_size=img_size)
- raw_arr = img_to_array(img)
- raw_arr = np.expand_dims(raw_arr, axis=0)
- raw_arr = preprocess_input(raw_arr)
- features = model.predict(raw_arr).flatten()
- return features
- if __name__ == "__main__":
- data_dir = 'raw'
- fnames = listdir(data_dir)
- fpaths = (join(data_dir, f) for f in fnames)
- model = ResNet50(weights='imagenet', include_top=False)
- img_size = (224, 224) # image size expected by ResNet model
- x = np.vstack([featurize(fpath, model, img_size) for fpath in image_paths])
- y = np.array(['dog' in fname for fname in fnames])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement