Skip to content

Commit

Permalink
remove redundant comments
Browse files Browse the repository at this point in the history
  • Loading branch information
chenyingshu committed Feb 6, 2025
1 parent 62d1021 commit ddb60b6
Show file tree
Hide file tree
Showing 18 changed files with 28 additions and 163 deletions.
40 changes: 3 additions & 37 deletions examples/openlrm/configs/train-sample-base.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
experiment:
type: lrm
# seed: 42 # args.seed
parent: lrm-objaverse
child: base

Expand Down Expand Up @@ -40,47 +39,14 @@ dataset:
pin_mem: true

train:
# mixed_precision: bf16 # REPLACE THIS BASED ON GPU TYPE
find_unused_parameters: false
loss:
pixel_weight: 1.0
perceptual_weight: 1.0
tv_weight: 5e-4
optim:
# lr: 4e-4 # args.start_learning_rate
# weight_decay: 0.05 #args.weight_decay
# beta1: 0.9 #args.betas
# beta2: 0.95 #args.betas
# clip_grad_norm: 1.0 # args.max_grad_norm
scheduler:
# type: cosine_annealing_warm_restarts_lr # args.scheduler
# warmup_real_iters: 300 # args.warmup_steps
batch_size: 1 # REPLACE THIS
accum_steps: 1 # REPLACE THIS
epochs: 10000 # 60 # REPLACE THIS
debug_global_steps: null
batch_size: 1

val:
batch_size: 1 #4
global_step_period: 100 #1000
batch_size: 1
global_step_period: 1000
debug_batches: null

saver:
auto_resume: true
load_model: null
# checkpoint_root: ./exps/checkpoints # args.output_path
# checkpoint_global_steps: 100 #1000 # args.ckpt_save_steps(unit:step) / args.ckpt_save_interval (unit: epoch)
checkpoint_keep_level: 5

# Not to use
logger:
stream_level: WARNING
log_level: INFO
log_root: ./exps/logs
tracker_root: ./exps/trackers
enable_profiler: false
trackers:
- tensorboard
image_monitor:
train_global_steps: 100
samples_per_log: 4
48 changes: 7 additions & 41 deletions examples/openlrm/configs/train-sample-large.yaml
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
experiment:
type: lrm
# seed: 42 # args.seed
parent: lrm-objaverse
child: large

model:
camera_embed_dim: 1024
rendering_samples_per_ray: 96 # 128
transformer_dim: 768 # 1024
transformer_layers: 12 # 16
transformer_heads: 12 # 16
rendering_samples_per_ray: 96 # w/ larger memory use: 128
transformer_dim: 768 # w/ larger memory use: 1024
transformer_layers: 12 # w/ larger net use: 16
transformer_heads: 12 # w/ larger memory use: 16
triplane_low_res: 32
triplane_high_res: 64
triplane_dim: 80
Expand Down Expand Up @@ -40,47 +39,14 @@ dataset:
pin_mem: true

train:
# mixed_precision: bf16 # REPLACE THIS BASED ON GPU TYPE
find_unused_parameters: false
loss:
pixel_weight: 1.0
perceptual_weight: 1.0
tv_weight: 5e-4
optim:
# lr: 4e-4 # args.start_learning_rate
# weight_decay: 0.05 #args.weight_decay
# beta1: 0.9 #args.betas
# beta2: 0.95 #args.betas
# clip_grad_norm: 1.0 # args.max_grad_norm
scheduler:
# type: cosine_annealing_warm_restarts_lr # args.scheduler
# warmup_real_iters: 300 # args.warmup_steps
batch_size: 1 # REPLACE THIS
accum_steps: 1 # REPLACE THIS
epochs: 10000 # 60 # REPLACE THIS
debug_global_steps: null
batch_size: 1

val:
batch_size: 1 #4
global_step_period: 100 #1000
batch_size: 1
global_step_period: 1000
debug_batches: null

saver:
auto_resume: true
load_model: null
# checkpoint_root: ./exps/checkpoints # args.output_path
# checkpoint_global_steps: 100 #1000 # args.ckpt_save_steps(unit:step) / args.ckpt_save_interval (unit: epoch)
checkpoint_keep_level: 5

