-
Notifications
You must be signed in to change notification settings - Fork 0
/
local_feature.py
112 lines (89 loc) · 4.14 KB
/
local_feature.py
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
from mlxtend.frequent_patterns import fpgrowth
import pandas as pd
import torch
import torch.nn as nn
import torchvision.models as models
from torchvision import transforms
from PIL import Image
# Load an image using PIL
image_path = r'C:\Users\mohit\OneDrive\Desktop\remove_background\entropy\CUB_200_2011\images\001.Black_footed_Albatross\Black_Footed_Albatross_0001_796111.jpg' # Replace with your image path
# Load a pretrained VGG-16 model
vgg16 = models.vgg16(pretrained=True)
vgg16.eval()
# Set up image transformations
transform = transforms.Compose([
transforms.Resize((224, 224)),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
])
# Load and transform an image
image = Image.open(image_path)
image = transform(image).unsqueeze(0)
# Pass the image through the VGG-16 model
with torch.no_grad():
features = vgg16.features(image)
# Obtain the salient object by performing a mask operation
features = features.squeeze(0)
A = features.sum(dim=0)
threshold = A.mean()
mask = (A > threshold).float()
mask = mask.unsqueeze(0).unsqueeze(0)
salient_object = features * mask
salient_object = salient_object.squeeze(0)
# Convert feature maps and activated positions into transactions and items
transactions = []
for i in range(salient_object.shape[0]):
feature_map = salient_object[i]
activated_positions = (feature_map > 0).nonzero(as_tuple=True)
items = [f'({x},{y})' for x, y in zip(*activated_positions)]
transactions.append(items)
# Mine frequent patterns using FPM
minsupp = 2 # minimum support threshold for FPM
I = sorted(set(item for transaction in transactions for item in transaction))
df = pd.DataFrame([[int(item in transaction) for item in I] for transaction in transactions], columns=I)
frequent_itemsets = fpgrowth(df, min_support=minsupp/len(transactions), use_colnames=True)
# Extract the local feature fL from the frequent patterns
patterns = torch.zeros_like(salient_object)
for itemset in frequent_itemsets['itemsets']:
for item in itemset:
x, y = map(int, item.strip('()').split(','))
patterns[:, x, y] = 1
fL_max_pooling = nn.functional.adaptive_max_pool2d(patterns, (1, 1)).view(-1)
fL_avg_pooling = nn.functional.adaptive_avg_pool2d(patterns, (1, 1)).view(-1)
fL = torch.cat((fL_max_pooling, fL_avg_pooling), dim=0)
class LocalFeature:
def __init__(self, vgg16):
self.vgg16 = vgg16
def extract(self, image):
with torch.no_grad():
features = self.vgg16.features(image)
# Obtain the salient object by performing a mask operation
features = features.squeeze(0)
A = features.sum(dim=0)
threshold = A.mean()
mask = (A > threshold).float()
mask = mask.unsqueeze(0).unsqueeze(0)
salient_object = features * mask
salient_object = salient_object.squeeze(0)
# Convert feature maps and activated positions into transactions and items
transactions = []
for i in range(salient_object.shape[0]):
feature_map = salient_object[i]
activated_positions = (feature_map > 0).nonzero(as_tuple=True)
items = [f'({x},{y})' for x, y in zip(*activated_positions)]
transactions.append(items)
# Mine frequent patterns using FPM
minsupp = 2 # minimum support threshold for FPM
I = sorted(set(item for transaction in transactions for item in transaction))
df = pd.DataFrame([[int(item in transaction) for item in I] for transaction in transactions], columns=I)
frequent_itemsets = fpgrowth(df, min_support=minsupp/len(transactions), use_colnames=True)
# Extract the local feature fL from the frequent patterns
patterns = torch.zeros_like(salient_object)
for itemset in frequent_itemsets['itemsets']:
for item in itemset:
x, y = map(int, item.strip('()').split(','))
patterns[:, x, y] = 1
fL_max_pooling = nn.functional.adaptive_max_pool2d(patterns, (1, 1)).view(-1)
fL_avg_pooling = nn.functional.adaptive_avg_pool2d(patterns, (1, 1)).view(-1)
fL = torch.cat((fL_max_pooling, fL_avg_pooling), dim=0)
return fL