Exemplo n.º 1
0
 def from_weights_file(filename, train=False, cuda=False):
     filters, blocks, weights = read_weights_file(filename)
     net = LeelaModel(filters, blocks)
     if not train:
         net.eval()
         for p in net.parameters():
             p.requires_grad = False
     parameters = []
     for module_name, module in net.named_modules():
         class_name = module.__class__.__name__
         for typ in ('weight', 'bias', 'running_mean', 'running_var'):
             param = getattr(module, typ, None)
             if param is not None:
                 parameters.append((module_name, class_name, typ, param))
     for i, w in enumerate(weights):
         w = torch.Tensor(w)
         module_name, class_name, typ, param = parameters[i]
         # print(f"{tuple(w.size())} -- {module_name} - {class_name} - {typ}: {tuple(param.size())}")
         if class_name == 'CenteredBatchNorm2d' and typ == 'bias':
             # Remember bias so it can be updated to a BatchNorm beta when the running_var is seen
             bn_bias_param = param
         if class_name == 'CenteredBatchNorm2d' and typ == 'running_var':
             # print("Updating bias")
             std = torch.sqrt(w + 1e-5)
             bn_bias_param.data.div_(std.view_as(param))
         param.data.copy_(w.view_as(param))
     if cuda:
         print("Enabling CUDA!")
         net.cuda()
     return net
Exemplo n.º 2
0
 def from_weights_file(filename, train=False, cuda=False, half=False):
     if cuda:
         torch.backends.cudnn.benchmark = True
     filters, blocks, weights = read_weights_file(filename)
     net = LeelaModel(filters, blocks)
     if not train:
         net.eval()
         for p in net.parameters():
             p.requires_grad = False
     parameters = []
     for module_name, module in net.named_modules():
         class_name = module.__class__.__name__
         for typ in ('weight', 'bias', 'mean', 'stddiv'):
             param = getattr(module, typ, None)
             if param is not None:
                 parameters.append((module_name, class_name, typ, param))
     for i, w in enumerate(weights):
         w = torch.Tensor(w)
         module_name, class_name, typ, param = parameters[i]
         # print(f"{tuple(w.size())} -- {module_name} - {class_name} - {typ}: {tuple(param.size())}")
         if class_name == 'Normalization' and typ == 'stddiv':
             # print('NStddiv')
             w = 1 / torch.sqrt(w + 1e-5)
         param.data.copy_(w.view_as(param))
     if half:
         net.half()
     if cuda:
         print("Enabling CUDA!")
         net.cuda()
     net.prenormalize()
     return net
Exemplo n.º 3
0
    def from_weights_file(filename, train=False, cuda=False, half=False):
        if cuda:
            torch.backends.cudnn.benchmark = True
        filters, blocks, weights = read_weights_file(filename)
        net = LeelaModel(filters, blocks)
        if not train:
            net.eval()
            for p in net.parameters():
                p.requires_grad = False
        parameters = []
        for module_name, module in net.named_modules():
            class_name = module.__class__.__name__
            for typ in ('weight', 'bias', 'running_mean', 'running_var'):
                param = getattr(module, typ, None)
                if param is not None:
                    if class_name == 'CenteredBatchNorm2d' and typ == 'weight':
                        continue
                    parameters.append((module_name, class_name, typ, param))
        for i, w in enumerate(weights):
            w = torch.Tensor(w)
            module_name, class_name, typ, param = parameters[i]
            # print(param.shape)
            # print(f"{tuple(w.size())} -- {module_name} - {class_name} - {typ}: {tuple(param.size())}")
            if class_name == 'CenteredBatchNorm2d' and typ == 'bias':
                # Remember bias so it can be updated to a BatchNorm beta when the running_var is seen
                bn_bias_param = param
            if class_name == 'CenteredBatchNorm2d' and typ == 'running_var':
                # print("Updating bias")
                std = torch.sqrt(w + 1e-5)
                bn_bias_param.detach().div_(std.view_as(param))
            param.data.copy_(w.view_as(param))
        if half:
            net.half()
        if cuda:
            print("Enabling CUDA!")
            net.cuda()
        return net


# Simple test to verify saving weights...
# net = load_network()
#
# net.model.save_weights_file('test_weights_2.txt')
#
# import numpy as np
#
# with open('test_weights_2.txt') as f1:
#     with open('weights_run1_21754.txt') as f2:
#         for l1, l2 in zip(f1, f2):
#             l1 = np.array([float(s) for s in l1.strip().split()])
#             l2 = np.array([float(s) for s in l2.strip().split()])
#             s = sum((l1 - l2) ** 2) / len(l1)
#             print(s)
Exemplo n.º 4
0
    def __init__(self, weights_filename):
        filters, blocks, weights = read_weights_file(weights_filename)
        cfg = yaml.safe_load(YAMLCFG)
        cfg['model']['filters'] = filters
        cfg['model']['residual_blocks'] = blocks
        cfg['name'] = 'online-{}x{}'.format(filters, blocks)
        print(yaml.dump(cfg, default_flow_style=False))

        x = [
            tf.placeholder(tf.float32, [None, 112, 8 * 8]),
            tf.placeholder(tf.float32, [None, 1858]),
            tf.placeholder(tf.float32, [None, 1])
        ]

        self.tfp = tfprocess.TFProcess(cfg)
        self.tfp.init_net(x)
        self.tfp.replace_weights(weights)