Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
2
2020-137
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
5
Merge Requests
5
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
2020-137
2020-137
Commits
5238fe8c
Commit
5238fe8c
authored
Nov 07, 2020
by
I.S.M. Dissanyake
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
video_classification/video_classifier.py file
parent
caa896bd
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
91 additions
and
0 deletions
+91
-0
video_classification/video_classifier.py
video_classification/video_classifier.py
+91
-0
No files found.
video_classification/video_classifier.py
0 → 100644
View file @
5238fe8c
import
keras
from
keras.models
import
Sequential
from
keras.applications.vgg16
import
VGG16
from
keras.layers
import
Dense
,
InputLayer
,
Dropout
,
Flatten
from
keras.layers
import
Conv2D
,
MaxPooling2D
,
GlobalMaxPooling2D
from
keras.preprocessing
import
image
import
numpy
as
np
import
pandas
as
pd
import
matplotlib.pyplot
as
plt
from
tqdm
import
tqdm
from
sklearn.model_selection
import
train_test_split
train
=
pd
.
read_csv
(
'UCF/train_new.csv'
)
train
.
head
()
# creating an empty list
train_image
=
[]
# for loop to read and store frames
for
i
in
tqdm
(
range
(
train
.
shape
[
0
])):
# loading the image and keeping the target size as (224,224,3)
img
=
image
.
load_img
(
'UCF/'
+
train
[
'image'
][
i
],
target_size
=
(
480
,
272
,
3
))
# converting it to array
img
=
image
.
img_to_array
(
img
)
# normalizing the pixel value
img
=
img
/
255
# appending the image to the train_image list
train_image
.
append
(
img
)
# converting the list to numpy array
X
=
np
.
array
(
train_image
)
# separating the target
y
=
train
[
'class'
]
# creating the training and validation set
X_train
,
X_test
,
y_train
,
y_test
=
train_test_split
(
X
,
y
,
random_state
=
42
,
test_size
=
0.2
,
stratify
=
y
)
# separating the target
y
=
train
[
'class'
]
# creating the training and validation set
X_train
,
X_test
,
y_train
,
y_test
=
train_test_split
(
X
,
y
,
random_state
=
42
,
test_size
=
0.2
,
stratify
=
y
)
# creating dummies of target variable for train and validation set
y_train
=
pd
.
get_dummies
(
y_train
)
y_test
=
pd
.
get_dummies
(
y_test
)
# creating the base model of pre-trained VGG16 model
base_model
=
VGG16
(
weights
=
'imagenet'
,
include_top
=
False
)
# extracting features for training frames
X_train
=
base_model
.
predict
(
X_train
)
X_train
.
shape
# extracting features for validation frames
X_test
=
base_model
.
predict
(
X_test
)
# reshaping the training as well as validation frames in single dimension
X_train
=
X_train
.
reshape
(
522
,
15
*
8
*
512
)
X_test
=
X_test
.
reshape
(
131
,
15
*
8
*
512
)
# normalizing the pixel values
max
=
X_train
.
max
()
X_train
=
X_train
/
max
X_test
=
X_test
/
max
#defining the model architecture
model
=
Sequential
()
model
.
add
(
Dense
(
1024
,
activation
=
'relu'
,
input_shape
=
(
61440
,)))
model
.
add
(
Dropout
(
0.5
))
model
.
add
(
Dense
(
512
,
activation
=
'relu'
))
model
.
add
(
Dropout
(
0.5
))
model
.
add
(
Dense
(
256
,
activation
=
'relu'
))
model
.
add
(
Dropout
(
0.5
))
model
.
add
(
Dense
(
128
,
activation
=
'relu'
))
model
.
add
(
Dropout
(
0.5
))
model
.
add
(
Dense
(
8
,
activation
=
'softmax'
))
# defining a function to save the weights of best model
from
keras.callbacks
import
ModelCheckpoint
mcp_save
=
ModelCheckpoint
(
'weight.hdf5'
,
save_best_only
=
True
,
monitor
=
'val_loss'
,
mode
=
'min'
)
# compiling the model
model
.
compile
(
loss
=
'categorical_crossentropy'
,
optimizer
=
'Adam'
,
metrics
=
[
'accuracy'
])
# training the model
model
.
fit
(
X_train
,
y_train
,
epochs
=
200
,
validation_data
=
(
X_test
,
y_test
),
callbacks
=
[
mcp_save
],
batch_size
=
128
)
model
.
save_weights
(
'model_saved.h5'
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment