Deep learning 在prototxt中使用批次大小1与在pycaffe中将批次大小强制为1时,结果存在差异

Deep learning 在prototxt中使用批次大小1与在pycaffe中将批次大小强制为1时,结果存在差异,deep-learning,caffe,pycaffe,Deep Learning,Caffe,Pycaffe,我在运行这个示例时对图层进行了一些手动更改。在训练过程中,一切都很顺利,我的最终测试准确率达到了99%。我现在正尝试使用pycaffe在python中使用生成的模型,并遵循给定的步骤。我想计算混淆矩阵,所以我从LMDB一个接一个地循环测试图像,然后运行网络。代码如下: net = caffe.Net(args.proto, args.model, caffe.TEST) ... datum = caffe.proto.caffe_pb2.Datum() datum.ParseFromString

我在运行这个示例时对图层进行了一些手动更改。在训练过程中,一切都很顺利,我的最终测试准确率达到了99%。我现在正尝试使用pycaffe在python中使用生成的模型,并遵循给定的步骤。我想计算混淆矩阵,所以我从LMDB一个接一个地循环测试图像,然后运行网络。代码如下:

net = caffe.Net(args.proto, args.model, caffe.TEST)
...
datum = caffe.proto.caffe_pb2.Datum()
datum.ParseFromString(value)
label = int(datum.label)
image = caffe.io.datum_to_array(datum).astype(np.uint8)
...
net.blobs['data'].reshape(1, 1, 28, 28) # Greyscale 28x28 images
net.blobs['data'].data[...] = image
net.forward()
# Get predicted label
print net.blobs['label'].data[0] # use this later for confusion matrix
这是我的网络定义

name: "MNISTNet"
layer {
  name: "mnist"
  type: "Data"
  top: "data"
  top: "label"
  include {
    phase: TRAIN
  }
  transform_param {
    scale: 0.00390625
  }
  data_param {
    source: "train_lmdb"
    batch_size: 64
    backend: LMDB
  }
}
layer {
  name: "mnist"
  type: "Data"
  top: "data"
  top: "label"
  include {
    phase: TEST
  }
  transform_param {
    scale: 0.00390625
  }
  data_param {
    source: "test_lmdb"
    batch_size: 100
    backend: LMDB
  }
}
layer {
  name: "conv1"
  type: "Convolution"
  bottom: "data"
  top: "conv1"
  param {
    lr_mult: 1
  }
  param {
    lr_mult: 2
  }
  convolution_param {
    num_output: 20
    kernel_size: 5
    stride: 1
    weight_filler {
      type: "xavier"
    }
    bias_filler {
      type: "constant"
    }
  }
}
layer {
  name: "relu1"
  type: "ReLU"
  bottom: "conv1"
  top: "conv1"
}
layer {
  name: "pool1"
  type: "Pooling"
  bottom: "conv1"
  top: "pool1"
  pooling_param {
    pool: MAX
    kernel_size: 2
    stride: 2
  }
}
layer {
  name: "conv2"
  type: "Convolution"
  bottom: "pool1"
  top: "conv2"
  param {
    lr_mult: 1
  }
  param {
    lr_mult: 2
  }
  convolution_param {
    num_output: 50
    kernel_size: 5
    stride: 1
    weight_filler {
      type: "xavier"
    }
    bias_filler {
      type: "constant"
    }
  }
}
layer {
  name: "relu2"
  type: "ReLU"
  bottom: "conv2"
  top: "conv2"
}
layer {
  name: "pool2"
  type: "Pooling"
  bottom: "conv2"
  top: "pool2"
  pooling_param {
    pool: MAX
    kernel_size: 2
    stride: 2
  }
}
layer {
  name: "fc1"
  type: "InnerProduct"
  bottom: "pool2"
  top: "fc1"
  param {
    lr_mult: 1
  }
  param {
    lr_mult: 2
  }
  inner_product_param {
    num_output: 500
    weight_filler {
      type: "xavier"
    }
    bias_filler {
      type: "constant"
    }
  }
}
layer {
  name: "relu3"
  type: "ReLU"
  bottom: "fc1"
  top: "fc1"
}
layer {
  name: "fc2"
  type: "InnerProduct"
  bottom: "fc1"
  top: "fc2"
  param {
    lr_mult: 1
  }
  param {
    lr_mult: 2
  }
  inner_product_param {
    num_output: 10
    weight_filler {
      type: "xavier"
    }
    bias_filler {
      type: "constant"
    }
  }
}
layer {
  name: "loss"
  type: "SoftmaxWithLoss"
  bottom: "fc2"
  bottom: "label"
}
layer {
  name: "accuracy"
  type: "Accuracy"
  bottom: "fc2"
  bottom: "label"
  top: "accuracy"
  include {
    phase: TEST
  }
}
请注意,测试批大小是100,这就是为什么我需要在python代码中进行重塑。现在,假设我将测试批处理大小更改为1,则完全相同的python代码打印不同的(并且大部分是正确的)预测类标签。因此,在批大小为1的情况下运行的代码会产生预期的结果,准确率约为99%,而批大小为100的代码则非常糟糕。 然而,基于Imagenet pycaffe教程,我看不出我做错了什么。作为最后一种手段,我可以创建一个批大小为1的prototxt副本进行测试,并在python代码中使用它,在培训时使用原始的prototxt,但这并不理想

另外,我认为预处理不应该是一个问题,因为它不能解释为什么它可以很好地处理批量1

感谢任何指点