text:
# python standard library
import logging
import itertools as it

# numpy/scipy
import numpy as np
from scipy import ndimage as nd
from scipy.special import factorial
from numpy.linalg import det
try:
    from scipy.spatial import Delaunay
except ImportError:
    logging.warning('Unable to load scipy.spatial.Delaunay. '+
        'Convex hull features not available.')

# local imports
from . import base

class Manager(base.Null):
    def __init__(self, *args, **kwargs):
        super(Manager, self).__init__()

    def write_fm(self, json_fm={}):
        if 'feature_list' not in json_fm:
            json_fm['feature_list'] = []
        json_fm['feature_list'].append('convex-hull')
        json_fm['convex-hull'] = {}
        return json_fm

    def convex_hull_ind(self, g, n1, n2=None):
        m = np.zeros_like(g.watershed);
        if n2 is not None:
            m.ravel()[g.boundary(n1, n2)] = 1
        else:
            m.ravel()[list(g.extent(n1))] = 1
        m = m - nd.binary_erosion(m) #Only need border
        ind = np.array(np.nonzero(m)).T
        return ind

    def convex_hull_vol(self, ind, g):
        # Compute the convex hull of the region
        try:
            tri = Delaunay(ind)
        except:
            # Just triangulate bounding box
            mins = ind.min(axis=0)
            maxes = ind.max(axis=0)
            maxes[maxes==mins] += 1
            ind = np.array(list(it.product(*tuple(np.array([mins,maxes]).T))))
            tri = Delaunay(ind)
        vol = 0
        for simplex in tri.vertices:
            pts = tri.points[simplex].T
            pts = pts - np.repeat(pts[:,0][:, np.newaxis], pts.shape[1], axis=1)
            pts = pts[:,1:]
            vol += abs(1/float(factorial(pts.shape[0])) * det(pts))
            return vol,tri

    def create_node_cache(self, g, n):
        vol, tri = self.convex_hull_vol(self.convex_hull_ind(g,n), g)
        return np.array([tri,vol])

    def create_edge_cache(self, g, n1, n2):
        vol, tri = self.convex_hull_vol(self.convex_hull_ind(g,n1,n2), g)
        return np.array([tri,vol])

    def update_node_cache(self, g, n1, n2, dst, src):
        tri1 = src[0]
        tri2 = dst[0]
        ind1 = tri1.points[np.unique(tri1.convex_hull.ravel())]
        ind2 = tri2.points[np.unique(tri2.convex_hull.ravel())]
        allind = np.concatenate((ind1,ind2))
        vol, tri = self.convex_hull_vol(allind, g)
        dst = np.array([tri,vol])

    def update_edge_cache(self, g, e1, e2, dst, src):
        tri1 = src[0]
        tri2 = dst[0]
        ind1 = tri1.points[np.unique(tri1.convex_hull.ravel())]
        ind2 = tri2.points[np.unique(tri2.convex_hull.ravel())]
        allind = np.concatenate((ind1,ind2))
        vol, tri = self.convex_hull_vol(allind, g)
        dst = np.array([tri,vol])

    def compute_node_features(self, g, n, cache=None):
        if cache is None:
            cache = g.nodes[n][self.default_cache]
        convex_vol = cache[1]
        features = []
        features.append(convex_vol)
        features.append(convex_vol/float(g.nodes[n]['size']))
        return np.array(features)

    def compute_edge_features(self, g, n1, n2, cache=None):
        if cache is None:
            cache = g.edges[n1, n2][self.default_cache]
        convex_vol = cache[1]

        features = []
        features.append(convex_vol)
        features.append(convex_vol / len(g.boundary(n1, n2)))
        return np.array(features)

    def compute_difference_features(self,g, n1, n2, cache1=None, cache2=None):
        if cache1 is None:
            cache1 = g.nodes[n1][self.default_cache]
        tri1 = cache1[0]
        convex_vol1 = cache1[1]

        if cache2 is None:
            cache2 = g.nodes[n2][self.default_cache]
        tri2 = cache2[0]
        convex_vol2 = cache2[1]

        ind1 = tri1.points[np.unique(tri1.convex_hull.ravel())]
        ind2 = tri2.points[np.unique(tri2.convex_hull.ravel())]
        allind = np.concatenate((ind1,ind2))
        convex_vol_both, tri_both = self.convex_hull_vol(allind, g)

        vol1 = float(g.nodes[n1]['size'])
        vol2 = float(g.nodes[n2]['size'])
        volborder = float(len(g.boundary(n1, n2)))
        volboth = vol1+vol2

        features = []
        features.append(abs(convex_vol1/vol1 - convex_vol2/vol2))
        features.append(abs(convex_vol1/vol1 - convex_vol_both/volboth))
        features.append(abs(convex_vol2/vol2 - convex_vol_both/volboth))
        features.append(abs(convex_vol_both/volboth))
        features.append((convex_vol1*vol2)/(convex_vol2*vol1))
        features.append(volborder/vol1)
        features.append(volborder/vol2)
        features.append(volborder/volboth)

        return np.array(features)

max_stars_repo_name: Pandinosaurus/gala

max_issues_repo_name: Pandinosaurus/gala

max_forks_repo_name: Pandinosaurus/gala

reason: import numpy,from numpy,from scipy


text:
import os
import numpy as np
from plotting.visualize_n_link import visualize_mixture
import matplotlib.pyplot as plt
from scipy.stats import multivariate_normal as normal
from experiments.GMM import GMM as GMM

def process_ess(basepath, force_recompute=False):
    for dirName, subdirList, fileList in os.walk(basepath):
        all_time_stamps = []
        fnames = []
        all_samples = []
        all_fevals = []
        for fname in sorted(fileList):
            if not force_recompute and os.path.exists(dirName+'/'+fname+'_processed_data'+ ".npz"):
                print("data was already processed " + dirName)
                continue
            else:
                print("processing: " + fname)
            if '_processed_data' not in fname and fname.endswith(".npz"):
                tmp = np.load(dirName + '/' + fname)
                samples = np.array(tmp['samples'].tolist()).reshape(1,-1,samples.shape[-1])
                all_samples.append(samples)
                timestamps = np.array(tmp['timestamps'].tolist())
                timestamps = timestamps[1:] - timestamps[0]
                all_time_stamps.append(timestamps)
                fnames.append(fname)
                fevals = tmp['nfevals']
                all_fevals.append(fevals)
                np.savez(dirName+'/'+fname+'_processed_data', samples= samples, timestamps=timestamps, fnames=[fname], fevals=fevals)
    return

def process_vips(basepath, force_recompute=False):
    for dirName, subdirList, fileList in os.walk(basepath):
        if os.path.exists(dirName+"/processed_data.npz") and not force_recompute:
            print("data was already processed " + dirName)
            continue

        timestamps = []
        fnames = []
        fevals = []
        samples = []
        fileFound = False
        for fname in sorted(fileList):
            if not force_recompute and os.path.exists(dirName+'/'+fname+'_processed_data'+ ".npz"):
                print("data was already processed " + fname)
                continue
            if not fileFound:
                print("processing " + dirName)
            fileFound = True
            if '_processed_data' not in fname and fname.endswith(".npz") and "gmm_dump" in fname:
                tmp = np.load(dirName + '/' + fname)
                weights = tmp['weights']
                means = tmp['means']
                covs = tmp['covs']
                mixture = GMM(means.shape[1])
                for mean, cov in zip(means, covs):
                    mixture.add_component(mean, cov)
                mixture.set_weights(weights)
                samples.append(mixture.sample(2000))
                timestamps.append(tmp['timestamps'][-1])
                fnames.append(fname)
                fevals.append(tmp['fevals'])
        if fileFound:
            timestamps = np.hstack(timestamps)
            permutation = np.argsort(timestamps)
            timestamps = timestamps[permutation]
            samples = np.array(samples)[permutation]
            fevals = np.hstack(fevals).astype(np.int32)[permutation]
            fnames = np.array(fnames)[permutation]
            if os.path.exists(dirName+'/target_mixture.npz'):
                target_mixture = np.load(dirName+'/target_mixture.npz')
                np.savez(dirName + '/processed_data', samples=samples, timestamps=timestamps, fnames=fnames,
                         fevals=fevals, true_means=target_mixture['true_means'] , true_covs=target_mixture['true_covs'] )
            else:
                np.savez(dirName+'/processed_data', samples= samples, timestamps=timestamps, fnames=fnames, fevals=fevals)
    return

def process_hmc(basepath, force_recompute=False):
    for dirName, subdirList, fileList in os.walk(basepath):
        fnames = []
        all_samples = []
        all_timestamps = []
        all_fevals = []
        for fname in sorted(fileList):
            if not force_recompute and os.path.exists(dirName+'/'+fname[:-4]+'_processed_data'+ ".npz"):
                print("data was already processed " + fname)
                continue
            if 'iter' not in fname and '_processed_data' not in fname and fname.endswith(".npz"):
                print("processing " + fname)
                tmp = np.load(dirName + '/' + fname)
                samples = np.array(tmp['samples'].tolist())
                all_samples.append(samples)
                timestamps = np.array(tmp['timestamps'].tolist())
                timestamps = (timestamps - timestamps[0])[1:]
                all_timestamps.append(timestamps)
                fnames.append(fname)
                fevals = tmp['nfevals']
                all_fevals.append(fevals)
                np.savez(dirName+'/'+fname[:-4]+'_processed_data', samples= samples, timestamps=timestamps, fnames=[fname], fevals=fevals)
            #else:
            #    print("skipping: " + fname)
    return

def process_nuts(basepath, force_recompute=False):
    for dirName, subdirList, fileList in os.walk(basepath):
        foundFile = False
        all_timestamps = []
        all_fnames = []
        all_fevals = []
        all_samples = []
        if not force_recompute and os.path.exists(dirName + '/processed_data' + ".npz"):
            print("data was already processed " + dirName)
            continue
        else:
            print("processing: " + dirName)
        for fname in sorted(fileList):
            if '_processed_data' not in fname and fname.endswith(".npz"):
                foundFile = True
                tmp = np.load(dirName + '/' + fname)
                samples = np.array(tmp['samples'].tolist()).reshape((-1,samples.shape[-1]))
                all_samples.append(samples)
                this_time_stamps = np.array(tmp['walltime'].tolist())
                all_fevals.append(np.array(tmp['n_evals'].tolist()))
                all_timestamps.append(this_time_stamps)
                all_fnames.append(fname)
        if foundFile:
            sorted_indices = np.argsort(all_fevals)
            all_fevals = np.hstack(all_fevals)[sorted_indices]
            all_timestamps = np.array(all_timestamps)[sorted_indices][-1]
            all_fnames = np.array(all_fnames)[sorted_indices]
            all_samples = np.array(all_samples)[sorted_indices]
            np.savez(dirName+'_processed_data', fevals=all_fevals, timestamps=all_timestamps, samples=all_samples, fnames=all_fnames)
    return

def process_vboost(basepath, force_recompute=False):
    import pickle
    for dirName, subdirList, fileList in os.walk(basepath):
        foundFile = False
        all_fevals = []
        all_timestamps = []
        all_samples = []
        all_fnames = []
        all_pickl_fnames = []
        all_pickl_means = []
        if not force_recompute and os.path.exists(dirName + '_processed_data' + ".npz"):
            print("data was already processed " + dirName)
            continue
        else:
            print("processing: " + dirName)
        for fname in sorted(fileList):
            if not 'processed_data' in fname and fname.endswith(".npz"):
                foundFile = True
                tmp = np.load(dirName + '/' + fname)
                samples = tmp['arr_0']
                timestamps = tmp['arr_1']
                nfevals = tmp['arr_2']
                all_fevals.append(nfevals)
                all_timestamps.append(timestamps)
                all_samples.append(samples)
                all_fnames.append(fname)
            if fname.endswith(".pkl"):
                all_pickl_fnames.append(fname)
                f = open(dirName + '/' + fname)
                data = pickle.load(f)
                means = np.empty((len(data),samples.shape[-1]))
                for i in range(0, len(data)):
                    means[i] = data[i][1][:samples.shape[-1]]
                all_pickl_means.append(means)
        if foundFile:
            sorted_indices = np.argsort(all_fevals)
            all_fevals = np.hstack(all_fevals)[sorted_indices]
            all_timestamps = np.array(all_timestamps)[sorted_indices][-1]
            all_timestamps = (all_timestamps - all_timestamps[0])[1:]
            all_fnames = np.array(all_fnames)[sorted_indices]
            all_samples = np.array(all_samples)[sorted_indices]
            all_pickl_means = np.array(all_pickl_means)[sorted_indices]
            np.savez(dirName+'_processed_data', fevals=all_fevals, timestamps=all_timestamps, samples=all_samples, fnames=all_fnames, means=all_pickl_means)
    return

def process_svgd(basepath, force_recompute=False):
    for dirName, subdirList, fileList in os.walk(basepath):
        foundFile = False
        all_timestamps = []
        all_fnames = []
        n_evals = []
        all_samples = []
        true_means = None
        true_covs = None
        if not force_recompute and os.path.exists(dirName + '_processed_data' + ".npz"):
            print("data was already processed " + dirName)
            continue
        else:
            print("processing: " + dirName)
        for fname in sorted(fileList):
            if '_processed_data' not in fname and 'iter' in fname and fname.endswith(".npz"):
                foundFile = True
                tmp = np.load(dirName + '/' + fname)
                samples = tmp['samples']
                all_samples.append(samples)
                this_time_stamps = tmp['timestamp']
                try:
                    n_evals.append(tmp['nfevals'])
                except:
                    print('debug')
                all_timestamps.append(this_time_stamps)
                all_fnames.append(fname)
                if os.path.exists(dirName + '/' + "svgd.npz"):
                    tmp = np.load(dirName + '/' + "svgd.npz")
                    if "true_means" in tmp.keys():
                        true_covs = tmp['true_covs']
                        true_means = tmp['true_means']
            elif 'initial' in fname:
                start = np.load(dirName + '/' + fname)['timestamp']
        if foundFile:
            all_fevals = np.array(n_evals)
            sorted_indices = np.argsort(all_fevals)
            all_fevals = all_fevals[sorted_indices]
            all_timestamps = np.hstack(all_timestamps) - start
            all_timestamps = all_timestamps[sorted_indices]
            all_fnames = np.array(all_fnames)[sorted_indices]
            all_samples = np.array(all_samples)[sorted_indices]
            if true_means is not None:
                np.savez(dirName+'_processed_data', fevals=all_fevals, timestamps=all_timestamps, samples=all_samples,
                         fnames=all_fnames, true_means=true_means, true_covs=true_covs)
                print("found target gmm")
            else:
                np.savez(dirName+'_processed_data', fevals=all_fevals, timestamps=all_timestamps, samples=all_samples, fnames=all_fnames)
    return

def process_slice(basepath, force_recompute=False):
    for dirName, subdirList, fileList in os.walk(basepath):
        foundFile = False
        all_timestamps = []
        all_fnames = []
        n_evals = []
        all_samples = []
        for fname in sorted(fileList):
            if not force_recompute and os.path.exists(dirName+'/'+fname+'_processed_data'+ ".npz"):
                print("data was already processed " + fname)
                continue
            else:
                print("processing: " + fname)
            if '_processed_data' not in fname and fname.endswith(".npz"):
                foundFile = True
                tmp = np.load(dirName + '/' + fname)
                samples =  np.array(tmp['samples'].tolist()).transpose()
                all_samples.append(samples)
                this_time_stamps = np.array(tmp['wallclocktime'].tolist())
                all_timestamps.append(this_time_stamps)
                all_fnames.append(fname)
                try:
                    n_evals.append(tmp['n_fevals'])
                except:
                    print("debug")
                all_fevals = np.array(n_evals)
                sorted_indices = np.argsort(all_fevals)
                all_fevals = all_fevals[sorted_indices]
                all_timestamps = np.array(all_timestamps)[sorted_indices][-1]
                all_fnames = np.array(all_fnames)[sorted_indices]
                all_samples = np.array(all_samples)[sorted_indices]
                np.savez(dirName + '/' + fname + '_processed_data', fevals=all_fevals, timestamps=all_timestamps, samples=all_samples,
                         fnames=all_fnames)
    return

def process_npvi(basepath, force_recompute=False):
    for dirName, subdirList, fileList in os.walk(basepath):
        all_timestamps = []
        all_fnames = []
        all_fevals = []
        mus = []
        sigmas = []
        all_samples = []
        final_mus = []
        final_covs = []
        true_means = None
        true_covs = None
        found_files = False
        if not force_recompute and os.path.exists(dirName + '_processed_data' + ".npz"):
            print("data was already processed " + dirName)
            continue
        else:
            print("processing: " + dirName)
        for fname in sorted(fileList):
            if '_processed_data' in fname:
                continue
            if 'iter' in fname and fname.endswith(".npz"):
                found_files = True
                tmp = np.load(dirName + '/' + fname)
                if true_means is None and 'true_means' in tmp.keys():
                    true_means = tmp['true_means']
                    true_covs = tmp['true_covs']
                all_fevals.append(tmp['n_feval'])
                this_time_stamps = tmp['timestamps']
                all_timestamps.append(this_time_stamps)
                all_fnames.append(fname)
                mus.append(tmp['mu'])
                sigmas.append(tmp['sigma'])
                samples = []
                for i in range(len(mus[-1])):
                    samples.append(
                        normal(mus[-1][i], sigmas[-1][i] * np.eye(len(mus[-1][i]))).rvs(int(2000 / len(mus[-1]))))
                all_samples.append(np.vstack(samples))
            elif '-comp' in fname:
                tmp = np.load(dirName + '/' + fname)
                final_mus = np.array(tmp['arr_1'].tolist())
                final_covs = [np.eye(len(final_mus[0])) * sigma2 for sigma2 in np.array(tmp['arr_2'].tolist())]
        if found_files:
            all_fevals = np.array(all_fevals)
            sorted_indices = np.argsort(all_fevals)
            all_fevals = all_fevals[sorted_indices]
            all_timestamps = np.array(all_timestamps)[sorted_indices][-1]
            all_timestamps = (all_timestamps - all_timestamps[0])[1:]
            all_fnames = np.array(all_fnames)[sorted_indices]
            all_samples = np.array(all_samples)[sorted_indices]
            if true_means is None:
                np.savez(dirName+'_processed_data', fevals=all_fevals, timestamps=all_timestamps, fnames=all_fnames,
                    final_mus=final_mus, final_covs=final_covs, mus=mus, sigmas=sigmas, samples=all_samples)
            else:
                np.savez(dirName+'_processed_data', fevals=all_fevals, timestamps=all_timestamps, fnames=all_fnames,
                    final_mus=final_mus, final_covs=final_covs, mus=mus, sigmas=sigmas, samples=all_samples,
                         true_means=true_means, true_covs=true_covs)
    return

def process_path(path, force_recompute=False):
    process_vips(path + 'vips', force_recompute)
    process_vboost(path + 'vboost', force_recompute)
    process_npvi(path + 'npvi', force_recompute)
    process_hmc(path + 'hmc', force_recompute)
    process_svgd(path + 'svgd', force_recompute)

def process_3link(force_recompute=False):
    process_path('../ICML/3link/', force_recompute)

def process_10link(force_recompute=False):
    process_path('../ICML/10link/', force_recompute)

def process_GMM(force_recompute=False):
    process_path('../ICML/gmm/', force_recompute)

def process_frisk(force_recompute=False):
    process_path('../ICML/frisk/', force_recompute)

def process_german_credit(force_recompute=False):
    process_path('../ICML/german_credit/', force_recompute)

def process_breast_cancer(force_recompute=False):
    process_path('../ICML/breast_cancer/', force_recompute)

if __name__ == "__main__":
#    process_npvi("../ICML/breast_cancer/npvi", True)
    process_vboost("/data/icml/learnToSample/data/ICML/10link/vboost/run1/rank10/seed0/", True)
    process_vips("/data/icml/learnToSample/data/ICML/frisk/vips/run3")
    process_vboost("/data/icml/learnToSample/data/ICML/frisk/vboost/run2/", True)
    process_vboost("/data/icml/learnToSample/data/ICML/german_credit/vboost/run1/", True)
    process_svgd("/data/icml/learnToSample/data/ICML/breast_cancer/svgd/run1")
    process_german_credit()
    process_frisk()
    process_breast_cancer()
    process_svgd("../ICML/10link/svgd/run2")
    process_vips("/data/icml/learnToSample/data/ICML/gmm/vips/run4_MT/explorative40_2")
    process_npvi("../ICML/gmm/npvi", True)
   # process_vips('../ICML/gmm/vips', True)
    process_10link()
    process_3link()
    process_german_credit()
    process_breast_cancer()
    process_frisk()
    process_GMM()

    print('wait')

ext: py

reason: import numpy,from scipy


text:
import numpy as np
import citysim3d.envs

from visual_dynamics.envs import Panda3dEnv
from visual_dynamics.spaces import Space, BoxSpace, TranslationAxisAngleSpace
from visual_dynamics.utils.config import ConfigObject

