def set_up(self, bottom, bottom_diff, params=None):
     self.bottom = bottom
     self.bottom_diff = bottom_diff
     self.top = hmarray.zeros(bottom.shape)
     self.mask = hmarray.random(bottom.shape)
     self.top_diff = hmarray.zeros(bottom.shape)
     return [(self.top, self.top_diff)]
Пример #2
0
 def set_up(self, bottom, bottom_diff):
     self.bottom = bottom
     self.bottom_diff = bottom_diff
     num, channels, height, width = bottom.shape
     pooled_height = (
         (height + 2 * self.padding - self.kernel_size) // self.stride) + 1
     pooled_width = (
         (width + 2 * self.padding - self.kernel_size) // self.stride) + 1
     top_shape = num, channels, pooled_height, pooled_width
     self.mask = hmarray.zeros(top_shape)
     self.top = hmarray.zeros(top_shape)
     self.top_diff = hmarray.zeros(top_shape)
     return [(self.top, self.top_diff)]
Пример #3
0
 def set_up(self, bottom, bottom_diff):
     self.bottom = bottom
     self.bottom_diff = bottom_diff
     num, channels, height, width = bottom.shape
     pooled_height = ((height + 2 * self.padding - self.kernel_size) //
                      self.stride) + 1
     pooled_width = ((width + 2 * self.padding - self.kernel_size) //
                     self.stride) + 1
     top_shape = num, channels, pooled_height, pooled_width
     self.mask = hmarray.zeros(top_shape)
     self.top = hmarray.zeros(top_shape)
     self.top_diff = hmarray.zeros(top_shape)
     return [(self.top, self.top_diff)]
Пример #4
0
    def __init__(self, layer_param, phase, params=None):
        self.phase = phase
        self.layer_param = layer_param
        conv_param = layer_param.convolution_param
        self.num_output = conv_param.num_output
        self.kernel_size = conv_param.kernel_size
        self.stride = conv_param.stride
        self.padding = conv_param.pad
        self.conv_param = conv_param
        self.bias_diff = hmarray.zeros((self.num_output, ))
        self.bias_history = hmarray.zeros((self.num_output, ))
        if params is not None:
            self.weights = params[0].data.view(hmarray)
            self.weights.sync_ocl()
            self.bias = params[1].data.view(hmarray)
            self.bias.sync_ocl()
        else:
            self.weights = None
            self.bias = hmarray.zeros((self.num_output, ))
            if conv_param.bias_filler.value != 0:
                self.bias.fill(conv_param.bias_filler.value)
                self.bias.sync_ocl()

        @compose
        def hm_backward(bottom_diff, bottom, top_diff, weights, weights_diff,
                        bias_diff, kernel_size, padding, stride):
            bottom_diff, weights_diff, bias_diff = \
                ConvBackward(bottom, top_diff, weights,
                             kernel_size=(kernel_size, kernel_size),
                             stride=(stride, stride),
                             padding=(padding, padding))
            return bottom_diff, weights_diff, bias_diff

        self.hm_backward = hm_backward

        @compose
        def hm_forward(top, bottom, weights, bias, kernel_size, padding,
                       stride):
            top = ConvForward(bottom,
                              weights,
                              bias,
                              kernel_size=(kernel_size, kernel_size),
                              padding=(padding, padding),
                              stride=(stride, stride))
            return top

        self.hm_forward = hm_forward
Пример #5
0
    def __init__(self, layer_param, phase, params=None):
        self.phase = phase
        self.layer_param = layer_param
        conv_param = layer_param.convolution_param
        self.num_output = conv_param.num_output
        self.kernel_size = conv_param.kernel_size
        self.stride = conv_param.stride
        self.padding = conv_param.pad
        self.conv_param = conv_param
        self.bias_diff = hmarray.zeros((self.num_output,))
        self.bias_history = hmarray.zeros((self.num_output,))
        if params is not None:
            self.weights = params[0].data.view(hmarray)
            self.weights.sync_ocl()
            self.bias = params[1].data.view(hmarray)
            self.bias.sync_ocl()
        else:
            self.weights = None
            self.bias = hmarray.zeros((self.num_output,))
            if conv_param.bias_filler.value != 0:
                self.bias.fill(conv_param.bias_filler.value)
                self.bias.sync_ocl()

        @compose
        def hm_backward(bottom_diff, bottom, top_diff, weights, weights_diff,
                        bias_diff, kernel_size, padding, stride):
            bottom_diff, weights_diff, bias_diff = \
                ConvBackward(bottom, top_diff, weights,
                             kernel_size=(kernel_size, kernel_size),
                             stride=(stride, stride),
                             padding=(padding, padding))
            return bottom_diff, weights_diff, bias_diff
        self.hm_backward = hm_backward

        @compose
        def hm_forward(top, bottom, weights, bias, kernel_size, padding,
                       stride):
            top = ConvForward(bottom, weights, bias,
                              kernel_size=(kernel_size, kernel_size),
                              padding=(padding, padding),
                              stride=(stride, stride))
            return top
        self.hm_forward = hm_forward
Пример #6
0
 def __init__(self, layer_param, phase, params=None):
     self.layer_param = layer_param
     self.phase = phase
     self.num_output = layer_param.inner_product_param.num_output
     if params is not None:
         self.weights = params[0].data.view(hmarray)
         self.weights.sync_ocl()
         self.bias = params[1].data.view(hmarray)
         self.bias.sync_ocl()
     else:
         self.weights = None
         self.bias = hmarray.zeros((self.num_output, ))
    def set_up(self, *bottoms):
        self.bottoms = bottoms
        top_shape = list(self.bottoms[0].shape)
        # self.num_concats = np.prod(self.bottoms[0].shape[0:self.concat_axis])
        # concat_input_size = np.prod(bottom.shape[self.concat_axis + 1:])
        # bottom_count_sum = np.prod(self.bottoms[0].shape)
        for i in range(2, len(bottoms), 2):
            top_shape[self.concat_axis] += self.bottoms[i].shape[self.concat_axis]

        self.top = hmarray.zeros(tuple(top_shape))

        return [(self.top, None)]
Пример #8
0
    def set_up(self, *bottoms):
        self.bottoms = bottoms
        top_shape = list(self.bottoms[0].shape)
        # self.num_concats = np.prod(self.bottoms[0].shape[0:self.concat_axis])
        # concat_input_size = np.prod(bottom.shape[self.concat_axis + 1:])
        # bottom_count_sum = np.prod(self.bottoms[0].shape)
        for i in range(2, len(bottoms), 2):
            top_shape[self.concat_axis] += \
                self.bottoms[i].shape[self.concat_axis]

        self.top = hmarray.zeros(tuple(top_shape))

        return [(self.top, None)]
Пример #9
0
    def set_up(self, bottom, bottom_diff):
        self.bottom = bottom
        self.bottom_diff = bottom_diff
        num, channels, height, width = bottom.shape

        weights_shape = (self.num_output, channels * self.kernel_size *
                         self.kernel_size)
        if self.weights is not None:
            self.weights = self.weights.reshape(weights_shape)
        else:
            n = 1.0 / np.sqrt(self.num_output)
            self.weights = hmarray.random(weights_shape, _range=(-n, n))
        self.weights_diff = hmarray.zeros(weights_shape)
        self.weights_history = hmarray.zeros(weights_shape)

        height_out = (height + 2 * self.padding - self.kernel_size) // \
            self.stride + 1
        width_out = (width + 2 * self.padding - self.kernel_size) // \
            self.stride + 1
        top_shape = (num, self.num_output, height_out, width_out)
        self.top = hmarray.zeros(top_shape)
        self.top_diff = hmarray.zeros(top_shape)
        return [(self.top, self.top_diff)]
Пример #10
0
    def set_up(self, bottom, bottom_diff):
        self.bottom = bottom
        self.bottom_diff = bottom_diff
        num, channels, height, width = bottom.shape

        weights_shape = (self.num_output,
                         channels * self.kernel_size * self.kernel_size)
        if self.weights is not None:
            self.weights = self.weights.reshape(weights_shape)
        else:
            n = 1.0 / np.sqrt(self.num_output)
            self.weights = hmarray.random(weights_shape, _range=(-n, n))
        self.weights_diff = hmarray.zeros(weights_shape)
        self.weights_history = hmarray.zeros(weights_shape)

        height_out = (height + 2 * self.padding - self.kernel_size) // \
            self.stride + 1
        width_out = (width + 2 * self.padding - self.kernel_size) // \
            self.stride + 1
        top_shape = (num, self.num_output, height_out, width_out)
        self.top = hmarray.zeros(top_shape)
        self.top_diff = hmarray.zeros(top_shape)
        return [(self.top, self.top_diff)]
Пример #11
0
    def __init__(self, net_param, params=None):
        self.blobs = {}
        self.layers = []

        self.net_param = net_param

        if self.net_param.state.phase == pb.TEST:
            if len(self.net_param.input_dim) > 0:
                self.blobs['data'] = hmarray.zeros(self.net_param.input_dim)
                self.blobs['data_diff'] = None
        if len(self.net_param.layer) > 0:
            layer_params = self.net_param.layer
        else:
            layer_params = self.net_param.layers
        # Initialize layers
        for layer_param in layer_params:
            if len(layer_param.include) > 0 and \
                    layer_param.include[0].phase != net_param.state.phase:
                continue
            log.info("Initializing layer %s", layer_param.name)
            log.debug("  Bottom : %s", ", ".join(layer_param.bottom))
            log.debug("  Top    : %s", ", ".join(layer_param.top))
            # Skip dropout layers for test
            if layer_param.type in (pb.V1LayerParameter.DROPOUT, "Dropout") \
                    and net_param.state.phase == pb.TEST:
                continue
            layer_constructor = self.layer_map[layer_param.type]
            if params is not None and layer_param.name in params:
                layer = layer_constructor(layer_param, net_param.state.phase,
                                          params[layer_param.name])
            else:
                layer = layer_constructor(layer_param, net_param.state.phase)
            self.layers.append(layer)
            bottom = []
            for blob in layer_param.bottom:
                bottom.append(self.blobs[blob])
                bottom.append(self.blobs["{}_diff".format(blob)])
            tops = layer.set_up(*bottom)
            for top, top_name in zip(tops, layer_param.top):
                self.blobs[top_name] = top[0]
                self.blobs["{}_diff".format(top_name)] = top[1]
Пример #12
0
def reference_col2im(data_col, kernel_size, stride, padding, shape):
    channels, height, width = shape
    kernel_h, kernel_w = kernel_size
    pad_h, pad_w = padding
    stride_h, stride_w = stride
    height_col = (height + 2 * pad_h - kernel_h) // stride_h + 1
    width_col = (width + 2 * pad_w - kernel_w) // stride_w + 1
    channels_col = channels * kernel_h * kernel_w
    data = hmarray.zeros(shape)
    for c in range(channels_col):
        w_offset = c % kernel_w
        h_offset = (c / kernel_w) % kernel_h
        c_im = c / kernel_h / kernel_w
        for h in range(height_col):
            for w in range(width_col):
                h_pad = h * stride_h - pad_h + h_offset
                w_pad = w * stride_w - pad_w + w_offset
                if (h_pad >= 0 and h_pad < height and w_pad >= 0
                        and w_pad < width):
                    data[c_im, h_pad, w_pad] += data_col[c, h * width_col + w]
    return data
Пример #13
0
def reference_col2im(data_col, kernel_size, stride, padding, shape):
    channels, height, width = shape
    kernel_h, kernel_w = kernel_size
    pad_h, pad_w = padding
    stride_h, stride_w = stride
    height_col = (height + 2 * pad_h - kernel_h) // stride_h + 1
    width_col = (width + 2 * pad_w - kernel_w) // stride_w + 1
    channels_col = channels * kernel_h * kernel_w
    data = hmarray.zeros(shape)
    for c in range(channels_col):
        w_offset = c % kernel_w
        h_offset = (c / kernel_w) % kernel_h
        c_im = c / kernel_h / kernel_w
        for h in range(height_col):
            for w in range(width_col):
                h_pad = h * stride_h - pad_h + h_offset
                w_pad = w * stride_w - pad_w + w_offset
                if (h_pad >= 0 and h_pad < height and w_pad >= 0 and w_pad <
                        width):
                    data[c_im, h_pad, w_pad] += data_col[c, h * width_col + w]
    return data
Пример #14
0
    def __init__(self, net_param, params=None):
        self.blobs = {}
        self.layers = []

        self.net_param = net_param

        if self.net_param.state.phase == pb.TEST:
            if len(self.net_param.input_dim) > 0:
                self.blobs["data"] = hmarray.zeros(self.net_param.input_dim)
                self.blobs["data_diff"] = None
        if len(self.net_param.layer) > 0:
            layer_params = self.net_param.layer
        else:
            layer_params = self.net_param.layers
        # Initialize layers
        for layer_param in layer_params:
            if len(layer_param.include) > 0 and layer_param.include[0].phase != net_param.state.phase:
                continue
            log.info("Initializing layer %s", layer_param.name)
            log.debug("  Bottom : %s", ", ".join(layer_param.bottom))
            log.debug("  Top    : %s", ", ".join(layer_param.top))
            # Skip dropout layers for test
            if layer_param.type in (pb.V1LayerParameter.DROPOUT, "Dropout") and net_param.state.phase == pb.TEST:
                continue
            layer_constructor = self.layer_map[layer_param.type]
            if params is not None and layer_param.name in params:
                layer = layer_constructor(layer_param, net_param.state.phase, params[layer_param.name])
            else:
                layer = layer_constructor(layer_param, net_param.state.phase)
            self.layers.append(layer)
            bottom = []
            for blob in layer_param.bottom:
                bottom.append(self.blobs[blob])
                bottom.append(self.blobs["{}_diff".format(blob)])
            tops = layer.set_up(*bottom)
            for top, top_name in zip(tops, layer_param.top):
                self.blobs[top_name] = top[0]
                self.blobs["{}_diff".format(top_name)] = top[1]
Пример #15
0
 def set_up(self, bottom, bottom_diff):
     self.bottom, self.bottom_diff = bottom, bottom_diff
     N = self.num_output
     K = np.prod(bottom.shape[1:])
     scale = 1.0 / np.sqrt(self.num_output)
     if self.weights is None:
         self.weights = hmarray.random((N, K), _range=(-scale, scale))
     self.weights_diff = hmarray.zeros((N, K))
     self.weights_history = hmarray.zeros((N, K))
     self.bias_diff = hmarray.zeros((self.num_output, ))
     self.bias_history = hmarray.zeros((self.num_output, ))
     self.bias_multiplier = hmarray((1, self.bottom.shape[0]))
     self.bias_multiplier.fill(1)
     self.bias_multiplier.sync_ocl()
     top_shape = (bottom.shape[0], N)
     self.top = hmarray.zeros(top_shape)
     self.top_diff = hmarray.zeros(top_shape)
     return [(self.top, self.top_diff)]
from hindemith.operations.lrn import LrnForward
from hindemith.operations.softmax import SoftmaxForward
from hindemith.core import compose
import caffe
import numpy as np
import time

prototxt = "models/alexnet-ng/deploy.prototxt"
caffemodel = "models/alexnet-ng/alexnet-ng.caffemodel"

caffe.set_mode_cpu()
caffe_net = caffe.Net(prototxt, caffemodel, caffe.TEST)

conv1_filters = caffe_net.params['conv1'][0].data.view(hmarray)
conv1_bias = caffe_net.params['conv1'][1].data.view(hmarray)
conv1 = hmarray.zeros(caffe_net.blobs['conv1'].data.shape)

norm1 = hmarray.zeros(caffe_net.blobs['norm1'].data.shape)
norm1_scale = hmarray.zeros(norm1.shape)

pool1 = hmarray.zeros(caffe_net.blobs['pool1'].data.shape)
pool1_mask = hmarray.zeros(pool1.shape)

conv2_filters = caffe_net.params['conv2'][0].data.view(hmarray)
conv2_bias = caffe_net.params['conv2'][1].data.view(hmarray)
conv2 = hmarray.zeros(caffe_net.blobs['conv2'].data.shape)

norm2 = hmarray.zeros(caffe_net.blobs['norm2'].data.shape)
norm2_scale = hmarray.zeros(norm2.shape)

pool2 = hmarray.zeros(caffe_net.blobs['pool2'].data.shape)
Пример #17
0
import pycl as cl
import caffe
import numpy as np
import time

prototxt = "benchmarks/googlenet.prototxt"
caffemodel = "models/googlenet/bvlc_googlenet.caffemodel"

caffe.set_mode_gpu()
caffe.set_device(1)
caffe_net = caffe.Net(prototxt, caffemodel, caffe.TEST)


conv1_7x7_s2_filters = caffe_net.params['conv1/7x7_s2'][0].data.view(hmarray)
conv1_7x7_s2_bias = caffe_net.params['conv1/7x7_s2'][1].data.view(hmarray)
conv1_7x7_s2 = hmarray.zeros(caffe_net.blobs['conv1/7x7_s2'].data.shape)

pool1_3x3_s2 = hmarray.zeros(caffe_net.blobs['pool1/3x3_s2'].data.shape)
pool1_3x3_s2_mask = hmarray.zeros(pool1_3x3_s2.shape)

pool1_norm1 = hmarray.zeros(caffe_net.blobs['pool1/norm1'].data.shape)
pool1_norm1_scale = hmarray.zeros(pool1_norm1.shape)

conv2_3x3_reduce_filters = caffe_net.params['conv2/3x3_reduce'][0].data.view(hmarray)
conv2_3x3_reduce_bias = caffe_net.params['conv2/3x3_reduce'][1].data.view(hmarray)
conv2_3x3_reduce = hmarray.zeros(caffe_net.blobs['conv2/3x3_reduce'].data.shape)

conv2_3x3_filters = caffe_net.params['conv2/3x3'][0].data.view(hmarray)
conv2_3x3_bias = caffe_net.params['conv2/3x3'][1].data.view(hmarray)
conv2_3x3 = hmarray.zeros(caffe_net.blobs['conv2/3x3'].data.shape)
 def set_up(self, bottom, bottom_diff):
     self.bottom = bottom
     self.top = hmarray.zeros(bottom.shape)
     return [(self.top, None)]
from hindemith.operations.lrn import LrnForward
from hindemith.operations.softmax import SoftmaxForward
from hindemith.core import compose
import caffe
import numpy as np
import time

prototxt = "models/alexnet-ng/deploy.prototxt"
caffemodel = "models/alexnet-ng/alexnet-ng.caffemodel"

caffe.set_mode_cpu()
caffe_net = caffe.Net(prototxt, caffemodel, caffe.TEST)

conv1_filters = caffe_net.params['conv1'][0].data.view(hmarray)
conv1_bias = caffe_net.params['conv1'][1].data.view(hmarray)
conv1 = hmarray.zeros(caffe_net.blobs['conv1'].data.shape)

norm1 = hmarray.zeros(caffe_net.blobs['norm1'].data.shape)
norm1_scale = hmarray.zeros(norm1.shape)

pool1 = hmarray.zeros(caffe_net.blobs['pool1'].data.shape)
pool1_mask = hmarray.zeros(pool1.shape)

conv2_filters = caffe_net.params['conv2'][0].data.view(hmarray)
conv2_bias = caffe_net.params['conv2'][1].data.view(hmarray)
conv2 = hmarray.zeros(caffe_net.blobs['conv2'].data.shape)

norm2 = hmarray.zeros(caffe_net.blobs['norm2'].data.shape)
norm2_scale = hmarray.zeros(norm2.shape)

pool2 = hmarray.zeros(caffe_net.blobs['pool2'].data.shape)
 def set_up(self, bottom, bottom_diff, label, label_diff):
     self.bottom, self.bottom_diff, self.label = bottom, bottom_diff, label
     self.top = hmarray.zeros((1, ))
     self.prob = hmarray.zeros(bottom.shape)
     return [(self.top, None)]
Пример #21
0
 def set_up(self, bottom, bottom_diff, label, _):
     self.bottom = bottom
     self.bottom_diff = bottom_diff
     self.label = label
     self.top = hmarray.zeros((1,))
     return [(self.top, None)]
Пример #22
0
 def set_up(self, bottom, bottom_diff):
     self.bottom, self.bottom_diff = bottom, bottom_diff
     self.top = hmarray.zeros(bottom.shape)
     self.scale = hmarray.zeros(bottom.shape)
     self.top_diff = hmarray.zeros(bottom.shape)
     return [(self.top, self.top_diff)]
Пример #23
0
 def set_up(self, bottom, bottom_diff, label, _):
     self.bottom = bottom
     self.bottom_diff = bottom_diff
     self.label = label
     self.top = hmarray.zeros((1, ))
     return [(self.top, None)]
from hindemith.operations.softmax import SoftmaxForward
from hindemith.core import compose
from hindemith.clibs.clblas import sgemm
import caffe
import numpy as np


prototxt = "models/alexnet-ng/deploy.prototxt"
caffemodel = "models/alexnet-ng/alexnet-ng.caffemodel"

# caffe.set_mode_gpu()
caffe_net = caffe.Net(prototxt, caffemodel, caffe.TEST)

conv1_filters = caffe_net.params["conv1"][0].data.view(hmarray)
conv1_bias = caffe_net.params["conv1"][1].data.view(hmarray)
conv1 = hmarray.zeros(caffe_net.blobs["conv1"].data.shape)

norm1 = hmarray.zeros(caffe_net.blobs["norm1"].data.shape)
norm1_scale = hmarray.zeros(norm1.shape)

pool1 = hmarray.zeros(caffe_net.blobs["pool1"].data.shape)
pool1_mask = hmarray.zeros(pool1.shape)

conv2_filters = caffe_net.params["conv2"][0].data.view(hmarray)
conv2_bias = caffe_net.params["conv2"][1].data.view(hmarray)
conv2 = hmarray.zeros(caffe_net.blobs["conv2"].data.shape)

norm2 = hmarray.zeros(caffe_net.blobs["norm2"].data.shape)
norm2_scale = hmarray.zeros(norm1.shape)

pool2 = hmarray.zeros(caffe_net.blobs["pool2"].data.shape)
Пример #25
0
 def set_up(self, bottom, bottom_diff):
     self.bottom, self.bottom_diff = bottom, bottom_diff
     self.top = hmarray.zeros(bottom.shape)
     self.scale = hmarray.zeros(bottom.shape)
     self.top_diff = hmarray.zeros(bottom.shape)
     return [(self.top, self.top_diff)]