<jupyter_start><jupyter_text><jupyter_code>! git clone https://github.com/Tessellate-Imaging/Monk_Object_Detection.git
 
! cd Monk_Object_Detection/18_mmaction/installation && cat requirements.txt | xargs -n 1 -L 1 pip install -vvvv
 
! pip install pillow==6.0.0
# Restart runtime
! cd Monk_Object_Detection/18_mmaction/lib/ && pip install -vvvv -e .
 
! pip uninstall -y mmcv
 
! cd Monk_Object_Detection/18_mmaction/lib/mmcv && MMCV_WITH_OPS=1 pip install -vvvv -e .
# Restart runtime
! mkdir Dataset
! mkdir Dataset/Videos
!mkdir Anomaly
from distutils.dir_util import copy_tree

# copy subdirectory example
fromDirectory = "/content/drive/MyDrive/Anomaly-Videos-Part-1/Arrest"
toDirectory = "/content/Anomaly"

copy_tree(fromDirectory, toDirectory)

fromDirectory = "/content/drive/MyDrive/Anomaly-Videos-Part-1/Assault"
toDirectory = "/content/Anomaly"

copy_tree(fromDirectory, toDirectory)

fromDirectory = "/content/drive/MyDrive/Anomaly-Videos-Part-2/Burglary"
toDirectory = "/content/Anomaly"

copy_tree(fromDirectory, toDirectory)

!mkdir Normal
fromDirectory = "/content/drive/MyDrive/Training-Normal-Videos-Part-1"
toDirectory = "/content/Normal"

copy_tree(fromDirectory, toDirectory)


fromDirectory = "/content/drive/MyDrive/Anomaly-Videos-Part-3/Robbery"
toDirectory = "/content/Anomaly"

copy_tree(fromDirectory, toDirectory)


import os

from random import sample

files = os.listdir('/content/Normal')
for file in sample(files,63):
    os.remove('/content/Normal/' + file)


print("Class Anomaly: {}".format(len(os.listdir("/content/Anomaly"))))
print("Class Normal: {}".format(len(os.listdir("/content/Normal"))))

classes = ["Anomaly", "Normal"]

f = open("Dataset/classes.txt", 'w');
for i in range(len(classes)):
    f.write(classes[i] + "\n");
f.close();

import os
from tqdm import tqdm
combined = [];

!mkdir All
!mv  /content/Anomaly /content/All 
!mv  /content/Normal /content/All


folder_name = "/content/All/"
class_name = "Anomaly";
folder_name += class_name + "/";

vid_list = os.listdir(folder_name);
for i in tqdm(range(len(vid_list))):
    os.system("cp " + folder_name + vid_list[i] + " Dataset/Videos/")
    wr = vid_list[i] + " " + str(classes.index(class_name))
    combined.append(wr)



folder_name = "/content/All/"
class_name = "Normal";
folder_name += class_name + "/";

vid_list = os.listdir(folder_name);
for i in tqdm(range(len(vid_list))):
    os.system("cp " + folder_name + vid_list[i] + " Dataset/Videos/")
    wr = vid_list[i] + " " + str(classes.index(class_name))
    combined.append(wr)

import random
random.shuffle(combined)

train_list = combined[:100];
val_list = combined[100:];



f = open("Dataset/train.txt", 'w')
for i in range(len(train_list)):
    f.write(train_list[i] + "\n");
f.close();

f = open("Dataset/val.txt", 'w')
for i in range(len(val_list)):
    f.write(val_list[i] + "\n");
f.close();


import os
import sys
sys.path.append("Monk_Object_Detection/18_mmaction/lib");
from train_engine import Detector_Videos
gtf = Detector_Videos();
video_dir = 'Dataset/Videos/';
anno_file = 'Dataset/train.txt';
classes_list_file = 'Dataset/classes.txt';

gtf.Train_Video_Dataset(video_dir, anno_file, classes_list_file);
video_dir = 'Dataset/Videos/';
anno_file = 'Dataset/val.txt';

gtf.Val_Video_Dataset(video_dir, anno_file);
gtf.Dataset_Params(batch_size=8, num_workers=4)
gtf.List_Models();

