Skip to content

Commit

Permalink
Flip operator (NVIDIA#130)
Browse files Browse the repository at this point in the history
* Add Flip operator

Signed-off-by: Serge Panev <spanev@nvidia.com>

* Enable support arguments for Flip operator

Signed-off-by: Serge Panev <spanev@nvidia.com>

* Add flip operator unit test

Signed-off-by: Janusz Lisiecki <jlisiecki@nvidia.com>

* Extend Augumentation gallery example by flip operation

Signed-off-by: Janusz Lisiecki <jlisiecki@nvidia.com>
Signed-off-by: Andrei <andreii@nvidia.com>
  • Loading branch information
JanuszL authored and drivanov committed Sep 4, 2018
1 parent 018c590 commit 03e8a39
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 7 deletions.
6 changes: 6 additions & 0 deletions dali/pipeline/operators/displacement/displacement_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,10 @@ TYPED_TEST(DisplacementTest, Rotate) {
this->RunTest({"Rotate", "angle", "10", 0.001});
}

TYPED_TEST(DisplacementTest, Flip) {
const OpArg params[] = {{"horizontal", "True", t_boolParam},
{"vertical", "True", t_boolParam}};
this->RunTest("Flip", params, 2);
}

} // namespace dali
35 changes: 35 additions & 0 deletions dali/pipeline/operators/displacement/flip.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright (c) 2017-2018, NVIDIA CORPORATION. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.


#include "dali/pipeline/operators/displacement/flip.h"
#include "dali/pipeline/operators/displacement/displacement_filter_impl_cpu.h"

namespace dali {

DALI_REGISTER_OPERATOR(Flip, Flip<CPUBackend>, CPU);

DALI_SCHEMA(Flip)
.DocStr("Flip the image on the horizontal and/or vertical axes.")
.NumInput(1)
.NumOutput(1)
.AllowMultipleInputSets()
.AddOptionalArg("horizontal",
R"code(Perform a horizontal flip. Default value is True.)code", true, true)
.AddOptionalArg("vertical",
R"code(Perform a vertical flip. Default value is False.)code", false, true)
.AddParent("DisplacementFilter");

} // namespace dali

24 changes: 24 additions & 0 deletions dali/pipeline/operators/displacement/flip.cu
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright (c) 2017-2018, NVIDIA CORPORATION. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.


#include "dali/pipeline/operators/displacement/flip.h"
#include "dali/pipeline/operators/displacement/displacement_filter_impl_gpu.cuh"

namespace dali {

DALI_REGISTER_OPERATOR(Flip, Flip<GPUBackend>, GPU);

} // namespace dali

55 changes: 55 additions & 0 deletions dali/pipeline/operators/displacement/flip.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright (c) 2017-2018, NVIDIA CORPORATION. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.


#ifndef DALI_PIPELINE_OPERATORS_DISPLACEMENT_FLIP_H_
#define DALI_PIPELINE_OPERATORS_DISPLACEMENT_FLIP_H_

#include <vector>
#include <cmath>
#include "dali/pipeline/operators/operator.h"
#include "dali/pipeline/operators/displacement/warpaffine.h"

namespace dali {

class FlipAugment : public WarpAffineAugment {
public:
explicit FlipAugment(const OpSpec& spec) {
use_image_center = true;
}

void Prepare(Param* p, const OpSpec& spec, ArgumentWorkspace *ws, int index) {
float horizontal = (spec.GetArgument<bool>("horizontal", ws, index)) ? -1.0 : 1.0;
float vertical = (spec.GetArgument<bool>("vertical", ws, index)) ? -1.0 : 1.0;
p->matrix[0] = 1.0 * horizontal;
p->matrix[1] = 0.0;
p->matrix[2] = 0.0;
p->matrix[3] = 0.0;
p->matrix[4] = 1.0 * vertical;
p->matrix[5] = 0.0;
}
};

template <typename Backend>
class Flip : public DisplacementFilter<Backend, FlipAugment> {
public:
inline explicit Flip(const OpSpec &spec)
: DisplacementFilter<Backend, FlipAugment>(spec) {}

virtual ~Flip() = default;
};

} // namespace dali

#endif // DALI_PIPELINE_OPERATORS_DISPLACEMENT_FLIP_H_
4 changes: 2 additions & 2 deletions dali/test/dali_test_matching.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ class GenericMatchingTest : public DALISingleOpTest<ImgType> {
OpArg arg = {paramOp.paramName, paramOp.paramVal, t_floatParam};
vector<OpArg> args;
args.push_back(arg);
opDescr aaa(paramOp.opName, paramOp.epsVal, &args);
RunTest(aaa);
opDescr finalDesc(paramOp.opName, paramOp.epsVal, &args);
RunTest(finalDesc);
}

void RunTest(const char *opName, const OpArg params[] = nullptr,
Expand Down
10 changes: 9 additions & 1 deletion dali/test/dali_test_single_op.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ typedef enum {
t_intParam,
t_floatParam,
t_stringParam,
t_floatVector
t_floatVector,
t_boolParam
} t_paramType;

typedef struct {
Expand Down Expand Up @@ -355,6 +356,13 @@ class DALISingleOpTest : public DALITest {

delete [] pTmp;
spec->AddArg(name, vect);
break;
}
case t_boolParam: {
bool b;
std::istringstream(val) >> std::nouppercase >> std::boolalpha >> b;
spec->AddArg(name, b);
break;
}
}
}
Expand Down
10 changes: 6 additions & 4 deletions docs/examples/augmentation_gallery.ipynb

Large diffs are not rendered by default.

0 comments on commit 03e8a39

Please sign in to comment.