def __compute_average_pooling_nd(data, size, step, dimension: int): for i in range(dimension): if data.shape[i] < size[i]: raise ValueError('Data shape smaller than size: {} {}'.format(data.shape, size)) pooling_array = [] pooling_grid = [range(0, data.shape[i] - size[i] + 1, step[i]) for i in range(dimension)] for index in multi_range(pooling_grid): pooling_array.append(numpy.mean(data[[slice(index[i], index[i] + size[i]) for i in range(dimension)]])) return numpy.array(pooling_array).reshape([len(g) for g in pooling_grid])
def __compute_average_unpooling_nd(pooling, size, step, dimension: int, unpooling_size=None): if unpooling_size is None: unpooling_array = numpy.zeros([size[i] + (pooling.shape[i] - 1) * step[i] for i in range(dimension)]) else: unpooling_array = numpy.zeros(unpooling_size) unpooling_grid = [range(0, unpooling_array.shape[i] - size[i] + 1, step[i]) for i in range(dimension)] for n, index in enumerate(multi_range(unpooling_grid)): sub_slice = [slice(index[i], index[i] + size[i]) for i in range(dimension)] unpooling_array[sub_slice] += pooling[numpy.unravel_index(n, pooling.shape)] return unpooling_array
def __compute_max_unpooling_nd(data, pooling, size, step, dimension: int): for i in range(dimension): if data.shape[i] < size[i]: raise ValueError('Data shape smaller than size: {} {}'.format(data.shape, size)) unpooling_array = numpy.zeros(data.shape) unpooling_grid = [range(0, data.shape[i] - size[i] + 1, step[i]) for i in range(dimension)] for n, index in enumerate(multi_range(unpooling_grid)): sub_slice = [slice(index[i], index[i] + size[i]) for i in range(dimension)] max_index = numpy.argmax(data[sub_slice]) sub_unpooling_array = unpooling_array[sub_slice] sub_unpooling_array[numpy.unravel_index(max_index, sub_unpooling_array.shape)] = pooling[numpy.unravel_index(n, pooling.shape)] return unpooling_array
def __compute_max_pooling_nd(data, size, step, dimension: int, reference=None): for i in range(dimension): if data.shape[i] < size[i]: raise ValueError('Data shape smaller than size: {} {}'.format(data.shape, size)) pooling_array = [] pooling_grid = [range(0, data.shape[i] - size[i] + 1, step[i]) for i in range(dimension)] for index in multi_range(pooling_grid): sub_slice = [slice(index[i], index[i] + size[i]) for i in range(dimension)] if reference is None: pooling_array.append(numpy.max(data[sub_slice])) else: max_index = numpy.argmax(reference[sub_slice]) sub_data = data[sub_slice] pooling_array.append(sub_data[numpy.unravel_index(max_index, sub_data.shape)]) return numpy.array(pooling_array).reshape([len(g) for g in pooling_grid])