class SimpleQuadPanda3dEnv(citysim3d.envs.SimpleQuadPanda3dEnv, Panda3dEnv):
    def _get_config(self):
        config = super(SimpleQuadPanda3dEnv, self)._get_config()
        car_action_space = self.car_action_space
        if not isinstance(car_action_space, ConfigObject):
            car_action_space = Space.create(car_action_space)
        config.update({'action_space': self.action_space,
                       'sensor_names': self.sensor_names,
                       'camera_size': self.camera_size,
                       'camera_hfov': self.camera_hfov,
                       'offset': self.offset.tolist(),
                       'car_env_class': self.car_env_class,
                       'car_action_space': car_action_space,
                       'car_model_names': self.car_model_names})
        return config

class Point3dSimpleQuadPanda3dEnv(SimpleQuadPanda3dEnv):
    def __init__(self, action_space, **kwargs):
        super(Point3dSimpleQuadPanda3dEnv, self).__init__(action_space, **kwargs)
        self._observation_space.spaces['pos'] = BoxSpace(-np.inf, np.inf, shape=(3,))

    def observe(self):
        obs = super(Point3dSimpleQuadPanda3dEnv, self).observe()
        obs['pos'] = np.array(self.car_node.getTransform(self.camera_node).getPos())
        return obs

def main():
    import os
    import numpy as np
    from panda3d.core import loadPrcFile

    assert "CITYSIM3D_DIR" in os.environ
    loadPrcFile(os.path.expandvars('${CITYSIM3D_DIR}/config.prc'))

    action_space = TranslationAxisAngleSpace(np.array([-20, -10, -10, -1.5707963267948966]),
                                             np.array([20, 10, 10, 1.5707963267948966]))
    sensor_names = ['image', 'depth_image']
    env = SimpleQuadPanda3dEnv(action_space, sensor_names)

    import time
    import cv2
    start_time = time.time()
    frames = 0

    from visual_dynamics.policies.quad_target_policy import QuadTargetPolicy
    pol = QuadTargetPolicy(env, (12, 18), (-np.pi / 2, np.pi / 2))

    obs = env.reset()
    pol.reset()
    image, depth_image = obs
    while True:
        try:
            env.render()
            image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
            cv2.imshow('Image window', image)
            key = cv2.waitKey(1)
            key &= 255
            if key == 27 or key == ord('q'):
                print("Pressed ESC or q, exiting")
                break

            quad_action = pol.act(obs)
            obs, _, _, _ = env.step(quad_action)
            image, depth_image = obs
            frames += 1
        except KeyboardInterrupt:
            break

    end_time = time.time()
    print("average FPS: {}".format(frames / (end_time - start_time)))

if __name__ == "__main__":
    main()

reason: import numpy


text:
import cv2
import os
import glob
import numpy as np
import collections
import argparse
import shutil

# Author : Ishaan Bhat
# Email : i.r.bhat@student.tue.nl

def computeMatchesORB(img1,img2):
    orb = cv2.ORB_create()
    sim_metric = 0
    # Keypoints and descriptors for the objects
    kp_ref, des_ref = orb.detectAndCompute(img1,None)
    kp_test, des_test = orb.detectAndCompute(img2,None)
    # Use Brute Force matcher + Hamming distance since binary features
    bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=False)
    matches = bf.match(des_ref,des_test)
    # Need to draw only good matches, so create a mask
    goodMatches = []
    # ratio test as per Lowe's paper
    for i,m in enumerate(matches):
        # For threshold see: https://stackoverflow.com/questions/22857398/matching-orb-features-with-a-threshold
        if m.distance < 64:
            goodMatches.append(m)
            sim_metric = sim_metric + 1
    img3 = cv2.drawMatches(img1,kp_ref,img2,kp_test,goodMatches,outImg=None,flags=2)
    return sim_metric

def computeMatchesSIFT(img1,img2):
    sift = cv2.xfeatures2d.SIFT_create()
    sim_metric = 0
    # Keypoints and descriptors for the objects
    kp_ref, des_ref = sift.detectAndCompute(img1,None)
    kp_test, des_test = sift.detectAndCompute(img2,None)
    # FLANN parameters
    FLANN_INDEX_KDTREE = 0
    index_params = dict(algorithm = FLANN_INDEX_KDTREE, trees = 5)
    search_params = dict(checks=50)   # or pass empty dictionary
    flann = cv2.FlannBasedMatcher(index_params,search_params)
    matches = flann.knnMatch(des_ref,des_test,k=2)
    # Need to draw only good matches, so create a mask
    matchesMask = [[0,0] for i in range(len(matches))]
    # ratio test as per Lowe's paper
    for i,(m,n) in enumerate(matches):
        if m.distance < 0.7*n.distance:
            matchesMask[i]=[1,0]
            sim_metric = sim_metric + 1
    return sim_metric

# Functions to compute metrics to be added here ...

def returnScore(plot = False):
    parser = argparse.ArgumentParser()
    parser.add_argument("--images",type=str,default="completions",help="Folder name for images")
    parser.add_argument("--num_images",type=int,default=10,help="Number of images to be analyzed")
    args = parser.parse_args()

    # Create a dir to store results
    if not os.path.exists(os.path.join(os.getcwd(),args.images,'results')):
        os.makedirs(os.path.join(os.getcwd(),args.images,'results'))
    else:
        shutil.rmtree(os.path.join(os.getcwd(),args.images,'results'))
        os.makedirs(os.path.join(os.getcwd(),args.images,'results'))

    resultPath = os.path.join(os.getcwd(),args.images,'results')

    # For each image completed
    for img in range(0,args.num_images):
        imagePath = os.path.join(os.getcwd(),args.images,'{:04d}'.format(img),'completed','*.jpg')
        refFile = open(os.path.join(os.getcwd(),args.images,'before_{:04d}.jpg'.format(img)))
        refImg = cv2.imread(refFile.name)
        refMatches = computeMatchesSIFT(refImg,refImg)
        images = glob.glob(imagePath)
        metricDict = {}
        print('Collecting metrics for {} image'.format(img))
        # For each checkpointed iteration of completion (per-image)
        for image in images:
            testImg = cv2.imread(image)
            # Computing the similarity metric
            simSIFT = computeMatchesSIFT(refImg,testImg)
            simScore = float(simSIFT)/float(refMatches)
            pathSet = image.split(os.sep)
            idx = pathSet[-1].split(".")
            # Creating a dictionary with key:value as  IterationID:SimilarityScore
            metricDict[int(idx[0])] = simScore
            print('For Image {}, iteration {} : SIFT sim metric = {}'.format(img,idx[0],simScore))

            # Add code to compute alternative metrics here (PSNR, MSE etc ..)
            # Store these in a dictionary similar to as done above
            # Order it once metrics for all intermediate images are calculated (as shown below)

        # Sort the dict based on key which is the iteration ID
        if plot is True:

            import matplotlib.pylab as plt

            orderedMetrics = sorted(metricDict.items())
            x,y = zip(*orderedMetrics)
            fig = plt.figure()
            ax = fig.add_subplot(111)
            # Plot the ordered dictionary
            ax.plot(x,y)
            plt.ylabel('Similarity Score (SIFT)')
            plt.xlabel('Iteration')

            # Save the graph as an image
            figPath = os.path.join(resultPath,'res_{:04d}.png'.format(img))
            plt.savefig(figPath)

if __name__ == "__main__":
    returnScore()


text:
import numpy as np

from draw_graphic import drawGraphic
from fibonacci import get_extremum_of_function_by_fibonacci_method
from golden_section import golden_section
from table import print_table

func = lambda x: x ** 2 + 2 * np.exp(x)
# так как у функция возврастающая на положительных иксах и убывающая на отрицательных
# проще найти минимумы (унимодальная функция)

if __name__ == '__main__':
    a = int(input('Enter left endside: '))
    b = int(input('Enter right endside: '))
    eps = float(input('Enter accuracy: '))
    qa = input('Do u want to set N - number of iterations for fibonacci method [y/n]: ')
    if qa == 'y':
        n = int(input('Enter number of iterations for fibonacci method: '))
    else:
        n = None

    gss = golden_section(func, a, b, eps)
    fib = get_extremum_of_function_by_fibonacci_method(func, a, b, eps, n)
    print_table(gss, fib)
    drawGraphic(a, b, func)


text:
import os, sys
import cv2
import numpy as np
import pickle
import time
import yaml
import trimesh

import torch

import xml.etree.ElementTree as ET

from sklearn.decomposition import IncrementalPCA

# from npbg.gl.import_ import import_model3d
from npbg.gl.programs import NNScene

class TicToc:
    def __init__(self):
        self.tic_toc_tic = None

    def tic(self):
        self.tic_toc_tic = time.time()

    def toc(self):
        assert self.tic_toc_tic, 'You forgot to call tic()'
        return (time.time() - self.tic_toc_tic) * 1000

    def tocp(self, str):
        print(f"{str} took {self.toc():.4f}ms")

    @staticmethod
    def print_timing(timing, name=''):
        print(f'\n=== {name} Timimg ===')
        for fn, times in timing.items():
            min, max, mean, p95 = np.min(times), np.max(times), np.mean(times), np.percentile(times, 95)
            print(f'{fn}:\tmin: {min:.4f}\tmax: {max:.4f}\tmean: {mean:.4f}ms\tp95: {p95:.4f}ms')

class FastRand:
    def __init__(self, shape, tform, bank_size):
        bank = []
        for i in range(bank_size):
            p = np.random.rand(*shape)
            p = tform(p)
            bank.append(p)

        self.bank = bank

    def toss(self):
        i = np.random.randint(0, len(self.bank))
        return self.bank[i]

def cv2_write(fn, x):
    x = np.clip(x, 0, 1) * 255
    x = x[..., :3][..., ::-1]
    cv2.imwrite(fn, x.astype(np.uint8))

def to_numpy(x, float16=False, flipv=True):
    if not isinstance(x, np.ndarray):
        x = x.detach().cpu().numpy()

    if float16:
        x = x.astype(np.float16)

    if flipv:
        x = x[::-1].copy()

    return x

def pca_color(tex, save='', load=''):
    tex = tex[0].transpose(1, 0)
    if load and os.path.exists(load):
        print('loading...')
        with open(load,'rb') as f:
            pca=pickle.load(f)
        print('applying...')
        res=pca.transform(tex)
    else:
        pca=IncrementalPCA(n_components=3, batch_size=64)
        print('applying...')
        res=pca.fit_transform(tex)
    if save and save != load:
        with open(save,'wb') as f:
            pickle.dump(pca,f)
    # pca_color_n=(pca_color - pca_color.min()) / (pca_color.max() - pca_color.min())
    # return res.transpose(1, 0)[None]
    return res

def crop_proj_matrix(pm, old_w, old_h, new_w, new_h):
    # NOTE: this is not precise
    old_cx = old_w / 2
    old_cy = old_h / 2
    new_cx = new_w / 2
    new_cy = new_h / 2

    pm_new = pm.copy()
    pm_new[0,0] = pm[0,0]*old_w/new_w
    pm_new[0,2] = (pm[0,2]-1)*old_w*new_cx/old_cx/new_w + 1
    pm_new[1,1] = pm[1,1]*old_h/new_h
    pm_new[1,2] = (pm[0,2]+1)*old_h*new_cy/old_cy/new_h - 1
    return pm_new

def recalc_proj_matrix_planes(pm, new_near=.01, new_far=1000.):
    depth = float(new_far - new_near)
    q = -(new_far + new_near) / depth
    qn = -2 * (new_far * new_near) / depth

    out = pm.copy()

    # Override near and far planes
    out[2, 2] = q
    out[2, 3] = qn

    return out

def get_proj_matrix(K, image_size, znear=.01, zfar=1000.):
    fx = K[0,0]
    fy = K[1,1]
    cx = K[0,2]
    cy = K[1,2]
    width, height = image_size
    m = np.zeros((4, 4))
    m[0][0] = 2.0 * fx / width
    m[0][1] = 0.0
    m[0][2] = 0.0
    m[0][3] = 0.0

    m[1][0] = 0.0
    m[1][1] = 2.0 * fy / height
    m[1][2] = 0.0
    m[1][3] = 0.0

    m[2][0] = 1.0 - 2.0 * cx / width
    m[2][1] = 2.0 * cy / height - 1.0
    m[2][2] = (zfar + znear) / (znear - zfar)
    m[2][3] = -1.0

    m[3][0] = 0.0
    m[3][1] = 0.0
    m[3][2] = 2.0 * zfar * znear / (znear - zfar)
    m[3][3] = 0.0

    return m.T

def rescale_K(K_, sx, sy, keep_fov=True):
    K = K_.copy()
    K[0, 2] = sx * K[0, 2]
    K[1, 2] = sy * K[1, 2]
    if keep_fov:
        K[0, 0] = sx * K[0, 0]
        K[1, 1] = sy * K[1, 1]
    return K

def crop_intrinsic_matrix(K, old_size, new_size):
    K = K.copy()
    K[0, 2] = new_size[0] * K[0, 2] / old_size[0]
    K[1, 2] = new_size[1] * K[1, 2] / old_size[1]
    return K

def intrinsics_from_xml(xml_file):
    root = ET.parse(xml_file).getroot()
    calibration = root.find('chunk/sensors/sensor/calibration')
    resolution = calibration.find('resolution')
    width = float(resolution.get('width'))
    height = float(resolution.get('height'))
    f = float(calibration.find('f').text)
    cx = width/2
    cy = height/2

    K = np.array([
        [f, 0, cx],
        [0, f, cy],
        [0, 0,  1]
        ], dtype=np.float32)

    return K, (width, height)

def extrinsics_from_xml(xml_file, verbose=False):
    root = ET.parse(xml_file).getroot()
    transforms = {}
    for e in root.findall('chunk/cameras')[0].findall('camera'):
        label = e.get('label')
        try:
            transforms[label] = e.find('transform').text
        except:
            if verbose:
                print('failed to align camera', label)

    view_matrices = []
    # labels_sort = sorted(list(transforms), key=lambda x: int(x))
    labels_sort = list(transforms)
    for label in labels_sort:
        extrinsic = np.array([float(x) for x in transforms[label].split()]).reshape(4, 4)
        extrinsic[:, 1:3] *= -1
        view_matrices.append(extrinsic)

    return view_matrices, labels_sort

def extrinsics_from_view_matrix(path):
    vm = np.loadtxt(path).reshape(-1,4,4)
    vm, ids = get_valid_matrices(vm)

    # we want consistent camera label data type, as cameras from xml
    ids = [str(i) for i in ids]

    return vm, ids

def setup_scene(scene, data, use_mesh=False, use_texture=False):
    if data['mesh'] is not None and data['pointcloud'] is None or use_mesh or use_texture:
        assert 'mesh' in data, 'use pointcloud or set mesh'
        model3d = data['mesh']
    else:
        assert 'pointcloud' in data, 'use mesh or set pointcloud'
        model3d = data['pointcloud']

    scene.set_vertices(
        positions=model3d['xyz'],
        colors=model3d['rgb'],
        normals=model3d['normals'],
        uv1d=model3d['uv1d'],
        uv2d=model3d['uv2d'],
        texture=data['texture'])

    if data['proj_matrix'] is not None:
        scene.set_proj_matrix(data['proj_matrix'])
    else:
        print('proj_matrix was not set')

    if data['view_matrix'] is not None or len(data['view_matrix']) > 0:
        scene.set_camera_view(data['view_matrix'][0])
    else:
        print('view_matrix was not set')

    scene.set_model_view(data['model3d_origin'])
    scene.set_indices(model3d['faces'])

    if data['point_sizes'] is not None:
        scene.set_point_sizes(data['point_sizes'])

    scene.set_use_texture(use_texture)

def load_scene_data(path):
    with open(path, 'r') as f:
        config = yaml.load(f)

    if 'pointcloud' in config:
        print('loading pointcloud...')
        pointcloud = import_model3d(fix_relative_path(config['pointcloud'], path))
    else:
        pointcloud = None

    if 'mesh' in config and config['mesh']:
        print('loading mesh...')
        uv_order = config['uv_order'] if 'uv_order' in config else 's,t'
        mesh = import_model3d(fix_relative_path(config['mesh'], path), uv_order=uv_order.split(','), is_mesh=True)
    else:
        mesh = None

    if config.get('texture'):
        print('loading texture...')
        texture = cv2.imread(fix_relative_path(config['texture'], path))
        assert texture is not None
        texture = texture[..., ::-1].copy()
    else:
        texture = None

    if 'intrinsic_matrix' in config:
        apath = fix_relative_path(config['intrinsic_matrix'], path)
        if apath[-3:] == 'xml':
            intrinsic_matrix, (width, height) = intrinsics_from_xml(apath)
            assert tuple(config['viewport_size']) == (width, height), f'calibration width, height: ({width}, {height})'
        else:
            intrinsic_matrix = np.loadtxt(apath)[:3, :3]
    else:
        intrinsic_matrix = None

    if 'proj_matrix' in config:
        proj_matrix = np.loadtxt(fix_relative_path(config['proj_matrix'], path))
        proj_matrix = recalc_proj_matrix_planes(proj_matrix)
    else:
        proj_matrix = None

    if 'view_matrix' in config:
        apath = fix_relative_path(config['view_matrix'], path)
        if apath[-3:] == 'xml':
            view_matrix, camera_labels = extrinsics_from_xml(apath)
        else:
            view_matrix, camera_labels = extrinsics_from_view_matrix(apath)
    else:
        view_matrix = None
    # print(camera_labels)

    if 'model3d_origin' in config:
        model3d_origin = np.loadtxt(fix_relative_path(config['model3d_origin'], path))
    else:
        model3d_origin = np.eye(4)

    if 'point_sizes' in config:
        point_sizes = np.load(fix_relative_path(config['point_sizes'], path))
    else:
        point_sizes = None

    config['viewport_size'] = tuple(config['viewport_size'])

    # if 'use_mesh' in config:
    #     use_mesh = config['use_mesh']
    # elif pointcloud is None and mesh is not None:
    #     use_mesh = True
    # else:
    #     use_mesh = False

    if 'net_path' in config:
        net_ckpt = os.path.join(config['net_path'], 'checkpoints', config['ckpt'])
        net_ckpt = fix_relative_path(net_ckpt, path)

        tex_ckpt = os.path.join(config['net_path'], 'checkpoints', config['texture_ckpt'])
        tex_ckpt = fix_relative_path(tex_ckpt, path)
    else:
        net_ckpt = None
        tex_ckpt = None

    return {
    'pointcloud': pointcloud,
    'point_sizes': point_sizes,
    'mesh': mesh,
    # 'use_mesh': use_mesh,
    'texture': texture,
    'proj_matrix': proj_matrix,
    'intrinsic_matrix': intrinsic_matrix,
    'view_matrix': view_matrix,
    'camera_labels': camera_labels,
    'model3d_origin': model3d_origin,
    'config': config,

    'net_ckpt': net_ckpt,
    'tex_ckpt': tex_ckpt
    }

def load_scene(config_path):
    scene_data = load_scene_data(config_path)

    scene = NNScene()
    setup_scene(scene, scene_data)

    return scene, scene_data

def fix_relative_path(path, config_path):
    if not os.path.exists(path) and not os.path.isabs(path):
        root = os.path.dirname(config_path)
        abspath = os.path.join(root, path)
        if os.path.exists(abspath):
            return abspath
    return path

def get_valid_matrices(mlist):
    ilist = []
    vmlist = []
    for i, m in enumerate(mlist):
        if np.isfinite(m).all():
            ilist.append(i)
            vmlist.append(m)

    return vmlist, ilist

def get_xyz_colors(xyz, r=8):
    mmin, mmax = xyz.min(axis=0), xyz.max(axis=0)
    color = (xyz - mmin) / (mmax - mmin)
    # color = 0.5 + 0.5 * xyz / r
    return np.clip(color, 0., 1.).astype(np.float32)

def get_normal_colors(normals):
    # [-1,+1]->[0,1]
    return (normals * 0.5 + 0.5).astype(np.float32)

def import_model3d(model_path, uv_order=None, is_mesh=False):
    print("import_model3d working dir:", os.getcwd())
    print("import_model3d loading: ", model_path)
    data = trimesh.load(model_path)

    n_pts = data.vertices.shape[0]

    model = {
        'rgb': None,
        'normals': None,
        'uv2d': None,
        'faces': None
    }

    if is_mesh:
        if hasattr(data.visual, 'vertex_colors'):
            model['rgb'] = data.visual.vertex_colors[:, :3] / 255.
        elif hasattr(data.visual, 'to_color'):
            try:
                # for some reason, it may fail (happens on blender exports)
                model['rgb'] = data.visual.to_color().vertex_colors[:, :3] / 255.
            except:
                print('data.visual.to_color failed')

        model['normals'] = data.vertex_normals

        if hasattr(data.visual, 'uv'):
            model['uv2d'] = data.visual.uv
        # elif model_path[-3:] == 'ply':
        #     mdata = data.metadata['ply_raw']['vertex']['data']
        #     if 's' in mdata and 't' in mdata:
        #         print('using s,t texture coords')
        #         model['uv2d'] = np.hstack([mdata['s'], mdata['t']])
        #         print(model['uv2d'].shape)

        model['faces'] = data.faces.flatten().astype(np.uint32)
    else:
        if hasattr(data, 'colors'):
            model['rgb'] = data.colors[:, :3] / 255.
        else:
            try:
                model['rgb'] = data.visual.vertex_colors[:, :3] / 255.
            except:
                pass

        if 'ply_raw' in data.metadata:
            normals = np.zeros((n_pts, 3), dtype=np.float32)

            # my exported point cloud stores the normal with one [1] dimension that is squeezed away here
            normals[:, 0] = np.squeeze(data.metadata['ply_raw']['vertex']['data']['nx'])
            normals[:, 1] = np.squeeze(data.metadata['ply_raw']['vertex']['data']['ny'])
            normals[:, 2] = np.squeeze(data.metadata['ply_raw']['vertex']['data']['nz'])
            model['normals'] = normals
        elif hasattr(data, 'vertex_normals'):
            model['normals'] = data.vertex_normals

        model['uv2d'] = np.zeros((n_pts, 2), dtype=np.float32)

    model['xyz'] = data.vertices
    model['xyz_c'] = get_xyz_colors(data.vertices)
    model['uv1d'] = np.arange(n_pts)

    if model['rgb'] is None:
        print(f'no colors in {model_path}')
        model['rgb'] = np.zeros((n_pts, 3), dtype=np.float32)

    if model['normals'] is None:
        print(f'no normals in {model_path}')
        model['rgb'] = np.zeros((n_pts, 3), dtype=np.float32)

    if model['uv2d'] is None:
        if is_mesh:
            print(f'no uv in {model_path}')
        model['uv2d'] = np.zeros((n_pts, 2), dtype=np.float32)

    if model['faces'] is None:
        if is_mesh:
            print(f'no faces in {model_path}')
        model['faces'] = np.array([0, 1, 2], dtype=np.uint32)

    print('=== 3D model ===')
    print('VERTICES: ', n_pts)
    print('EXTENT: ', model['xyz'].min(0), model['xyz'].max(0))
    print('================')

    return model

def get_vec(view_mat):
    view_mat = view_mat.copy()
    rvec0 = cv2.Rodrigues(view_mat[:3, :3])[0].flatten()
    t0 = view_mat[:3, 3]
    return rvec0, t0

def nearest_train(view_mat, test_pose, p=0.05):
    dists = []
    angs = []
    test_rvec, test_t = get_vec(test_pose)
    for i in range(len(view_mat)):
        rvec, t = get_vec(view_mat[i])
        dists.append(
            np.linalg.norm(test_t - t)
        )
        angs.append(
            np.linalg.norm(test_rvec - rvec)
        )
    angs_sort = np.argsort(angs)
    angs_sort = angs_sort[:int(len(angs_sort) * p)]
    dists_pick = [dists[i] for i in angs_sort]
    ang_dist_i = angs_sort[np.argmin(dists_pick)]
    return ang_dist_i #, angs_sort[0]

max_stars_repo_name: darglein/npbg

max_issues_repo_name: darglein/npbg

max_forks_repo_name: darglein/npbg


text:
import os
import numpy as np
import pandas as pd

def run():

    print('Loading data...')

    # load competiton data
    fnc_df = pd.read_csv('./data/raw/fnc.csv')
    loading_df = pd.read_csv('./data/raw/loading.csv')
    labels_df = pd.read_csv('./data/raw/train_scores.csv')

    # merge data
    df = fnc_df.merge(loading_df, on='Id')
    df = df.merge(labels_df, how='left', on='Id')

    # split train and test
    df.loc[df['Id'].isin(labels_df['Id']), 'is_test'] = 0
    df.loc[~df['Id'].isin(labels_df['Id']), 'is_test'] = 1

    train = df.query('is_test==0')
    test = df.query('is_test==1')
    y_median = train['domain1_var2'].median()

    # folder for pred
    folder_preds = './predicts/domain1_var2/stack/'
    if not os.path.exists(folder_preds):
        os.makedirs(folder_preds)

    # making prediction
    print('Creating prediction...')
    d12_prediction = pd.DataFrame()
    d12_prediction['Id'] = test['Id'].values
    d12_prediction['pred'] = y_median
    d12_prediction.to_csv(folder_preds + 'domain1_var2_stack.csv', index=False)
    print('domain1_var2 pred is saved as', folder_preds + 'domain1_var2_stack.csv')

max_stars_repo_name: DESimakov/TReNDS

max_issues_repo_name: DESimakov/TReNDS

max_forks_repo_name: DESimakov/TReNDS


text:
# Copyright (c) 2020-2021, NVIDIA CORPORATION.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

import cudf.comm.serialize  # noqa: F401
import cupy as cp
import dask
import numpy as np
from toolz import first
from collections.abc import Iterable

from cuml.dask.common.utils import get_client

from cuml import Base
from cuml.common.array import CumlArray
from cuml.dask.common.utils import wait_and_raise_from_futures
from cuml.raft.dask.common.comms import Comms
from cuml.dask.common.input_utils import DistributedDataHandler
from cuml.dask.common import parts_to_ranks
from cuml.internals import BaseMetaClass

from dask_cudf.core import DataFrame as dcDataFrame
from dask_cudf.core import Series as dcSeries
from functools import wraps

from distributed.client import Future

class BaseEstimator(object, metaclass=BaseMetaClass):

    def __init__(self, *, client=None, verbose=False, **kwargs):
        """
        Constructor for distributed estimators.
        """
        self.client = get_client(client)
        self.verbose = verbose
        self.kwargs = kwargs

        self.internal_model = None

    def get_combined_model(self):
        """
        Return single-GPU model for serialization

        Returns
        -------

        model : Trained single-GPU model or None if the model has not
               yet been trained.
        """

        internal_model = self._check_internal_model(self._get_internal_model())

        if isinstance(self.internal_model, Iterable):
            # This function needs to return a single instance of cuml.Base,
            # even if the class is just a composite.
            raise ValueError("Expected a single instance of cuml.Base "
                             "but got %s instead." % type(self.internal_model))

        elif isinstance(self.internal_model, Future):
            internal_model = self.internal_model.result()

        return internal_model

    def _set_internal_model(self, model):

        """
        Assigns model (a Future or list of futures containins a single-GPU
        model) to be an internal model.

        This function standardizes upon the way we set the internal model
        so that it could either be futures, a single future, or a class local
        to the client.

        In order for `get_combined model()` to provide a consistent output,
        self.internal_model is expected to be either a single future
        containing a cuml.Base instance or a local cuml.Base on the client.
        An iterable can be passed into this method when a trained model
        has been replicated across the workers. In this case, only the
        first element of the iterable will be set as the internal_model

        If multiple different parameters have been trained across the cluster,
        such as in RandomForests or some approx. nearest neighbors algorithms,
        they should be combined into a single model and the combined model
        should be passed to `set_internal_model()`

        Parameters
        ----------

        model : distributed.client.Future[cuml.Base], cuml.Base, or None

        """
        self.internal_model = self._check_internal_model(model)

    @staticmethod
    def _check_internal_model(model):
        """
        Performs a brief validation that a model meets the requirements
        to be set as an `internal_model`

        Parameters
        ----------

        model : distributed.client.Future[cuml.Base], cuml.Base, or None

        Returns
        -------

        model : distributed.client.Future[cuml.Base], cuml.Base, or None

        """
        if isinstance(model, Iterable):
            # If model is iterable, just grab the first
            model = first(model)

        if isinstance(model, Future):
            if model.type is None:
                wait_and_raise_from_futures([model])

            if not issubclass(model.type, Base):
                raise ValueError("Dask Future expected to contain cuml.Base "
                                 "but found %s instead." % model.type)

        elif model is not None and not isinstance(model, Base):
            raise ValueError("Expected model of type cuml.Base but found %s "
                             "instead." % type(model))
        return model

    def _get_internal_model(self):
        """
        Gets the internal model from the instance.

        This function is a convenience for future maintenance and
        should never perform any expensive operations like data
        transfers between the client and the Dask cluster.

        Returns
        -------

        internal_model : dask.client.Future[cuml.Base], cuml.Base or None
        """
        return self.internal_model

    @staticmethod
    @dask.delayed
    def _get_model_attr(model, name):
        if hasattr(model, name):
            return getattr(model, name)
        else:
            raise AttributeError("Attribute %s does not exist on model %s" %
                                 (name, type(model)))

    def __getattr__(self, attr):
        """
        Method gives access to the correct format of cuml Array attribute to
        the users and proxies attributes to the underlying trained model.

        If the attribute being requested is not directly on the local object,
        this function will see if the local object contains the attribute
        prefixed with an _. In the case the attribute does not exist on this
        local instance, the request will be proxied to self.internal_model and
        will be fetched either locally or remotely depending on whether
        self.internal_model is a local object instance or a future.
        """
        real_name = '_' + attr

        ret_attr = None

        # First check locally for _ prefixed attr
        if real_name in self.__dict__:
            ret_attr = self.__dict__[real_name]

        # Otherwise, if the actual attribute name exists on the
        # object, just return it.
        elif attr in self.__dict__:
            ret_attr = self.__dict__[attr]

        # If we didn't have an attribute on the local model, we might
        # have it on the distributed model.
        internal_model = self._get_internal_model()
        if ret_attr is None and internal_model is not None:
            if isinstance(internal_model, Base):
                # If model is not distributed, just return the
                # requested attribute
                ret_attr = getattr(internal_model, attr)
            else:
                # Otherwise, fetch the attribute from the distributed
                # model and return it
                ret_attr = BaseEstimator._get_model_attr(
                    internal_model, attr).compute()
        else:
            raise AttributeError("Attribute %s not found in %s" %
                                 (attr, type(self)))

        if isinstance(ret_attr, CumlArray):
            # Dask wrappers aren't meant to be pickled, so we can
            # store the raw type on the instance
            return ret_attr.to_output(self.output_type)
        else:
            return ret_attr

class DelayedParallelFunc(object):
    def _run_parallel_func(self,
                           func,
                           X,
                           n_dims=1,
                           delayed=True,
                           output_futures=False,
                           output_dtype=None,
                           output_collection_type=None,
                           **kwargs):
        """
        Runs a function embarrassingly parallel on a set of workers while
        reusing instances of models and constraining the number of
        tasks that can execute concurrently on each worker.

        Note that this mixin assumes the subclass has been trained and
        includes a `self._get_internal_model()` function containing a subclass
        of `cuml.Base`.

        This is intended to abstract functions like predict, transform, and
        score, which can execute embarrassingly parallel but need addition
        execution constraints which result from the more limited GPU
        resources.

        Parameters
        ----------
        func : dask.delayed function to propagate to the workers to execute
               embarrassingly parallel, shared between tasks on each worker

        X : Dask cuDF dataframe  or CuPy backed Dask Array (n_rows, n_features)
            Distributed dense matrix (floats or doubles) of shape
            (n_samples, n_features).

        delayed : bool return a lazy (delayed) object?

        output_futures : bool returns the futures pointing the to the resuls
                         of the parallel function executions on the workers,
                         rather than a dask collection object.

        output_collection_type : None or a string in {'cupy', 'cudf'}
            Choose to return the resulting collection as a CuPy backed
            dask.array or a dask_cudf.DataFrame. If None, will use the same
            collection type as used in the input of fit.
            Unused if output_futures=True.

        Returns
        -------
        y : dask cuDF (n_rows, 1)
        """
        X_d = X.to_delayed()

        if output_collection_type is None:
            output_collection_type = self.datatype

        model_delayed = dask.delayed(self._get_internal_model(),
                                     pure=True,
                                     traverse=False)

        func = dask.delayed(func, pure=False, nout=1)
        if isinstance(X, dcDataFrame):
            preds = [func(model_delayed, part, **kwargs) for part in X_d]
            dtype = first(X.dtypes) if output_dtype is None else output_dtype
        elif isinstance(X, dcSeries):
            preds = [func(model_delayed, part, **kwargs) for part in X_d]
            dtype = X.dtype if output_dtype is None else output_dtype
        else:
            preds = [func(model_delayed, part[0])
                     for part in X_d]
            dtype = X.dtype if output_dtype is None else output_dtype

        # TODO: Put the following conditionals in a
        #  `to_delayed_output()` function
        # TODO: Add eager path back in

        if output_collection_type == 'cupy':

            # todo: add parameter for option of not checking directly
            shape = (np.nan,) * n_dims
            preds_arr = [
                dask.array.from_delayed(pred,
                                        meta=cp.zeros(1, dtype=dtype),
                                        shape=shape,
                                        dtype=dtype)
                for pred in preds]

            if output_futures:
                return self.client.compute(preds)
            else:
                output = dask.array.concatenate(preds_arr, axis=0,
                                                allow_unknown_chunksizes=True
                                                )
                return output if delayed else output.persist()

        elif output_collection_type == 'cudf':
            if output_futures:
                return self.client.compute(preds)
            else:
                output = dask.dataframe.from_delayed(preds)
                return output if delayed else output.persist()
        else:
            raise ValueError("Expected cupy or cudf but found %s" %
                             (output_collection_type))

class DelayedPredictionProbaMixin(DelayedParallelFunc):

    def _predict_proba(self, X, delayed=True, **kwargs):
        return self._run_parallel_func(func=_predict_proba_func, X=X,
                                       n_dims=2, delayed=delayed, **kwargs)

class DelayedPredictionMixin(DelayedParallelFunc):

    def _predict(self, X, delayed=True, **kwargs):
        return self._run_parallel_func(func=_predict_func, X=X,
                                       n_dims=1, delayed=delayed,
                                       **kwargs)

class DelayedTransformMixin(DelayedParallelFunc):

    def _transform(self, X, n_dims=1, delayed=True, **kwargs):
        return self._run_parallel_func(func=_transform_func,
                                       X=X,
                                       n_dims=n_dims,
                                       delayed=delayed,
                                       **kwargs)

class DelayedInverseTransformMixin(DelayedParallelFunc):

    def _inverse_transform(self, X, n_dims=1, delayed=True, **kwargs):
        return self._run_parallel_func(func=_inverse_transform_func,
                                       X=X,
                                       n_dims=n_dims,
                                       delayed=delayed,
                                       **kwargs)

class SyncFitMixinLinearModel(object):

    def _fit(self, model_func, data):

        n_cols = data[0].shape[1]

        data = DistributedDataHandler.create(data=data, client=self.client)
        self.datatype = data.datatype

        comms = Comms(comms_p2p=False)
        comms.init(workers=data.workers)

        data.calculate_parts_to_sizes(comms)
        self.ranks = data.ranks

        worker_info = comms.worker_info(comms.worker_addresses)
        parts_to_sizes, _ = parts_to_ranks(self.client,
                                           worker_info,
                                           data.gpu_futures)

        lin_models = dict([(data.worker_info[worker_data[0]]["rank"],
                            self.client.submit(
            model_func,
            comms.sessionId,
            self.datatype,
            **self.kwargs,
            pure=False,
            workers=[worker_data[0]]))

            for worker, worker_data in
            enumerate(data.worker_to_parts.items())])

        lin_fit = dict([(worker_data[0], self.client.submit(
            _func_fit,
            lin_models[data.worker_info[worker_data[0]]["rank"]],
            worker_data[1],
            data.total_rows,
            n_cols,
            parts_to_sizes,
            data.worker_info[worker_data[0]]["rank"],
            pure=False,
            workers=[worker_data[0]]))

            for worker, worker_data in
            enumerate(data.worker_to_parts.items())])

        wait_and_raise_from_futures(list(lin_fit.values()))

        comms.destroy()
        return lin_models

def _func_fit(f, data, n_rows, n_cols, partsToSizes, rank):
    return f.fit(data, n_rows, n_cols, partsToSizes, rank)

def mnmg_import(func):

    @wraps(func)
    def check_cuml_mnmg(*args, **kwargs):
        try:
            return func(*args, **kwargs)
        except ImportError:
            raise RuntimeError("cuML has not been built with multiGPU support "
                               "enabled. Build with the --multigpu flag to"
                               " enable multiGPU support.")

    return check_cuml_mnmg

def _predict_func(model, data, **kwargs):
    return model.predict(data, **kwargs)

def _predict_proba_func(model, data, **kwargs):
    return model.predict_proba(data, **kwargs)

def _transform_func(model, data, **kwargs):
    return model.transform(data, **kwargs)

def _inverse_transform_func(model, data, **kwargs):
    return model.inverse_transform(data, **kwargs)

reason: import numpy,import cupy


text:
#!/usr/bin/env python3

"""
Plots jump rates against parameters.

Usage:
    jump_rates.py -h | --help
    jump_rates.py lambda [<index>] [options]
    jump_rates.py theta [<index>] [options]
    jump_rates.py check [options]

Options:
    -h --help               Show this screen.
    --length=<val>          Vertical voxel length. [default: 0.025]
    --kappa=<val>           Aspect ratio of the voxels. [default: 1.0]
    --diff=<val>            Macro-scale diffusion constant. [default: 1.0]
    --save=<path>           Save the figure in <path>.

"""

import numpy as np
from matplotlib import pyplot as plt

from docopt import docopt

class JumpRate(object):
    def __init__(self, diff, h):
        """
        diff - diffusion coefficient
        h - compartment size (in the y-direction in 2d)
        """
        assert isinstance(diff, (float, int))
        assert isinstance(h, (float, int))

        self.__diff__ = float(diff)
        self.__h__ = float(h)
        self.__num_dims__ = 1

    @staticmethod
    def __get_parameter__(param, const=False):
        if param is None:
            param = 1
        if isinstance(param, (int, float)):
            param = float(param)
            if const:
                param = 1
        elif isinstance(param, (tuple, list, np.ndarray)):
            param = np.array(param)
            if const:
                param = np.ones(len(param))
        return param

    def get_diff(self):
        return self.__diff__

    def get_h(self):
        return self.__h__

    def get_lambda(self, index, parameter=None):
        assert isinstance(index, int)
        if index == 1:
            return self.__lambda_1__(parameter)
        elif index == 2:
            return self.__lambda_2__(parameter)
        else:
            return self.__lambda_0__(parameter)

    def get_theta(self, index, parameter=None):
        assert isinstance(index, int)
        l_1 = self.__lambda_1__(parameter)
        l_2 = self.__lambda_2__(parameter)
        l_0 = self.__lambda_0__(parameter)

        if index == 1:
            return l_1 / l_0
        elif index == 2:
            return l_2 / l_0
        else:
            return (l_1 + l_2) / l_0

    def __lambda_1__(self, parameter):
        parameter = self.__get_parameter__(parameter, const=True)
        val = self.__diff__ / (self.__h__ ** 2)
        return val * parameter

    def __lambda_2__(self, parameter):
        parameter = self.__get_parameter__(parameter, const=True)
        val = self.__diff__ / (self.__h__ ** 2)
        return val * parameter

    def __lambda_0__(self, parameter):
        parameter = self.__get_parameter__(parameter, const=True)
        val = 2.0 * self.__diff__ / (self.__h__ ** 2)
        return val * parameter

class JumpRate2D(JumpRate):
    def __init__(self, diff, h, kappa):
        """
        diff - diffusion coefficient
        h - compartment size (in the y-direction in 2d)
        kappa - compartment aspect ratio (horizontal/vertical)
        """
        super().__init__(diff, h)
        self.__kappa__ = self.__get_parameter__(kappa)
        self.__num_dims__ = 2

    def get_kappa(self):
        return self.__kappa__

    def get_lambda(self, index, parameter=None):
        assert isinstance(index, int)
        if index == 1:
            return self.__lambda_1__(parameter)
        elif index == 2:
            return self.__lambda_2__(parameter)
        elif index == 3:
            return self.__lambda_3__(parameter)
        else:
            return self.__lambda_0__(parameter)

    def get_theta(self, index, parameter=None):
        assert isinstance(index, int)
        l_1 = self.__lambda_1__(parameter)
        l_2 = self.__lambda_2__(parameter)
        l_3 = self.__lambda_3__(parameter)
        l_0 = self.__lambda_0__(parameter)

        if index == 1:
            return l_1 / l_0
        elif index == 2:
            return l_2 / l_0
        elif index == 3:
            return l_3 / l_0
        else:
            return (2*l_1 + 4*l_2 + 2*l_3) / l_0

    def __lambda_1__(self, parameter):
        raise NotImplementedError

    def __lambda_2__(self, parameter):
        raise NotImplementedError

    def __lambda_3__(self, parameter):
        raise NotImplementedError

    def __lambda_0__(self, parameter):
        l_1 = self.__lambda_1__(parameter)
        l_2 = self.__lambda_2__(parameter)
        l_3 = self.__lambda_3__(parameter)
        return 2 * l_1 + 4 * l_2 + 2 * l_3

    def check(self, parameter):
        l1 = self.__lambda_1__(parameter)
        l2 = self.__lambda_2__(parameter)
        l3 = self.__lambda_3__(parameter)
        l0 = self.__lambda_0__(parameter)
        return np.absolute(((2 * l1 + 4*l2 + 2*l3) - l0)/l0)

class FDM(JumpRate2D):

    def __lambda_1__(self, alpha):
        alpha = self.__get_parameter__(alpha)
        val = self.__diff__ * (1.0 - self.__kappa__ * alpha)
        val /= (self.__kappa__ ** 2 * self.__h__ ** 2)
        return val

    def __lambda_2__(self, alpha):
        alpha = self.__get_parameter__(alpha)
        val = self.__diff__ * alpha
        val /= (2.0 * self.__kappa__ * self.__h__ ** 2)
        return val

    def __lambda_3__(self, alpha):
        alpha = self.__get_parameter__(alpha)
        val = self.__diff__ * (self.__kappa__**2 - self.__kappa__ * alpha)
        val /= (self.__kappa__ ** 2 * self.__h__ ** 2)
        return val

class FEM(JumpRate2D):
    def __init__(self, diff, h, kappa, alt_lambda_0=False):
        super().__init__(diff, h, kappa)
        self.__a__ = 2 * 2.0
        self.__b__ = 2 * 1.0
        self.__c__ = 1.0
        self.__d__ = 2 * 4.0
        self.__denominator__ = 6.0 * self.__kappa__ ** 2 * self.__h__ ** 2
        self.__alt_lambda_0__ = alt_lambda_0  # Use sum of lambdas for lambda_0

    def __lambda_1__(self, parameter):
        parameter = self.__get_parameter__(parameter, const=True)
        val = self.__diff__ * (self.__a__ - self.__b__ * self.__kappa__ ** 2)
        val /= self.__denominator__
        return val * parameter

    def __lambda_2__(self, parameter):
        parameter = self.__get_parameter__(parameter, const=True)
        val = self.__diff__ * self.__c__ * (self.__kappa__ ** 2 + 1.0)
        val /= self.__denominator__
        return val * parameter

    def __lambda_3__(self, parameter):
        parameter = self.__get_parameter__(parameter, const=True)
        val = self.__diff__ * (self.__a__ * self.__kappa__ ** 2 - self.__b__)
        val /= self.__denominator__
        return val * parameter

    def __lambda_0__(self, parameter):
        parameter = self.__get_parameter__(parameter, const=True)
        if self.__alt_lambda_0__:
            val = 2 * self.__lambda_1__(parameter)
            val += 4 * self.__lambda_2__(parameter)
            val += 2 * self.__lambda_3__(parameter)
        else:
            val = self.__diff__ * self.__d__ * (self.__kappa__ ** 2 + 1)
            val /= self.__denominator__
        return val * parameter

class FVM(JumpRate2D):

    def __lambda_1__(self, parameter):
        parameter = self.__get_parameter__(parameter, const=True)
        val = self.__diff__ / (self.__kappa__ ** 2 * self.__h__ ** 2)
        return val * parameter

    def __lambda_2__(self, parameter):
        parameter = self.__get_parameter__(parameter, const=True)
        return 0 * parameter

    def __lambda_3__(self, parameter):
        parameter = self.__get_parameter__(parameter, const=True)
        val = self.__diff__ / (self.__h__ ** 2)
        return val * parameter

class FET(JumpRate2D):

    def __init__(self, diff, h, kappa, trunc_order=100):
        super().__init__(diff, h, kappa)
        assert isinstance(trunc_order, (float, int))
        self.__trunc_order__ = int(trunc_order)

    def __lambda_1__(self, beta_y):
        beta_y = self.__get_parameter__(beta_y)
        theta_1 = 0

        for k in range(1, self.__trunc_order__+1):
            for j in range(1, self.__trunc_order__+1):
                temp = (self.__kappa__**2 * (2*j-1)**2 + (2*k-1)**2)

                val = 8.0 * (-1)**(k + 1) * (2.0 * k - 1.0)
                val /= (np.pi**2 * temp * (2.0 * j - 1.0))
                theta_1 += np.sin((j - 0.5) * np.pi * beta_y) * val

        return theta_1 * self.__lambda_0__(beta_y)

    def __lambda_2__(self, beta_x, beta_y=None):
        if beta_y is None:
            beta_y = beta_x
        beta_x = self.__get_parameter__(beta_x)
        beta_y = self.__get_parameter__(beta_y)
        assert len(beta_x) == len(beta_y)

        theta_2 = 0.0
        for k in range(1, self.__trunc_order__+1):
            for j in range(1, self.__trunc_order__+1):
                temp = (self.__kappa__**2 * (2*j-1)**2 + (2*k-1)**2)

                val1 = (2*j-1)*(np.sin(np.pi*(beta_x*k-0.5*beta_x+k))+1.0)
                val1 *= self.__kappa__**2
                val1 /= (2 * k - 1)
                val2 = (2*k-1)*(np.sin(np.pi*(beta_y*j-0.5*beta_y+j))+1.0)
                val2 /= (2 * j - 1)
                theta_2 += 4.0*(-1)**(j+k) * (val1 + val2) / (np.pi**2 * temp)

        return theta_2 * self.__lambda_0__(beta_x)

    def __lambda_3__(self, beta_x):
        beta_x = self.__get_parameter__(beta_x)

        theta_3 = 0
        for k in range(1, self.__trunc_order__+1):
            for j in range(1, self.__trunc_order__+1):
                temp = (self.__kappa__**2 * (2*j-1)**2 + (2*k-1)**2)

                val = 8.0 * (-1)**(j+1) * (2 * j - 1) * self.__kappa__**2
                val /= (np.pi**2 * temp * (2 * k - 1))
                theta_3 += np.sin((k - 0.5) * np.pi * beta_x) * val

        return theta_3 * self.__lambda_0__(beta_x)

    def __lambda_0__(self, parameter):
        parameter = self.__get_parameter__(parameter, const=True)
        k = np.arange(1, self.__trunc_order__ + 1)
        kk, jj = np.meshgrid(k, k)

        top = 64 * (-1)**(jj + kk) * self.__kappa__**2 * self.__h__**2
        bottom_1 = self.__diff__ * np.pi**4 * (2*kk-1)*(2*jj-1)
        bottom_2 = (self.__kappa__**2 * (2*jj-1)**2 + (2*kk-1)**2)
        return 1.0/np.sum(top/(bottom_1*bottom_2)) * parameter

if __name__ == '__main__':
    args = docopt(__doc__)

    args["--length"] = float(args["--length"])
    args["--kappa"] = float(args["--kappa"])
    args["--diff"] = float(args["--diff"])
    if args["<index>"] in ["1", "2", "3"]:
        args["<index>"] = int(args["<index>"])
    else:
        args["<index>"] = 0

    fdm = FDM(args["--diff"], args["--length"], args["--kappa"])
    fem = FEM(args["--diff"], args["--length"], args["--kappa"])
    fvm = FVM(args["--diff"], args["--length"], args["--kappa"])
    fet = FET(args["--diff"], args["--length"], args["--kappa"])

    x = np.linspace(0, 1, 50)
    fig = plt.figure()

    if args["lambda"]:

        plt.plot(x, fdm.get_lambda(args["<index>"], x), label="FDM")
        plt.plot(x, fem.get_lambda(args["<index>"], x), label="FEM")
        plt.plot(x, fvm.get_lambda(args["<index>"], x), label="FVM")
        plt.plot(x, fet.get_lambda(args["<index>"], x), label="FET")
        plt.ylabel(r"$\lambda_{}$".format(args["<index>"]))

    elif args["theta"]:
        plt.plot(x, fdm.get_theta(args["<index>"], x), label="FDM")
        plt.plot(x, fem.get_theta(args["<index>"], x), label="FEM")
        plt.plot(x, fvm.get_theta(args["<index>"], x), label="FVM")
        plt.plot(x, fet.get_theta(args["<index>"], x), label="FET")
        plt.ylabel(r"$\theta_{}$".format(args["<index>"]))

    elif args["check"]:
        plt.plot(x, fdm.check(x), label="FDM")
        plt.plot(x, fem.check(x), label="FEM")
        plt.plot(x, fvm.check(x), label="FVM")
        plt.plot(x, fet.check(x), label="FET")
        plt.ylabel(r"$\left|\sum_i \lambda_i - \lambda_0\right|/\lambda_0$")

    plt.xlabel(r"$\alpha$, $\beta$")
    plt.legend()
    plt.tight_layout()

    if args["--save"]:
        plt.savefig(args["--save"])
    else:
        plt.show()

max_stars_repo_name: BartoszBartmanski/StoSpa

max_issues_repo_name: BartoszBartmanski/StoSpa

max_forks_repo_name: BartoszBartmanski/StoSpa


text:
# Generate HoG feature from testset edge images

import cv2 as cv
import scipy.io as sio
import numpy as np

# HoG parameters
win_size = (128, 128)
block_size = (32, 32)
block_stride = (32, 32)
cell_size = (32, 32)
n_bins = 9

im_h, im_w = 180, 320

hog = cv.HOGDescriptor(win_size, block_size, block_stride, cell_size, n_bins)

# database camera
data = sio.loadmat(r'C:\Users\mostafa\Desktop\test\SCCvSD-master\python\hog/my_testset_feature.mat')
edge_map = data['edge_map'] # (720, 1280, 1, 186)

h, w, c = edge_map.shape
n=1
features = []
for i in range(n):
    edge_image = edge_map[:,:,:]
    edge_image = cv.resize(edge_image, (im_w, im_h))
    feat = hog.compute(edge_image)
    features.append(feat)

features = np.squeeze(np.asarray(features), axis=2)

print('feature dimension {}'.format(features.shape))

save_file = r'C:\Users\mostafa\Desktop\test\SCCvSD-master\python\hog/my_testset_feature_HoG.mat'
sio.savemat(save_file, {'features':features,
                        'edge_map':edge_map})

print('save to file: {}'.format(save_file))

max_stars_repo_name: FootballAnalysis/footballanalysis

reason: import numpy,import scipy


text:
import argparse
import shutil
import errno
import time
import glob
import os
import cv2
import numpy as np

from merge_tools import do_merge_box

DEBUG = True

