NvidiaDeepgrowSegTool2D.cpp 15.7 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 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 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
/*
 * Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *  * Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *  * Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *  * Neither the name of NVIDIA CORPORATION nor the names of its
 *    contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <NvidiaDeepgrowSegTool2D.h>

#include <usGetModuleContext.h>
#include <usModuleResource.h>

#include <mitkIOUtil.h>
#include <mitkImageCast.h>
#include <mitkLabelSetImage.h>
#include <mitkLevelWindowPreset.h>
#include <mitkPointSetShapeProperty.h>
#include <mitkToolManager.h>
#include <mitkImageAccessByItk.h>
#include <mitkProgressBar.h>

#include <itkExtractImageFilter.h>
#include <itkImageFileReader.h>
#include <itkImageFileWriter.h>
#include <itkIntensityWindowingImageFilter.h>
#include <itkPasteImageFilter.h>
#include <itkAddImageFilter.h>

#include <nvidia/aiaa/client.h>
#include <nvidia/aiaa/utils.h>
#include <chrono>

MITK_TOOL_MACRO(MITKNVIDIAAIAAMODULE_EXPORT, NvidiaDeepgrowSegTool2D, "NVIDIA Deepgrow Tool");

NvidiaDeepgrowSegTool2D::NvidiaDeepgrowSegTool2D() {
  m_PointSetNode = mitk::DataNode::New();
  m_PointSetNode->GetPropertyList()->SetProperty("name", mitk::StringProperty::New("Deepgrow_Foreground_Points"));
  m_PointSetNode->GetPropertyList()->SetProperty("helper object", mitk::BoolProperty::New(true));

  m_PointSet = mitk::PointSet::New();
  m_PointSetNode->SetData(m_PointSet);
}

NvidiaDeepgrowSegTool2D::~NvidiaDeepgrowSegTool2D() {
}

us::ModuleResource NvidiaDeepgrowSegTool2D::GetIconResource() const {
  auto moduleContext = us::GetModuleContext();
  auto module = moduleContext->GetModule();
  auto resource = module->GetResource("nvidia_logo.png");
  return resource;
}

bool NvidiaDeepgrowSegTool2D::CanHandle(const mitk::BaseData * referenceData, const mitk::BaseData * workingData) const {
  if (!Superclass::CanHandle(referenceData, workingData))
    return false;

  if (referenceData == nullptr)
    return false;

  const auto* image = dynamic_cast<const mitk::Image*>(referenceData);
  if (image == nullptr)
    return false;

  if (image->GetDimension() != 3)
    return false;

  return true;
}

const char* NvidiaDeepgrowSegTool2D::GetName() const {
  return "Deepgrow";
}

const char** NvidiaDeepgrowSegTool2D::GetXPM() const {
  return nullptr;
}

void NvidiaDeepgrowSegTool2D::Activated() {
  Superclass::Activated();

  if (!GetDataStorage()->Exists(m_PointSetNode))
    GetDataStorage()->Add(m_PointSetNode, GetWorkingData());

  m_pointInteractor = mitk::PointSetDataInteractor::New();
  m_pointInteractor->LoadStateMachine("PointSet.xml");
  m_pointInteractor->SetEventConfig("PointSetConfig.xml");
  m_pointInteractor->SetDataNode(m_PointSetNode);
}

void NvidiaDeepgrowSegTool2D::Deactivated() {
  m_PointSet->Clear();
  GetDataStorage()->Remove(m_PointSetNode);

  Superclass::Deactivated();
}

mitk::DataNode* NvidiaDeepgrowSegTool2D::GetReferenceData() {
  return this->m_ToolManager->GetReferenceData(0);
}

mitk::DataStorage* NvidiaDeepgrowSegTool2D::GetDataStorage() {
  return this->m_ToolManager->GetDataStorage();
}

mitk::DataNode* NvidiaDeepgrowSegTool2D::GetWorkingData() {
  return this->m_ToolManager->GetWorkingData(0);
}

mitk::DataNode::Pointer NvidiaDeepgrowSegTool2D::GetPointSetNode() {
  return m_PointSetNode;
}

void NvidiaDeepgrowSegTool2D::SetServerURI(const std::string &serverURI, const int serverTimeout) {
  m_AIAAServerUri = serverURI;
  m_AIAAServerTimeout = serverTimeout;
  m_AIAAModelList = nvidia::aiaa::ModelList();

  try {
    nvidia::aiaa::Client client(serverURI, serverTimeout);
    m_AIAAModelList = client.models("", nvidia::aiaa::Model::deepgrow);
  } catch (nvidia::aiaa::exception &e) {
    std::string msg = "nvidia.aiaa.error." + std::to_string(e.id) + "\ndescription: " + e.name();
    Tool::GeneralMessage("Failed to communicate with Nvidia AIAA Server\nFix server URI in Nvidia AIAA preferences (Ctrl+P)\n\n" + msg);
  }
}

void NvidiaDeepgrowSegTool2D::GetModelInfo(std::map<std::string, std::string> &deepgrow) {
  for (size_t i = 0; i < m_AIAAModelList.size(); i++) {
    auto &model = m_AIAAModelList.models[i];
    std::string tooltip = model.description + "<br/>";

    if (model.type == nvidia::aiaa::Model::deepgrow) {
      deepgrow[model.name] = tooltip;
    }
  }

  MITK_INFO("nvidia") << "Total Deepgrow Models: " << deepgrow.size();
}

void NvidiaDeepgrowSegTool2D::ClearPoints() {
  m_foregroundPoints = nvidia::aiaa::PointSet();
  m_backgroundPoints = nvidia::aiaa::PointSet();

  m_PointSet->Clear();
  mitk::RenderingManager::GetInstance()->RequestUpdateAll();
}

void NvidiaDeepgrowSegTool2D::PointAdded(mitk::DataNode::Pointer imageNode, bool background) {
  mitk::DataNode *node = GetPointSetNode();
  if (node == nullptr) {
    return;
  }

  mitk::PointSet::Pointer pointSet = dynamic_cast<mitk::PointSet*>(node->GetData());
  if (pointSet.IsNull()) {
    mitk::Tool::ErrorMessage("PointSetNode does not contain a pointset");
    return;
  }

  nvidia::aiaa::PointSet finalPointSet;
  mitk::Image *image = dynamic_cast<mitk::Image*>(imageNode->GetData());
  auto points = pointSet->GetPointSet()->GetPoints();

  nvidia::aiaa::Point point;
  if (points->Size()) {
    auto pt = (--points->End())->Value();
    MITK_DEBUG("nvidia") << "(Deepgrow) Added Point: " << pt;

    if (!image->GetGeometry()->IsInside(pt)) {
      MITK_INFO("nvidia") << "(Deepgrow) Point Not in Geometry (IGNORED)";
      return;
    }

    itk::Index<3> index;
    image->GetGeometry(0)->WorldToIndex(pt, index);

    point.push_back(index[0]);
    point.push_back(index[1]);
    point.push_back(index[2]);

    MITK_DEBUG("nvidia") << "(Deepgrow) " << pt << " => [" << index[0] << "," << index[1] << "," << index[2] << "]";
  }

  if (background) {
    m_backgroundPoints.push_back(point);
    MITK_INFO("nvidia") << "(Deepgrow) All Background Points: " << finalPointSet.toJson();
  } else {
    m_foregroundPoints.push_back(point);
    MITK_INFO("nvidia") << "(Deepgrow) All Foreground Points: " << finalPointSet.toJson();
  }
}

void NvidiaDeepgrowSegTool2D::RunDeepGrow(const std::string &model, mitk::DataNode::Pointer imageNode) {
  m_AIAACurrentModelName = model;
  if (m_AIAACurrentModelName.empty()) {
    Tool::GeneralMessage("Deepgrow Model is not selected\n\n");
    return;
  }

  mitk::Image *image = dynamic_cast<mitk::Image*>(imageNode->GetData());
  std::string imageId = image->GetUID();
  MITK_INFO("nvidia") << "(Deepgrow) Image ID: " << imageId;

  AccessByItk_1(image, ItkImageProcessRunDeepgrow, imageId);
}

int NvidiaDeepgrowSegTool2D::GetCurrentSlice(const nvidia::aiaa::PointSet &points) {
  if (points.empty()) {
    return -1;
  }
  return points.points[points.points.size() - 1][2];
}

nvidia::aiaa::PointSet NvidiaDeepgrowSegTool2D::GetPointsForCurrentSlice(const nvidia::aiaa::PointSet &points, int sliceIndex) {
  if (points.empty() || sliceIndex < 0) {
    return points;
  }

  nvidia::aiaa::PointSet filtered;
  for (auto point : points.points) {
    if (point[2] == sliceIndex) {
      filtered.push_back(point);
    }
  }
  return filtered;
}

template<typename TPixel, unsigned int VImageDimension>
void NvidiaDeepgrowSegTool2D::ItkImageProcessRunDeepgrow(itk::Image<TPixel, VImageDimension> *itkImage, std::string imageId) {
  MITK_INFO("nvidia") << "(Deepgrow) ++++++++ Nvidia Deepgrow begins";

  nvidia::aiaa::Client client(m_AIAAServerUri, m_AIAAServerTimeout);
  std::string tmpInputFileName = nvidia::aiaa::Utils::tempfilename() + ".nii.gz";
  std::string tmpResultFileName = nvidia::aiaa::Utils::tempfilename() + ".nii.gz";

  int totalSteps = 3;
  int currentSteps = 0;
  mitk::ProgressBar::GetInstance()->AddStepsToDo(totalSteps);

  try {
    std::string aiaaSessionId;
    auto it = m_AIAASessions.find(imageId);
    if (it != m_AIAASessions.end()) {
      aiaaSessionId = it->second;
      MITK_INFO("nvidia") << "(Deepgrow) AIAA Session already exists: " << aiaaSessionId;
    }

    if (aiaaSessionId.empty()) {
      MITK_INFO("nvidia") << "(Deepgrow) Trying to add current image into AIAA session";

      typedef itk::Image<TPixel, VImageDimension> ImageType;
      auto writer = itk::ImageFileWriter<ImageType>::New();
      writer->SetInput(itkImage);
      writer->SetFileName(tmpInputFileName);
      writer->Update();

      aiaaSessionId = client.createSession(tmpInputFileName);
      m_AIAASessions[imageId] = aiaaSessionId;
    }
    MITK_INFO("nvidia") << "(Deepgrow) AIAA session ID: " << aiaaSessionId;
    currentSteps++;
    mitk::ProgressBar::GetInstance()->Progress(1);

    int sliceIndex = GetCurrentSlice(m_foregroundPoints);
    if (sliceIndex < 0) {
      sliceIndex = GetCurrentSlice(m_backgroundPoints);
    }
    MITK_INFO("nvidia") << "(Deepgrow) current slice: " << sliceIndex;

    auto foreground = GetPointsForCurrentSlice(m_foregroundPoints, sliceIndex);
    auto background = GetPointsForCurrentSlice(m_backgroundPoints, sliceIndex);
    MITK_INFO("nvidia") << "(Deepgrow) Foreground Points for current slice: " << foreground.toJson();
    MITK_INFO("nvidia") << "(Deepgrow) Background Points for current slice: " << background.toJson();

    nvidia::aiaa::Model model = client.model(m_AIAACurrentModelName);
    client.deepgrow(model, foreground, background, tmpInputFileName, tmpResultFileName, aiaaSessionId);
    currentSteps++;
    mitk::ProgressBar::GetInstance()->Progress(1);

    DisplayResult<TPixel, VImageDimension>(tmpResultFileName, sliceIndex);
    currentSteps++;
    mitk::ProgressBar::GetInstance()->Progress(1);
  } catch (nvidia::aiaa::exception &e) {
    if (e.id == nvidia::aiaa::exception::AIAA_SESSION_TIMEOUT) {
      m_AIAASessions.erase(imageId);
    }
    std::string msg = "nvidia.aiaa.error." + std::to_string(e.id) + "\ndescription: " + e.name();
    Tool::GeneralMessage("Failed to execute 'deepgrow' on Nvidia AIAA Server (Retry Again)\n\n" + msg);
  }

  std::remove(tmpInputFileName.c_str());
  std::remove(tmpResultFileName.c_str());

  mitk::ProgressBar::GetInstance()->Progress(totalSteps - currentSteps);
  MITK_INFO("nvidia") << "(Deepgrow) ++++++++ Nvidia Deepgrow ends";
}

template<typename TPixel, unsigned int VImageDimension>
void NvidiaDeepgrowSegTool2D::DisplayResult(const std::string &tmpResultFileName, int sliceIndex) {
  using LabelImageType = itk::Image<mitk::Label::PixelType, VImageDimension> ;
  using ImageReaderType = itk::ImageFileReader<LabelImageType> ;

  auto labelSetImage = dynamic_cast<mitk::LabelSetImage*>(m_ToolManager->GetWorkingData(0)->GetData());
  auto labelSetActive = labelSetImage->GetActiveLabelSet();
  auto labelActive = labelSetImage->GetActiveLabel(labelSetImage->GetActiveLayer());

  std::string labelName = labelActive->GetName();
  mitk::Color labelColor = labelActive->GetColor();

  MITK_INFO("nvidia") << "(display) labelSetActive: " << labelSetActive;
  MITK_INFO("nvidia") << "(display) labelActive: " << labelActive;
  MITK_INFO("nvidia") << "(display) labelName: " << labelName;
  MITK_INFO("nvidia") << "(display) labelColor: " << labelColor;

  std::string labelNameDeepgrow = labelName + "Deepgrow";

  mitk::DataNode::Pointer newNode = mitk::DataNode::New();
  newNode->SetProperty("binary", mitk::BoolProperty::New(true));
  newNode->SetProperty("name", mitk::StringProperty::New(labelNameDeepgrow));
  newNode->SetProperty("color", mitk::ColorProperty::New(labelColor));
  newNode->SetProperty("opacity", mitk::FloatProperty::New(0));
  newNode->SetProperty("helper object", mitk::BoolProperty::New(true));

  // new image mask
  auto reader = ImageReaderType::New();
  reader->SetFileName(tmpResultFileName);
  reader->Update();
  auto itkResultImage = reader->GetOutput();

  // Record the just-created layer ID
  unsigned int layerTotal = labelSetImage->GetNumberOfLayers();
  unsigned int layerToReplace = labelSetImage->GetActiveLayer();
  MITK_INFO("nvidia") << "Total Layers: " << layerTotal << "; Layer To Replace: " << layerToReplace;

  mitk::Image::Pointer resultImage;
  auto tempLayerImage = labelSetImage->GetLayerImage(layerToReplace);
  if (tempLayerImage) {
    typename LabelImageType::Pointer itkExistingImage;
    mitk::CastToItkImage(tempLayerImage, itkExistingImage);

    MITK_INFO("nvidia") << "++++ Result Image: " << itkResultImage->GetLargestPossibleRegion();
    MITK_INFO("nvidia") << "++++ Existing Image: " << itkExistingImage->GetLargestPossibleRegion();

    auto inputRegion = itkResultImage->GetBufferedRegion();
    auto inputSize = inputRegion.GetSize();
    inputSize[2] = 1;  // we extract along z direction

    auto inputIndex = inputRegion.GetIndex();
    inputIndex[2] = sliceIndex;

    typename LabelImageType::RegionType desiredRegion;
    desiredRegion.SetSize(inputSize);
    desiredRegion.SetIndex(inputIndex);

    itk::ImageRegionIterator<LabelImageType> imageIterator(itkExistingImage, desiredRegion);
    while (!imageIterator.IsAtEnd()) {
      imageIterator.Set(0);
      ++imageIterator;
    }
    MITK_INFO("nvidia") << "Current Slice Reset: " << sliceIndex;


    using AddImageFilterType = itk::AddImageFilter<LabelImageType, LabelImageType>;
    auto addFilter = AddImageFilterType::New();

    addFilter->SetInput1(itkResultImage);
    addFilter->SetInput2(itkExistingImage);
    addFilter->Update();


    auto mergedImage = addFilter->GetOutput();
    mitk::CastToMitkImage(mergedImage, resultImage);
    MITK_INFO("nvidia") << "++++ Merged Result Image: " << mergedImage->GetLargestPossibleRegion();
  } else {
    mitk::CastToMitkImage(itkResultImage, resultImage);
  }

  newNode->SetData(resultImage);

  // delete the old image, if there was one:
  mitk::DataNode::Pointer binaryNode = m_ToolManager->GetDataStorage()->GetNamedNode(labelNameDeepgrow);
  m_ToolManager->GetDataStorage()->Remove(binaryNode);

  m_ToolManager->GetDataStorage()->Add(newNode, m_ToolManager->GetWorkingData(0));
  mitk::RenderingManager::GetInstance()->RequestUpdateAll();


  // labelSetImage can only push a new layer at the end, so need to reload whole image for change
  for (unsigned int layerID = 0; layerID < layerTotal; layerID++) {
    auto tempLayerImage = labelSetImage->GetLayerImage(0);
    auto updateLabelSet = labelSetImage->GetLabelSet(0);

    if (layerID != layerToReplace) {
      labelSetImage->AddLayer(tempLayerImage);
    } else {
      labelSetImage->AddLayer(resultImage);
    }

    // Copy LabelSet to Newly added Layer
    labelSetImage->AddLabelSetToLayer(labelSetImage->GetActiveLayer(), updateLabelSet);

    // Now remove the first layer (which also removes its LabelSet)
    labelSetImage->SetActiveLayer(0);
    labelSetImage->RemoveLayer();
  }

  // Set active layer back to the active layer before updating
  labelSetImage->SetActiveLayer(layerToReplace);
  mitk::RenderingManager::GetInstance()->RequestUpdateAll();
}