Caffe Examples

ImageNet数据集训练深度神经网络 最近在使用AutoDL算力云复现几个深度学习中知识蒸馏的代码,训练一个图像分类的模型时,用到了李飞飞教授的ImageNet1K数据集,所以想把这个数据集的介绍以及使用给刚入门深度学习图像分类的新手小白们分享一下,内容很简单,也算是做一个简单的笔记。ImageNet是一个大规模的图像数据库,由斯坦福大学的李飞飞教授团队创建。数据集包含超过1400万张带有标注的图像,涵盖超过2万个类别。这个数据集在计算机视觉领域中被广泛使用,特别是在图像分类、目标检测和语义分割等任务中。 阅读详情

1.ImageNet tutorial(Brewing tutorial)

目标:用自己的图片数据训练自己的model。
This guide is meant to get you ready to train your own model on your own data.
运行结束,产生:bvlc_reference_caffenet.caffemodel
数据源:ILSVRC12 challenge(可扩展)
Papline:
(1)假设数据源已经OK,如下:
/path/to/imagenet/train/n01440764/n01440764_10026.JPEG
/path/to/imagenet/val/ILSVRC2012_val_00000001.JPEG

(2)准备附加数据
使用下面这条语句可以从主页中得到caffe_ilsvrc12.tar.gz压缩包,
./data/ilsvrc12/get_ilsvrc_aux.sh

该压缩包中包含:

        det_synset_words.txt               --------  

        imagenet.bet.pickle                 --------

        imagenet_mean.binaryproto   -------- 

        synsets.txt                               --------   1000个分类及文件夹名称

        synset_words.txt                     --------   1000个分类(包含训练数据集中1000个文件夹名称和分类名称)

        test.txt                                     --------   包含10万张测试图片名称及标签(均为0)

        train.txt                                   --------   训练数据中1000个文件夹名称、文件中的图片名称 (每个分类文件中包含1300个图片)和类别编号(0-999)

        val.txt                                     --------    5万张评估照片,包含照片名称及每个照片的类别代码标签(0-999)

The training and validation input are described intrain.txtandval.txtas text listing all the files and their labels. Note that we use a different indexing for labels than the ILSVRC devkit: we sort the synset names in their ASCII order, and then label them from 0 to 999. Seesynset_words.txt for the synset/name mapping.

注:在创建.mdb数据库的时候,需要将图片进行尺寸变换,才不会出现越界的问题。在这个案例中所有的图片都调整为256*256大小。

        在新版本的caffe-master中,使用examples/imagenet/create_imagenet.sh将.jpg格式的图片转换为.mdb数据库文件。在create_imagenet.sh会有RESIZE=false一条语句,将其更改为RESIZE=true即可。

You may want to resize the images to 256x256 in advance. By default, we do not explicitly do this because in a cluster environment, one may benefit from resizing images in a parallel fashion, using mapreduce. For example, Yangqing used his lightweight mincepie package. If you prefer things to be simpler, you can also use shell commands, something like:

