- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我用图像训练了一个模型。现在想将 fc-6
功能提取到 .npy
文件中。我正在使用 caffe.set_mode_gpu()
运行 caffe.Classifier
并提取特征。
而不是每帧提取和保存特征。我将一个文件夹的所有功能保存到一个临时变量中,并将完整视频的结果保存到一个 npy 文件中(减少对磁盘的写入操作次数)。
我还听说我可以使用 Caffe.Net 然后传递一批图像。但我不确定必须进行哪些预处理以及这样做是否更快?
import os
import shutil
import sys
import glob
from multiprocessing import Pool
import numpy as np
import os, sys, getopt
import time
def keep_fldrs(path,listr):
ll =list()
for x in listr:
if os.path.isdir(path+x):
ll.append(x)
return ll
def keep_img(path,listr):
ll = list()
for x in listr:
if os.path.isfile(path+str(x)) & str(x).endswith('.jpg'):
ll.append(x)
return ll
def ifdir(path):
if not os.path.isdir(path):
os.makedirs(path)
# Main path to your caffe installation
caffe_root = '/home/anilil/projects/lstm/lisa-caffe-public/python'
# Model prototxt file
model_prototxt = '/home/anilil/projects/caffe2tensorflow/deploy_singleFrame.prototxt'
# Model caffemodel file
model_trained = '/home/anilil/projects/caffe2tensorflow/snapshots_singleFrame_flow_v2_iter_55000.caffemodel'
sys.path.insert(0, caffe_root)
import caffe
caffe.set_mode_gpu()
net = caffe.Classifier(model_prototxt, model_trained,
mean=np.array([128, 128, 128]),
channel_swap=(2,1,0),
raw_scale=255,
image_dims=(255, 255))
Root='/media/anilil/Data/Datasets/UCf_scales/ori_mv_vis/Ori_MV/'
Out_fldr='/media/anilil/Data/Datasets/UCf_scales/ori_mv_vis/feat_fc6/'
allcalsses=keep_fldrs(Root,os.listdir(Root))
for classin in allcalsses:
temp_class=Root+classin+'/'
temp_out_class=Out_fldr+classin+'/'
ifdir(temp_out_class)
allvids_folders=keep_fldrs(temp_class,os.listdir(temp_class))
for each_vid_fldr in allvids_folders:
temp_pres_dir=temp_class+each_vid_fldr+'/'
temp_out_pres_dir=temp_out_class+each_vid_fldr+'/'
ifdir(temp_out_pres_dir)
all_images=keep_img(temp_pres_dir,os.listdir(temp_pres_dir))
frameno=0
if os.path.isfile(temp_out_pres_dir+'video.npy'):
continue
start = time.time()
temp_npy= np.ndarray((len(all_images),4096),dtype=np.float32)
for each_image in all_images:
input_image = caffe.io.load_image(temp_pres_dir+each_image)
prediction = net.predict([input_image],oversample=False)
temp_npy[frameno,:]=net.blobs['fc6'].data[0]
frameno=frameno+1
np.save(temp_out_pres_dir+'video.npy',temp_npy)
end = time.time()
print "lenght of imgs {} and time taken is {}".format(len(all_images),(end - start))
print ('Class {} done'.format(classin))
lenght of imgs 426 and time taken is 388.539139032
lenght of imgs 203 and time taken is 185.467905998
最佳答案
我在 this post. 中找到了最佳答案
到现在为止我用过一个
net = caffe.Classifier(model_prototxt, model_trained,
mean=np.array([128, 128, 128]),
channel_swap=(2,1,0),
raw_scale=255,
image_dims=(255, 255))
初始化模型并获取每张图像的输出。但这种方法非常慢,每张图像需要大约 0.9 秒。
最好的想法是传递一批图像(可能是 100,200,250)改变。取决于您的 GPU 上有多少内存。
为此我设置了 caffe.set_mode_gpu()
因为我有一个并且当你发送大批量时它会更快。使用您训练的模型初始化模型。
net=caffe.Net(model_prototxt,model_trained,caffe.TEST)
创建一个 Transformer 并确保根据您训练模型的方式设置均值和其他值。
transformer = caffe.io.Transformer({'data': net.blobs['data'].data.shape})
transformer.set_transpose('data', (2,0,1)) # height*width*channel -> channel*height*width
mean_file = np.array([128, 128, 128])
transformer.set_mean('data', mean_file) #### subtract mean ####
transformer.set_raw_scale('data', 255) # pixel value range
transformer.set_channel_swap('data', (2,1,0)) # RGB -> BGR
data_blob_shape = net.blobs['data'].data.shape
data_blob_shape = list(data_blob_shape)
读取一组图像并转换为网络输入。
net.blobs['data'].reshape(len(all_images), data_blob_shape[1], data_blob_shape[2], data_blob_shape[3])
images = [temp_pres_dir+str(x) for x in all_images]
net.blobs['data'].data[...] = map(lambda x:
transformer.preprocess('data',caffe.io.load_image(x)), images)
通过网络传递一批图像。
out = net.forward()
您可以根据需要使用此输出。
关于python - Caffe 特征提取太慢? caffe.Classifier 或 caffe.Net,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36405677/
我想将 Bagging 方法生成的分类器直接复制到 EnsembleSelection 分类器,而不将其复制到我的代码下面的文件中: data.setClassIndex(datamoufi.numA
先运行标准缩放器再运行分类器是否会产生与使用流水线相同的结果? 您好,我有一个分类问题并尝试使用 scikit learn 的 StandardScaler() 缩放 X 变量。我看到这样做有两种选择
本文整理了Java中weka.classifiers.rules.ZeroR类的一些代码示例,展示了ZeroR类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等
这个问题是同一线程的延续 here .以下是本书中的一个最小工作示例: Wehrens R. Chemometrics with R multivariate data analysis in the
您能否帮助了解与 Haar 分类器培训相关的几点: 1)正图像应该只包含训练对象还是可以包含其他一些对象?就像我想识别一些交通标志一样,正面图像应该只包含交通标志还是也可以包含高速公路? 2)创建样本
首先,我想通知这里有人问了同样的问题 grails plugin dependencies with classifiers但他们没有答案。 我正在尝试将 jar "json-lib.jar"及其依赖
我正在创建一个手势识别器,它使用前馈神经网络对手势进行分类。 问题是我希望实时识别手势。 我从过去 60 个记录的帧中创建一个输入 vector ,如果用户做出了手势,则神经网络可以对输入进行分类,并
我在我的应用程序的几个模块中使用了 Guava 。我在 Tomcat 上部署这个应用程序,所以我在我的模块中使用了这个依赖项 com.google.guava guava 现在我需
我正在尝试创建一个 Haar 分类器来识别对象,但我似乎无法弄清楚每个阶段生成的结果表代表什么。 例如1 ===== TRAINING 1-stage ===== 例如2 ===== TRAININ
我是 Weka 的新人。我想使用Weka自训练模型。我在创建项目时导入了weka.jar。但我想知道如何解决这个问题?预先感谢您的帮助。 import java.io.File; import wek
我已经根据 IBM 的预配置分类器对图像进行了分类。 现在我尝试创建并使用我自己的分类器(称为“圣诞老人”)来识别圣诞老人的图像: VisualRecognition service = ne
我正在尝试使用 scikit-learn 构建一个简单的 SVM 文档分类器,我正在使用以下代码: import os import numpy as np import scipy.sparse a
我想编写一个函数,其中一个输入表示为NumPy数组。该函数应将矩阵分类为(I)一对一、(Ii)On、(Iii)两者(即可逆)或(Iv)两者都不是。它应该返回以字符串形式表示的分类(即“一对一”、“到”
我想编写一个函数,其中一个输入表示为NumPy数组。该函数应将矩阵分类为(I)一对一、(Ii)On、(Iii)两者(即可逆)或(Iv)两者都不是。它应该返回以字符串形式表示的分类(即“一对一”、“到”
本文整理了Java中weka.classifiers.rules.ZeroR.()方法的一些代码示例,展示了ZeroR.()的具体用法。这些代码示例主要来源于Github/Stackoverflow/
本文整理了Java中weka.classifiers.rules.ZeroR.buildClassifier()方法的一些代码示例,展示了ZeroR.buildClassifier()的具体用法。这些
本文整理了Java中weka.classifiers.rules.ZeroR.getCapabilities()方法的一些代码示例,展示了ZeroR.getCapabilities()的具体用法。这些
本文整理了Java中weka.classifiers.rules.ZeroR.distributionForInstance()方法的一些代码示例,展示了ZeroR.distributionForIn
本文整理了Java中weka.classifiers.rules.ZeroR.toString()方法的一些代码示例,展示了ZeroR.toString()的具体用法。这些代码示例主要来源于Githu
我下载 Resnet18 模型来训练模型。 当我输入时 model 显示 ResNet( (conv1): Conv2d(3, 64, kernel_size=(7, 7), stride=(2,
我是一名优秀的程序员,十分优秀!