text:
import numpy as np
import tensorflow as tf

def atan2(y, x):
    angle = tf.where(tf.greater(x,0.0), tf.atan(y/x), tf.zeros_like(x))
    angle = tf.where(tf.logical_and(tf.less(x,0.0), tf.greater_equal(y,0.0)),
                      tf.atan(y/x) + np.pi, angle)
    angle = tf.where(tf.logical_and(tf.less(x,0.0), tf.less(y,0.0)),
                      tf.atan(y/x) - np.pi, angle)
    angle = tf.where(tf.logical_and(tf.equal(x,0.0), tf.greater(y,0.0)),
                      np.pi * tf.ones_like(x), angle)
    angle = tf.where(tf.logical_and(tf.equal(x,0.0), tf.less(y,0.0)),
                      -np.pi * tf.ones_like(x), angle)
    angle = tf.where(tf.logical_and(tf.equal(x,0.0),tf.equal(y,0.0)),
                      np.nan * tf.zeros_like(x), angle)
    return angle

def flow_to_color(flow, mask=None, max_flow=None):
    """Converts flow to 3-channel color image.

    Args:
        flow: tensor of shape [num_batch, height, width, 2].
        mask: flow validity mask of shape [num_batch, height, width, 1].
    """
    n = 8
    num_batch, height, width, _ = tf.unstack(tf.shape(flow))
    mask = tf.ones([num_batch, height, width, 1]) if mask is None else mask
    flow_u, flow_v = tf.unstack(flow, axis=3)
    if max_flow is not None:
        max_flow = tf.maximum(max_flow, 1)
    else:
        max_flow = tf.reduce_max(tf.abs(flow * mask))
    mag = tf.sqrt(tf.reduce_sum(tf.square(flow), 3))
    angle = atan2(flow_v, flow_u)

    im_h = tf.mod(angle / (2 * np.pi) + 1.0, 1.0)
    im_s = tf.clip_by_value(mag * n / max_flow, 0, 1)
    im_v = tf.clip_by_value(n - im_s, 0, 1)
    im_hsv = tf.stack([im_h, im_s, im_v], 3)
    im = tf.image.hsv_to_rgb(im_hsv)
    return im * mask

def flow_error_image(flow_1, flow_2, mask_occ, mask_noc=None, log_colors=True):
    """Visualize the error between two flows as 3-channel color image.

    Adapted from the KITTI C++ devkit.

    Args:
        flow_1: first flow of shape [num_batch, height, width, 2].
        flow_2: second flow (ground truth)
        mask_occ: flow validity mask of shape [num_batch, height, width, 1].
            Equals 1 at (occluded and non-occluded) valid pixels.
        mask_noc: Is 1 only at valid pixels which are not occluded.
    """
    mask_noc = tf.ones(tf.shape(mask_occ)) if mask_noc is None else mask_noc
    diff_sq = (flow_1 - flow_2) ** 2
    diff = tf.sqrt(tf.reduce_sum(diff_sq, [3], keep_dims=True))
    if log_colors:
        num_batch, height, width, _ = tf.unstack(tf.shape(flow_1))
        colormap = [
            [0,0.0625,49,54,149],
            [0.0625,0.125,69,117,180],
            [0.125,0.25,116,173,209],
            [0.25,0.5,171,217,233],
            [0.5,1,224,243,248],
            [1,2,254,224,144],
            [2,4,253,174,97],
            [4,8,244,109,67],
            [8,16,215,48,39],
            [16,1000000000.0,165,0,38]]
        colormap = np.asarray(colormap, dtype=np.float32)
        colormap[:, 2:5] = colormap[:, 2:5] / 255
        mag = tf.sqrt(tf.reduce_sum(tf.square(flow_2), 3, keep_dims=True))
        error = tf.minimum(diff / 3, 20 * diff / mag)
        im = tf.zeros([num_batch, height, width, 3])
        for i in range(colormap.shape[0]):
            colors = colormap[i, :]
            cond = tf.logical_and(tf.greater_equal(error, colors[0]),
                                  tf.less(error, colors[1]))
            im = tf.where(tf.tile(cond, [1, 1, 1, 3]),
                           tf.ones([num_batch, height, width, 1]) * colors[2:5],
                           im)
        im = tf.where(tf.tile(tf.cast(mask_noc, tf.bool), [1, 1, 1, 3]),
                       im, im * 0.5)
        im = im * mask_occ
    else:
        error = (tf.minimum(diff, 5) / 5) * mask_occ
        im_r = error # errors in occluded areas will be red
        im_g = error * mask_noc
        im_b = error * mask_noc
        im = tf.concat(axis=3, values=[im_r, im_g, im_b])
    return im

def flow_error_avg(flow_1, flow_2, mask):
    """Evaluates the average endpoint error between flow batches."""
    with tf.variable_scope('flow_error_avg'):
        diff = euclidean(flow_1 - flow_2) * mask
        error = tf.reduce_sum(diff) / tf.reduce_sum(mask)
        return error

def outlier_ratio(gt_flow, flow, mask, threshold=3.0, relative=0.05):
    diff = euclidean(gt_flow - flow) * mask
    if relative is not None:
        threshold = tf.maximum(threshold, euclidean(gt_flow) * relative)
        outliers = tf.cast(tf.greater_equal(diff, threshold), tf.float32)
    else:
        outliers = tf.cast(tf.greater_equal(diff, threshold), tf.float32)
    ratio = tf.reduce_sum(outliers) / tf.reduce_sum(mask)
    return ratio

def outlier_pct(gt_flow, flow, mask, threshold=3.0, relative=0.05):
    frac = outlier_ratio(gt_flow, flow, mask, threshold, relative) * 100
    return frac

def euclidean(t):
    return tf.sqrt(tf.reduce_sum(t ** 2, [3], keep_dims=True))


text:
import random
import logging
import numpy as np

import torch
import torch.nn as nn
import torch.nn.functional as F

def make_divisible(x, divisible_by=8):
    return int(np.ceil(x*1. / divisible_by)* divisible_by)

def conv_1x1_bn(input_channel, output_channel):
    return nn.Sequential(
        nn.Conv2d(input_channel, output_channel, 1, 1, 0, bias=False),
        nn.BatchNorm2d(output_channel),
        nn.ReLU6(inplace=True)
    )

class Flatten(nn.Module):
    def forward(self, x):
        return x.mean(3).mean(2)

class HSwish(nn.Module):
    def forward(self, x):
        out = x * F.relu6(x+3, inplace=True) / 6
        return out

class ConvBNRelu(nn.Sequential):
    def __init__(self,
                 input_channel,
                 output_channel,
                 kernel,
                 stride,
                 pad,
                 activation="relu",
                 bn=True,
                 group=1,
                 *args,
                 **kwargs):

        super(ConvBNRelu, self).__init__()

        assert activation in ["hswish", "relu", None]
        assert stride in [1, 2, 4]

        self.add_module("conv", nn.Conv2d(input_channel, output_channel, kernel, stride, pad, groups=group, bias=False))
        if bn:
            self.add_module("bn", nn.BatchNorm2d(output_channel))

        if activation == "relu":
            self.add_module("relu", nn.ReLU6(inplace=True))
        elif activation == "hswish":
            self.add_module("hswish", HSwish())

class SEModule(nn.Module):
    reduction = 4
    def __init__(self, C):
        super(SEModule, self).__init__()
        mid = max(C // self.reduction, 8)
        conv1 = nn.Conv2d(C, mid, 1, 1, 0)
        conv2 = nn.Conv2d(mid, C, 1, 1, 0)

        self.operation = nn.Sequential(
                nn.AdaptiveAvgPool2d(1), conv1, nn.ReLU(inplace=True), conv2, nn.Sigmoid()
            )

    def forward(self, x):
        return x * self.operation(x)

class ShuffleBlock(nn.Module):
    def __init__(self, groups):
        super(ShuffleBlock, self).__init__()
        self.groups = groups

    def forward(self, x):
        N, C, H, W = x.size()
        g = self.groups
        return x.view(N,g,int(C//g),H,W).permute(0,2,1,3,4).contiguous().view(N,C,H,W)

class GhostModule(nn.Module):
    def __init__(self,
                input_channel,
                output_channel,
                kernel=1,
                ratio=2,
                dw_kernel_size=3,
                stride=2,
                relu=True):
        slef.output_channel = output_channel
        init_channels = math.ceil(output_channel/ratio)
        new_channels = init_channels*(ratio-1)

        self.primary_conv = nn.Sequential(
                    nn.Conv2d(input_channel, init_channels, kernel, stride, kernel//2, bias=False),
                    nn.BatchNorm2d(init_channels),
                    nn.ReLU6(inplace=True) if relu else nn.Sequential()
                )
        self.cheap_operation = nn.Sequential(
                    nn.Conv2d(init_channels, new_channels, dw_kernel_size, 1, dw_kernel_size//2, groups=init_channels, bias=False),
                    nn.BatchNorm2d(new_channels),
                    nn.ReLU6(inplace=True) if relu else nn.Sequential()
                )

    def forward(self, x):
        x1 = self.primary_conv(x)
        x2 = self.cheap_operation(x1)
        out = torch.cat([x1, x2], dim=1)
        return out[:, :self.output_channel, :, :]

class BasicConv(nn.Module):
    def __init__(self,
                input_channel,
                output_channel,
                kernel,
                stride,
                activation,
                group=1,
                se=False,
                *args,
                **kwargs):

        super(BasicConv, self).__init__()
        self.use_res_connect = True if (stride==1 and input_channel == output_channel) else False

        self.conv1 = ConvBNRelu(input_channel,
                                input_channel,
                                kernel=kernel,
                                stride=1,
                                pad=(kernel//2),
                                activation=activation)

        self.conv2 = ConvBNRelu(input_channel,
                                output_channel,
                                kernel=kernel,
                                stride=stride,
                                pad=(kernel//2),
                                activation=activation)

    def forward(self, x):
        y = self.conv1(x)
        y = self.conv2(y)

        y = y + x if self.use_res_connect else y
        return y

class MixedBlock(nn.Module):
    def __init__(self,
                 input_channel,
                 output_channel,
                 stride,
                 split_block=4,
                 kernels=[3, 5, 7, 9],
                 axis=1,
                 group=1,
                 activation="relu",
                 block_type="MB"):
        super(MixedBlock, self).__init__()
        self.blocks = nn.ModuleList()

        self.skip_index = None
        for i, k in enumerate(kernels):
            if k == "skip":
                operation = nn.Conv2d(input_channel, output_channel, 1, stride, 0, bias=False)
                self.skip_index = i
            else:
                operation = ConvBNRelu(
                            input_channel,
                            output_channel,
                            kernel=k,
                            stride=stride,
                            pad=(k//2),
                            activation=activation,
                            group=group,
                        )
            self.blocks.append(operation)
        self.split_block = split_block

        # Order without skip operation
        self.split_block = split_block-1
        self.order = np.random.permutation(self.split_block)
        self.index = 0
        random.shuffle(self.order)
        # =============================
        self.skip_operation = False

        self.arch_param = None

    def forward(self, x, arch_flag=False):
        if not arch_flag:
            index = self.order[self.index] if not self.skip_operation else self.skip_index
            x = self.blocks[index](x) if index != self.skip_index else self.blocks[index](x)*0
            return x, self.skip_operation
        else:
            block_probability = self.arch_param.cuda()
            # If skip connection, then output set 0
            x = sum(b(x)*p if i != self.skip_index else b(x)*0 for i, (b, p) in enumerate(zip(self.blocks, block_probability)) if p > 1e-2)
            return x, False

    def set_arch_param(self, arch_param):
        self.arch_param = arch_param

    def set_training_order(self, reset=False, skip=False):
        """
        Choose the convolution operation. If skip is true, choose skip operation
        """
        self.skip_operation = False
        if reset:
            self.order = np.random.permutation(self.split_block)
            random.shuffle(self.order)
            self.index = 0

        if skip:
            self.skip_operation = True
        else:
            self.index += 1
            if self.index == self.split_block:
                self.order = np.random.permutation(self.split_block)
                random.shuffle(self.order)
                self.index = 0

class MPDBlock(nn.Module):
    """Mixed path depthwise block"""
    def __init__(self,
                 input_channel,
                 output_channel,
                 stride,
                 split_block=4,
                 kernels=[3, 5, 7, 9],
                 axis=1,
                 activation="relu",
                 block_type="MB",
                 search=False):
        super(MPDBlock, self).__init__()
        self.block_input_channel = input_channel//split_block
        self.block_output_channel = output_channel//split_block

        self.split_block = len(kernels)
        self.blocks = nn.ModuleList()

        for b in range(split_block):
            if search:
                operation = MixedBlock(
                            self.block_input_channel,
                            self.block_output_channel,
                            stride=stride,
                            split_block=len(kernels),
                            kernels=kernels,
                            group=self.block_output_channel,
                            activation=activation,
                        )
            else:
                operation = nn.Conv2d(
                            self.block_input_channel,
                            self.block_output_channel,
                            kernels[b],
                            stride,
                            (kernels[b]//2),
                            groups=self.block_output_channel,
                            bias=False)
            self.blocks.append(operation)

        if not search:
            self.bn = nn.BatchNorm2d(output_channel)
            self.relu = nn.ReLU6(inplace=True)

        self.axis = axis
        self.search = search

    def forward(self, x, arch_flag=False):
        split_x = torch.split(x, self.block_input_channel, dim=self.axis)
        # =================== SBN
        skip_connection_num = 0
        output_list = []
        skip_flag = False
        for x_i, conv_i in zip(split_x, self.blocks):
            if self.search:
                output, skip_flag = conv_i(x_i, arch_flag)
                skip_connection_num += 1 if skip_flag else 0
            else:
                output = conv_i(x_i)

            output_list.append(output)

        x = torch.cat(output_list, dim=self.axis)

        if not self.search:
            x = self.bn(x)
            x = self.relu(x)
        return x, skip_connection_num
        # ===================

    def set_arch_param(self, arch_param):
        for i, l in enumerate(self.blocks):
            l.set_arch_param(arch_param[i*self.split_block:(i+1)*self.split_block])

    def set_training_order(self, active_block, reset=False, static=False):
        # ================ Choose active_block
        if static and active_block == 0:
            # At least choose one block in static layer
            active_block = random.randint(1, len(self.blocks))

        # ================ Architecture Specific
        active_order = np.random.permutation(len(self.blocks))
        #active_order = np.array([0, 1, 2, 3, 4, 5])
        active_blocks = active_order[:active_block]

        for b_num, b in enumerate(self.blocks):
            if b_num in active_blocks:
                b.set_training_order(reset, skip=False)
            else:
                b.set_training_order(reset, skip=True)

class MBConv(nn.Module):
    def __init__(self,
                 input_channel,
                 output_channel,
                 expansion,
                 kernels,
                 stride,
                 activation,
                 split_block=1,
                 group=1,
                 se=False,
                 search=False,
                 *args,
                 **kwargs):
        super(MBConv, self).__init__()
        self.use_res_connect = True if (stride==1 and input_channel == output_channel) else False
        mid_depth = int(input_channel * expansion)

        self.group = group

        if input_channel == mid_depth:
            self.point_wise = nn.Sequential()
        else:
            self.point_wise = ConvBNRelu(input_channel,
                                         mid_depth,
                                         kernel=1,
                                         stride=1,
                                         pad=0,
                                         activation=activation,
                                         group=group,
                                    )

        self.depthwise = MPDBlock(mid_depth,
                                  mid_depth,
                                  stride,
                                  split_block=expansion,
                                  kernels=kernels,
                                  activation=activation,
                                  search=search)

        self.point_wise_1 = ConvBNRelu(mid_depth,
                                       output_channel,
                                       kernel=1,
                                       stride=1,
                                       pad=0,
                                       activation=None,
                                       bn=False if search else True,
                                       group=group,
                                    )
        self.se = SEModule(mid_depth) if se else None

        self.expansion = expansion
        self.search = search
        if self.search:
            # reference : https://arxiv.org/pdf/1908.06022.pdf
            self.learnable_stabilizer = nn.Conv2d(input_channel, output_channel, 1, stride, 0, bias=False)
            # ====================
            # reference : https://arxiv.org/pdf/2001.05887.pdf
            self.sbn = nn.ModuleList()
            for i in range(expansion):
                self.sbn.append(nn.BatchNorm2d(output_channel))
            #self.sbn = nn.BatchNorm2d(output_channel)
            self.skip_connection_num = 0
            # ===================
            self.input_temp = None

            # =================== Training order
            self.index = 0

            self.order = [2, 3, 4, 5, 6]
            self.order = np.array(self.order)
            random.shuffle(self.order)

            self.order_distill = [2, 3, 4, 5, 6]
            self.order_distill = np.array(self.order_distill)
            random.shuffle(self.order_distill)
            # ===================

    def forward(self, x, arch_flag=False):
        y = self.point_wise(x)
        y, skip_connection_num = self.depthwise(y) if not arch_flag else self.depthwise(y, arch_flag)

        y = self.se(y) if self.se is not None else y
        if self.search:
            self.input_temp = y
        y = self.point_wise_1(y)

        if self.search:
            # ============== SBN
            if arch_flag:
                skip_connection_num = round(self.skip_connection_num)
            if skip_connection_num != self.expansion:
                y = self.sbn[skip_connection_num](y)
            # ==============
            #y = self.sbn(y)

        if skip_connection_num == self.expansion and self.search:
            y = x
            # Skip connection
            #y = self.learnable_stabilizer(y)
        y = y + x if self.use_res_connect else y

        return y

    def set_arch_param(self, arch_param):
        self.depthwise.set_arch_param(arch_param)

        # Count continue skip num
        self.skip_connection_num = 0
        kernel_nums = len(arch_param) // self.expansion
        for i in range(0, len(arch_param), kernel_nums):
            split_arch_param = arch_param[i:i+kernel_nums]
            self.skip_connection_num += split_arch_param[-1].item()

    def set_training_order(self,reset=False, state=None, static=False):
        expansion = None
        if reset:
            self.index = 0
            random.shuffle(self.order)
            random.shuffle(self.order_distill)
            if state is None:
                expansion = self.order[self.index]
            else:
                expansion = self.order_distill[self.index]
            self.index += 1
        else:
            if state == "Max":
                expansion = 6
            elif state == "Min":
                expansion = 1
            elif state == "Random":
                expansion = self.order_distill[self.index]

                self.index += 1
                if self.index == len(self.order_distill):
                    self.index = 0
                    random.shuffle(self.order_distill)
            else:
                expansion = self.order[self.index]

                self.index += 1
                if self.index == len(self.order):
                    self.index = 0
                    random.shuffle(self.order)

        self.depthwise.set_training_order(expansion, reset, static)

    def bn_statics_tracking(self):
        mean_list = []
        var_list = []
        if self.input_temp is not None:
            print(torch.mean(self.input_temp))
        for bn in self.sbn:
            state_dict = bn.state_dict()
            running_mean = state_dict["running_mean"]
            running_var = state_dict["running_var"]

            mean_list.append(running_mean)
            var_list.append(running_var)

        return mean_list, var_list

if __name__ == "__main__":
    i = torch.zeros((1, 3, 32, 32))
    block = MBConv(3, 16, 6, [3], 2, "relu", split_block=1)

ext: py

reason: import numpy


text:
#
# Copyright (C) 2021 Intel Corporation
# SPDX-License-Identifier: Apache-2.0
#
#
import os
import math
import shutil
import warnings
import numpy as np
import tensorflow as tf

from tqdm import tqdm
from utils.infer_util import InferRequestsQueue, loader

warnings.filterwarnings("ignore")
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.ERROR)

OUTPUT, SWAP, INFER, ARG = 5, 6, 7, 8

def infer_section_sync(arg_obj, logger, get_functions):
    """
    Infer on section data synchronously. In order to use this in your
    configuration, specify ``infer_type`` as ``section_sync``. Section inference
    requires that additional parameters such as ``slice``, ``subsampl``, and
    ``slice_no`` be specified in the JSON configuration file. This function's
    specific arguments will be filled according to your configuration inputs.

    :param arg_obj: Arguments object that holds parameters needed for inference.
    :param logger: Common logger object for logging coherence.
    :param get_functions: Functions associated with parameters given.
    :return: None
    """
    logger.setLevel(OUTPUT)
    output_folder = arg_obj.output
    if not os.path.exists(output_folder):
        logger.output('Making new folder for output storage.')
        os.mkdir(output_folder)
    else:
        logger.output('Output folder already exists. Deleting...')
        shutil.rmtree(output_folder)
        logger.output('Making new folder for output storage.')
        os.mkdir(output_folder)

    logger.setLevel(INFER)
    logger.infer('Setting up inference...')
    preprocess, postprocess, model = get_functions(
        arg_obj.model, arg_obj.given_model_name)
    in_shape = model.get_input_shape()
    logger.infer('Using model: {}'.format(model.name))

    # Expects one input and one output layer
    assert (len(model.get_inputs()) <
            2), "[ERROR] Expects model with one input layer."
    assert (len(model.get_outputs()) <
            2), "[ERROR] Expects model with one output layer."

    slice_type = arg_obj.slice
    subsampl = 1  # arg_obj.subsampl
    im_size = arg_obj.im_size
    slice_no = arg_obj.slice_no
    return_full_size = arg_obj.return_to_fullsize
    sep = os.path.sep

    data_arr = []
    assert (os.path.isdir(arg_obj.data) or os.path.isfile(
        arg_obj.data)), "[ERROR] Unexpected data input."
    if os.path.isdir(arg_obj.data):
        for data_file_name in os.listdir(arg_obj.data):
            path_to_file = arg_obj.data + sep + data_file_name
            data, data_info = loader(path_to_file)
            data_arr.append(
                {'name': data_file_name, 'data': data, 'data_info': data_info})
    if os.path.isfile(arg_obj.data):
        data, data_info = loader(arg_obj.data)
        data_arr.append({'name': arg_obj.data.replace(
            "/", "-"), 'data': data, 'data_info': data_info})

    logger.infer('Conducting inference...')
    def ls(N): return np.linspace(0, N - 1, N, dtype='int')
    for data_dict in data_arr:
        input_name = data_dict['name']
        data = data_dict['data']
        data_info = data_dict['data_info']

        logger.infer('Conducting inference on input: {}...'.format(input_name))
        logger.infer(
            'Inference Config - Slice Type {} on Slice No. {}...'.format(slice_type, slice_no))

        N0, N1, N2 = data.shape
        x0_range = ls(N0)
        x1_range = ls(N1)
        x2_range = ls(N2)

        check_slice_type = slice_type == 'inline' or slice_type == 'crossline' or slice_type == 'timeslice'
        assert check_slice_type, "[ERROR] Invalid slice_type: {}".format(
            slice_type)

        if slice_type == 'inline':
            slice_no = slice_no - data_info['inline_start']
            class_cube = data[::subsampl, 0:1, ::subsampl] * 0
            x1_range = np.array([slice_no])

        elif slice_type == 'crossline':
            slice_no = slice_no - data_info['crossline_start']
            class_cube = data[::subsampl, ::subsampl, 0:1, ] * 0
            x2_range = np.array([slice_no])

        elif slice_type == 'timeslice':
            slice_no = slice_no - data_info['timeslice_start']
            class_cube = data[0:1, ::subsampl, ::subsampl] * 0
            x0_range = np.array([slice_no])

        assert slice_no > - \
            1, "[ERROR] Invalid slice_no. For {}, refer to: {}".format(
                input_name, data_info)

        n0, n1, n2 = class_cube.shape
        # x0_grid, x1_grid, x2_grid = np.meshgrid(ls(n0,), ls(n1), ls(n2), indexing='ij')
        X0_grid, X1_grid, X2_grid = np.meshgrid(
            x0_range, x1_range, x2_range, indexing='ij')
        X0_grid_sub = X0_grid[::subsampl, ::subsampl, ::subsampl]
        X1_grid_sub = X1_grid[::subsampl, ::subsampl, ::subsampl]
        X2_grid_sub = X2_grid[::subsampl, ::subsampl, ::subsampl]

        w = in_shape[2] // 2
        h = in_shape[3] // 2

        # Decide iterator axis
        # X0_grid_sub.size / w * w
        iter_axis = range(
            math.ceil(n1 / in_shape[2]) * math.ceil(n2 / in_shape[2]))
        if slice_type == 'inline':
            # X1_grid_sub.size / w * w
            iter_axis = range(
                math.ceil(n0 / in_shape[2]) * math.ceil(n2 / in_shape[2]))
        if slice_type == 'crossline':
            # X2_grid_sub.size / w * w
            iter_axis = range(
                math.ceil(n0 / in_shape[2]) * math.ceil(n1 / in_shape[2]))

        iter_axis = tqdm(iter_axis)

        next_X0_1, next_X0_2 = 0, in_shape[2]
        next_X1_1, next_X1_2 = 0, in_shape[2]
        next_X2_1, next_X2_2 = 0, in_shape[2]

        for i in iter_axis:
            X0 = X0_grid_sub.ravel()[
                i] if slice_type == 'timeslice' else next_X0_1 + w
            X1 = X1_grid_sub.ravel()[
                i] if slice_type == 'inline' else next_X1_1 + w
            X2 = X2_grid_sub.ravel()[
                i] if slice_type == 'crossline' else next_X2_1 + w

            mini_sheet = np.zeros(in_shape)
            found_mini_sheet = False
            if slice_type == 'inline' and next_X0_1 == X0 - w and next_X2_1 == X2 - w:
                # X1 out
                end_X0 = min(next_X0_2, X0 + w + 1)
                end_X2 = min(next_X2_2, X2 + w + 1)

                mini_sheet = data[X0-w:end_X0, X1, X2-w:end_X2]
                mini_sheet = mini_sheet[np.newaxis, np.newaxis, :, :]
                found_mini_sheet = True

            if slice_type == 'crossline' and next_X0_1 == X0 - w and next_X1_1 == X1 - w:
                # X2 out
                end_X0 = min(next_X0_2, X0 + w + 1)
                end_X1 = min(next_X1_2, X1 + w + 1)

                mini_sheet = data[X0-w: end_X0, X1-w: end_X1, X2]
                mini_sheet = mini_sheet[np.newaxis, np.newaxis, :, :]
                found_mini_sheet = True

            if slice_type == 'timeslice' and next_X1_1 == X1 - w and next_X2_1 == X2 - w:
                # X0 out
                end_X1 = min(next_X1_2, X1 + w + 1)
                end_X2 = min(next_X2_2, X2 + w + 1)

                mini_sheet = data[X0, X1-w: end_X1, X2-w: end_X2]
                mini_sheet = mini_sheet[np.newaxis, np.newaxis, :, :]
                found_mini_sheet = True

            if found_mini_sheet:
                orig_shape = mini_sheet.shape
                input_dict = preprocess(mini_sheet, model.get_inputs(), model)

                output_dict, latency = model.infer(input_dict)
                output_dict = postprocess(output_dict, orig_shape)
                out = output_dict[list(output_dict.keys())[0]]
                out = np.squeeze(out)

                if slice_type == 'inline':
                    # X1 out
                    class_cube[X0 - w: X0 + w + 1, 0, X2 - w: X2 + w + 1] = out

                    if next_X2_2 == n2:
                        next_X0_1, next_X0_2 = X0 + w, min(X0 + 3*w + 1, n0)
                        next_X2_1, next_X2_2 = 0, in_shape[2]
                    else:
                        next_X2_1, next_X2_2 = X2 + w, min(X2 + 3*w + 1, n2)

                if slice_type == 'crossline':
                    # X2 out
                    class_cube[X0 - w: X0 + w + 1, X1 - w: X1 + w + 1, 0] = out

                    if next_X1_2 == n1:
                        next_X0_1, next_X0_2 = X0 + w, min(X0 + 3*w + 1, n0)
                        next_X1_1, next_X1_2 = 0, in_shape[2]
                    else:
                        next_X1_1, next_X1_2 = X1 + w, min(X1 + 3*w + 1, n1)

                if slice_type == 'timeslice':
                    # X0 out
                    class_cube[0, X1 - w: X1 + w + 1, X2 - w: X2 + w + 1] = out

                    if next_X2_2 == n2:
                        next_X1_1, next_X1_2 = X1 + w, min(X1 + 3*w + 1, n1)
                        next_X2_1, next_X2_2 = 0, in_shape[2]
                    else:
                        next_X2_1, next_X2_2 = X2 + w, min(X2 + 3*w + 1, n2)

        input_ref = input_name + "-input"
        save_path = output_folder + sep + input_ref
        logger.infer('Saving output to output path: {}'.format(
            save_path + sep + "out.npy"
        ))
        if not os.path.exists(save_path):
            os.mkdir(save_path)

        np.save(save_path + sep + "out", class_cube)
        logger.infer('Complete!')

def infer_section_async(arg_obj, logger, get_functions):
    """
    Infer on section data asynchronously. In order to use this in your
    configuration, specify ``infer_type`` as ``section_async``. Section inference
    requires that additional parameters such as ``slice``, ``subsampl``, and
    ``slice_no`` be specified in the JSON configuration file. This function's
    specific arguments will be filled according to your configuration inputs.

    :param arg_obj: Arguments object that holds parameters needed for inference.
    :param logger: Common logger object for logging coherence.
    :param get_functions: Functions associated with parameters given.
    :return: None
    """
    logger.setLevel(OUTPUT)
    output_folder = arg_obj.output
    if not os.path.exists(output_folder):
        logger.output('Making new folder for output storage.')
        os.mkdir(output_folder)
    else:
        logger.output('Output folder already exists. Deleting...')
        shutil.rmtree(output_folder)
        logger.output('Making new folder for output storage.')
        os.mkdir(output_folder)

    logger.setLevel(INFER)
    logger.infer('Setting up inference queues and requests...')
    preprocess, postprocess, model = get_functions(
        arg_obj.model, arg_obj.given_model_name, arg_obj.infer_type, arg_obj.streams)
    in_shape = model.get_input_shape()
    logger.infer('Using model: {}'.format(model.name))

    # Expects one input and one output layer
    assert (len(model.get_inputs()) <
            2), "[ERROR] Expects model with one input layer."
    assert (len(model.get_outputs()) <
            2), "[ERROR] Expects model with one output layer."

    slice_type = arg_obj.slice
    subsampl = 1  # arg_obj.subsampl
    im_size = arg_obj.im_size
    slice_no = arg_obj.slice_no
    return_full_size = arg_obj.return_to_fullsize
    sep = os.path.sep

    data_arr = []
    assert (os.path.isdir(arg_obj.data) or os.path.isfile(
        arg_obj.data)), "[ERROR] Unexpected data input."
    if os.path.isdir(arg_obj.data):
        for data_file_name in os.listdir(arg_obj.data):
            path_to_file = arg_obj.data + sep + data_file_name
            data, data_info = loader(path_to_file)
            data_arr.append({'name': data_file_name, 'data': data})
    if os.path.isfile(arg_obj.data):
        data, data_info = loader(arg_obj.data)
        data_arr.append({'name': arg_obj.data.replace("/", "-"), 'data': data})

    def async_callback(param_dict):
        """
        Params:
        param_dict - dictionary which holds:
            (1) request
            (2) postprocess
            (3) file_name
        """
        request = param_dict['request']
        postprocess = param_dict['postprocess']
        order_dict = param_dict['order_dict']
        orig_shape = param_dict['orig_shape']
        slice_type = param_dict['slice_type']
        i = param_dict['order']

        output_blobs = request.output_blobs
        out_layer = list(output_blobs.keys())[0]
        output_dict = {out_layer: output_blobs[out_layer].buffer}

        output_dict = postprocess(output_dict, orig_shape)

        out = output_dict[list(output_dict.keys())[0]]
        out = np.squeeze(out)
        if slice_type == 'inline':
            out = out[:, np.newaxis, :]
        if slice_type == 'crossline':
            out = out[:, :, np.newaxis]
        if slice_type == 'timeslice':
            out = out[np.newaxis, :, :]

        order_dict[i] = {
            'x0x1x2': param_dict['x0x1x2'], 'out': out
        }

        return out

    requests = model.get_requests()
    request_queue = InferRequestsQueue(requests, async_callback, postprocess)

    logger.infer('Conducting inference...')
    def ls(N): return np.linspace(0, N - 1, N, dtype='int')
    for data_dict in data_arr:
        input_name = data_dict['name']
        data = data_dict['data']

        logger.infer('Conducting inference on input: {}...'.format(input_name))

        N0, N1, N2 = data.shape
        x0_range = ls(N0)
        x1_range = ls(N1)
        x2_range = ls(N2)
        pred_points = (x0_range[::subsampl],
                       x1_range[::subsampl], x2_range[::subsampl])

        check_slice_type = slice_type == 'inline' or slice_type == 'crossline' or slice_type == 'timeslice'
        assert check_slice_type, "[ERROR] Invalid slice_type: {}".format(
            slice_type)

        if slice_type == 'inline':
            slice_no = slice_no - data_info['inline_start']
            class_cube = data[::subsampl, 0:1, ::subsampl] * 0
            x1_range = np.array([slice_no])

        elif slice_type == 'crossline':
            slice_no = slice_no - data_info['crossline_start']
            class_cube = data[::subsampl, ::subsampl, 0:1, ] * 0
            x2_range = np.array([slice_no])

        elif slice_type == 'timeslice':
            slice_no = slice_no - data_info['timeslice_start']
            class_cube = data[0:1, ::subsampl, ::subsampl] * 0
            x0_range = np.array([slice_no])

        assert slice_no > - \
            1, f"[ERROR] Invalid slice_no. For {input_name}, refer to: {data_info}"

        n0, n1, n2 = class_cube.shape
        x0_grid, x1_grid, x2_grid = np.meshgrid(
            ls(n0,), ls(n1), ls(n2), indexing='ij')
        X0_grid, X1_grid, X2_grid = np.meshgrid(
            x0_range, x1_range, x2_range, indexing='ij')
        X0_grid_sub = X0_grid[::subsampl, ::subsampl, ::subsampl]
        X1_grid_sub = X1_grid[::subsampl, ::subsampl, ::subsampl]
        X2_grid_sub = X2_grid[::subsampl, ::subsampl, ::subsampl]

        w = in_shape[2] // 2
        h = in_shape[3] // 2
        order_dict = {}

        # Decide iterator axis
        # X0_grid_sub.size / w * w
        iter_axis = range(
            math.ceil(n1 / in_shape[2]) * math.ceil(n2 / in_shape[2]))
        if slice_type == 'inline':
            # X1_grid_sub.size / w * w
            iter_axis = range(
                math.ceil(n0 / in_shape[2]) * math.ceil(n2 / in_shape[2]))
        if slice_type == 'crossline':
            # X2_grid_sub.size / w * w
            iter_axis = range(
                math.ceil(n0 / in_shape[2]) * math.ceil(n1 / in_shape[2]))

        iter_axis = tqdm(iter_axis)

        next_X0_1, next_X0_2 = 0, in_shape[2]
        next_X1_1, next_X1_2 = 0, in_shape[2]
        next_X2_1, next_X2_2 = 0, in_shape[2]

        for i in iter_axis:
            X0 = X0_grid_sub.ravel()[
                i] if slice_type == 'timeslice' else next_X0_1 + w
            X1 = X1_grid_sub.ravel()[
                i] if slice_type == 'inline' else next_X1_1 + w
            X2 = X2_grid_sub.ravel()[
                i] if slice_type == 'crossline' else next_X2_1 + w

            mini_sheet = np.zeros(in_shape)
            found_mini_sheet = False
            end_X0 = end_X1 = end_X2 = 1
            if slice_type == 'inline' and next_X0_1 == X0 - w and next_X2_1 == X2 - w:
                # X1 out
                end_X0 = min(next_X0_2, X0 + w + 1)
                end_X2 = min(next_X2_2, X2 + w + 1)

                mini_sheet = data[X0-w:end_X0, X1, X2-w:end_X2]
                mini_sheet = mini_sheet[np.newaxis, np.newaxis, :, :]
                found_mini_sheet = True

            if slice_type == 'crossline' and next_X0_1 == X0 - w and next_X1_1 == X1 - w:
                # X2 out
                end_X0 = min(next_X0_2, X0 + w + 1)
                end_X1 = min(next_X1_2, X1 + w + 1)

                mini_sheet = data[X0-w:end_X0, X1-w:end_X1, X2]
                mini_sheet = mini_sheet[np.newaxis, np.newaxis, :, :]
                found_mini_sheet = True

            if slice_type == 'timeslice' and next_X1_1 == X1 - w and next_X2_1 == X2 - w:
                # X0 out
                end_X1 = min(next_X1_2, X1 + w + 1)
                end_X2 = min(next_X2_2, X2 + w + 1)

                mini_sheet = data[X0, X1-w: end_X1, X2-w: end_X2]
                mini_sheet = mini_sheet[np.newaxis, np.newaxis, :, :]
                found_mini_sheet = True

            if found_mini_sheet:
                orig_shape = mini_sheet.shape
                input_dict = preprocess(mini_sheet, model.get_inputs(), model)

                # Inference! input_dict => {output_layer: output_data}, latency
                infer_request = request_queue.get_idle_request()
                infer_request.start_async(input_dict, input_name, {
                    'x0x1x2': [
                        (0, 1) if slice_type == 'timeslice' else (X0 - w, end_X0),
                        (0, 1) if slice_type == 'inline' else (X1 - w, end_X1),
                        (0, 1) if slice_type == 'crossline' else (X2 - w, end_X2)
                    ], 'order': i, 'order_dict': order_dict, 'orig_shape': orig_shape,
                    'slice_type': slice_type
                })

                if slice_type == 'inline':
                    # X1 out

                    if next_X2_2 == n2:
                        next_X0_1, next_X0_2 = X0 + w, min(X0 + 3*w + 1, n0)
                        next_X2_1, next_X2_2 = 0, in_shape[2]
                    else:
                        next_X2_1, next_X2_2 = X2 + w, min(X2 + 3*w + 1, n2)

                if slice_type == 'crossline':
                    # X2 out

                    if next_X1_2 == n1:
                        next_X0_1, next_X0_2 = X0 + w, min(X0 + 3*w + 1, n0)
                        next_X1_1, next_X1_2 = 0, in_shape[2]
                    else:
                        next_X1_1, next_X1_2 = X1 + w, min(X1 + 3*w + 1, n1)

                if slice_type == 'timeslice':
                    # X0 out

                    if next_X2_2 == n2:
                        next_X1_1, next_X1_2 = X1 + w, min(X1 + 3*w + 1, n1)
                        next_X2_1, next_X2_2 = 0, in_shape[2]
                    else:
                        next_X2_1, next_X2_2 = X2 + w, min(X2 + 3*w + 1, n2)

        logger.infer('Cleaning up requests...')
        request_queue.wait_all()

        logger.infer('Placing prediction in proper cube spot...')
        available_keys = set(list(order_dict.keys()))

        # Decide iterator axis
        # X0_grid_sub.size / w * w
        iter_axis = range(
            math.ceil(n1 / in_shape[2]) * math.ceil(n2 / in_shape[2]))
        if slice_type == 'inline':
            # X1_grid_sub.size / w * w
            iter_axis = range(
                math.ceil(n0 / in_shape[2]) * math.ceil(n2 / in_shape[2]))
        if slice_type == 'crossline':
            # X2_grid_sub.size / w * w
            iter_axis = range(
                math.ceil(n0 / in_shape[2]) * math.ceil(n1 / in_shape[2]))

        iter_axis = tqdm(iter_axis)

        for i in iter_axis:
            if i in available_keys:
                out_w_param = order_dict[i]
                out = out_w_param['out']
                x0, x1, x2 = out_w_param['x0x1x2']
                x0_1, x0_2 = x0
                x1_1, x1_2 = x1
                x2_1, x2_2 = x2

                class_cube[x0_1:x0_2, x1_1:x1_2, x2_1:x2_2] = out

        input_ref = input_name + "-input"
        save_path = output_folder + sep + input_ref
        logger.infer('Saving output to output path: {}'.format(
            save_path + sep + "out.npy"))
        if not os.path.exists(save_path):
            os.mkdir(save_path)

        np.save(save_path + sep + "out", class_cube)
        logger.infer('Complete!')


text:
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
from sklearn.datasets.samples_generator import make_blobs

class MeanShift:
    ## Class Variables ##
    X = radius = bandwidth = max_itr = centroids = None

    ## Constructors ##
    def __init__(self, radius, bandwidth, max_itr=10):
        self.radius = radius
        self.bandwidth = bandwidth
        self.max_itr = max_itr

    ## Methods ##
    def parse_data(self, file_path, sep):
        df = pd.read_csv(file_path, sep=sep, header=None)
        self.X = df.values

    def gen_data(self):
        self.X, y = make_blobs(n_samples=300, n_features=2, centers=4, cluster_std=1)

    def clusterify(self):
        self._init_centroid()

        for i in range(self.max_itr):
            for j, centroid in enumerate(self.centroids):
                neighbours = self._neighbourhood_points(self.centroids[j])
                new_centroid = self._calc_new_centroid(self.centroids[j], neighbours)
                self.centroids[j] = new_centroid

    def _init_centroid(self):
        self.centroids = np.copy(self.X)

    def _neighbourhood_points(self, centroid):
        neighbours = []
        for i, old_centroid in enumerate(self.centroids):
            if self.euclidean_dist(old_centroid, centroid) <= self.radius:
                neighbours.append(old_centroid)

        return neighbours

    def _calc_new_centroid(self, centroid, neighbours):
        numer = denom = 0

        for neighbour in neighbours:
            weight = self._gaussian_kernel(self.euclidean_dist(neighbour, centroid))
            numer += (weight * neighbour)
            denom += weight

        return numer / denom

    def _gaussian_kernel(self, distance):
        return (1 / (self.bandwidth * np.sqrt(2 * np.pi))) \
               * np.exp(-0.5 * (distance / self.bandwidth) ** 2)

    def euclidean_dist(self, p1, p2):
        sqr_dist = 0

        for i in range(len(p1)):
            sqr_dist += (p1[i] - p2[i]) ** 2

        return np.sqrt(sqr_dist)

def main():
    mean_shift = MeanShift(radius=3, bandwidth=5)
    mean_shift.gen_data()
    mean_shift.clusterify()

    plt.scatter(mean_shift.X[:, 0], mean_shift.X[:, 1], s=30)

    for i, centroids in enumerate(mean_shift.centroids):
        plt.scatter(mean_shift.centroids[i][0], mean_shift.centroids[i][1], s=130, color='r', marker='x')

    plt.show()

if __name__ == "__main__":
    main()

max_stars_repo_name: sundogu/ML-Mean-Shift-Cluster

max_issues_repo_name: sundogu/ML-Mean-Shift-Cluster

max_forks_repo_name: sundogu/ML-Mean-Shift-Cluster


text:
import numpy as np

from gensei.base import is_sequence, ordered_iteritems

def get_random(ranges0, ranges1):
    """
    Get an array of random numbers `a` with the uniform distribution so that
    `ranges0[i] <= a[i] < ranges1[i]`.

    Returns
    -------
    a : array
        The array of random numbers.
    """
    ranges0 = np.atleast_1d(ranges0)
    return (ranges1 - ranges0) * np.random.random(len(ranges0)) + ranges0

def get_random_direction(dim):
    """
    Get a uniform random direction in unit ball of dimension `dim`.
    """
    while 1:
        coor = np.random.normal(0.0, 1.0, size=dim)
        r = np.linalg.norm(coor)
        if (r <= 1.0) and (r > 1e-5) :
            break
    out = coor / r

    return out

def evaluate(val, shape=None):
    """
    Evaluate a single value or a sequence of values.

    The values can be have a uniform (keyword 'random') or normal (keyword
    'normal') random distribution.

    Parameters
    ----------
    val : config value
        The value to be evaluated. It can be one of:

        - list: evaluate() is called recursively for each item
        - tuple:
            - ('random', [`min. values`], [`max. values`]) (uniform)
            - ('random', `min. value`, `max. value`)
            - ('normal', `mean`, `standard deviation`)
            - ('random direction', `dim`) : uniform distribution of unit vectors
              in dimension `dim`
        - 'random' : random value(s) in [0, 1)
        - 'random direction' : uniform distribution of unit vectors
              in dimension 3
        - other values are returned as they are.
    shape : tuple, optional
        Shape of the data when `val` is 'random'.

    Returns
    -------
    out : data
        The evaluated data.
    """
    if isinstance(val, list):
        out = []
        for sub_val in val:
            out.append(evaluate(sub_val))
        out = np.array(out, dtype=np.float64)

    elif isinstance(val, tuple):
        if val[0] == 'normal':
            out = np.random.normal(val[1], val[2])

        elif val[0] == 'random':
            if is_sequence(val[1]):
                out = get_random(val[1], val[2])

            else:
                out = (val[2] - val[1]) * np.random.random() + val[1]

        elif val[0] == 'random direction':
            out = get_random_direction(val[1])

        else:
            raise ValueError('unsupported value type! (%s)' % val[0])

    elif val == 'random':
        out = np.random.random(shape)

    elif val == 'random direction':
        out = get_random_direction(3)

    else:
        out = val

    return out

def get_suffix(n):
    """Get suffix format string given a number of files.

    Examples:

        n = 5 -> '%01d'
        n = 15 -> '%02d'
        n = 1005 -> '%04d'
    """
    if n > 1:
        n_digit = int(np.log10(n - 1) + 1)
        suffix = '%%0%dd' % n_digit
    else:
        suffix = '%d'
    return suffix

def format_dict(d, raw=None, indent=2):
    """Format a dictionary for printing.

    Parameters:

        d : dict
            The dictionary.

        raw : dict
            The raw (unadjusted) dictionary to compare with.

        indent : int
            The indentation level.

    Return:

        msg : string
           The string with dictionary's key : val formatted in two columns.
    """
    if raw is None:
        raw = d

    msg = ''
    for key, val in ordered_iteritems(d):
        if val == raw[key]:
            msg += (' ' * indent) + ('%s : %s\n' % (key, val))
        else:
            msg += (' ' * indent) + ('%s : %s (%s)\n' % (key, val, raw[key]))

    msg = msg[:-1]

    return msg

max_stars_repo_name: rc/gensei

max_issues_repo_name: rc/gensei

max_forks_repo_name: rc/gensei


text:
# coding:utf8

# @author leone
# @desc 从人脸图像文件中提取人脸特征存入 CSV 增加录入多张人脸到 CSV 的功能
# @version 2018-12-12

# return_128d_features()          获取某张图像的 128D 特征
# write_into_csv()                获取某个路径下所有图像的特征，并写入 CSV
# compute_the_mean()              从 CSV　中读取　128D 特征，并计算特征均值

import csv
import os

import cv2
import dlib
import numpy as np
import pandas as pd
from skimage import io

face_path = "../data/faces_from_camera/"
csv_path = "../data/csv_from_camera/"

# Dlib 正向人脸检测器
detector = dlib.get_frontal_face_detector()

# Dlib 人脸预测器
predictor = dlib.shape_predictor("../data/data_dlib/shape_predictor_5_face_landmarks.dat")

# Dlib 人脸识别模型
# Face recognition model, the object maps human faces into 128D vectors
facerec = dlib.face_recognition_model_v1("../data/data_dlib/dlib_face_recognition_resnet_model_v1.dat")

# 返回单张图像的 128D 特征
def return_128d_features(path_img):
    img = io.imread(path_img)
    img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    faces = detector(img_gray, 1)

    print("检测到人脸的图像：", path_img, "\n")

    # 因为有可能截下来的人脸再去检测，检测不出来人脸了
    # 所以要确保是 检测到人脸的人脸图像 拿去算特征
    if len(faces) != 0:
        shape = predictor(img_gray, faces[0])
        face_descriptor = facerec.compute_face_descriptor(img_gray, shape)
    else:
        face_descriptor = 0
        print("no face")

    # print(face_descriptor)
    return face_descriptor

#   将文件夹中照片特征提取出来写入 CSV文件
#   path_faces_personX:     图像文件夹的路径
#   path_csv:               要生成的 CSV 路径

def write_into_csv(path_faces_personX, path_csv):
    dir_pics = os.listdir(path_faces_personX)
    with open(path_csv, "w", newline="") as csvfile:
        writer = csv.writer(csvfile)
        for i in range(len(dir_pics)):
            # 调用return_128d_features()得到128d特征
            print("正在读的人脸图像：", path_faces_personX + "/" + dir_pics[i])
            features_128d = return_128d_features(path_faces_personX + "/" + dir_pics[i])
            #  print(features_128d)
            # 遇到没有检测出人脸的图片跳过
            if features_128d == 0:
                i += 1
            else:
                writer.writerow(features_128d)

# 读取某人所有的人脸图像的数据，写入 person_X.csv
faces = os.listdir(face_path)
for person in faces:
    print("##### " + person + " #####")
    print(csv_path + person + ".csv")
    write_into_csv(face_path + person, csv_path + person + ".csv")

# 从 CSV 中读取数据，计算 128D 特征的均值
def compute_the_mean(path_csv_rd):
    column_names = []

    # 128列特征
    for feature_num in range(128):
        column_names.append("features_" + str(feature_num + 1))

    # 利用pandas读取csv
    rd = pd.read_csv(path_csv_rd, names=column_names)

    # 存放128维特征的均值
    feature_mean = []

    for feature_num in range(128):
        tmp_arr = rd["features_" + str(feature_num + 1)]
        tmp_arr = np.array(tmp_arr)

        # 计算某一个特征的均值
        tmp_mean = np.mean(tmp_arr)
        feature_mean.append(tmp_mean)
    return feature_mean

# 存放所有特征均值的 CSV 的路径
path_csv_feature_all = "../data/features_all.csv"

# 存放人脸特征的 CSV 的路径
path_csv_rd = "../data/csv_from_camera/"

with open(path_csv_feature_all, "w", newline="") as csvfile:
    writer = csv.writer(csvfile)
    csv_rd = os.listdir(path_csv_rd)
    print("##### 得到的特征均值 / The generated average values of features stored in: #####")

    for i in range(len(csv_rd)):
        feature_mean = compute_the_mean(path_csv_rd + csv_rd[i])
        # print(feature_mean)
        print(path_csv_rd + csv_rd[i])
        writer.writerow(feature_mean)

max_stars_repo_name: janlle/dlib-face-recognition

max_issues_repo_name: janlle/dlib-face-recognition

max_forks_repo_name: janlle/dlib-face-recognition


text:
from sklearn.base import BaseEstimator, ClassifierMixin, RegressorMixin, ClusterMixin
from rpy2 import robjects
from rpy2.robjects.packages import importr
import rpy2.robjects.numpy2ri
import numpy as np

class NNSRPY2Estimator(BaseEstimator):
    """An example of classifier"""

    def __init__(
            self,
            factor_2_dummy=True,
            order=None,
            stn=0.96,
            dim_red_method=None,
            tau=None,
            type=None,
            location="top",
            std_errors=False,
            confidence_interval=None,
            threshold=0,
            n_best=None,
            noise_reduction="mean",
            norm=None,
            dist="L2",
            ncores=None,
            multivariate_call=False
    ):
        """
        Called when initializing the classifier
        """
        # TODO: install R package automatically: https://rpy2.readthedocs.io/en/latest/introduction.html#getting-started

        self.factor_2_dummy = factor_2_dummy
        self.order = order
        self.stn = stn
        self.dim_red_method = dim_red_method
        self.tau = tau
        self.type = type
        self.location = location
        self.std_errors = std_errors
        self.confidence_interval = confidence_interval
        self.threshold = threshold
        self.n_best = n_best
        self.noise_reduction = noise_reduction
        self.norm = norm
        self.dist = dist
        self.ncores = ncores
        self.multivariate_call = multivariate_call

    def _retnull(self, value):
        if value is None:
            return robjects.NULL
        elif type(value) is bool:
            return robjects.vectors.BoolVector([False])
        return value

    def _nns_get_parameters(self, return_value=False):
        return {
            "factor_2_dummy": self._retnull(self.factor_2_dummy),
            "order": self._retnull(self.order),
            "stn": self._retnull(self.stn),
            "dim_red_method": self._retnull(self.dim_red_method),
            "tau": self._retnull(self.tau),
            "type": self._retnull(self.type),
            "location": self._retnull(self.location),
            "return_values": self._retnull(return_value),
            "plot": self._retnull(False),
            "plot_regions": self._retnull(False),
            "residual_plot": self._retnull(False),
            "std_errors": self._retnull(self.std_errors),
            "confidence_interval": self._retnull(self.confidence_interval),
            "threshold": self._retnull(self.threshold),
            "n_best": self._retnull(self.n_best),
            "noise_reduction": self._retnull(self.noise_reduction),
            "norm": self._retnull(self.norm),
            "dist": self._retnull(self.dist),
            "ncores": self._retnull(self.ncores),
            "multivariate_call": self._retnull(self.multivariate_call)
        }

    def fit(self, X, y=None):
        """
        This should fit classifier. All the "work" should be done here.

        Note: assert is not a good choice here and you should rather
        use try/except blog with exceptions. This is just for short syntax.
        """
        self.r_NNS_ = importr('NNS')  # require(NNS)
        rpy2.robjects.numpy2ri.activate()
        self.X_ = X
        self.y_ = y
        return self

    def predict(self, X, y=None):
        if not hasattr(self, "r_NNS_"):
            raise RuntimeError("You must train estimator before predicting data!")
        params = self._nns_get_parameters(True)
        params['point_est'] = X
        ret = self.r_NNS_.NNS_reg(self.X_, self.y_, **params)
        for i in range(len(ret.names)):
            if ret.names[i] == 'Point.est':
                return np.array(ret[i])
        raise RuntimeError("Can't find return Point.est value")

class NNSRPY2Classifier(NNSRPY2Estimator, ClassifierMixin):
    def __init__(self, **kwargs):
        kwargs['type'] = "CLASS"
        super().__init__(**kwargs)

    def predict_proba(self, X, y=None):
        # No not flor classification
        # Could run reg regression on it however
        # 0,1 classes like logistic regression works with type = NULL
        # Set type = NULL and noise.reduction = “mode”
        error, ret = None, None
        old_type, old_noise_reduction = self.type, self.noise_reduction
        try:
            self.type = None
            self.noise_reduction = "mode"
            ret = np.clip(self.predict(X, y), 0, 1)     # clip >=0, <=1
        except Exception as e:
            error = e
        # set back to original value
        self.type, self.noise_reduction = old_type, old_noise_reduction
        if error is not None:
            raise error
        return ret

class NNSRPY2Regressor(NNSRPY2Estimator, RegressorMixin):
    def __init__(self, **kwargs):
        kwargs['type'] = None
        super().__init__(**kwargs)

# TODO: check if we could export a cluster class (unsupervised algorithm)
class NNSRPY2Clusterer(NNSRPY2Estimator, ClusterMixin):
    def predict(self, X, y=None):
        if not hasattr(self, "r_NNS_"):
            raise RuntimeError("You must train estimator before predicting data!")
        params = self._nns_get_parameters(True)
        params['point_est'] = X
        ret = self.r_NNS_.NNS_reg(self.X_, self.y_, **params)
        for i in range(len(ret.names)):
            if ret.names[i] == 'Point.est.NNS.ID':     ## !! should return the NNS.ID of the Point.est value !!
                return np.array(ret[i])
        raise RuntimeError("Can't find return Point.est.NNS.ID value")

#if __name__ == '__main__':
#    model = NNSRPY2Clusterer()
#    x = np.array([1, 2, 3, 4])
#    x_new = x + 1
#    y = x ** 3
#    y_new = x_new + 1
#    model.fit(x, y)
#    print(model.predict(x, y))