|  | import torch | 
					
						
						|  | from torch import tensor | 
					
						
						|  | import torch.nn as nn | 
					
						
						|  | import sys,os | 
					
						
						|  | import math | 
					
						
						|  | import sys | 
					
						
						|  | sys.path.append(os.getcwd()) | 
					
						
						|  | from lib.utils import initialize_weights | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  | from lib.models.common import Conv, SPP, Bottleneck, BottleneckCSP, Focus, Concat, Detect | 
					
						
						|  | from torch.nn import Upsample | 
					
						
						|  | from lib.utils import check_anchor_order | 
					
						
						|  | from lib.core.evaluate import SegmentationMetric | 
					
						
						|  | from lib.utils.utils import time_synchronized | 
					
						
						|  |  | 
					
						
						|  | CSPDarknet_s = [ | 
					
						
						|  | [ -1, Focus, [3, 32, 3]], | 
					
						
						|  | [ -1, Conv, [32, 64, 3, 2]], | 
					
						
						|  | [ -1, BottleneckCSP, [64, 64, 1]], | 
					
						
						|  | [ -1, Conv, [64, 128, 3, 2]], | 
					
						
						|  | [ -1, BottleneckCSP, [128, 128, 3]], | 
					
						
						|  | [ -1, Conv, [128, 256, 3, 2]], | 
					
						
						|  | [ -1, BottleneckCSP, [256, 256, 3]], | 
					
						
						|  | [ -1, Conv, [256, 512, 3, 2]], | 
					
						
						|  | [ -1, SPP, [512, 512, [5, 9, 13]]], | 
					
						
						|  | [ -1, BottleneckCSP, [512, 512, 1, False]] | 
					
						
						|  | ] | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  | MCnet_SPP = [ | 
					
						
						|  | [ -1, Focus, [3, 32, 3]], | 
					
						
						|  | [ -1, Conv, [32, 64, 3, 2]], | 
					
						
						|  | [ -1, BottleneckCSP, [64, 64, 1]], | 
					
						
						|  | [ -1, Conv, [64, 128, 3, 2]], | 
					
						
						|  | [ -1, BottleneckCSP, [128, 128, 3]], | 
					
						
						|  | [ -1, Conv, [128, 256, 3, 2]], | 
					
						
						|  | [ -1, BottleneckCSP, [256, 256, 3]], | 
					
						
						|  | [ -1, Conv, [256, 512, 3, 2]], | 
					
						
						|  | [ -1, SPP, [512, 512, [5, 9, 13]]], | 
					
						
						|  | [ -1, BottleneckCSP, [512, 512, 1, False]], | 
					
						
						|  | [ -1, Conv,[512, 256, 1, 1]], | 
					
						
						|  | [ -1, Upsample, [None, 2, 'nearest']], | 
					
						
						|  | [ [-1, 6], Concat, [1]], | 
					
						
						|  | [ -1, BottleneckCSP, [512, 256, 1, False]], | 
					
						
						|  | [ -1, Conv, [256, 128, 1, 1]], | 
					
						
						|  | [ -1, Upsample, [None, 2, 'nearest']], | 
					
						
						|  | [ [-1,4], Concat, [1]], | 
					
						
						|  | [ -1, BottleneckCSP, [256, 128, 1, False]], | 
					
						
						|  | [ -1, Conv, [128, 128, 3, 2]], | 
					
						
						|  | [ [-1, 14], Concat, [1]], | 
					
						
						|  | [ -1, BottleneckCSP, [256, 256, 1, False]], | 
					
						
						|  | [ -1, Conv, [256, 256, 3, 2]], | 
					
						
						|  | [ [-1, 10], Concat, [1]], | 
					
						
						|  | [ -1, BottleneckCSP, [512, 512, 1, False]], | 
					
						
						|  |  | 
					
						
						|  | [ [17, 20, 23], Detect,  [13, [[3,9,5,11,4,20], [7,18,6,39,12,31], [19,50,38,81,68,157]], [128, 256, 512]]], | 
					
						
						|  | [ 17, Conv, [128, 64, 3, 1]], | 
					
						
						|  | [ -1, Upsample, [None, 2, 'nearest']], | 
					
						
						|  | [ [-1,2], Concat, [1]], | 
					
						
						|  | [ -1, BottleneckCSP, [128, 64, 1, False]], | 
					
						
						|  | [ -1, Conv, [64, 32, 3, 1]], | 
					
						
						|  | [ -1, Upsample, [None, 2, 'nearest']], | 
					
						
						|  | [ -1, Conv, [32, 16, 3, 1]], | 
					
						
						|  | [ -1, BottleneckCSP, [16, 8, 1, False]], | 
					
						
						|  | [ -1, Upsample, [None, 2, 'nearest']], | 
					
						
						|  | [ -1, SPP, [8, 2, [5, 9, 13]]] | 
					
						
						|  | ] | 
					
						
						|  |  | 
					
						
						|  | MCnet_fast = [ | 
					
						
						|  | [ -1, Focus, [3, 32, 3]], | 
					
						
						|  | [ -1, Conv, [32, 64, 3, 2]], | 
					
						
						|  | [ -1, BottleneckCSP, [64, 128, 1, True, True]], | 
					
						
						|  | [ -1, BottleneckCSP, [128, 256, 1, True, True]], | 
					
						
						|  | [ -1, BottleneckCSP, [256, 512, 1, True, True]], | 
					
						
						|  | [ -1, SPP, [512, 512, [5, 9, 13]]], | 
					
						
						|  | [ -1, BottleneckCSP, [512, 512, 1, False]], | 
					
						
						|  | [ -1, Conv,[512, 256, 1, 1]], | 
					
						
						|  | [ -1, Upsample, [None, 2, 'nearest']], | 
					
						
						|  | [ [-1, 6], Concat, [1]], | 
					
						
						|  | [ -1, BottleneckCSP, [512, 256, 1, False]], | 
					
						
						|  | [ -1, Conv, [256, 128, 1, 1]], | 
					
						
						|  | [ -1, Upsample, [None, 2, 'nearest']], | 
					
						
						|  | [ [-1,4], Concat, [1]], | 
					
						
						|  | [ -1, BottleneckCSP, [256, 128, 1, False, True]], | 
					
						
						|  | [ [-1, 14], Concat, [1]], | 
					
						
						|  | [ -1, BottleneckCSP, [256, 256, 1, False, True]], | 
					
						
						|  | [ [-1, 10], Concat, [1]], | 
					
						
						|  | [ -1, BottleneckCSP, [512, 512, 1, False]], | 
					
						
						|  | [ [17, 20, 23], Detect,  [1, [[3,9,5,11,4,20], [7,18,6,39,12,31], [19,50,38,81,68,157]], [128, 256, 512]]], | 
					
						
						|  |  | 
					
						
						|  | [ 16, Conv, [256, 64, 3, 1]], | 
					
						
						|  | [ -1, Upsample, [None, 2, 'nearest']], | 
					
						
						|  | [ [-1,2], Concat, [1]], | 
					
						
						|  | [ -1, BottleneckCSP, [128, 32, 1, False]], | 
					
						
						|  |  | 
					
						
						|  | [ -1, Upsample, [None, 2, 'nearest']], | 
					
						
						|  |  | 
					
						
						|  | [ -1, BottleneckCSP, [32, 8, 1, False]], | 
					
						
						|  | [ -1, Upsample, [None, 2, 'nearest']], | 
					
						
						|  | [ -1, Conv, [8, 2, 1, 1]], | 
					
						
						|  |  | 
					
						
						|  | [ 16, Conv, [256, 64, 3, 1]], | 
					
						
						|  | [ -1, Upsample, [None, 2, 'nearest']], | 
					
						
						|  | [ [-1,2], Concat, [1]], | 
					
						
						|  | [ -1, BottleneckCSP, [128, 32, 1, False]], | 
					
						
						|  |  | 
					
						
						|  | [ -1, Upsample, [None, 2, 'nearest']], | 
					
						
						|  |  | 
					
						
						|  | [ 31, BottleneckCSP, [32, 8, 1, False]], | 
					
						
						|  | [ -1, Upsample, [None, 2, 'nearest']], | 
					
						
						|  | [ -1, Conv, [8, 2, 1, 1]], | 
					
						
						|  | ] | 
					
						
						|  |  | 
					
						
						|  | MCnet_light = [ | 
					
						
						|  | [ -1, Focus, [3, 32, 3]], | 
					
						
						|  | [ -1, Conv, [32, 64, 3, 2]], | 
					
						
						|  | [ -1, BottleneckCSP, [64, 64, 1]], | 
					
						
						|  | [ -1, Conv, [64, 128, 3, 2]], | 
					
						
						|  | [ -1, BottleneckCSP, [128, 128, 3]], | 
					
						
						|  | [ -1, Conv, [128, 256, 3, 2]], | 
					
						
						|  | [ -1, BottleneckCSP, [256, 256, 3]], | 
					
						
						|  | [ -1, Conv, [256, 512, 3, 2]], | 
					
						
						|  | [ -1, SPP, [512, 512, [5, 9, 13]]], | 
					
						
						|  | [ -1, BottleneckCSP, [512, 512, 1, False]], | 
					
						
						|  | [ -1, Conv,[512, 256, 1, 1]], | 
					
						
						|  | [ -1, Upsample, [None, 2, 'nearest']], | 
					
						
						|  | [ [-1, 6], Concat, [1]], | 
					
						
						|  | [ -1, BottleneckCSP, [512, 256, 1, False]], | 
					
						
						|  | [ -1, Conv, [256, 128, 1, 1]], | 
					
						
						|  | [ -1, Upsample, [None, 2, 'nearest']], | 
					
						
						|  | [ [-1,4], Concat, [1]], | 
					
						
						|  | [ -1, BottleneckCSP, [256, 128, 1, False]], | 
					
						
						|  | [ -1, Conv, [128, 128, 3, 2]], | 
					
						
						|  | [ [-1, 14], Concat, [1]], | 
					
						
						|  | [ -1, BottleneckCSP, [256, 256, 1, False]], | 
					
						
						|  | [ -1, Conv, [256, 256, 3, 2]], | 
					
						
						|  | [ [-1, 10], Concat, [1]], | 
					
						
						|  | [ -1, BottleneckCSP, [512, 512, 1, False]], | 
					
						
						|  | [ [17, 20, 23], Detect,  [1, [[4,12,6,18,10,27], [15,38,24,59,39,78], [51,125,73,168,97,292]], [128, 256, 512]]], | 
					
						
						|  |  | 
					
						
						|  | [ 16, Conv, [256, 128, 3, 1]], | 
					
						
						|  | [ -1, Upsample, [None, 2, 'nearest']], | 
					
						
						|  |  | 
					
						
						|  | [ -1, BottleneckCSP, [128, 64, 1, False]], | 
					
						
						|  | [ -1, Conv, [64, 32, 3, 1]], | 
					
						
						|  | [ -1, Upsample, [None, 2, 'nearest']], | 
					
						
						|  | [ -1, Conv, [32, 16, 3, 1]], | 
					
						
						|  | [ -1, BottleneckCSP, [16, 8, 1, False]], | 
					
						
						|  | [ -1, Upsample, [None, 2, 'nearest']], | 
					
						
						|  | [ -1, Conv, [8, 3, 3, 1]], | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  | [ 30, BottleneckCSP, [16, 8, 1, False]], | 
					
						
						|  | [ -1, Upsample, [None, 2, 'nearest']], | 
					
						
						|  | [ -1, Conv, [8, 2, 3, 1]], | 
					
						
						|  | ] | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  | MCnet_share = [ | 
					
						
						|  | [ -1, Focus, [3, 32, 3]], | 
					
						
						|  | [ -1, Conv, [32, 64, 3, 2]], | 
					
						
						|  | [ -1, BottleneckCSP, [64, 64, 1]], | 
					
						
						|  | [ -1, Conv, [64, 128, 3, 2]], | 
					
						
						|  | [ -1, BottleneckCSP, [128, 128, 3]], | 
					
						
						|  | [ -1, Conv, [128, 256, 3, 2]], | 
					
						
						|  | [ -1, BottleneckCSP, [256, 256, 3]], | 
					
						
						|  | [ -1, Conv, [256, 512, 3, 2]], | 
					
						
						|  | [ -1, SPP, [512, 512, [5, 9, 13]]], | 
					
						
						|  | [ -1, BottleneckCSP, [512, 512, 1, False]], | 
					
						
						|  | [ -1, Conv,[512, 256, 1, 1]], | 
					
						
						|  | [ -1, Upsample, [None, 2, 'nearest']], | 
					
						
						|  | [ [-1, 6], Concat, [1]], | 
					
						
						|  | [ -1, BottleneckCSP, [512, 256, 1, False]], | 
					
						
						|  | [ -1, Conv, [256, 128, 1, 1]], | 
					
						
						|  | [ -1, Upsample, [None, 2, 'nearest']], | 
					
						
						|  | [ [-1,4], Concat, [1]], | 
					
						
						|  | [ -1, BottleneckCSP, [256, 128, 1, False]], | 
					
						
						|  | [ -1, Conv, [128, 128, 3, 2]], | 
					
						
						|  | [ [-1, 14], Concat, [1]], | 
					
						
						|  | [ -1, BottleneckCSP, [256, 256, 1, False]], | 
					
						
						|  | [ -1, Conv, [256, 256, 3, 2]], | 
					
						
						|  | [ [-1, 10], Concat, [1]], | 
					
						
						|  | [ -1, BottleneckCSP, [512, 512, 1, False]], | 
					
						
						|  | [ [17, 20, 23], Detect,  [1, [[3,9,5,11,4,20], [7,18,6,39,12,31], [19,50,38,81,68,157]], [128, 256, 512]]], | 
					
						
						|  |  | 
					
						
						|  | [ 16, Conv, [256, 64, 3, 1]], | 
					
						
						|  | [ -1, Upsample, [None, 2, 'nearest']], | 
					
						
						|  | [ [-1,2], Concat, [1]], | 
					
						
						|  | [ -1, BottleneckCSP, [128, 64, 1, False]], | 
					
						
						|  | [ -1, Conv, [64, 32, 3, 1]], | 
					
						
						|  | [ -1, Upsample, [None, 2, 'nearest']], | 
					
						
						|  | [ -1, Conv, [32, 16, 3, 1]], | 
					
						
						|  | [ -1, BottleneckCSP, [16, 8, 1, False]], | 
					
						
						|  |  | 
					
						
						|  | [ 16, Conv, [256, 64, 3, 1]], | 
					
						
						|  | [ -1, Upsample, [None, 2, 'nearest']], | 
					
						
						|  | [ [-1,2], Concat, [1]], | 
					
						
						|  | [ -1, BottleneckCSP, [128, 64, 1, False]], | 
					
						
						|  | [ -1, Conv, [64, 32, 3, 1]], | 
					
						
						|  | [ -1, Upsample, [None, 2, 'nearest']], | 
					
						
						|  | [ -1, Conv, [32, 16, 3, 1]], | 
					
						
						|  | [ -1, BottleneckCSP, [16, 8, 1, False]], | 
					
						
						|  |  | 
					
						
						|  | [ [31,39], Concat, [1]], | 
					
						
						|  | [ -1, Conv, [32, 8, 3, 1]], | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  | [ [32,42], Concat, [1]], | 
					
						
						|  | [ -1, Upsample, [None, 2, 'nearest']], | 
					
						
						|  | [ -1, Conv, [16, 2, 3, 1]], | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  | [ [40,42], Concat, [1]], | 
					
						
						|  | [ -1, Upsample, [None, 2, 'nearest']], | 
					
						
						|  | [ -1, Conv, [16, 2, 3, 1]] | 
					
						
						|  | ] | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  | MCnet_no_share = [ | 
					
						
						|  | [ -1, Focus, [3, 32, 3]], | 
					
						
						|  | [ -1, Conv, [32, 64, 3, 2]], | 
					
						
						|  | [ -1, BottleneckCSP, [64, 64, 1]], | 
					
						
						|  | [ -1, Conv, [64, 128, 3, 2]], | 
					
						
						|  | [ -1, BottleneckCSP, [128, 128, 3]], | 
					
						
						|  | [ -1, Conv, [128, 256, 3, 2]], | 
					
						
						|  | [ -1, BottleneckCSP, [256, 256, 3]], | 
					
						
						|  | [ -1, Conv, [256, 512, 3, 2]], | 
					
						
						|  | [ -1, SPP, [512, 512, [5, 9, 13]]], | 
					
						
						|  | [ -1, BottleneckCSP, [512, 512, 1, False]], | 
					
						
						|  | [ -1, Conv,[512, 256, 1, 1]], | 
					
						
						|  | [ -1, Upsample, [None, 2, 'nearest']], | 
					
						
						|  | [ [-1, 6], Concat, [1]], | 
					
						
						|  | [ -1, BottleneckCSP, [512, 256, 1, False]], | 
					
						
						|  | [ -1, Conv, [256, 128, 1, 1]], | 
					
						
						|  | [ -1, Upsample, [None, 2, 'nearest']], | 
					
						
						|  | [ [-1,4], Concat, [1]], | 
					
						
						|  | [ -1, BottleneckCSP, [256, 128, 1, False]], | 
					
						
						|  | [ -1, Conv, [128, 128, 3, 2]], | 
					
						
						|  | [ [-1, 14], Concat, [1]], | 
					
						
						|  | [ -1, BottleneckCSP, [256, 256, 1, False]], | 
					
						
						|  | [ -1, Conv, [256, 256, 3, 2]], | 
					
						
						|  | [ [-1, 10], Concat, [1]], | 
					
						
						|  | [ -1, BottleneckCSP, [512, 512, 1, False]], | 
					
						
						|  | [ [17, 20, 23], Detect,  [13, [[3,9,5,11,4,20], [7,18,6,39,12,31], [19,50,38,81,68,157]], [128, 256, 512]]], | 
					
						
						|  |  | 
					
						
						|  | [ 16, Conv, [256, 64, 3, 1]], | 
					
						
						|  | [ -1, Upsample, [None, 2, 'nearest']], | 
					
						
						|  | [ [-1,2], Concat, [1]], | 
					
						
						|  | [ -1, BottleneckCSP, [128, 64, 1, False]], | 
					
						
						|  | [ -1, Conv, [64, 32, 3, 1]], | 
					
						
						|  | [ -1, Upsample, [None, 2, 'nearest']], | 
					
						
						|  | [ -1, Conv, [32, 16, 3, 1]], | 
					
						
						|  | [ -1, BottleneckCSP, [16, 8, 1, False]], | 
					
						
						|  | [ -1, Upsample, [None, 2, 'nearest']], | 
					
						
						|  | [ -1, Conv, [8, 3, 3, 1]], | 
					
						
						|  |  | 
					
						
						|  | [ 16, Conv, [256, 64, 3, 1]], | 
					
						
						|  | [ -1, Upsample, [None, 2, 'nearest']], | 
					
						
						|  | [ [-1,2], Concat, [1]], | 
					
						
						|  | [ -1, BottleneckCSP, [128, 64, 1, False]], | 
					
						
						|  | [ -1, Conv, [64, 32, 3, 1]], | 
					
						
						|  | [ -1, Upsample, [None, 2, 'nearest']], | 
					
						
						|  | [ -1, Conv, [32, 16, 3, 1]], | 
					
						
						|  | [ -1, BottleneckCSP, [16, 8, 1, False]], | 
					
						
						|  | [ -1, Upsample, [None, 2, 'nearest']], | 
					
						
						|  | [ -1, Conv, [8, 2, 3, 1]] | 
					
						
						|  | ] | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  | class MCnet(nn.Module): | 
					
						
						|  | def __init__(self, block_cfg, **kwargs): | 
					
						
						|  | super(MCnet, self).__init__() | 
					
						
						|  | layers, save= [], [] | 
					
						
						|  | self.nc = 13 | 
					
						
						|  | self.detector_index = -1 | 
					
						
						|  | self.Da_out_idx = 45 if len(block_cfg)==49 else 34 | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  | for i, (from_, block, args) in enumerate(block_cfg): | 
					
						
						|  | block = eval(block) if isinstance(block, str) else block | 
					
						
						|  | if block is Detect: | 
					
						
						|  | self.detector_index = i | 
					
						
						|  | block_ = block(*args) | 
					
						
						|  | block_.index, block_.from_ = i, from_ | 
					
						
						|  | layers.append(block_) | 
					
						
						|  | save.extend(x % i for x in ([from_] if isinstance(from_, int) else from_) if x != -1) | 
					
						
						|  | self.model, self.save = nn.Sequential(*layers), sorted(save) | 
					
						
						|  | self.names = [str(i) for i in range(self.nc)] | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  | Detector = self.model[self.detector_index] | 
					
						
						|  | if isinstance(Detector, Detect): | 
					
						
						|  | s = 128 | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  | with torch.no_grad(): | 
					
						
						|  | detects, _, _= self.forward(torch.zeros(1, 3, s, s)) | 
					
						
						|  | Detector.stride = torch.tensor([s / x.shape[-2] for x in detects]) | 
					
						
						|  |  | 
					
						
						|  | Detector.anchors /= Detector.stride.view(-1, 1, 1) | 
					
						
						|  | check_anchor_order(Detector) | 
					
						
						|  | self.stride = Detector.stride | 
					
						
						|  | self._initialize_biases() | 
					
						
						|  |  | 
					
						
						|  | initialize_weights(self) | 
					
						
						|  |  | 
					
						
						|  | def forward(self, x): | 
					
						
						|  | cache = [] | 
					
						
						|  | out = [] | 
					
						
						|  |  | 
					
						
						|  | for i, block in enumerate(self.model): | 
					
						
						|  |  | 
					
						
						|  | if block.from_ != -1: | 
					
						
						|  | x = cache[block.from_] if isinstance(block.from_, int) else [x if j == -1 else cache[j] for j in block.from_] | 
					
						
						|  | x = block(x) | 
					
						
						|  | if isinstance(block, Detect): | 
					
						
						|  | out.append(x) | 
					
						
						|  | if i == self.Da_out_idx: | 
					
						
						|  | m=nn.Sigmoid() | 
					
						
						|  | out.append(m(x)) | 
					
						
						|  | cache.append(x if block.index in self.save else None) | 
					
						
						|  | """t1 = time_synchronized() | 
					
						
						|  | print(str(i) + " : " + str(t1-t0)) | 
					
						
						|  | times.append(t1-t0) | 
					
						
						|  | print(sum(times[:25])) | 
					
						
						|  | print(sum(times[25:33])) | 
					
						
						|  | print(sum(times[33:41])) | 
					
						
						|  | print(sum(times[41:43])) | 
					
						
						|  | print(sum(times[43:46])) | 
					
						
						|  | print(sum(times[46:]))""" | 
					
						
						|  | m=nn.Sigmoid() | 
					
						
						|  | out.append(m(x)) | 
					
						
						|  | return out | 
					
						
						|  |  | 
					
						
						|  | def _initialize_biases(self, cf=None): | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  | m = self.model[self.detector_index] | 
					
						
						|  | for mi, s in zip(m.m, m.stride): | 
					
						
						|  | b = mi.bias.view(m.na, -1) | 
					
						
						|  | b[:, 4] += math.log(8 / (640 / s) ** 2) | 
					
						
						|  | b[:, 5:] += math.log(0.6 / (m.nc - 0.99)) if cf is None else torch.log(cf / cf.sum()) | 
					
						
						|  | mi.bias = torch.nn.Parameter(b.view(-1), requires_grad=True) | 
					
						
						|  |  | 
					
						
						|  | class CSPDarknet(nn.Module): | 
					
						
						|  | def __init__(self, block_cfg, **kwargs): | 
					
						
						|  | super(CSPDarknet, self).__init__() | 
					
						
						|  | layers, save= [], [] | 
					
						
						|  |  | 
					
						
						|  | self.nc = 1 | 
					
						
						|  | self.detector_index = -1 | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  | for i, (from_, block, args) in enumerate(block_cfg): | 
					
						
						|  | block = eval(block) if isinstance(block, str) else block | 
					
						
						|  | if block is Detect: | 
					
						
						|  | self.detector_index = i | 
					
						
						|  | block_ = block(*args) | 
					
						
						|  | block_.index, block_.from_ = i, from_ | 
					
						
						|  | layers.append(block_) | 
					
						
						|  | save.extend(x % i for x in ([from_] if isinstance(from_, int) else from_) if x != -1) | 
					
						
						|  | self.model, self.save = nn.Sequential(*layers), sorted(save) | 
					
						
						|  | self.names = [str(i) for i in range(self.nc)] | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  | Detector = self.model[self.detector_index] | 
					
						
						|  | if isinstance(Detector, Detect): | 
					
						
						|  | s = 128 | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  | with torch.no_grad(): | 
					
						
						|  | detects, _ = self.forward(torch.zeros(1, 3, s, s)) | 
					
						
						|  | Detector.stride = torch.tensor([s / x.shape[-2] for x in detects]) | 
					
						
						|  |  | 
					
						
						|  | Detector.anchors /= Detector.stride.view(-1, 1, 1) | 
					
						
						|  | check_anchor_order(Detector) | 
					
						
						|  | self.stride = Detector.stride | 
					
						
						|  | self._initialize_biases() | 
					
						
						|  |  | 
					
						
						|  | initialize_weights(self) | 
					
						
						|  |  | 
					
						
						|  | def forward(self, x): | 
					
						
						|  | cache = [] | 
					
						
						|  | out = [] | 
					
						
						|  | for i, block in enumerate(self.model): | 
					
						
						|  | if block.from_ != -1: | 
					
						
						|  | x = cache[block.from_] if isinstance(block.from_, int) else [x if j == -1 else cache[j] for j in block.from_] | 
					
						
						|  | start = time.time() | 
					
						
						|  | x = block(x) | 
					
						
						|  | end = time.time() | 
					
						
						|  | print(start-end) | 
					
						
						|  | """y = None if isinstance(x, list) else x.shape""" | 
					
						
						|  | if isinstance(block, Detect): | 
					
						
						|  | out.append(x) | 
					
						
						|  | cache.append(x if block.index in self.save else None) | 
					
						
						|  | m=nn.Sigmoid() | 
					
						
						|  | out.append(m(x)) | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  | return out | 
					
						
						|  |  | 
					
						
						|  | def _initialize_biases(self, cf=None): | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  | m = self.model[self.detector_index] | 
					
						
						|  | for mi, s in zip(m.m, m.stride): | 
					
						
						|  | b = mi.bias.view(m.na, -1) | 
					
						
						|  | b[:, 4] += math.log(8 / (640 / s) ** 2) | 
					
						
						|  | b[:, 5:] += math.log(0.6 / (m.nc - 0.99)) if cf is None else torch.log(cf / cf.sum()) | 
					
						
						|  | mi.bias = torch.nn.Parameter(b.view(-1), requires_grad=True) | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  | def get_net(cfg, **kwargs): | 
					
						
						|  |  | 
					
						
						|  | m_block_cfg = MCnet_no_share | 
					
						
						|  | model = MCnet(m_block_cfg, **kwargs) | 
					
						
						|  | return model | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  | if __name__ == "__main__": | 
					
						
						|  | from torch.utils.tensorboard import SummaryWriter | 
					
						
						|  | model = get_net(False) | 
					
						
						|  | input_ = torch.randn((1, 3, 256, 256)) | 
					
						
						|  | gt_ = torch.rand((1, 2, 256, 256)) | 
					
						
						|  | metric = SegmentationMetric(2) | 
					
						
						|  |  | 
					
						
						|  | detects, dring_area_seg, lane_line_seg = model(input_) | 
					
						
						|  | for det in detects: | 
					
						
						|  | print(det.shape) | 
					
						
						|  | print(dring_area_seg.shape) | 
					
						
						|  | print(dring_area_seg.view(-1).shape) | 
					
						
						|  | _,predict=torch.max(dring_area_seg, 1) | 
					
						
						|  | print(predict.shape) | 
					
						
						|  | print(lane_line_seg.shape) | 
					
						
						|  |  | 
					
						
						|  | _,lane_line_pred=torch.max(lane_line_seg, 1) | 
					
						
						|  | _,lane_line_gt=torch.max(gt_, 1) | 
					
						
						|  | metric.reset() | 
					
						
						|  | metric.addBatch(lane_line_pred.cpu(), lane_line_gt.cpu()) | 
					
						
						|  | acc = metric.pixelAccuracy() | 
					
						
						|  | meanAcc = metric.meanPixelAccuracy() | 
					
						
						|  | mIoU = metric.meanIntersectionOverUnion() | 
					
						
						|  | FWIoU = metric.Frequency_Weighted_Intersection_over_Union() | 
					
						
						|  | IoU = metric.IntersectionOverUnion() | 
					
						
						|  | print(IoU) | 
					
						
						|  | print(mIoU) |