MCPcopy Create free account
hub / github.com/dcharatan/flowmap / FlowPredictorGMFlow

Class FlowPredictorGMFlow

flowmap/flow/flow_predictor_gmflow.py:27–85  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

25
26
27class FlowPredictorGMFlow(FlowPredictor[FlowPredictorGMFlowCfg]):
28 def __init__(self, cfg: FlowPredictorGMFlowCfg) -> None:
29 super().__init__(cfg)
30
31 # Warn that GMFlow isn't installed.
32 if GMFlow is None:
33 print(
34 "Warning: GMFlow could not be imported. Did you forget to initialize "
35 "the git submodules?"
36 )
37 sys.exit(1)
38
39 # Ensure that the checkpoint exists.
40 checkpoint = "gmflow-scale1-mixdata-train320x576-4c3a6e9a.pth"
41 checkpoint_path = cfg.cache_path / checkpoint
42 if not checkpoint_path.exists():
43 checkpoint_path.parent.mkdir(exist_ok=True, parents=True)
44 print("Downloading GMFlow checkpoint.")
45 urllib.request.urlretrieve(
46 f"https://s3.eu-central-1.amazonaws.com/avg-projects/unimatch/pretrained/{checkpoint}",
47 checkpoint_path,
48 )
49
50 # Set up the model.
51 self.model = GMFlow(
52 feature_channels=128,
53 num_scales=1,
54 upsample_factor=8,
55 num_head=1,
56 attention_type="swin",
57 ffn_dim_expansion=4,
58 num_transformer_layers=6,
59 )
60
61 # Load the pre-trained checkpoint.
62 checkpoint = torch.load(checkpoint_path)
63 weights = checkpoint["model"] if "model" in checkpoint else checkpoint
64 self.model.load_state_dict(weights, strict=False)
65
66 def forward(
67 self,
68 videos: Float[Tensor, "batch frame 3 height width"],
69 ) -> Float[Tensor, "batch frame-1 height width 2"]:
70 source, target, b, f = split_videos(videos)
71
72 result = self.model(
73 source * 255,
74 target * 255,
75 attn_splits_list=[2],
76 corr_radius_list=[-1],
77 prop_radius_list=[-1],
78 pred_bidir_flow=False,
79 )
80 flow = result["flow_preds"][-1]
81
82 # Normalize the optical flow.
83 _, _, h, w = source.shape
84 wh = torch.tensor((w, h), dtype=torch.float32, device=flow.device)

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected