-
Notifications
You must be signed in to change notification settings - Fork 1
/
feature_extraction.py
120 lines (69 loc) · 2.52 KB
/
feature_extraction.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
# -*- coding: utf-8 -*-
"""Feature_Extraction.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1Wu8iOw7GMuOmO1EGiUM2g4o5fNbaFiJ-
"""
from google.colab import drive
drive.mount("/content/drive", force_remount=True)
import os
os.chdir('/content/drive/MyDrive/Colab Notebooks/Mask_RCNN/samples/videos/Shots/save')
dir = os.getcwd()
print(dir)
import tensorflow as tf
from keras.applications.resnet50 import ResNet50
from keras.applications.vgg16 import VGG16
from keras.layers import Flatten, Input
from keras.models import Model
from keras.preprocessing import image
from keras.applications.imagenet_utils import preprocess_input
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import Dropout
from keras.layers import LSTM
from keras.utils import to_categorical
from matplotlib import pyplot
import numpy as np
import pandas as pd
import time
import cv2
model = ResNet50(weights='imagenet', pooling=max, include_top = False)
root_dir = os.getcwd()
print(root_dir)
features_straight_drive = []
for j in range(35):
path = os.path.join(root_dir, 'Straight_drive', str(j) + '.mp4')
for i in range(50):
img_path = os.path.join(path,str(i) + '.jpg')
img = image.load_img(img_path, target_size=(224, 224))
img_data = image.img_to_array(img)
img_data = np.expand_dims(img_data, axis=0)
img_data = preprocess_input(img_data)
resnet_feature = model2.predict(img_data)
features_straight_drive.append(resnet_feature.squeeze())
print(j)
features_pull_shot = np.load('features_pull_shot.npy')
features_cover_drive = np.load('features_cover_drive.npy')
features_leg_drive = np.load('features_leg_drive.npy')
fsd = np.array(features_straight_drive).reshape(35,50,-1)
fcd = np.array(features_cover_drive).reshape(35,50,-1)
fps = np.array(features_pull_shot).reshape(35,50,-1)
fld = np.array(features_leg_drive).reshape(35,50,-1)
n_timesteps = 50
n_features = 7*7*512
n_samples = 35
trainX = np.concatenate((fsd, fcd, fps, fld))
trainY = np.zeros(140*4)
trainY = trainY.reshape(-1,4)
for i in range(4):
for j in range(35):
trainY[i*35 + j][i] = 1
model = Sequential()
model.add(LSTM(256,dropout=0.2,input_shape=(n_timesteps,n_features)))
model.add(Dense(1024, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(4, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
# fit model
model.fit(trainX, trainY, epochs=8)
model.save('model1.h5')