one / one.py
dp92's picture
Update one.py
7f2b08c
raw
history blame
1.42 kB
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