import torch
import torchvision.models as models
import torchvision.transforms as transforms
from PIL import Image
resnet = models.resnet101(pretrained=True)
feature_extractor = torch.nn.Sequential(*list(resnet.children())[:-1])
feature_extractor.eval()
transform = 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])
])
img = Image.open(
"/home/a/Downloads/Laysan_Albat1_545.jpg").convert(
'RGB')
img_tensor = transform(img)
img_tensor = img_tensor.unsqueeze(0)
with torch.no_grad():
features = feature_extractor(img_tensor)
feature_vector = torch.flatten(features, start_dim=1)
print(feature_vector.shape)