MCPcopy Create free account
hub / github.com/YesianRohn/TextSSR / closure

Function closure

diffusers/examples/community/fresco_v2v.py:226–254  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

224 while n_iter[0] < iters:
225
226 def closure():
227 optimizer.zero_grad()
228
229 loss = 0
230
231 # temporal consistency loss
232 if optimize_temporal and flows is not None and occs is not None:
233 c1 = rearrange(cs[:, :], "b f c h w -> (b f) c h w")
234 c2 = rearrange(cs[:, reshuffle_list], "b f c h w -> (b f) c h w")
235 warped_image1 = flow_warp(c1, bwd_flow_)
236 warped_image2 = flow_warp(c2, fwd_flow_)
237 loss = (
238 abs((c2 - warped_image1) * (1 - bwd_occ_)) + abs((c1 - warped_image2) * (1 - fwd_occ_))
239 ).mean() * 2
240
241 # spatial consistency loss
242 if attention_probs is not None and intra_weight > 0:
243 cs_vector = rearrange(cs, "b f c h w -> (b f) (h w) c")
244 # attention_scores = torch.bmm(cs_vector, cs_vector.transpose(-1, -2))
245 # cs_attention_probs = attention_scores.softmax(dim=-1)
246 cs_vector = cs_vector / ((cs_vector**2).sum(dim=2, keepdims=True) ** 0.5)
247 cs_attention_probs = torch.bmm(cs_vector, cs_vector.transpose(-1, -2))
248 tmp = F.l1_loss(cs_attention_probs, attention_probs) * intra_weight
249 loss = tmp + loss
250
251 loss.backward()
252 n_iter[0] += 1
253
254 return loss
255
256 optimizer.step(closure)
257

Callers

nothing calls this directly

Calls 2

transposeMethod · 0.80
flow_warpFunction · 0.70

Tested by

no test coverage detected