class MergeBox(object):

    def __init__(self):
        args = self.parse_arguments()
        self.output_dir = args.output_dir
        self.input_dir = args.input_dir

    def parse_arguments(self):
        """
            Parse the command line arguments of the program.
        """

        parser = argparse.ArgumentParser(
            description="生成labelme 格式数据"
        )
        parser.add_argument(
            "-o",
            "--output_dir",
            type=str,
            nargs="?",
            help="输出文件的本地路径",
            required=True
        )
        parser.add_argument(
            "-i",
            "--input_dir",
            type=str,
            nargs="?",
            help="输入文件路径",
            required=True
        )

        return parser.parse_args()

    def parse_file_list(self, input_dir, output_dir):
        """
        """

        label_file_list = glob.glob(os.path.join(input_dir, '*.txt'))

        for label_file in label_file_list:

            real_name = label_file.split('/')[-1].split('.')[0]

            image_file = os.path.join(input_dir, "{}.jpg".format(real_name))
            label_image_file = os.path.join(output_dir, "{}.jpg".format(real_name))
            print(image_file)
            if os.path.exists(image_file):
                self.draw_box(label_file, image_file, label_image_file)

    def draw_box(self, label_file, image_file, label_image_file):

        if not os.path.exists(label_file) or not os.path.exists(image_file):
            print('【警告】文件不存在  --------file:  {} '.format(label_file))
            return

        with open(label_file, 'r', encoding='utf-8') as f:
            lines = f.readlines()

        lines = do_merge_box(lines)

        bg_image = cv2.imread(image_file)
        raw_image = cv2.imread(image_file)

        for index, line in enumerate(lines):
            if len(line) < 8:
                continue

            points = line.split(',')
            left = int(points[0]) if int(points[6]) > int(points[0]) else int(points[6])
            right = int(points[2]) if int(points[4]) < int(points[2]) else int(points[4])
            top = int(points[1]) if int(points[3]) > int(points[1]) else int(points[3])
            bottom = int(points[5]) if int(points[7]) < int(points[5]) else int(points[7])
            height = bottom - top
            width = right - left

            colors = (0, 0, 255)
            if index == 189:
                print(line)
                print("left={}  right={}  top={} bottom={}".format(left, right, top, bottom))

            # cv2.fillPoly(bg_image, [pts], (255, 255, 255))
            roi_corners=np.array([[(int(points[0]), int(points[1])),
                                   (int(points[2]), int(points[3])),
                                   (int(points[4]), int(points[5])),
                                   (int(points[6]), int(points[7]))]], dtype=np.int32)
            mask = np.ones(bg_image.shape, dtype=np.uint8)
            channels=bg_image.shape[2]
            #输入点的坐标
            channel_count=channels
            ignore_mask_color = (255,)*channel_count
            #创建mask层
            cv2.fillPoly(mask, roi_corners, ignore_mask_color)
            #为每个像素进行与操作，除mask区域外，全为0
            masked_image = cv2.bitwise_and(bg_image, mask)
            c_img = masked_image[top: int(top + height), left: int(left + width)]
            cv2.imwrite(os.path.join(self.output_dir, '{}.jpg'.format(index)), c_img)

            # 画矩形框
            pts = np.array([[int(points[0]), int(points[1])],
                            [int(points[2]), int(points[3])],
                            [int(points[4]), int(points[5])],
                            [int(points[6]), int(points[7])]], np.int32)  # 每个点都是(x, y)
            pts = roi_corners.reshape((-1, 1, 2))
            cv2.polylines(bg_image, [pts], True, (0, 0, 255))
            # cv2.rectangle(bg_image, (left, top), (left+width, top+height), colors, 1)

        cv2.imwrite(label_image_file, bg_image)
        print('【输出】生成合格后的图片{} .'.format(label_image_file))

    def main(self):
        time_start = time.time()
        # Argument parsing
        args = self.parse_arguments()
        if os.path.exists(args.output_dir):
            shutil.rmtree(args.output_dir)

        try:
            os.makedirs(args.output_dir)
        except OSError as e:
            if e.errno != errno.EEXIST:
                raise

        if not os.path.exists(args.input_dir):
            print("输入路径不能为空  input_dir[{}] ".format(args.input_dir))
            return
        self.parse_file_list(args.input_dir, args.output_dir)

        time_elapsed = time.time() - time_start
        print('The code run {:.0f}m {:.0f}s'.format(time_elapsed // 60, time_elapsed % 60))

if __name__ == "__main__":
    mergeBox = MergeBox()
    mergeBox.main()

max_stars_repo_name: dikers/ocr-train-data-generator

max_issues_repo_name: dikers/ocr-train-data-generator

max_forks_repo_name: dikers/ocr-train-data-generator


text:
from models.meta_architectures import BuildFrameworks
import tensorflow as tf
import cv2
import os
import time
import numpy as np
from core.tools import tools
from core.config import cfgs

def txt_visdrone(img_dir, save_path, ensemble_path,normalized_result=False):
    # initialize
    net = BuildFrameworks(False)
    img_plac = tf.placeholder(dtype=tf.uint8, shape=[None, None, 3])

    if cfgs.PRETRAIN_BACKBONE_NAME in ['resnet101_v1d', 'resnet50_v1d']:
        img_tensor = img_plac / 255 - tf.constant([[cfgs.PIXEL_MEAN_]])
        img_tensor = img_tensor / tf.constant([cfgs.PIXEL_STD])
    else:
        img_tensor = img_plac - tf.constant([[cfgs.PIXEL_MEAN]])
    img_tensor = tf.expand_dims(img_tensor, axis=0)

    # detection result dict
    det_dict = net.build_framworks(input_img_batch=img_tensor)
    final_boxes, final_scores, final_category = det_dict[
                                                    "final_boxes"], det_dict["final_scores"], det_dict["final_category"]

    restorer, restore_ckpt = net.get_restorer()
    if restore_ckpt == cfgs.PRETRAIN_BACKBONE_WEIGHTS:
        raise IndexError("----->Please select the trained weights rather than pretrained weights.")
    img_name = os.listdir(path=img_dir)
    with tf.Session() as sess:
        restorer.restore(sess, restore_ckpt)
        for i in range(len(img_name)):
            # read images
            img_array = cv2.imread(os.path.join(img_dir, img_name[i]))
            raw_h = img_array.shape[0]
            raw_w = img_array.shape[1]
            detected_scores_, detected_boxes_, detected_categories_ = [], [], []
            start=time.time()
            if cfgs.MULTI_SCALSE_TESTING:
                short_size_len=cfgs.SHORT_SIZE_LIMITATION_LIST
            else:
                short_size_len=[cfgs.SHORT_SIZE_LIMITATION]
            for short_size in short_size_len:
                if raw_h < raw_w:
                    new_h, new_w = short_size, min(int(short_size * float(raw_w) / raw_h), cfgs.MAX_LENGTH)
                else:
                    new_h, new_w = min(int(short_size * float(raw_h) / raw_w), cfgs.MAX_LENGTH), short_size
                img_resize = cv2.resize(img_array, (new_w, new_h))
                resized_img_, final_boxes_, final_scores_, final_category_ = sess.run(
                    [img_plac, final_boxes, final_scores, final_category],
                    feed_dict={img_plac: img_resize[:, :, ::-1]})

                #剔除变成一条线的框
                inds_inside = np.where(
                    (final_boxes_[:, 2] > final_boxes_[:, 0]) &
                    (final_boxes_[:, 3] > final_boxes_[:, 1])
                )[0]

                final_boxes_ = final_boxes_[inds_inside, :]
                final_scores_ = final_scores_[inds_inside]
                final_category_ = final_category_[inds_inside]

                xmin, ymin, xmax, ymax = final_boxes_[:, 0], final_boxes_[:, 1], \
                                         final_boxes_[:, 2], final_boxes_[:, 3]

                resized_h, resized_w = resized_img_.shape[0], resized_img_.shape[1]

                # normalized boxes
                xmin = xmin / resized_w
                xmax = xmax / resized_w
                ymin = ymin / resized_h
                ymax = ymax / resized_h

                resized_boxes = np.transpose(np.stack([xmin, ymin, xmax, ymax]))

                detected_scores_.append(final_scores_)
                detected_boxes_.append(resized_boxes)
                detected_categories_.append(final_category_)
            end=time.time()

            from ensemble_boxes import weighted_boxes_fusion

            boxes, scores, labels = weighted_boxes_fusion(detected_boxes_, detected_scores_, detected_categories_,
                                                          weights=None,
                                                          iou_thr=0.6, skip_box_thr=0.001, conf_type='avg')

            detected_scores = np.array(scores)
            detected_boxes = np.array(boxes)
            detected_categories = np.array(labels)
            if not normalized_result:
                # map coordinate to raw picture
                xmin, ymin, xmax, ymax = detected_boxes[:, 0] * raw_w, detected_boxes[:, 1] * raw_h, \
                                         detected_boxes[:, 2] * raw_w, detected_boxes[:, 3] * raw_h
                boxes = np.transpose(np.stack([xmin, ymin, xmax - xmin, ymax - ymin]))
                result_full_path = save_path + \
                                   str('/' + img_name[i].split('.')[0]) + '.txt'
            else:
                result_full_path = ensemble_path +\
                                   str('/' + img_name[i].split('.')[0]) + '.txt'
            if not os.path.exists(save_path):
                os.mkdir(save_path)
            if not os.path.exists(ensemble_path):
                os.makedirs(ensemble_path)
            with open(result_full_path, 'w') as fout:
                num = 0
                if normalized_result:
                    fout.write(str(raw_w))
                    fout.write(',')
                    fout.write(str(raw_h))
                    fout.write('\n')
                for j, box in enumerate(boxes):
                    if num < 500:
                        if detected_categories[j] != 1 and detected_categories[j] != 12:
                            fout.write(str(box[0]))
                            fout.write(',')
                            fout.write(str(box[1]))
                            fout.write(',')
                            fout.write(str(box[2]))
                            fout.write(',')
                            fout.write(str(box[3]))
                            fout.write(',')
                            fout.write(str(detected_scores[j]))
                            fout.write(',')
                            fout.write(str(detected_categories[j] - 1))
                            fout.write(',-1,')
                            fout.write('-1' + '\n')
                            num += 1
                    else:
                        break
                fout.close()
            tools.view_bar(
                '{}, time cost: {}s'.format(
                    img_name[i],
                    (end - start)),
                i + 1,
                len(img_name))

if __name__ == '__main__':
    # os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
    img_dir = cfgs.IMG_INFERENCE_DIR
    save_path = r'./test_result_txt'
    ensemble_path = './test_model_ensemble' + '/' + cfgs.PRETRAIN_BACKBONE_NAME +'/'
    #if you want to get nomalized results for  model ensemble, set normalized_result = True
    txt_visdrone(img_dir, save_path,ensemble_path,normalized_result=cfgs.NORMALIZED_RESULTS_FOR_MODEL_ENSEMBLE)

max_stars_repo_name: postor/visdrone-docker

max_issues_repo_name: postor/visdrone-docker

max_forks_repo_name: postor/visdrone-docker