innat commited on
Commit
ed0cf5e
1 Parent(s): 5333c42

Create utils.py

Browse files
Files changed (1) hide show
  1. utils.py +39 -0
utils.py ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import tensorflow as tf
2
+ import numpy as np
3
+ from einops import rearrange
4
+ from decord import VideoReader
5
+
6
+ num_frames = 32
7
+ input_size = 224
8
+ patch_size = (16, 16)
9
+ IMAGENET_MEAN = np.array([0.485, 0.456, 0.406])
10
+ IMAGENET_STD = np.array([0.229, 0.224, 0.225])
11
+
12
+ def format_frames(frame, output_size):
13
+ frame = tf.image.convert_image_dtype(frame, tf.uint8)
14
+ frame = tf.image.resize(frame, size=output_size)
15
+ frame = frame / 255.
16
+ frame = frame - IMAGENET_MEAN
17
+ frame = frame / IMAGENET_STD
18
+ return frame
19
+
20
+ def read_video(file_path):
21
+ container = VideoReader(file_path)
22
+ return container
23
+
24
+ def frame_sampling(container, num_frames):
25
+ interval = len(container) // num_frames
26
+ bids = np.arange(num_frames) * interval
27
+ offset = np.random.randint(interval, size=bids.shape)
28
+ frame_index = bids + offset
29
+ frames = container.get_batch(frame_index).asnumpy()
30
+ frames = np.stack(frames)
31
+ frames = format_frames(frames, [input_size] * 2)
32
+ return frames
33
+
34
+ def denormalize(z):
35
+ mean = np.array([0.485, 0.456, 0.406])
36
+ std = np.array([0.225, 0.225, 0.225])
37
+ x = (z * std) + mean
38
+ x = x * 255
39
+ return x.clip(0, 255)