Vignesh38 commited on
Commit
d8fe2bd
·
1 Parent(s): a549a3b

Fix PyTorch 2.6 weights_only loading and syntax warnings

Browse files
Files changed (4) hide show
  1. app.py +4 -1
  2. models/basic_model.py +6 -3
  3. models/evaluator.py +6 -2
  4. models/networks.py +3 -3
app.py CHANGED
@@ -25,7 +25,10 @@ checkpoint_path = os.path.join("checkpoints", "BIT_LEVIR", "best_ckpt.pt")
25
  if not os.path.exists(checkpoint_path):
26
  raise FileNotFoundError(f"Checkpoint not found at: {checkpoint_path}")
27
 
28
- checkpoint = torch.load(checkpoint_path, map_location=device)
 
 
 
29
  model.load_state_dict(checkpoint["model_G_state_dict"])
30
  model.to(device)
31
  model.eval()
 
25
  if not os.path.exists(checkpoint_path):
26
  raise FileNotFoundError(f"Checkpoint not found at: {checkpoint_path}")
27
 
28
+ try:
29
+ checkpoint = torch.load(checkpoint_path, map_location=device, weights_only=False)
30
+ except TypeError:
31
+ checkpoint = torch.load(checkpoint_path, map_location=device)
32
  model.load_state_dict(checkpoint["model_G_state_dict"])
33
  model.to(device)
34
  model.eval()
models/basic_model.py CHANGED
@@ -28,9 +28,12 @@ class CDEvaluator():
28
  def load_checkpoint(self, checkpoint_name='best_ckpt.pt'):
29
 
30
  if os.path.exists(os.path.join(self.checkpoint_dir, checkpoint_name)):
31
- # load the entire checkpoint
32
- checkpoint = torch.load(os.path.join(self.checkpoint_dir, checkpoint_name),
33
- map_location=self.device)
 
 
 
34
 
35
  self.net_G.load_state_dict(checkpoint['model_G_state_dict'])
36
  self.net_G.to(self.device)
 
28
  def load_checkpoint(self, checkpoint_name='best_ckpt.pt'):
29
 
30
  if os.path.exists(os.path.join(self.checkpoint_dir, checkpoint_name)):
31
+ try:
32
+ checkpoint = torch.load(os.path.join(self.checkpoint_dir, checkpoint_name),
33
+ map_location=self.device, weights_only=False)
34
+ except TypeError:
35
+ checkpoint = torch.load(os.path.join(self.checkpoint_dir, checkpoint_name),
36
+ map_location=self.device)
37
 
38
  self.net_G.load_state_dict(checkpoint['model_G_state_dict'])
39
  self.net_G.to(self.device)
models/evaluator.py CHANGED
@@ -64,8 +64,12 @@ class CDEvaluator():
64
 
65
  if os.path.exists(os.path.join(self.checkpoint_dir, checkpoint_name)):
66
  self.logger.write('loading last checkpoint...\n')
67
- # load the entire checkpoint
68
- checkpoint = torch.load(os.path.join(self.checkpoint_dir, checkpoint_name), map_location=self.device)
 
 
 
 
69
 
70
  self.net_G.load_state_dict(checkpoint['model_G_state_dict'])
71
 
 
64
 
65
  if os.path.exists(os.path.join(self.checkpoint_dir, checkpoint_name)):
66
  self.logger.write('loading last checkpoint...\n')
67
+ try:
68
+ checkpoint = torch.load(os.path.join(self.checkpoint_dir, checkpoint_name),
69
+ map_location=self.device, weights_only=False)
70
+ except TypeError:
71
+ checkpoint = torch.load(os.path.join(self.checkpoint_dir, checkpoint_name),
72
+ map_location=self.device)
73
 
74
  self.net_G.load_state_dict(checkpoint['model_G_state_dict'])
75
 
models/networks.py CHANGED
@@ -263,7 +263,7 @@ class BASE_Transformer(ResNet):
263
  mlp_dim = 2*dim
264
 
265
  self.with_pos = with_pos
266
- if with_pos is 'learned':
267
  self.pos_embedding = nn.Parameter(torch.randn(1, self.token_len*2, 32))
268
  decoder_pos_size = 256//4
269
  self.with_decoder_pos = with_decoder_pos
@@ -294,9 +294,9 @@ class BASE_Transformer(ResNet):
294
 
295
  def _forward_reshape_tokens(self, x):
296
  # b,c,h,w = x.shape
297
- if self.pool_mode is 'max':
298
  x = F.adaptive_max_pool2d(x, [self.pooling_size, self.pooling_size])
299
- elif self.pool_mode is 'ave':
300
  x = F.adaptive_avg_pool2d(x, [self.pooling_size, self.pooling_size])
301
  else:
302
  x = x
 
263
  mlp_dim = 2*dim
264
 
265
  self.with_pos = with_pos
266
+ if with_pos == 'learned':
267
  self.pos_embedding = nn.Parameter(torch.randn(1, self.token_len*2, 32))
268
  decoder_pos_size = 256//4
269
  self.with_decoder_pos = with_decoder_pos
 
294
 
295
  def _forward_reshape_tokens(self, x):
296
  # b,c,h,w = x.shape
297
+ if self.pool_mode == 'max':
298
  x = F.adaptive_max_pool2d(x, [self.pooling_size, self.pooling_size])
299
+ elif self.pool_mode == 'ave':
300
  x = F.adaptive_avg_pool2d(x, [self.pooling_size, self.pooling_size])
301
  else:
302
  x = x