Skip to content

Commit

Permalink
utils: DAG import script bugfixes
Browse files Browse the repository at this point in the history
  • Loading branch information
vedaldi committed Oct 12, 2015
1 parent a73c76f commit 9308c75
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 26 deletions.
2 changes: 1 addition & 1 deletion utils/import-caffe-dag.py
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,7 @@ def escape(name):
outputs = ['prob']

cmodel.addLayer(CaffeSoftMax(layerName,
cmodel.layers[name].inputs[0:1],
cmodel.layers[name].outputs[0:1],
outputs))

cmodel.display()
Expand Down
46 changes: 21 additions & 25 deletions utils/layers.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ def __init__(self, name, inputs, outputs, kernelSize, hasBias, numFilters, numFi
self.filterDimension = None
self.stride = stride
self.pad = pad
self.display()

def display(self):
super(CaffeConv, self).display()
Expand All @@ -277,13 +278,13 @@ def display(self):
print " Filter Dimension", self.filterDimension

def reshape(self, model):
if len(model.vars[self.inputs[0]].size) == 0: return
model.vars[self.outputs[0]].size = \
getFilterOutputSize(model.vars[self.inputs[0]].size[0:2],
self.kernelSize, self.stride, self.pad) + \
[self.numFilters, model.vars[self.inputs[0]].size[3]]
self.filterDimension = \
model.vars[self.inputs[0]].size[2] / self.numFilterGroups
varin = model.vars[self.inputs[0]]
varout = model.vars[self.outputs[0]]
if len(varin.size) == 0: return
varout.size = getFilterOutputSize(varin.size[0:2], \
self.kernelSize, self.stride, self.pad) + \
[self.numFilters, varin.size[3]]
self.filterDimension = varin.size[2] / self.numFilterGroups

def getTransforms(self, model):
return [[getFilterTransform(self.kernelSize, self.stride, self.pad)]]
Expand All @@ -292,7 +293,6 @@ def transpose(self, model):
self.kernelSize = reorder(self.kernelSize, [1,0])
self.stride = reorder(self.stride, [1,0])
self.pad = reorder(self.pad, [2,3,0,1])
print model.params[self.params[0]].value
if model.params[self.params[0]].value.size > 0:
print "Layer %s transposing filters" % self.name
param = model.params[self.params[0]]
Expand Down Expand Up @@ -333,16 +333,11 @@ def reshape(self, model):
if len(model.vars[self.inputs[0]].size) == 0: return
s = model.vars[self.inputs[0]].size
self.kernelSize = [s[0], s[1], s[2], self.numFilters]
print "Layer %s: inner product converted to filter bank of shape", self.kernelSize
if model.params[self.params[0]].value.size > 0:
print "Layer %s: reshaping inner product paramters into filter bank" % self.name
param = model.params[self.params[0]]
param.value = param.value.reshape(
size[0],
size[1],
size[2],
opts.num_output,
order='F')
print "Layer %s: inner product converted to filter bank of shape %s" % (self.name, self.kernelSize)
param = model.params[self.params[0]]
if param.value.size > 0:
print "Layer %s: reshaping inner product paramters of shape %s into a filter bank" % (self.name, param.value.shape)
param.value = param.value.reshape(self.kernelSize, order='F')
super(CaffeInnerProduct, self).reshape(model)

# --------------------------------------------------------------------
Expand Down Expand Up @@ -421,7 +416,7 @@ def reshape(self, model):
stride = self.stride
# MatConvNet uses a slighly different definition of padding, which we think
# is the correct one (it corresponds to the filters)
self.padCorrected = copy.deepcopy(self.pad)
self.padCorrected = copy.deepcopy(self.pad)
for i in [0, 1]:
self.padCorrected[1 + i*2] = min(
self.pad[1 + i*2] + self.stride[i] - 1,
Expand Down Expand Up @@ -463,18 +458,19 @@ def transpose(self, model):
def toMatlab(self):
mlayer = super(CaffeConcat, self).toMatlab()
mlayer['type'][0] = u'dagnn.Concat'
mlayer['block'][0] = dictToMatlabStruct({'dim': float(self.concatDim)+1})
mlayer['block'][0] = dictToMatlabStruct({'dim': float(self.concatDim) + 1})
return mlayer

def reshape(self, model):
sizes = [model.vars[x].size for x in self.inputs]
osize = sizes[0]
for s in sizes:
for i in range(len(s)):
osize = copy.deepcopy(sizes[0])
osize[self.concatDim] = 0
for thisSize in sizes:
for i in range(len(thisSize)):
if self.concatDim == i:
osize[i] = osize[i] + s[i]
osize[i] = osize[i] + thisSize[i]
else:
if osize[i] != s[i]:
if osize[i] != thisSize[i]:
print "Warning: concat layer: inconsistent input dimensions", sizes
model.vars[self.outputs[0]].size = osize

Expand Down

0 comments on commit 9308c75

Please sign in to comment.