# Not to use
logger:
stream_level: WARNING
log_level: INFO
log_root: ./exps/logs
tracker_root: ./exps/trackers
enable_profiler: false
trackers:
- tensorboard
image_monitor:
train_global_steps: 100
samples_per_log: 4
40 changes: 3 additions & 37 deletions examples/openlrm/configs/train-sample-small.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
experiment:
type: lrm
# seed: 42 # args.seed
parent: lrm-objaverse
child: small

Expand Down Expand Up @@ -40,47 +39,14 @@ dataset:
pin_mem: true

train:
# mixed_precision: bf16 # REPLACE THIS BASED ON GPU TYPE
find_unused_parameters: false
loss:
pixel_weight: 1.0
perceptual_weight: 1.0
tv_weight: 5e-4
optim:
# lr: 4e-4 # args.start_learning_rate
# weight_decay: 0.05 #args.weight_decay
# beta1: 0.9 #args.betas
# beta2: 0.95 #args.betas
# clip_grad_norm: 1.0 # args.max_grad_norm
scheduler:
# type: cosine_annealing_warm_restarts_lr # args.scheduler
# warmup_real_iters: 300 # args.warmup_steps
batch_size: 1 # REPLACE THIS
accum_steps: 1 # REPLACE THIS
epochs: 10000 # 60 # REPLACE THIS
debug_global_steps: null
batch_size: 1

val:
batch_size: 1 #4
global_step_period: 100 #1000
batch_size: 1
global_step_period: 1000
debug_batches: null

saver:
auto_resume: true
load_model: null
# checkpoint_root: ./exps/checkpoints # args.output_path
# checkpoint_global_steps: 100 #1000 # args.ckpt_save_steps(unit:step) / args.ckpt_save_interval (unit: epoch)
checkpoint_keep_level: 5

# Not to use
logger:
stream_level: WARNING
log_level: INFO
log_root: ./exps/logs
tracker_root: ./exps/trackers
enable_profiler: false
trackers:
- tensorboard
image_monitor:
train_global_steps: 100
samples_per_log: 4
10 changes: 3 additions & 7 deletions examples/openlrm/eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@

from mindone.utils.logger import set_logger

# __dir__ = os.path.dirname(os.path.abspath(__file__))
# sys.path.insert(0, os.path.abspath(os.path.join(__dir__, "../.."))) # for loading mindone


logger = logging.getLogger(__name__)


