visualizermodel.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 "dense.h"
  29. using namespace remotecontrol;
  30. using namespace QtProtobuf;
  31. VisualizerModel::VisualizerModel(std::shared_ptr<RemoteControlClient> &client, QObject *parent) : QObject(parent)
  32. , m_client(client)
  33. {
  34. m_client->getConfiguration({}, this, [this](QGrpcAsyncReply *reply) {
  35. qDeleteAll(m_layers);
  36. m_networkConfig = reply->read<Configuration>();
  37. for(int i = 0; i < m_networkConfig.sizes().size(); i++) {
  38. m_layers.append(new NetworkLayerState);
  39. auto layerState = m_layers.last();
  40. auto currenSize = m_networkConfig.sizes()[i];
  41. auto &activations = layerState->m_activations;
  42. auto &weights = layerState->m_weights;
  43. activations.setDimentions(currenSize, 1);
  44. QList<ValueIndicator*> data;
  45. for (int k = 0; k < currenSize; k++) {
  46. data.append(new ValueIndicator(&activations));
  47. }
  48. activations.setData(data);
  49. if (i != 0) {
  50. auto previousSize = m_networkConfig.sizes()[i - 1];
  51. int tolalItems = currenSize * previousSize;
  52. weights.setDimentions(currenSize,previousSize);
  53. data.clear();
  54. for (int k = 0; k < tolalItems; k++) {
  55. data.append(new ValueIndicator(&weights));
  56. }
  57. weights.setData(data);
  58. }
  59. }
  60. sizesChanged();
  61. });
  62. QObject::connect(client.get(), &remotecontrol::RemoteControlClient::ActivationsUpdated, [this](const remotecontrol::LayerMatrix &activations) {
  63. if (m_layers.isEmpty()) {
  64. return;
  65. }
  66. auto layer = m_layers[activations.layer()];
  67. layer->m_activations.updateValues(Dense(activations.matrix().matrix()));
  68. layer->m_actiovationTrigger.updateLayer();
  69. });
  70. QObject::connect(client.get(), &remotecontrol::RemoteControlClient::WeightsUpdated, [this](const remotecontrol::LayerMatrix &weights) {
  71. if (m_layers.isEmpty()) {
  72. return;
  73. }
  74. auto layer = m_layers[weights.layer()];
  75. layer->m_weights.updateValues(Dense(weights.matrix().matrix()));
  76. layer->m_weightTrigger.updateLayer();
  77. });
  78. client->subscribeActivationsUpdates({});
  79. client->subscribeWeightsUpdates({});
  80. }
  81. ValueIndicator *VisualizerModel::activation(int layer, int row)
  82. {
  83. ValueIndicator* indicator = m_layers[layer]->m_activations.value<ValueIndicator*>(row, 0);
  84. QQmlEngine::setObjectOwnership(indicator, QQmlEngine::CppOwnership);
  85. return indicator;
  86. }
  87. ValueIndicator *VisualizerModel::weight(int layer, int row, int column)
  88. {
  89. ValueIndicator* indicator = m_layers[layer]->m_weights.value<ValueIndicator*>(row, column);
  90. QQmlEngine::setObjectOwnership(indicator, QQmlEngine::CppOwnership);
  91. return indicator;
  92. }
  93. LayerTrigger *VisualizerModel::activationTrigger(int layer)
  94. {
  95. LayerTrigger* trigger = &m_layers[layer]->m_actiovationTrigger;
  96. QQmlEngine::setObjectOwnership(trigger, QQmlEngine::CppOwnership);
  97. return trigger;
  98. }
  99. LayerTrigger *VisualizerModel::weightTrigger(int layer)
  100. {
  101. LayerTrigger* trigger = &m_layers[layer]->m_weightTrigger;
  102. QQmlEngine::setObjectOwnership(trigger, QQmlEngine::CppOwnership);
  103. return trigger;
  104. }