首页 > 解决方案 > Caffe 提供的 AlexNet 模型

问题描述

我一直在使用 TensorFlow,但我是 Caffe 的新手。我想尝试一个在 ImageNet 上训练的 AlexNet 的可靠实现,我发现一个包含在官方 Caffe 存储库中。

我能够在非常短的 Caffe 代码中将bvlc_alexnet.caffemodel文件中打包的权重和deploy.prototxt中指定的模型链接起来,并获得一个输出向量,该向量具有 1000 个概率,对应于网络将图像分类到的 1000 个类别。

import numpy as np
import matplotlib.pyplot as plt
import sys
import caffe
import operator

MODEL_FILE = 'D:\\Desktop\\caffe\\models\\bvlc_alexnet\\deploy.prototxt'
PRETRAINED = 'D:\\AlexNet_Caffe\\bvlc_alexnet.caffemodel'

net = caffe.Classifier(MODEL_FILE, PRETRAINED)


IMAGE_FILE = 'D:\\Desktop\\caffe\\python\\testIm1.jpg'
input_image1 = caffe.io.load_image(IMAGE_FILE)

IMAGE_FILE = 'D:\\Desktop\\caffe\\python\\testIm2.jpg'
input_image2 = caffe.io.load_image(IMAGE_FILE)


pred = net.predict([input_image1, input_image2])
print pred # prints the array of 1000 probabilities


index, value = max(enumerate(pred[0]), key=operator.itemgetter(1))

print index # prints the index of max probability
print value # prints the max probability


index, value = max(enumerate(pred[1]), key=operator.itemgetter(1))

print index # prints the index of max probability
print value # prints the max probability

在我的情况下,我能够只指定模型和权重并获得输出,但是,无论我输入什么图像,输出似乎都是相同的 (669)。

在 Caffe 存储库中,有一个用于获取 ImageNet 数据集的脚本。它下载和提取的 tarball 包含以下文件:

$ ls -al
total 80395
drwxr-xr-x 1 root 197121        0 Aug  8 14:09 ./
drwxr-xr-x 1 root 197121        0 Aug  7 16:27 ../
-rw-r--r-- 1 root 197121      187 Feb 25  2014 ._imagenet_mean.binaryproto
-rw-r--r-- 1 root 197121      187 Apr  8  2014 ._synset_words.txt
-rw-r--r-- 1 root 197121      187 Feb 25  2014 ._synsets.txt
-rw-r--r-- 1 root 197121      187 Feb 25  2014 ._test.txt
-rw-r--r-- 1 root 197121      187 Feb 25  2014 ._train.txt
-rw-r--r-- 1 root 197121      187 Feb 25  2014 ._val.txt
-rw-r--r-- 1 root 197121 17858008 Aug  8 14:09 caffe_ilsvrc12.tar.gz
-rw-r--r-- 1 root 197121     3787 Jun  8  2014 det_synset_words.txt
-rwxr-xr-x 1 root 197121      610 Aug  8 13:41 get_ilsvrc_aux.sh*
-rw-r--r-- 1 root 197121 14931117 Jul 11  2014 imagenet.bet.pickle
-rw-r--r-- 1 root 197121   786446 Feb 25  2014 imagenet_mean.binaryproto
-rw-r--r-- 1 root 197121    31675 Apr  8  2014 synset_words.txt
-rw-r--r-- 1 root 197121    10000 Feb 25  2014 synsets.txt
-rw-r--r-- 1 root 197121  3200000 Feb 25  2014 test.txt
-rw-r--r-- 1 root 197121 43829433 Feb 25  2014 train.txt
-rw-r--r-- 1 root 197121  1644500 Feb 25  2014 val.txt

我不确定我是否还需要使用这些文件imagenet_mean.binaryprotoimagenet.bet.pickle

有人可以澄清一下吗?

标签: pythonmodeldeep-learningcaffeimagenet

解决方案


您需要减去图像均值。

当训练深度模型时,输入通常被归一化为大致均值 = 0。在 Caffe 的 AlexNet 中,图像均值保存在imagenet_mean.binaryproto.

请参阅此示例,了解如何使用预训练模型获取分类结果。


推荐阅读