Esempio n. 1
0
 def parse_pooling(self, layer, attributes):
     attributes.update({
         'input_shape': layer.input_shape,
         'pool_size': layer.pool_size,
         'strides': layer.stride,
         'padding': padding_string(layer.pad, layer.pool_size)
     })
Esempio n. 2
0
 def parse_convolution(self, layer, attributes):
     idx_offset = 0  # Normally: params = [w, b]
     # With the low-precision version of caffe (ADAPTIoN), there is a second
     # copy of quantized parameters available.
     if hasattr(layer, 'lpfp_param'):
         attributes['Qm.f'] = (layer.lpfp_param.bd, layer.lpfp_param.ad)
         attributes['quantize_bias'] = layer.lpfp_param.round_bias
         if attributes['Qm.f'] != (0, 0):
             idx_offset = 1  # params = [w, w_lp, b, b_lp]
     weights = self.input_model[0].params[layer.name][0].data
     bias = self.input_model[0].params[layer.name][1 + idx_offset].data
     weights = weights[:, :, ::-1, ::-1]
     weights = np.transpose(weights, (2, 3, 1, 0))
     print("Flipped kernels.")
     attributes['parameters'] = [weights, bias]
     p = layer.convolution_param
     # Take maximum here because sometimes not not all fields are set
     # (e.g. kernel_h == 0 even though kernel_size == [3])
     filter_size = [
         max(p.kernel_w, p.kernel_size[0]),
         max(p.kernel_h, p.kernel_size[-1])
     ]
     pad = p.pad[0] if len(p.pad) > 0 else 0
     pad = (max(p.pad_w, pad), max(p.pad_h, pad))
     padding = padding_string(pad, filter_size)
     strides = [max(p.stride_w, p.stride[0]), max(p.stride_h, p.stride[0])]
     attributes.update({
         'filters': p.num_output,
         'kernel_size': filter_size,
         'padding': padding,
         'strides': strides
     })
Esempio n. 3
0
 def parse_convolution(self, layer, attributes):
     weights = k.get_value(layer.W)
     weights = np.transpose(weights, (2, 3, 1, 0))
     bias = k.get_value(layer.b)
     if bias is None:
         bias = np.zeros(layer.num_filters)
     attributes['parameters'] = [weights, bias]
     padding = padding_string(layer.pad, layer.filter_size)
     attributes.update({
         'input_shape': layer.input_shape,
         'filters': layer.num_filters,
         'kernel_size': layer.filter_size,
         'padding': padding,
         'strides': layer.stride
     })
Esempio n. 4
0
 def parse_pooling(self, layer, attributes):
     p = layer.pooling_param
     # Take maximum here because sometimes not not all fields are set
     # (e.g. kernel_h == 0 even though kernel_size == 2)
     pool_size = [
         max(p.kernel_w, p.kernel_size),
         max(p.kernel_h, p.kernel_size)
     ]
     pad = (max(p.pad_w, p.pad), max(p.pad_h, p.pad))
     padding = padding_string(pad, pool_size)
     strides = [max(p.stride_w, p.stride), max(p.stride_h, p.stride)]
     attributes.update({
         'pool_size': pool_size,
         'padding': padding,
         'strides': strides
     })
Esempio n. 5
0
 def parse_convolution(self, layer, attributes):
     weights = layer.W.get_value()
     weights = np.transpose(weights, (2, 3, 1, 0))
     if keras.backend.backend() != 'theano':  # Assumes lasagne uses theano.
         weights = keras.utils.conv_utils.convert_kernel(weights)
     bias = layer.b.get_value()
     if bias is None:
         bias = np.zeros(layer.num_filters)
     attributes['parameters'] = [weights, bias]
     padding = padding_string(layer.pad, layer.filter_size)
     attributes.update({
         'input_shape': layer.input_shape,
         'filters': layer.num_filters,
         'kernel_size': layer.filter_size,
         'padding': padding,
         'strides': layer.stride
     })