Expand Down Expand Up @@ -99,7 +95,6 @@ def load_data(root_dirs, uid, render_image_res, num_all_views=32, normalize_came
def evaluate(args):
save_dir = args.output_path

# device_num = 1
rank_id = 0
ms.set_context(
mode=args.mode,
Expand Down Expand Up @@ -202,13 +197,12 @@ def evaluate(args):
)[
0
] # only batch=1 [M, C, H, W]
# print(f"render_images {render_images.shape}")

batch_time = time.time() - start_time
batches_time.append(batch_time)
logger.info("Batch time cost: %.3fs.", batch_time)

target_images = data_batch["target_images"] # [M, C, H, W]
# print(f"target_images {target_images.shape}")

for view_id, (target, pred) in enumerate(zip(target_images, render_images)):
target = target.asnumpy()
Expand All @@ -219,6 +213,8 @@ def evaluate(args):
# save mviews outputs
im = Image.fromarray(np.clip(pred.transpose(1, 2, 0) * 255, 0, 255).astype(np.uint8))
im.save(os.path.join(image_path, f"{uids[index]}_{view_id:03d}.jpg"))

# if save target image:
# im = Image.fromarray(np.clip(target.transpose(1, 2, 0) * 255, 0, 255).astype(np.uint8))
# im.save(os.path.join(image_path, f"{uids[index]}_{view_id:03d}_target.jpg"))

Expand Down
4 changes: 0 additions & 4 deletions examples/openlrm/openlrm/datasets/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ def __init__(self, root_dirs: list[str], meta_path: str):
super().__init__()
self.root_dirs = root_dirs
self.uids = self._load_uids(meta_path)
# self.uids = self.uids[:1] #TODO: debug use, delete later
print(f"uids: {self.uids}")

def __len__(self):
return len(self.uids)
Expand Down Expand Up @@ -66,9 +64,7 @@ def _load_rgba_image(file_path, bg_color: float = 1.0, resize=None, crop_pos=Non
rgba = vision.Resize([resize, resize], Inter.BICUBIC)(rgba)
if (crop_pos is not None) and (crop_size is not None): # rand crop
assert (crop_pos[0] + crop_size <= rgba.shape[0]) and (crop_pos[1] + crop_size <= rgba.shape[1])
# print(f"crop rgba: {rgba.shape}")
rgba = vision.Crop(crop_pos, crop_size)(rgba)
# print(f"crop_pos {crop_pos}, crop_size {crop_size} cropped rgba {rgba.shape}")

# convert to Tensor, in shape [B, C, H, W]
rgba = ms.Tensor(rgba).float() / 255.0
Expand Down
3 changes: 0 additions & 3 deletions examples/openlrm/openlrm/launch.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# import os, sys
# sys.path.insert(0, os.path.abspath("../..")) # for mindone

import argparse

from openlrm.runners import REGISTRY_RUNNERS
Expand Down
4 changes: 1 addition & 3 deletions examples/openlrm/openlrm/losses/lpips.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,9 @@ def construct(self, input, target, normalize=False):
else:
in0_input, in1_input = input, target
outs0, outs1 = self.net(in0_input), self.net(in1_input)
val = 0 # ms.Tensor(0, dtype=input.dtype)
val = 0
for kk in range(len(self.chns)):
diff = (normalize_tensor(outs0[kk]) - normalize_tensor(outs1[kk])) ** 2
# res += spatial_average(lins[kk](diff), keepdim=True)
# lin_layer = lins[kk]
val += ops.mean(self.lins[kk](diff), axis=[2, 3], keep_dims=True)
return val

Expand Down
15 changes: 0 additions & 15 deletions examples/openlrm/openlrm/models/encoders/dino.py
Original file line number Diff line number Diff line change
Expand Up @@ -423,34 +423,19 @@ def _init_weights(self, module: Union[nn.Dense, nn.Conv2d, nn.LayerNorm]) -> Non
TruncatedNormal(sigma=self.config.initializer_range, mean=0.0), module.weight.data.shape, ms.float32
)
)
# module.weight.data = nn.init.trunc_normal_(
# module.weight.data.to(ms.float32), mean=0.0, std=self.config.initializer_range
# ).to(module.weight.dtype)
if module.bias is not None:
module.bias.set_data(initializer("Zero", module.bias.shape, ms.float32))
# module.bias.data.zero_()
elif isinstance(module, nn.LayerNorm):
module.beta.data.set_data(initializer("Zero", module.beta.data.shape, ms.float32))
module.gamma.data.set_data(initializer("One", module.gamma.data.shape, ms.float32))
elif isinstance(module, ViTEmbeddings):
# module.position_embeddings.data = nn.init.trunc_normal_(
# module.position_embeddings.data.to(ms.float32),
# mean=0.0,
# std=self.config.initializer_range,
# ).to(module.position_embeddings.dtype)
module.position_embeddings.data.set_data(
initializer(
TruncatedNormal(sigma=self.config.initializer_range, mean=0.0),
module.position_embeddings.data.shape,
ms.float32,
)
)

