This repository has been archived by the owner on Jan 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 31
/
CGTNodes.py
2160 lines (1780 loc) · 75.5 KB
/
CGTNodes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
bl_info = {
"name": "Caffe Nodes",
"category": "Object",
}
import subprocess
import bpy
from bpy.types import NodeTree, Node, NodeSocket
def calcsize(self, context, axis='x'):
'''NOTE - this function works out the dimensions of an image by the time it has reached a certain layer.
It traverses all the layers, builds up several lists about the properties of each layer, then computes the
size up to a given layer.'''
x = 0.0
node = self
try:
node.inputs[0].links[0].from_node
except IndexError:
return 0
# These are the lists to be populated
kernelsizes = []
strides = []
paddings = []
offsets = []
fcsizes = []
reversals = []
passes = []
counter = 0
while 1 == 1:
if node.bl_idname in ["ConvNodeType", "PoolNodeType", "DeConvNodeType"]:
if node.square_kernel:
kernelsizes.extend([node.kernel_size])
elif axis == 'x':
kernelsizes.extend([node.kernel_w])
elif axis == 'y':
kernelsizes.extend([node.kernel_h])
else:
raise RuntimeError
if node.square_stride:
strides.extend([node.stride])
elif axis == 'x':
strides.extend([node.stride_w])
elif axis == 'y':
strides.extend([node.stride_h])
else:
raise RuntimeError
if node.square_padding:
paddings.extend([node.pad])
elif axis == 'x':
paddings.extend([node.pad_w])
elif axis == 'y':
paddings.extend([node.pad_h])
else:
raise RuntimeError
offsets.extend([0])
fcsizes.extend([0])
passes.extend([0])
if node.bl_idname == "DeConvNodeType":
reversals.extend([1])
else:
reversals.extend([0])
node = node.inputs[0].links[0].from_node
elif node.bl_idname == "FCNodeType":
kernelsizes.extend([0])
strides.extend([0])
paddings.extend([0])
offsets.extend([0])
passes.extend([0])
reversals.extend([0])
fcsizes.extend([node.num_output])
node = node.inputs[0].links[0].from_node
elif node.bl_idname == "DataNodeType":
# When the data node is reached, we must be at the back of the nodetree, so start to work forwards
square = 0
if node.db_type == 'ImageData':
if node.new_height == node.new_width:
square = 1
h = node.new_height
w = node.new_width
if node.db_type != 'ImageData':
if node.height == node.width:
square = 1
h = node.height
w = node.width
if square:
x = float(w)
elif axis == 'x':
x = float(w)
else:
x = float(h)
# work forwards
numofnodes = len(passes)
for node in range(numofnodes):
# - 1 as starts from 0
node = (numofnodes - 1) - node
padding = paddings[node]
stride = strides[node]
ksize = kernelsizes[node]
offset = offsets[node]
reversal = reversals[node]
if passes[node] == 0:
if fcsizes[node] == 0:
if reversal == 0:
#########################
x = ((x + (2 * padding) - ksize) / stride + 1 - offset)
###################
else:
x = (x * stride - stride) + ksize - 2 * padding
else:
x = fcsizes[node]
break
elif node.bl_idname not in ["DataNodeType", "FCNodeType", "DeConvNodeType", "ConvNodeType", "PoolNodeType"]:
kernelsizes.extend([0])
strides.extend([0])
paddings.extend([0])
offsets.extend([0])
reversals.extend([0])
fcsizes.extend([0])
passes.extend([1])
node = node.inputs[0].links[0].from_node
counter += 1
if counter > 1000:
x = 0
break
return str(round(x, 2))
############################## Function for determining number of gpus
def getgpus():
command = ['nvidia-smi', '-L']
try:
proc = subprocess.Popen(command, bufsize=1, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
universal_newlines=True)
except OSError:
return 'Error'
lines = []
while proc.poll() is None: # while alive
line = proc.stdout.readline()
if line:
# Process output here
lines.append(line)
return len(lines)
##################################
# Derived from the NodeTree base type, similar to Menu, Operator, Panel, etc.
class CaffeTree(NodeTree):
# Description string
'''A custom node tree type that will show up in the node editor header'''
# Optional identifier string. If not explicitly defined, the python class name is used.
bl_idname = 'CaffeNodeTree'
# Label for nice name display
bl_label = 'Caffe Node Tree'
bl_icon = 'NODETREE'
# Custom socket type
class ImageSocket(NodeSocket):
# Description string
'''Blob socket type'''
# Optional identifier string. If not explicitly defined, the python class name is used.
bl_idname = 'ImageSocketType'
# Label for nice name display
bl_label = 'Image Socket'
# Enum items list
# Optional function for drawing the socket input value
def draw(self, context, layout, node, text):
layout.label(text)
# Socket color
def draw_color(self, context, node):
return (0.0, 1.0, 1.0, 0.5)
class OutputSocket(NodeSocket):
# Description string
'''Custom node socket type'''
# Optional identifier string. If not explicitly defined, the python class name is used.
bl_idname = 'OutputSocketType'
# Label for nice name display
bl_label = 'Output Socket'
# Enum items list
output_name = bpy.props.StringProperty(name='')
# Optional function for drawing the socket input value
def draw(self, context, layout, node, text):
layout.label("Optional name")
layout.prop(self, "output_name")
# Socket color
def draw_color(self, context, node):
return (0.0, 1.0, 1.0, 0.5)
class InPlaceOutputSocket(NodeSocket):
# Description string
'''Custom node socket type'''
# Optional identifier string. If not explicitly defined, the python class name is used.
bl_idname = 'InPlaceOutputSocketType'
# Label for nice name display
bl_label = 'In Place Output Socket'
# Enum items list
output_name = bpy.props.StringProperty(name='', default='')
def draw(self, context, layout, node, text):
layout.label(text)
# Socket color
def draw_color(self, context, node):
return (0.0, 1.0, 1.0, 0.5)
class LabelSocket(NodeSocket):
# Description string
'''Label socket type'''
# Optional identifier string. If not explicitly defined, the python class name is used.
bl_idname = 'LabelSocketType'
# Label for nice name display
bl_label = 'Label Socket'
# Enum items list
# Optional function for drawing the socket input value
def draw(self, context, layout, node, text):
layout.label(text)
# Socket color
def draw_color(self, context, node):
return (0.5, 1.0, 0.2, 0.5)
class LossSocket(NodeSocket):
# Description string
'''Loss socket type'''
# Optional identifier string. If not explicitly defined, the python class name is used.
bl_idname = 'LossSocketType'
# Label for nice name display
bl_label = 'Loss Socket'
# Enum items list
# Optional function for drawing the socket input value
def draw(self, context, layout, node, text):
layout.label(text)
# Socket color
def draw_color(self, context, node):
return (1.0, 0.3, 1.0, 0.5)
class NAFlatSocket(NodeSocket):
# Description string
'''NAFlat socket type'''
# Optional identifier string. If not explicitly defined, the python class name is used.
bl_idname = 'NAFlatSocketType'
# Label for nice name display
bl_label = 'Linear Flat Socket'
# Enum items list
# Optional function for drawing the socket input value
def draw(self, context, layout, node, text):
layout.label(text)
# Socket color
def draw_color(self, context, node):
return (1.0, 0.2, 0.2, 0.5)
class AFlatSocket(NodeSocket):
# Description string
'''AFlat socket type'''
# Optional identifier string. If not explicitly defined, the python class name is used.
bl_idname = 'AFlatSocketType'
# Label for nice name display
bl_label = 'Non linear Flat Socket'
# Enum items list
# Optional function for drawing the socket input value
def draw(self, context, layout, node, text):
layout.label(text)
# Socket color
def draw_color(self, context, node):
return (0.0, 0.8, 0.8, 0.5)
class params_p_gw(bpy.types.PropertyGroup):
name = bpy.props.StringProperty(name='Shared name')
lr_mult = bpy.props.FloatProperty(default=1.0)
decay_mult = bpy.props.FloatProperty(default=1.0)
def draw(self, context, layout):
# layout.prop(self, "name")
layout.prop(self, "lr_mult")
layout.prop(self, "decay_mult")
class params_p_gb(bpy.types.PropertyGroup):
name = bpy.props.StringProperty(name='Shared name')
lr_mult = bpy.props.FloatProperty(default=2.0)
decay_mult = bpy.props.FloatProperty(default=0.0)
def draw(self, context, layout):
# layout.prop(self, "name")
layout.prop(self, "lr_mult")
layout.prop(self, "decay_mult")
class CaffeTreeNode:
@classmethod
def poll(cls, ntree):
return ntree.bl_idname == 'CaffeNodeTree'
extra_params = bpy.props.BoolProperty(name='Extra Parameters', default=False)
weight_params = bpy.props.PointerProperty(type=params_p_gw)
bias_params = bpy.props.PointerProperty(type=params_p_gb)
phases = [("TRAIN", "TRAIN", "Train only"),
("TEST", "TEST", "Test only"),
("BOTH", "BOTH", "Both")]
include_in = bpy.props.EnumProperty(items=phases, default="BOTH")
def draw_include_in(self, layout):
layout.prop(self, "include_in")
def draw_extra_params(self, context, layout):
layout.prop(self, "extra_params")
if self.extra_params:
layout.label("Weight Params")
self.weight_params.draw(context, layout)
layout.label("Bias Params")
self.bias_params.draw(context, layout)
class DataNode(Node, CaffeTreeNode):
# === Basics ===
# Description string
'''A data node'''
# Optional identifier string. If not explicitly defined, the python class name is used.
bl_idname = 'DataNodeType'
# Label for nice name display
bl_label = 'Data input'
# Icon identifier
bl_icon = 'SOUND'
DBs = [
("LMDB", "LMDB", "Lmdb database"),
("LEVELDB", "LEVELDB", "LevelDB database"),
("ImageData", "ImageData", "Image files"),
("HDF5Data", "HDF5Data", "HDF5 Data")
]
Phases = [
("TRAINANDTEST", "TRAINANDTEST", "Train and Test"),
("TRAIN", "TRAIN", "Train"),
("TEST", "TEST", "Test")
]
# === Custom Properties ===
db_type = bpy.props.EnumProperty(name="Database type", description="Type of Data", items=DBs, default='HDF5Data')
train_batch_size = bpy.props.IntProperty(min=1, default=100)
test_batch_size = bpy.props.IntProperty(min=1, default=100)
train_path = bpy.props.StringProperty(
name="Train Data Path",
default="",
description="Get the path to the data",
subtype='DIR_PATH'
)
test_path = bpy.props.StringProperty(
name="Test Data Path",
default="",
description="Get the path to the data",
subtype='DIR_PATH'
)
train_data = bpy.props.StringProperty(
name="Train Data File",
default="",
description="Get the path to the data",
subtype='FILE_PATH'
)
test_data = bpy.props.StringProperty(
name="Test Data File",
default="",
description="Get the path to the data",
subtype='FILE_PATH'
)
# Transformation params
# include_in = bpy.props.EnumProperty(name="Include in", description="Phases to include in",items=Phases,default='TRAINANDTEST')
scale = bpy.props.FloatProperty(default=1.0, min=0)
mirror = bpy.props.BoolProperty(name='Random Mirror', default=False)
use_mean_file = bpy.props.BoolProperty(name='Use mean file', default=False)
mean_file = bpy.props.StringProperty(
name="Mean File Path",
default="",
description="Mean file location",
subtype='FILE_PATH'
)
# TODO: Add Mean Value and random crop
# Image data params
new_height = bpy.props.IntProperty(name="New image height", min=0, default=0, soft_max=1000)
new_width = bpy.props.IntProperty(name="New image width", min=0, default=0, soft_max=1000)
height = bpy.props.IntProperty(name="Image height", min=0, default=0, soft_max=1000)
width = bpy.props.IntProperty(name="Image width", min=0, default=0, soft_max=1000)
channels = bpy.props.IntProperty(name="Image Channels", min=1, default=1, soft_max=5)
is_color = bpy.props.BoolProperty(name="Is color image", default=True)
# For Image data + HDF5 data
shuffle = bpy.props.BoolProperty(name='Shuffle', default=False)
# For Data + Image data
rand_skip = bpy.props.IntProperty(name="Random skip", min=0, default=0, soft_max=1000)
# TODO: Add non supervised property
# === Optional Functions ===
def init(self, context):
self.outputs.new('OutputSocketType', "Image Stack")
self.outputs.new('OutputSocketType', "Label")
# Copy function to initialize a copied node from an existing one.
def copy(self, node):
print("Copying from node ", node)
# Free function to clean up on removal.
def free(self):
print("Removing node ", self, ", Goodbye!")
# Additional buttons displayed on the node.
def draw_buttons(self, context, layout):
layout.prop(self, "db_type")
layout.prop(self, "include_in")
if self.db_type in ('ImageData', 'HDF5Data'):
if self.include_in != 'TEST':
layout.prop(self, "train_data")
if self.include_in != 'TRAIN':
layout.prop(self, "test_data")
else:
if self.include_in != 'TEST':
layout.prop(self, "train_path")
if self.include_in != 'TRAIN':
layout.prop(self, "test_path")
if self.include_in != 'TEST':
layout.prop(self, "train_batch_size")
if self.include_in != 'TRAIN':
layout.prop(self, "test_batch_size")
if self.db_type in ('ImageData', 'LMDB', 'LEVELDB'):
layout.label("Transformation Parameters")
layout.prop(self, "scale")
layout.prop(self, "mirror")
layout.prop(self, "use_mean_file")
if self.use_mean_file:
layout.prop(self, "mean_file")
layout.label("Special Parameters")
if self.db_type == 'ImageData':
layout.prop(self, "shuffle")
layout.prop(self, "new_height")
layout.prop(self, "new_width")
layout.prop(self, "channels")
layout.prop(self, "is_color")
layout.prop(self, "rand_skip")
elif self.db_type == 'HDF5Data':
layout.prop(self, "shuffle")
layout.prop(self, "height")
layout.prop(self, "width")
layout.prop(self, "channels")
else:
layout.prop(self, "rand_skip")
layout.prop(self, "height")
layout.prop(self, "width")
layout.prop(self, "channels")
def draw_label(self):
return "Data Node"
class filler_p_g(bpy.types.PropertyGroup):
types = [("constant", "constant", "Constant val"),
("uniform", "uniform", "Uniform dist"),
("gaussian", "gaussian", "Gaussian dist"),
("positive_unitball", "positive_unitball", "Positive unit ball dist"),
("xavier", "xavier", "Xavier dist"),
("msra", "msra", "MSRA dist"),
("bilinear", "bilinear", "Bi-linear upsample weights")]
vnormtypes = [("FAN_IN", "FAN_IN", "Constant val"),
("FAN_OUT", "FAN_OUT", "Uniform dist"),
("AVERAGE", "AVERAGE", "Gaussian dist")]
type = bpy.props.EnumProperty(name='Type', items=types, default='xavier')
value = bpy.props.FloatProperty(default=0.0, soft_max=1000.0, soft_min=-1000.0)
min = bpy.props.FloatProperty(default=0.0, soft_max=1000.0, soft_min=-1000.0)
max = bpy.props.FloatProperty(default=1.0, soft_max=1000.0, soft_min=-1000.0)
mean = bpy.props.FloatProperty(default=0.0, soft_max=1000.0, soft_min=-1000.0)
std = bpy.props.FloatProperty(default=1.0, soft_max=1000.0, soft_min=-1000.0)
variance_norm = bpy.props.EnumProperty(name='Weight variance norm', default='FAN_IN', items=vnormtypes)
is_sparse = bpy.props.BoolProperty(name="Use Sparsity", default=False)
sparse = bpy.props.IntProperty(default=100, min=1)
def draw(self, context, layout):
layout.prop(self, "type")
if self.type == 'constant':
layout.prop(self, "value")
elif self.type in ('xavier', 'msra'):
layout.prop(self, "variance_norm")
elif self.type == 'gaussian':
layout.prop(self, "mean")
layout.prop(self, "std")
layout.prop(self, "is_sparse")
if self.is_sparse:
layout.prop(self, "sparse")
elif self.type == 'uniform':
layout.prop(self, "min")
layout.prop(self, "max")
class PoolNode(Node, CaffeTreeNode):
# === Basics ===
# Description string
'''A pooling node'''
# Optional identifier string. If not explicitly defined, the python class name is used.
bl_idname = 'PoolNodeType'
# Label for nice name display
bl_label = 'Pooling Node'
# Icon identifier
bl_icon = 'SOUND'
n_type = 'Pooling'
# === Custom Properties ===
modes = [
("MAX", "MAX", "Max pooling"),
("AVE", "AVE", "Average pooling"),
("STOCHASTIC", "SGD", "Stochastic pooling"),
]
square_padding = bpy.props.BoolProperty(name="Equal x,y padding", default=True)
pad = bpy.props.IntProperty(name="Padding", default=0, min=0, soft_max=5)
pad_h = bpy.props.IntProperty(name="Padding height", default=0, min=0, soft_max=5)
pad_w = bpy.props.IntProperty(name="Padding width", default=0, min=0, soft_max=5)
square_kernel = bpy.props.BoolProperty(name="Equal x,y kernel", default=True)
kernel_size = bpy.props.IntProperty(name="Kernel size", default=5, min=1, soft_max=25)
kernel_h = bpy.props.IntProperty(name="Kernel height", default=5, min=1, soft_max=25)
kernel_w = bpy.props.IntProperty(name="Kernel width", default=5, min=1, soft_max=25)
# TODO: Maybe add group
square_stride = bpy.props.BoolProperty(name="Equal x,y stride", default=True)
stride = bpy.props.IntProperty(name="Stride", default=1, min=1, soft_max=5)
stride_h = bpy.props.IntProperty(name="Stride height", default=1, min=1, soft_max=5)
stride_w = bpy.props.IntProperty(name="Stride width", default=1, min=1, soft_max=5)
mode = bpy.props.EnumProperty(name='Mode', default='MAX', items=modes)
# === Optional Functions ===
def init(self, context):
self.inputs.new('ImageSocketType', "Input image")
self.outputs.new('OutputSocketType', "Output image")
# Copy function to initialize a copied node from an existing one.
def copy(self, node):
print("Copying from node ", node)
# Free function to clean up on removal.
def free(self):
print("Removing node ", self, ", Goodbye!")
# Additional buttons displayed on the node.
def draw_buttons(self, context, layout):
try:
if calcsize(self, context, axis='x') != calcsize(self, context, axis='y'):
layout.label("image x,y output is %s,%s pixels" %
(calcsize(self, context, axis='x'), calcsize(self, context, axis='y')))
else:
layout.label("image output is %s pixels" % calcsize(self, context, axis='x'))
except IndexError:
pass
if self.square_padding:
layout.prop(self, "pad")
else:
layout.prop(self, "pad_h")
layout.prop(self, "pad_w")
if self.square_kernel:
layout.prop(self, "kernel_size")
else:
layout.prop(self, "kernel_h")
layout.prop(self, "kernel_w")
if self.square_stride:
layout.prop(self, "stride")
else:
layout.prop(self, "stride_h")
layout.prop(self, "stride_w")
layout.prop(self, "square_padding")
layout.prop(self, "square_kernel")
layout.prop(self, "square_stride")
layout.prop(self, "mode")
class EltwiseNode(Node, CaffeTreeNode):
# === Basics ===
# Description string
'''An element-wise node'''
# Optional identifier string. If not explicitly defined, the python class name is used.
bl_idname = 'EltwiseNodeType'
# Label for nice name display
bl_label = 'Element-wise Node'
# Icon identifier
bl_icon = 'SOUND'
n_type = 'Eltwise'
# === Custom Properties ===
eltwiseOps = [
("PROD", "PROD", "Eltwise prod: c(i) -> a(i)*b(i)"),
("SUM", "SUM", "Eltwise sum: c(i) -> a(i)+b(i)"),
("MAX", "MAX", "Eltwise max: c(i) -> max [a(i),b(i)]"),
]
coeff = bpy.props.FloatProperty(default=2.0, soft_max=10.0, min=0)
stable_prod_grad = bpy.props.BoolProperty(name='Stable(slower) gradient', default=1)
operation = bpy.props.EnumProperty(name='Operation', default='SUM', items=eltwiseOps)
# === Optional Functions ===
def init(self, context):
self.inputs.new('ImageSocketType', "Input blob A")
self.inputs.new('ImageSocketType', "Input blob B")
self.outputs.new('OutputSocketType', "Output blob C")
# Copy function to initialize a copied node from an existing one.
def copy(self, node):
print("Copying from node ", node)
# Free function to clean up on removal.
def free(self):
print("Removing node ", self, ", Goodbye!")
# Additional buttons displayed on the node.
def draw_buttons(self, context, layout):
layout.prop(self, "operation")
if self.operation == 'PROD':
layout.prop(self, "stable_prod_grad")
elif self.operation == 'SUM':
layout.prop(self, "coeff")
class ExpNode(Node, CaffeTreeNode):
# === Basics ===
# Description string
'''An exponential node'''
# Optional identifier string. If not explicitly defined, the python class name is used.
bl_idname = 'ExpNodeType'
# Label for nice name display
bl_label = 'Exponential Node'
# Icon identifier
bl_icon = 'SOUND'
n_type = 'Exp'
# === Custom Properties ===
base = bpy.props.FloatProperty(default=-1.0, soft_max=10.0, min=0)
scale = bpy.props.FloatProperty(default=1.0, soft_max=10.0, min=0)
shift = bpy.props.FloatProperty(default=0.0, soft_max=10.0, min=-10)
# === Optional Functions ===
def init(self, context):
self.inputs.new('ImageSocketType', "Input blob")
self.outputs.new('OutputSocketType', "Output blob")
# Copy function to initialize a copied node from an existing one.
def copy(self, node):
print("Copying from node ", node)
# Free function to clean up on removal.
def free(self):
print("Removing node ", self, ", Goodbye!")
# Additional buttons displayed on the node.
def draw_buttons(self, context, layout):
layout.prop(self, "base")
layout.prop(self, "scale")
layout.prop(self, "shift")
self.draw_extra_params(context, layout)
class MVNNode(Node, CaffeTreeNode):
# === Basics ===
# Description string
'''Mean variance normalization node'''
# Optional identifier string. If not explicitly defined, the python class name is used.
bl_idname = 'MVNNodeType'
# Label for nice name display
bl_label = 'MVN Node'
# Icon identifier
bl_icon = 'SOUND'
n_type = 'MVN'
# === Custom Properties ===
normalize_variance = bpy.props.BoolProperty(default=True)
across_channels = bpy.props.BoolProperty(default=False)
eps = bpy.props.FloatProperty(default=1e-9, soft_max=1.0, min=1e-20)
# === Optional Functions ===
def init(self, context):
self.inputs.new('ImageSocketType', "Input blob")
self.outputs.new('OutputSocketType', "Output blob")
# Copy function to initialize a copied node from an existing one.
def copy(self, node):
print("Copying from node ", node)
# Free function to clean up on removal.
def free(self):
print("Removing node ", self, ", Goodbye!")
# Additional buttons displayed on the node.
def draw_buttons(self, context, layout):
layout.prop(self, "normalize_variance")
layout.prop(self, "across_channels")
layout.prop(self, "eps")
self.draw_extra_params(context, layout)
class BatchNormNode(Node, CaffeTreeNode):
# === Basics ===
# Description string
'''Batch normalization node'''
# Optional identifier string. If not explicitly defined, the python class name is used.
bl_idname = 'BatchNormNodeType'
# Label for nice name display
bl_label = 'Batch Norm Node'
# Icon identifier
bl_icon = 'SOUND'
n_type = 'BatchNorm'
# === Custom Properties ===
use_global_stats = bpy.props.BoolProperty(default=True)
eps = bpy.props.FloatProperty(default=1e-5, soft_max=1.0, min=1e-20)
moving_average_fraction = bpy.props.FloatProperty(default=.999, soft_max=1.0, min=.5)
# === Optional Functions ===
def init(self, context):
self.inputs.new('ImageSocketType', "Input blob")
self.outputs.new('OutputSocketType', "Output blob")
# Copy function to initialize a copied node from an existing one.
def copy(self, node):
print("Copying from node ", node)
# Free function to clean up on removal.
def free(self):
print("Removing node ", self, ", Goodbye!")
# Additional buttons displayed on the node.
def draw_buttons(self, context, layout):
#TODO: Find a prototxt which shows how eps and mav are set
layout.label('''eps, mav, and use global average are all default.''')
layout.label('They will have sliders implemented when I can find')
layout.label('an example prototxt with them not default.')
# layout.prop(self, "use_global_stats")
# layout.prop(self, "moving_average_fraction")
# layout.prop(self, "eps")
class ConvNode(Node, CaffeTreeNode):
# === Basics ===
# Description string
'''A Convolution node'''
# Optional identifier string. If not explicitly defined, the python class name is used.
bl_idname = 'ConvNodeType'
# Label for nice name display
bl_label = 'Convolution Node'
# Icon identifier
bl_icon = 'SOUND'
n_type = "Convolution"
# === Custom Properties ===
num_output = bpy.props.IntProperty(name="Number of outputs", default=20, min=1, soft_max=300)
bias_term = bpy.props.BoolProperty(name='Include Bias term', default=True)
square_padding = bpy.props.BoolProperty(name="Equal x,y padding", default=True)
pad = bpy.props.IntProperty(name="Padding", default=0, min=0, soft_max=5)
pad_h = bpy.props.IntProperty(name="Padding height", default=0, min=0, soft_max=5)
pad_w = bpy.props.IntProperty(name="Padding width", default=0, min=0, soft_max=5)
square_kernel = bpy.props.BoolProperty(name="Equal x,y kernel", default=True)
kernel_size = bpy.props.IntProperty(name="Kernel size", default=5, min=1, soft_max=25)
kernel_h = bpy.props.IntProperty(name="Kernel height", default=5, min=1, soft_max=25)
kernel_w = bpy.props.IntProperty(name="Kernel width", default=5, min=1, soft_max=25)
# TODO: Maybe add group
square_stride = bpy.props.BoolProperty(name="Equal x,y stride", default=True)
stride = bpy.props.IntProperty(name="Stride", default=1, min=1, soft_max=5)
stride_h = bpy.props.IntProperty(name="Stride height", default=1, min=1, soft_max=5)
stride_w = bpy.props.IntProperty(name="Stride width", default=1, min=1, soft_max=5)
weight_filler = bpy.props.PointerProperty(type=filler_p_g)
bias_filler = bpy.props.PointerProperty(type=filler_p_g)
# === Optional Functions ===
def init(self, context):
self.inputs.new('ImageSocketType', "Input image")
self.outputs.new('OutputSocketType', "Output image")
self.color = [1, 0, 1]
self.use_custom_color = True
# Copy function to initialize a copied node from an existing one.
def copy(self, node):
print("Copying from node ", node)
# Free function to clean up on removal.
def free(self):
print("Removing node ", self, ", Goodbye!")
# Additional buttons displayed on the node.
def draw_buttons(self, context, layout):
# TODO: Finish calcsize
try:
if calcsize(self, context, axis='x') != calcsize(self, context, axis='y'):
layout.label("image x,y output is %s,%s pixels" %
(calcsize(self, context, axis='x'), calcsize(self, context, axis='y')))
else:
layout.label("image output is %s pixels" % calcsize(self, context, axis='x'))
except IndexError:
pass
layout.prop(self, "num_output")
layout.prop(self, "bias_term")
if self.square_padding:
layout.prop(self, "pad")
else:
layout.prop(self, "pad_h")
layout.prop(self, "pad_w")
if self.square_kernel:
layout.prop(self, "kernel_size")
else:
layout.prop(self, "kernel_h")
layout.prop(self, "kernel_w")
if self.square_stride:
layout.prop(self, "stride")
else:
layout.prop(self, "stride_h")
layout.prop(self, "stride_w")
layout.prop(self, "square_padding")
layout.prop(self, "square_kernel")
layout.prop(self, "square_stride")
layout.label("Weight Filler")
self.weight_filler.draw(context, layout)
layout.label("bias Filler")
self.bias_filler.draw(context, layout)
self.draw_extra_params(context, layout)
class DeConvNode(Node, CaffeTreeNode):
# === Basics ===
# Description string
'''A DeConvolution node'''
# Optional identifier string. If not explicitly defined, the python class name is used.
bl_idname = 'DeConvNodeType'
# Label for nice name display
bl_label = 'DeConvolution Node'
# Icon identifier
bl_icon = 'SOUND'
n_type = "Deconvolution"
# === Custom Properties ===
num_output = bpy.props.IntProperty(name="Number of outputs", default=20, min=1, soft_max=300)
bias_term = bpy.props.BoolProperty(name='Include Bias term', default=True)
square_padding = bpy.props.BoolProperty(name="Equal x,y padding", default=True)
pad = bpy.props.IntProperty(name="Padding", default=0, min=0, soft_max=5)
pad_h = bpy.props.IntProperty(name="Padding height", default=0, min=0, soft_max=5)
pad_w = bpy.props.IntProperty(name="Padding width", default=0, min=0, soft_max=5)
square_kernel = bpy.props.BoolProperty(name="Equal x,y kernel", default=True)
kernel_size = bpy.props.IntProperty(name="Kernel size", default=5, min=1, soft_max=25)
kernel_h = bpy.props.IntProperty(name="Kernel height", default=5, min=1, soft_max=25)
kernel_w = bpy.props.IntProperty(name="Kernel width", default=5, min=1, soft_max=25)
# TODO: Maybe add group
square_stride = bpy.props.BoolProperty(name="Equal x,y stride", default=True)
stride = bpy.props.IntProperty(name="Stride", default=1, min=1, soft_max=5)
stride_h = bpy.props.IntProperty(name="Stride height", default=1, min=1, soft_max=5)
stride_w = bpy.props.IntProperty(name="Stride width", default=1, min=1, soft_max=5)
weight_filler = bpy.props.PointerProperty(type=filler_p_g)
bias_filler = bpy.props.PointerProperty(type=filler_p_g)
# === Optional Functions ===
def init(self, context):
self.inputs.new('ImageSocketType', "Input image")
self.outputs.new('OutputSocketType', "Output image")
self.color = [1, 0, 1]
self.use_custom_color = True
# Copy function to initialize a copied node from an existing one.
def copy(self, node):
print("Copying from node ", node)
# Free function to clean up on removal.
def free(self):
print("Removing node ", self, ", Goodbye!")
# Additional buttons displayed on the node.
def draw_buttons(self, context, layout):
# TODO: Finish calcsize
try:
if calcsize(self, context, axis='x') != calcsize(self, context, axis='y'):
layout.label("image x,y output is %s,%s pixels" %
(calcsize(self, context, axis='x'), calcsize(self, context, axis='y')))
else:
layout.label("image output is %s pixels" % calcsize(self, context, axis='x'))
except IndexError:
pass
layout.prop(self, "num_output")
layout.prop(self, "bias_term")
layout.prop(self, "square_padding")
if self.square_padding:
layout.prop(self, "pad")
else:
layout.prop(self, "pad_h")
layout.prop(self, "pad_w")
layout.prop(self, "square_kernel")
if self.square_kernel:
layout.prop(self, "kernel_size")
else:
layout.prop(self, "kernel_h")
layout.prop(self, "kernel_w")
layout.prop(self, "square_stride")
if self.square_stride:
layout.prop(self, "stride")
else:
layout.prop(self, "stride_h")
layout.prop(self, "stride_w")
layout.label("Weight Filler")
self.weight_filler.draw(context, layout)
layout.label("bias Filler")
self.bias_filler.draw(context, layout)
self.draw_extra_params(context, layout)
class FCNode(Node, CaffeTreeNode):
# === Basics ===
# Description string
'''An inner product node'''
# Optional identifier string. If not explicitly defined, the python class name is used.
bl_idname = 'FCNodeType'
# Label for nice name display
bl_label = 'Fully connected Node'
# Icon identifier
bl_icon = 'SOUND'
n_type = 'InnerProduct'
# === Custom Properties ===
num_output = bpy.props.IntProperty(name="Number of outputs", default=10, min=1)
bias_term = bpy.props.BoolProperty(name='Include Bias term', default=True)
weight_filler = bpy.props.PointerProperty(type=filler_p_g)
bias_filler = bpy.props.PointerProperty(type=filler_p_g)
specax = bpy.props.BoolProperty(name="Specify Axis", default=0)
axis = bpy.props.IntProperty(name="Starting axis", default=1)
# === Optional Functions ===
def init(self, context):
self.inputs.new('ImageSocketType', "Input image")
self.outputs.new('OutputSocketType', "Output Activations")
self.color = [1, 0, 0]
self.use_custom_color = True
# Copy function to initialize a copied node from an existing one.
def copy(self, node):
print("Copying from node ", node)