YOLO v3 PyTorch版本源碼解讀(一):模型結構解讀

PyTorch 代碼鏈接:https://github.com/ultralytics/yolov3

本篇主要是對代碼文件中 models.py的解讀,同時由於用到了utils文件夾下 parse_config.py中的兩個函數,所以也對其進行了分析。

1. utils文件夾

1.1. parse_config.py

這個py文件中定義了兩個函數——parse_model_cfg和parse_data_cfg,其中parse_model_cfg返回的module_defs存儲的是所有的網絡參數信息,一個list中嵌套了多個dict,每一個dict對應的是網絡中的一個子模塊——卷積、池化、特徵拼接、跨層連接或者 yolo輸出層;parse_data_cfg是將rbc.data文件中的內容存儲到options這個dict中,獲取的時候就可以對這個對象通過key提取。

# 返回的module_defs存儲的是所有的網絡參數信息,一個list中嵌套了多個dict
def parse_model_cfg(path):
    """Parses the yolo-v3 layer configuration file and returns module definitions"""
    file = open(path, 'r')
    lines = file.read().split('\n')     # 讀取cfg文件的每一行存入列表
    lines = [x for x in lines if x and not x.startswith('#')]   # 刪除掉空行或者以"#"開頭的行
    lines = [x.rstrip().lstrip() for x in lines]  # 去掉左右兩邊的空格
    module_defs = []
    for line in lines:
        if line.startswith('['):  # 當遇到 '['時,意味着要新建一個字典了,也就說說每個字典對應一個 新塊
            module_defs.append({})
            module_defs[-1]['type'] = line[1:-1].rstrip()
            if module_defs[-1]['type'] == 'convolutional':
                module_defs[-1]['batch_normalize'] = 0  # pre-populate with zeros (may be overwritten later)
        else:
            key, value = line.split("=")  # 用"="分割兩邊的key和value
            value = value.strip()
            module_defs[-1][key.rstrip()] = value.strip()

    return module_defs
# 將rbc.data文件中的內容存儲到options這個dict中,獲取的時候就可以對這個對象通過key提取value;
def parse_data_cfg(path):
    """Parses the data configuration file"""
    options = dict()       
    options['gpus'] = '0,1,2,3'
    options['num_workers'] = '10'
    with open(path, 'r') as fp:
        lines = fp.readlines()
    for line in lines:
        line = line.strip()
        if line == '' or line.startswith('#'):
            continue
        key, value = line.split('=')
        options[key.strip()] = value.strip()
    return options

2. Models.py

該文件主要用於定義yolo v3的網絡結構,

2.1 create_modules

根據由cfg返回的嵌套多個dict的列表創建網絡結構,當遇到 [route] 和 [shortcut] 時,該層實際上相當於 EmptyLayer層,僅僅做了一個線性映射,不同的地方在於送往下一層之前要做拼接或者短接,而這一點不用在模型結構中體現,在DarkNet的forward中可以看出來。這裏需要注意的是在定義nn.Conv2d時,bias = not bn,也就是說如果
卷積層之後有bn層的話,卷積層沒有偏置,否則有偏置。

# 根據cfg文件創建模塊
def create_modules(module_defs):
    """
    Constructs module list of layer blocks from module configuration in module_defs
    """
    # 獲取網絡超參數
    hyperparams = module_defs.pop(0)
    # 保存上一層中輸出feature maps 的卷積核個數,作爲下一次操作中卷積核的通道數
    output_filters = [int(hyperparams['channels'])]
    # 創建一個list,其中存放的是module
    module_list = nn.ModuleList()
    yolo_index = -1

    for i, module_def in enumerate(module_defs):
        modules = nn.Sequential()
        # 根據不同的層進行不同的設計
        # 卷積層
        if module_def['type'] == 'convolutional':
            bn = int(module_def['batch_normalize'])
            filters = int(module_def['filters'])
            kernel_size = int(module_def['size'])
            pad = (kernel_size - 1) // 2 if int(module_def['pad']) else 0
            modules.add_module('conv_%d' % i, nn.Conv2d(in_channels=output_filters[-1],
                                                        out_channels=filters,
                                                        kernel_size=kernel_size,
                                                        stride=int(module_def['stride']),
                                                        padding=pad,
                                                        bias=not bn))
            if bn:
                modules.add_module('batch_norm_%d' % i, nn.BatchNorm2d(filters))
            if module_def['activation'] == 'leaky':
                # modules.add_module('leaky_%d' % i, nn.PReLU(num_parameters=filters, init=0.10))
                modules.add_module('leaky_%d' % i, nn.LeakyReLU(0.1, inplace=True))
        # 最大池化模塊
        elif module_def['type'] == 'maxpool':
            kernel_size = int(module_def['size'])
            stride = int(module_def['stride'])
            if kernel_size == 2 and stride == 1:
                modules.add_module('_debug_padding_%d' % i, nn.ZeroPad2d((0, 1, 0, 1)))
            maxpool = nn.MaxPool2d(kernel_size=kernel_size, stride=stride, padding=int((kernel_size - 1) // 2))
            modules.add_module('maxpool_%d' % i, maxpool)
        # 上採樣模塊
        elif module_def['type'] == 'upsample':
            upsample = nn.Upsample(scale_factor=int(module_def['stride']), mode='nearest')
            modules.add_module('upsample_%d' % i, upsample)
        # 這裏做的是在卷積核個數維度上的拼接
        elif module_def['type'] == 'route':
            layers = [int(x) for x in module_def['layers'].split(',')]
            filters = sum([output_filters[i + 1 if i > 0 else i] for i in layers])
            modules.add_module('route_%d' % i, EmptyLayer())
        # 跨層連接
        elif module_def['type'] == 'shortcut':
            filters = output_filters[int(module_def['from'])]
            modules.add_module('shortcut_%d' % i, EmptyLayer())

        elif module_def['type'] == 'yolo':
            yolo_index += 1
            anchor_idxs = [int(x) for x in module_def['mask'].split(',')]
            # Extract anchors
            anchors = [float(x) for x in module_def['anchors'].split(',')]
            anchors = [(anchors[i], anchors[i + 1]) for i in range(0, len(anchors), 2)]
            anchors = [anchors[i] for i in anchor_idxs]
            # print("anchors = ", anchors)
            nc = int(module_def['classes'])  # number of classes
            img_size = hyperparams['height']
            # Define detection layer
            modules.add_module('yolo_%d' % i, YOLOLayer(anchors, nc, img_size, yolo_index))

        # Register module list and number of output filters
        # 將module添加到module_list中
        module_list.append(modules)
        output_filters.append(filters)

    return hyperparams, module_list

# 該層就相當於一個普通的 線性映射層
class EmptyLayer(nn.Module):
    """Placeholder for 'route' and 'shortcut' layers"""

    def __init__(self):
        super(EmptyLayer, self).__init__()

    def forward(self, x):
        return x

2.2 定義 YOLOLayer類

當遇到 [yolo] 時,送入 YOLOLayer類,

class YOLOLayer(nn.Module):
    def __init__(self, anchors, nc, img_size, yolo_index):
        super(YOLOLayer, self).__init__()

        self.anchors = torch.Tensor(anchors)
        self.na = len(anchors)  # number of anchors (3)
        self.nc = nc  # number of classes (80)
        self.nx = 0  # initialize number of x gridpoints,即本層feature map的寬
        self.ny = 0  # initialize number of y gridpoints,即本層feature map的高

        if ONNX_EXPORT:  # grids must be computed in __init__
            stride = [32, 16, 8][yolo_index]  # stride of this layer
            nx = int(img_size[1] / stride)  # number x grid points
            ny = int(img_size[0] / stride)  # number y grid points
            create_grids(self, max(img_size), (nx, ny))
    # p的維度爲 [batch_size, C_out, H, W]
    def forward(self, p, img_size, var=None):
        if ONNX_EXPORT:
            bs = 1  # batch size
        else:
            bs, ny, nx = p.shape[0], p.shape[-2], p.shape[-1]
            if (self.nx, self.ny) != (nx, ny):
                create_grids(self, img_size, (nx, ny), p.device)

        # p.view(bs, 255, 13, 13) -- > (bs, 3, 13, 13, 85)  # (bs, anchors, grid, grid, classes + xywh)
        p = p.view(bs, self.na, self.nc + 5, self.ny, self.nx).permute(0, 1, 3, 4, 2).contiguous()  # prediction

        if self.training:
            return p

        elif ONNX_EXPORT:
            # Constants CAN NOT BE BROADCAST, ensure correct shape!
            ngu = self.ng.repeat((1, self.na * self.nx * self.ny, 1))
            grid_xy = self.grid_xy.repeat((1, self.na, 1, 1, 1)).view((1, -1, 2))
            anchor_wh = self.anchor_wh.repeat((1, 1, self.nx, self.ny, 1)).view((1, -1, 2)) / ngu

            # p = p.view(-1, 5 + self.nc)
            # xy = torch.sigmoid(p[..., 0:2]) + grid_xy[0]  # x, y
            # wh = torch.exp(p[..., 2:4]) * anchor_wh[0]  # width, height
            # p_conf = torch.sigmoid(p[:, 4:5])  # Conf
            # p_cls = F.softmax(p[:, 5:85], 1) * p_conf  # SSD-like conf
            # return torch.cat((xy / ngu[0], wh, p_conf, p_cls), 1).t()

            p = p.view(1, -1, 5 + self.nc)
            # 求得bx, by
            xy = torch.sigmoid(p[..., 0:2]) + grid_xy  # x, y
            # 求得bw, bh
            wh = torch.exp(p[..., 2:4]) * anchor_wh  # width, height
            # 得到置信度
            p_conf = torch.sigmoid(p[..., 4:5])  # Conf
            # 得到類別概率
            p_cls = p[..., 5:5 + self.nc]
            # Broadcasting only supported on first dimension in CoreML. See onnx-coreml/_operators.py
            # p_cls = F.softmax(p_cls, 2) * p_conf  # SSD-like conf
            p_cls = torch.exp(p_cls).permute((2, 1, 0))
            p_cls = p_cls / p_cls.sum(0).unsqueeze(0) * p_conf.permute((2, 1, 0))  # F.softmax() equivalent
            p_cls = p_cls.permute(2, 1, 0)
            return torch.cat((xy / ngu, wh, p_conf, p_cls), 2).squeeze().t()

        else:  # inference
            # s = 1.5  # scale_xy  (pxy = pxy * s - (s - 1) / 2)
            io = p.clone()  # inference output
            io[..., 0:2] = torch.sigmoid(io[..., 0:2]) + self.grid_xy  # xy
            io[..., 2:4] = torch.exp(io[..., 2:4]) * self.anchor_wh  # wh yolo method
            # io[..., 2:4] = ((torch.sigmoid(io[..., 2:4]) * 2) ** 3) * self.anchor_wh  # wh power method
            io[..., 4:] = torch.sigmoid(io[..., 4:])  # p_conf, p_cls
            # io[..., 5:] = F.softmax(io[..., 5:], dim=4)  # p_cls
            io[..., :4] *= self.stride
            if self.nc == 1:
                io[..., 5] = 1  # single-class model https://github.com/ultralytics/yolov3/issues/235

            # reshape from [1, 3, 13, 13, 85] to [1, 507, 85]
            return io.view(bs, -1, 5 + self.nc), p
def create_grids(self, img_size=416, ng=(13, 13), device='cpu'):
    nx, ny = ng  # x and y grid size
    self.img_size = img_size
    # 該層特徵圖相對於原始圖片的縮放尺寸
    self.stride = img_size / max(ng)

    # build xy offsets
    """
    meshgrid的作用是:根據傳入的兩個一維數組參數生成兩個數組元素的列表。如果第一個參數是xarray,維度是xdimesion;  
    第二個參數是yarray,維度是ydimesion;那麼生成的第一個二維數組是以xarray爲行,共ydimesion行的向量;而第二個
    二維數組是以yarray的轉置爲列,共xdimesion列的向量。  

    x = np.array([1,2,3])
    y = np.array([4,5,6,7])
    X,Y = np.meshgrid(x,y)
    X  #以xarray[1,2,3]爲行,2行的向量
    Y  #以yarray轉置爲列[4,5,6,7],共3列向量
    array([[1, 2, 3],
        [1, 2, 3],
        [1, 2, 3],
        [1, 2, 3]])

    array([[4, 4, 4],
        [5, 5, 5],
        [6, 6, 6],
        [7, 7, 7]])
    """
    yv, xv = torch.meshgrid([torch.arange(ny), torch.arange(nx)])
    # torch.stack指定了做完stack之後生成結果放置的維度
    # 如果 ng = (13, 13),那麼 torch.stack((xv, yv), 2)的維度爲 (13, 13, 2) -------->  (1, 1, 13, 13, 2)
    self.grid_xy = torch.stack((xv, yv), 2).to(device).float().view((1, 1, ny, nx, 2))

    # build wh gains
    # 將anchor box的 w和h的數值映射到該層feature map上
    self.anchor_vec = self.anchors.to(device) / self.stride
    # self.anchor_vec: [3, 2] -------> anchor_wh: [1, 3, 1, 1, 2]
    self.anchor_wh = self.anchor_vec.view(1, self.na, 1, 1, 2).to(device)
    self.ng = torch.Tensor(ng).to(device)
    self.nx = nx
    self.ny = ny

2.3 初始化模型

在 train.py 中通過 model = Darknet(cfg).to(device) 完成對模型的構建;

class Darknet(nn.Module):
    """YOLOv3 object detection model"""

    def __init__(self, cfg, img_size=(416, 416)):
        super(Darknet, self).__init__()

        self.module_defs = parse_model_cfg(cfg)
        self.module_defs[0]['cfg'] = cfg
        self.module_defs[0]['height'] = img_size
        self.hyperparams, self.module_list = create_modules(self.module_defs)
        self.yolo_layers = get_yolo_layers(self)

        # Darknet Header https://github.com/AlexeyAB/darknet/issues/2914#issuecomment-496675346
        self.version = np.array([0, 2, 5], dtype=np.int32)  # (int32) version info: major, minor, revision
        self.seen = np.array([0], dtype=np.int64)  # (int64) number of images seen during training

    def forward(self, x, var=None):
        img_size = max(x.shape[-2:])
        layer_outputs = []
        output = []

        for i, (module_def, module) in enumerate(zip(self.module_defs, self.module_list)):
            mtype = module_def['type']
            if mtype in ['convolutional', 'upsample', 'maxpool']:
                x = module(x)
            elif mtype == 'route':
                # 在 C_{out} 維度做拼接
                layer_i = [int(x) for x in module_def['layers'].split(',')]
                if len(layer_i) == 1:
                    x = layer_outputs[layer_i[0]]
                else:
                    x = torch.cat([layer_outputs[i] for i in layer_i], 1)
            elif mtype == 'shortcut':
                # 跨層連接
                layer_i = int(module_def['from'])
                x = layer_outputs[-1] + layer_outputs[layer_i]
            elif mtype == 'yolo':
                x = module[0](x, img_size)
                output.append(x)
            layer_outputs.append(x)

        if self.training:
            return output
        elif ONNX_EXPORT:
            output = torch.cat(output, 1)  # cat 3 layers 85 x (507, 2028, 8112) to 85 x 10647
            nc = self.module_list[self.yolo_layers[0]][0].nc  # number of classes
            return output[5:5 + nc].t(), output[:4].t()  # ONNX scores, boxes
        else:
            io, p = list(zip(*output))  # inference output, training output
            return torch.cat(io, 1), p

    def fuse(self):
        # Fuse Conv2d + BatchNorm2d layers throughout model
        fused_list = nn.ModuleList()
        for a in list(self.children())[0]:
            for i, b in enumerate(a):
                if isinstance(b, nn.modules.batchnorm.BatchNorm2d):
                    # fuse this bn layer with the previous conv2d layer
                    conv = a[i - 1]
                    fused = torch_utils.fuse_conv_and_bn(conv, b)
                    a = nn.Sequential(fused, *list(a.children())[i + 1:])
                    break
            fused_list.append(a)
        self.module_list = fused_list
        # model_info(self)  # yolov3-spp reduced from 225 to 152 layers

2.4 加載權重——load_darknet_weights()

權重文件有兩種—— “.pt” 和 “.weights"結尾的,以”.pt"結尾的文件需要用 torch.load()來讀取,以 ".weights"結尾的文件需要用 load_darknet_weights()來讀取。

在 train.py 中調用load_darknet_weights():

if '-tiny.cfg' in cfg:
	cutoff = load_darknet_weights(model, weights + 'yolov3-tiny.conv.15')
else:
    cutoff = load_darknet_weights(model, weights + 'darknet53.conv.74')
# 從列表中將權重讀出來,並用這些權重初始化網絡參數

def load_darknet_weights(self, weights, cutoff=-1):
    # Parses and loads the weights stored in 'weights'
    # cutoff: save layers between 0 and cutoff (if cutoff = -1 all are saved)
    # 獲取權重文件名
    weights_file = weights.split(os.sep)[-1]

    # Try to download weights if not available locally
    if not os.path.isfile(weights):
        try:
            url = 'https://pjreddie.com/media/files/' + weights_file
            print('Downloading ' + url)
            os.system('curl ' + url + ' -o ' + weights)
        except IOError:
            print(weights + ' not found.\nTry https://drive.google.com/drive/folders/1uxgUBemJVw9wZsdpboYbzUN4bcRhsuAI')

    # Establish cutoffs
    if weights_file == 'darknet53.conv.74':
        cutoff = 75
    elif weights_file == 'yolov3-tiny.conv.15':
        cutoff = 15

    # Read weights file
    with open(weights, 'rb') as f:
        # Read Header https://github.com/AlexeyAB/darknet/issues/2914#issuecomment-496675346
        self.version = np.fromfile(f, dtype=np.int32, count=3)  # (int32) version info: major, minor, revision
        self.seen = np.fromfile(f, dtype=np.int64, count=1)  # (int64) number of images seen during training

        weights = np.fromfile(f, dtype=np.float32)  # The rest are weights

    ptr = 0
    for i, (module_def, module) in enumerate(zip(self.module_defs[:cutoff], self.module_list[:cutoff])):
        # 全卷積網絡,只有卷積層和bn層有參數,由於bn層的參數是按照 bias, weight, running_mean, running_var的順序寫入列表的,
        # 所以讀取的時候也應該按照這個順序,同時由於有bn層的時候卷積層沒有偏置,所以不用讀取卷積層的偏置
        if module_def['type'] == 'convolutional':
            conv_layer = module[0]
            if module_def['batch_normalize']:
                # Load BN bias, weights, running mean and running variance
                bn_layer = module[1]
                num_b = bn_layer.bias.numel()  # Number of biases
                # Bias
                bn_b = torch.from_numpy(weights[ptr:ptr + num_b]).view_as(bn_layer.bias)
                bn_layer.bias.data.copy_(bn_b)
                ptr += num_b
                # Weight
                bn_w = torch.from_numpy(weights[ptr:ptr + num_b]).view_as(bn_layer.weight)
                bn_layer.weight.data.copy_(bn_w)
                ptr += num_b
                # Running Mean
                bn_rm = torch.from_numpy(weights[ptr:ptr + num_b]).view_as(bn_layer.running_mean)
                bn_layer.running_mean.data.copy_(bn_rm)
                ptr += num_b
                # Running Var
                bn_rv = torch.from_numpy(weights[ptr:ptr + num_b]).view_as(bn_layer.running_var)
                bn_layer.running_var.data.copy_(bn_rv)
                ptr += num_b
            else:
                # Load conv. bias
                num_b = conv_layer.bias.numel()
                conv_b = torch.from_numpy(weights[ptr:ptr + num_b]).view_as(conv_layer.bias)
                conv_layer.bias.data.copy_(conv_b)
                ptr += num_b
            # Load conv. weights
            num_w = conv_layer.weight.numel()
            conv_w = torch.from_numpy(weights[ptr:ptr + num_w]).view_as(conv_layer.weight)
            conv_layer.weight.data.copy_(conv_w)
            ptr += num_w

    return cutoff

2.5 保存權重——save_weights()

與 load_darknet_weights()相對應,用save_weights格式保存的權重需用load_darknet_weights()纔可以正確讀取。

def save_weights(self, path='model.weights', cutoff=-1):
    # Converts a PyTorch model to Darket format (*.pt to *.weights)
    # Note: Does not work if model.fuse() is applied
    with open(path, 'wb') as f:
        # Write Header https://github.com/AlexeyAB/darknet/issues/2914#issuecomment-496675346
        self.version.tofile(f)  # (int32) version info: major, minor, revision
        self.seen.tofile(f)  # (int64) number of images seen during training

        # Iterate through layers
        for i, (module_def, module) in enumerate(zip(self.module_defs[:cutoff], self.module_list[:cutoff])):
            if module_def['type'] == 'convolutional':
                conv_layer = module[0]
                # If batch norm, load bn first
                if module_def['batch_normalize']:
                    bn_layer = module[1]
                    bn_layer.bias.data.cpu().numpy().tofile(f)
                    bn_layer.weight.data.cpu().numpy().tofile(f)
                    bn_layer.running_mean.data.cpu().numpy().tofile(f)
                    bn_layer.running_var.data.cpu().numpy().tofile(f)
                # Load conv bias
                else:
                    conv_layer.bias.data.cpu().numpy().tofile(f)
                # Load conv weights
                conv_layer.weight.data.cpu().numpy().tofile(f)

2.6 “.pt” 和 ".weights"權重文件的轉換

def convert(cfg='cfg/yolov3-spp.cfg', weights='weights/yolov3-spp.weights'):
    # Converts between PyTorch and Darknet format per extension (i.e. *.weights convert to *.pt and vice versa)
    # from models import *; convert('cfg/yolov3-spp.cfg', 'weights/yolov3-spp.weights')

    # Initialize model
    model = Darknet(cfg)

    # Load weights and save
    if weights.endswith('.pt'):  # if PyTorch format
        model.load_state_dict(torch.load(weights, map_location='cpu')['model'])
        save_weights(model, path='converted.weights', cutoff=-1)
        print("Success: converted '%s' to 'converted.weights'" % weights)

    elif weights.endswith('.weights'):  # darknet format
        _ = load_darknet_weights(model, weights)

        chkpt = {'epoch': -1,
                 'best_fitness': None,
                 'training_results': None,
                 'model': model.state_dict(),
                 'optimizer': None}

        torch.save(chkpt, 'converted.pt')
        print("Success: converted '%s' to 'converted.pt'" % weights)

    else:
        print('Error: extension not supported.')
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章