gpt4 book ai didi

python - 以图像作为标签的 Caffe 测试网

转载 作者:行者123 更新时间:2023-12-01 02:23:53 24 4
gpt4 key购买 nike

问题

我尝试创建一个 CNN,其中使用图像作为标签,值在 0 到 1 之间。经过一些训练,我的网络损失约为 23。现在我想查看结果。为此,我使用这个 python 脚本:

import caffe
import numpy as np
from PIL import Image

net = caffe.Net('D:/caffe/net.prototxt',
'D:/caffe/net_iter_35000.caffemodel',
caffe.TEST)

# load input and configure preprocessing
transformer = caffe.io.Transformer({'data': net.blobs['data'].data.shape})

transformer.set_mean('data', np.load('train_mean.npy').mean(1).mean(1))
transformer.set_transpose('data', (2,0,1))
transformer.set_channel_swap('data', (2,1,0))
transformer.set_raw_scale('data', 255.0)

#note we can change the batch size on-the-fly
#since we classify only one image, we change batch size from 10 to 1
net.blobs['data'].reshape(1,3,360,360)

#load the image in the data layer
im = caffe.io.load_image('train/img0.png')
net.blobs['data'].data[...] = transformer.preprocess('data', im)

#compute
out = net.forward()

result = out['conv7'][0][0]

现在我期望 result 的值大约在 0 到 1 之间。但实际上 result.max() 返回 5.92,而 result.min() 返回 -4315.5。

Python 脚本中是否存在错误,或者该值对于 23 的损失是否正常?

<小时/>

其他信息

我的train_test.prototxt:

layer {
name: "mynet"
type: "Data"
top: "data0"
top: "label0"
include {
phase: TRAIN
}
transform_param {
mean_file: "train_mean.binaryproto"
scale: 0.00390625
}
data_param {
source: "train_lmdb"
batch_size: 32
backend: LMDB
}
}

layer {
name: "mynetlabel"
type: "Data"
top: "data1"
top: "label1"
include {
phase: TRAIN
}
transform_param {
scale: 0.00390625
}
data_param {
source: "train_label_lmdb_2"
batch_size: 32
backend: LMDB
}
}

layer {
name: "mnist"
type: "Data"
top: "data0"
top: "label0"
include {
phase: TEST
}
transform_param {
mean_file: "train_mean.binaryproto"
scale: 0.00390625
}
data_param {
source: "val_lmdb"
batch_size: 16
backend: LMDB
}
}
layer {
name: "mnistlabel"
type: "Data"
top: "data1"
top: "label1"
include {
phase: TEST
}
transform_param {
scale: 0.00390625
}
data_param {
source: "val_label_lmdb_2"
batch_size: 16
backend: LMDB
}
}
.
.
.
layer {
name: "conv7"
type: "Convolution"
bottom: "conv6"
top: "conv7"
param {
lr_mult: 5.0
decay_mult: 1.0
}
param {
lr_mult: 10.0
decay_mult: 0.0
}
convolution_param {
num_output: 1
pad: 0
kernel_size: 1
weight_filler {
type: "gaussian"
std: 0.00999999977648
}
bias_filler {
type: "constant"
}
}
}

layer {
name: "accuracy"
type: "Accuracy"
bottom: "conv7"
bottom: "data1"
top: "accuracy"
include {
phase: TEST
}
}

layer {
name: "loss"
type: "SigmoidCrossEntropyLoss"
bottom: "conv7"
bottom: "data1"
top: "loss"
}

我的net.prototxt:

layer {
name: "data"
type: "Input"
top: "data"
input_param { shape: { dim: 50 dim: 3 dim: 360 dim: 360 } }
transform_param {
scale: 0.00390625
}
}
.
.
.
layer {
name: "conv7"
type: "Convolution"
bottom: "conv6"
top: "conv7"
param {
lr_mult: 5.0
decay_mult: 1.0
}
param {
lr_mult: 10.0
decay_mult: 0.0
}
convolution_param {
num_output: 1
pad: 0
kernel_size: 1
weight_filler {
type: "gaussian"
std: 0.00999999977648
}
bias_filler {
type: "constant"
}
}
}

最佳答案

您的train_val.prototxt使用"SigmoidWithCrossEntropy" ,正如该层的名称所示,它(内部)包含 "Sigmoid"层和交叉熵损失。因此,在部署网络时,您应该将此层替换为 "Sigmoid" net.prototxt 文件中的图层。
请参阅this answer了解更多详情。

附注,
caffe 不支持使用 "Accuracy" 层进行单个二进制输出:"Accuracy" 层假设预测的维度等于类的数量(适用于 "SoftmaxWithLoss”)。在您的情况下,您有两个标签 {0, 1} 但输出的暗度仅为 1。请参阅 this answer了解更多详情。

关于python - 以图像作为标签的 Caffe 测试网,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47629598/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com