caffe学习系列:python代码收藏

1.批量修改(重命名)文件名

# -*- coding: cp936 -*-
import os
path = 'D:\\图片\\'
for file in os.listdir(path):
   if os.path.isfile(os.path.join(path,file))==True:
        if file.find('.')<0:
            newname=file+'rsfdjndk.jpg'
            os.rename(os.path.join(path,file),os.path.join(path,newname))
            print file,'ok'
#        print file.split('.')[-1]

2.c++extract_features.bin提取层特征并保存

extract_features.bin \
pretrained_net_param  feature_extraction_proto_file \
extract_feature_blob_name1[,name2,...]  save_feature_dataset_name1[,name2,...] \
num_mini_batches  db_type  [CPU/GPU] [DEVICE_ID=0]

参数1 extract_features.bin 文件路径;
参数2模型参数(.caffemodel)文件的路径。
参数3是描述网络结构的prototxt文件。程序会从参数1的caffemodel文件里找对应名称的layer读取参数。
参数4是需要提取的blob名称,对应网络结构prototxt里的名称。blob名称可以有多个,用逗号分隔。每个blob提取出的特征会分开保存。
参数5是保存提取出来的特征的数据库路径,可以有多个,和参数3中一一对应,以逗号分隔。如果用LMDB的话,路径必须是不存在的(已经存在的话要改名或者删除)。
参数6是提取特征所需要执行的batch数量。这个数值和prototxt里DataLayer中的Caffe的DataLayer(或者ImageDataLayer)中的batch_size参数相乘,就是会被输入网络的总样本数。设置参数时需要确保batch_size * num_mini_batches等于需要提取特征的样本总数,否则提取的特征就会不够数或者是多了。(注意:这里的数值是num_mini_batches,而不是样本总数)
参数7是保存特征使用的数据库类型,支持lmdb和leveldb两种(小写)。推荐使用lmdb,因为lmdb的访问速度更快,还支持多进程同时读取。
参数8决定使用GPU还是CPU,直接写对应的三个大写字母就行。省略不写的话默认是CPU。
参数9决定使用哪个GPU,在多GP

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
# -*- coding: utf-8 -*- import numpy as np import librosa import random def extract_power(y, sr, size=3): """ extract log mel spectrogram feature :param y: the input signal (audio time series) :param sr: sample rate of 'y' :param size: the length (seconds) of random crop from original audio, default as 3 seconds :return: log-mel spectrogram feature """ # normalization y = y.astype(np.float32) normalization_factor = 1 / np.max(np.abs(y)) y = y * normalization_factor # random crop start = random.randint(0, len(y) - size * sr) y = y[start: start + size * sr] # extract log mel spectrogram ##### powerspec = np.abs(librosa.stft(y,n_fft=128, hop_length=1024)) ** 2 #logmelspec = librosa.power_to_db(melspectrogram) return powerspec def extract_logmel(y, sr, size=3): """ extract log mel spectrogram feature :param y: the input signal (audio time series) :param sr: sample rate of 'y' :param size: the length (seconds) of random crop from original audio, default as 3 seconds :return: log-mel spectrogram feature """ # normalization y = y.astype(np.float32) normalization_factor = 1 / np.max(np.abs(y)) y = y * normalization_factor # random crop start = random.randint(0, len(y) - size * sr) y = y[start: start + size * sr] # extract log mel spectrogram ##### melspectrogram = librosa.feature.melspectrogram(y=y, sr=sr, n_fft=2048, hop_length=1024, n_mels=90) logmelspec = librosa.power_to_db(melspectrogram) return logmelspec def extract_mfcc(y, sr, size=3): """ extract MFCC feature :param y: np.ndarray [shape=(n,)], real-valued the input signal (audio time series) :param sr: sample rate of 'y' :param size: the length (seconds) of random crop from original audio, default as 3 seconds :return: MFCC feature """ # normalization y = y.astype(np.float32) normalization_factor = 1 / np.max(np.abs(y))
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值