visualizermodel.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. m_layers.last()->m_activations.setDimentions(m_networkConfig.sizes()[i], 1);
  40. QList<ValueIndicator*> data;
  41. for (int k = 0; k < m_networkConfig.sizes()[i]; k++) {
  42. data.append(new ValueIndicator);
  43. }
  44. m_layers.last()->m_activations.setData(data);
  45. if (i != 0) {
  46. int tolalItems = m_networkConfig.sizes()[i]*m_networkConfig.sizes()[i - 1];
  47. m_layers.last()->m_weights.setDimentions(m_networkConfig.sizes()[i], m_networkConfig.sizes()[i - 1]);
  48. data.clear();
  49. for (int k = 0; k < tolalItems; k++) {
  50. data.append(new ValueIndicator);
  51. }
  52. m_layers.last()->m_weights.setData(data);
  53. }
  54. }
  55. sizesChanged();
  56. });
  57. QObject::connect(client.get(), &remotecontrol::RemoteControlClient::ActivationsUpdated, [this](const remotecontrol::LayerMatrix &activations) {
  58. if (m_layers.isEmpty()) {
  59. return;
  60. }
  61. Dense dense(activations.matrix().matrix());
  62. m_layers[activations.layer()]->m_activations.updateValues(Dense(activations.matrix().matrix()));
  63. // qDebug() << "ActivationsUpdated:" << dense.rows() << dense.columns() << activations.layer();
  64. });
  65. QObject::connect(client.get(), &remotecontrol::RemoteControlClient::BiasesUpdated, [this](const remotecontrol::LayerMatrix &biases) {
  66. if (m_layers.isEmpty()) {
  67. return;
  68. }
  69. Dense dense(biases.matrix().matrix());
  70. // qDebug() << "BiasesUpdated:" << dense.rows() << dense.columns();
  71. });
  72. QObject::connect(client.get(), &remotecontrol::RemoteControlClient::WeightsUpdated, [this](const remotecontrol::LayerMatrix &weights) {
  73. if (m_layers.isEmpty()) {
  74. return;
  75. }
  76. m_layers[weights.layer()]->m_weights.updateValues(Dense(weights.matrix().matrix()));
  77. });
  78. client->subscribeActivationsUpdates({});
  79. client->subscribeBiasesUpdates({});
  80. client->subscribeWeightsUpdates({});
  81. }
  82. ValueIndicator *VisualizerModel::activation(int layer, int row)
  83. {
  84. ValueIndicator* indicator = m_layers[layer]->m_activations.value<ValueIndicator*>(row, 0);
  85. QQmlEngine::setObjectOwnership(indicator, QQmlEngine::CppOwnership);
  86. return indicator;
  87. }
  88. ValueIndicator *VisualizerModel::weight(int layer, int row, int column)
  89. {
  90. ValueIndicator* indicator = m_layers[layer]->m_weights.value<ValueIndicator*>(row, column);
  91. QQmlEngine::setObjectOwnership(indicator, QQmlEngine::CppOwnership);
  92. return indicator;
  93. }