Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flip operator #130

Merged
merged 4 commits into from
Aug 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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[] = NULL,
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 @@ -92,7 +92,8 @@ typedef enum {
t_intParam,
t_floatParam,
t_stringParam,
t_floatVector
t_floatVector,
t_boolParam
} t_paramType;

typedef struct {
Expand Down Expand Up @@ -353,6 +354,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.