gtf.Model_Params(model_name="tsn_r50", gpu_devices=[0])

gtf.Hyper_Params(lr=0.001, momentum=0.9, weight_decay=0.0001)

gtf.Training_Params(num_epochs=100, val_interval=10)
gtf.Train();

from infer_engine import Infer_Videos
import os
import sys
sys.path.append("Monk_Object_Detection/18_mmaction/lib");
gtf = Infer_Videos();
gtf.Dataset_Params('/content/Dataset/classes.txt');
config_file = "/content/work_dirs/config.py"
checkpoint_file = "/content/work_dirs/latest.pth"

gtf.Model_Params(config_file, checkpoint_file, use_gpu=True)
!wget https://www.dropbox.com/sh/75v5ehq4cdg5g5g/AABqY-3fJSmSMafFIlJXRE-9a/Anomaly-Videos-Part-4.zip?dl=0
!unzip /content/Anomaly-Videos-Part-4.zip?dl=0
!rm -r /content/Anomaly-Videos-Part-4.zip?dl=0


import os
for filename in os.listdir('/content/Anomaly-Videos-Part-4/Shoplifting'):
     gtf.Predict('/content/Anomaly-Videos-Part-4/Shoplifting/'+filename)
     print(r[0]+'ddd')
     print('--------------------------------------------------------')
import os
for filename in os.listdir('/content/Anomaly-Videos-Part-4/Stealing'):
     gtf.Predict('/content/Anomaly-Videos-Part-4/Stealing/'+filename)
     print(r[0]+'ddd')
     print('--------------------------------------------------------')
import os
for filename in os.listdir('/content/Anomaly-Videos-Part-4/Vandalism'):
     gtf.Predict('/content/Anomaly-Videos-Part-4/Vandalism/'+filename)
     print(r[0]+'ddd')
     print('--------------------------------------------------------')
import os
for filename in os.listdir('/content/drive/MyDrive/Training-Normal-Videos-Part-1'):
     gtf.Predict('/content/drive/MyDrive/Training-Normal-Videos-Part-1/'+filename)
     print(r[0]+'ddd')
     print('--------------------------------------------------------')
video_path = '/content/Anomaly-Videos-Part-4/Shoplifting/Shoplifting001_x264.mp4'
gtf.Predict(video_path)[0][0]
results
results[0][0]
import os
for filename in os.listdir('/content/Anomaly-Videos-Part-4/Vandalism'):
     r = gtf.Predict('/content/Anomaly-Videos-Part-4/Vandalism/'+filename)[0][0]
     print(r[0]+'ddd')
     print('--------------------------------------------------------')<jupyter_output>Normal:  0.003947051167488098
Anomaly:  -0.007833234667778015
Nddd
--------------------------------------------------------
Anomaly:  0.019918944835662842
Normal:  -0.019947577714920044
Addd
--------------------------------------------------------
Normal:  0.00033650461584329607
Anomaly:  -0.00043894704431295394
Nddd
--------------------------------------------------------
Anomaly:  0.043809237480163576
Normal:  -0.0485731029510498
Addd
--------------------------------------------------------
Anomaly:  0.07006038188934326
Normal:  -0.07416847705841065
Addd
--------------------------------------------------------
Anomaly:  0.04858159065246582
Normal:  -0.05699889659881592
Addd
--------------------------------------------------------
Anomaly:  0.012964738607406616
Normal:  -0.015538061857223512
Addd
--------------------------------------------------------
Normal:  0.0016378228366374968
Anomaly:  -0.000918545350432396
Nddd
--------------------------------------------------------
Normal:  [...]
{'fraction_non_alphanumeric': 0.2476248850750843, 'fraction_numerical': 0.06068035550107263, 'mean_word_length': 2.8735905044510384, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 2, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '147940', 'license': 'Apache-2.0', 'n_tokens_mistral': 2546, 'n_tokens_neox': 2271, 'n_words': 354, 'path': '19_5_tsn_r50.ipynb', 'repo_name': 'ezzatmostafa96/Action-Recognition-in-Real-Time', 'size': '6526'}