• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2017 The TensorFlow Authors. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7     http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 
16 #include "tensorflow/cc/ops/array_ops.h"
17 #include "tensorflow/cc/ops/const_op.h"
18 #include "tensorflow/cc/ops/nn_ops.h"
19 #include "tensorflow/cc/ops/sendrecv_ops.h"
20 #include "tensorflow/cc/ops/standard_ops.h"
21 #include "tensorflow/core/framework/tensor_testutil.h"
22 #include "tensorflow/core/lib/core/status_test_util.h"
23 #include "tensorflow/core/platform/test.h"
24 #include "tensorflow/core/platform/test_benchmark.h"
25 #include "tensorflow/core/public/session.h"
26 #include "tensorflow/tools/graph_transforms/transform_utils.h"
27 
28 namespace tensorflow {
29 namespace graph_transforms {
30 
31 // Declare here, so we don't need a public header.
32 Status FlattenAtrousConv(const GraphDef& input_graph_def,
33                          const TransformFuncContext& context,
34                          GraphDef* output_graph_def);
35 
36 class FlattenAtrousConvTest : public ::testing::Test {
37  protected:
38   template <class TConvOp>
TestFlattenAtrousConv()39   void TestFlattenAtrousConv() {
40     auto root = tensorflow::Scope::NewRootScope();
41     using namespace ::tensorflow::ops;  // NOLINT(build/namespaces)
42 
43     Tensor input_data(DT_FLOAT, TensorShape({1, 3, 3, 2}));
44     test::FillValues<float>(
45         &input_data, {.1f, .4f, .2f, .5f, .3f, .6f, -1.0f, -.4f, -.2f, -.5f,
46                       -.3f, -.6f, .1f, .4f, .2f, .5f, .3f, .6f});
47     Output input_op =
48         Const(root.WithOpName("input_op"), Input::Initializer(input_data));
49 
50     Tensor block_shape_data(DT_INT32, TensorShape({2}));
51     test::FillValues<int>(&block_shape_data, {2, 2});
52     Output block_shape_op = Const(root.WithOpName("block_shape_op"),
53                                   Input::Initializer(block_shape_data));
54 
55     Tensor paddings_data(DT_INT32, TensorShape({2, 2}));
56     test::FillValues<int>(&paddings_data, {1, 2, 1, 2});
57     Output paddings_op = Const(root.WithOpName("paddings_op"),
58                                Input::Initializer(paddings_data));
59 
60     Output space_to_batch_op =
61         SpaceToBatchND(root.WithOpName("space_to_batch_op"), input_op,
62                        block_shape_op, paddings_op);
63 
64     Tensor weights_data(DT_FLOAT, TensorShape({2, 2, 2, 1}));
65     test::FillValues<float>(&weights_data,
66                             {.1f, .2f, .3f, .4f, .1f, .2f, .3f, .4f});
67     Output weights_op =
68         Const(root.WithOpName("weights_op"), Input::Initializer(weights_data));
69 
70     Output conv_op = TConvOp(root.WithOpName("conv_op"), space_to_batch_op,
71                              weights_op, {1, 1, 1, 1}, "VALID");
72 
73     Tensor crops_data(DT_INT32, TensorShape({2, 2}));
74     test::FillValues<int>(&crops_data, {0, 1, 0, 1});
75     Output crops_op =
76         Const(root.WithOpName("crops_op"), Input::Initializer(crops_data));
77 
78     Output batch_to_space_op = BatchToSpaceND(
79         root.WithOpName("output"), conv_op, block_shape_op, crops_op);
80 
81     GraphDef original_graph_def;
82     TF_ASSERT_OK(root.ToGraphDef(&original_graph_def));
83 
84     std::unique_ptr<Session> original_session(NewSession(SessionOptions()));
85     TF_ASSERT_OK(original_session->Create(original_graph_def));
86     std::vector<Tensor> original_outputs;
87     TF_ASSERT_OK(original_session->Run({}, {"output"}, {}, &original_outputs));
88 
89     GraphDef modified_graph_def;
90     TF_ASSERT_OK(FlattenAtrousConv(original_graph_def, {{}, {"output"}},
91                                    &modified_graph_def));
92 
93     std::unique_ptr<Session> modified_session(NewSession(SessionOptions()));
94     TF_ASSERT_OK(modified_session->Create(modified_graph_def));
95     std::vector<Tensor> modified_outputs;
96     TF_ASSERT_OK(modified_session->Run({}, {"output"}, {}, &modified_outputs));
97 
98     EXPECT_EQ(3, modified_graph_def.node_size());
99 
100     EXPECT_EQ("input_op", modified_graph_def.node(0).name());
101     EXPECT_EQ("weights_op", modified_graph_def.node(1).name());
102     EXPECT_EQ("output", modified_graph_def.node(2).name());
103 
104     EXPECT_EQ("Const", modified_graph_def.node(0).op());
105     EXPECT_EQ("Const", modified_graph_def.node(1).op());
106     EXPECT_EQ(conv_op.node()->type_string(), modified_graph_def.node(2).op());
107 
108     test::ExpectTensorNear<float>(original_outputs[0], modified_outputs[0],
109                                   1e-6);
110   }
111 };
112 
TEST_F(FlattenAtrousConvTest,TestFlattenAtrousConv2D)113 TEST_F(FlattenAtrousConvTest, TestFlattenAtrousConv2D) {
114   TestFlattenAtrousConv<::tensorflow::ops::Conv2D>();
115 }
TEST_F(FlattenAtrousConvTest,TestFlattenAtrousDepthwiseConv2dNative)116 TEST_F(FlattenAtrousConvTest, TestFlattenAtrousDepthwiseConv2dNative) {
117   TestFlattenAtrousConv<::tensorflow::ops::DepthwiseConv2dNative>();
118 }
119 
120 }  // namespace graph_transforms
121 }  // namespace tensorflow
122