minhdang commited on
Commit
b2cc914
·
verified ·
1 Parent(s): 939e831

Rename app.txt to app.py

Browse files
Files changed (2) hide show
  1. app.py +79 -0
  2. app.txt +0 -0
app.py ADDED
@@ -0,0 +1,79 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import torch.nn as nn
3
+ from torchvision.transforms._transforms_video import (CenterCropVideo,NormalizeVideo)
4
+ from torchvision.transforms import (Compose,Lambda,RandomCrop,RandomHorizontalFlip,Resize)
5
+ from pytorchvideo.transforms import (ApplyTransformToKey,Normalize,RandomShortSideScale,UniformTemporalSubsample,Permute)
6
+ import numpy as np
7
+ import gradio as gr
8
+ import spaces
9
+ # torch.save(model.state_dict(), 'model.pth')
10
+ video_transform = Compose([
11
+ ApplyTransformToKey(key = 'video',
12
+ transform = Compose([
13
+ UniformTemporalSubsample(20),
14
+ Lambda(lambda x:x/255),
15
+ Normalize((0.45,0.45,0.45),(0.225,0.225,0.225)),
16
+ RandomShortSideScale(min_size = 248, max_size = 256),
17
+ CenterCropVideo(224),
18
+ RandomHorizontalFlip(p=0.5),
19
+ ]),
20
+ ),
21
+ ])
22
+ #============================================================
23
+ class mymodel_test(nn.Module):
24
+ def __init__(self):
25
+ super(mymodel_test,self).__init__()
26
+ self.video_model = torch.hub.load('facebookresearch/pytorchvideo','efficient_x3d_xs', pretrained=False)
27
+ self.relu = nn.ReLU()
28
+
29
+ self.Linear = nn.Linear(400,1)
30
+
31
+ def forward(self,x):
32
+
33
+ x = self.relu(self.video_model(x))
34
+ x = self.Linear(x)
35
+ return x
36
+
37
+
38
+ #============================================================
39
+
40
+ model_test = mymodel_test()
41
+ model_test.load_state_dict(torch.load('model.pth'))
42
+ from pytorchvideo.data.encoded_video import EncodedVideo
43
+ @spaces.GPU
44
+ def interface_video(video_path):
45
+ video = EncodedVideo.from_path(video_path)
46
+
47
+ video_data = video.get_clip(0,2)
48
+
49
+ video_data = video_transform(video_data)
50
+
51
+ video_data['video'].shape
52
+ model = model_test
53
+
54
+ inputs = video_data['video']
55
+
56
+ inputs = torch.unsqueeze(inputs, 0 )
57
+ inputs.shape
58
+ preds = model(inputs)
59
+
60
+ preds = preds.detach().cpu().numpy()
61
+ preds = np.where(preds>0.5,1,0)
62
+ if(preds==0):
63
+ return 'non violence'
64
+ else:
65
+ return 'violence'
66
+ # return preds
67
+ demo = gr.Blocks()
68
+ with demo:
69
+ with gr.Row():
70
+ with gr.Column():
71
+ with gr.Row():
72
+ input_video = gr.Video(label='Input Video', height=360)
73
+ # input_video = load_video(input_video)
74
+ with gr.Row():
75
+ submit_video_button = gr.Button('Submit')
76
+ with gr.Column():
77
+ label_video = gr.Label()
78
+ submit_video_button.click(fn=iinterface_video, inputs=input_video, outputs=label_video)
79
+ demo.launch()
app.txt DELETED
File without changes