def run_detection(self, spec, chunk_duration, detection_thresh, low_res=True): """audio is the raw samples should be 1D vector sampling_rate should be divided by 10 if the recordings are not time expanded """ # run the cnn - low_res will be faster but less accurate if low_res: prob = self.eval_network(spec) scale_fact = 8.0 else: prob_1 = self.eval_network(spec) prob_2 = self.eval_network(spec[:, 2:]) prob = np.zeros(prob_1.shape[0]*2, dtype=np.float32) prob[0::2] = prob_1 prob[1::2] = prob_2 scale_fact = 4.0 f_size = self.smooth_op_prediction_sigma / scale_fact nms_win = np.round(self.nms_win_size / scale_fact) # smooth the outputs - this might not be necessary prob = gaussian_filter1d(prob, f_size) # perform non maximum suppression call_time, call_prob = nms.nms_1d(prob, nms_win, chunk_duration) # remove detections below threshold if call_prob.shape[0] > 0: inds = (call_prob >= detection_thresh) call_prob = call_prob[inds] call_time = call_time[inds] return call_time, call_prob
def test(self, file_name=None, file_duration=None, audio_samples=None, sampling_rate=None): # compute features and perform classification features = self.create_or_load_features(file_name, audio_samples, sampling_rate) y_prediction = self.test_fn(features)[:, np.newaxis] # smooth the output prediction if self.params.smooth_op_prediction: y_prediction = gaussian_filter1d(y_prediction, self.params.smooth_op_prediction_sigma, axis=0) # perform non max suppression pos, prob = nms.nms_1d(y_prediction[:,0].astype(np.float), self.params.nms_win_size, file_duration) return pos, prob, y_prediction
def test(self, file_name=None, file_duration=None, audio_samples=None, sampling_rate=None): # compute features features = self.create_or_load_features(file_name, audio_samples, sampling_rate) # make prediction y_prediction = self.forest.test(features)[:, 1][:, np.newaxis] # smooth the output if self.params.smooth_op_prediction: y_prediction = gaussian_filter1d( y_prediction, self.params.smooth_op_prediction_sigma, axis=0) pos, prob = nms.nms_1d(y_prediction[:, 0], self.params.nms_win_size, file_duration) return pos, prob, y_prediction
def test_nms(): import matplotlib.pyplot as plt import numpy as np #import pyximport; pyximport.install(reload_support=True) import nms as nms_fast y = np.sin(np.arange(1000) / 100.0 * np.pi) y = y + np.random.random(y.shape) * 0.5 win_size = int(0.1 * y.shape[0] / 2.0) pos, prob = nms_1d(y, win_size, y.shape[0]) pos_f, prob_f = nms_fast.nms_1d(y, win_size, y.shape[0]) print('diff between implementations =', 1 - np.isclose(prob_f, prob).mean()) print('diff between implementations =', 1 - np.isclose(pos_f, pos).mean()) plt.close('all') plt.plot(y) plt.plot((pos).astype('int'), prob, 'ro', ms=10) plt.plot((pos_f).astype('int'), prob, 'bo') # shift so we can see them plt.show()
def test(self, file_name=None, file_duration=None, audio_samples=None, sampling_rate=None): # compute features and perform classification features = self.create_or_load_features(file_name, audio_samples, sampling_rate) # predictions for max classes y_predictions = self.test_fn(features) # trying to get rid of rows with 0 highest call_predictions = y_predictions call_predictions = call_predictions[:,1:] high_preds = np.array([np.max(x) for x in call_predictions])[:, np.newaxis] classes = np.array([np.argmax(x)+1 for x in call_predictions])[:, np.newaxis] # smooth the output prediction if self.params.smooth_op_prediction: high_preds = gaussian_filter1d(high_preds, self.params.smooth_op_prediction_sigma, axis=0) # perform non max suppression pos, prob, classes = nms.nms_1d(high_preds[:,0].astype(np.float), classes,self.params.nms_win_size, file_duration) return pos, prob, high_preds, classes