File size: 1,794 Bytes
8322740
1ba3e62
 
 
 
 
 
 
 
fcf6aaa
8322740
 
1ba3e62
fcf6aaa
 
1ba3e62
fcf6aaa
1ba3e62
 
 
 
 
 
 
 
 
 
fcf6aaa
8322740
 
1ba3e62
 
fcf6aaa
1ba3e62
fcf6aaa
1ba3e62
 
fcf6aaa
8322740
 
1ba3e62
 
fcf6aaa
1ba3e62
fcf6aaa
1ba3e62
 
fcf6aaa
1ba3e62
 
 
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
51
# svm_vgg_preprocessor.py
import torch
import numpy as np
from torchvision import transforms
from torchvision.models import vgg16
from PIL import Image

class FeatureExtractor:
    def __init__(self):
        # Load VGG16 with original architecture
        self.vgg = vgg16(weights='DEFAULT')
        self.vgg.eval()
        
        # Use only convolutional features (matches original code)
        self.conv_extractor = self.vgg.features
        
        # Original preprocessing
        self.preprocess = transforms.Compose([
            transforms.Resize((224, 224)),
            transforms.ToTensor(),
            transforms.Normalize(
                mean=[0.485, 0.456, 0.406],
                std=[0.229, 0.224, 0.225]
            )
        ])

    def extract_fc_cnn_features(self, image_path):
        """Matches original FC-CNN feature extraction (conv features)"""
        img = Image.open(image_path).convert('RGB')
        img_tensor = self.preprocess(img).unsqueeze(0)
        
        with torch.no_grad():
            features = self.conv_extractor(img_tensor)
            
        return features.squeeze().numpy().flatten()

    def extract_fv_cnn_features(self, image_path):
        """Matches original FV-CNN feature extraction (conv features)"""
        img = Image.open(image_path).convert('RGB')
        img_tensor = self.preprocess(img).unsqueeze(0)
        
        with torch.no_grad():
            features = self.conv_extractor(img_tensor)
            
        return features.squeeze().numpy().flatten()

    def extract_combined_features(self, image_path):
        """Original concatenation of identical features"""
        fc = self.extract_fc_cnn_features(image_path)
        fv = self.extract_fv_cnn_features(image_path)
        return np.concatenate((fc, fv))