You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mxnet.apache.org by GitBox <gi...@apache.org> on 2017/11/27 14:35:12 UTC

[GitHub] dbsxdbsx opened a new issue #8833: For gluon, how to define NET (MODEL) and LOSS for multi_label?

dbsxdbsx opened a new issue #8833: For gluon, how to define NET (MODEL) and LOSS for multi_label? 
URL: https://github.com/apache/incubator-mxnet/issues/8833
 
 
   This maybe a complex question about applicaiton of mxnet.  And I really got stuck for a loooog time. I hope someone could help me, thanks~
   
   **target:**
   I want to transfer[ the multi_label net](https://github.com/xlvector/learning-dl/blob/master/mxnet/ocr/cnn_ocr.py) to gluon version. The core idea is to learn to predict 4 labels in a net. l And I have 2 problems with mxnet 0.12 on win10.
   
   First, accordingly, for gluon I wrote with hybridize like:
   ```
   class Net(gluon.HybridBlock):
       def __init__(self, **kwargs):
          super(Net, self).__init__(**kwargs)
   
           with self.name_scope():
               self.cov1 = gluon.nn.Conv2D(channels=32, kernel_size=(5, 5))
               self.cov2 = gluon.nn.Conv2D(channels=32, kernel_size=(5, 5))
               self.cov3 = gluon.nn.Conv2D(channels=32, kernel_size=(3, 3))
   
               self.max_pool = gluon.nn.MaxPool2D(pool_size=(2, 2), strides=(1, 1))
               self.avg_pool = gluon.nn.AvgPool2D(pool_size=(2, 2), strides=(1, 1))
   
               self.flatten = gluon.nn.Flatten()
               self.dense_256 = gluon.nn.Dense(256)
               self.dense_10 = gluon.nn.Dense(10)
   
       def hybrid_forward(self, F, x):
           x = F.relu(self.max_pool(self.cov1(x)))
           x = F.relu(self.max_pool(self.cov2(x)))
           for _ in range(2):
               x = F.relu(self.avg_pool(self.cov3(x)))
   
           x = self.flatten(x)
           x = F.relu(self.dense_256(x))
           x1 = self.dense_10(x)  # for 10 nums
           x2 = self.dense_10(x)
           x3 = self.dense_10(x)
           x4 = self.dense_10(x)
           out1 = F.concat(*[x1, x2, x3, x4], dim=0)
           return out1
   
           # return x1
           # maybe no need to use the following  3 statements in gluon
           # label = mx.symbol.transpose(data = label)
           # label = mx.symbol.Reshape(data = label, target_shape = (0,))
           # return mx.symbol.SoftmaxOutput(data=fc2, label=label, name="softmax")
   ```
   
   About the net:
   **Q1; As I didn't find any example to modify label when buiding net in hybrid class,  I guess it is correct to just end with `out1 = F.concat(*[x1, x2, x3, x4], dim=0)`?**
   
   Then, when defining the 'trainer' (gluon version), the core code is like:
   ```
   for i, batch in enumerate(train_iter):  # data shape = BYXC, label shape = (batchsize,)
       # get batch info and store data label on GPU
       data, label, batch_size = get_batch(batch, ctx)
       with autograd.record():
           output = net(data)  # data shape must be BCYX, output shape = (batchsize*Label_NUM,SOFTMAX_NUM)
           label = label.T.reshape((-1,)) # before: label shape = (batchsize*Label_NUM,)
           L = losser(output, label) 
       L.backward()
       trainer.step(batch_size)
   
   def get_batch(batch, ctx):
       """return data and label on ctx"""
       '''the batch shape should be BCYX'''
       if isinstance(batch, list):  # for gluon.data.DataLoader
           data, label = batch
       else:  # for (subclass) of mx.io.DataBatch
           data, label = batch.data[0], batch.label[0]
   
       return (data.as_in_context(ctx), label.as_in_context(ctx), data.shape[0])  # data.shape[0] = batch_size
       # if no use try_all_gpu(),TypeError: object of type 'Context' has no len()
       # return (gluon.utils.split_and_load(data, ctx),
       #         gluon.utils.split_and_load(label, ctx),
       #         data.shape[0])
   ```
   And here with the losser(loss function):
   **Q2: As it would not be treated as correct unless all 4 labels in 1 example are matched,then is `  label = label.T.reshape((-1,))` essential and correct  here to calculate  loss?**  by the way, even for the referred old version, I still didn't understand why the label need to transpose and flat to a long vector.
   
   

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services