-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel_utils.py
More file actions
136 lines (103 loc) · 4.29 KB
/
model_utils.py
File metadata and controls
136 lines (103 loc) · 4.29 KB
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
from keras.applications.vgg19 import VGG19
from keras.applications.vgg16 import VGG16
from keras.applications.resnet50 import ResNet50
from keras.models import Sequential
from keras.layers.core import Dense, Dropout, Flatten
from keras.layers import LSTM
from keras.layers.convolutional import Convolution2D, Convolution3D, MaxPooling2D, Conv2D, MaxPooling3D, Conv3D
from keras import backend
import numpy as np
import tensorflow as tf
import os
tf.control_flow_ops = tf
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
def remove_last_layers(model):
"""To remove the last FC layers of VGG and get the 4096 dim features"""
model.layers.pop()
model.layers.pop()
model.outputs = [model.layers[-1].output]
model.layers[-1].outbound_nodes = []
vgg_model_16 = VGG16(include_top=True, weights="imagenet")
vgg_model_19 = VGG19(include_top=True, weights="imagenet")
resnet50_model = ResNet50(weights='imagenet', include_top=False, pooling='avg')
remove_last_layers(vgg_model_16)
remove_last_layers(vgg_model_19)
def get_features_batch(frames, model_name="vgg16"):
if model_name.lower() in ["vgg16", "vgg_16"]:
model = vgg_model_16
if model_name.lower() in ["vgg19", "vgg_19"]:
model = vgg_model_19
if model_name.lower() in ["resnet", "resnet50"]:
model = resnet50_model
imageTensor = np.array(frames)
### /255 causing error. Maybe Vanishing gradients
modelFeature = model.predict(imageTensor, verbose=1)
return modelFeature
def get_features(image, model_name="vgg16"):
if backend.image_dim_ordering()=='th':
print( "Please switch to tensorflow backend. Update to reorder will come soon.")
return None
if model_name.lower() in ["vgg16", "vgg_16"]:
model = vgg_model_16
if model_name.lower() in ["vgg19", "vgg_19"]:
model = vgg_model_19
imageTensor = np.zeros((1, 224, 224, 3))
imageTensor[0] = image
### /255 causing error. Maybe Vanishing gradients
modelFeature = model.predict(imageTensor)[0]
return modelFeature
def mlp_model(number_of_classes=2, dim=None):
# Model.
model = Sequential()
model.add(Dense(512,input_dim=dim, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(512, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(number_of_classes, activation='softmax'))
return model
def spatial_model(number_of_classes=2, dim=None):
"""Classification layers here."""
model = Sequential()
model.add(Dense(2048, input_dim=dim, activation='relu'))
model.add(Dropout(0.25))
model.add(Dense(256, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(64, activation='relu'))
model.add(Dense(number_of_classes, activation='softmax'))
return model
def lstm_model(number_of_classes=2, number_of_frames=9, input_dim=4096):
"""Classification layers here with LSTM."""
if number_of_frames == None:
print( "Need to specify the number of frames (as timestep).")
return
model = Sequential()
model.add(LSTM(64, return_sequences=True, stateful=False, input_shape=(number_of_frames, input_dim)))
model.add(LSTM(64, return_sequences=True, stateful=False))
model.add(LSTM(64))
model.add(Dense(32, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(number_of_classes, activation='softmax'))
return model
def optical_flow_model(number_of_classes=2):
model = Sequential()
model.add(Convolution2D(48, 7, 7, border_mode='same', input_shape=(2, 224, 224), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(512))
model.add(Dropout(0.5))
model.add(Dense(64, activation='relu'))
model.add(Dense(32, activation='relu'))
model.add(Dense(number_of_classes, activation='softmax'))
return model
if __name__=="__main__":
import cv2
inputImage = cv2.resize(cv2.imread("testImages/test1.jpg"), (224, 224))
from time import time
start = time()
vector = get_features(inputImage, 'vgg19')
print( 'time taken by vgg 19:',time()-start,'seconds. Vector shape:',vector.shape)
start = time()
vector = get_features(inputImage, 'vgg16')
print( 'time taken by vgg 16:',time()-start,'seconds. Vector shape:',vector.shape)
model = spatial_model(4)
print( model.summary())