visualizermodel.cpp 5.0 KB

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