main.qml 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. import remotecontrol 1.0
  30. ApplicationWindow {
  31. id: root
  32. visible: true
  33. width: 1024
  34. height: 768
  35. Rectangle {
  36. id: bottomLayer
  37. anchors.fill: parent
  38. color: "#000000"
  39. }
  40. Row {
  41. id: rowww
  42. anchors.left: parent.left
  43. anchors.top: parent.top
  44. anchors.margins: 10
  45. Repeater {
  46. id: layerRepeater
  47. model: visualizerModel.sizes.length
  48. delegate: Item {
  49. id: layerDelegate
  50. property alias neurons: neuronRepeater
  51. anchors.verticalCenter: parent.verticalCenter
  52. property int layerIndex: model.index
  53. property int layerSize: visualizerModel.sizes[layerIndex]
  54. width: 120
  55. height: activationsColumn.height
  56. Column {
  57. id: activationsColumn
  58. width: 50
  59. spacing: 10
  60. Repeater {
  61. id: neuronRepeater
  62. model: layerSize
  63. delegate:Rectangle {
  64. width: 20
  65. height: 20
  66. radius: 10
  67. color: "#00ffffff"
  68. Rectangle {
  69. id: neuron
  70. property ValueIndicator activation: visualizerModel.activation(layerIndex, model.index)
  71. anchors.fill: parent
  72. radius: 15
  73. color: "#00ff00"
  74. ColorAnimation {
  75. id: anim
  76. target: neuron
  77. property: "color"
  78. to: "#000000"
  79. duration: 100
  80. }
  81. function updateColor() {
  82. var alpha = activation.value;
  83. neuron.color = anim.to
  84. anim.stop()
  85. anim.from = anim.to
  86. anim.to = Qt.rgba(0, 1, 0, Math.max(0.08, alpha))
  87. anim.start()
  88. }
  89. Component.onCompleted: {
  90. visualizerModel.activationTrigger(layerIndex).updateLayer.connect(neuron.updateColor);
  91. }
  92. }
  93. }
  94. }
  95. }
  96. }
  97. }
  98. onWidthChanged: {
  99. console.log("width: " + width)
  100. if (width !== visualizerModel.sizes.length*120) {
  101. return
  102. }
  103. for (var i = 1; i < layerRepeater.count; i++) {
  104. var neurons = layerRepeater.itemAt(i).neurons
  105. var neuronsPrev = layerRepeater.itemAt(i - 1).neurons
  106. for (var j = 0; j < neurons.count; j++) {
  107. var neuron = neurons.itemAt(j)
  108. var coord = layerRepeater.itemAt(i).mapToItem(root.contentItem, neuron.x + neuron.width/2, neuron.y + neuron.height/2)
  109. for(var k = 0; k < neuronsPrev.count; k++) {
  110. var neuronPrev = neuronsPrev.itemAt(k)
  111. var coordPrev = layerRepeater.itemAt(i - 1).mapToItem(root.contentItem, neuronPrev.x + neuronPrev.width/2, neuronPrev.y + neuronPrev.height/2)
  112. var angle = Math.atan2(coordPrev.y - coord.y, coordPrev.x - coord.x) * 180 / Math.PI
  113. var length = Math.sqrt(Math.pow(coordPrev.x - coord.x, 2) + Math.pow(coordPrev.y - coord.y, 2))
  114. var obj = connectionComponent.createObject(bottomLayer, {
  115. x: coord.x,
  116. y: coord.y,
  117. width: length,
  118. angle: angle,
  119. weight: visualizerModel.weight(i, j, k),
  120. })
  121. visualizerModel.weightTrigger(i).updateLayer.connect(obj.updateColor);
  122. }
  123. }
  124. }
  125. }
  126. }
  127. Component {
  128. id: connectionComponent
  129. Rectangle {
  130. id: connection
  131. property alias angle: trans.angle
  132. property ValueIndicator weight: null
  133. transformOrigin: Item.Left
  134. transform: Rotation {
  135. id: trans
  136. }
  137. color: "transparent"
  138. height: 1
  139. function updateColor() {
  140. var newColor = weight.value;
  141. connection.color = Qt.rgba(newColor, 0, 1.0 - newColor, newColor > 0 ? 0.5 : 0.0)
  142. }
  143. }
  144. }
  145. Row {
  146. anchors.right: parent.right
  147. anchors.top: parent.top
  148. anchors.margins: 20
  149. spacing: 10
  150. Text {
  151. color: "#ffffff"
  152. text: "Active state: " + visualizerModel.networkState.state
  153. }
  154. Button {
  155. id: start
  156. text: "Start"
  157. enabled: visualizerModel.networkState.state === NetworkState.Idle
  158. onClicked: {
  159. visualizerModel.start();
  160. }
  161. }
  162. }
  163. }