File size: 24,028 Bytes
2af0e94 75854b3 2af0e94 75854b3 2af0e94 75854b3 2af0e94 75854b3 2af0e94 75854b3 2af0e94 75854b3 2af0e94 75854b3 2af0e94 75854b3 2af0e94 75854b3 2af0e94 75854b3 2af0e94 75854b3 2af0e94 75854b3 2af0e94 75854b3 2af0e94 75854b3 2af0e94 75854b3 2af0e94 75854b3 2af0e94 75854b3 2af0e94 75854b3 2af0e94 75854b3 2af0e94 75854b3 2af0e94 75854b3 2af0e94 75854b3 2af0e94 75854b3 2af0e94 75854b3 2af0e94 75854b3 2af0e94 75854b3 2af0e94 75854b3 2af0e94 75854b3 2af0e94 75854b3 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 | import os, sys
ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
sys.path.append(ROOT_DIR)
import gc
import torch
import torchvision
from torch import nn
from torchvision.utils import save_image
from torch.utils.data import DataLoader
from torch.optim import Adam, SGD
from Diffusion.diffuser import DeformDDPM
from Diffusion.networks import get_net, STN
from torchvision.transforms import Lambda
import Diffusion.losses as losses
import random
import glob
import numpy as np
import utils
from tqdm import tqdm
from Dataloader.dataloader0 import get_dataloader
from Dataloader.dataLoader import *
from Dataloader.dataloader_utils import thresh_img
import yaml
import argparse
####################
import torch.multiprocessing as mp
from torch.utils.data.distributed import DistributedSampler
from torch.nn.parallel import DistributedDataParallel as DDP
import torch.distributed as dist
# from torch.distributed import init_process_group
###############
def ddp_setup(rank, world_size):
"""
Args:
rank: Unique identifier of each process
world_size: Total number of processes
"""
os.environ["MASTER_ADDR"] = "localhost"
os.environ["MASTER_PORT"] = "12355"
dist.init_process_group(backend="nccl", rank=rank, world_size=world_size)
torch.cuda.set_device(rank)
use_distributed = True
# use_distributed = False
EPS = 1e-5
MSK_EPS = 0.01
TEXT_EMBED_PROB = 0.7
AUG_RESAMPLE_PROB = 0.5
LOSS_WEIGHTS_DIFF = [2.0, 2.0, 4.0] # [ang, dist, reg]
# LOSS_WEIGHTS_REGIST = [9.0, 1.0, 16.0] # [imgsim, imgmse, ddf]
LOSS_WEIGHTS_REGIST = [1.0, 0.05, 128] # [imgsim, imgmse, ddf]
DIFF_REG_BATCH_RATIO = 2
# AUG_PERMUTE_PROB = 0.35
parser = argparse.ArgumentParser()
# config_file_path = 'Config/config_cmr.yaml'
parser.add_argument(
"--config",
"-C",
help="Path for the config file",
type=str,
# default="Config/config_cmr.yaml",
# default="Config/config_lct.yaml",
default="Config/config_all.yaml",
required=False,
)
args = parser.parse_args()
#=======================================================================================================================
def main_train(rank=0,world_size=1,train_mode_ratio=1,thresh_imgsim=0.01):
if use_distributed:
ddp_setup(rank,world_size)
if torch.distributed.is_initialized():
print(f"World size: {torch.distributed.get_world_size()}")
print(f"Communication backend: {torch.distributed.get_backend()}")
gpu_id = rank
# Load the YAML file into a dictionary
with open(args.config, 'r') as file:
hyp_parameters = yaml.safe_load(file)
print(hyp_parameters)
# epoch_per_save=10
epoch_per_save=hyp_parameters['epoch_per_save']
data_name=hyp_parameters['data_name']
net_name = hyp_parameters['net_name']
Net=get_net(net_name)
suffix_pth=f'_{data_name}_{net_name}.pth'
model_save_path = os.path.join('Models',f'{data_name}_{net_name}/')
model_dir=model_save_path
transformer=utils.get_transformer(img_sz=hyp_parameters["ndims"]*[hyp_parameters['img_size']])
# Data_Loader=get_dataloader(data_name=hyp_parameters['data_name'], mode='train')
# tsfm = torchvision.transforms.Compose([
# torchvision.transforms.ToTensor(),
# ])
# dataset = Data_Loader(target_res = [hyp_parameters["img_size"]]*hyp_parameters["ndims"], transforms=None, noise_scale=hyp_parameters['noise_scale'])
# train_loader = DataLoader(
# dataset,
# batch_size=hyp_parameters['batchsize'],
# # shuffle=False,
# shuffle=True,
# drop_last=True,
# )
# dataset = OminiDataset_v1(transform=None)
dataset = OMDataset_indiv(transform=None)
train_loader = DataLoader(
dataset,
batch_size=hyp_parameters['batchsize'],
shuffle=True,
drop_last=True,
)
# datasetp = OminiDataset_paired(transform=None)
datasetp = OMDataset_pair(transform=None)
train_loader_p = DataLoader(
datasetp,
batch_size=hyp_parameters['batchsize']//DIFF_REG_BATCH_RATIO,
shuffle=True,
drop_last=True,
)
Deformddpm = DeformDDPM(
network=Net(
n_steps=hyp_parameters["timesteps"],
ndims=hyp_parameters["ndims"],
num_input_chn = hyp_parameters["num_input_chn"],
res = hyp_parameters['img_size']
),
n_steps=hyp_parameters["timesteps"],
image_chw=[1] + [hyp_parameters["img_size"]]*hyp_parameters["ndims"],
device=hyp_parameters["device"],
batch_size=hyp_parameters["batchsize"],
img_pad_mode=hyp_parameters["img_pad_mode"],
v_scale=hyp_parameters["v_scale"],
)
ddf_stn = STN(
img_sz=hyp_parameters["img_size"],
ndims=hyp_parameters["ndims"],
# padding_mode="zeros",
padding_mode=hyp_parameters["padding_mode"],
device=hyp_parameters["device"],
)
if use_distributed:
Deformddpm.to(rank)
Deformddpm = DDP(Deformddpm, device_ids=[rank])
ddf_stn.to(rank)
else:
Deformddpm.to(hyp_parameters["device"])
ddf_stn.to(hyp_parameters["device"])
# ddf_stn = DDP(ddf_stn, device_ids=[rank])
# mse = nn.MSELoss()
# loss_reg = losses.Grad(penalty=['l1', 'negdetj'], ndims=hyp_parameters["ndims"])
# loss_reg = losses.Grad(penalty=['l1', 'negdetj', 'range'], ndims=hyp_parameters["ndims"])
loss_reg = losses.Grad(penalty=['l1', 'negdetj', 'range'], ndims=hyp_parameters["ndims"],outrange_thresh=0.2,outrange_weight=1e3)
loss_reg1 = losses.Grad(penalty=['l1', 'negdetj', 'range'], ndims=hyp_parameters["ndims"],outrange_thresh=0.6,outrange_weight=1e3)
loss_dist = losses.MRSE(img_sz=hyp_parameters["img_size"])
# loss_ang = losses.MRSE(img_sz=hyp_parameters["img_size"])
loss_ang = losses.NCC(img_sz=hyp_parameters["img_size"])
# loss_imgsim = losses.LNCC()
loss_imgsim = losses.MSLNCC()
loss_imgmse = losses.LMSE()
optimizer = Adam(Deformddpm.parameters(), lr=hyp_parameters["lr"])
# hyp_parameters["lr"]=0.00000001
# optimizer_regist = Adam(Deformddpm.parameters(), lr=hyp_parameters["lr"]*0.01)
# optimizer_regist = SGD(Deformddpm.parameters(), lr=hyp_parameters["lr"]*0.01, momentum=0.98)
# optimizer = SGD(Deformddpm.parameters(), lr=hyp_parameters["lr"], momentum=0.9)
# # LR scheduler ----- YHM
# scheduler = torch.optim.lr_scheduler.CyclicLR(optimizer, hyp_parameters["lr"], hyp_parameters["lr"]*10, step_size_up=500, step_size_down=500, mode='triangular', gamma=1.0, scale_fn=None, scale_mode='cycle', cycle_momentum=True, base_momentum=0.8, max_momentum=0.9, last_epoch=-1)
# Deformddpm.network.load_state_dict(torch.load('/home/data/jzheng/Adaptive_Motion_Generator-master/models/1000.pth'))
# check for existing models
if not os.path.exists(model_dir):
os.makedirs(model_dir, exist_ok=True)
model_files = glob.glob(os.path.join(model_dir, "*.pth"))
model_files.sort()
if model_files:
if gpu_id == 0:
print(model_files)
initial_epoch, Deformddpm, optimizer = ddp_load_dict(gpu_id, Deformddpm, optimizer, model_files[-1])
else:
initial_epoch = 0
if gpu_id == 0:
print('len_train_data: ',len(dataset))
# Training loop
for epoch in range(initial_epoch,hyp_parameters["epoch"]):
epoch_loss_tot = 0.0
epoch_loss_gen_d = 0.0
epoch_loss_gen_a = 0.0
epoch_loss_reg = 0.0
epoch_loss_regist = 0.0
epoch_loss_imgsim = 0.0
epoch_loss_imgmse = 0.0
epoch_loss_ddfreg = 0.0
# Set model inside to train model
Deformddpm.train()
loss_nan_step = 0 # yu: count the number of nan loss steps
total = min(len(train_loader), len(train_loader_p))
# for step, batch in tqdm(enumerate(train_loader)):
# for step, batch in tqdm(enumerate(train_loader)):
# for step, batch in enumerate(train_loader_omni):
for step, (batch, batch_p) in tqdm(enumerate(zip(train_loader, train_loader_p)), total=total):
# x0, _ = batch
# ==========================================================================
# diffusion train on single image
# x0 = batch # for omni dataset
[x0,embd] = batch # for om dataset
x0 = x0.to(hyp_parameters["device"]).type(torch.float32)
# print('embd:', embd.shape)
if np.random.uniform(0,1)<TEXT_EMBED_PROB:
embd = embd.to(hyp_parameters["device"]).type(torch.float32)
else:
embd = None
n = x0.size()[0] # batch_size -> n
x0 = x0.to(hyp_parameters["device"])
blind_mask = utils.get_random_deformed_mask(x0.shape[2:],apply_possibility=0.6).to(hyp_parameters["device"])
# random deformation + rotation
if hyp_parameters["ndims"]>2:
if np.random.uniform(0,1)<AUG_RESAMPLE_PROB:
x0 = utils.random_resample(x0, deform_scale=0)
# elif np.random.uniform(0,1)<AUG_RESAMPLE_PROB+AUG_PERMUTE_PROB:
else:
[x0] = utils.random_permute([x0], select_dims=[-1,-2,-3])
# x0 = transformer(x0)
if hyp_parameters['noise_scale']>0:
if np.random.uniform(0,1)<AUG_RESAMPLE_PROB:
x0 = thresh_img(x0, [0, 2*hyp_parameters['noise_scale']])
x0 = x0 * (np.random.normal(1, hyp_parameters['noise_scale'] * 1)) + np.random.normal(0, hyp_parameters['noise_scale'] * 1)
# Picking some noise for each of the images in the batch, a timestep and the respective alpha_bars
t = torch.randint(0, hyp_parameters["timesteps"], (n,)).to(
hyp_parameters["device"]
) # pick up a seq of rand number from 0 to 'timestep'
# proc_type = random.choice(['adding', 'independ', 'downsample', 'slice', 'project', 'none', 'uncon', 'uncon', 'uncon'])
proc_type = random.choice(['adding', 'downsample', 'slice', 'slice1', 'none', 'uncon', 'uncon', 'uncon'])
# print('proc_type:', proc_type)
cond_img, _, cond_ratio = Deformddpm.module.proc_cond_img(x0,proc_type=proc_type)
pre_dvf_I,dvf_I = Deformddpm(img_org=x0, t=t, cond_imgs=cond_img, mask=blind_mask,proc_type=[],text=embd) # forward diffusion process
# print(torch.max(torch.abs(pre_dvf_I)))
# print(torch.max(torch.abs(dvf_I)))
loss_tot=0
loss_ddf = loss_reg(pre_dvf_I,img=x0)
trm_pred = ddf_stn(pre_dvf_I, dvf_I)
loss_gen_d = loss_dist(pred=trm_pred,inv_lab=dvf_I,ddf_stn=None,mask=blind_mask)
loss_gen_a = loss_ang(pred=trm_pred,inv_lab=dvf_I,ddf_stn=None,mask=blind_mask)
loss_tot += LOSS_WEIGHTS_DIFF[0] * loss_gen_a + LOSS_WEIGHTS_DIFF[1] * loss_gen_d
loss_tot += LOSS_WEIGHTS_DIFF[2] * loss_ddf
loss_tot = torch.sqrt(1.+MSK_EPS-cond_ratio) * loss_tot
# >> JZ: print nan in x0
if torch.isnan(x0).any():
print(f"*** Encountered NaN in input image x0 at epoch {epoch}, step {step}.")
# >> JZ: print loss of ddf
if loss_ddf>0.001:
print(f"*** High diffusion DDF loss at epoch {epoch}, step {step}: {loss_ddf.item()}.")
# yu: check if loss_tot==nan or inf
if torch.isnan(loss_tot) or torch.isinf(loss_tot):
print(f"*** Encountered NaN or Inf loss at epoch {epoch}, step {step}. Skipping this batch.")
loss_nan_step += 1
continue
if loss_nan_step > 5:
print(f"*** Too many NaN or Inf losses ({loss_nan_step} times) at epoch {epoch}, step {step}. Stopping training.")
raise ValueError("Too many NaN losses detected in loss_tot. Code terminated.")
optimizer.zero_grad()
loss_tot.backward()
optimizer.step()
epoch_loss_tot += loss_tot.item() / total
epoch_loss_gen_d += loss_gen_d.item() / total
epoch_loss_gen_a += loss_gen_a.item() / total
epoch_loss_reg += loss_ddf.item() / total
# print(loss_gen_a.item())
# if 0:
# if loss_gen_a.item() < -0.3 and step%train_mode_ratio == 0:
if step%train_mode_ratio == 0:
# ==========================================================================
# registration train on paired images
# x1, y1 = next(iter(train_loader_p))
# [x1, y1, _, embd_y] = next(iter(train_loader_p))
[x1, y1, _, embd_y] = batch_p
if np.random.uniform(0,1)<TEXT_EMBED_PROB:
# embd_x = embd_x.to(hyp_parameters["device"]).type(torch.float32)
embd_y = embd_y.to(hyp_parameters["device"]).type(torch.float32)
else:
# embd_x = None
embd_y = None
x1 = x1.to(hyp_parameters["device"]).type(torch.float32)
y1 = y1.to(hyp_parameters["device"]).type(torch.float32)
n = x1.size()[0] # batch_size -> n
# random deformation + rotation
# if hyp_parameters["ndims"]>2:
# if np.random.uniform(0,1)<0.6:
# x1 = utils.random_resample(x1, deform_scale=0)
# y1 = utils.random_resample(y1, deform_scale=0)
# x1 = transformer(x1)
# y1 = transformer(y1)
[x1, y1] = utils.random_permute([x1, y1], select_dims=[-1,-2,-3])
if hyp_parameters['noise_scale']>0:
[x1, y1] = thresh_img([x1, y1], [0, 2*hyp_parameters['noise_scale']])
random_scale = np.random.normal(1, hyp_parameters['noise_scale'] * 1)
random_shift = np.random.normal(0, hyp_parameters['noise_scale'] * 1)
x1 = x1 * random_scale + random_shift
y1 = y1 * random_scale + random_shift
# x1 = thresh_img(x1, [0, 2*hyp_parameters['noise_scale']])
# x1 = x1 * (np.random.normal(1, hyp_parameters['noise_scale'] * 1)) + np.random.normal(0, hyp_parameters['noise_scale'] * 1)
# y1 = thresh_img(y1, [0, 2*hyp_parameters['noise_scale']])
# y1 = y1 * (np.random.normal(1, hyp_parameters['noise_scale'] * 1)) + np.random.normal(0, hyp_parameters['noise_scale'] * 1)
# # Picking some noise for each of the images in the batch, a timestep and the respective alpha_bars
# t = torch.randint(0, hyp_parameters["timesteps"], (n,)).to(
# hyp_parameters["device"]
# ) # pick up a seq of rand number from 0 to 'timestep'
# scale_regist = np.random.uniform(0.6,1.)
# T_regist = sorted(random.sample(range(0, int(hyp_parameters["timesteps"] * scale_regist) + 1), 16), reverse=True)
# print('T_regist (0.6,1) sampling range:', T_regist)
scale_regist = np.random.uniform(0.0,0.7)
select_timestep = np.random.randint(8, 17) # select a random number of timesteps to sample, between 8 and 16
T_regist = sorted(random.sample(range(int(hyp_parameters["timesteps"] * scale_regist),hyp_parameters["timesteps"]), select_timestep), reverse=True)
# print('T_regist (0.1,0.7) sampling range:', T_regist)
# scale_regist = np.random.uniform(0.4,1.)
# T_regist = [int(hyp_parameters["timesteps"]*scale_regist)]
# scale_regist = np.random.uniform(0.6,1.)
# init_T = int(hyp_parameters["timesteps"] * scale_regist)
# T_regist = sorted(random.sample(range(0, int(hyp_parameters["timesteps"] * scale_regist)), 2)+list(range(init_T,hyp_parameters["timesteps"]+1)), reverse=True)
T_regist = [[t for _ in range(hyp_parameters["batchsize"]//2)] for t in T_regist]
# print('T_regist:', T_regist)
# proc_type = random.choice(['adding', 'independ', 'downsample', 'slice', 'project', 'none', 'none'])
proc_type = random.choice(['downsample', 'slice', 'slice1', 'none', 'none'])
# proc_type = random.choice(['project'])
y1_proc, msk_tgt, cond_ratio = Deformddpm.module.proc_cond_img(y1,proc_type=proc_type)
msk_tgt = msk_tgt+MSK_EPS
[ddf_comp,ddf_rand],[img_rec,img_diff,img_save],_ = Deformddpm(img_org=x1, cond_imgs=y1_proc, T=[None, T_regist], proc_type=[],text=embd_y) # forward diffusion process
loss_sim = loss_imgsim(img_rec, y1, label=msk_tgt*(y1>thresh_imgsim)) # calculate loss for the registration process
loss_mse = loss_imgmse(img_rec, y1, label=msk_tgt*(y1>=0.0)) # calculate loss for the registration process
# loss_ddf1 = loss_reg1(ddf_comp,img=y1,msk=(msk_tgt+MSK_EPS)) # calculate loss for the registration process
# loss_sim = loss_imgsim(img_rec, y1, label=(y1>thresh_imgsim)) # calculate loss for the registration process
# loss_mse = loss_imgmse(img_rec, y1, label=(y1>=0.0)) # calculate loss for the registration process
loss_ddf1 = loss_reg1(ddf_comp, img=y1) # calculate loss for the registration process
loss_regist = 0
loss_regist += LOSS_WEIGHTS_REGIST[0] * loss_sim
loss_regist += LOSS_WEIGHTS_REGIST[1] * loss_mse
loss_regist += LOSS_WEIGHTS_REGIST[2] * loss_ddf1
# print('proc_type:', proc_type, 'cond_ratio:', cond_ratio.item())
# print('loss_regist:', loss_regist.item(), 'loss_sim:', loss_sim.item(), 'loss_ddf1:', loss_ddf1.item())
# >> JZ: print nan in x0
if torch.isnan(x0).any():
print(f"*** Encountered NaN in input image x0 at epoch {epoch}, step {step}.")
# >> JZ: print loss of ddf
if loss_ddf1>0.002:
print(f"*** High registration DDF loss at epoch {epoch}, step {step}: {loss_ddf1.item()}.")
# # Print gradients for each parameter
# for name, param in Deformddpm.named_parameters():
# if param.grad is not None:
# print(f"Gradient for {name}: {param.grad.norm()}")
# else:
# print(f"Gradient for {name}: None")
loss_regist = torch.sqrt(cond_ratio+MSK_EPS) *loss_regist
optimizer.zero_grad()
loss_regist.backward()
torch.nn.utils.clip_grad_norm_(Deformddpm.parameters(), max_norm=0.2)
optimizer.step()
epoch_loss_regist += loss_regist.item() / total
epoch_loss_imgsim += loss_sim.item() / total
epoch_loss_imgmse += loss_mse.item() / total
epoch_loss_ddfreg += loss_ddf1.item() / total
if step % 10 == 0:
print('step:',step,':', loss_tot.item(),'=',loss_gen_a.item(),'+', loss_gen_d.item(),'+',loss_ddf.item())
print(f' loss_regist: {loss_regist} = {loss_sim} (imgsim) + {loss_mse} (imgmse) + {loss_ddf1} (ddf)')
# break # FOR TESTING
# else:
# print('loss_gen_a:',loss_gen_a.item()) # FOR TESTING
# pass
if 1:
# if gpu_id == 0:
print('==================')
print(epoch,':', epoch_loss_tot,'=',epoch_loss_gen_a,'+', epoch_loss_gen_d,'+',epoch_loss_reg, ' (ang+dist+regul)')
print(f' loss_regist: {epoch_loss_regist} = {epoch_loss_imgsim} (imgsim) + {epoch_loss_imgmse} (imgmse) + {epoch_loss_ddfreg} (ddf)')
print('==================')
# # LR schedular step ----- YHM
# scheduler.step()
if 0 == epoch % epoch_per_save:
save_dir=model_save_path + str(epoch).rjust(6, '0') + suffix_pth
os.makedirs(os.path.dirname(model_save_path), exist_ok=True)
# break # FOR TESTING
if not use_distributed:
print(f"saved in {save_dir}")
# torch.save(Deformddpm.state_dict(), save_dir)
torch.save({
'model_state_dict': Deformddpm.state_dict(),
'optimizer_state_dict': optimizer.state_dict(),
'epoch': epoch
}, save_dir)
elif gpu_id == 0:
print(f"saved in {save_dir}")
# torch.save(Deformddpm.module.state_dict(), save_dir)
torch.save({
'model_state_dict': Deformddpm.module.state_dict(),
'optimizer_state_dict': optimizer.state_dict(),
'epoch': epoch
}, save_dir)
# Resource cleanup at the end of training
torch.cuda.empty_cache()
gc.collect()
if use_distributed and dist.is_initialized():
dist.destroy_process_group()
def ddp_load_dict(gpu_id, Deformddpm, optimizer, model_file,use_distributed=True, load_strict=False):
if gpu_id == 0:
# if 0:
utils.print_memory_usage("Before Loading Model")
if 1:
gc.collect()
torch.cuda.empty_cache()
# Deformddpm.network.load_state_dict(torch.load(latest_model_file))
# Deformddpm.load_state_dict(torch.load(latest_model_file), strict=False)
checkpoint = torch.load(model_file)
# checkpoint = torch.load(latest_model_file, map_location=f"cuda:{rank}")
if use_distributed:
Deformddpm.module.load_state_dict(checkpoint['model_state_dict'], strict=load_strict)
else:
Deformddpm.load_state_dict(checkpoint['model_state_dict'], strict=load_strict)
if load_strict:
optimizer.load_state_dict(checkpoint['optimizer_state_dict'])
utils.print_memory_usage("After Loading Checkpoint on GPU")
if use_distributed:
# Broadcast model weights from rank 0 to all other GPUs
dist.barrier()
for param in Deformddpm.parameters():
dist.broadcast(param.data, src=0) # Synchronize model across ranks
dist.barrier()
for param_group in optimizer.param_groups:
for param in param_group['params']:
if param.grad is not None:
dist.broadcast(param.grad, src=0) # Sync optimizer gradients
# initial_epoch = checkpoint['epoch'] + 1
# get the epoch number from the filename and add 1 to set as initial_epoch
initial_epoch = int(os.path.basename(model_file).split('.')[0][:6]) + 1
return initial_epoch, Deformddpm, optimizer
if __name__ == "__main__":
if use_distributed:
world_size = torch.cuda.device_count()
print(f"Distributed GPU number = {world_size}")
mp.spawn(main_train,args = (world_size,),nprocs = world_size)
else:
main_train(0,1) |