Skip to content

Instantly share code, notes, and snippets.

@gauravbansal98
Created May 13, 2020 12:03
Show Gist options
  • Save gauravbansal98/0c583fc7adf7bdc971dd56bd031dae64 to your computer and use it in GitHub Desktop.
Save gauravbansal98/0c583fc7adf7bdc971dd56bd031dae64 to your computer and use it in GitHub Desktop.
# extract features from each photo in the directory
def extract_features(directory):
# load the model
model = Encoder()
# model.to(device)
model.eval()
# extract features from each photo
features = dict()
for i, name in enumerate(listdir(directory)):
# load an image from file
filename = directory + '/' + name
image = load_image(filename, size=224)
# convert the image pixels to a numpy array
image = transforms.ToTensor()(image)
# reshape data for the model
image = image.unsqueeze(0)
# prepare the image for the VGG model
image = normalize_batch(image)
# get features
feature = model(image)
# get image id
image_id = name.split('.')[0]
# store feature
features[image_id] = feature
# print('>%s' % name)
if i%50==0:
print("{} image done.".format(i))
return features
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment