<commit_before>/******************************************************************************
 * Copyright 2019 The Apollo Authors. 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 "modules/prediction/evaluator/vehicle/junction_map_evaluator.h"

#include <omp.h>
#include <unordered_map>
#include <utility>

#include "cyber/common/file.h"
#include "modules/prediction/common/prediction_gflags.h"
#include "modules/prediction/common/prediction_map.h"
#include "modules/prediction/common/prediction_system_gflags.h"
#include "modules/prediction/common/prediction_util.h"
#include "modules/prediction/common/semantic_map.h"

namespace apollo {
namespace prediction {

JunctionMapEvaluator::JunctionMapEvaluator() : device_(torch::kCPU) {
  LoadModel();
}

void JunctionMapEvaluator::Clear() {}

bool JunctionMapEvaluator::Evaluate(Obstacle* obstacle_ptr) {
  // Sanity checks.
  omp_set_num_threads(1);
  Clear();
  CHECK_NOTNULL(obstacle_ptr);
  int id = obstacle_ptr->id();
  if (!obstacle_ptr->latest_feature().IsInitialized()) {
    AERROR << "Obstacle [" << id << "] has no latest feature.";
    return false;
  }
  Feature* latest_feature_ptr = obstacle_ptr->mutable_latest_feature();
  CHECK_NOTNULL(latest_feature_ptr);

  // Assume obstacle is NOT closed to any junction exit
  if (!latest_feature_ptr->has_junction_feature() ||
      latest_feature_ptr->junction_feature().junction_exit_size() < 2) {
    ADEBUG << "Obstacle [" << id << "] has less than two junction_exits.";
    return false;
  }
  // Extract features of junction_exit_mask
  std::vector<double> feature_values;
  if (!ExtractFeatureValues(obstacle_ptr, &feature_values)) {
    ADEBUG << "Obstacle [" << id << "] failed to extract junction exit mask";
    return false;
  }

  if (!FLAGS_enable_semantic_map) {
    ADEBUG << "Not enable semantic map, exit junction_map_evaluator.";
    return false;
  }
  cv::Mat feature_map;
  if (!SemanticMap::Instance()->GetMapById(id, &feature_map)) {
    return false;
  }

  // Build input features for torch
  std::vector<torch::jit::IValue> torch_inputs;
  // Process the feature_map
  cv::cvtColor(feature_map, feature_map, CV_BGR2RGB);
  cv::Mat img_float;
  feature_map.convertTo(img_float, CV_32F, 1.0 / 255);
  torch::Tensor img_tensor = torch::from_blob(img_float.data, {1, 224, 224, 3});
  img_tensor = img_tensor.permute({0, 3, 1, 2});
  img_tensor[0][0] = img_tensor[0][0].sub(0.485).div(0.229);
  img_tensor[0][1] = img_tensor[0][1].sub(0.456).div(0.224);
  img_tensor[0][2] = img_tensor[0][2].sub(0.406).div(0.225);
  torch_inputs.push_back(std::move(img_tensor));
  // Process junction_exit_mask
  torch::Tensor junction_exit_mask =
      torch::zeros({1, static_cast<int>(feature_values.size())});
  for (size_t i = 0; i < feature_values.size(); ++i) {
    junction_exit_mask[0][i] = static_cast<float>(feature_values[i]);
  }
  torch_inputs.push_back(std::move(junction_exit_mask));
<commit_msg>Prediction: update tuple for torch_inputs<commit_after>/******************************************************************************
 * Copyright 2019 The Apollo Authors. 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 "modules/prediction/evaluator/vehicle/junction_map_evaluator.h"

#include <omp.h>
#include <unordered_map>
#include <utility>

#include "cyber/common/file.h"
#include "modules/prediction/common/prediction_gflags.h"
#include "modules/prediction/common/prediction_map.h"
#include "modules/prediction/common/prediction_system_gflags.h"
#include "modules/prediction/common/prediction_util.h"
#include "modules/prediction/common/semantic_map.h"

namespace apollo {
namespace prediction {

JunctionMapEvaluator::JunctionMapEvaluator() : device_(torch::kCPU) {
  LoadModel();
}

void JunctionMapEvaluator::Clear() {}

bool JunctionMapEvaluator::Evaluate(Obstacle* obstacle_ptr) {
  // Sanity checks.
  omp_set_num_threads(1);
  Clear();
  CHECK_NOTNULL(obstacle_ptr);
  int id = obstacle_ptr->id();
  if (!obstacle_ptr->latest_feature().IsInitialized()) {
    AERROR << "Obstacle [" << id << "] has no latest feature.";
    return false;
  }
  Feature* latest_feature_ptr = obstacle_ptr->mutable_latest_feature();
  CHECK_NOTNULL(latest_feature_ptr);

  // Assume obstacle is NOT closed to any junction exit
  if (!latest_feature_ptr->has_junction_feature() ||
      latest_feature_ptr->junction_feature().junction_exit_size() < 2) {
    ADEBUG << "Obstacle [" << id << "] has less than two junction_exits.";
    return false;
  }
  // Extract features of junction_exit_mask
  std::vector<double> feature_values;
  if (!ExtractFeatureValues(obstacle_ptr, &feature_values)) {
    ADEBUG << "Obstacle [" << id << "] failed to extract junction exit mask";
    return false;
  }

  if (!FLAGS_enable_semantic_map) {
    ADEBUG << "Not enable semantic map, exit junction_map_evaluator.";
    return false;
  }
  cv::Mat feature_map;
  if (!SemanticMap::Instance()->GetMapById(id, &feature_map)) {
    return false;
  }

  // Build input features for torch
  std::vector<torch::jit::IValue> torch_inputs;
  // Process the feature_map
  cv::cvtColor(feature_map, feature_map, CV_BGR2RGB);
  cv::Mat img_float;
  feature_map.convertTo(img_float, CV_32F, 1.0 / 255);
  torch::Tensor img_tensor = torch::from_blob(img_float.data, {1, 224, 224, 3});
  img_tensor = img_tensor.permute({0, 3, 1, 2});
  img_tensor[0][0] = img_tensor[0][0].sub(0.485).div(0.229);
  img_tensor[0][1] = img_tensor[0][1].sub(0.456).div(0.224);
  img_tensor[0][2] = img_tensor[0][2].sub(0.406).div(0.225);
  // Process junction_exit_mask
  torch::Tensor junction_exit_mask =
      torch::zeros({1, static_cast<int>(feature_values.size())});
  for (size_t i = 0; i < feature_values.size(); ++i) {
    junction_exit_mask[0][i] = static_cast<float>(feature_values[i]);
  }
  torch_inputs.push_back(
      torch::jit::Tuple::create({img_tensor, junction_exit_mask}));

{'fraction_non_alphanumeric': 0.17173423423423423, 'fraction_numerical': 0.019425675675675675, 'mean_word_length': 5.183637946040035, 'pattern_counts': {'":': 0, '<': 61, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6078266', 'n_tokens_mistral': 2375, 'n_tokens_neox': 2264, 'n_words': 519}