From patchwork Tue Jun 1 22:04:48 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wenlong Ding X-Patchwork-Id: 28040 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a6b:b214:0:0:0:0:0 with SMTP id b20csp204312iof; Tue, 1 Jun 2021 22:13:35 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy29xEri8pu6ogS0hXKlMXVE0kRe3BdbLpIcnhXwWrwphigqsGCDsG995btJUOpGNaTagDr X-Received: by 2002:a17:906:6c8a:: with SMTP id s10mr32284796ejr.276.1622610814887; Tue, 01 Jun 2021 22:13:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1622610814; cv=none; d=google.com; s=arc-20160816; b=x9b5V3+QS1AAmgg0Yo2R0b0mDNZzey8R6X9SiZAXRf30xsy9U2Mp7lVSuJ7T2UimpT 5M5JSIwMrYb5wjPxJfmZy2Wv5MWkaqIkEkWJbscPRDng8NL7DSFgSy+2xx2hexiwWayt zcvYzE2SrMYBKLFlPQurQxHItr1JDYKqyU9UB+d4DjfI2xGKkwBP0d5E9+m9T7XpblkA nbDb0J5Ni7eCMSuEYWDgL4BiTFVkawQwyEa1YW9cwxZbvaWREwQWC3M6sVJ92Whhz9Dt yf/Ue34SG94ZnZEWvjocUqlFXzdnNQOmPrCTatYKAVM4uuMCTjeTU4VUSgwemxVmGVQH imRQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:cc:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:mime-version:references:in-reply-to :message-id:date:to:from:ironport-sdr:ironport-sdr:delivered-to; bh=bqWoesDsS4A8SbjXJFBhojcQeThRN8eE36MLmTOETVc=; b=ED9vJ216lkJYagRHPSk3TLZ2Gg56d7sxUzXXL9fuP1oLPr6aJdIZ9xavYW95JiZISk nFMER9D6lRvxmHZ2t0Ir/pYMYTM/7ZmXxI8MqKz65dE4ox6cvPjj8nT1hROQbV5VvZSw XWKvLujzvO3jCp5Kvtq+gfSl3IqziQUwsaPRgzkeIZopXKCHvTgJqDcXVz+eUpNP6dt2 NZ286RdTszg1KVikQNAbU0p4a80hYKuAedd13jRAqpuZvj6i2Gvp/JwEXjr8J6bBtl9H qIr5SlVXtSAPWbYIKb8h6sjuImenjU3MHyCxAyqOSSXXZp2Cz0T4sgvilYIuWLPNMxZf U+ag== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id m7si1194774edc.575.2021.06.01.22.13.12; Tue, 01 Jun 2021 22:13:34 -0700 (PDT) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 6FA92689D32; Wed, 2 Jun 2021 08:12:57 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mga07.intel.com (mga07.intel.com [134.134.136.100]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id F0C016899B3 for ; Wed, 2 Jun 2021 08:12:49 +0300 (EEST) IronPort-SDR: wfHAQa7RWooW/ugTk8vXC71yVph5EK/z9hJD4m8iu1BXufO1hg70c3+92Jj2g9MDAF1QJufaFI RDyNy8WwbwLg== X-IronPort-AV: E=McAfee;i="6200,9189,10002"; a="267578063" X-IronPort-AV: E=Sophos;i="5.83,241,1616482800"; d="scan'208";a="267578063" Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Jun 2021 22:12:48 -0700 IronPort-SDR: esYcwaXMDe/ETQf84DJgEyORicIniaZ0tL0huI++8lSLUPcAEE/3ChadI+x5HC4yCHVrjh04sk ngZtyQxL9hDQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.83,241,1616482800"; d="scan'208";a="447257834" Received: from dwl-h370m-d3h.sh.intel.com ([10.239.98.152]) by fmsmga008.fm.intel.com with ESMTP; 01 Jun 2021 22:12:47 -0700 From: Wenlong Ding To: ffmpeg-devel@ffmpeg.org Date: Wed, 2 Jun 2021 06:04:48 +0800 Message-Id: <20210601220448.108767-2-wenlong.ding@intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210601220448.108767-1-wenlong.ding@intel.com> References: <20210601220448.108767-1-wenlong.ding@intel.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 2/2] tests/dnn/dnn-layer-batchnormalization-test: add unit test for BN X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: Wenlong Ding Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: M1uvw73rJMmn Signed-off-by: Wenlong Ding --- tests/dnn/.gitignore | 1 + tests/dnn/Makefile | 1 + tests/dnn/dnn-layer-batchnormalization-test.c | 117 ++++++++++++++++++ tests/fate/dnn.mak | 5 + 4 files changed, 124 insertions(+) create mode 100644 tests/dnn/dnn-layer-batchnormalization-test.c diff --git a/tests/dnn/.gitignore b/tests/dnn/.gitignore index 03b04d6653..f432352ca8 100644 --- a/tests/dnn/.gitignore +++ b/tests/dnn/.gitignore @@ -6,3 +6,4 @@ /dnn-layer-mathunary-test /dnn-layer-avgpool-test /dnn-layer-dense-test +/dnn-layer-batchnormalization-test diff --git a/tests/dnn/Makefile b/tests/dnn/Makefile index ef827520de..058342b677 100644 --- a/tests/dnn/Makefile +++ b/tests/dnn/Makefile @@ -6,6 +6,7 @@ DNNTESTPROGS += dnn-layer-mathbinary DNNTESTPROGS += dnn-layer-maximum DNNTESTPROGS += dnn-layer-mathunary DNNTESTPROGS += dnn-layer-avgpool +DNNTESTPROGS += dnn-layer-batchnormalization DNNTESTOBJS := $(DNNTESTOBJS:%=$(DNNTESTSDIR)%) $(DNNTESTPROGS:%=$(DNNTESTSDIR)/%-test.o) DNNTESTPROGS := $(DNNTESTPROGS:%=$(DNNTESTSDIR)/%-test$(EXESUF)) diff --git a/tests/dnn/dnn-layer-batchnormalization-test.c b/tests/dnn/dnn-layer-batchnormalization-test.c new file mode 100644 index 0000000000..ab6b8faf99 --- /dev/null +++ b/tests/dnn/dnn-layer-batchnormalization-test.c @@ -0,0 +1,117 @@ +/* + * Copyright (c) 2021 + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include +#include +#include +#include "libavfilter/dnn/dnn_backend_native_layer_batchnormalization.h" +#include "libavutil/avassert.h" + +#define EPSON 0.00001 + +static int test(void) +{ + // the input data is generated with below python code. + /* + import numpy as np + + in_data = np.random.rand(1, 5, 4, 3) + + print('input_data:') + print(in_data.shape) + print(list(in_data.flatten())) + */ + DnnLayerBatchnormalizationParams params; + DnnOperand operands[2]; + int32_t input_indexes[1]; + float input_data[1*5*4*3] = { + 0.03592029475149139, 0.8177031019998039, 0.7439828967882774, 0.12351433689912827, 0.10151796406770697, 0.3746884445121409, 0.17609107818009584, + 0.9180125678699762, 0.413786266728411, 0.15388845485290492, 0.6527147025219426, 0.8921708075708997, 0.9315511108099018, 0.6089771636865586, + 0.9096167333314585, 0.35798486932034357, 0.242417367935445, 0.3354539420380003, 0.5885835336919439, 0.2330763092626813, 0.19528398816193004, + 0.3537750057949528, 0.8908506440029637, 0.6745876262153178, 0.7975390896917102, 0.42115552951085633, 0.026252384244236926, 0.5987492499419249, + 0.2969445642379439, 0.6612326239920433, 0.5186612970859783, 0.9154125186721356, 0.11647732132450173, 0.32631441601002287, 0.5222573083721183, + 0.47294093689301375, 0.009744493333811666, 0.034835869186598645, 0.14643674017261454, 0.06114675456320906, 0.07678534832197992, 0.1968478741676435, + 0.3126360178524745, 0.6361975996395828, 0.18012602596211258, 0.8609077898378013, 0.8446431696067955, 0.7497824163136522, 0.8651808467465752, + 0.922506696896085, 0.6057714540829449, 0.6226843061111148, 0.3894522432026163, 0.15585932050469298, 0.5738772930525835, 0.4978427887293949, + 0.6015976578669766, 0.9169836327963268, 0.6957180046975255, 0.49003527036757355 + }; + + float expected_output[1*5*4*3]; + float *output; + int32_t channel = 3; + float mean[3] = {0.1, 0.15, 0.14}; + float variance[3] = {0.12, 0.21, 0.24}; + float variance_eps = 0.0001; + float scale = 1.0; + float offset = 0.1; + + for (int i = 0; i < 1*5*4*3; ++i) { + expected_output[i] = scale * (input_data[i] - mean[i % channel]) / sqrt(variance[i % channel] + variance_eps) + offset; + } + + params.channel = channel; + params.mean = av_malloc_array(params.channel, sizeof(*params.mean)); + if (!params.mean) { + return 1; + } + params.mean[0] = mean[0]; + params.mean[1] = mean[1]; + params.mean[2] = mean[2]; + params.variance = av_malloc_array(params.channel, sizeof(*params.variance)); + if (!params.variance) { + av_freep(¶ms.mean); + return 1; + } + params.variance[0] = variance[0]; + params.variance[1] = variance[1]; + params.variance[2] = variance[2]; + params.scale = scale; + params.offset = offset; + params.variance_eps = variance_eps; + + operands[0].data = input_data; + operands[0].dims[0] = 1; + operands[0].dims[1] = 5; + operands[0].dims[2] = 4; + operands[0].dims[3] = 3; + operands[1].data = NULL; + + input_indexes[0] = 0; + ff_dnn_execute_layer_batchnormalization(operands, input_indexes, 1, ¶ms, NULL); + output = operands[1].data; + for (int i = 0; i < sizeof(expected_output) / sizeof(float); i++) { + if (fabs(output[i] - expected_output[i]) > EPSON) { + printf("at index %d, output: %f, expected_output: %f\n", i, output[i], expected_output[i]); + av_freep(¶ms.mean); + av_freep(¶ms.variance); + av_freep(&output); + return 1; + } + } + av_freep(¶ms.mean); + av_freep(¶ms.variance); + av_freep(&output); + return 0; +} + +int main(int argc, char **argv) +{ + return test(); +} diff --git a/tests/fate/dnn.mak b/tests/fate/dnn.mak index ef07ee45f8..cf741c7dd9 100644 --- a/tests/fate/dnn.mak +++ b/tests/fate/dnn.mak @@ -38,6 +38,11 @@ fate-dnn-layer-avgpool: $(DNNTESTSDIR)/dnn-layer-avgpool-test$(EXESUF) fate-dnn-layer-avgpool: CMD = run $(DNNTESTSDIR)/dnn-layer-avgpool-test$(EXESUF) fate-dnn-layer-avgpool: CMP = null +FATE_DNN += fate-dnn-layer-batchnormalization +fate-dnn-layer-batchnormalization: $(DNNTESTSDIR)/dnn-layer-batchnormalization-test$(EXESUF) +fate-dnn-layer-batchnormalization: CMD = run $(DNNTESTSDIR)/dnn-layer-batchnormalization-test$(EXESUF) +fate-dnn-layer-batchnormalization: CMP = null + FATE-$(CONFIG_DNN) += $(FATE_DNN) fate-dnn: $(FATE_DNN)