自定义咖啡损失图层时出错

时间:2017-03-19 09:13:36

标签: python neural-network deep-learning caffe pycaffe

我在使用自己的损失函数训练模型时遇到错误。

我想要实现的是:

loss function

我的实施是:

  1. train.prototxt

    layer {
      name: 'similarity_loss'
      type: 'Python'
      bottom: 'fc7'
      bottom: 'labels'
      propagate_down: 1
      propagate_down: 0
      top: 'loss_similarity'
      loss_weight: 1
      python_param {
        module: 'roi_similarity_measure.roi_similarity_loss_layer'
        layer: 'RoISimilarityLossLayer'
      }
    }
    
  2. 损失层

    import caffe
    import numpy as np
    class RoISimilarityLossLayer(caffe.Layer):
        def setup(self, bottom, top):
            # check input pair
            if len(bottom) != 2:
                raise Exception("Need two inputs to compute distance - fc7 and labels.")
            # set textual probability matrix for 20 classes
            self._num_classes = 20 + 1
            self._num_rois = (bottom[1].data.shape)[0]
            self._probs = np.ndarray(shape=(self._num_classes, self._num_classes), dtype=float, order='C')
        def reshape(self, bottom, top):
            pass
        def forward(self, bottom, top):
            self._similarity = np.zeros((128, 128), dtype=np.float)
            labels = bottom[1].data.astype(np.int)
            for i in xrange(self._num_rois):
                for j in xrange(self._num_rois):
                    self._similarity[i][j] = self._probs[labels[i]][labels[j]]
            fc7 = bottom[0].data
            fc7_euclidean = 0
            for i in xrange(self._num_rois):
                for j in xrange(self._num_rois):
                    fc7_euclidean += ((fc7[i] - fc7[j]) ** 2) * self._similarity[i][j]
            top[0].data[...] = fc7_euclidean
            top[0].diff[...] = np.zeros((128, 4096), dtype=np.float)
        def backward(self, top, propagate_down, bottom):
            for num in xrange(len(propagate_down)):
                if not propagate_down[num]:
                    continue
                D = np.zeros((128, 128), dtype=np.float)
                for i in xrange(self._num_rois):
                    D[i][i] = self._similarity[i].sum()
                    L = D - self._similarity
                # diff in this layer is calculated by : 2 * (L + transpose L)*fc7
                top[0].diff[...] = 2*(L+L.transpose()).dot(bottom[0].data)
    
  3. 错误

  4. I0320 09:01:25.520689 22184 layer_factory.hpp:77] Creating layer similarity_loss
    I0320 09:01:25.521440 22184 net.cpp:84] Creating Layer similarity_loss
    I0320 09:01:25.521452 22184 net.cpp:406] similarity_loss <- fc7_drop7_0_split_2
    I0320 09:01:25.521471 22184 net.cpp:406] similarity_loss <- labels_roi-data_1_split_1
    I0320 09:01:25.521478 22184 net.cpp:380] similarity_loss -> loss_similarity
    F0320 09:01:25.521548 22184 blob.cpp:145] Check failed: diff_ 
    *** Check failure stack trace: ***
    ./experiments/scripts/faster_rcnn_end2end.sh: line 57: 22184 Aborted (core dumped) ./tools/train_net.py --gpu ${GPU_ID} --solver models/${PT_DIR}/${NET}/faster_rcnn_end2end/solver.prototxt --weights data/imagenet_models/${NET}.v2.caffemodel --imdb ${TRAIN_IMDB} --iters ${ITERS} --cfg experiments/cfgs/faster_rcnn_end2end.yml ${EXTRA_ARGS}
    

    我是caffe的新手,这对我来说很困惑。谢谢你的帮助。

0 个答案:

没有答案