File size: 1,422 Bytes
7f2b08c 8d7e7f9 7f2b08c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
import torch
import torch.nn as nn
import torchvision.models as models
import torchvision.transforms as transforms
import os
from PIL import Image
# Define the ResNet-50 model
model = models.resnet50(pretrained=True)
# Remove the classification head (the fully connected layer)
num_features = model.fc.in_features
model.fc = nn.Identity()
# Set the model to evaluation mode
model.eval()
# Define the preprocessing transforms
preprocess = transforms.Compose([
transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor(),
transforms.Normalize(
mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225]
)
])
# Define the dictionary to store the feature vectors
features = {}
# Iterate over the images and extract the features
image_dir = 'lfw'
for root, dirs, files in os.walk(image_dir):
for file in files:
# Load the image
image_path = os.path.join(root, file)
image = Image.open(image_path).convert('RGB')
# Apply the preprocessing transforms
input_tensor = preprocess(image)
input_batch = input_tensor.unsqueeze(0)
# Extract the features from the penultimate layer
with torch.no_grad():
features_tensor = model(input_batch)
features_vector = torch.squeeze(features_tensor).numpy()
# Store the feature vector in the dictionary
features[file] = features_vector |