-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'dev/0.5.4' into 'master'
Begin netharn 0.5.4 See merge request computer-vision/netharn!4
- Loading branch information
Showing
46 changed files
with
2,158 additions
and
629 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
|
||
def main(): | ||
import netharn as nh | ||
import ubelt as ub | ||
|
||
model = nh.layers.Sequential(*[ | ||
nh.layers.ConvNormNd(2, 3, 1), | ||
# nh.layers.ConvNormNd(2, 1, 1), | ||
# nh.layers.ConvNormNd(2, 1, 1), | ||
]) | ||
|
||
params = dict(model.named_parameters()) | ||
param_keys = set(params) | ||
key_groups = {} | ||
other_keys = param_keys.copy() | ||
if 1: | ||
key_groups['norm'] = {p for p in other_keys if p.endswith(('.norm.weight', '.norm.weight'))} | ||
other_keys -= key_groups['norm'] | ||
if 1: | ||
key_groups['bias'] = {p for p in other_keys if p.endswith('.bias')} | ||
other_keys -= key_groups['bias'] | ||
if 1: | ||
key_groups['weight'] = {p for p in other_keys if p.endswith('.weight')} | ||
other_keys -= key_groups['weight'] | ||
key_groups['other'] = other_keys | ||
|
||
named_param_groups = {} | ||
for group_name, keys in key_groups.items(): | ||
if keys: | ||
param_group = {} | ||
param_group['params'] = list(ub.dict_subset(params, keys).values()) | ||
named_param_groups[group_name] = param_group | ||
|
||
if 'bias' in named_param_groups: | ||
named_param_groups['bias']['weight_decay'] = 0 | ||
if 'norm' in named_param_groups: | ||
named_param_groups['norm']['weight_decay'] = 0 | ||
|
||
import torch | ||
param_groups = list(named_param_groups.values()) | ||
|
||
optim_defaults = { | ||
'lr': 1e-3, | ||
'weight_decay': 1e1, | ||
} | ||
optim = torch.optim.AdamW(param_groups, **optim_defaults) | ||
|
||
learn = True | ||
|
||
model = model.train(learn) | ||
import time | ||
|
||
with torch.set_grad_enabled(learn): | ||
for i in range(10000): | ||
|
||
if learn: | ||
optim.zero_grad() | ||
inputs = torch.rand(3, 3, 2, 2) | ||
outputs = model(inputs) | ||
target = outputs.data.detach() | ||
# target = target * 1.0001 | ||
target = torch.rand(3, 1, 2, 2) * 1e-3 | ||
# target.fill_(0) | ||
loss = ((outputs - target) ** 2).sum() | ||
|
||
if learn: | ||
loss.backward() | ||
optim.step() | ||
optim.zero_grad() | ||
# print(ub.repr2(named_param_groups, nl=2)) | ||
state = model.state_dict() | ||
state = ub.dict_diff(state, params) | ||
time.sleep(0.01) | ||
print('loss = {!r}'.format(float(loss.item()))) | ||
print('param_state = ' + ub.repr2(params) + '\n' + | ||
'buffer_state = ' + ub.repr2(state, nl=3)) | ||
|
||
time.sleep(0.1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
|
||
|
||
def debug_optimizer(harn, snapshot_state): | ||
""" | ||
debuging an issue where the param groups were created in different orders | ||
each time. | ||
""" | ||
if False: | ||
# DEBUG: check that all optimizer params exist in the model | ||
self = harn.optimizer | ||
state_dict = snapshot_state['optimizer_state_dict'] | ||
for param_group in harn.optimizer.param_groups: | ||
print('-----') | ||
print(param_group['weight_decay']) | ||
print('-----') | ||
for p in param_group['params']: | ||
|
||
# Find the model param that correspond to this | ||
found = None | ||
for name, mp in harn.model.named_parameters(): | ||
if mp is p: | ||
found = name | ||
break | ||
|
||
assert found is not None | ||
print('found = {!r}'.format(found)) | ||
|
||
state = self.state[p] | ||
if state: | ||
avg_shape = tuple(state['exp_avg'].shape) | ||
p_shape = tuple(p.shape) | ||
if avg_shape == p_shape: | ||
print('avg_shape = {!r}'.format(avg_shape)) | ||
else: | ||
print('p_shape = {!r}'.format(p_shape)) | ||
print('avg_shape = {!r}'.format(avg_shape)) | ||
|
||
if 0: | ||
self = harn.optimizer | ||
for param_group in harn.optimizer.param_groups: | ||
for p in param_group['params']: | ||
print(p.grad is None) | ||
|
||
for n, mp in harn.model.named_parameters(): | ||
assert mp.requires_grad | ||
if mp.grad is not None: | ||
mp.grad.detach_() | ||
mp.grad.zero_() | ||
|
||
batch = harn._demo_batch() | ||
outputs = harn.model(batch['im']) | ||
loss = outputs['class_energy'].mean() | ||
|
||
harn.optimizer.zero_grad() | ||
loss.backward() | ||
|
||
for param_group in harn.optimizer.param_groups: | ||
for param in param_group['params']: | ||
if param.grad is None: | ||
found = None | ||
for name, mp in harn.model.named_parameters(): | ||
if mp is p: | ||
found = name | ||
break | ||
print('no grad for found = {!r}'.format(found)) | ||
|
||
harn.optimizer.step() | ||
|
||
if 0: | ||
snapshot_state_old = harn.get_snapshot_state() | ||
torch.save(snapshot_state_old, 'foo.pt') | ||
snapshot_state = harn.xpu.load('foo.pt') | ||
|
||
prev_states = harn.prev_snapshots() | ||
snapshot_state = harn.xpu.load(prev_states[-1]) | ||
|
||
snapshot_state_old['optimizer_state_dict']['state'].keys() | ||
snapshot_state['optimizer_state_dict']['state'].keys() | ||
state_dict = snapshot_state['optimizer_state_dict'] | ||
|
||
for id, state in state_dict['state'].items(): | ||
pass | ||
|
||
for group in self.param_groups: | ||
for param in group['params']: | ||
print(param.shape) | ||
|
||
for group in state_dict['param_groups']: | ||
for paramid in group['params']: | ||
state = state_dict['state'][paramid] | ||
print(state['exp_avg'].shape) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.