for name in /path/to/imagenet/val/*.JPEG; do
    convert -resize 256x256\! $name $name
done

图片大小转化完毕后,需要产生数据库.mdb文件,用以下命令:

./examples/imagenet/create_imagenet.sh.
最终会产生两个文件夹:

examples/imagenet/ilsvrc12_train_leveldb 和 examples/imagenet/ilsvrc12_val_leveldb

其中,包含data.mdb 和 lock.mdb.(注:在这个过程中可设置 GLOG_logtostderr=1来显示更多的信息,以便监视。)

至此,数据准备完毕。

(3)计算图像均值

输入图像的时候需要计算图像的均值,使用以下语句:

    The model requires us to subtract the image mean from each image, so we have to compute the mean. tools/compute_image_mean.cpp implements that - it is also a good example to familiarize yourself on how to manipulate the multiple components, such as protocol buffers, leveldbs, and logging, if you are not familiar with them. Anyway, the mean computation can be carried out as:

./examples/imagenet/make_imagenet_mean.sh
最终产生data/ilsvrc12/imagenet_mean.binaryproto文件便于后续使用。

(4)模型定义

    We are going to describe a reference implementation for the approach first proposed by Krizhevsky, Sutskever, and Hinton in their NIPS 2012 paper.

    The network definition(models/bvlc_reference_caffenet/train_val.prototxt) follows the one inKrizhevsky et al. Note that if you deviated from file paths suggested in this guide, you’ll need to adjust the relevant paths in the.prototxt files.

    If you look carefully atmodels/bvlc_reference_caffenet/train_val.prototxt, you will notice several include sections specifying either phase:TRAIN or phase:TEST. These sections allow us to define two closely related networks in one file: the networkused for training and the network used for testing. These two networks are almost iden tical, sharing all layers except for those marked with include { phase: TRAIN } or include { phase: TEST}. In this case, only the input layers and one output layer are different.

    Input layer differences: The training network’s data input layer draws its data from examples/imagenet/ilsvrc12_train_leveldb and randomly mirrors the input image. The testing network’s data layer takes data fromexamples/imagenet/ilsvrc12_val_leveldb and does not perform random mirroring.

     Output layer differences: Both networks output the softmax_loss layer, which in training is used to compute the loss function and to initialize the backpropagation, while in validation this loss is simply reported. The testing network also has a second output layer, accuracy, which is used to report the accuracy on the test set. In the process of training, the test network will occasionally be instantiated and tested on the test set, producing lines like Test score #0: xxx and Test score #1: xxx. In this case score 0 is the accuracy (which will start around 1/1000 = 0.001 for an untrained network) and score 1 is the loss (which will start around 7 for an untrained network).

    • We will run in batches of 256, and run a total of 450,000 iterations (about 90 epochs).
    • For every 1,000 iterations, we test the learned net on the validation data.
    • We set the initial learning rate to 0.01, and decrease it every 100,000 iterations (about 20 epochs).
    • Information will be displayed every 20 iterations.
    • The network will be trained with momentum 0.9 and a weight decay of 0.0005.
    • For every 10,000 iterations, we will take a snapshot of the current status.

Sound good? This is implemented inmodels/bvlc_reference_caffenet/solver.prototxt.

(5)训练ImageNet

训练的网络结构如下所示:


(6)实例


分类:cat

2.Training LeNet on MNIST with Caffe




3.CIFAR-10 tutorial

目标:Train and test Caffe on CIFAR-10 data. (This example reproducesAlex Krizhevsky'paper results in Caffe.) 
来源:Learning Multiple Layers of Features from Tiny Images(Alex Krizhevsky April 8, 2009)
数据集:CIFAR-10   (http://www.cs.toronto.edu/~kriz/cifar.html)

Model:
      The CIFAR-10 model is a CNN that composes layers of convolution, pooling, rectified linear unit (ReLU) nonlinearities, and local contrast normalization with a linear classifier on top of it all. We have defined the model in theCAFFE_ROOT/examples/cifar10 directory’s cifar10_quick_train_test.prototxt.
      cifar10 directory’s cifar10_quick_train_test.prototxt
网络结构如下:


4.Feature extraction with Caffe C++ code

Target:
         Extract CaffeNet / AlexNet features using the Caffe utility

Info:

        In this tutorial, we will extract features using a pre-trained model with the included C++ utility. Note that we recommend using the Python interface for this task, as for example in the filter visualization example.

Pipline:

(1)创建临时文件夹用来存储特征文件

mkdir examples/_temp

(2)Generate a list of the files to process. We’re going to use the images that ship with caffe.

find `pwd`/examples/images -type f -exec echo {} \; > examples/_temp/temp.txt

(3)The ImageDataLayerwe’ll use expects labels after each filenames, so let’s add a 0 to the end of each line

sed "s/$/ 0/" examples/_temp/temp.txt > examples/_temp/file_list.txt

(4)Let’s copy and modify the network definition. We’ll be using the ImageDataLayer, which will load and resize images for us.

cp examples/feature_extraction/imagenet_val.prototxt examples/_temp

(5)Extract Features

./build/tools/extract_features.bin models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel examples/_temp/imagenet_val.prototxt fc7 examples/_temp/features 10 leveldb

The name of feature blob that you extract is fc7, which represents thehighest level feature of the reference model. We can use any other layer, as well, such asconv5 or pool3.
The last parameter above is the number of data mini-batches.
The features are stored to LevelDBexamples/_temp/features, ready for access by some other code.
If you meet with the error “Check failed: status.ok() Failed to open leveldb examples/_temp/features”, it is because the directory examples/_temp/features has been created the last time you run the command. Remove it and run again.

rm -rf examples/_temp/features/

(6)If you’d like to use the Python wrapper for extracting features, check out the filtervisualization notebook.

5.CaffeNet C++ Classification example(Classifying ImageNet: using the C++ API)

Target:
         A simple example performing image classification using the low-level C++ API.

Info:

        Caffe, at its core, is written in C++. It is possible to use the C++ API of Caffe to implement an image classification application similar to the Python code presented in one of the Notebook example. To look at a more general-purpose example of the Caffe C++ API, you should study the source code of the command line toolcaffe intools/caffe.cpp.

Example:

(1) Presentation

        A simple C++ code is proposed inexamples/cpp_classification/classification.cpp. For the sake of simplicity, this example does not support oversampling of a single sample nor batching of multiple independant samples. This example is not trying to reach the maximum possible classification throughput on a system, but special care was given to avoid unnecessary pessimization while keeping the code readable.

(2) Compiling

      The C++ example is built automatically when compiling Caffe. To compile Caffe you should follow the documented instructions. The classification example will be built asexamples/classification.bin in your build directory.

(3) Usage

        To use the pre-trained CaffeNet model with the classification example, you need to download it from the “Model Zoo” using the following script:./scripts/download_model_binary.py models/bvlc_reference_caffenet. The ImageNet labels file (also called the synset file) is also required in order to map a prediction to the name of the class:./data/ilsvrc12/get_ilsvrc_aux.sh. 

        Using the files that were downloaded, we can classify the provided cat image (examples/images/cat.jpg) using this command:

./build/examples/cpp_classification/classification.bin models/bvlc_reference_caffenet/deploy.prototxt models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel data/ilsvrc12/imagenet_mean.binaryproto data/ilsvrc12/synset_words.txt examples/images/cat.jpg
The output should look like this:

---------- Prediction for examples/images/cat.jpg ---------- 0.3134 - "n02123045 tabby, tabby cat" 0.2380 - "n02123159 tiger cat" 0.1235 - "n02124075 Egyptian cat" 0.1003 - "n02119022 red fox, Vulpes vulpes" 0.0715 - "n02127052 lynx, catamount"

(3) Improving Performance

To further improve performance, you will need to leverage the GPU more, here are some guidelines:

Move the data on the GPU early and perform all preprocessing operations there.

If you have many images to classify simultaneously, you should use batching (independent images are classified in a single forward pass).

Use multiple classification threads to ensure the GPU is always fully utilized and not waiting for an I/O blocked CPU thread.

5.CaffeNet C++ Classification example(Classifying ImageNet: using the C++ API)

























【机器学习】ImageNet的基本概念以及如何使用ImageNet数据集 ImageNet是一个大规模的视觉数据库,旨在为研究人员和开发者提供一个丰富的资源,以促进计算机视觉领域的发展。该项目始于2007年,由斯坦福大学的教授李飞飞领导,并于2009年在计算机视觉与模式识别会议(CVPR)上发布。ImageNet根据WordNet的层级结构(目前仅限于名词)组织,其中每个层级节点都由成百上千张图像来描绘 阅读详情

相关推荐

解析./build/tools/caffe train --solver=examples/mnist/lenet_solver.prototxt

解析./build/tools/caffe train --solver=examples/mnist/lenet_solver.prototxt

大山的专栏 3237

【PyTorch】ImageNet数据集的使用和miniImageNet的构建

ImageNet是由斯坦福大学等机构从2007年着手开始组件的大型计算机视觉数据集。自从2009年发布以来,已经成为了计算机视觉领域广泛用于指标评价的数据集。直到目前,该数据集有超过1400万张图像,是深度学习领域中图像分类、检测、定位的最常用数据集之一。“ImageNet大型视觉识别任务”,即ImageNetLarge-ScaleVisualRecognitionChallenge,是基于ImageNet的一项比赛。使用的数据集是ImageNet的子集。

Ace2NoU的博客 1万+

caffe源码阅读(一)---examples/cpp_classification/classification.cpp

前言: 这个一个使用C++代码进行分类的简单例子,为了让我们的应用更加广泛,还需要阅读tool/caffe.cpp 描述: 这个例子不支持过采样(oversampling of a single sample)也不支持多个独立样本的批量处理(batching of multiple independent samples),这样是为了使得代码可读性更好。 编译: 会有个.bin文件 刚才运行了一下...

小膜仙的博客 610

ImageNet数据集简介与下载详细步骤

   ImageNet是一种数据集,而不是神经网络模型。斯坦福大学教授李飞飞为了解决机器学习中过拟合和泛化的问题而牵头构建的数据集。该数据集从2007年开始手机建立,直到2009年作为论文的形式在CVPR 2009上面发布。直到目前,该数据集仍然是深度学习领域中图像分类、检测、定位的最常用数据集之一。    基于ImageNet有一个比赛,从2010年开始举行,到2017年最后一届结束。该比赛称为ILSVRC,全称是ImageNet Large-Scale Visual Recognition Challe

qq_36665989的博客 21万+

caffe学习笔记(二)——caffe结构分析

一、caffe目录结构: data —— 用于存放下载的训练数据 docs —— 帮助文档 examples —— 代码样例 matlab —— MATLAB接口文件 python —— PYTHON接口文件 models —— 一些配置好的模型参数 scripts —— 一些文档和数据会用到的脚本 核心代码: tools —— 保存的源码是用于生成二进制处理程序的,caffe在训练时实际是直接调

孤旅人。。。的博客 3256

Pspice安装教程:Windows系统完整指南(含环境变量设置)

详细讲解在Windows系统中完成Pspice安装的全过程,涵盖安装步骤与关键的环境变量设置,帮助用户顺利运行仿真工具。适合初学者快速上手pspice安装教程中的核心环节。

weixin_42230607的博客 1188

ImageNet数据集介绍

ImageNet是一个庞大的视觉对象识别数据库,由斯坦福大学的计算机科学系开发。它是一个包含超过1400万张图像的大规模图像数据库,用于训练和评估计算机视觉算法。ImageNet数据集的目标是识别和分类图像中的各种物体和场景。它涵盖了从动物到交通工具等各个领域的图像。ImageNet数据集是一个规模庞大且多样性的图像数据库,用于训练和评估计算机视觉算法。它具有高质量的标注和挑战性的任务,广泛应用于图像分类、目标检测和图像生成等领域。

牛肉胡辣汤 7403

详细Ubuntu 22.04 配置最新UR机械臂仿真环境(ros2-humble, ur-sim, ur-ros2-driver)

使用ubuntu22.04.01和ur-sim, ur-driver进行ur机械臂的仿真

verloge的博客 1万+

caffe自带examples里面的数据的训练以及测试

我的caffe安装是在cpu模式下进行的 1.使用MNIST数据集进行训练 下面指令如果没有特殊说明都是在caffe目录下进行的 (1)首先需要获取数据: (caffe自带的数据不是以数据的形式存在,而是以一个脚本文件存在,因此,你需要先运行脚本文件获取数据)步骤如下: 先到caffe下的以下路径: cddata/mnist 运行: sudo sh./get_mnist.sh

晴天 822

离线反激转换器反馈环路设计:从理论到实践的Type II补偿器详解

在开关电源设计中,反馈环路稳定性是决定电源性能与可靠性的核心。其本质是通过补偿网络对功率级的小信号特性进行校正,以构建一个具有足够相位裕度和增益裕度的闭环系统。反激转换器作为一种广泛应用的隔离拓扑,其功率级在连续导通模式(CCM)和非连续导通模式(DCM)下呈现不同的零极点特性,这直接决定了补偿器的选型。Type II补偿器因其传递函数中固有的积分环节、低频零点与高频极点,恰好能与反激功率级的1P2Z(一个极点、两个零点)特性形成互补,有效塑造理想的回路增益曲线,实现快速稳定的动态响应。本文聚焦于采用TL4

weixin_30162121的博客 520

YOLO26改进 - C2PSA | C2PSA融合CPIASA跨范式交互与对齐自注意力机制 : 交互对齐机制,提升小目标与遮挡目标判别力 | ACM MM2025

本文介绍了跨范式表征与对齐Transformer(CPRAformer)及其核心的跨范式交互与对齐自注意力机制(CPIA - SA),并阐述了其在YOLO26中的结合应用。传统图像去雨模型特征提取局限大,CPIA - SA通过协同“空间 - 通道”与“全局 - 局部”范式解决该问题,由SPC - SA、SPR - SA和AAFM子模块构成,经双路提取、动态过滤、空间细化、对齐融合四步工作。我们将相关代码引入指定目录,在`ultralytics/nn/tasks.py`中注册,配置`yolo26 - C2PS

魔改工程师的博客 669

dlunion caffe加入hard examples 训练 R-Net,O-Net

dlunion caffe加入hard examples 训练 R-Net,O-Net 上一篇提到,dlunion作者的caffe训练MTCNN并没有加入hard examples,所里这里将加入hard examples 训练R-Net和O-Net. 生成R-Net的训练数据 执行 python create_hard1.py create_hard1.py的代码如下: import sys ...

工地搬砖第一年的博客 360

从 VS Code 登录 ABAP Destination,一次把前后端连接跑通

最近很多 ABAP 开发者开始把 VS Code 纳入日常开发环境,原因很直接,VS Code 的轻量体验、命令面板、文件树、插件生态和 AI 编程能力,已经深深影响了现代开发者的工作方式。SAP 官方发布的 ABAP Development Tools for VS Code,也把 ABAP Cloud 开发模型带进了这个编辑器里。

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 236

caffe 学习系列之examples-CIFAR

 安装完Caffe后,如何开始学习Caffe呢?一个不错的方法就是从Caffe自带的examples开始学起。在caffe安装的根目录下可以找到examples这个文件夹,这个文件里就包括了一些实例。本篇笔记就是使用CIFAR-10进行训练学习。一、预备知识1.深度学习中经常看到epoch、 iteration和batchsize,下面按自己的理解说说这三个的区别:    (1)batchsize...

qq_34650787的博客 256

7.91版本unidbg调用方式

【代码】xhs最新7.91版本unidbg调用方式。

Codeooo 博客 1113

在blender中安装vtk插件

Blender 是一个开源的、功能强大的3D计算机图形软件,广泛用于创建动画片、视觉效果、艺术作品、3D打印模型、互动3D应用程序和视频游戏。Blender 是由 Blender 基金会开发和维护的,具有丰富的功能和灵活的扩展性。在github上下载zip插件。

如果想成为中心,那么就到中心去吧。 301

caffe 04 win10 运行examples的mnist测试用例

01 资料caffe官网Examples中有关于mnist测试的详细介绍。http://caffe.berkeleyvision.org/gathered/examples/mnist.html。 caffe github下载的代码中有关于mnist的网络定义示例和下载数据sh文件。 目录 文件类型 文件作用 examples\mnist *.prototxt caffe 网

一朵花开的时间 2340

【ArcGIS Engine二次开发】入门基础(2):ArcGIS开发方式(VBA、DLL、Add-in、Engine)对比

文章目录2.1 使用VBA进行桌面软件开发2.1.1 VBA开发方式2.1.1 VBA代码的安全性2.2 使用DLL进行桌面软件开发2.2.1 DLL开发方式2.2.2 DLL功能的应用部署2.3 使用Add-in进行桌面软件开发2.4 使用ArcGIS Engine构建独立应用程序 对于桌面GIS应用来说,ArcGIS平台不仅提供了用于开发独立运行的GIS应用软件的二次开发工包ArcGIS Engine,还提供了对ArcGIS Deskop桌面软件进行扩展的开发的话,在开发个具体的GIS 行业应用软件时

智慧空间实验室 6521

caffe 06 win10 运行examples的imagenet测试用例

01 资料caffe官网Examples中有关于ImageNet测试的详细介绍。 http://caffe.berkeleyvision.org/gathered/examples/imagenet.html caffe github下载的代码中有关于ImageNet的网络定义示例和下载数据sh文件。 目录 文件类型 文件作用 data\ilsvrc12 get_ilsv

一朵花开的时间 1043

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

caffe 05 win10 运行examples的cifar10测试用例

01 资料caffe官网Examples中有关于cifar10测试的详细介绍。 http://caffe.berkeleyvision.org/gathered/examples/cifar10.html caffe github下载的代码中有关于cifar10的网络定义示例和下载数据sh文件。 目录 文件类型 文件作用 examples\cifar10 *.prototxt

一朵花开的时间 1173

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

windows下python配置caffe接口

配置电脑情况: windows7 64位,无GPU,anaconda2,  python2.7,  VS 2013 之前已经成功安装并配置了VS2013+caffe,在此基础上想用python import caffe。 之前的配置过程请参照:【caffe-Windows7】caffe+VS2013+Windows7无GPU配置教程 现在开始介绍解决python中import ca

VivienFu的专栏 1万+

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

caffe-ssd 检测视频并保存

caffe-ssd版本的代码中 /examples下有ssd_detect.ipynd的图像检测实例 /examples/ssd/下有ssd_pascal_video.py的视频检测实例,但没有保存视频接口 博主将图像检测的修改为为视频接口并保存,代码如下: import numpy as np import matplotlib.pyplot as plt import cv2 #%matpl...

Rock的博客 1075

caffe框架源码目录结构

先自顶向下捋清楚框架结构: 工程目录下文件夹 data/ 用于存放下载的训练数据 docs/ 帮助文档 examples/ 代码样例 matlab/ MATLAB接口文件 python/ PYTHON接口文件 models/ 一些配置好的模型参数 scripts/ 一些文档和数据会用到的脚本 tools/ 保存的是用于生成二进制处...

青天白鹭的博客 615

Caffe 运行MNIST报错:./train_lenet.sh: 4: ./train_lenet.sh: ./build/tools/caffe: not found

报错入下图所示: 产生bug原因: 是因为你在examples/mnist/ 目录下执行脚本.  解决办法: 回退到caffe目录下, 执行 ./examples/mnist/train_lenet.sh

CVer 754

caffe模型文件解析_Caffe学习系列(10):命令行解析

caffe的运行提供三种接口:c++接口(命令行)、python接口和matlab接口。本文先对命令行进行解析,后续会依次介绍其它两个接口。caffe的c++主程序(caffe.cpp)放在根目录下的tools文件夹内, 当然还有一些其它的功能文件,如:convert_imageset.cpp, train_net.cpp, test_net.cpp等也放在这个文件夹内。经过编译后,这些文件都被编...

weixin_34501627的博客 256
上一篇: 使用Python中draw_net.py画网络结构图
下一篇: Caffe-Caffe Models
Hi_Panda_CRL
博客等级 码龄16年 91粉丝 104原创
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值