Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Unsupervised Video Summarization
Manage
Activity
Members
Labels
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Hussain Kanafani
Unsupervised Video Summarization
Commits
9d8eebb4
Commit
9d8eebb4
authored
4 years ago
by
Hussain Kanafani
Browse files
Options
Downloads
Patches
Plain Diff
feature extracted with pretrained model by using avg pool in the last layer
parent
93dfbedd
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
feature_extraction.py
+58
-0
58 additions, 0 deletions
feature_extraction.py
with
58 additions
and
0 deletions
feature_extraction.py
0 → 100644
+
58
−
0
View file @
9d8eebb4
import
numpy
as
np
import
torchvision.models
as
models
import
torchvision
as
tv
import
torch
import
torch.nn
as
nn
import
os
from
PIL
import
Image
import
argparse
class
FeatureExtractor
(
nn
.
Module
):
def
__init__
(
self
,
arch
=
tv
.
models
.
resnet50
):
super
(
FeatureExtractor
,
self
).
__init__
()
self
.
tranform
=
tv
.
transforms
.
Compose
([
tv
.
transforms
.
Resize
([
224
,
224
]),
tv
.
transforms
.
ToTensor
(),
tv
.
transforms
.
Normalize
(
mean
=
[
0.485
,
0.456
,
0.406
],
std
=
[
0.229
,
0.224
,
0.225
])])
self
.
model
=
nn
.
Sequential
(
*
(
list
(
arch
.
children
())[:
-
2
]
+
[
nn
.
MaxPool2d
(
4
,
1
)]))
def
forward
(
self
,
frame
):
features
=
self
.
model
(
frame
)
features
=
features
.
reshape
((
features
.
shape
[
0
],
-
1
))
return
features
if
__name__
==
'
__main__
'
:
parser
=
argparse
.
ArgumentParser
(
description
=
'
Features Extraction
'
)
parser
.
add_argument
(
'
--frames
'
,
metavar
=
'
Frames-dir
'
,
default
=
'
./frames
'
,
help
=
'
path to input frames
'
)
parser
.
add_argument
(
'
--model
'
,
default
=
'
resnet50
'
,
help
=
'
pretrained model architecture e.g. resnet50 or alexnet
'
)
args
=
parser
.
parse_args
()
frames_dir
=
args
.
frames
if
args
.
model
==
'
alexnet
'
:
model_arch
=
models
.
alexnet
(
pretrained
=
True
)
else
:
model_arch
=
models
.
resnet50
(
pretrained
=
True
)
isCuda
=
torch
.
cuda
.
is_available
()
if
isCuda
:
model
=
FeatureExtractor
(
model_arch
).
cuda
()
else
:
model
=
FeatureExtractor
(
model_arch
)
feature
=
dict
()
for
i
,
video
in
enumerate
(
os
.
listdir
(
frames_dir
)):
video
=
os
.
path
.
join
(
frames_dir
,
video
)
print
(
video
)
feature
[
i
]
=
[]
for
frame
in
os
.
listdir
(
video
):
frame
=
os
.
path
.
join
(
video
,
frame
)
print
(
frame
)
img
=
Image
.
open
(
frame
)
img
=
model
.
tranform
(
img
)
img
=
img
.
view
((
1
,)
+
img
.
shape
)
feat
=
model
(
img
)
print
(
feat
.
shape
)
feature
[
i
].
append
(
feat
.
cpu
().
detach
().
numpy
()[
0
])
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment