celeba_op_test.cc 5.5 KB
Newer Older
Z
zhunaipan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/**
 * Copyright 2020 Huawei Technologies Co., Ltd
 *
 * 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 <fstream>
#include <iostream>
#include <memory>
#include <string>

#include "common/common.h"
L
liubuyu 已提交
22 23 24 25 26
#include "minddata/dataset/core/client.h"
#include "minddata/dataset/core/global_context.h"
#include "minddata/dataset/engine/datasetops/source/celeba_op.h"
#include "minddata/dataset/engine/datasetops/source/sampler/subset_random_sampler.h"
#include "minddata/dataset/util/status.h"
Z
zhunaipan 已提交
27 28 29 30 31 32 33 34 35 36 37 38 39 40
#include "gtest/gtest.h"
#include "utils/log_adapter.h"
#include "securec.h"

using namespace mindspore::dataset;
using mindspore::MsLogLevel::ERROR;
using mindspore::ExceptionType::NoExceptionType;
using mindspore::LogStream;

std::shared_ptr<RepeatOp> Repeat(int repeat_cnt);

std::shared_ptr<ExecutionTree> Build(std::vector<std::shared_ptr<DatasetOp>> ops);

std::shared_ptr<CelebAOp> Celeba(int32_t num_workers, int32_t rows_per_buffer, int32_t queue_size,
J
Jamie Nisbet 已提交
41 42
                                 const std::string &dir, std::shared_ptr<Sampler> sampler = nullptr,
                                 bool decode = false, const std::string &dataset_type="all") {
Z
zhunaipan 已提交
43 44
  std::shared_ptr<CelebAOp> so;
  CelebAOp::Builder builder;
Z
Zirui Wu 已提交
45 46 47 48 49 50 51
  Status rc = builder.SetNumWorkers(num_workers)
                .SetCelebADir(dir)
                .SetRowsPerBuffer(rows_per_buffer)
                .SetOpConnectorSize(queue_size)
                .SetSampler(std::move(sampler))
                .SetDecode(decode)
                .SetUsage(dataset_type).Build(&so);
Z
zhunaipan 已提交
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
  return so;
}

class MindDataTestCelebaDataset : public UT::DatasetOpTesting {
protected:
};

TEST_F(MindDataTestCelebaDataset, TestSequentialCeleba) {
  std::string dir = datasets_root_path_ + "/testCelebAData/";
  uint32_t expect_labels[2][40] = {{0,1,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1,1,0,1,0,0,1,0,0,1,0,0,0,1,1,0,1,0,1,0,0,1},
                                   {0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,1,0,1,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1}};
  uint32_t count = 0;
  auto tree = Build({Celeba(16, 2, 32, dir)});
  tree->Prepare();
  Status rc = tree->Launch();
  if (rc.IsError()) {
    MS_LOG(ERROR) << "Return code error detected during tree launch: " << rc.ToString() << ".";
    EXPECT_TRUE(false);
  } else {
    DatasetIterator di(tree);
    TensorMap tersor_map;
    di.GetNextAsMap(&tersor_map);
    EXPECT_TRUE(rc.IsOk());
    while (tersor_map.size() != 0) {
      uint32_t label;
      for (int index = 0; index < 40; index++) {
        tersor_map["attr"]->GetItemAt<uint32_t>(&label, {index});
        EXPECT_TRUE(expect_labels[count][index] == label);
      }
      count++;
      di.GetNextAsMap(&tersor_map);
    }
    EXPECT_TRUE(count == 2);
  }
}

TEST_F(MindDataTestCelebaDataset, TestCelebaRepeat) {
  std::string dir = datasets_root_path_ + "/testCelebAData/";
  uint32_t expect_labels[4][40] = {{0,1,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1,1,0,1,0,0,1,0,0,1,0,0,0,1,1,0,1,0,1,0,0,1},
                                   {0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,1,0,1,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1},
                                   {0,1,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1,1,0,1,0,0,1,0,0,1,0,0,0,1,1,0,1,0,1,0,0,1},
                                   {0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,1,0,1,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1}};
  uint32_t count = 0;
  auto tree = Build({Celeba(16, 2, 32, dir), Repeat(2)});
  tree->Prepare();
  Status rc = tree->Launch();
  if (rc.IsError()) {
    MS_LOG(ERROR) << "Return code error detected during tree launch: " << rc.ToString() << ".";
    EXPECT_TRUE(false);
  } else {
    DatasetIterator di(tree);
    TensorMap tersor_map;
    di.GetNextAsMap(&tersor_map);
    EXPECT_TRUE(rc.IsOk());
    while (tersor_map.size() != 0) {
      uint32_t label;
      for (int index = 0; index < 40; index++) {
        tersor_map["attr"]->GetItemAt<uint32_t>(&label, {index});
        EXPECT_TRUE(expect_labels[count][index] == label);
      }
      count++;
      di.GetNextAsMap(&tersor_map);
    }
    EXPECT_TRUE(count == 4);
  }
}

TEST_F(MindDataTestCelebaDataset, TestSubsetRandomSamplerCeleba) {
  std::vector<int64_t> indices({1});
J
Jamie Nisbet 已提交
121 122
  int64_t num_samples = 0;
  std::shared_ptr<Sampler> sampler = std::make_shared<SubsetRandomSampler>(num_samples, indices);
Z
zhunaipan 已提交
123 124 125
  uint32_t expect_labels[1][40] = {{0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,1,0,1,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1}};
  std::string dir = datasets_root_path_ + "/testCelebAData/";
  uint32_t count = 0;
J
Jamie Nisbet 已提交
126
  auto tree = Build({Celeba(16, 2, 32, dir, std::move(sampler))});
Z
zhunaipan 已提交
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
  tree->Prepare();
  Status rc = tree->Launch();
  if (rc.IsError()) {
    MS_LOG(ERROR) << "Return code error detected during tree launch: " << rc.ToString() << ".";
    EXPECT_TRUE(false);
  } else {
    DatasetIterator di(tree);
    TensorMap tersor_map;
    di.GetNextAsMap(&tersor_map);
    EXPECT_TRUE(rc.IsOk());
    while (tersor_map.size() != 0) {
      uint32_t label;
      for (int index = 0; index < 40; index++) {
        tersor_map["attr"]->GetItemAt<uint32_t>(&label, {index});
        EXPECT_TRUE(expect_labels[count][index] == label);
      }
      count++;
      di.GetNextAsMap(&tersor_map);
    }
    EXPECT_TRUE(count == 1);
  }
}