-
Notifications
You must be signed in to change notification settings - Fork 3
/
mainA.py
103 lines (80 loc) · 4.14 KB
/
mainA.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
# ------------------------------------------------------------------
#
# Note1: This is highly un-optimized. It may take several minutes to
# process a small image. We split the detection of Figure 5
# into multiple parts for this reason, however, this has no
# effect on the algorithm itself.
#
# Note2: Due to size constraints, we had to omit the trained models
# for the primitive capsules. Running this file will automatically
# re-train them, however, this might take over 4 hours (as tested
# on a Laptop with a nVidia GTX 1060)
from TestPrimitives import TestPrimitives
from TestPrimitives import TestRenderer
from CapsuleNetwork import CapsuleNetwork
from Observation import Observation
from AttributePool import AttributePool
from VividNet import VividNet
from Capsule import Capsule
from GraphicsUserInterface import GraphicsUserInterface
from numba import cuda, float32, int32
from Utility import Utility
import numpy as np
import random
import math
# We restrict our GPU to only use X %. This has no specific
# reason, just to allow us to work in the background without
# CUDA running out of Memory
import tensorflow as tf
from keras.backend.tensorflow_backend import set_session
config = tf.ConfigProto()
config.gpu_options.per_process_gpu_memory_fraction = 0.5
set_session(tf.Session(config=config))
if __name__ == '__main__':
testUI = GraphicsUserInterface()
retrain = 0 # After initial Training (2 loops), set this to 0 to avoid
# retraining each run
vividNet = VividNet("vividnetA")
primCaps = vividNet.setRenderer(TestRenderer, TestPrimitives, retrain)
semCaps = vividNet.loadSemantic()
# UI Button Functions for Meta-Learning
def addNewSemanticCapsule(nameCaps : str, observationList : list):
print("Training new Capsule '" + nameCaps + "' from:")
for obs in observationList:
obs.printOutputs(True)
resCaps = vividNet._capsuleNetwork.addSemanticCapsule(nameCaps, observationList)
vividNet.saveSemantic()
def trainExistingSemanticCapsule(nameCaps : str, observationList : list):
print("Training existing Capsule '" + nameCaps + "' from:")
for obs in observationList:
obs.printOutputs(True)
resCaps = vividNet._capsuleNetwork.addSemanticTraining(nameCaps, observationList)
vividNet.saveSemantic()
def addNewAttribute(nameCaps : str, nameAttr : str, observationList : list):
print("Training new Attribute '" + nameAttr + "' for '" + nameCaps + "' from:")
for obs in observationList:
obs.printOutputs(True)
resCaps = vividNet._capsuleNetwork.addAttribute(nameCaps, nameAttr, observationList)
vividNet.saveSemantic()
def trainExistingAttribute(nameCaps : str, nameAttr : str, observationList : list):
print("Training existing Attribute '" + nameAttr + "' for '" + nameCaps + "' from:")
for obs in observationList:
obs.printOutputs(True)
resCaps = vividNet._capsuleNetwork.addAttributeTraining(nameCaps, nameAttr, observationList)
vividNet.saveSemantic()
for i in range(3):
# Load the test image
imageReal, width, height = Utility.loadImage("Examples/Ascene" + str(i) + ".png")
# Feed-Forward Pass through the Network
allObs, recommendation = vividNet._capsuleNetwork.showInput(imageReal, width, height, 1)
# Draw from the detected data
imageObserved, semantics, texts = vividNet._capsuleNetwork.generateImage(width, height, allObs, False)
# Print all Observations
for capsule in allObs.keys():
print(str(len(allObs[capsule])) + "x " + capsule.getName())
for index, obs in enumerate(allObs[capsule]):
print("Observation " + str(index))
obs.printOutputs(False)
testUI.draw(imageReal, imageObserved, width, height, width, height, semantics, texts,
addNewSemanticCapsule, trainExistingSemanticCapsule, addNewAttribute, trainExistingAttribute,
False, recommendation)