我编写了python代码,以编程方式生成卷积神经网络(CNN),用于训练和验证.prototxt文件在caffe中.以下是我的功能:
def custom_net(lmdb, batch_size):
# define your own net!
n = caffe.NetSpec()
# keep this data layer for all networks
n.data, n.label = L.Data(batch_size=batch_size, backend=P.Data.LMDB, source=lmdb,
ntop=2, transform_param=dict(scale=1. / 255))
n.conv1 = L.Convolution(n.data, kernel_size=6,
num_output=48, weight_filler=dict(type='xavier'))
n.pool1 = L.Pooling(n.conv1, kernel_size=2, stride=2, pool=P.Pooling.MAX)
n.conv2 = L.Convolution(n.pool1, kernel_size=5,
num_output=48, weight_filler=dict(type='xavier'))
n.pool2 = L.Pooling(n.conv2, kernel_size=2, stride=2, pool=P.Pooling.MAX)
n.conv3 = L.Convolution(n.pool2, kernel_size=4,
num_output=48, weight_filler=dict(type='xavier'))
n.pool3 = L.Pooling(n.conv3, kernel_size=2, stride=2, pool=P.Pooling.MAX)
n.conv4 = L.Convolution(n.pool3, kernel_size=2,
num_output=48, weight_filler=dict(type='xavier'))
n.pool4 = L.Pooling(n.conv4, kernel_size=2, stride=2, pool=P.Pooling.MAX)
n.fc1 = …Run Code Online (Sandbox Code Playgroud) python neural-network deep-learning caffe conv-neural-network
我有一个已经在CIFAR-10上训练过的模型,但我没有意识到如何在pycaffe中进行预测.
我从lmdb获得了一个图像,但我不知道如何在网络中加载它并获得预测的类.
我的代码:
net = caffe.Net('acc81/model.prototxt',
'acc81/cifar10_full_iter_70000.caffemodel.h5',
caffe.TEST)
lmdb_env = lmdb.open('cifar10_test_lmdb/')
lmdb_txn = lmdb_env.begin()
lmdb_cursor = lmdb_txn.cursor()
for key, value in lmdb_cursor:
datum = caffe.proto.caffe_pb2.Datum()
datum.ParseFromString(value)
image = caffe.io.datum_to_array(datum)
image = image.astype(np.uint8)
# What's next with the image variable?
# If i try:
# out = net.forward_all(data=np.asarray([image]))
# I get Exception: Input blob arguments do not match net inputs.
print("Image class is " + label)
Run Code Online (Sandbox Code Playgroud)