[45a3e1]: / darkflow / dark / connected.py

Download this file

113 lines (97 with data), 3.2 kB

  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
from .layer import Layer
import numpy as np
class extract_layer(Layer):
def setup(self, old_inp, old_out,
activation, inp, out):
if inp is None: inp = range(old_inp)
self.activation = activation
self.old_inp = old_inp
self.old_out = old_out
self.inp = inp
self.out = out
self.wshape = {
'biases': [len(self.out)],
'weights': [len(self.inp), len(self.out)]
}
@property
def signature(self):
sig = ['connected']
sig += self._signature[1:-2]
return sig
def present(self):
args = self.signature
self.presenter = connected_layer(*args)
def recollect(self, val):
w = val['weights']
b = val['biases']
if w is None: self.w = val; return
w = np.take(w, self.inp, 0)
w = np.take(w, self.out, 1)
b = np.take(b, self.out)
assert1 = w.shape == tuple(self.wshape['weights'])
assert2 = b.shape == tuple(self.wshape['biases'])
assert assert1 and assert2, \
'Dimension does not match in {} recollect'.format(
self._signature)
self.w['weights'] = w
self.w['biases'] = b
class select_layer(Layer):
def setup(self, inp, old,
activation, inp_idx,
out, keep, train):
self.old = old
self.keep = keep
self.train = train
self.inp_idx = inp_idx
self.activation = activation
inp_dim = inp
if inp_idx is not None:
inp_dim = len(inp_idx)
self.inp = inp_dim
self.out = out
self.wshape = {
'biases': [out],
'weights': [inp_dim, out]
}
@property
def signature(self):
sig = ['connected']
sig += self._signature[1:-4]
return sig
def present(self):
args = self.signature
self.presenter = connected_layer(*args)
def recollect(self, val):
w = val['weights']
b = val['biases']
if w is None: self.w = val; return
if self.inp_idx is not None:
w = np.take(w, self.inp_idx, 0)
keep_b = np.take(b, self.keep)
keep_w = np.take(w, self.keep, 1)
train_b = b[self.train:]
train_w = w[:, self.train:]
self.w['biases'] = np.concatenate(
(keep_b, train_b), axis=0)
self.w['weights'] = np.concatenate(
(keep_w, train_w), axis=1)
class connected_layer(Layer):
def setup(self, input_size,
output_size, activation):
self.activation = activation
self.inp = input_size
self.out = output_size
self.wshape = {
'biases': [self.out],
'weights': [self.inp, self.out]
}
def finalize(self, transpose):
weights = self.w['weights']
if weights is None: return
shp = self.wshape['weights']
if not transpose:
weights = weights.reshape(shp[::-1])
weights = weights.transpose([1, 0])
else:
weights = weights.reshape(shp)
self.w['weights'] = weights