-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextract_features.py
153 lines (117 loc) · 4.5 KB
/
extract_features.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
from PIL import Image
import numpy as np
import os
import torch
import torch.nn as nn
from torch.nn import Sequential, Linear, Dropout, ReLU
from torchvision import datasets, transforms, models
from torchvision.models import inception_v3, Inception_V3_Weights
from torchvision.models.feature_extraction import (
create_feature_extractor,
get_graph_node_names,
)
from torchvision import models
from feature_extraction.models import InceptionV3Regressor
import yaml
from tqdm import tqdm
from flopth import flopth
def isRepetetive(features_path, name):
return name + ".csv" in os.listdir(features_path)
def get_top_triangle(matrix):
indices = np.triu_indices(matrix.shape[0])
return matrix[indices]
def gram_matrix(input):
a, b, c, d = (
input.size()
) # a=batch size(=1), b=number of feature maps, (c,d)=dimensions of a f. map (N=c*d)
features = input.view(a * b, c * d) # resize F_XL into \hat F_XL
G = torch.matmul(features, features.t()) # compute the gram product
return G.div(a * b * c * d)
def flatten_symmetric(matrix):
# Get the indices of the upper triangular part of the matrix
row_indices, col_indices = torch.triu_indices(matrix.size(0), matrix.size(1))
# Get the values of the upper triangular part of the matrix
upper_triangular_part = matrix[row_indices, col_indices]
return upper_triangular_part
def extract_spatial_features(
videos_path,
features_path,
model,
feature_extractor,
image_transforms,
device,
isGram=True,
):
video_names = os.listdir(videos_path)
skipped = []
for index in range(len(video_names)):
video_name = video_names[index]
print(index + 1)
video_path = os.path.join(videos_path, video_name)
video_features_path = os.path.join(features_path, video_name)
if os.path.exists(video_features_path):
skipped.append(video_name)
continue
frame_names = os.listdir(video_path)
for frame_index in range(len(frame_names)):
frame_name = frame_names[frame_index]
with torch.no_grad():
frame_path = os.path.join(video_path, frame_name)
frame = image_transforms(Image.open(frame_path))[None, :].to(device)
features = feature_extractor(frame)
for feature_layer in features:
video_directory = os.path.join(video_features_path, feature_layer)
# video_directory = os.path.join(video_features_path)
path = os.path.join(video_directory, str(frame_index) + ".pt")
os.makedirs(video_directory, exist_ok=True)
if feature_layer == "inception.avgpool":
torch.save(features[feature_layer].flatten(), path)
else:
upper_triangle_gram = flatten_symmetric(
gram_matrix(features[feature_layer])
)
torch.save(upper_triangle_gram, path)
print("Skipped List: ", len(skipped))
def get_model(model_path, device):
model = InceptionV3Regressor()
if model_path:
model.load_state_dict(torch.load(model_path))
else:
print("Creating raw model...")
model = model.to(device)
for param in model.parameters():
param.requires_grad = False
print("Model is ready!!!")
return model
def extract_frame_features():
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
print("The process is starting on this device: ", device)
model_path = (
"C:\\Users\\easad\\Desktop\\Feature Extraction\\6\\trained_model_SGD_3.pth"
)
model = get_model(model_path, device)
layers_name = [
"inception.Mixed_5b.cat",
"inception.Mixed_5c.cat",
"inception.avgpool",
]
feature_extractor = create_feature_extractor(model, return_nodes=list(layers_name))
image_transforms = transforms.Compose(
[
transforms.Resize(299),
transforms.CenterCrop(299),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),
]
)
frames_path = "E:\\IPRIA Datasets\\konvid1k\\frames"
features_path = "D:\\IPRIA\\KoNViD-1k\\Spatial Features"
extract_spatial_features(
frames_path,
features_path,
model,
feature_extractor,
image_transforms,
device=device,
)
extract_frame_features()