main.qml 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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: "#00ff00"
  73. ColorAnimation {
  74. id: anim
  75. target: neuron
  76. property: "color"
  77. to: "#000000"
  78. duration: 150
  79. }
  80. function updateColor() {
  81. var alpha = activation.value;
  82. neuron.color = anim.to
  83. anim.stop()
  84. anim.from = anim.to
  85. anim.to = Qt.rgba(0, 1, 0, Math.max(0.08, alpha))
  86. anim.start()
  87. }
  88. Component.onCompleted: {
  89. visualizerModel.activationTrigger(layerIndex).updateLayer.connect(neuron.updateColor);
  90. }
  91. }
  92. }
  93. }
  94. }
  95. }
  96. }
  97. onWidthChanged: {
  98. console.log("width: " + width)
  99. if (width !== visualizerModel.sizes.length*120) {
  100. return
  101. }
  102. for (var i = 1; i < layerRepeater.count; i++) {
  103. var neurons = layerRepeater.itemAt(i).neurons
  104. var neuronsPrev = layerRepeater.itemAt(i - 1).neurons
  105. for (var j = 0; j < neurons.count; j++) {
  106. var neuron = neurons.itemAt(j)
  107. var coord = layerRepeater.itemAt(i).mapToItem(root.contentItem, neuron.x + neuron.width/2, neuron.y + neuron.height/2)
  108. for(var k = 0; k < neuronsPrev.count; k++) {
  109. var neuronPrev = neuronsPrev.itemAt(k)
  110. var coordPrev = layerRepeater.itemAt(i - 1).mapToItem(root.contentItem, neuronPrev.x + neuronPrev.width/2, neuronPrev.y + neuronPrev.height/2)
  111. var angle = Math.atan2(coordPrev.y - coord.y, coordPrev.x - coord.x) * 180 / Math.PI
  112. var length = Math.sqrt(Math.pow(coordPrev.x - coord.x, 2) + Math.pow(coordPrev.y - coord.y, 2))
  113. var obj = connectionComponent.createObject(bottomLayer, {
  114. x: coord.x,
  115. y: coord.y,
  116. width: length,
  117. angle: angle,
  118. weight: visualizerModel.weight(i, j, k),
  119. })
  120. visualizerModel.weightTrigger(i).updateLayer.connect(obj.updateColor);
  121. }
  122. }
  123. }
  124. }
  125. }
  126. Component {
  127. id: connectionComponent
  128. Rectangle {
  129. id: connection
  130. property alias angle: trans.angle
  131. property ValueIndicator weight: null
  132. transformOrigin: Item.Left
  133. transform: Rotation {
  134. id: trans
  135. }
  136. color: "transparent"
  137. height: 1
  138. function updateColor() {
  139. var newColor = weight.value;
  140. connection.color = Qt.rgba(newColor, 0, 1.0 - newColor, newColor > 0 ? 0.5 : 0.0)
  141. }
  142. }
  143. }
  144. Button {
  145. id: start
  146. text: "Start"
  147. anchors.right: parent.right
  148. anchors.top: parent.top
  149. anchors.margins: 20
  150. onClicked: {
  151. visualizerModel.start();
  152. }
  153. }
  154. }