# module.cls_token.data = nn.init.trunc_normal_(
# module.cls_token.data.to(ms.float32),
# mean=0.0,
# std=self.config.initializer_range,
# ).to(module.cls_token.dtype)
module.cls_token.data.set_data(
initializer(
TruncatedNormal(sigma=self.config.initializer_range, mean=0.0),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
# Avoid using nested tensor for now, deprecating usage of NestedTensorBlock
from .block import Block, BlockWithModulation

# from .dino_head import DINOHead #not use
# from .dino_head import DINOHead # not use
from .mlp import Mlp
from .patch_embed import PatchEmbed
from .swiglu_ffn import SwiGLUFFN, SwiGLUFFNFused
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def construct(self, x: Tensor, attn_bias=None) -> Tensor:
v = v.swapaxes(1, 2)
# 'b n h d' -> (b, h=num_head, n, d) == BNSD

if attn_bias is not None: # TODO
if attn_bias is not None: # TODO & FIXME: add this function
raise AssertionError(f"Do not support MemEffAttention in Dinov2 with attn_bias {attn_bias} yet!")
# q_bias, k_bias, v_bias = attn_bias.chunk(3)
# q = q + q_bias
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def __init__(
ffn_layer: Callable[..., nn.Cell] = Mlp,
) -> None:
super().__init__()
# print(f"biases: qkv: {qkv_bias}, proj: {proj_bias}, ffn: {ffn_bias}")

self.norm1 = norm_layer((dim,))
self.attn = attn_class(
dim,
Expand Down Expand Up @@ -95,7 +95,7 @@ def ffn_residual_func(x: Tensor) -> Tensor:
)
elif self.training and self.sample_drop_ratio > 0.0:
x = x + self.drop_path1(attn_residual_func(x))
x = x + self.drop_path1(ffn_residual_func(x)) # FIXME: drop_path2
x = x + self.drop_path1(ffn_residual_func(x))
else:
x = x + attn_residual_func(x)
x = x + ffn_residual_func(x)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@ def __init__(
nlayers = max(nlayers, 1)
self.mlp = _build_mlp(nlayers, in_dim, bottleneck_dim, hidden_dim=hidden_dim, use_bn=use_bn, bias=mlp_bias)
self.apply(self._init_weights)
# from torch.nn.utils import weight_norm # Deprecated
# self.last_layer = weight_norm(nn.Linear(bottleneck_dim, out_dim, has_bias=False))
# self.last_layer.weight_g.set_data(initializer("ones"))
self.last_layer = nn.Dense(bottleneck_dim, out_dim, has_bias=False)
self.last_layer.weight.set_data(initializer("ones"))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def construct(self, x: Tensor) -> Tensor:

x = self.proj(x) # B C H W
H, W = x.shape[2], x.shape[3]
x = x.flatten(start_dim=2).swapaxes(1, 2) # B HW C
x = x.flatten(start_dim=2).swapaxes(1, 2) # B H W C
x = self.norm(x)
if not self.flatten_embedding:
x = x.reshape(-1, H, W, self.embed_dim) # B H W C
Expand Down
5 changes: 4 additions & 1 deletion examples/openlrm/openlrm/models/modeling_lrm.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,4 +245,7 @@ def construct(
render_results["images_rgb"].shape[1] == M
), "Number of rendered views should be consistent with render_cameras"

return (planes, render_results["images_rgb"]) # keys also includes 'images_depth', 'images_weight' which are never used.
return (
planes,
render_results["images_rgb"],
) # keys also include 'images_depth', 'images_weight' which are never used.
2 changes: 1 addition & 1 deletion examples/openlrm/openlrm/runners/train/lrm.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ def _build_model(self, cfg):

# Build training utils: lr, optim, callbacks, trainer
def _build_utils(self, args, cfg, dataset_size):
total_train_steps = cfg.train.epochs
total_train_steps = args.epochs
# build learning rate scheduler
if not args.decay_steps:
args.decay_steps = total_train_steps - args.warmup_steps # fix lr scheduling
Expand Down
5 changes: 0 additions & 5 deletions examples/openlrm/openlrm/utils/hf_hub.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@

from mindone.safetensors.mindspore import load_file

# from huggingface_hub.utils import validate_hf_hub_args


def wrap_model_hub(model_cls: nn.Cell):
"""
Expand Down Expand Up @@ -136,9 +134,6 @@ def _from_pretrained(
)

print(f"state_dict.dtype {state_dict[loaded_keys[0]].dtype}") # float32
# print(f"model.dtype {model.dtype}")
# if state_dict[loaded_keys[0]].dtype != model.dtype:
# model = model.to(state_dict[loaded_keys[0]].dtype)

# Instantiate the model
param_not_load, ckpt_not_load = ms.load_param_into_net(model, state_dict, strict_load=strict)
Expand Down
Loading

0 comments on commit ddb60b6

Please sign in to comment.