text:
"""
..
    Copyright (c) 2016-2017, Magni developers.
    All rights reserved.
    See LICENSE.rst for further information.

Module providing public functions for the magni.imaging.measurements
subpackage.

Routine listings
----------------
lissajous_sample_image(h, w, scan_length, num_points, f_y=1., f_x=1.,
    theta_y=0., theta_x=np.pi / 2)
    Function for lissajous sampling an image.
lissajous_sample_surface(l, w, speed, sample_rate, time, f_y=1., f_x=1.,
    theta_y=0., theta_x=np.pi / 2, speed_mode=0)
    Function for lissajous sampling a surface.

"""

from __future__ import division

import numpy as np

from magni.imaging.measurements import _util
from magni.utils.validation import decorate_validation as _decorate_validation
from magni.utils.validation import validate_numeric as _numeric

__all__ = ['lissajous_sample_image', 'lissajous_sample_surface']

_min_l = _util.min_l
_min_w = _util.min_w
_min_speed = _util.min_speed
_min_sample_rate = _util.min_sample_rate
_min_time = _util.min_time
_min_scan_length = _util.min_scan_length
_min_num_points = _util.min_num_points

def lissajous_sample_image(h, w, scan_length, num_points, f_y=1., f_x=1.,
                           theta_y=0., theta_x=np.pi / 2):
    """
    Sample an image using a lissajous pattern.

    The coordinates (in units of pixels) resulting from sampling an image of
    size `h` times `w` using a lissajous pattern are determined. The
    `scan_length` determines the length of the path scanned whereas
    `num_points` indicates the number of samples taken on that path.

    Parameters
    ----------
    h : int
        The height of the area to scan in units of pixels.
    w : int
        The width of the area to scan in units of pixels.
    scan_length : float
        The length of the path to scan in units of pixels.
    num_points : int
        The number of samples to take on the scanned path.
    f_y : float
        The frequency of the y-sinusoid (the default value is 1.0).
    f_x : float
        The frequency of the x-sinusoid (the default value is 1.0).
    theta_y : float
        The starting phase of the y-sinusoid (the default is 0.0).
    theta_x : float
        The starting phase of the x-sinusoid (the default is pi / 2).

    Returns
    -------
    coords : ndarray
        The coordinates of the samples arranged into a 2D array, such that each
        row is a coordinate pair (x, y).

    Notes
    -----
    The orientation of the coordinate system is such that the width `w` is
    measured along the x-axis whereas the height `h` is measured along the
    y-axis.

    Examples
    --------
    For example,

    >>> import numpy as np
    >>> from magni.imaging.measurements import lissajous_sample_image
    >>> h = 10
    >>> w = 10
    >>> scan_length = 50.0
    >>> num_points = 12
    >>> np.set_printoptions(suppress=True)
    >>> lissajous_sample_image(h, w, scan_length, num_points)
    array([[ 5.        ,  9.5       ],
           [ 1.40370042,  7.70492686],
           [ 0.67656563,  3.75183526],
           [ 3.39871123,  0.79454232],
           [ 7.39838148,  1.19240676],
           [ 9.48459832,  4.62800824],
           [ 7.99295651,  8.36038857],
           [ 4.11350322,  9.41181634],
           [ 0.94130617,  6.94345168],
           [ 1.0071768 ,  2.92458128],
           [ 4.25856283,  0.56150128],
           [ 8.10147506,  1.7395012 ],
           [ 9.4699986 ,  5.51876059]])

    """

    @_decorate_validation
    def validate_input():
        _numeric('h', 'integer', range_='[2;inf)')
        _numeric('w', 'integer', range_='[2;inf)')
        _numeric('scan_length', 'floating',
                 range_='[{};inf)'.format(_min_scan_length))
        _numeric('num_points', 'integer',
                 range_='[{};inf)'.format(_min_num_points))
        _numeric('f_y', 'floating', range_='(0;inf)')
        _numeric('f_x', 'floating', range_='(0;inf)')
        _numeric('theta_y', 'floating', range_='(-inf;inf)')
        _numeric('theta_x', 'floating', range_='(-inf;inf)')

    validate_input()

    coords = lissajous_sample_surface(
        float(h - 1), float(w - 1), scan_length, float(num_points), 1.,
        f_y=f_y, f_x=f_x, theta_y=theta_y, theta_x=theta_x)
    coords = coords + 0.5

    return coords

def lissajous_sample_surface(l, w, speed, sample_rate, time, f_y=1., f_x=1.,
                             theta_y=0., theta_x=np.pi / 2, speed_mode=0):
    """
    Sample a surface area using a lissajous pattern.

    The coordinates (in units of meters) resulting from sampling an area of
    size `l` times `w` using a lissajous pattern are determined. The scanned
    path is determined from the probe `speed` and the scan `time`.

    Parameters
    ----------
    l : float
        The length of the area to scan in units of meters.
    w : float
        The width of the area to scan in units of meters.
    speed : float
        The probe speed in units of meters/second.
    sample_rate : float
        The sample rate in units of Hertz.
    time : float
        The scan time in units of seconds.
    f_y : float
        The frequency of the y-sinusoid (the default value is 1.0).
    f_x : float
        The frequency of the x-sinusoid (the default value is 1.0).
    theta_y : float
        The starting phase of the y-sinusoid (the default is 0.0).
    theta_x : float
        The starting phase of the x-sinusoid (the default is pi / 2).
    speed_mode : int
        The speed mode used to select sampling points (the default is 0 which
        implies that the speed argument determines the speed, and f_y and f_x
        determine the ratio between the relative frequencies used).

    Returns
    -------
    coords : ndarray
        The coordinates of the samples arranged into a 2D array, such that each
        row is a coordinate pair (x, y).

    Notes
    -----
    The orientation of the coordinate system is such that the width `w` is
    measured along the x-axis whereas the length `l` is measured along the
    y-axis.

    Generally, the lissajous sampling pattern does not provide constant speed,
    and this cannot be compensated for without violating f_y, f_x, or both.
    Therefore, `speed_mode` allows the user to determine how this issue is
    handled: In `speed_mode` 0, constant speed equal to `speed` is ensured by
    non-uniform sampling of a lissajous curve, whereby `f_y` and `f_x` are not
    constant frequencies. In `speed_mode` 1, average speed equal to `speed` is
    ensured by scaling `f_y` and `f_x` by the same constant. In `speed_mode` 2,
    `f_y` and `f_x` are kept constant and the `speed` is only used to determine
    the path length in combination with `time`.

    Examples
    --------
    For example,

    >>> import numpy as np
    >>> from magni.imaging.measurements import lissajous_sample_surface
    >>> l = 1e-6
    >>> w = 1e-6
    >>> speed = 7e-7
    >>> sample_rate = 1.0
    >>> time = 12.0
    >>> np.set_printoptions(suppress=True)
    >>> lissajous_sample_surface(l, w, speed, sample_rate, time)
    array([[ 0.0000005 ,  0.000001  ],
           [ 0.00000001,  0.00000058],
           [ 0.00000033,  0.00000003],
           [ 0.00000094,  0.00000025],
           [ 0.00000082,  0.00000089],
           [ 0.00000017,  0.00000088],
           [ 0.00000007,  0.00000024],
           [ 0.00000068,  0.00000003],
           [ 0.00000099,  0.0000006 ],
           [ 0.00000048,  0.000001  ],
           [ 0.        ,  0.00000057],
           [ 0.00000035,  0.00000002],
           [ 0.00000094,  0.00000027]])

    """

    @_decorate_validation
    def validate_input():
        _numeric('l', 'floating', range_='[{};inf)'.format(_min_l))
        _numeric('w', 'floating', range_='[{};inf)'.format(_min_w))
        _numeric('speed', 'floating', range_='[{};inf)'.format(_min_speed))
        _numeric('sample_rate', 'floating',
                 range_='[{};inf)'.format(_min_sample_rate))
        _numeric('time', 'floating', range_='[{};inf)'.format(_min_time))
        _numeric('f_y', 'floating', range_='(0;inf)')
        _numeric('f_x', 'floating', range_='(0;inf)')
        _numeric('theta_y', 'floating', range_='(-inf;inf)')
        _numeric('theta_x', 'floating', range_='(-inf;inf)')
        _numeric('speed_mode', 'integer', range_='[0;2]')

    validate_input()

    s_x = w / 2
    s_y = l / 2

    if speed_mode in (0, 1):
        # The probe moves 4 * s_x * f_x and 4 * s_y * f_y pixels a second in
        # the x-direction and y-direction, respectively, and the 2-norm of this
        # is a lower bound on the distance per second. Thus, t is an upper
        # bound on the scan time.
        t = speed * time / np.sqrt((4 * s_x * f_x)**2 + (4 * s_y * f_y)**2)
        # The above assumes that f_x * t and f_y * t are integral numbers and
        # so t is increased to ensure the upper bound.
        t = max(np.ceil(f_x * t) / f_x, np.ceil(f_y * t) / f_y)
        # The distance between sampling points on the curve is chosen small
        # enough to approximate the curve by straight line segments.
        dt = 1 / (10**4 * max(f_x, f_y))
        t = np.linspace(0, t, int(t / dt))

        x = s_x * np.cos(2 * np.pi * f_x * t + theta_x) + s_x
        y = s_y * np.cos(2 * np.pi * f_y * t + theta_y) + s_y
        dx = x[1:] - x[:-1]
        dy = y[1:] - y[:-1]
        l = np.zeros(t.shape)
        l[1:] = np.cumsum((dx**2 + dy**2)**(1 / 2))

        if speed_mode == 0:
            # Constant speed entails constant distance between samples.
            l_mode_0 = np.linspace(0, speed * time, sample_rate * time + 1)
            t = np.interp(l_mode_0, l, t)
        else:  # speed_mode == 1
            # The value of t where the desired scan length is reached.
            t_end = np.argmax(l > speed * time) * dt
            t = np.linspace(0, t_end, sample_rate * time + 1)
    else:  # speed_mode == 2
        t = np.linspace(0, time, sample_rate * time + 1)

    x = s_x * np.cos(2 * np.pi * f_x * t + theta_x) + s_x
    y = s_y * np.cos(2 * np.pi * f_y * t + theta_y) + s_y

    return np.column_stack((x, y))

max_stars_repo_name: SIP-AAU/Magni

max_issues_repo_name: SIP-AAU/Magni

max_forks_repo_name: SIP-AAU/Magni


text:
import unittest
from unittest.mock import MagicMock, patch
import os

import numpy as np
import numpy.testing as npt
from spdata.common import DATA_ROOT
import spdata.types as ty

import aspect._export as ex

class DatasetNameTest(unittest.TestCase):
    def test_extracts_dataset_name(self):
        name = 'some-dataset'
        analysis_root = os.path.join(DATA_ROOT, name, 'tSNE', 'blah')
        found = ex.dataset_name(analysis_root)
        self.assertEqual(name, found)

