gmancino-ball commited on
Commit
6f86bd8
·
verified ·
1 Parent(s): 2905f64

Update script.py

Browse files
Files changed (1) hide show
  1. script.py +38 -13
script.py CHANGED
@@ -5,6 +5,7 @@ import tqdm.auto as tqdm
5
  import os
6
  import io
7
  import torch
 
8
  import time
9
  import av
10
  import torch
@@ -16,27 +17,53 @@ import numpy as np
16
  # So you must include everything you need in your model repo.
17
 
18
 
19
- def preprocess(file_like):
20
- # Open the video file
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  file_like.seek(0)
22
  container = av.open(file_like)
23
  frames = []
24
  every = 10
25
- MAX_MEMORY = 100 * 1024 * 1024 ## 100 MB maximum - some videos are large
26
  current_memory = 0
27
  for i, frame in enumerate(container.decode(video=0)):
28
  if i % every == 0:
29
  frame_array = frame.to_ndarray(format="rgb24")
30
  frame_tensor = torch.from_numpy(frame_array).permute(2, 0, 1).float()
31
- frames.append(frame_tensor)
 
 
 
 
 
 
 
32
  ## Memory check
33
- frame_bytes = frame_tensor.numel() * 4 # float32 4 bytes
34
  current_memory += frame_bytes
35
- if current_memory >= MAX_MEMORY:
36
  break
37
 
38
- video_tensor = torch.stack(frames)
39
- return video_tensor
40
 
41
 
42
  class Model(torch.nn.Module):
@@ -71,11 +98,9 @@ for el in tqdm.tqdm(dataset_remote):
71
  # el["video"]["path"] containts the filename. This is just for reference and you cant actually load it
72
 
73
  # if you are using libraries that expect a file. You can use BytesIO object
74
- # print("processing", el["id"])
75
- raise ValueError
76
  try:
77
  file_like = io.BytesIO(el["video"]["bytes"])
78
- tensor = preprocess(file_like)
79
 
80
  with torch.no_grad():
81
  # soft decision (such as log likelihood score)
@@ -90,11 +115,11 @@ for el in tqdm.tqdm(dataset_remote):
90
  # "id" and "pred" are required. "score" will not be used in scoring but we encourage you to include it. We'll use it for analysis of the results
91
 
92
  out.append(dict(id=el["id"], pred=pred, score=score))
 
93
  except Exception as e:
94
  print(e)
95
  print("failed", el["id"])
96
- # raise e
97
  out.append(dict(id=el["id"]))
98
 
99
  # save the final result and that's it
100
- pd.DataFrame(out).to_csv("submission.csv", index=False)
 
5
  import os
6
  import io
7
  import torch
8
+ from torchvision import transforms
9
  import time
10
  import av
11
  import torch
 
17
  # So you must include everything you need in your model repo.
18
 
19
 
20
+ def preprocess(
21
+ file_like: io.BytesIO, crop_size: int = -1, max_memory: int = 50 * 1024 * 1024, device: str = "cpu"
22
+ ) -> torch.Tensor:
23
+ """
24
+ This preprocessing function loads videos and reduces their input size if necessary.
25
+ This is just a guide function; square center cropping may not be the most appropriate,
26
+ 50 MB per video may not be enough, etc.
27
+
28
+ Args:
29
+ file_like (io.BytesIO): video bytes
30
+ crop_size (int, optional): center crop adjustment (if frames are too large, this will crop)
31
+ max_memory (int, optional): maximum memory per video to be saved as a tensor
32
+ device (str, optional): which device to store the tensors on
33
+ Returns:
34
+ torch.Tensor: Tensor of video
35
+ """
36
+ ## Define crop if applicable
37
+ center_crop_transform = None
38
+ if crop_size > 0:
39
+ center_crop_transform = transforms.CenterCrop(crop_size)
40
+
41
+ ## Open the video file
42
  file_like.seek(0)
43
  container = av.open(file_like)
44
  frames = []
45
  every = 10
 
46
  current_memory = 0
47
  for i, frame in enumerate(container.decode(video=0)):
48
  if i % every == 0:
49
  frame_array = frame.to_ndarray(format="rgb24")
50
  frame_tensor = torch.from_numpy(frame_array).permute(2, 0, 1).float()
51
+
52
+ ## Crop
53
+ if center_crop_transform is not None:
54
+ frame_tensor = center_crop_transform(frame_tensor)
55
+
56
+ ## Append to the list
57
+ frames.append(frame_tensor.to(device))
58
+
59
  ## Memory check
60
+ frame_bytes = frame_tensor.numel() * 4 # float32 = 4 bytes
61
  current_memory += frame_bytes
62
+ if current_memory >= max_memory:
63
  break
64
 
65
+ ## Stack as video
66
+ return torch.stack(frames)
67
 
68
 
69
  class Model(torch.nn.Module):
 
98
  # el["video"]["path"] containts the filename. This is just for reference and you cant actually load it
99
 
100
  # if you are using libraries that expect a file. You can use BytesIO object
 
 
101
  try:
102
  file_like = io.BytesIO(el["video"]["bytes"])
103
+ tensor = preprocess(file_like, device=device)
104
 
105
  with torch.no_grad():
106
  # soft decision (such as log likelihood score)
 
115
  # "id" and "pred" are required. "score" will not be used in scoring but we encourage you to include it. We'll use it for analysis of the results
116
 
117
  out.append(dict(id=el["id"], pred=pred, score=score))
118
+
119
  except Exception as e:
120
  print(e)
121
  print("failed", el["id"])
 
122
  out.append(dict(id=el["id"]))
123
 
124
  # save the final result and that's it
125
+ pd.DataFrame(out).to_csv("submission.csv", index=False)