Update svm_vgg_preprocessor.py
Browse files- svm_vgg_preprocessor.py +11 -20
svm_vgg_preprocessor.py
CHANGED
|
@@ -7,17 +7,14 @@ from PIL import Image
|
|
| 7 |
|
| 8 |
class FeatureExtractor:
|
| 9 |
def __init__(self):
|
| 10 |
-
# Load
|
| 11 |
self.vgg = vgg16(weights='DEFAULT')
|
| 12 |
self.vgg.eval()
|
| 13 |
|
| 14 |
-
#
|
| 15 |
-
self.
|
| 16 |
-
self.fc_features = torch.nn.Sequential(
|
| 17 |
-
*list(self.vgg.classifier.children())[:-2]
|
| 18 |
-
)
|
| 19 |
|
| 20 |
-
#
|
| 21 |
self.preprocess = transforms.Compose([
|
| 22 |
transforms.Resize((224, 224)),
|
| 23 |
transforms.ToTensor(),
|
|
@@ -28,33 +25,27 @@ class FeatureExtractor:
|
|
| 28 |
])
|
| 29 |
|
| 30 |
def extract_fc_cnn_features(self, image_path):
|
| 31 |
-
"""
|
| 32 |
-
# Load and preprocess image
|
| 33 |
img = Image.open(image_path).convert('RGB')
|
| 34 |
img_tensor = self.preprocess(img).unsqueeze(0)
|
| 35 |
|
| 36 |
with torch.no_grad():
|
| 37 |
-
|
| 38 |
-
conv_out = self.conv_features(img_tensor)
|
| 39 |
-
# Flatten for FC layers
|
| 40 |
-
flattened = torch.flatten(conv_out, 1)
|
| 41 |
-
# Get FC features
|
| 42 |
-
fc_features = self.fc_features(flattened)
|
| 43 |
|
| 44 |
-
return
|
| 45 |
|
| 46 |
def extract_fv_cnn_features(self, image_path):
|
| 47 |
-
"""FV-CNN feature extraction (
|
| 48 |
img = Image.open(image_path).convert('RGB')
|
| 49 |
img_tensor = self.preprocess(img).unsqueeze(0)
|
| 50 |
|
| 51 |
with torch.no_grad():
|
| 52 |
-
|
| 53 |
|
| 54 |
-
return
|
| 55 |
|
| 56 |
def extract_combined_features(self, image_path):
|
| 57 |
-
"""
|
| 58 |
fc = self.extract_fc_cnn_features(image_path)
|
| 59 |
fv = self.extract_fv_cnn_features(image_path)
|
| 60 |
return np.concatenate((fc, fv))
|
|
|
|
| 7 |
|
| 8 |
class FeatureExtractor:
|
| 9 |
def __init__(self):
|
| 10 |
+
# Load VGG16 with original architecture
|
| 11 |
self.vgg = vgg16(weights='DEFAULT')
|
| 12 |
self.vgg.eval()
|
| 13 |
|
| 14 |
+
# Use only convolutional features (matches original code)
|
| 15 |
+
self.conv_extractor = self.vgg.features
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
+
# Original preprocessing
|
| 18 |
self.preprocess = transforms.Compose([
|
| 19 |
transforms.Resize((224, 224)),
|
| 20 |
transforms.ToTensor(),
|
|
|
|
| 25 |
])
|
| 26 |
|
| 27 |
def extract_fc_cnn_features(self, image_path):
|
| 28 |
+
"""Matches original FC-CNN feature extraction (conv features)"""
|
|
|
|
| 29 |
img = Image.open(image_path).convert('RGB')
|
| 30 |
img_tensor = self.preprocess(img).unsqueeze(0)
|
| 31 |
|
| 32 |
with torch.no_grad():
|
| 33 |
+
features = self.conv_extractor(img_tensor)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
+
return features.squeeze().numpy().flatten()
|
| 36 |
|
| 37 |
def extract_fv_cnn_features(self, image_path):
|
| 38 |
+
"""Matches original FV-CNN feature extraction (conv features)"""
|
| 39 |
img = Image.open(image_path).convert('RGB')
|
| 40 |
img_tensor = self.preprocess(img).unsqueeze(0)
|
| 41 |
|
| 42 |
with torch.no_grad():
|
| 43 |
+
features = self.conv_extractor(img_tensor)
|
| 44 |
|
| 45 |
+
return features.squeeze().numpy().flatten()
|
| 46 |
|
| 47 |
def extract_combined_features(self, image_path):
|
| 48 |
+
"""Original concatenation of identical features"""
|
| 49 |
fc = self.extract_fc_cnn_features(image_path)
|
| 50 |
fv = self.extract_fv_cnn_features(image_path)
|
| 51 |
return np.concatenate((fc, fv))
|