Update one.py
Browse files
one.py
CHANGED
@@ -1,50 +1,49 @@
|
|
1 |
import torch
|
2 |
-
import torch.nn as nn
|
3 |
-
import torchvision.models as models
|
4 |
import torchvision.transforms as transforms
|
5 |
-
import os
|
6 |
from PIL import Image
|
|
|
7 |
|
8 |
-
#
|
9 |
-
|
10 |
-
|
11 |
-
# Remove the classification head (the fully connected layer)
|
12 |
-
num_features = model.fc.in_features
|
13 |
-
model.fc = nn.Identity()
|
14 |
|
15 |
# Set the model to evaluation mode
|
16 |
model.eval()
|
17 |
|
18 |
-
#
|
|
|
|
|
|
|
19 |
preprocess = transforms.Compose([
|
20 |
transforms.Resize(256),
|
21 |
transforms.CenterCrop(224),
|
22 |
transforms.ToTensor(),
|
23 |
-
transforms.Normalize(
|
24 |
-
|
25 |
-
std=[0.229, 0.224, 0.225]
|
26 |
-
)
|
27 |
])
|
28 |
|
29 |
-
# Define
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
|
|
|
|
|
|
|
1 |
import torch
|
|
|
|
|
2 |
import torchvision.transforms as transforms
|
|
|
3 |
from PIL import Image
|
4 |
+
from transformers import AutoModel, AutoTokenizer
|
5 |
|
6 |
+
# Load the pre-trained ResNet50 model from Hugging Face
|
7 |
+
model_name = 'pytorch/vision:v0.9.0'
|
8 |
+
model = AutoModel.from_pretrained(model_name)
|
|
|
|
|
|
|
9 |
|
10 |
# Set the model to evaluation mode
|
11 |
model.eval()
|
12 |
|
13 |
+
# Load the tokenizer
|
14 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
15 |
+
|
16 |
+
# Define the preprocessing pipeline
|
17 |
preprocess = transforms.Compose([
|
18 |
transforms.Resize(256),
|
19 |
transforms.CenterCrop(224),
|
20 |
transforms.ToTensor(),
|
21 |
+
transforms.Normalize(mean=[0.485, 0.456, 0.406],
|
22 |
+
std=[0.229, 0.224, 0.225])
|
|
|
|
|
23 |
])
|
24 |
|
25 |
+
# Define a function to extract features from an image
|
26 |
+
def extract_features(image_path, model, tokenizer, preprocess):
|
27 |
+
# Load the image
|
28 |
+
image = Image.open(image_path).convert('RGB')
|
29 |
+
# Apply the preprocessing pipeline
|
30 |
+
image = preprocess(image)
|
31 |
+
# Add a batch dimension to the image tensor
|
32 |
+
image = image.unsqueeze(0)
|
33 |
+
# Encode the image using the tokenizer
|
34 |
+
inputs = tokenizer(image, padding=True, truncation=True, return_tensors='pt')
|
35 |
+
# Pass the inputs through the model to get the features
|
36 |
+
outputs = model(**inputs)
|
37 |
+
# Return the features
|
38 |
+
return outputs.last_hidden_state.squeeze().detach().numpy()
|
39 |
+
|
40 |
+
# Define a dictionary to store the features
|
41 |
+
features_dict = {}
|
42 |
+
|
43 |
+
# Loop over the images and extract the features
|
44 |
+
for image_name in image_names:
|
45 |
+
# Extract the features for this image
|
46 |
+
image_path = os.path.join(images_folder, image_name)
|
47 |
+
features = extract_features(image_path, model, tokenizer, preprocess)
|
48 |
+
# Add the features to the dictionary
|
49 |
+
features_dict[image_name] = features
|