visualizermodel.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * MIT License
  3. *
  4. * Copyright (c) 2019 Alexey Edelev <semlanik@gmail.com>
  5. *
  6. * This file is part of NeuralNetwork project https://git.semlanik.org/semlanik/NeuralNetwork
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy of this
  9. * software and associated documentation files (the "Software"), to deal in the Software
  10. * without restriction, including without limitation the rights to use, copy, modify,
  11. * merge, publish, distribute, sublicense, and/or sell copies of the Software, and
  12. * to permit persons to whom the Software is furnished to do so, subject to the following
  13. * conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in all copies
  16. * or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  19. * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  20. * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
  21. * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  22. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  23. * DEALINGS IN THE SOFTWARE.
  24. */
  25. #include "visualizermodel.h"
  26. #include <QDebug>
  27. #include <qgrpcasyncreply.h>
  28. #include <QQmlEngine>
  29. #include "remotecontrol.qpb.h"
  30. #include "visualization.qpb.h"
  31. using namespace remotecontrol;
  32. using namespace QtProtobuf;
  33. VisualizerModel::VisualizerModel(std::shared_ptr<RemoteControlClient> &client,
  34. std::shared_ptr<visualization::VisualizationClient> &visualizationClient,
  35. QObject *parent) : QObject(parent)
  36. , m_client(client)
  37. , m_visualizationClient(visualizationClient)
  38. , m_networkState(new NetworkState{NetworkState::None})
  39. {
  40. m_client->getConfiguration({}, this, [this](QGrpcAsyncReply *reply) {
  41. qDeleteAll(m_layers);
  42. m_networkConfig = reply->read<Configuration>();
  43. for(int i = 0; i < m_networkConfig.sizes().size(); i++) {
  44. m_layers.append(new NetworkLayerState);
  45. auto layerState = m_layers.last();
  46. auto currenSize = m_networkConfig.sizes()[i];
  47. auto &activations = layerState->m_activations;
  48. auto &weights = layerState->m_weights;
  49. activations.setDimentions(currenSize, 1);
  50. QList<ValueIndicator*> data;
  51. for (int k = 0; k < currenSize; k++) {
  52. data.append(new ValueIndicator(&activations));
  53. }
  54. activations.setData(data);
  55. if (i != 0) {
  56. auto previousSize = m_networkConfig.sizes()[i - 1];
  57. int tolalItems = currenSize * previousSize;
  58. weights.setDimentions(currenSize,previousSize);
  59. data.clear();
  60. for (int k = 0; k < tolalItems; k++) {
  61. data.append(new ValueIndicator(&weights));
  62. }
  63. weights.setData(data);
  64. }
  65. }
  66. sizesChanged();
  67. });
  68. auto subscription = client->subscribeActivationsUpdates({});
  69. QObject::connect(subscription, &QtProtobuf::QGrpcSubscription::updated, [this, subscription]() {
  70. if (m_layers.isEmpty()) {
  71. return;
  72. }
  73. auto activations = subscription->read<remotecontrol::LayerMatrix>();
  74. auto layer = m_layers[activations.layer()];
  75. layer->m_activations.updateValues(Dense(activations.matrix().matrix()));
  76. layer->m_actiovationTrigger.updateLayer();
  77. });
  78. subscription = client->subscribeWeightsUpdates({});
  79. QObject::connect(subscription, &QtProtobuf::QGrpcSubscription::updated, [this, subscription]() {
  80. if (m_layers.isEmpty()) {
  81. return;
  82. }
  83. auto weights = subscription->read<remotecontrol::LayerMatrix>();
  84. auto layer = m_layers[weights.layer()];
  85. layer->m_weights.updateValues(Dense(weights.matrix().matrix()));
  86. layer->m_weightTrigger.updateLayer();
  87. });
  88. subscription = client->subscribeStateUpdates({}, m_networkState);
  89. }
  90. ValueIndicator *VisualizerModel::activation(int layer, int row)
  91. {
  92. ValueIndicator* indicator = m_layers[layer]->m_activations.value<ValueIndicator*>(row, 0);
  93. QQmlEngine::setObjectOwnership(indicator, QQmlEngine::CppOwnership);
  94. return indicator;
  95. }
  96. ValueIndicator *VisualizerModel::weight(int layer, int row, int column)
  97. {
  98. ValueIndicator* indicator = m_layers[layer]->m_weights.value<ValueIndicator*>(row, column);
  99. QQmlEngine::setObjectOwnership(indicator, QQmlEngine::CppOwnership);
  100. return indicator;
  101. }
  102. LayerTrigger *VisualizerModel::activationTrigger(int layer)
  103. {
  104. LayerTrigger* trigger = &m_layers[layer]->m_actiovationTrigger;
  105. QQmlEngine::setObjectOwnership(trigger, QQmlEngine::CppOwnership);
  106. return trigger;
  107. }
  108. LayerTrigger *VisualizerModel::weightTrigger(int layer)
  109. {
  110. LayerTrigger* trigger = &m_layers[layer]->m_weightTrigger;
  111. QQmlEngine::setObjectOwnership(trigger, QQmlEngine::CppOwnership);
  112. return trigger;
  113. }
  114. void VisualizerModel::start()
  115. {
  116. m_visualizationClient->Run({});
  117. }