SPECTRA = [[4]]
COORDINATES = ty.Coordinates([1], [2], [3])
MZ = [5]
LABELS = [6]
DATASET = ty.Dataset(SPECTRA, COORDINATES, MZ, LABELS)

def returns(value):
    return MagicMock(return_value=value)

def throws(exception):
    return MagicMock(side_effect=exception)

@patch.object(ex, ex.dataset_name.__name__, new=returns('data'))
@patch.object(ex, ex.load_dataset.__name__, new=returns(DATASET))
class GetMetadataTest(unittest.TestCase):
    def test_loads_coordinates_and_labels_of_analysed_dataset(self):
        metadata = ex.get_metadata('blah')
        self.assertSequenceEqual(metadata.labels, LABELS)
        self.assertSequenceEqual(metadata.coordinates.x, COORDINATES.x)
        self.assertSequenceEqual(metadata.coordinates.y, COORDINATES.y)
        self.assertSequenceEqual(metadata.coordinates.z, COORDINATES.z)

TRANSFORMED_DATASET = np.array([[1, 2]])
METADATA = ex.Metadata(COORDINATES, LABELS)

@patch('data_utils.dumps_txt')
@patch.object(ex.joblib, ex.joblib.load.__name__, new=returns(TRANSFORMED_DATASET))
@patch.object(ex, ex.get_metadata.__name__, new=returns(METADATA))
class RegenerateDatasetTest(unittest.TestCase):
    def test_exports_transformed_dataset(self, mock_dumps: MagicMock):
        ex.regenerate_dataset('blah.txt', 'analysis-root')
        mock_dumps.assert_called_once()
        path, dataset = mock_dumps.call_args[0]
        npt.assert_equal(dataset.spectra, TRANSFORMED_DATASET)
        self.assertEqual(path, 'blah.txt')

@patch.object(ex, ex.regenerate_dataset.__name__)
@patch.object(os.path, 'exists', new=returns(True))
class EnsureDatasetTest(unittest.TestCase):
    def test_regenerates_nonexistent_dataset(self, mock_regenerate: MagicMock):
        with patch.object(os.path, 'exists', new=returns(False)):
            ex.ensure_dataset('analysis-root')
        mock_regenerate.assert_called_once()

    def test_does_nothing_for_existing_dataset(self, mock_regenerate: MagicMock):
        ex.ensure_dataset('analysis-root')
        mock_regenerate.assert_not_called()

    def test_returns_path_to_txt_dataset(self, mock_regenerate: MagicMock):
        path = ex.ensure_dataset('analysis-root')
        self.assertIn('data.txt', path)
        self.assertIn('analysis-root', path)

@patch.object(ex, 'find_root', new=returns('analysis-root'))
@patch('common.require_post_variable', new=returns('new dataset name'))
@patch.object(ex, ex.ensure_dataset.__name__, new=returns('data.txt'))
@patch('data_utils.push_to_repo')
class ExportTest(unittest.TestCase):
    def test_pushes_transformed_dataset_to_repo(self, mock_push: MagicMock):
        ex.export('blah')
        mock_push.assert_called_once_with('data.txt', 'new dataset name')

    def test_returns_empty_table_on_success(self, mock_push: MagicMock):
        response = ex.export('blah')
        self.assertIn('columns', response)
        self.assertIn('data', response)

ext: py

max_stars_repo_name: spectre-team/tsne-worker

max_issues_repo_name: spectre-team/tsne-worker

max_forks_repo_name: spectre-team/tsne-worker

reason: import numpy


text:
import qctests.CSIRO_long_gradient
import util.testingProfile
import numpy

##### CSIRO_long_gradient ---------------------------------------------------

def test_CSIRO_long_gradient():
    '''
    Spot-check the nominal behavior of the CSIRO long gradient test.
    '''

    # nominal
    p = util.testingProfile.fakeProfile([20,10,15,15,15,10], [0,5,10,15,20,25])
    qc = qctests.CSIRO_long_gradient.test(p, None)
    truth = numpy.zeros(6, dtype=bool)
    truth[1] = True

    assert numpy.array_equal(qc, truth), 'failed to flag a nominal long inversion'

    # gradlong too large
    p = util.testingProfile.fakeProfile([20,10,15,15,15,10], [0,10,20,30,40,50])
    qc = qctests.CSIRO_long_gradient.test(p, None)
    truth = numpy.zeros(6, dtype=bool)

    assert numpy.array_equal(qc, truth), 'should not flag a long inversion with such a large gradient'

    # too shallow
    p = util.testingProfile.fakeProfile([20,10,15,15,15,10], [0,1,6,11,16,21])
    qc = qctests.CSIRO_long_gradient.test(p, None)
    truth = numpy.zeros(6, dtype=bool)

    print(truth, qc)

    assert numpy.array_equal(qc, truth), 'should not flag a long inversion that begins at < 5m'

max_stars_repo_name: BillMills/AutoQC

max_issues_repo_name: castelao/AutoQC

max_forks_repo_name: BillMills/AutoQC


text:
import argparse
import queue
import time
from pathlib import Path

import cv2
import depthai
import numpy as np
from imutils.video import FPS

parser = argparse.ArgumentParser()
parser.add_argument('-nd', '--no-debug', action="store_true", help="Prevent debug output")
parser.add_argument('-cam', '--camera', action="store_true", help="Use DepthAI 4K RGB camera for inference (conflicts with -vid)")
parser.add_argument('-vid', '--video', type=str, help="Path to video file to be used for inference (conflicts with -cam)")
args = parser.parse_args()

if not args.camera and not args.video:
    raise RuntimeError("No source selected. Please use either \"-cam\" to use RGB camera as a source or \"-vid <path>\" to run on video")

debug = not args.no_debug

def cos_dist(a, b):
    return np.dot(a, b)/(np.linalg.norm(a)*np.linalg.norm(b))

def frame_norm(frame, bbox):
    return (np.clip(np.array(bbox), 0, 1) * np.array([*frame.shape[:2], *frame.shape[:2]])[::-1]).astype(int)

def to_planar(arr: np.ndarray, shape: tuple) -> list:
    return [val for channel in cv2.resize(arr, shape).transpose(2, 0, 1) for y_col in channel for val in y_col]

def create_pipeline():
    print("Creating pipeline...")
    pipeline = depthai.Pipeline()

    if args.camera:
        # ColorCamera
        print("Creating Color Camera...")
        cam = pipeline.createColorCamera()
        cam.setPreviewSize(300, 300)
        cam.setResolution(depthai.ColorCameraProperties.SensorResolution.THE_1080_P)
        cam.setInterleaved(False)
        cam.setBoardSocket(depthai.CameraBoardSocket.RGB)
        cam_xout = pipeline.createXLinkOut()
        cam_xout.setStreamName("cam_out")
        cam.preview.link(cam_xout.input)

    # NeuralNetwork
    print("Creating Face Detection Neural Network...")
    detection_nn = pipeline.createNeuralNetwork()
    detection_nn.setBlobPath(str(Path("models/face-detection-retail-0004.blob").resolve().absolute()))
    detection_nn_xout = pipeline.createXLinkOut()
    detection_nn_xout.setStreamName("detection_nn")
    detection_nn.out.link(detection_nn_xout.input)

    if args.camera:
        cam.preview.link(detection_nn.input)
    else:
        detection_in = pipeline.createXLinkIn()
        detection_in.setStreamName("detection_in")
        detection_in.out.link(detection_nn.input)

    # NeuralNetwork
    print("Creating Age Gender Neural Network...")
    age_gender_in = pipeline.createXLinkIn()
    age_gender_in.setStreamName("age_gender_in")
    age_gender_nn = pipeline.createNeuralNetwork()
    age_gender_nn.setBlobPath(str(Path("models/age-gender-recognition-retail-0013.blob").resolve().absolute()))
    age_gender_nn_xout = pipeline.createXLinkOut()
    age_gender_nn_xout.setStreamName("age_gender_nn")
    age_gender_in.out.link(age_gender_nn.input)
    age_gender_nn.out.link(age_gender_nn_xout.input)

    print("Pipeline created.")
    return pipeline

device = depthai.Device(create_pipeline())
print("Starting pipeline...")
device.startPipeline()
if args.camera:
    cam_out = device.getOutputQueue("cam_out", 1, True)
else:
    detection_in = device.getInputQueue("detection_in")
detection_nn = device.getOutputQueue("detection_nn")
age_gender_in = device.getInputQueue("age_gender_in")
age_gender_nn = device.getOutputQueue("age_gender_nn")

bboxes = []
results = []
face_bbox_q = queue.Queue()
next_id = 0

if args.video:
    cap = cv2.VideoCapture(str(Path(args.video).resolve().absolute()))

fps = FPS()
fps.start()

def should_run():
    return cap.isOpened() if args.video else True

def get_frame():
    if args.video:
        return cap.read()
    else:
        return True, np.array(cam_out.get().getData()).reshape((3, 300, 300)).transpose(1, 2, 0).astype(np.uint8)

