main.qml 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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: {
  73. var alpha = activation.value
  74. Qt.rgba(0, 1, 0, alpha)
  75. }
  76. }
  77. }
  78. }
  79. }
  80. }
  81. }
  82. onWidthChanged: {
  83. console.log("width: " + width)
  84. if (width !== visualizerModel.sizes.length*120) {
  85. return
  86. }
  87. for (var i = 1; i < layerRepeater.count; i++) {
  88. var neurons = layerRepeater.itemAt(i).neurons
  89. var neuronsPrev = layerRepeater.itemAt(i - 1).neurons
  90. for (var j = 0; j < neurons.count; j++) {
  91. var neuron = neurons.itemAt(j)
  92. var coord = layerRepeater.itemAt(i).mapToItem(root.contentItem, neuron.x + neuron.width/2, neuron.y + neuron.height/2)
  93. for(var k = 0; k < neuronsPrev.count; k++) {
  94. var neuronPrev = neuronsPrev.itemAt(k)
  95. var coordPrev = layerRepeater.itemAt(i - 1).mapToItem(root.contentItem, neuronPrev.x + neuronPrev.width/2, neuronPrev.y + neuronPrev.height/2)
  96. var angle = Math.atan2(coordPrev.y - coord.y, coordPrev.x - coord.x) * 180 / Math.PI
  97. var length = Math.sqrt(Math.pow(coordPrev.x - coord.x, 2) + Math.pow(coordPrev.y - coord.y, 2))
  98. connection.createObject(bottomLayer, {
  99. x: coord.x,
  100. y: coord.y,
  101. width: length,
  102. angle: angle,
  103. weight: visualizerModel.weight(i, j, k),
  104. })
  105. }
  106. }
  107. }
  108. }
  109. }
  110. Component {
  111. id: connection
  112. Rectangle {
  113. property alias angle: trans.angle
  114. property ValueIndicator weight: null
  115. transformOrigin: Item.Left
  116. transform: Rotation {
  117. id: trans
  118. }
  119. height: 1
  120. color: {
  121. var color = weight.value
  122. Qt.rgba(color, 0, 1.0/color, color > 0 ? 0.5 : 0.0)
  123. }
  124. }
  125. }
  126. }