Spaces:
Sleeping
Sleeping
Update FeatureExtraction.py
Browse files- FeatureExtraction.py +28 -40
FeatureExtraction.py
CHANGED
@@ -1,41 +1,29 @@
|
|
1 |
-
import cv2
|
2 |
-
import numpy as np
|
3 |
-
from tensorflow.keras.
|
4 |
-
from tensorflow.keras.
|
5 |
-
from tensorflow.keras.
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
self.
|
13 |
-
self.
|
14 |
-
self.
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
self.
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
# Create the feature extraction model
|
30 |
-
self.model = Model(self.base_model.input, self.x_model)
|
31 |
-
|
32 |
-
def extract_feature(self, frames_buffer):
|
33 |
-
x_op = np.zeros((2048, 40)) # Shape (features_dim, seq_length)
|
34 |
-
for i in range(len(frames_buffer)):
|
35 |
-
x_t = frames_buffer[i]
|
36 |
-
x_t = cv2.resize(x_t, (224, 224)) # Resize each frame to the required input size
|
37 |
-
x_t = np.expand_dims(x_t, axis=0) # Add batch dimension
|
38 |
-
x = self.model.predict(x_t)
|
39 |
-
x_op[:, i] = x
|
40 |
-
|
41 |
return x_op
|
|
|
1 |
+
import cv2
|
2 |
+
import numpy as np
|
3 |
+
from tensorflow.keras.applications.resnet import ResNet152
|
4 |
+
from tensorflow.keras.layers import AveragePooling2D, Flatten
|
5 |
+
from tensorflow.keras.models import Model
|
6 |
+
|
7 |
+
class FeatureExtractor:
|
8 |
+
def __init__(self, img_shape, channels, seq_length):
|
9 |
+
self.seq_length = seq_length
|
10 |
+
self.height = img_shape[0]
|
11 |
+
self.width = img_shape[1]
|
12 |
+
self.channels = channels
|
13 |
+
self.base_model = ResNet152(include_top=False, input_shape=(224, 224, 3), weights='imagenet')
|
14 |
+
for layer in self.base_model.layers:
|
15 |
+
layer.trainable = False
|
16 |
+
self.op = self.base_model.output
|
17 |
+
self.x_model = AveragePooling2D((7, 7), name='avg_pool')(self.op)
|
18 |
+
self.x_model = Flatten()(self.x_model)
|
19 |
+
self.model = Model(self.base_model.input, self.x_model)
|
20 |
+
|
21 |
+
def extract_feature(self, frames_buffer):
|
22 |
+
x_op = np.zeros((2048, self.seq_length))
|
23 |
+
for i in range(len(frames_buffer)):
|
24 |
+
x_t = frames_buffer[i]
|
25 |
+
x_t = cv2.resize(x_t, (224, 224))
|
26 |
+
x_t = np.expand_dims(x_t, axis=0)
|
27 |
+
x = self.model.predict(x_t)
|
28 |
+
x_op[:, i] = x.flatten()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
return x_op
|