ultralytics / yolov3

YOLOv3 in PyTorch > ONNX > CoreML > TFLite
https://docs.ultralytics.com
GNU Affero General Public License v3.0
10.22k stars 3.45k forks source link

Can [shotcut] with activation? #1190

Closed st1312st closed 4 years ago

st1312st commented 4 years ago

Hello!

I see [shotcut] have activation in .cfg file,but i can not see shotcut use activation in code. ####################################### in .cfg [shortcut] from=-4 activation=leaky

####################################### in model.py

elif mdef['type'] == 'shortcut':  # nn.Sequential() placeholder for 'shortcut' layer
        layers = mdef['from']
        filters = output_filters[-1]
        routs.extend([i + l if l < 0 else l for l in layers])
        modules = WeightedFeatureFusion(layers=layers, weight='weights_type' in mdef)

####################################### in utils/layers.py

class WeightedFeatureFusion(nn.Module): def init(self, layers, weight=False): super(WeightedFeatureFusion, self).init() self.layers = layers # layer indices self.weight = weight # apply weights boolean self.n = len(layers) + 1 # number of layers if weight: self.w = nn.Parameter(torch.zeros(self.n), requires_grad=True) # layer weights

def forward(self, x, outputs):
    # Weights
    if self.weight:
        w = torch.sigmoid(self.w) * (2 / self.n)  # sigmoid weights (0-1)
        x = x * w[0]

    # Fusion
    nx = x.shape[1]  # input channels
    for i in range(self.n - 1):
        a = outputs[self.layers[i]] * w[i + 1] if self.weight else outputs[self.layers[i]]  # feature to add
        na = a.shape[1]  # feature channels

        # Adjust channels
        if nx == na:  # same shape
            x = x + a
        elif nx > na:  # slice input
            x[:, :na] = x[:, :na] + a  # or a = nn.ZeroPad2d((0, 0, 0, 0, 0, dc))(a); x = x + a
        else:  # slice feature
            x = x + a[:, :nx]

    return x

########################################

so can i use [shotcut] with activation? If everyone can help me, Thank you!

github-actions[bot] commented 4 years ago

Hello @st1312st, thank you for your interest in our work! Please visit our Custom Training Tutorial to get started, and see our Google Colab Notebook, Docker Image, and GCP Quickstart Guide for example environments.

If this is a bug report, please provide screenshots and minimum viable code to reproduce your issue, otherwise we can not help you.

glenn-jocher commented 4 years ago

@st1312st that is a very good discovery!

And a bit strange also, as our mAP results are great with both the other darknet-trained yolo3.weights and our newer pytorch-trained yolov3-spp-ultralytics.pt models, so it appears this omission has not negatively impacted anything.

Perhaps this means that leaky(leaky(x) + leaky(y)) is not that different that leaky(x) + leaky(y)

st1312st commented 4 years ago

OK,it is amazing ,thank you! ######################################################### elif mdef['type'] == 'shortcut': # nn.Sequential() placeholder for 'shortcut' layer layers = mdef['from'] filters = output_filters[-1] routs.extend([i + l if l < 0 else l for l in layers]) modules = WeightedFeatureFusion(layers=layers, weight='weights_type' in mdef) if mdef['activation'] == 'leaky': modules.add_module('activation', nn.LeakyReLU(0.1, inplace=True))

############################################################ Maybe this code can work.

glenn-jocher commented 4 years ago

@st1312st can you please validate the effects of the change by running test.py on coco mAP before and after? Thank you!

github-actions[bot] commented 4 years ago

This issue is stale because it has been open 30 days with no activity. Remove Stale label or comment or this will be closed in 5 days.