try:
    while should_run():
        read_correctly, frame = get_frame()

        if not read_correctly:
            break

        if frame is not None:
            fps.update()
            debug_frame = frame.copy()

            if not args.camera:
                nn_data = depthai.NNData()
                nn_data.setLayer("input", to_planar(frame, (300, 300)))
                detection_in.send(nn_data)

        while detection_nn.has():
            bboxes = np.array(detection_nn.get().getFirstLayerFp16())
            bboxes = bboxes.reshape((bboxes.size // 7, 7))
            bboxes = bboxes[bboxes[:, 2] > 0.7][:, 3:7]

            for raw_bbox in bboxes:
                bbox = frame_norm(frame, raw_bbox)
                det_frame = frame[bbox[1]:bbox[3], bbox[0]:bbox[2]]

                nn_data = depthai.NNData()
                nn_data.setLayer("data", to_planar(det_frame, (48, 96)))
                age_gender_in.send(nn_data)
                face_bbox_q.put(bbox)

        while age_gender_nn.has():
            det = age_gender_nn.get()
            age = int(float(np.squeeze(np.array(det.getLayerFp16('age_conv3')))) * 100)
            gender = np.squeeze(np.array(det.getLayerFp16('prob')))
            gender_str = "female" if gender[0] > gender[1] else "male"
            bbox = face_bbox_q.get()

            while not len(results) < len(bboxes) and len(results) > 0:
                results.pop(0)
            results.append({
                "bbox": bbox,
                "gender": gender_str,
                "age": age,
                "ts": time.time()
            })

        results = list(filter(lambda result: time.time() - result["ts"] < 0.2, results))

        if debug and frame is not None:
            for result in results:
                bbox = result["bbox"]
                cv2.rectangle(debug_frame, (bbox[0], bbox[1]), (bbox[2], bbox[3]), (10, 245, 10), 2)
                y = (bbox[1] + bbox[3]) // 2
                cv2.putText(debug_frame, str(result["age"]), (bbox[0], y), cv2.FONT_HERSHEY_TRIPLEX, 1.0, (255, 255, 255))
                cv2.putText(debug_frame, result["gender"], (bbox[0], y + 20), cv2.FONT_HERSHEY_TRIPLEX, 1.0, (255, 255, 255))

            aspect_ratio = frame.shape[1] / frame.shape[0]
            cv2.imshow("Camera_view", cv2.resize(debug_frame, (int(900),  int(900 / aspect_ratio))))
            if cv2.waitKey(1) == ord('q'):
                cv2.destroyAllWindows()
                break
except KeyboardInterrupt:
    pass

fps.stop()
print("FPS: {:.2f}".format(fps.fps()))
if args.video:
    cap.release()

max_stars_repo_name: dbandrews/depthai-experiments

max_issues_repo_name: dbandrews/depthai-experiments

max_forks_repo_name: dbandrews/depthai-experiments


text:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Apr 24 22:35:41 2019

@author: john.onwuemeka; Ge Li
"""

import numpy as np
import matplotlib.pyplot as plt
from obspy.core import read
from mtspec import mt_deconvolve
from obspy.signal.filter import lowpass

def plot_source_time_func(self,file1,file2,time_win):
    #adapted from mtspec manual (krischer, 2016)
    if self.wvtype == 'P':
        starttime1 = self.P_tt[self.mainev]
        starttime2 = self.P_tt[self.egfev]
    elif self.wvtype == 'S':
        starttime1 = self.S_tt[self.mainev]
        starttime2 = self.S_tt[self.egfev]
    st1 = read(file1)
    st2 = read(file2)
    delt = st1[0].stats.delta
    channel = st1[0].stats.channel
    chalist = [channel[0:2]+i for i in ('Z','N','E')]
    RSTF = {}
    for i in chalist:
        try:
            st1 = read(file1.replace(chalist[0],i),starttime=starttime1,endtime=starttime1+time_win)
            st2 = read(file2.replace(chalist[0],i),starttime=starttime2,endtime=starttime2+time_win)
            st_mtd = mt_deconvolve(st1[0].data, st2[0].data, delt,nfft=len(st1[0].data),
                      time_bandwidth=4, number_of_tapers=7,weights='constant',
                      demean=True)
        except OSError:
            pass
        st_dec = st_mtd['deconvolved']
#        freq_dec = st_mtd['frequencies']
        xlen = len(st_dec)
        time = np.linspace(0, xlen*delt,xlen)
        l1 = np.arange(0, xlen)
        index1 = np.where((l1 >= 0,l1 <= xlen / 2))[0]
        sla = st_dec[index1]
#        sla_freq = freq_dec[index1[:-1]]
        index2 = np.where((l1 > xlen / 2) & (l1 <= xlen + 1))[0]
        slb = st_dec[index2]
#        slb_freq = freq_dec[index2[:-1]]
        slba = np.concatenate((sla, slb))
#        slba_freq = np.concatenate((sla_freq, slb_freq))
#        index3 = np.where(slba == max(slba))[0]
#        freqmin = 2*slba_freq[index3[:-1]]
        slba = lowpass(slba, 5, st1[0].stats.sampling_rate, corners=4, zerophase=True)
        slba /= slba.max()
        RSTF[i] = []
        RSTF[i] = slba
    RSTF = sum(RSTF.values())
    RSTF /= RSTF.max()
    return time,RSTF
#    fig.save(self.output_dir+'/'+evname1+'_STF.pdf',dpi=200)

max_stars_repo_name: Johumel/SAES

max_issues_repo_name: Johumel/SAES

max_forks_repo_name: Johumel/SAES


text:
#================================================================
#
#   File name   : utils.py
#   Author      : PyLessons
#   Created date: 2020-09-27
#   Website     : https://pylessons.com/
#   GitHub      : https://github.com/pythonlessons/TensorFlow-2.x-YOLOv3
#   Description : additional yolov3 and yolov4 functions
#
#================================================================
from multiprocessing import Process, Queue, Pipe
import cv2
import time
import random
import colorsys
import numpy as np
import tensorflow as tf
from yolov3.configs import *
from yolov3.yolov4 import *
from tensorflow.python.saved_model import tag_constants

def load_yolo_weights(model, weights_file):
    tf.keras.backend.clear_session() # used to reset layer names
    # load Darknet original weights to TensorFlow model
    if YOLO_TYPE == "yolov3":
        range1 = 75 if not TRAIN_YOLO_TINY else 13
        range2 = [58, 66, 74] if not TRAIN_YOLO_TINY else [9, 12]
    if YOLO_TYPE == "yolov4":
        range1 = 110 if not TRAIN_YOLO_TINY else 21
        range2 = [93, 101, 109] if not TRAIN_YOLO_TINY else [17, 20]

    with open(weights_file, 'rb') as wf:
        major, minor, revision, seen, _ = np.fromfile(wf, dtype=np.int32, count=5)

        j = 0
        for i in range(range1):
            if i > 0:
                conv_layer_name = 'conv2d_%d' %i
            else:
                conv_layer_name = 'conv2d'

            if j > 0:
                bn_layer_name = 'batch_normalization_%d' %j
            else:
                bn_layer_name = 'batch_normalization'

            conv_layer = model.get_layer(conv_layer_name)
            filters = conv_layer.filters
            k_size = conv_layer.kernel_size[0]
            in_dim = conv_layer.input_shape[-1]

            if i not in range2:
                # darknet weights: [beta, gamma, mean, variance]
                bn_weights = np.fromfile(wf, dtype=np.float32, count=4 * filters)
                # tf weights: [gamma, beta, mean, variance]
                bn_weights = bn_weights.reshape((4, filters))[[1, 0, 2, 3]]
                bn_layer = model.get_layer(bn_layer_name)
                j += 1
            else:
                conv_bias = np.fromfile(wf, dtype=np.float32, count=filters)

            # darknet shape (out_dim, in_dim, height, width)
            conv_shape = (filters, in_dim, k_size, k_size)
            conv_weights = np.fromfile(wf, dtype=np.float32, count=np.product(conv_shape))
            # tf shape (height, width, in_dim, out_dim)
            conv_weights = conv_weights.reshape(conv_shape).transpose([2, 3, 1, 0])

            if i not in range2:
                conv_layer.set_weights([conv_weights])
                bn_layer.set_weights(bn_weights)
            else:
                conv_layer.set_weights([conv_weights, conv_bias])

        assert len(wf.read()) == 0, 'failed to read all data'

def Load_Yolo_model():
    gpus = tf.config.experimental.list_physical_devices('GPU')
    if len(gpus) > 0:
        print(f'GPUs {gpus}')
        try: tf.config.experimental.set_memory_growth(gpus[0], True)
        except RuntimeError: pass

    if YOLO_FRAMEWORK == "tf": # TensorFlow detection
        if YOLO_TYPE == "yolov4":
            Darknet_weights = YOLO_V4_TINY_WEIGHTS if TRAIN_YOLO_TINY else YOLO_V4_WEIGHTS
        if YOLO_TYPE == "yolov3":
            Darknet_weights = YOLO_V3_TINY_WEIGHTS if TRAIN_YOLO_TINY else YOLO_V3_WEIGHTS

        if YOLO_CUSTOM_WEIGHTS == False:
            print("Loading Darknet_weights from:", Darknet_weights)
            yolo = Create_Yolo(input_size=YOLO_INPUT_SIZE, CLASSES=YOLO_COCO_CLASSES)
            load_yolo_weights(yolo, Darknet_weights) # use Darknet weights
        else:
            print("Loading custom weights from:", YOLO_CUSTOM_WEIGHTS)
            yolo = Create_Yolo(input_size=YOLO_INPUT_SIZE, CLASSES=TRAIN_CLASSES)
            yolo.load_weights(f"./checkpoints/{TRAIN_MODEL_NAME}") # use custom weights

    elif YOLO_FRAMEWORK == "trt": # TensorRT detection
        saved_model_loaded = tf.saved_model.load(YOLO_CUSTOM_WEIGHTS, tags=[tag_constants.SERVING])
        signature_keys = list(saved_model_loaded.signatures.keys())
        yolo = saved_model_loaded.signatures['serving_default']

    return yolo

def image_preprocess(image, target_size, gt_boxes=None):
    ih, iw    = target_size
    h,  w, _  = image.shape

    scale = min(iw/w, ih/h)
    nw, nh  = int(scale * w), int(scale * h)
    image_resized = cv2.resize(image, (nw, nh))

    image_paded = np.full(shape=[ih, iw, 3], fill_value=128.0)
    dw, dh = (iw - nw) // 2, (ih-nh) // 2
    image_paded[dh:nh+dh, dw:nw+dw, :] = image_resized
    image_paded = image_paded / 255.

    if gt_boxes is None:
        return image_paded

    else:
        gt_boxes[:, [0, 2]] = gt_boxes[:, [0, 2]] * scale + dw
        gt_boxes[:, [1, 3]] = gt_boxes[:, [1, 3]] * scale + dh
        return image_paded, gt_boxes

def draw_detections(image, bboxes, birds_eye=None, combinations=None, distances=None, centroids=None, centroids_transformed=None, dist_threshold = None):
    image_h, image_w, _ = image.shape

    # Set thickness of pen
    bbox_thick = int(0.6 * (image_h + image_w)/1000)
    if bbox_thick < 1: bbox_thick = 1
    fontScale = 0.75 * bbox_thick

    # Combinations wasn't supplied, just draw bboxes
    if not combinations:
        for i, bbox in enumerate(bboxes):
            coor = np.array(bbox[:4], dtype=np.int32)
            score = bbox[4]

            (x1, y1), (x2, y2) = (coor[0], coor[1]), (coor[2], coor[3])
            # put object rectangle
            cv2.rectangle(image, (x1, y1), (x2, y2), (255,255,0), bbox_thick*2)

            # get text label
            label = " {:.2f}".format(score)

            # get text size
            (text_width, text_height), baseline = cv2.getTextSize(label, cv2.FONT_HERSHEY_COMPLEX_SMALL,
                                                                  fontScale, thickness=bbox_thick)
            # put filled text rectangle
            cv2.rectangle(image, (x1, y1), (x1 + text_width, y1 - text_height - baseline), (255,255,0), thickness=cv2.FILLED)

            # put text above rectangle
            cv2.putText(image, label, (x1, y1-4), cv2.FONT_HERSHEY_COMPLEX_SMALL,
                        fontScale, (255,255,255), bbox_thick, lineType=cv2.LINE_AA)

        return image
    # Need to draw violation lines too
    red_combinations = []
    green_combinations = []

    # Fetch red and green combinations (violating, not violating the distance threshold)
    for dist_count, pair in enumerate(combinations):
        if distances[dist_count] <= dist_threshold:
            red_combinations.append(pair)
        else:
            green_combinations.append(pair)

    # Fetch the unique box IDs in red and green
    red_unique = list(set(list(map(lambda x: x[0], red_combinations))+list(map(lambda x: x[1], red_combinations))))
    green_unique = list(set(list(map(lambda x: x[0], green_combinations))+list(map(lambda x: x[1], green_combinations))))

    green_only = list(np.setdiff1d(green_unique, red_unique))

    # Drawing green bounding boxes on OG Frame and Green dots in birds eye frame
    for id in green_only:
        bbox = bboxes[id]
        coor = np.array(bbox[:4], dtype=np.int32)
        score = bbox[4]
        (x1, y1), (x2, y2) = (coor[0], coor[1]), (coor[2], coor[3])

        # put object rectangle and label
        cv2.rectangle(image, (x1, y1), (x2, y2), (255,255,0), bbox_thick*2)
        label = " {:.2f}".format(score)
        (text_width, text_height), baseline = cv2.getTextSize(label, cv2.FONT_HERSHEY_COMPLEX_SMALL,
                                                                  fontScale, thickness=bbox_thick)
        # Put filled rect
        cv2.rectangle(image, (x1, y1), (x1 + text_width, y1 - text_height - baseline), (255,255,0), thickness=cv2.FILLED)
        # Text above rect
        cv2.putText(image, label, (x1, y1-4), cv2.FONT_HERSHEY_COMPLEX_SMALL,
                        fontScale, (255,255,255), bbox_thick, lineType=cv2.LINE_AA)

        # Dots in birdseye
        birds_eye = cv2.circle(birds_eye, centroids_transformed[id], 3, (255,255,0),-1)

    # Drawing red bounding boxes,lines, red dots and lines in birds eye
    drawn_boxes = np.zeros((len(bboxes)), dtype=np.uint8)
    for id, pair in enumerate(red_combinations):
        pairs = [pair[0], pair[1]]

        # Draw red bounding box
        for box_id in pairs:
            if drawn_boxes[box_id] == 0:
                drawn_boxes[box_id] = 1

                bbox = bboxes[box_id]
                coor = np.array(bbox[:4], dtype=np.int32)
                score = bbox[4]
                (x1, y1), (x2, y2) = (coor[0], coor[1]), (coor[2], coor[3])
                # put object rectangle and label
                cv2.rectangle(image, (x1, y1), (x2, y2), (0,0,255), bbox_thick*2)
                label = " {:.2f}".format(score)
                (text_width, text_height), baseline = cv2.getTextSize(label, cv2.FONT_HERSHEY_COMPLEX_SMALL,
                                                                  fontScale, thickness=bbox_thick)
                # Put filled rect
                cv2.rectangle(image, (x1, y1), (x1 + text_width, y1 - text_height - baseline), (0,0,255), thickness=cv2.FILLED)
                # Text above rect
                cv2.putText(image, label, (x1, y1-4), cv2.FONT_HERSHEY_COMPLEX_SMALL,
                            fontScale, (255,255,255), bbox_thick, lineType=cv2.LINE_AA)

                # Dots in birdseye
                birds_eye = cv2.circle(birds_eye, centroids_transformed[box_id], 3, (0,0,255),-1)

        # Draw lines OG frame, birds eye view
        cv2.line(image, centroids[pair[0]], centroids[pair[1]], (0,0,255), bbox_thick)
        cv2.line(birds_eye, centroids_transformed[pair[0]], centroids_transformed[pair[1]], (0,0,255), 1)

    # Print number of violations
    num_violations = len(red_unique)
    label = "Number of violations: {}".format(num_violations)

    # put filled text rectangle
    start_x = int(image_w/30)
    start_y = int(image_h/25)
    fontScale = int(0.5 * min(image_h, image_w) / 1000)
    if fontScale < 1: fontScale = 1

    (text_width, text_height), baseline = cv2.getTextSize(label, cv2.FONT_HERSHEY_COMPLEX_SMALL,
                                                              fontScale, thickness=bbox_thick)

    # OG Frame
    cv2.rectangle(image, (start_x, start_y), (start_x + text_width, start_y - text_height - baseline), (0,0,255), thickness=cv2.FILLED)
    cv2.putText(image, label, (start_x, start_y), cv2.FONT_HERSHEY_COMPLEX_SMALL, fontScale, (255,255,255), bbox_thick,lineType=cv2.LINE_AA)

    # Birds eye frame
    cv2.rectangle(birds_eye, (start_x, start_y), (start_x + text_width, start_y - text_height - baseline), (0,0,255), thickness=cv2.FILLED)
    cv2.putText(birds_eye,label,(start_x, start_y),cv2.FONT_HERSHEY_COMPLEX_SMALL,fontScale,(255,255,255), bbox_thick,lineType=cv2.LINE_AA)

    # Return
    return image, birds_eye

def bboxes_iou(boxes1, boxes2):
    boxes1 = np.array(boxes1)
    boxes2 = np.array(boxes2)

    boxes1_area = (boxes1[..., 2] - boxes1[..., 0]) * (boxes1[..., 3] - boxes1[..., 1])
    boxes2_area = (boxes2[..., 2] - boxes2[..., 0]) * (boxes2[..., 3] - boxes2[..., 1])

    left_up       = np.maximum(boxes1[..., :2], boxes2[..., :2])
    right_down    = np.minimum(boxes1[..., 2:], boxes2[..., 2:])

    inter_section = np.maximum(right_down - left_up, 0.0)
    inter_area    = inter_section[..., 0] * inter_section[..., 1]
    union_area    = boxes1_area + boxes2_area - inter_area
    ious          = np.maximum(1.0 * inter_area / union_area, np.finfo(np.float32).eps)

    return ious

def nms(bboxes, iou_threshold, sigma=0.3, method='nms', limits = None):
    """
    :param bboxes: (xmin, ymin, xmax, ymax, score, class)

    Note: soft-nms, https://arxiv.org/pdf/1704.04503.pdf
          https://github.com/bharatsingh430/soft-nms
    """
    min_limits = limits[0]
    max_limits = limits[1]

    classes_in_img = list(set(bboxes[:, 5]))
    CLASSES = YOLO_COCO_CLASSES
    NUM_CLASS  = read_class_names(CLASSES)
    best_bboxes = []
    for cls in classes_in_img:
        cls_mask = (bboxes[:, 5] == cls)
        cls_bboxes = bboxes[cls_mask]
        # Process 1: Determine whether the number of bounding boxes is greater than 0
        while len(cls_bboxes) > 0:
            # Process 2: Select the bounding box with the highest score according to socre order A
            max_ind = np.argmax(cls_bboxes[:, 4])
            best_bbox = cls_bboxes[max_ind]
            if NUM_CLASS[int(best_bbox[5])] == "person":
                if best_bbox[0] >= min_limits[0] and best_bbox[1] >= min_limits[1]:
                    if best_bbox[2] <= max_limits[0] and best_bbox[3] <= max_limits[1]:
                        best_bboxes.append(best_bbox)
            cls_bboxes = np.concatenate([cls_bboxes[: max_ind], cls_bboxes[max_ind + 1:]])
            # Process 3: Calculate this bounding box A and
            # Remain all iou of the bounding box and remove those bounding boxes whose iou value is higher than the threshold
            iou = bboxes_iou(best_bbox[np.newaxis, :4], cls_bboxes[:, :4])
            weight = np.ones((len(iou),), dtype=np.float32)

            assert method in ['nms', 'soft-nms']

            if method == 'nms':
                iou_mask = iou > iou_threshold
                weight[iou_mask] = 0.0

            if method == 'soft-nms':
                weight = np.exp(-(1.0 * iou ** 2 / sigma))

            cls_bboxes[:, 4] = cls_bboxes[:, 4] * weight
            score_mask = cls_bboxes[:, 4] > 0.
            cls_bboxes = cls_bboxes[score_mask]

    return best_bboxes

def postprocess_boxes(pred_bbox, original_image, input_size, score_threshold):
    valid_scale=[0, np.inf]
    pred_bbox = np.array(pred_bbox)

    pred_xywh = pred_bbox[:, 0:4]
    pred_conf = pred_bbox[:, 4]
    pred_prob = pred_bbox[:, 5:]

    # 1. (x, y, w, h) --> (xmin, ymin, xmax, ymax)
    pred_coor = np.concatenate([pred_xywh[:, :2] - pred_xywh[:, 2:] * 0.5,
                                pred_xywh[:, :2] + pred_xywh[:, 2:] * 0.5], axis=-1)
    # 2. (xmin, ymin, xmax, ymax) -> (xmin_org, ymin_org, xmax_org, ymax_org)
    org_h, org_w = original_image.shape[:2]
    resize_ratio = min(input_size / org_w, input_size / org_h)

    dw = (input_size - resize_ratio * org_w) / 2
    dh = (input_size - resize_ratio * org_h) / 2

    pred_coor[:, 0::2] = 1.0 * (pred_coor[:, 0::2] - dw) / resize_ratio
    pred_coor[:, 1::2] = 1.0 * (pred_coor[:, 1::2] - dh) / resize_ratio

    # 3. clip some boxes those are out of range
    pred_coor = np.concatenate([np.maximum(pred_coor[:, :2], [0, 0]),
                                np.minimum(pred_coor[:, 2:], [org_w - 1, org_h - 1])], axis=-1)
    invalid_mask = np.logical_or((pred_coor[:, 0] > pred_coor[:, 2]), (pred_coor[:, 1] > pred_coor[:, 3]))
    pred_coor[invalid_mask] = 0

    # 4. discard some invalid boxes
    bboxes_scale = np.sqrt(np.multiply.reduce(pred_coor[:, 2:4] - pred_coor[:, 0:2], axis=-1))
    scale_mask = np.logical_and((valid_scale[0] < bboxes_scale), (bboxes_scale < valid_scale[1]))

    # 5. discard boxes with low scores
    classes = np.argmax(pred_prob, axis=-1)
    scores = pred_conf * pred_prob[np.arange(len(pred_coor)), classes]
    score_mask = scores > score_threshold
    mask = np.logical_and(scale_mask, score_mask)
    coors, scores, classes = pred_coor[mask], scores[mask], classes[mask]

    return np.concatenate([coors, scores[:, np.newaxis], classes[:, np.newaxis]], axis=-1)

def detect_image(Yolo, image_path, output_path, input_size=416, show=False, CLASSES=YOLO_COCO_CLASSES, score_threshold=0.3, iou_threshold=0.45, rectangle_colors=''):
    original_image      = cv2.imread(image_path)
    original_image      = cv2.cvtColor(original_image, cv2.COLOR_BGR2RGB)
    original_image      = cv2.cvtColor(original_image, cv2.COLOR_BGR2RGB)

    image_data = image_preprocess(np.copy(original_image), [input_size, input_size])
    image_data = image_data[np.newaxis, ...].astype(np.float32)

    if YOLO_FRAMEWORK == "tf":
        pred_bbox = Yolo.predict(image_data)
    elif YOLO_FRAMEWORK == "trt":
        batched_input = tf.constant(image_data)
        result = Yolo(batched_input)
        pred_bbox = []
        for key, value in result.items():
            value = value.numpy()
            pred_bbox.append(value)

    pred_bbox = [tf.reshape(x, (-1, tf.shape(x)[-1])) for x in pred_bbox]
    pred_bbox = tf.concat(pred_bbox, axis=0)

    bboxes = postprocess_boxes(pred_bbox, original_image, input_size, score_threshold)
    bboxes = nms(bboxes, iou_threshold, method='nms')

    image = draw_bbox(original_image, bboxes, CLASSES=CLASSES, rectangle_colors=rectangle_colors)
    # CreateXMLfile("XML_Detections", str(int(time.time())), original_image, bboxes, read_class_names(CLASSES))

    if output_path != '': cv2.imwrite(output_path, image)
    if show:
        # Show the image
        cv2.imshow("predicted image", image)
        # Load and hold the image
        cv2.waitKey(0)
        # To close the window after the required kill value was provided
        cv2.destroyAllWindows()

    return image

def Predict_bbox_mp(Frames_data, Predicted_data, Processing_times):
    gpus = tf.config.experimental.list_physical_devices('GPU')
    if len(gpus) > 0:
        try: tf.config.experimental.set_memory_growth(gpus[0], True)
        except RuntimeError: print("RuntimeError in tf.config.experimental.list_physical_devices('GPU')")
    Yolo = Load_Yolo_model()
    times = []
    while True:
        if Frames_data.qsize()>0:
            image_data = Frames_data.get()
            t1 = time.time()
            Processing_times.put(time.time())

            if YOLO_FRAMEWORK == "tf":
                pred_bbox = Yolo.predict(image_data)
            elif YOLO_FRAMEWORK == "trt":
                batched_input = tf.constant(image_data)
                result = Yolo(batched_input)
                pred_bbox = []
                for key, value in result.items():
                    value = value.numpy()
                    pred_bbox.append(value)

            pred_bbox = [tf.reshape(x, (-1, tf.shape(x)[-1])) for x in pred_bbox]
            pred_bbox = tf.concat(pred_bbox, axis=0)

            Predicted_data.put(pred_bbox)

def postprocess_mp(Predicted_data, original_frames, Processed_frames, Processing_times, input_size, CLASSES, score_threshold, iou_threshold, rectangle_colors, realtime):
    times = []
    while True:
        if Predicted_data.qsize()>0:
            pred_bbox = Predicted_data.get()
            if realtime:
                while original_frames.qsize() > 1:
                    original_image = original_frames.get()
            else:
                original_image = original_frames.get()

            bboxes = postprocess_boxes(pred_bbox, original_image, input_size, score_threshold)
            bboxes = nms(bboxes, iou_threshold, method='nms')
            image = draw_bbox(original_image, bboxes, CLASSES=CLASSES, rectangle_colors=rectangle_colors)
            times.append(time.time()-Processing_times.get())
            times = times[-20:]

            ms = sum(times)/len(times)*1000
            fps = 1000 / ms
            image = cv2.putText(image, "Time: {:.1f}FPS".format(fps), (0, 30), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (0, 0, 255), 2)
            #print("Time: {:.2f}ms, Final FPS: {:.1f}".format(ms, fps))

            Processed_frames.put(image)

def Show_Image_mp(Processed_frames, show, Final_frames):
    while True:
        if Processed_frames.qsize()>0:
            image = Processed_frames.get()
            Final_frames.put(image)
            if show:
                cv2.imshow('output', image)
                if cv2.waitKey(25) & 0xFF == ord("q"):
                    cv2.destroyAllWindows()
                    break

# detect from webcam
def detect_video_realtime_mp(video_path, output_path, input_size=416, show=False, CLASSES=YOLO_COCO_CLASSES, score_threshold=0.3, iou_threshold=0.45, rectangle_colors='', realtime=False):
    if realtime:
        vid = cv2.VideoCapture(0)
    else:
        vid = cv2.VideoCapture(video_path)

    # by default VideoCapture returns float instead of int
    width = int(vid.get(cv2.CAP_PROP_FRAME_WIDTH))
    height = int(vid.get(cv2.CAP_PROP_FRAME_HEIGHT))
    fps = int(vid.get(cv2.CAP_PROP_FPS))
    codec = cv2.VideoWriter_fourcc(*'XVID')
    out = cv2.VideoWriter(output_path, codec, fps, (width, height)) # output_path must be .mp4
    no_of_frames = int(vid.get(cv2.CAP_PROP_FRAME_COUNT))

    original_frames = Queue()
    Frames_data = Queue()
    Predicted_data = Queue()
    Processed_frames = Queue()
    Processing_times = Queue()
    Final_frames = Queue()

    p1 = Process(target=Predict_bbox_mp, args=(Frames_data, Predicted_data, Processing_times))
    p2 = Process(target=postprocess_mp, args=(Predicted_data, original_frames, Processed_frames, Processing_times, input_size, CLASSES, score_threshold, iou_threshold, rectangle_colors, realtime))
    p3 = Process(target=Show_Image_mp, args=(Processed_frames, show, Final_frames))
    p1.start()
    p2.start()
    p3.start()

    while True:
        ret, img = vid.read()
        if not ret:
            break

        original_image = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        original_image = cv2.cvtColor(original_image, cv2.COLOR_BGR2RGB)
        original_frames.put(original_image)

        image_data = image_preprocess(np.copy(original_image), [input_size, input_size])
        image_data = image_data[np.newaxis, ...].astype(np.float32)
        Frames_data.put(image_data)

    while True:
        if original_frames.qsize() == 0 and Frames_data.qsize() == 0 and Predicted_data.qsize() == 0  and Processed_frames.qsize() == 0  and Processing_times.qsize() == 0 and Final_frames.qsize() == 0:
            p1.terminate()
            p2.terminate()
            p3.terminate()
            break
        elif Final_frames.qsize()>0:
            image = Final_frames.get()
            if output_path != '': out.write(image)

    cv2.destroyAllWindows()

def detect_video(Yolo, video_path, output_path, input_size=416, show=False, CLASSES=YOLO_COCO_CLASSES, score_threshold=0.3, iou_threshold=0.45, rectangle_colors=''):
    times, times_2 = [], []
    vid = cv2.VideoCapture(video_path)

    # by default VideoCapture returns float instead of int
    width = int(vid.get(cv2.CAP_PROP_FRAME_WIDTH))
    height = int(vid.get(cv2.CAP_PROP_FRAME_HEIGHT))
    fps = int(vid.get(cv2.CAP_PROP_FPS))
    codec = cv2.VideoWriter_fourcc(*'XVID')
    out = cv2.VideoWriter(output_path, codec, fps, (width, height)) # output_path must be .mp4

    while True:
        _, img = vid.read()

        try:
            original_image = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
            original_image = cv2.cvtColor(original_image, cv2.COLOR_BGR2RGB)
        except:
            break

        image_data = image_preprocess(np.copy(original_image), [input_size, input_size])
        image_data = image_data[np.newaxis, ...].astype(np.float32)

        t1 = time.time()
        if YOLO_FRAMEWORK == "tf":
            pred_bbox = Yolo.predict(image_data)
        elif YOLO_FRAMEWORK == "trt":
            batched_input = tf.constant(image_data)
            result = Yolo(batched_input)
            pred_bbox = []
            for key, value in result.items():
                value = value.numpy()
                pred_bbox.append(value)

        t2 = time.time()

        pred_bbox = [tf.reshape(x, (-1, tf.shape(x)[-1])) for x in pred_bbox]
        pred_bbox = tf.concat(pred_bbox, axis=0)

        bboxes = postprocess_boxes(pred_bbox, original_image, input_size, score_threshold)
        bboxes = nms(bboxes, iou_threshold, method='nms')

        image = draw_bbox(original_image, bboxes, CLASSES=CLASSES, rectangle_colors=rectangle_colors)

        t3 = time.time()
        times.append(t2-t1)
        times_2.append(t3-t1)

        times = times[-20:]
        times_2 = times_2[-20:]

        ms = sum(times)/len(times)*1000
        fps = 1000 / ms
        fps2 = 1000 / (sum(times_2)/len(times_2)*1000)

        image = cv2.putText(image, "Time: {:.1f}FPS".format(fps), (0, 30), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (0, 0, 255), 2)
        # CreateXMLfile("XML_Detections", str(int(time.time())), original_image, bboxes, read_class_names(CLASSES))

        print("Time: {:.2f}ms, Detection FPS: {:.1f}, total FPS: {:.1f}".format(ms, fps, fps2))
        if output_path != '': out.write(image)
        if show:
            cv2.imshow('output', image)
            if cv2.waitKey(25) & 0xFF == ord("q"):
                cv2.destroyAllWindows()
                break

    cv2.destroyAllWindows()

# detect from webcam
def detect_realtime(Yolo, output_path, input_size=416, show=False, CLASSES=YOLO_COCO_CLASSES, score_threshold=0.3, iou_threshold=0.45, rectangle_colors=''):
    times = []
    vid = cv2.VideoCapture(0)

    # by default VideoCapture returns float instead of int
    width = int(vid.get(cv2.CAP_PROP_FRAME_WIDTH))
    height = int(vid.get(cv2.CAP_PROP_FRAME_HEIGHT))
    fps = int(vid.get(cv2.CAP_PROP_FPS))
    codec = cv2.VideoWriter_fourcc(*'XVID')
    out = cv2.VideoWriter(output_path, codec, fps, (width, height)) # output_path must be .mp4

    while True:
        _, frame = vid.read()

        try:
            original_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
            original_frame = cv2.cvtColor(original_frame, cv2.COLOR_BGR2RGB)
        except:
            break
        image_data = image_preprocess(np.copy(original_frame), [input_size, input_size])
        image_data = image_data[np.newaxis, ...].astype(np.float32)

        t1 = time.time()
        if YOLO_FRAMEWORK == "tf":
            pred_bbox = Yolo.predict(image_data)
        elif YOLO_FRAMEWORK == "trt":
            batched_input = tf.constant(image_data)
            result = Yolo(batched_input)
            pred_bbox = []
            for key, value in result.items():
                value = value.numpy()
                pred_bbox.append(value)

        t2 = time.time()

        pred_bbox = [tf.reshape(x, (-1, tf.shape(x)[-1])) for x in pred_bbox]
        pred_bbox = tf.concat(pred_bbox, axis=0)

        bboxes = postprocess_boxes(pred_bbox, original_frame, input_size, score_threshold)
        bboxes = nms(bboxes, iou_threshold, method='nms')

        times.append(t2-t1)
        times = times[-20:]

        ms = sum(times)/len(times)*1000
        fps = 1000 / ms

        print("Time: {:.2f}ms, {:.1f} FPS".format(ms, fps))

        frame = draw_bbox(original_frame, bboxes, CLASSES=CLASSES, rectangle_colors=rectangle_colors)
        # CreateXMLfile("XML_Detections", str(int(time.time())), original_frame, bboxes, read_class_names(CLASSES))
        image = cv2.putText(frame, "Time: {:.1f}FPS".format(fps), (0, 30),
                          cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (0, 0, 255), 2)

        if output_path != '': out.write(frame)
        if show:
            cv2.imshow('output', frame)
            if cv2.waitKey(25) & 0xFF == ord("q"):
                cv2.destroyAllWindows()
                break

    cv2.destroyAllWindows()

max_stars_repo_name: hassaanakram/social-distance-estimation

max_issues_repo_name: hassaanakram/social-distance-estimation

max_forks_repo_name: hassaanakram/social-distance-estimation


text:
import numpy as np
from scipy.special import spherical_jn
from scipy.misc import derivative
from scipy.interpolate import InterpolatedUnivariateSpline as ius
from scipy.integrate import quadrature

import sys
sys.path.append('../utils/')
from tools import loginterp

j0 = lambda x: spherical_jn(0, x)

#Get model as parameter
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('-m', '--model', help='model name to use', default='ModelA')
parser.add_argument('-s', '--size', help='for small or big box', default='big')
args = parser.parse_args()
print(args, args.model)

model = args.model
boxsize = args.size

alist    = [0.1429,0.1538,0.1667,0.1818,0.2000,0.2222,0.2500,0.2857,0.3333]
suff = 'm1_00p3mh-alpha-0p8-subvol'
if boxsize == 'big':
    suff = suff + '-big'
    bs = 1024
else: bs = 256

outfolder = '../data/outputs/' + suff[:]
outfolder += "/%s/"%model

#

def get_ilpk(aa):
    '''return log-interpolated pk at redshift z(aa) for Hankel transform'''
    pkd = np.loadtxt(outfolder + "HI_bias_{:06.4f}.txt".format(aa))[1:,:]
    kk, pk = pkd[:, 0], pkd[:, 2]**2 * pkd[:, 3]

    #on large scales, extend with b^2*P_lin
    klin, plin = np.loadtxt('../data/pk_Planck2018BAO_matterpower_z000.dat', unpack=True)
    ipklin = ius(klin, plin)

    kt = np.concatenate((klin[(klin < kk[0])], kk))
    pt = np.concatenate((plin[(klin < kk[0])]*pk[0]/ipklin(kk[0]), pk))

    #On small scales, truncate at k=1
    pt = pt[kt<1]
    kt = kt[kt<1]
    ilpk = loginterp(kt, pt)
    return ilpk

def xij0f(k, r, ipk):
    '''return integrand for hankel transform with j0'''
    return k**3*ipk(k)/2/np.pi**2 *j0(k*r)/k

if __name__=="__main__":

    r = np.linspace(1, 120, 5)
    for iz, aa in enumerate(alist):
        zz = 1/aa-1
        print('For redshift : z = ', zz)

        xi = np.zeros_like(r)
        ilpk = get_ilpk(aa)

        for i in range(r.size):
            if i%1 == 0: print(i, r[i])
            f = lambda k: xij0f(k, r[i], ilpk)
            xi[i] = quadrature(f, 1e-5, 1e2, maxiter=1000)[0]

        np.savetxt(outfolder + "HI_hankelxi_{:06.4f}.txt".format(aa), np.stack((r, xi)).T, header='r, xi')

reason: import numpy,from scipy


text:
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you 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 numpy as np
import tvm
import tvm.testing
from tvm import topi
import tvm.topi.testing

@tvm.testing.parametrize_targets
def test_cumsum(ctx, target):
    def check_cumsum(np_ref, data, axis=None, dtype=None):
        implementations = {
            "generic": (lambda x: topi.cumsum(x, axis, dtype), topi.generic.schedule_extern),
            "cuda": (lambda x: topi.cuda.cumsum(x, axis, dtype), topi.cuda.schedule_scan),
            "nvptx": (lambda x: topi.cuda.cumsum(x, axis, dtype), topi.cuda.schedule_scan),
            "vulkan": (lambda x: topi.cuda.cumsum(x, axis, dtype), topi.cuda.schedule_scan),
            "metal": (lambda x: topi.cuda.cumsum(x, axis, dtype), topi.cuda.schedule_scan),
        }
        fcompute, fschedule = tvm.topi.testing.dispatch(target, implementations)
        tvm.topi.testing.compare_numpy_tvm([data], np_ref, target, ctx, fcompute, fschedule)

    data = np.array([2, 3, 0])
    check_cumsum(np.cumsum(data), data)

    data = np.random.rand(10) > 0.5
    data = data.astype(np.int32)
    check_cumsum(np.cumsum(data, dtype=np.int32), data)
    check_cumsum(np.cumsum(data), data, dtype="int64")

    data = np.random.rand(10) > 0.5
    check_cumsum(np.cumsum(data, dtype=np.int32), data, dtype="int32")

    for in_dtype in ["float32", "float64"]:
        if target == "metal" and in_dtype == "float64":
            # float64 is not supported in metal
            continue
        data = np.random.randn(10, 10).astype(in_dtype)
        check_cumsum(np.cumsum(data), data)
        check_cumsum(np.cumsum(data, axis=0), data, axis=0)
        check_cumsum(np.cumsum(data, axis=1), data, axis=1)

        data = np.random.randn(10, 5, 10).astype(in_dtype)
        check_cumsum(np.cumsum(data), data)
        check_cumsum(np.cumsum(data, axis=0), data, axis=0)
        check_cumsum(np.cumsum(data, axis=1), data, axis=1)
        check_cumsum(np.cumsum(data, axis=-1), data, axis=-1)

    for in_dtype in ["int32", "int64"]:
        data = np.random.randint(-100, 100, size=(100, 100)).astype(in_dtype)
        check_cumsum(np.cumsum(data, dtype=in_dtype), data)
        check_cumsum(np.cumsum(data), data, dtype="int64")
        check_cumsum(np.cumsum(data, axis=0, dtype=in_dtype), data, axis=0)
        check_cumsum(np.cumsum(data, axis=1, dtype=in_dtype), data, axis=1)

        data = np.random.randint(1 << 30, (1 << 31) - 1, size=(100)).astype(in_dtype)
        check_cumsum(np.cumsum(data), data, dtype="int64")

if __name__ == "__main__":
    test_cumsum(tvm.context("cpu"), tvm.target.Target("llvm"))
    test_cumsum(tvm.context("cuda"), tvm.target.Target("cuda"))
    test_cumsum(tvm.context("nvptx"), tvm.target.Target("nvptx"))
    test_cumsum(tvm.context("vulkan"), tvm.target.Target("vulkan"))
    test_cumsum(tvm.context("metal"), tvm.target.Target("metal"))

max_stars_repo_name: eleflea/tvm

max_issues_repo_name: eleflea/tvm

max_forks_repo_name: eleflea/tvm


text:
import numpy as np
import math
import time
from GridMean import *
import cv2
from Parameters import *

class MappingTemplate(object):
    def __init__(self, size, large_size=40, scale=0.1):
        """
        this parent class is used as a template for all other classes needing a map
        :param size: float, or int of map real world size
        """
        self.size = size
        self.large_size = large_size
        # scale is size of each grid cell
        self.scale = 0.1
        self.small_num_cells = math.ceil(self.size / self.scale)
        self.small_map = np.zeros([self.small_num_cells, self.small_num_cells])
        self.small_count = np.zeros([self.small_num_cells, self.small_num_cells], dtype=np.int)
        self.small_coded = np.zeros([self.small_num_cells, self.small_num_cells], dtype=np.uint8)
        # create large maps
        self.large_num_cells = math.ceil(self.large_size / self.scale)
        self.large_map = np.zeros([self.large_num_cells, self.large_num_cells])
        self.large_count = np.zeros([self.large_num_cells, self.large_num_cells], dtype=np.int)
        self.large_coded = np.zeros([self.large_num_cells, self.large_num_cells], dtype=np.uint8)
        # mid point of matrices of large matrices
        self.mid = math.ceil(self.large_size / (2 * self.scale))
        # half size of small matrices
        self.half_small = math.ceil(self.size / (2 * self.scale))
        # create codes for maps
        self.map_codes = MapCodes

    def snippet(self, pos_row, pos_col, large_mat):
        """
        takes a small portion of large map
        :param pos_row: mid point row of where small mat should be taken
        :param pos_col: same as above
        :param large_mat: matrix which is taken from
        :return: smaller square matrix of shape "half small * 2"
        """
        return large_mat[pos_row - self.half_small:pos_row + self.half_small,
                    pos_col - self.half_small:pos_col + self.half_small]

    def place(self, pos_row, pos_col, small_mat, large_mat):
        """
        Same as snippet but in reverse
        """
        large_mat[pos_row - self.half_small:pos_row + self.half_small,
        pos_col - self.half_small:pos_col + self.half_small] = small_mat
        return large_mat

    def convert_coords(self, x, z, mat, values=None):
        """
        :param x: horizontal coordinate converted into column
        :param z: vertical coordinate converted into row
        :param mat: matrix to which the cartesian coords are applied to
        :param values: if values are linked with coordinates,
            they also need to be linked to filtered coordinates
        :return: ndarray of rows and cols as well as filtered values if included
        """
        max_row, max_col = mat.shape
        row = (max_row / 2) - (z / self.scale)
        col = (max_col / 2) + (x / self.scale)
        if type(row) == np.ndarray:
            # check whether rows and cols are within bounds
            row_within_bounds = np.where(row < (max_row - 1))
            row, col = [arr[row_within_bounds] for arr in [row, col]]
            if values is not None:
                values = values[row_within_bounds]
            col_within_bounds = np.where(col < (max_col - 1))
            row, col = [arr[col_within_bounds] for arr in [row, col]]
            if values is not None:
                values = values[col_within_bounds]
        else:
            return int(row), int(col)

        if values is not None:
            return row.astype(np.int), col.astype(np.int), values
        else:
            return row.astype(np.int), col.astype(np.int)

class AngleMapper(MappingTemplate):
    def __init__(self, point_range):
        self.size = point_range * 2
        MappingTemplate.__init__(self, self.size)
        # create image for debugging
        self.small_coded_vis = None
        # used for navigation
        self.position = []
        # take cython grid mean
        self.C_GridMean = C_GridMean

    def update(self, x, z, angles, orientation, position):
        start = time.time()
        pos_row, pos_col = self.convert_coords(position[0], position[1], self.large_map)
        # take smaller snippets of large maps
        self.small_map = self.snippet(pos_row, pos_col, self.large_map)
        self.small_count = self.snippet(pos_row, pos_col, self.large_count)
        self.small_coded = self.snippet(pos_row, pos_col, self.large_coded)

        # place angles onto the map with cython written loop which averages values sharing same cell
        rows, cols, angles = self.convert_coords(x, z, self.small_map, angles)
        indices = np.array([rows, cols])
        self.small_count, self.small_map = self.C_GridMean(indices, angles, self.small_count, self.small_map)

        # paint the coded map
        self.small_coded[np.where(self.small_count > 0)] = self.map_codes.explored
        self.small_coded[np.where(self.small_map > 0.7)] = self.map_codes.obstacle

        # place snippets back onto map
        self.large_map = self.place(pos_row, pos_col, self.small_map, self.large_map)
        self.large_count = self.place(pos_row, pos_col, self.small_count, self.large_count)
        self.large_coded = self.place(pos_row, pos_col, self.small_coded, self.large_coded)
        self.position = [pos_row, pos_col]


text:
"""Tests for the top-level API."""
import jax
import jax.numpy as jnp
import pytest_cases

import probfindiff
from probfindiff import collocation, stencil
from probfindiff.utils import autodiff
from probfindiff.utils import kernel as kernel_module
from probfindiff.utils import kernel_zoo

@pytest_cases.parametrize("kernel", [kernel_zoo.exponentiated_quadratic, None])
@pytest_cases.parametrize("noise_variance", [1e-5, 0.0])
def case_backward(kernel, noise_variance):
    return probfindiff.backward(
        order_derivative=1,
        order_method=3,
        dx=0.05,
        kernel=kernel,
        noise_variance=noise_variance,
    )

@pytest_cases.parametrize("kernel", [kernel_zoo.exponentiated_quadratic, None])
@pytest_cases.parametrize("noise_variance", [1e-5, 0.0])
def case_forward(kernel, noise_variance):
    return probfindiff.forward(
        order_derivative=1,
        order_method=3,
        dx=0.05,
        kernel=kernel,
        noise_variance=noise_variance,
    )

@pytest_cases.parametrize("kernel", [kernel_zoo.exponentiated_quadratic, None])
@pytest_cases.parametrize("noise_variance", [1e-5, 0.0])
@pytest_cases.case(tags=("central",))
def case_central(kernel, noise_variance):
    return probfindiff.central(
        order_derivative=1,
        order_method=2,
        dx=0.1,
        kernel=kernel,
        noise_variance=noise_variance,
    )

@pytest_cases.parametrize("kernel", [kernel_zoo.exponentiated_quadratic, None])
@pytest_cases.parametrize("noise_variance", [1e-5, 0.0])
def case_from_grid(kernel, noise_variance):
    xs = 0.02 * jnp.arange(-2.0, 3.0)
    scheme = probfindiff.from_grid(
        order_derivative=1, xs=xs, kernel=kernel, noise_variance=noise_variance
    )
    return scheme, xs

@pytest_cases.parametrize_with_cases("scheme, xs", cases=".")
def test_scheme_types(scheme, xs):

    assert isinstance(scheme, probfindiff.FiniteDifferenceScheme)
    assert isinstance(xs, jnp.ndarray)

@pytest_cases.parametrize_with_cases("scheme, _", cases=".")
def test_scheme_shapes(scheme, _):
    assert scheme.weights.ndim == 1
    assert scheme.covs_marginal.ndim == 0

@pytest_cases.parametrize_with_cases("scheme, xs", cases=".")
def test_differentiate(scheme, xs):
    fx = jnp.sin(xs)
    dfx_approx, _ = probfindiff.differentiate(fx, scheme=scheme)
    assert dfx_approx.shape == ()

@pytest_cases.parametrize_with_cases("scheme, xs", cases=".")
def test_differentiate_along_axis(scheme, xs):
    fx = jnp.sin(xs)[:, None] * jnp.cos(xs)[None, :]
    dfx_approx, _ = probfindiff.differentiate_along_axis(fx, axis=1, scheme=scheme)
    assert dfx_approx.ndim == 1

@pytest_cases.parametrize_with_cases("scheme, xs", cases=".", has_tag=("central",))
def test_multivariate(scheme, xs):

    # A simple function R^d -> R
    f, d = lambda z: (z[0] + 1) ** 2 + (z[1] - 1) ** 2 + (z[2] - 1) ** 2, 3

    # Some point x in R^d
    x = jnp.array([1.0, 2.0, 3.0])

    # The gradient takes values in R^d
    df = jax.grad(f)

    xs = stencil.multivariate(xs_1d=xs, shape_input=(3,))
    assert xs.ndim == 3
    assert xs.shape[0] == xs.shape[1] == 3

    xs_shifted = x[None, :, None] + xs
    dfx, _ = probfindiff.differentiate(f(xs_shifted), scheme=scheme)

    assert dfx.shape == df(x).shape == (d,)
    assert jnp.allclose(dfx, df(x), rtol=1e-2, atol=1e-2)

def test_central_coefficients_polynomial():
    """For polynomials, the usual [1, -2, 1] coefficients should emerge."""
    x, xs = jnp.array(0.0), jnp.array([-1.0, 0.0, 1.0])

    k = lambda x, y: kernel_zoo.polynomial(x, y, p=jnp.ones((3,)))
    L = autodiff.compose(autodiff.derivative, autodiff.derivative)
    ks = kernel_module.differentiate(k=k, L=L)
    coeffs, unc_base = collocation.non_uniform_nd(
        x=jnp.array([x]), xs=xs[:, None], ks=ks, noise_variance=0.0
    )

    assert jnp.allclose(coeffs, jnp.array([1.0, -2.0, 1.0]))
    assert jnp.allclose(unc_base, 0.0)

max_stars_repo_name: schmidtjonathan/probfindiff

max_issues_repo_name: schmidtjonathan/probfindiff

max_forks_repo_name: schmidtjonathan/probfindiff

reason: import jax


text:
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import json
import numpy as np

import time
import os
from six.moves import cPickle

import opts
import models
from dataloader import *
from dataloaderraw import *
import eval_utils
import argparse
import misc.utils as utils
import torch

# Input arguments and options
parser = argparse.ArgumentParser()
# Input paths
parser.add_argument('--ids', nargs='+', required=True, help='id of the models to ensemble')
parser.add_argument('--weights', nargs='+', required=False, default=None, help='id of the models to ensemble')
# parser.add_argument('--models', nargs='+', required=True
#                 help='path to model to evaluate')
# parser.add_argument('--infos_paths', nargs='+', required=True, help='path to infos to evaluate')
opts.add_eval_options(parser)
opts.add_diversity_opts(parser)

opt = parser.parse_args()

model_infos = []
model_paths = []
for id in opt.ids:
    if '-' in id:
        id, app = id.split('-')
        app = '-'+app
    else:
        app = ''
    model_infos.append(utils.pickle_load(open('log_%s/infos_%s%s.pkl' %(id, id, app))))
    model_paths.append('log_%s/model%s.pth' %(id,app))

# Load one infos
infos = model_infos[0]

# override and collect parameters
replace = ['input_fc_dir', 'input_att_dir', 'input_box_dir', 'input_label_h5', 'input_json', 'batch_size', 'id']
for k in replace:
    setattr(opt, k, getattr(opt, k) or getattr(infos['opt'], k, ''))

vars(opt).update({k: vars(infos['opt'])[k] for k in vars(infos['opt']).keys() if k not in vars(opt)}) # copy over options from model

opt.use_box = max([getattr(infos['opt'], 'use_box', 0) for infos in model_infos])
assert max([getattr(infos['opt'], 'norm_att_feat', 0) for infos in model_infos]) == max([getattr(infos['opt'], 'norm_att_feat', 0) for infos in model_infos]), 'Not support different norm_att_feat'
assert max([getattr(infos['opt'], 'norm_box_feat', 0) for infos in model_infos]) == max([getattr(infos['opt'], 'norm_box_feat', 0) for infos in model_infos]), 'Not support different norm_box_feat'

vocab = infos['vocab'] # ix -> word mapping

# Setup the model
from models.AttEnsemble import AttEnsemble

_models = []
for i in range(len(model_infos)):
    model_infos[i]['opt'].start_from = None
    model_infos[i]['opt'].vocab = vocab
    tmp = models.setup(model_infos[i]['opt'])
    tmp.load_state_dict(torch.load(model_paths[i]))
    _models.append(tmp)

if opt.weights is not None:
    opt.weights = [float(_) for _ in opt.weights]
model = AttEnsemble(_models, weights=opt.weights)
model.seq_length = opt.max_length
model.cuda()
model.eval()
crit = utils.LanguageModelCriterion()

# Create the Data Loader instance
if len(opt.image_folder) == 0:
  loader = DataLoader(opt)
else:
  loader = DataLoaderRaw({'folder_path': opt.image_folder,
                            'coco_json': opt.coco_json,
                            'batch_size': opt.batch_size,
                            'cnn_model': opt.cnn_model})
# When eval using provided pretrained model, the vocab may be different from what you have in your cocotalk.json
# So make sure to use the vocab in infos file.
loader.ix_to_word = infos['vocab']

opt.id = '+'.join([_+str(__) for _,__ in zip(opt.ids, opt.weights)])
# Set sample options
loss, split_predictions, lang_stats = eval_utils.eval_split(model, crit, loader,
    vars(opt))

print('loss: ', loss)
if lang_stats:
  print(lang_stats)

if opt.post_processing == 1:
    for i in range(len(split_predictions)):
      split_predictions[i]['caption'] = utils.post_processing_for_conceptual(split_predictions[i]['caption'])

if opt.dump_json == 1:
    # dump the json
    json.dump(split_predictions, open('vis/vis.json', 'w'))
    json.dump(split_predictions, open(os.path.join('eval_results/', opt.id + '_' + opt.split + '.json'), 'w'))

max_stars_repo_name: aliabd/cos-cvae

max_issues_repo_name: aliabd/cos-cvae

max_forks_repo_name: aliabd/cos-cvae


text:
import numpy as np
from scipy.special import gamma
from skimage import color

class MLVMeasurement():
    def __init__(self):
        self.gam = np.linspace(0.2,10,9801)

    def __estimateggdparam(self,vec):
        gam = self.gam
        r_gam = (gamma(1/gam)*gamma(3/gam))/((gamma(2/gam)) ** 2)
        sigma_sq = np.mean(vec ** 2)
        sigma = np.sqrt(sigma_sq)
        return sigma

    def __MLVMap(self,img):
        xs, ys = img.shape
        x=img
        x1=np.zeros((xs,ys))
        x2=np.zeros((xs,ys))
        x3=np.zeros((xs,ys))
        x4=np.zeros((xs,ys))
        x5=np.zeros((xs,ys))
        x6=np.zeros((xs,ys))
        x7=np.zeros((xs,ys))
        x8=np.zeros((xs,ys))
        x9=np.zeros((xs,ys))
        x1[0:xs-2,0:ys-2] = x[1:xs-1,1:ys-1]
        x2[0:xs-2,1:ys-1] = x[1:xs-1,1:ys-1]
        x3[0:xs-2,2:ys]   = x[1:xs-1,1:ys-1]
        x4[1:xs-1,0:ys-2] = x[1:xs-1,1:ys-1]
        x5[1:xs-1,1:ys-1] = x[1:xs-1,1:ys-1]
        x6[1:xs-1,2:ys]   = x[1:xs-1,1:ys-1]
        x7[2:xs,0:ys-2]   = x[1:xs-1,1:ys-1]
        x8[2:xs,1:ys-1]   = x[1:xs-1,1:ys-1]
        x9[2:xs,2:ys]     = x[1:xs-1,1:ys-1]
        x1=x1[1:xs-1,1:ys-1]
        x2=x2[1:xs-1,1:ys-1]
        x3=x3[1:xs-1,1:ys-1]
        x4=x4[1:xs-1,1:ys-1]
        x5=x5[1:xs-1,1:ys-1]
        x6=x6[1:xs-1,1:ys-1]
        x7=x7[1:xs-1,1:ys-1]
        x8=x8[1:xs-1,1:ys-1]
        x9=x9[1:xs-1,1:ys-1]
        dd=[]
        dd.append(x1-x5)
        dd.append(x2-x5)
        dd.append(x3-x5)
        dd.append(x4-x5)
        dd.append(x6-x5)
        dd.append(x7-x5)
        dd.append(x8-x5)
        dd.append(x9-x5)
        map = np.max(dd,axis=0)
        return map

    def getScore(self,x):#x should be double gray image
        if(x.ndim == 3):#color
            x = color.rgb2gray(x)
        map = self.__MLVMap(x)
        xs,ys = map.shape
        xy_number=xs*ys
        vec = map.reshape((xy_number,))
        vec[::-1].sort()#descend
        svec=vec[0:xy_number]
        a=np.arange(xy_number)
        q=np.exp(-0.01*a)
        svec=svec*q
        svec=svec[0:1000]
        return self.__estimateggdparam(svec)

max_stars_repo_name: polyedr/drfx-deblur

max_issues_repo_name: polyedr/drfx-deblur

max_forks_repo_name: polyedr/drfx-deblur


text:
import os
import streamlit as st
import glob
import json
import numpy as np
import ada.utils.experimentation as xp
from copy import deepcopy

def configure_network(default_file, on_sidebar=True):
    network_params = xp.load_json_dict(default_file)
    stmod = st.sidebar if on_sidebar else st
    stmod.header("Network configuration")
    stmod.subheader("Learning parameters")
    stmod.markdown("$\\lambda$ controls the weight of the critic")
    adapt_lambda = stmod.checkbox(
        "Use adaptive lambda", value=network_params["train_params"]["adapt_lambda"]
    )
    lambda_init = stmod.number_input(
        "Final (max) lambda",
        value=float(network_params["train_params"]["lambda_init"]),
        step=1e-3,
        format="%.1f",
    )
    adapt_lr = stmod.checkbox(
        "Use adaptive learning rate", value=network_params["train_params"]["adapt_lr"]
    )
    init_lr = stmod.number_input(
        "Initial learning rate",
        value=float(network_params["train_params"]["init_lr"]),
        step=1e-4,
        format="%.4f",
    )
    nb_init_epochs = stmod.number_input(
        "Number of warmup epochs",
        min_value=0,
        value=network_params["train_params"]["nb_init_epochs"],
    )
    nb_adapt_epochs = stmod.number_input(
        "Number of adaptation epochs",
        min_value=1,
        value=network_params["train_params"]["nb_adapt_epochs"],
    )
    stmod.subheader("Architecture details")
    stmod.markdown(
        "Choose the width of the Feature extractor hidden layer. The size of the "
        "final layer width is used for both Task Classifier and Critic. "
        "The Critic can have more than one layer, all of same width."
    )
    hidden_size = stmod.text_input(
        "Feature hidden layers",
        value=network_params["archi_params"]["feature"]["hidden_size"],
    )
    hidden_size = json.loads(hidden_size)
    critic_layers = stmod.text_input(
        "Critic hidden layers",
        value=network_params["archi_params"]["critic"]["hidden_size"],
    )
    critic_layers = json.loads(critic_layers)

    network_params["train_params"]["adapt_lambda"] = adapt_lambda
    network_params["train_params"]["lambda_init"] = lambda_init
    network_params["train_params"]["adapt_lr"] = adapt_lr
    network_params["train_params"]["init_lr"] = init_lr
    network_params["train_params"]["nb_init_epochs"] = nb_init_epochs
    network_params["train_params"]["nb_adapt_epochs"] = nb_adapt_epochs
    network_params["archi_params"]["feature"]["hidden_size"] = hidden_size
    network_params["archi_params"]["critic"]["hidden_size"] = critic_layers
    return network_params

def configure_dataset(default_dir, on_sidebar=True):
    stmod = st.sidebar if on_sidebar else st
    stmod.header("Dataset")
    json_files = glob.glob(f"{default_dir}/*.json")
    all_params_files = [(f, xp.load_json_dict(f)) for f in json_files]
    toy_files = [
        f for (f, p) in all_params_files if p.get("dataset_group", "none") == "toy"
    ]
    dataset = stmod.selectbox("Dataset", toy_files, index=0)
    default_params = xp.load_json_dict(dataset)
    if on_sidebar:
        return default_params

    toy_params = deepcopy(default_params)

    # centers position
    default_centers = np.array([[-0.5, 0.0], [0.5, 0]])
    param_centers = default_params["cluster"].get("centers", default_centers.tolist())
    new_centers_st = stmod.text_input(
        "Position of class centers (source)", param_centers
    )
    new_centers = json.loads(new_centers_st)
    n_clusters = len(new_centers)
    stmod.markdown(f"{n_clusters} classes.")
    toy_params["cluster"]["centers"] = new_centers
    toy_params["cluster"]["n_clusters"] = n_clusters

    # centers radii
    radius0 = default_params["cluster"]["radius"]
    same_radius = stmod.checkbox(
        "Use same variance everywhere (class/dimension)",
        value=isinstance(radius0, float),
    )
    if same_radius:
        if not isinstance(radius0, float):
            radius0 = np.array(radius0).flatten()[0]
        radius = stmod.number_input(
            "Class variance",
            step=10 ** (np.floor(np.log10(radius0))),
            value=radius0,
            format="%.4f",
        )
        toy_params["cluster"]["radius"] = radius
    else:
        if isinstance(radius0, float):
            radii = (np.ones_like(new_centers) * radius0).tolist()
        else:
            radii = radius0
        new_radius_st = stmod.text_input(
            "Variance of each class along each dimension", radii
        )
        new_radius = json.loads(new_radius_st)
        shape_variance = np.array(new_radius).shape
        shape_clusters = np.array(new_centers).shape
        if shape_variance == shape_clusters:
            stmod.markdown(
                ":heavy_check_mark: Shape of variance values matches the shape of clusters."
            )
        else:
            stmod.markdown(
                ":warning: Warning: Shape of variances doesn't match the shape of clusters."
            )

        toy_params["cluster"]["radius"] = new_radius

    # class balance
    proba_classes = default_params["cluster"]["proba_classes"]
    if n_clusters == 2:
        new_proba_classes = stmod.number_input(
            "Probability of class 1",
            step=10 ** (np.floor(np.log10(proba_classes))),
            value=proba_classes,
            format="%.4f",
        )
    else:
        new_proba_classes_st = stmod.text_input(
            "Weight or probability of each class (will be normalized to sum to 1)",
            proba_classes,
        )
        new_proba_classes = json.loads(new_proba_classes_st)
        nb_probas = len(new_proba_classes)
        if nb_probas == n_clusters:
            stmod.markdown(
                ":heavy_check_mark: class probas values matches the number of clusters."
            )
        else:
            stmod.markdown(
                ":warning: Warning: class probas values don't match the number of clusters."
            )
    toy_params["cluster"]["proba_classes"] = new_proba_classes

    # target shift
    default_cond_shift = default_params["shift"]["data_shift"]
    if n_clusters == 2:
        cond_shift = stmod.checkbox(
            "Class-conditional shift", value="cond" in default_cond_shift
        )
    else:
        cond_shift = False

    if cond_shift:
        rotation0 = default_params["shift"]["re"]
        if isinstance(rotation0, float):
            default_r0 = rotation0
            default_r1 = rotation0
        else:
            default_r0 = default_params["shift"]["re"][0]
            default_r1 = default_params["shift"]["re"][1]
        re0 = stmod.slider(
            "Rotation class 0", min_value=-np.pi, max_value=np.pi, value=default_r0,
        )
        re1 = stmod.slider(
            "Rotation class 1", min_value=-np.pi, max_value=np.pi, value=default_r1,
        )
        transl0 = default_params["shift"]["te"]
        if isinstance(transl0, float):
            default_t0 = transl0
            default_t1 = transl0
        else:
            default_t0 = default_params["shift"]["te"][0]
            default_t1 = default_params["shift"]["te"][1]
        te0 = stmod.slider(
            "Translation class 0", min_value=-3.0, max_value=3.0, value=default_t0,
        )
        te1 = stmod.slider(
            "Translation class 1", min_value=-3.0, max_value=3.0, value=default_t1,
        )
        toy_params["shift"]["re"] = [re0, re1]
        toy_params["shift"]["te"] = [te0, te1]
    else:
        re = stmod.slider(
            "Rotation",
            min_value=-np.pi,
            max_value=np.pi,
            value=default_params["shift"]["re"],
        )
        te = stmod.slider(
            "Translation",
            min_value=-3.0,
            max_value=3.0,
            value=default_params["shift"]["te"],
        )
        toy_params["shift"]["re"] = re
        toy_params["shift"]["te"] = te

    test_view_data(toy_params)

    # choose a new (unique) name for the dataset and save
    data_hash = xp.param_to_hash(toy_params)
    default_hash = xp.param_to_hash(default_params)
    default_name = toy_params["dataset_name"]
    if default_hash != data_hash:
        if toy_params["dataset_name"] == default_params["dataset_name"]:
            default_name = data_hash

    data_name = st.text_input("Choose a (unique) name for your dataset", default_name)
    data_name = data_name.replace(" ", "_")
    toy_params["dataset_name"] = data_name
    data_file = os.path.join(default_dir, f"{data_name}.json")
    if os.path.exists(data_file):
        st.text(f"Data set with this name exists! {data_file}")
    else:
        if st.button("Save dataset"):
            with open(data_file, "w") as fd:
                fd.write(json.dumps(toy_params))
                default_params = deepcopy(toy_params)
            st.text(f"Configuration saved to {data_file}")

    return toy_params, data_hash

def test_view_data(data_params):
    from ada.utils.plotting import colored_scattered_plot2x2
    from ada.datasets.toys import CausalBlobs, get_datashift_params

    target_shift = get_datashift_params(**data_params["shift"])
    source_data = CausalBlobs(
        ".tmp_view",
        n_samples=data_params["n_samples"],
        transform=get_datashift_params("no_shift"),
        cluster_params=data_params["cluster"],
    )
    target_data = CausalBlobs(
        ".tmp_view",
        n_samples=data_params["n_samples"],
        transform=target_shift,
        cluster_params=data_params["cluster"],
    )
    X_s, y_s = source_data.data, source_data.targets
    X_t, y_t = target_data.data, target_data.targets

    fig, ax = colored_scattered_plot2x2(X_s, X_t, y_s, y_t, set_aspect_equal=True)
    fig.set_tight_layout(tight=None)
    st.write("View data")
    st.pyplot(fig)

    source_data.delete_from_disk()
    target_data.delete_from_disk()

max_stars_repo_name: ccrndn/pytorch-ada

max_issues_repo_name: ccrndn/pytorch-ada

max_forks_repo_name: ccrndn/pytorch-ada