main.qml 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. import QtQuick 2.11
  26. import QtQuick.Window 2.11
  27. import QtQuick.Controls 1.4
  28. import NeuralNetworkUi 0.1
  29. ApplicationWindow {
  30. id: root
  31. visible: true
  32. width: 1024
  33. height: 768
  34. Rectangle {
  35. id: bottomLayer
  36. anchors.fill: parent
  37. color: "#000000"
  38. }
  39. Row {
  40. id: rowww
  41. anchors.left: parent.left
  42. anchors.top: parent.top
  43. anchors.margins: 10
  44. Repeater {
  45. id: layerRepeater
  46. model: visualizerModel.sizes.length
  47. delegate: Item {
  48. id: layerDelegate
  49. property alias neurons: neuronRepeater
  50. anchors.verticalCenter: parent.verticalCenter
  51. property int layerIndex: model.index
  52. property int layerSize: visualizerModel.sizes[layerIndex]
  53. width: 120
  54. height: activationsColumn.height
  55. Column {
  56. id: activationsColumn
  57. width: 50
  58. spacing: 20
  59. Repeater {
  60. id: neuronRepeater
  61. model: layerSize
  62. delegate:Rectangle {
  63. width: 30
  64. height: 30
  65. radius: 15
  66. color: "#00ffffff"
  67. Rectangle {
  68. id: neuron
  69. property ValueIndicator activation: visualizerModel.activation(layerIndex, model.index)
  70. anchors.fill: parent
  71. radius: 15
  72. color: "transparent"
  73. function updateColor() {
  74. var alpha = activation.value;
  75. neuron.color = Qt.rgba(0, 1, 0, Math.max(0.08, alpha))
  76. }
  77. Component.onCompleted: {
  78. visualizerModel.activationTrigger(layerIndex).updateLayer.connect(neuron.updateColor);
  79. }
  80. }
  81. }
  82. }
  83. }
  84. }
  85. }
  86. onWidthChanged: {
  87. console.log("width: " + width)
  88. if (width !== visualizerModel.sizes.length*120) {
  89. return
  90. }
  91. for (var i = 1; i < layerRepeater.count; i++) {
  92. var neurons = layerRepeater.itemAt(i).neurons
  93. var neuronsPrev = layerRepeater.itemAt(i - 1).neurons
  94. for (var j = 0; j < neurons.count; j++) {
  95. var neuron = neurons.itemAt(j)
  96. var coord = layerRepeater.itemAt(i).mapToItem(root.contentItem, neuron.x + neuron.width/2, neuron.y + neuron.height/2)
  97. for(var k = 0; k < neuronsPrev.count; k++) {
  98. var neuronPrev = neuronsPrev.itemAt(k)
  99. var coordPrev = layerRepeater.itemAt(i - 1).mapToItem(root.contentItem, neuronPrev.x + neuronPrev.width/2, neuronPrev.y + neuronPrev.height/2)
  100. var angle = Math.atan2(coordPrev.y - coord.y, coordPrev.x - coord.x) * 180 / Math.PI
  101. var length = Math.sqrt(Math.pow(coordPrev.x - coord.x, 2) + Math.pow(coordPrev.y - coord.y, 2))
  102. var obj = connectionComponent.createObject(bottomLayer, {
  103. x: coord.x,
  104. y: coord.y,
  105. width: length,
  106. angle: angle,
  107. weight: visualizerModel.weight(i, j, k),
  108. })
  109. visualizerModel.weightTrigger(i).updateLayer.connect(obj.updateColor);
  110. }
  111. }
  112. }
  113. }
  114. }
  115. Component {
  116. id: connectionComponent
  117. Rectangle {
  118. id: connection
  119. property alias angle: trans.angle
  120. property ValueIndicator weight: null
  121. transformOrigin: Item.Left
  122. transform: Rotation {
  123. id: trans
  124. }
  125. color: "transparent"
  126. height: 1
  127. function updateColor() {
  128. var newColor = weight.value;
  129. connection.color = Qt.rgba(newColor, 0, 1.0 - newColor, newColor > 0 ? 0.5 : 0.0)
  130. }
  131. }
  132. }
  133. }