小武实习的debug日记

OSError: Unable to open file (unable to open file: name = ‘GWD_DATA/DT3/train/4G\u4e2d\u56fd\u8054\u901a/gl_20190626000190230001.h5’, errno = 2, error message = ‘No such file or directory’, flags = 0, o_flags = 0)

路径中有中文,该怎么办呢?
python3的解决办法:字符串.encode(‘utf-8’).decode(‘unicode_escape’)
python2:字符串.decode(‘unicode_escape’)

小武与随机种子数的

np.random.seed(args.seed)
torch.manual_seed(args.seed)
if torch.cuda.is_available():
if not args.cuda:
print(‘WARNING: You have a CUDA device, so you should probably run with --cuda’)
else:
torch.cuda.manual_seed_all(args.seed)

小武与cudnn在训练中相遇:

torch.backends.cudnn.benchmark = true

总的来说,大部分情况下,设置这个 flag 可以让内置的 cuDNN 的 auto-tuner 自动寻找最适合当前配置的高效算法,来达到优化运行效率的问题。

一般来讲,应该遵循以下准则:

如果网络的输入数据维度或类型上变化不大,设置 torch.backends.cudnn.benchmark = true 可以增加运行效率;
如果网络的输入数据在每次 iteration 都变化的话,会导致 cnDNN 每次都会去寻找一遍最优配置,这样反而会降低运行效率。
这下就清晰明了很多了。

不同的层用不同的学习率:
ignored_params = list(map(id, model.add_block.parameters()))
base_params = filter(lambda p: id§ not in ignored_params, model.parameters())

optimizer = optim.SGD(
[
{‘params’: base_params, ‘lr’: 0。01},
{‘params’: model.add_block.parameters(), ‘lr’: 0.1},
]
weight_decay=1e-5, momentum=0.9, nesterov=True)

扫描二维码关注公众号,回复: 6731613 查看本文章

冻结某些层:当有预训练模型的时候:

ignored_params = list(map(id, model.layer3.parameters()))#+list(map(id, model.saliency.parameters()))
base_params = filter(lambda p: id(p) not in ignored_params, model.parameters())
last_params = filter(lambda p: id(p) in ignored_params, model.parameters())
for p in base_params:
    p.requires_grad = False
for p in last_params:
    p.requires_grad = True

小武与模型保存:

             if use_gpu:
                state_dict = model.module.state_dict()
            else:
                state_dict = model.state_dict()
            
            state_dict = {k: v for k, v in state_dict.items() if 'classifier' not in k}

            save_checkpoint({
                'state_dict': state_dict,
                #                'optimizer' : optimizer.state_dict(),
                'epoch': epoch,
            }, False, osp.join(args.save_dir,'checkpoint_ep' + str(epoch + 1) + '_step' + str(batch_idx + 1) + '.pth.tar'))

文件重新命名:
srcpath = ‘/mnt/hgfs/share/DT2/query’
a = 81002

for root,dirs,files in os.walk(srcpath):
a += 1
b = 0
for imagefile in files[0::2]:
b += 1
jsonfile = files[files.index(imagefile)+1]
oldimage = root + ‘/’ + imagefile
newimage = root + ‘/’ + ‘20190626000’ + str(a) + ‘000’ + str(b) + ‘.jpg’
oldjson = root + ‘/’ + jsonfile
newjson = root + ‘/’ + ‘20190626000’ + str(a) + ‘000’ + str(b) + ‘.json’
os.rename(oldimage,newimage)
os.rename(oldjson,newjson)

猜你喜欢

转载自blog.csdn.net/weixin_37721058/article/details/94548349