ModelBoard.vue 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <template>
  2. <v-card width="50%" flat>
  3. <v-row>
  4. <v-col md="6" cols="12">
  5. <v-menu
  6. v-for="(layer, i) in layers"
  7. :key="i"
  8. offset-x
  9. :close-on-content-click="false"
  10. >
  11. <template v-slot:activator="{ on }">
  12. <v-card height="50" width="100%" class="ml-2" tile v-on="on">
  13. <v-row>
  14. <v-col cols="2" class="text-center">
  15. {{ i + 1 }}
  16. </v-col>
  17. <v-col cols="10" class="text-center">
  18. {{ layer.layerName }}
  19. </v-col>
  20. </v-row>
  21. </v-card>
  22. </template>
  23. <!-- Conv2d Setting Board -->
  24. <layer-setting-board
  25. v-if="layer.layerType === 'conv2d'"
  26. :layer="layer"
  27. >
  28. <v-row>
  29. <v-col cols="12" md="4">
  30. <v-select
  31. v-model="layer.kernelSize"
  32. :items="[1, 2, 3, 4, 5, 6]"
  33. label="Kernel Size"
  34. />
  35. </v-col>
  36. <v-col cols="12" md="4">
  37. <v-select
  38. v-model="layer.filters"
  39. :items="[16, 32, 48, 64]"
  40. label="Filters"
  41. />
  42. </v-col>
  43. <v-col cols="12" md="4">
  44. <v-select
  45. v-model="layer.activation"
  46. :items="activations"
  47. label="Activation"
  48. />
  49. </v-col>
  50. </v-row>
  51. </layer-setting-board>
  52. <!-- Pooling Layer Setting Board -->
  53. <layer-setting-board
  54. v-else-if="isPoolingLayer(layer.layerType)"
  55. :layer="layer"
  56. >
  57. <v-row>
  58. <v-col cols="12" md="6">
  59. <v-select
  60. v-model="layer.poolsize"
  61. label="Pool Size"
  62. :items="[2, 3, 4, 5, 6]"
  63. />
  64. </v-col>
  65. <v-col cols="12" md="6">
  66. <v-select
  67. v-model="layer.strides"
  68. label="Strides"
  69. :items="[2, 3, 4, 5, 6]"
  70. />
  71. </v-col>
  72. </v-row>
  73. </layer-setting-board>
  74. <!-- Dense Layer Setting Board -->
  75. <layer-setting-board
  76. v-else-if="layer.layerType === 'dense'"
  77. :layer="layer"
  78. >
  79. <v-row>
  80. <v-col cols="12" md="4">
  81. <v-select
  82. v-model="layer.activation"
  83. :items="activations"
  84. label="Activation"
  85. />
  86. </v-col>
  87. <v-col cols="12" md="4">
  88. <v-text-field
  89. v-model="layer.units"
  90. type="number"
  91. label="Units"
  92. />
  93. </v-col>
  94. </v-row>
  95. </layer-setting-board>
  96. <!-- Dense Layer Setting Board -->
  97. </v-menu>
  98. <v-row>
  99. <v-col cols="12" class="text-right mx-2">
  100. <!-- buttons for adding new layer -->
  101. <v-menu :close-on-content-click="false" offset-x>
  102. <template v-slot:activator="{ on }">
  103. <v-btn block large outlined color="primary" v-on="on">
  104. Add Layer
  105. </v-btn>
  106. </template>
  107. <v-card max-width="450" class="pa-2">
  108. <v-row>
  109. <v-btn
  110. v-for="(item, i) in addLayerBtns"
  111. :key="i"
  112. color="primary"
  113. class="ma-2"
  114. @click="item.clickCallback"
  115. >
  116. {{ item.layerType }}
  117. </v-btn>
  118. </v-row>
  119. </v-card>
  120. </v-menu>
  121. </v-col>
  122. <v-col cols="12">
  123. <v-btn block outlined color="success" @click="compileModel">
  124. Compile
  125. </v-btn>
  126. </v-col>
  127. </v-row>
  128. </v-col>
  129. </v-row>
  130. </v-card>
  131. </template>
  132. <script lang="ts">
  133. import Vue from 'vue'
  134. import * as tf from '@tensorflow/tfjs'
  135. import { Layer, LayerType, activations } from './layer'
  136. import LayerSettingBoard from './LayerSetttingBoard.vue'
  137. import { IMAGE_H, IMAGE_W } from './data'
  138. const headers = [
  139. { text: 'Layer Name', value: 'layerName' },
  140. { text: 'Output Shape', value: 'outputShape' },
  141. { text: 'Number of Params', value: 'paramsNum' },
  142. { text: 'Trainable', value: 'trainable' }
  143. ]
  144. interface AddLayerButton {
  145. layerType: LayerType
  146. clickCallback: () => void
  147. }
  148. export default Vue.extend({
  149. components: {
  150. LayerSettingBoard
  151. },
  152. data() {
  153. return {
  154. headers,
  155. addLayerBtns: [] as AddLayerButton[],
  156. layers: [] as Layer[],
  157. settingBoardWidth: 500,
  158. settingBoardHeight: 300,
  159. activations
  160. }
  161. },
  162. created() {
  163. this.addLayerBtns = [
  164. {
  165. layerType: 'conv2d',
  166. clickCallback: () => this.layers.push(Layer.conv2dLayer())
  167. },
  168. {
  169. layerType: 'maxPooling2d',
  170. clickCallback: () => this.layers.push(Layer.maxPooling2dLayer())
  171. },
  172. {
  173. layerType: 'averagePooling2d',
  174. clickCallback: () => this.layers.push(Layer.averagePooling2dLayer())
  175. },
  176. {
  177. layerType: 'flatten',
  178. clickCallback: () => this.layers.push(Layer.flattenLayer())
  179. },
  180. {
  181. layerType: 'dense',
  182. clickCallback: () => this.layers.push(Layer.denseLayer())
  183. }
  184. ]
  185. },
  186. methods: {
  187. isPoolingLayer(layerType: string) {
  188. return layerType === 'maxPooling2d' || layerType === 'averagePooling2d'
  189. },
  190. compileModel() {
  191. const model = tf.sequential()
  192. for (let i = 0; i < this.layers.length; i++) {
  193. const layer = this.layers[i]
  194. if (i === 0) {
  195. model.add(layer.toTfLayer([IMAGE_W, IMAGE_H, 1]))
  196. } else {
  197. model.add(layer.toTfLayer())
  198. }
  199. }
  200. model.summary()
  201. }
  202. }
  203. })
  204. </script>
  205. <style scoped></style>