facebookarchive / caffe2

Caffe2 is a lightweight, modular, and scalable deep learning framework.
https://caffe2.ai
Apache License 2.0
8.42k stars 1.94k forks source link

Model building question #2462

Open joey3639570 opened 6 years ago

joey3639570 commented 6 years ago

I'm new to caffe2 and deep learning. I was building my resnet152 model, and I was stuck on building "scale layer" and "element wise layer". Here's part of my codes.

res2a_branch2a =brew.conv(model,pool1,'res2a_branch2a',dim_in=64,dim_out=64,kernel=1,stride=1)
    bn2a_branch2a =brew.spatial_bn(model,res2a_branch2a,'bn2a_branch2a')
    scale2a_branch2a =brew.scale(model,bn2a_branch2a,'scale2a_branch2a')
    res2a_branch2a_relu =brew.relu(model,scale2a_branch2a,'res2a_branch2a')
    #
    res2a_branch2b =brew.conv(model,res2a_branch2a_relu,'res2a_branch2b',dim_in=64,dim_out=64,kernel=3,pad=1,stride=1)
    bn2a_branch2b =brew.spatial_bn(model,res2a_branch2b,'bn2a_branch2b')
    scale2a_branch2b =brew.scale(model,bn2a_branch2b,'scale2a_branch2b')
    res2a_branch2b_relu =brew.relu(model,scale2a_branch2b,'res2a_branch2b_relu')
    #
    res2a_branch2c =brew.conv(model,res2a_branch2b_relu,'res2a_branch2c',dim_in=64,dim_out=256,kernel=1,stride=1)
    bn2a_branch2c =brew.spatial_bn(model,res2a_branch2c,'bn2a_branch2c')
    scale2a_branch2c =brew.scale(model,bn2a_branch2c,'scale2a_branch2c')
    ##left branch(1)
    res2a_branch1 =brew.conv(model,pool1,'res2a_branch1',dim_in=64,dim_out=256,kernel=1,stride=1)
    bn2a_branch1 =brew.spatial_bn(model,res2a_branch2a,'bn2a_branch1')
    scale2a_branch1 =brew.scale(model,bn2a_branch2a,'scale2a_branch1')
    ###elementwise sum
    res2a=brew.elementwise_linear(model)
    res2a_relu =brew.relu(model,res2a,'res2a_relu')

The first question is how to build scale layer. I found out that I can't use brew to build scale layer. What should I do or import to build a scale layer?

The second question is for the elementwise layer. Although I know I can use brew to build it, I don't know what to type in. I hope that someone can show me an example of using them.

JiYuanFeng commented 6 years ago

In some caffe version, they use the BatchNorm layer and a Scale layer to implement the BN, And I think in caffe2, the BN layer just rely on the spatial_bn which is shown in https://github.com/caffe2/caffe2/blob/929c33cee0942e05dd36be465ad04e7625f3715f/caffe2/python/models/resnet.py so just add the spaital_bn!

joey3639570 commented 6 years ago

@JiYuanFeng Thank you so much! This helps me a lot! I think that all I need to do now is to solve the problem of element wise layer. Thank you again! Btw, It seems that I can't use your link, could you post it again?