-
Notifications
You must be signed in to change notification settings - Fork 57
/
mamonfight22.py
111 lines (93 loc) · 3.42 KB
/
mamonfight22.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
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
import numpy as np
from skimage.transform import resize
def video_mamonreader(cv2,filename):
frames = np.zeros((30, 160, 160, 3), dtype=np.float)
i=0
print(frames.shape)
vc = cv2.VideoCapture(filename)
if vc.isOpened():
rval , frame = vc.read()
else:
rval = False
frm = resize(frame,(160,160,3))
frm = np.expand_dims(frm,axis=0)
if(np.max(frm)>1):
frm = frm/255.0
frames[i][:] = frm
i +=1
print("reading video")
while i < 30:
rval, frame = vc.read()
frm = resize(frame,(160,160,3))
frm = np.expand_dims(frm,axis=0)
if(np.max(frm)>1):
frm = frm/255.0
frames[i][:] = frm
i +=1
return frames
def mamon_videoFightModel(tf,wight='mamon-videofight100.hdf5'):
layers = tf.contrib.keras.layers
models = tf.contrib.keras.models
losses = tf.contrib.keras.losses
optimizers = tf.contrib.keras.optimizers
metrics = tf.contrib.keras.metrics
num_classes = 2
input_shapes = (160,160,3)
vg19 = tf.contrib.keras.applications.vgg19.VGG19
base_model = vg19(include_top=False,weights=None,input_shape=(100,100,3))
for layer in base_model.layers:
layer.trainable = False
model = models.Sequential()
num_classes = 2
cnn = models.Sequential()
cnn.add(base_model)
cnn.add(layers.Flatten())
model = models.Sequential()
model.add(layers.TimeDistributed(cnn, input_shape=(40, 100, 100, 3)))
model.add(layers.LSTM(40))
model.add(layers.Dense(13, activation='relu'))
model.add(layers.Dropout(0.1))
model.add(layers.Dense(num_classes, activation="sigmoid"))
adam = optimizers.Adam(lr=0.0005, beta_1=0.9, beta_2=0.999, epsilon=1e-08)
model.load_weights(wight)
model.compile(loss='binary_crossentropy', optimizer= adam, metrics=["accuracy"])
return model
def mamon_videoFightModel2(tf,wight='mamonbest947oscombo.hdfs'):
layers = tf.contrib.keras.layers
models = tf.contrib.keras.models
losses = tf.contrib.keras.losses
optimizers = tf.contrib.keras.optimizers
metrics = tf.contrib.keras.metrics
num_classes = 2
cnn = models.Sequential()
#cnn.add(base_model)
input_shapes=(160,160,3)
np.random.seed(1234)
vg19 = tf.keras.applications.vgg19.VGG19
base_model = vg19(include_top=False,weights='imagenet',input_shape=(160, 160,3))
# Freeze the layers except the last 4 layers
#for layer in base_model.layers:
# layer.trainable = False
cnn = models.Sequential()
cnn.add(base_model)
cnn.add(layers.Flatten())
model = models.Sequential()
model.add(layers.TimeDistributed(cnn, input_shape=(30, 160, 160, 3)))
model.add(layers.LSTM(30 , return_sequences= True))
model.add(layers.TimeDistributed(layers.Dense(90)))
model.add(layers.Dropout(0.1))
model.add(layers.GlobalAveragePooling1D())
model.add(layers.Dense(512, activation='relu'))
model.add(layers.Dropout(0.3))
model.add(layers.Dense(num_classes, activation="sigmoid"))
adam = optimizers.Adam(lr=0.0005, beta_1=0.9, beta_2=0.999, epsilon=1e-08)
model.load_weights(wight)
rms = optimizers.RMSprop()
model.compile(loss='binary_crossentropy', optimizer=adam, metrics=["accuracy"])
return model
def pred_fight(model,video,acuracy=0.9):
pred_test = model.predict(video)
if pred_test[0][1] >=acuracy:
return True , pred_test[0][1]
else:
return False , pred_test[0][1]