-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathViolence_Detection.py
482 lines (273 loc) · 11.3 KB
/
Violence_Detection.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
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
# Primero instalar openCV package para importar cv2
import cv2
import os
import numpy as np
import matplotlib.pyplot as plt
# Para descargar los datasets
import download
from random import shuffle
from keras.applications import VGG16
from keras import backend as K
from keras.models import Model, Sequential
from keras.layers import Input
from keras.layers import LSTM
from keras.layers import Dense, Activation
import sys
import h5py
def print_progress(count, max_count):
# Percentage completion.
pct_complete = count / max_count
# Status-message. Note the \r which means the line should
# overwrite itself.
msg = "\r- Progress: {0:.1%}".format(pct_complete)
# Print it.
sys.stdout.write(msg)
sys.stdout.flush()
# Directorio donde vamos a poner todos los videos
in_dir = "data"
# Tamanyo de cada imagen
img_size = 224
img_size_touple = (img_size, img_size)
# Donde se van a almacenar todas las imagene
#images = []
# Numero de canales
num_channels = 3
# Tamanyo imagen cuando se aplana en vector 1 dimension
img_size_flat = img_size * img_size * num_channels
# Numero de clases
num_classes = 2
# Numero de videos para entreno
_num_files_train = 1
# Numero de frames por video
_images_per_file = 20
# Numero de imagenes total en el training-set
_num_images_train = _num_files_train * _images_per_file
# Extension de video
video_exts = ".avi"
# Url de descarga directa
url_hockey = "http://visilab.etsii.uclm.es/personas/oscar/FightDetection/HockeyFights.zip"
url_movies = "http://visilab.etsii.uclm.es/personas/oscar/FightDetection/Peliculas.rar"
in_dir = "data"
# Funcion para descargar los datos
def download_data(in_dir, url):
# Si la carpeta no existe la creamos
if not os.path.exists(in_dir):
os.makedirs(in_dir)
# Para descargar del link directo y extraer los archivos
download.maybe_download_and_extract(url,in_dir)
#def label_vid(vid_name):
#
# word_label =
#
download_data(in_dir,url_hockey)
def get_frames(current_dir, file_name):
in_file = os.path.join(current_dir, file_name)
images = []
vidcap = cv2.VideoCapture(in_file)
success,image = vidcap.read()
count = 0
while count<_images_per_file:
RGB_img = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
res = cv2.resize(RGB_img, dsize=(img_size, img_size),
interpolation=cv2.INTER_CUBIC)
# Convertir imagen en un vector y añadirlo
#images.append(res.flatten())
images.append(res)
success,image = vidcap.read()
count += 1
resul = np.array(images)
# Mirar esto alomejor no va despues
resul = (resul / 255.).astype(np.float16)
return resul
image_model = VGG16(include_top=True, weights='imagenet')
image_model.summary()
# We will use the output of the layer prior to the final
# classification-layer which is named fc2. This is a fully-connected (or dense) layer.
transfer_layer = image_model.get_layer('fc2')
image_model_transfer = Model(inputs=image_model.input,
outputs=transfer_layer.output)
transfer_values_size = K.int_shape(transfer_layer.output)[1]
print("La entrada de la red dimensiones:",K.int_shape(image_model.input)[1:3])
print("La salida de la red dimensiones: ", transfer_values_size)
def get_transfer_values(current_dir, file_name):
# Pre-allocate input-batch-array for images.
shape = (_images_per_file,) + img_size_touple + (3,)
image_batch = np.zeros(shape=shape, dtype=np.float16)
image_batch = get_frames(current_dir, file_name)
# Arreglar esto para obtener los valores de los filtros despues de pooling
# Pre-allocate output-array for transfer-values.
# Note that we use 16-bit floating-points to save memory.
shape = (_images_per_file, transfer_values_size)
transfer_values = np.zeros(shape=shape, dtype=np.float16)
transfer_values = \
image_model_transfer.predict(image_batch)
return transfer_values
in_dir_prueba = 'data'
def proces_transfer(vid_names, in_dir, labels):
count = 0
tam = len(vid_names)
# Pre-allocate input-batch-array for images.
shape = (_images_per_file,) + img_size_touple + (3,)
while count<tam:
video_name = vid_names[count]
image_batch = np.zeros(shape=shape, dtype=np.float16)
image_batch = get_frames(in_dir, video_name)
# Note that we use 16-bit floating-points to save memory.
shape = (_images_per_file, transfer_values_size)
transfer_values = np.zeros(shape=shape, dtype=np.float16)
transfer_values = \
image_model_transfer.predict(image_batch)
labels1 = labels[count]
aux = np.ones([20,2])
labelss = labels1*aux
yield transfer_values, labelss
count+=1
def make_files(n_files):
gen = proces_transfer(names_training, in_dir_prueba, labels_training)
numer = 1
# Read the first chunk to get the column dtypes
chunk = next(gen)
row_count = chunk[0].shape[0]
row_count2 = chunk[1].shape[0]
with h5py.File('prueba.h5', 'w') as f:
# Initialize a resizable dataset to hold the output
maxshape = (None,) + chunk[0].shape[1:]
maxshape2 = (None,) + chunk[1].shape[1:]
dset = f.create_dataset('data', shape=chunk[0].shape, maxshape=maxshape,
chunks=chunk[0].shape, dtype=chunk[0].dtype)
dset2 = f.create_dataset('labels', shape=chunk[1].shape, maxshape=maxshape2,
chunks=chunk[1].shape, dtype=chunk[1].dtype)
# Write the first chunk of rows
dset[:] = chunk[0]
dset2[:] = chunk[1]
for chunk in gen:
if numer == n_files:
break
# Resize the dataset to accommodate the next chunk of rows
dset.resize(row_count + chunk[0].shape[0], axis=0)
dset2.resize(row_count2 + chunk[1].shape[0], axis=0)
# Write the next chunk
dset[row_count:] = chunk[0]
dset2[row_count:] = chunk[1]
# Increment the row count
row_count += chunk[0].shape[0]
row_count2 += chunk[1].shape[0]
print_progress(numer, n_files)
numer += 1
def make_files_validation(n_files):
gen = proces_transfer(names_validation, in_dir_prueba, labels_validation)
numer = 1
# Read the first chunk to get the column dtypes
chunk = next(gen)
row_count = chunk[0].shape[0]
row_count2 = chunk[1].shape[0]
with h5py.File('pruebavalidation.h5', 'w') as f:
# Initialize a resizable dataset to hold the output
maxshape = (None,) + chunk[0].shape[1:]
maxshape2 = (None,) + chunk[1].shape[1:]
dset = f.create_dataset('data', shape=chunk[0].shape, maxshape=maxshape,
chunks=chunk[0].shape, dtype=chunk[0].dtype)
dset2 = f.create_dataset('labels', shape=chunk[1].shape, maxshape=maxshape2,
chunks=chunk[1].shape, dtype=chunk[1].dtype)
# Write the first chunk of rows
dset[:] = chunk[0]
dset2[:] = chunk[1]
for chunk in gen:
if numer == n_files:
break
# Resize the dataset to accommodate the next chunk of rows
dset.resize(row_count + chunk[0].shape[0], axis=0)
dset2.resize(row_count2 + chunk[1].shape[0], axis=0)
# Write the next chunk
dset[row_count:] = chunk[0]
dset2[row_count:] = chunk[1]
# Increment the row count
row_count += chunk[0].shape[0]
row_count2 += chunk[1].shape[0]
print_progress(numer, n_files)
numer += 1
#valores = get_transfer_values(in_dir, file_name)
#transfer_values = cache.cache(cache_path='datos_cache.pkl',
# fn=get_transfer_values,
# current_dir=in_dir,
# file_name="no381_xvid.avi")
def label_video_names(in_dir):
names = []
labels = []
for current_dir, dir_names,file_names in os.walk(in_dir):
for file_name in file_names:
if file_name[0:2] == 'fi':
labels.append([1,0])
names.append(file_name)
elif file_name[0:2] == 'no':
labels.append([0,1])
names.append(file_name)
c = list(zip(names,labels))
shuffle(c)
names, labels = zip(*c)
return names, labels
names, labels = label_video_names(in_dir_prueba)
training_set = int(len(names)*0.8)
validation_set = int(len(names)*0.2)
names_training = names[0:training_set]
names_validation = names[training_set:]
labels_training = labels[0:training_set]
labels_validation = labels[training_set:]
make_files(training_set)
make_files_validation(validation_set)
def process_alldata_training():
joint_transfer=[]
frames_num=20
count = 0
with h5py.File('prueba.h5', 'r') as f:
X_batch = f['data'][:]
y_batch = f['labels'][:]
for i in range(int(len(X_batch)/frames_num)):
inc = count+frames_num
joint_transfer.append([X_batch[count:inc],y_batch[count]])
count =inc
data =[]
target=[]
for i in joint_transfer:
data.append(i[0])
target.append(np.array(i[1]))
return data, target
def process_alldata_validation():
joint_transfer=[]
frames_num=20
count = 0
with h5py.File('pruebavalidation.h5', 'r') as f:
X_batch = f['data'][:]
y_batch = f['labels'][:]
for i in range(int(len(X_batch)/frames_num)):
inc = count+frames_num
joint_transfer.append([X_batch[count:inc],y_batch[count]])
count =inc
data =[]
target=[]
for i in joint_transfer:
data.append(i[0])
target.append(np.array(i[1]))
return data, target
chunk_size = 4096
n_chunks = 20
rnn_size = 512
model = Sequential()
model.add(LSTM(rnn_size, input_shape=(n_chunks, chunk_size)))
model.add(Dense(1024))
model.add(Activation('relu'))
model.add(Dense(50))
model.add(Activation('sigmoid'))
model.add(Dense(2))
model.add(Activation('softmax'))
model.compile(loss='mean_squared_error', optimizer='adam',metrics=['accuracy'])
epoch = 20
batchS = 100
data, target = process_alldata_training()
data_val, target_val = process_alldata_validation()
#model.fit(np.array(data), np.array(labels[0:8]), epochs=epoch, batch_size=batchS, verbose=2)
model.fit(np.array(data), np.array(target), epochs=epoch, batch_size=batchS, verbose=2)
#model.fit(data, target, epochs=epoch, batch_size=batchS, verbose=2)
result = model.evaluate(np.array(data_val), np.array(target_val))
for name, value in zip(model.metrics_names, result):
print(name, value)