text:
import numpy as np
import cv2
import random
import threading
import os
import time
import logging
import tensorflow as tf
import matplotlib.pyplot as plt
from PIL import Image

def fold(image):
    rows,cols,channel = image.shape

    new_row = int (rows/2)
    dst = image[0:new_row, 0:cols]
    return dst

def Rotation(image):
    rows,cols,channel = image.shape
    angle = np.random.uniform(low=-20.0, high=20.0)
    M = cv2.getRotationMatrix2D((cols/2,rows/2),angle,1)
    dst = cv2.warpAffine(image, M, (cols,rows))
    return dst

def Translate(image):
    rows,cols,channel = image.shape
    x_ = cols*0.15
    y_ = rows*0.15
    scale = np.random.uniform(0.80, 1.20)
    x = np.random.uniform(-x_, x_)
    y = np.random.uniform(-y_, y_)
    M = np.float32([[scale, 0, x], [0, scale, y]])
    dst = cv2.warpAffine(image, M, (image.shape[1], image.shape[0]))
    return dst

def Affine(image):
    img_info=image.shape
    image_height=img_info[0]
    image_weight=img_info[1]
    mat_src=np.float32([[0,0],[0,image_height-1],[image_weight-1,0]])

    x1 = np.random.uniform(0, 50)
    y1 = np.random.uniform(0, 50)

    x2 = np.random.uniform(200, 400)
    y2 = np.random.uniform(300, 500)

    x3 = np.random.uniform(200, 400)
    y3 = np.random.uniform(300, 500)

    mat_dst=np.float32([[x1,y1],[x2,image_height-y2],[image_weight-x3,y3]])
    mat_Affine=cv2.getAffineTransform(mat_src,mat_dst)
    dst=cv2.warpAffine(image,mat_Affine,(image_height,image_weight))
    return dst

def Crop(image):
    rows,cols,channel = image.shape
    L_delta = int(np.random.uniform(1, cols*0.15))
    R_delta = int(np.random.uniform(1, cols*0.15))
    U_delta = int(np.random.uniform(1, rows*0.15))
    D_delta = int(np.random.uniform(1, rows*0.15))
    TOP = 0 + L_delta
    DOWN = rows - R_delta
    LEFT = 0 + U_delta
    RIGHT = cols - D_delta
    crop_img = image[TOP:DOWN, LEFT:RIGHT]
    dst = cv2.copyMakeBorder(crop_img, L_delta, R_delta, U_delta , D_delta, cv2.BORDER_CONSTANT, value=(0, 0, 0, 0))
    return dst

def Hsv(image):
    hue_vari = 1
    sat_vari = 0.5
    val_vari = 0.5
    hue_delta = np.random.randint(-hue_vari, hue_vari)
    sat_mult = 1 + np.random.uniform(-sat_vari, sat_vari)
    val_mult = 1 + np.random.uniform(-val_vari, val_vari)

    img_hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV).astype(np.float)
    img_hsv[:, :, 0] = (img_hsv[:, :, 0] + hue_delta) % 180
    img_hsv[:, :, 1] *= sat_mult
    img_hsv[:, :, 2] *= val_mult
    img_hsv[img_hsv > 255] = 255

    dst = cv2.cvtColor(np.round(img_hsv).astype(np.uint8), cv2.COLOR_HSV2BGR)
    return dst

def Gamma(image):
    gamma_vari = 0.15
    log_gamma_vari = np.log(gamma_vari)
    alpha = np.random.uniform(-log_gamma_vari, log_gamma_vari)
    gamma = np.exp(alpha)
    gamma_table = [np.power(x / 255.0, gamma) * 255.0 for x in range(256)]
    gamma_table = np.round(np.array(gamma_table)).astype(np.uint8)
    dst = cv2.LUT(image, gamma_table)
    return dst

def Motion_blur(image):
    image = np.array(image)
    degree_ = 25
    angle_ = 45
    degree = int(np.random.uniform(1, degree_))
    angle = int(np.random.uniform(-angle_, angle_))
    M = cv2.getRotationMatrix2D((degree / 2, degree / 2), angle, 1)
    motion_blur_kernel = np.diag(np.ones(degree))
    motion_blur_kernel = cv2.warpAffine(motion_blur_kernel, M, (degree, degree))
    motion_blur_kernel = motion_blur_kernel / degree
    blurred = cv2.filter2D(image, -1, motion_blur_kernel)
    cv2.normalize(blurred, blurred, 0, 255, cv2.NORM_MINMAX)
    dst = np.array(blurred, dtype=np.uint8)
    return dst

def Gaussian_blur(image):

    kernel = [random.randint(1, 50) * 2 + 1 for x in range(1)]
    dst = cv2.GaussianBlur(image, ksize=(kernel[0], kernel[0]), sigmaX=0, sigmaY=0)
    return dst

def imageProcessing(image, image_name, function, times, write_path):
    for _i in range(0, times, 1):
        if (function == 'Rotation'):
            new_image = Rotation(image)
        elif (function == 'Crop'):
            new_image = Crop(image)
        elif (function == 'Hsv'):
            new_image = Hsv(image)
        elif (function == 'Gamma'):
            new_image = Gamma(image)
        elif (function == 'Motion_blur'):
            new_image = Motion_blur(image)
        elif (function == 'Gaussian_blur'):
            new_image = Gaussian_blur(image)
        elif (function == 'Translate'):
            new_image = Translate(image)
        elif (function == 'Affine'):
            new_image = Affine(image)
        else:
            new_image = image

        path = write_path+image_name+"_"+function+"_"+str(_i)+".JPG"
        cv2.imwrite(path ,new_image)

ext: py

reason: import numpy


text:
"""
Normalization functions inherit from the ``OperationInterface`` class.
This means that they can all be called upon an ``AnnotatedGEM`` or a ``GeneSetCollection``.

These (classes) functions have static methods that implement the transform on a ``numpy``
or ``xarray`` source.
"""

import numpy as np
import param

from ..models import Interface

__all__ = [
    "ReadsPerKilobaseMillion",
    "UpperQuartile",
]

class UpperQuartile(Interface, param.ParameterizedFunction):
    """
    Under this normalization method, after removing genes having zero read counts for all samples,
    the remaining gene counts are divided by the upper quartile of counts different from zero in
    the computation of the normalization factors associated with their sample and multiplied by
    the mean upper quartile across all samples of the dataset. [method_compare]_

    Original R code.  ::

        uq<-function(X){

          #excluding zero counts in each sample
          UQ<-function(y){
            quantile(y, 0.75)
          }
          X<-X+0.1
          upperQ<-apply(X,2,UQ)
          f.uq<-upperQ/mean(upperQ)
          upq.res<-scale(X,center=FALSE,scale=f.uq)
          return(upq.res)
        }

    .. [method_compare] `A comparison of per sample global scaling and per gene normalization methods for differential expression analysis of RNA-seq data <https://doi.org/10.1371/journal.pone.0176185>`_

    """

    # For clarity of testing I write normalization methods as their own, self-contained functions.
    # I often create a numpy and xarray version.
    # @staticmethods decorated functions do not depend on the state of the class, and may be called
    # without instantiating the class.
    @staticmethod
    def np_upper_quartile(counts):
        """
        Perform the upper quartile normalization.

        :param counts: A numpy array containing the raw count values. The shape is assumed to be
            (samples by genes). Zero counts are expected to be present as zeros.

        :returns: The upper quartile normalized count matrix.
        """
        adjusted_counts = counts + 0.1
        per_sample_quantile = np.quantile(adjusted_counts, q=0.75, axis=1)
        per_sample_quantile_mean = np.mean(per_sample_quantile)
        return adjusted_counts / (per_sample_quantile[:, np.newaxis] / per_sample_quantile_mean)

    @staticmethod
    def xr_upper_quartile(counts):
        """
        Perform the upper quartile normalization.

        :param counts: An ``xarray.DataArray`` containing the raw count values. The shape is
            assumed to be (samples by genes). Zero counts are expected to be present as zeros.

        :returns: The upper quartile normalized count matrix.
        """
        adjusted_counts = counts + 0.1
        per_sample_quantile = adjusted_counts.quantile(0.75, dim="Gene")
        per_sample_quantile_mean = per_sample_quantile.mean()
        return (adjusted_counts / (per_sample_quantile / per_sample_quantile_mean)).drop("quantile")

    def __call__(self, *args, **params):
        """
        Perform the upper quartile normalization.

        :return: The upper quartile normalized count matrix.
        """
        super().__init__(*args, **params)
        counts = self.x_count_data
        return self.xr_upper_quartile(counts)

class ReadsPerKilobaseMillion(Interface, param.ParameterizedFunction):
    """
    RPKM or FPKM -- Reads or Fragments per per Kilobase Million.

    These methods attempt to compensate for sequencing depth and gene length.
    The utility of this method is disputed in the literature [cite me].
    """
    length_variable = param.String(default="lengths")

    @staticmethod
    def xr_reads_per_kilobase_million(counts, lengths, sample_dim="Sample"):
        scaling_factor = counts.sum(dim=sample_dim) / 1e6
        reads_per_million = counts / scaling_factor
        normalized_counts = reads_per_million / lengths
        return normalized_counts

    @staticmethod
    def np_reads_per_kilobase_million(counts, lengths):
        scaling_factor = counts.sum(axis=0) / 1e6
        reads_per_million = counts / scaling_factor
        normalized_counts = reads_per_million / lengths
        return normalized_counts

    def __call__(self, *args, **params):
        super().__init__(*args, **params)
        counts = self.x_count_data
        lengths = self.gem.data[self.length_variable].sel({self.gene_index_name: self.get_gene_index()}).copy()
        return self.xr_reads_per_kilobase_million(counts=counts, lengths=lengths, sample_dim=self.sample_index_name)

max_stars_repo_name: SystemsGenetics/GEMprospector

max_issues_repo_name: SystemsGenetics/GEMprospector

max_forks_repo_name: SystemsGenetics/GEMprospector


text:
import pandas as pd

import os
## Python이 실행될 때 DJANGO_SETTINGS_MODULE이라는 환경 변수에 현재 프로젝트의 settings.py파일 경로를 등록합니다.
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "hyo1.settings")
## 이제 장고를 가져와 장고 프로젝트를 사용할 수 있도록 환경을 만듭니다.
import django
django.setup()
from food.models import Food
from gmm.models import Predict, Label

# length = len(data)
#
# for index, row in data.iterrows():
#     if Food.objects.filter(name=row['식품명']):
#         continue
#     Food.objects.create(name=row['식품명'], serving=row['1회제공량'], carbohydrate=row['탄수화물(g)'],\
#                         sugars=row['총당류(g)'], protein=row['단백질(g)'], province=row['지방(g)'], \
#                         saturated_fatty_acids=row['총 포화 지방산(g)'], cholesterol=row['콜레스테롤(㎎)'],\
#                         salt=row['나트륨(㎎)'])

# for food in Food.objects.all():
#     food.delete()

# from sklearn.externals import joblib
import joblib
import numpy as np

scaler = joblib.load(filename='minmax_scaler.pkl')
pca = joblib.load(filename='pca.pkl')
gmm = joblib.load(filename='gmm.pkl')

for food in Food.objects.all():
    next = np.array([food.carbohydrate, food.sugars, food.protein, food.province, food.saturated_fatty_acids,\
                    food.cholesterol, food.salt]).reshape(1, 7)
    next = scaler.transform(next)
    next = pca.transform(next)
    next = gmm.predict(next)
    # label = Label.objects.get(index=next[0])
    food.label = next[0]
    food.save()


text:
"""
Script for running preprocessing on a all or a subset of data described in
a U-Time project directory, loading all selected files with specified:
    - scaler
    - strip function
    - quality control function
    - channel selection
    - re-sampling

Loaded (and processed) files according to those settings are then saved to a single H5 archive for all
specified datasets and dataset splits.

The produced, pre-processed H5 archive of data may be consumed by the 'ut train' script setting flag --preprocessed.

This script should be called form within a U-Time project directory
"""

import numpy as np
import os
import h5py
from argparse import ArgumentParser
from concurrent.futures import ThreadPoolExecutor
from functools import partial
from utime import Defaults

def get_argparser():
    """
    Returns an argument parser for this script
    """
    parser = ArgumentParser(description='TODO')  # TODO
    parser.add_argument("--out_path", type=str, required=True,
                        help="Path to output h5 archive")
    parser.add_argument("--dataset_splits", type=str, required=True, nargs='*',
                        help="Dataset splits (e.g. train_data) to preprocess "
                             "and save; space-separated list.")
    parser.add_argument("--overwrite", action='store_true',
                        help='Overwrite previous pre-processed data')
    parser.add_argument("--num_threads", type=int, default=1,
                        help="Number of threads to use for loading and "
                             "writing. Note: HDF5 must be compiled in "
                             "thread-safe mode!")
    return parser

def copy_dataset_hparams(hparams, hparams_out_path):
    groups_to_save = ('select_channels', 'alternative_select_channels',
                      'load_time_channel_sampling_groups')
    with open(hparams_out_path, 'w') as out_f:
        for group in groups_to_save:
            try:
                data = hparams.get_group(group)
            except ValueError:
                continue  # Not found
            else:
                data = data.replace("load_time_channel_sampling_groups",
                                    "access_time_channel_sampling_groups")
                out_f.write(data + "\n")

def add_dataset_entry(hparams_out_path, h5_path,
                      split_identifier, period_length_sec):
    field = "{}_data:\n  " \
            "data_dir: {}\n  " \
            "period_length_sec: {}\n  " \
            "identifier: {}\n\n".format(split_identifier,
                                        h5_path,
                                        period_length_sec,
                                        split_identifier.upper())
    with open(hparams_out_path, "a") as out_f:
        out_f.write(field)

def preprocess_study(h5_file_group, study):
    """
    TODO

    Args:
        h5_file_group:
        study:

    Returns:
        None
    """
    # Create groups
    study_group = h5_file_group.create_group(study.identifier)
    psg_group = study_group.create_group("PSG")
    with study.loaded_in_context():
        X, y = study.get_all_periods()
        for chan_ind, channel_name in enumerate(study.select_channels):
            # Create PSG channel datasets
            psg_group.create_dataset(channel_name.original_name,
                                     data=X[..., chan_ind])
        # Create hypnogram dataset
        study_group.create_dataset("hypnogram", data=y)

        # Create class --> index lookup groups
        cls_to_indx_group = study_group.create_group('class_to_index')
        dtype = np.dtype('uint16') if len(y) <= 65535 else np.dtype('uint32')
        classes = study.hypnogram.classes
        for class_ in classes:
            inds = np.where(y == class_)[0].astype(dtype)
            cls_to_indx_group.create_dataset(
                str(class_), data=inds
            )

        # Set attributes, currently only sample rate is (/may be) used
        study_group.attrs['sample_rate'] = study.sample_rate

def run(args):
    """
    Run the script according to args - Please refer to the argparser.

    args:
        args:    (Namespace)  command-line arguments
    """
    from mpunet.logging import Logger
    from utime.hyperparameters import YAMLHParams
    from utime.utils.scriptutils import assert_project_folder
    from utime.utils.scriptutils import get_splits_from_all_datasets

    project_dir = os.path.abspath("./")
    assert_project_folder(project_dir)

    # Get logger object
    logger = Logger(project_dir + "/preprocessing_logs",
                    active_file='preprocessing',
                    overwrite_existing=args.overwrite,
                    no_sub_folder=True)
    logger("Args dump: {}".format(vars(args)))

    # Load hparams
    hparams = YAMLHParams(Defaults.get_hparams_path(project_dir),
                          logger=logger,
                          no_version_control=True)

    # Initialize and load (potentially multiple) datasets
    datasets = get_splits_from_all_datasets(hparams,
                                            splits_to_load=args.dataset_splits,
                                            logger=logger,
                                            return_data_hparams=True)

    # Check if file exists, and overwrite if specified
    if os.path.exists(args.out_path):
        if args.overwrite:
            os.remove(args.out_path)
        else:
            from sys import exit
            logger("Out file at {} exists, and --overwrite was not set."
                   "".format(args.out_path))
            exit(0)

    # Create dataset hparams output directory
    out_dir = Defaults.get_pre_processed_data_configurations_dir(project_dir)
    if not os.path.exists(out_dir):
        os.mkdir(out_dir)

    with ThreadPoolExecutor(args.num_threads) as pool:
        with h5py.File(args.out_path, "w") as h5_file:
            for dataset, dataset_hparams in datasets:
                # Create a new version of the dataset-specific hyperparameters
                # that contain only the fields needed for pre-processed data
                name = dataset[0].identifier.split("/")[0]
                hparams_out_path = os.path.join(out_dir, name + ".yaml")
                copy_dataset_hparams(dataset_hparams, hparams_out_path)

                # Update paths to dataset hparams in main hparams file
                hparams.set_value(subdir='datasets', name=name,
                                  value=hparams_out_path, overwrite=True)
                # Save the hyperparameters to the pre-processed main hparams
                hparams.save_current(Defaults.get_pre_processed_hparams_path(
                    project_dir
                ))

                # Process each dataset
                for split in dataset:
                    # Add this split to the dataset-specific hparams
                    add_dataset_entry(hparams_out_path,
                                      args.out_path,
                                      split.identifier.split("/")[-1].lower(),
                                      split.period_length_sec)
                    # Overwrite potential load time channel sampler to None
                    split.set_load_time_channel_sampling_groups(None)

                    # Create dataset group
                    split_group = h5_file.create_group(split.identifier)

                    # Run the preprocessing
                    process_func = partial(preprocess_study, split_group)

                    logger.print_to_screen = True
                    logger("Preprocessing dataset:", split)
                    logger.print_to_screen = False
                    n_pairs = len(split.pairs)
                    for i, _ in enumerate(pool.map(process_func,
                                                   split.pairs)):
                        print("  {}/{}".format(i+1, n_pairs),
                              end='\r', flush=True)
                    print("")

def entry_func(args=None):
    # Get the script to execute, parse only first input
    parser = get_argparser()
    args = parser.parse_args(args)
    run(args=args)

if __name__ == "__main__":
    entry_func()

max_issues_repo_name: amiyapatanaik/U-Time

max_forks_repo_name: amiyapatanaik/U-Time


text:
import random
from math import isclose
from tempfile import NamedTemporaryFile

import pytest
import torch
import numpy as np

from lhotse import CutSet, MonoCut, NumpyHdf5Writer
from lhotse.array import seconds_to_frames
from lhotse.dataset.collation import (
    TokenCollater,
    collate_audio,
    collate_custom_field,
    collate_features,
)
from lhotse.testing.dummies import dummy_cut, dummy_recording, dummy_supervision
from lhotse.utils import nullcontext as does_not_raise

@pytest.mark.parametrize("add_bos", [True, False])
@pytest.mark.parametrize("add_eos", [True, False])
def test_token_collater(add_bos, add_eos):
    test_sentences = [
        "Testing the first sentence.",
        "Let's add some more punctuation, shall we?",
        "How about number 42!",
    ]

    cuts = CutSet.from_cuts(
        dummy_cut(idx, idx, supervisions=[dummy_supervision(idx, idx, text=sentence)])
        for idx, sentence in enumerate(test_sentences)
    )

    token_collater = TokenCollater(cuts, add_bos=add_bos, add_eos=add_eos)
    tokens_batch, tokens_lens = token_collater(cuts)

    assert isinstance(tokens_batch, torch.LongTensor)
    assert isinstance(tokens_lens, torch.IntTensor)

    extend = int(add_bos) + int(add_eos)
    expected_len = len(max(test_sentences, key=len)) + extend
    assert tokens_batch.shape == (len(test_sentences), expected_len)
    assert torch.all(
        tokens_lens
        == torch.IntTensor([len(sentence) + extend for sentence in test_sentences])
    )

    reconstructed = token_collater.inverse(tokens_batch, tokens_lens)
    assert reconstructed == test_sentences

def test_collate_audio_padding():
    cuts = CutSet.from_json("test/fixtures/ljspeech/cuts.json")
    assert len(set(cut.num_samples for cut in cuts)) > 1

    correct_pad = max(cut.num_samples for cut in cuts)
    audio, audio_lens = collate_audio(cuts)

    assert audio.shape[-1] == correct_pad
    assert max(audio_lens).item() == correct_pad

def test_collate_audio_padding_fault_tolerant_return_vals():
    cuts = CutSet.from_json("test/fixtures/ljspeech/cuts.json")
    assert len(set(cut.num_samples for cut in cuts)) > 1

    correct_pad = max(cut.num_samples for cut in cuts)
    audio, audio_lens, cuts_ok = collate_audio(cuts, fault_tolerant=True)

    assert len(cuts) == len(cuts_ok)
    assert audio.shape[-1] == correct_pad
    assert max(audio_lens).item() == correct_pad

def test_collate_feature_padding():
    cuts = CutSet.from_json("test/fixtures/ljspeech/cuts.json")
    assert len(set(cut.num_frames for cut in cuts)) > 1

    correct_pad = max(cut.num_frames for cut in cuts)
    features, features_lens = collate_features(cuts)

    assert features.shape[1] == correct_pad
    assert max(features_lens).item() == correct_pad

def test_collate_custom_array():
    EMBEDDING_SIZE = 300

    cuts = CutSet.from_json("test/fixtures/ljspeech/cuts.json")
    with NamedTemporaryFile(suffix=".h5") as f, NumpyHdf5Writer(f.name) as writer:
        expected_xvectors = []
        for cut in cuts:
            expected_xvectors.append(np.random.randn(EMBEDDING_SIZE).astype(np.float32))
            cut.xvector = writer.store_array(cut.id, expected_xvectors[-1])

        xvectors = collate_custom_field(cuts, "xvector")
        assert isinstance(xvectors, torch.Tensor)
        assert xvectors.dtype == torch.float32
        assert xvectors.shape == (len(cuts), EMBEDDING_SIZE)
        for idx, xvec in enumerate(expected_xvectors):
            torch.testing.assert_allclose(xvectors[idx], xvec)

def test_collate_custom_numbers():
    cuts = CutSet.from_json("test/fixtures/ljspeech/cuts.json")
    expected_snrs = []
    for cut in cuts:
        expected_snrs.append(random.random() * 20)
        cut.snr = expected_snrs[-1]

    snrs = collate_custom_field(cuts, "snr")
    assert isinstance(snrs, torch.Tensor)
    assert snrs.dtype == torch.float32
    assert snrs.shape == (len(cuts),)
    for idx, snr in enumerate(expected_snrs):
        assert isclose(snrs[idx], snr, abs_tol=1e-5)

@pytest.mark.parametrize(
    "pad_value",
    [0.0, None],  # user forgot to specify pad_value
)
def test_collate_custom_temporal_array_floats(pad_value):
    VOCAB_SIZE = 500

    cuts = CutSet.from_json("test/fixtures/ljspeech/cuts.json")
    max_num_frames = max(cut.num_frames for cut in cuts)

    with NamedTemporaryFile(suffix=".h5") as f, NumpyHdf5Writer(f.name) as writer:
        expected_posteriors = []
        for cut in cuts:
            expected_posteriors.append(
                np.random.randn(cut.num_frames, VOCAB_SIZE).astype(np.float32)
            )
            cut.posterior = writer.store_array(
                cut.id,
                expected_posteriors[-1],
                frame_shift=cut.frame_shift,
                temporal_dim=0,
            )

        posteriors, posterior_lens = collate_custom_field(
            cuts, "posterior", pad_value=pad_value
        )

        assert isinstance(posterior_lens, torch.Tensor)
        assert posterior_lens.dtype == torch.int32
        assert posterior_lens.shape == (len(cuts),)
        assert posterior_lens.tolist() == [c.num_frames for c in cuts]

        assert isinstance(posteriors, torch.Tensor)
        assert posteriors.dtype == torch.float32
        assert posteriors.shape == (len(cuts), max_num_frames, VOCAB_SIZE)
        for idx, post in enumerate(expected_posteriors):
            exp_len = post.shape[0]
            torch.testing.assert_allclose(posteriors[idx, :exp_len], post)
            expected_pad_value = 0 if pad_value is None else pad_value
            torch.testing.assert_allclose(
                posteriors[idx, exp_len:],
                expected_pad_value * torch.ones_like(posteriors[idx, exp_len:]),
            )

@pytest.mark.parametrize(
    "pad_value",
    [-100, None],  # None means user forgot to specify pad_value
)
def test_collate_custom_temporal_array_ints(pad_value):
    CODEBOOK_SIZE = 512
    FRAME_SHIFT = 0.04

    cuts = CutSet.from_json("test/fixtures/ljspeech/cuts.json")
    max_num_frames = max(seconds_to_frames(cut.duration, FRAME_SHIFT) for cut in cuts)

    with NamedTemporaryFile(suffix=".h5") as f, NumpyHdf5Writer(f.name) as writer:
        expected_codebook_indices = []
        for cut in cuts:
            expected_codebook_indices.append(
                np.random.randint(
                    CODEBOOK_SIZE, size=(seconds_to_frames(cut.duration, FRAME_SHIFT),)
                ).astype(np.int16)
            )
            cut.codebook_indices = writer.store_array(
                cut.id,
                expected_codebook_indices[-1],
                frame_shift=FRAME_SHIFT,
                temporal_dim=0,
            )

        codebook_indices, codebook_indices_lens = collate_custom_field(
            cuts, "codebook_indices", pad_value=pad_value
        )

        assert isinstance(codebook_indices_lens, torch.Tensor)
        assert codebook_indices_lens.dtype == torch.int32
        assert codebook_indices_lens.shape == (len(cuts),)
        assert codebook_indices_lens.tolist() == [
            seconds_to_frames(c.duration, FRAME_SHIFT) for c in cuts
        ]

        assert isinstance(codebook_indices, torch.Tensor)
        assert codebook_indices.dtype == torch.int16
        assert codebook_indices.shape == (len(cuts), max_num_frames)
        for idx, cbidxs in enumerate(expected_codebook_indices):
            exp_len = cbidxs.shape[0]
            # PyTorch < 1.9.0 doesn't have an assert_equal function.
            np.testing.assert_equal(codebook_indices[idx, :exp_len].numpy(), cbidxs)
            expected_pad_value = 0 if pad_value is None else pad_value
            np.testing.assert_equal(
                codebook_indices[idx, exp_len:].numpy(), expected_pad_value
            )

@pytest.mark.parametrize(
    "pad_direction",
    ["right", "left", "both"],
)
def test_collate_custom_temporal_array_ints(pad_direction):
    CODEBOOK_SIZE = 512
    FRAME_SHIFT = 0.04
    EXPECTED_PAD_VALUE = 0

    cuts = CutSet.from_json("test/fixtures/ljspeech/cuts.json")
    max_num_frames = max(seconds_to_frames(cut.duration, FRAME_SHIFT) for cut in cuts)

    with NamedTemporaryFile(suffix=".h5") as f, NumpyHdf5Writer(f.name) as writer:
        expected_codebook_indices = []
        for cut in cuts:
            expected_codebook_indices.append(
                np.random.randint(
                    CODEBOOK_SIZE, size=(seconds_to_frames(cut.duration, FRAME_SHIFT),)
                ).astype(np.int16)
            )
            cut.codebook_indices = writer.store_array(
                cut.id,
                expected_codebook_indices[-1],
                frame_shift=FRAME_SHIFT,
                temporal_dim=0,
            )

        codebook_indices, codebook_indices_lens = collate_custom_field(
            cuts, "codebook_indices", pad_direction=pad_direction
        )

        assert isinstance(codebook_indices_lens, torch.Tensor)
        assert codebook_indices_lens.dtype == torch.int32
        assert codebook_indices_lens.shape == (len(cuts),)
        assert codebook_indices_lens.tolist() == [
            seconds_to_frames(c.duration, FRAME_SHIFT) for c in cuts
        ]

        assert isinstance(codebook_indices, torch.Tensor)
        assert (
            codebook_indices.dtype == torch.int64
        )  # the dtype got promoted by default
        assert codebook_indices.shape == (len(cuts), max_num_frames)
        for idx, cbidxs in enumerate(expected_codebook_indices):
            exp_len = cbidxs.shape[0]
            # PyTorch < 1.9.0 doesn't have an assert_equal function.
            if pad_direction == "right":
                np.testing.assert_equal(codebook_indices[idx, :exp_len].numpy(), cbidxs)
                np.testing.assert_equal(
                    codebook_indices[idx, exp_len:].numpy(), EXPECTED_PAD_VALUE
                )
            if pad_direction == "left":
                np.testing.assert_equal(
                    codebook_indices[idx, -exp_len:].numpy(), cbidxs
                )
                np.testing.assert_equal(
                    codebook_indices[idx, :-exp_len].numpy(), EXPECTED_PAD_VALUE
                )
            if pad_direction == "both":
                half = (max_num_frames - exp_len) // 2
                np.testing.assert_equal(
                    codebook_indices[idx, :half].numpy(), EXPECTED_PAD_VALUE
                )
                np.testing.assert_equal(
                    codebook_indices[idx, half : half + exp_len].numpy(), cbidxs
                )
                if half > 0:
                    # indexing like [idx, -0:] would return the whole array rather
                    # than an empty slice.
                    np.testing.assert_equal(
                        codebook_indices[idx, -half:].numpy(), EXPECTED_PAD_VALUE
                    )

def test_collate_custom_attribute_missing():
    cuts = CutSet.from_json("test/fixtures/ljspeech/cuts.json")
    with pytest.raises(AttributeError):
        collate_custom_field(cuts, "nonexistent_attribute")

def test_padding_issue_478():
    """
    https://github.com/lhotse-speech/lhotse/issues/478
    """
    with NamedTemporaryFile(suffix=".h5") as f, NumpyHdf5Writer(f.name) as writer:

        # Prepare data for cut 1.
        cut1 = MonoCut(
            "c1", start=0, duration=4.9, channel=0, recording=dummy_recording(1)
        )
        ali1 = np.random.randint(500, size=(121,))
        cut1.label_alignment = writer.store_array(
            "c1", ali1, frame_shift=0.04, temporal_dim=0
        )

        # Prepare data for cut 2.
        cut2 = MonoCut(
            "c2", start=0, duration=4.895, channel=0, recording=dummy_recording(2)
        )
        ali2 = np.random.randint(500, size=(121,))
        cut2.label_alignment = writer.store_array(
            "c2", ali2, frame_shift=0.04, temporal_dim=0
        )

        # Test collation behavior on this cutset.
        cuts = CutSet.from_cuts([cut1, cut2])
        label_alignments, label_alignment_lens = collate_custom_field(
            cuts, "label_alignment"
        )

        np.testing.assert_equal(label_alignments[0].numpy(), ali1)
        np.testing.assert_equal(label_alignments[1].numpy(), ali2)

max_stars_repo_name: armusc/lhotse

max_issues_repo_name: armusc/lhotse

max_forks_repo_name: armusc/lhotse


text:
#!/usr/bin/env python3
from __future__ import division, print_function

from ete3 import Tree
import numpy as np
from numpy import median, mean

import itertools as itl
from math import ceil, log
from string import ascii_uppercase

METRICS = [
    'mean',
    'median',
    'min',
    'max',
]

def pwdist(tree):
    '''Finds the (off-diagonal) pairwise distances between all tips of `tree`.
    '''
    dists = []
    for a, b in itl.combinations(tree.get_leaf_names(), 2):
        a = tree&a
        dists.append(a.get_distance(b))
    return np.array(dists)

def normalise_tree(tree, to=1.0, metric='mean'):
    '''
    Normalise branch lengths of `tree` such that `metric` of the pairwise
    distances is `to`.

    By default, normalise such that the mean of all pairwise distances is 1.0.
    '''
    dists = pwdist(tree)
    assert metric in METRICS
    current = eval('{}(dists)'.format(metric))

    for node in tree.iter_descendants():
        node.dist /= current
    return tree

def alphbetise_names(tree):
    '''Replace numeric tip labels with alphabetic ones. 1 -> A, 2 -> B etc.

    If there are more than 26 tips, labels are AA, AB, ..., ZZ and so forth for
    any number of tips.
    '''
    label_len = ceil(log(len(tree)) / log(26))  # how many letters do we need?
    labels = [''.join(letters)
              for letters in itl.product(ascii_uppercase, repeat=label_len)]

    tiplabels = list(sorted(tree.get_leaf_names(), key=int))
    for i, leaf in enumerate(tiplabels):
        node = tree&leaf
        node.name = labels[i]
    return tree

def main(treefile, to, metric):
    with open(treefile) as fh:
        for treeline in fh:
            tree = Tree(treeline)
            tree = alphbetise_names(tree)
            tree = normalise_tree(tree, to, metric)
            print(tree.write(format=5))

CLI = '''
USAGE:
    scaletree [options] TREEFILE

OPTIONS:
    -t TO       Scale tree metric to TO. [default: 1.0]
    -m METRIC   Metric for scaling. Must be one of mean, min, max.
                [default: mean]
'''

if __name__ == '__main__':
    from docopt import docopt
    opts = docopt(CLI)

    treefile = opts['TREEFILE']
    to = float(opts['-t'])
    metric = opts['-m']

    main(treefile, to, metric)

reason: import numpy,from numpy


text:
import sys
import os
HOME=os.environ['HOME']
sys.path.insert(1,HOME+'/github/StreamingSVM')
from numpy import genfromtxt
import numpy as np

class LoadLibSVM:

    def __init__(self, filename, n_features=1):
        self.filename = filename
        self.n_features = n_features

    def load_data(self):
        print("Reading " + self.filename + " with " + str(self.n_features))
        data = genfromtxt(self.filename, delimiter=',')
        rem = len(data) % 5
        if(rem == 0):
            for i in range(0, rem):
                data = np.delete(data, 0, 0)

        arrs = np.array_split(data, 5)

        training = np.concatenate((arrs[0],arrs[1], arrs[2]), axis=0)
        testing = np.concatenate((arrs[3], arrs[4]), axis=0)
        print(len(data),training.shape,training[0].shape)
        y_training = training[:,0]
        x_training = np.delete(training, 0, axis=1)
        y_testing = testing[:, 0]
        x_testing = np.delete(testing, 0, axis=1)
        print(x_training.shape, y_training.shape, x_testing.shape, y_testing.shape)
        return x_training, y_training, x_testing, y_testing

    def load_all_data(self):
        data = genfromtxt(self.filename, delimiter=',')
        y = data[:, 0]
        x = np.delete(data, 0, axis=1)
        return x, y

max_stars_repo_name: vibhatha/PSGDSVMPY

max_issues_repo_name: vibhatha/PSGDSVMPY

max_forks_repo_name: vibhatha/PSGDSVMPY


text:
import torch
from torch import nn
import os

import numpy as np
import matplotlib.pyplot as plt
from ipdb import set_trace
from scipy.linalg import solve_continuous_are
from scipy.io import loadmat
from scipy.interpolate import interpn

from systems.cartpole import CartPole
from stable_baselines3 import A2C, PPO, DDPG
from stable_baselines3.common.env_checker import check_env
from stable_baselines3.common.sb2_compat.rmsprop_tf_like import RMSpropTFLike

sys = {'mc': 5, 'mp': 1, 'l': 0.9, 'g': 9.81, 'Q': np.diag([25, 0.02, 25, 0.02]), 'R': np.diag([0.001, 0.001]),\
	   'goal': np.array([[0], [0], [np.pi], [0]]), 'u0': np.zeros((2,1)), 'T': 4, 'dt': 2.5e-3, 'gamma_': 0.997, 'X_DIMS': 4, 'U_DIMS': 2,\
	   'x_limits': np.array([[-1.5, 1.5], [-3, 3], [0, 2*np.pi], [-3, 3]]), 'u_limits': np.array([[-9, 9], [-9, 9]])}
# sys = {'mc': 5, 'mp': 1, 'l': 0.9, 'g': 9.81, 'Q': np.diag([25, 0.02, 25, 0.02]), 'R': np.diag([0.001, 0.001]),\
# 	   'goal': np.array([[0], [0], [np.pi], [0]]), 'u0': np.zeros((2,1)), 'T': 4, 'dt': 1e-3, 'gamma_': 0.997, 'X_DIMS': 4, 'U_DIMS': 2,\
# 	   'x_limits': np.array([[-1, 1], [-1, 1], [3*np.pi/4, 5*np.pi/4], [-1, 1]]), 'u_limits': np.array([[-9, 9], [-9, 9]])}
fixed_start = False
normalized_actions = True
env = CartPole(sys, fixed_start=fixed_start, normalized_actions=normalized_actions)
check_env(env)

# Load DP solution to compare
# filename = '~/Documents/MATLAB/iLQG/DP/data/cartpole/decomp0/final.mat'
# policy_analytical = loadmat(filename)

env_dummy = CartPole(sys, fixed_start=fixed_start, normalized_actions=normalized_actions)
check_env(env_dummy)
goal = sys['goal']
u0 = sys['u0']
A = np.zeros((sys['X_DIMS'], sys['X_DIMS']))
B = np.zeros((sys['X_DIMS'], sys['U_DIMS']))
for xx in range(sys['X_DIMS']):
	perturb_p = np.zeros(goal.shape)
	perturb_p[xx] = 1e-4
	perturb_m = np.zeros(goal.shape)
	perturb_m[xx] = -1e-4
	dyn_p = env_dummy.dyn(goal + perturb_p, u0)
	dyn_m = env_dummy.dyn(goal + perturb_m, u0)
	A[:, xx:(xx+1)] = (dyn_p - dyn_m) / (2e-4)

for uu in range(sys['U_DIMS']):
	perturb_p = np.zeros(u0.shape)
	perturb_p[uu] = 1e-4
	perturb_m = np.zeros(u0.shape)
	perturb_m[uu] = -1e-4
	dyn_p = env_dummy.dyn(goal, u0 + perturb_p)
	dyn_m = env_dummy.dyn(goal, u0 + perturb_m)
	B[:, uu:(uu+1)] = (dyn_p - dyn_m) / (2e-4)

Q = sys['Q']
R = sys['R']
lambda_ = (1 - sys['gamma_']) / sys['dt']
P = solve_continuous_are(A - lambda_/2*np.eye(sys['X_DIMS']), B, Q, R)
K = np.matmul(np.linalg.inv(R), np.matmul(B.T, P))

# Test the policy
obs = env_dummy.reset()
start = obs
u_limits = sys['u_limits']
value = 0
for i in range(int(3*sys['T']/sys['dt'])):
	action = np.matmul(-K, obs[:,np.newaxis] - goal)[:,0]
	action = np.maximum(u_limits[:,0], np.minimum(u_limits[:,1], action))
	if (normalized_actions):
		action = (2*action - (u_limits[:,1] + u_limits[:,0])) / (u_limits[:,1] - u_limits[:,0])

	obs, reward, done, info = env_dummy.step(action)
	value *= sys['gamma_']
	value += reward
	if done:
		print('Start state :', start, ', Final state :', obs, 'Value :', value)
		obs = env_dummy.reset()
		start = obs
		value = 0

set_trace()
# Compute Policy and Value function numerically
algorithm = 'A2C'
if (fixed_start):
	save_path = os.path.join('examples/data/cartpole_fixedstart', algorithm)
else:
	save_path = os.path.join('examples/data/cartpole', algorithm)
log_path = os.path.join(save_path, 'tb_log')
files = [f for f in os.listdir(save_path) if os.path.isfile(os.path.join(save_path, f))]
save_timestep = 0
ff_latest = ''
for ff in files:
	if 'model' not in ff:
		continue
	tt = ff.split('_')[-1]
	tt = int(tt.split('.')[0])
	if (tt > save_timestep):
		save_timestep = tt
		ff_latest = ff

total_timesteps = 10000000
if ((save_timestep <= total_timesteps) and (save_timestep > 0)):
	if (algorithm == 'A2C'):
		model = A2C.load(os.path.join(save_path, 'model_'+str(save_timestep)))
	elif (algorithm == 'PPO'):
		model = PPO.load(os.path.join(save_path, 'model_'+str(save_timestep)))
	elif (algorithm == 'DDPG'):
		model = DDPG.load(os.path.join(save_path, 'model_'+str(save_timestep)))
else:
	if (normalized_actions):
		policy_std = 0.1
	else:
		policy_std = 0.1 * sys['u_limits'][:,1]

	if (algorithm == 'A2C'):
		policy_kwargs = dict(activation_fn=nn.ReLU, net_arch=[dict(pi=[32, 32], vf=[32, 32])], log_std_init=np.log(policy_std), optimizer_class=RMSpropTFLike, optimizer_kwargs=dict(eps=1e-5))
		model = A2C('MlpPolicy', env, gamma=sys['gamma_'], n_steps=50, tensorboard_log=log_path, verbose=1, policy_kwargs=policy_kwargs)
	elif (algorithm == 'PPO'):
		policy_kwargs = dict(activation_fn=nn.ReLU, net_arch=[dict(pi=[32, 32], vf=[32, 32])])
		model = PPO('MlpPolicy', env, gamma=sys['gamma_'], n_steps=2048, clip_range_vf=None, clip_range=0.5, tensorboard_log=log_path, verbose=1, policy_kwargs=policy_kwargs)
	elif (algorithm == 'DDPG'):
		policy_kwargs = dict(activation_fn=nn.ReLU, net_arch=dict(pi=[16, 16], qf=[16, 16]))
		model = DDPG('MlpPolicy', env, gamma=sys['gamma_'], tensorboard_log=log_path, verbose=1, policy_kwargs=policy_kwargs)

save_every = total_timesteps
# save_every = 500000
timesteps = save_timestep
log_steps = 4000
while timesteps < total_timesteps:
	model.learn(total_timesteps=save_every, log_interval=round(log_steps/model.n_steps))
	timesteps = timesteps + save_every
	model.save(os.path.join(save_path, 'model_' + str(timesteps)))

# Test the learned policy
obs = env.reset()
start = obs
for i in range(24000):
	action, _state = model.predict(obs, deterministic=True)
	obs, reward, done, info = env.step(action)
	# if (i%100==0):
	# 	print('State : ', obs, ', Action : ', action)
		# set_trace()
	if done:
		print('Start state :', start, ', Final state :', obs)
		obs = env.reset()
		start = obs

set_trace()

reason: import numpy,from scipy


text:
import torch
import numpy as np
from torch import nn
import torch.nn.functional as F
from typing import Any, Dict, List, Type
from torch.distributions import kl_divergence

from tianshou.policy import A2CPolicy
from tianshou.data import Batch, ReplayBuffer

class NPGPolicy(A2CPolicy):
    """Implementation of Natural Policy Gradient.

    https://proceedings.neurips.cc/paper/2001/file/4b86abe48d358ecf194c56c69108433e-Paper.pdf

    :param torch.nn.Module actor: the actor network following the rules in
        :class:`~tianshou.policy.BasePolicy`. (s -> logits)
    :param torch.nn.Module critic: the critic network. (s -> V(s))
    :param torch.optim.Optimizer optim: the optimizer for actor and critic network.
    :param dist_fn: distribution class for computing the action.
    :type dist_fn: Type[torch.distributions.Distribution]
    :param bool advantage_normalization: whether to do per mini-batch advantage
        normalization. Default to True.
    :param int optim_critic_iters: Number of times to optimize critic network per
        update. Default to 5.
    :param float gae_lambda: in [0, 1], param for Generalized Advantage Estimation.
        Default to 0.95.
    :param bool reward_normalization: normalize estimated values to have std close to
        1. Default to False.
    :param int max_batchsize: the maximum size of the batch when computing GAE,
        depends on the size of available memory and the memory cost of the
        model; should be as large as possible within the memory constraint.
        Default to 256.
    :param bool action_scaling: whether to map actions from range [-1, 1] to range
        [action_spaces.low, action_spaces.high]. Default to True.
    :param str action_bound_method: method to bound action to range [-1, 1], can be
        either "clip" (for simply clipping the action), "tanh" (for applying tanh
        squashing) for now, or empty string for no bounding. Default to "clip".
    :param Optional[gym.Space] action_space: env's action space, mandatory if you want
        to use option "action_scaling" or "action_bound_method". Default to None.
    :param lr_scheduler: a learning rate scheduler that adjusts the learning rate in
        optimizer in each policy.update(). Default to None (no lr_scheduler).
    :param bool deterministic_eval: whether to use deterministic action instead of
        stochastic action sampled by the policy. Default to False.
    """

    def __init__(
        self,
        actor: torch.nn.Module,
        critic: torch.nn.Module,
        optim: torch.optim.Optimizer,
        dist_fn: Type[torch.distributions.Distribution],
        advantage_normalization: bool = True,
        optim_critic_iters: int = 5,
        actor_step_size: float = 0.5,
        **kwargs: Any,
    ) -> None:
        super().__init__(actor, critic, optim, dist_fn, **kwargs)
        del self._weight_vf, self._weight_ent, self._grad_norm
        self._norm_adv = advantage_normalization
        self._optim_critic_iters = optim_critic_iters
        self._step_size = actor_step_size
        # adjusts Hessian-vector product calculation for numerical stability
        self._damping = 0.1

    def process_fn(
        self, batch: Batch, buffer: ReplayBuffer, indices: np.ndarray
    ) -> Batch:
        batch = super().process_fn(batch, buffer, indices)
        old_log_prob = []
        with torch.no_grad():
            for b in batch.split(self._batch, shuffle=False, merge_last=True):
                old_log_prob.append(self(b).dist.log_prob(b.act))
        batch.logp_old = torch.cat(old_log_prob, dim=0)
        if self._norm_adv:
            batch.adv = (batch.adv - batch.adv.mean()) / batch.adv.std()
        return batch

    def learn(  # type: ignore
        self, batch: Batch, batch_size: int, repeat: int, **kwargs: Any
    ) -> Dict[str, List[float]]:
        actor_losses, vf_losses, kls = [], [], []
        for step in range(repeat):
            for b in batch.split(batch_size, merge_last=True):
                # optimize actor
                # direction: calculate villia gradient
                dist = self(b).dist
                log_prob = dist.log_prob(b.act)
                log_prob = log_prob.reshape(log_prob.size(0), -1).transpose(0, 1)
                actor_loss = -(log_prob * b.adv).mean()
                flat_grads = self._get_flat_grad(
                    actor_loss, self.actor, retain_graph=True).detach()

                # direction: calculate natural gradient
                with torch.no_grad():
                    old_dist = self(b).dist

                kl = kl_divergence(old_dist, dist).mean()
                # calculate first order gradient of kl with respect to theta
                flat_kl_grad = self._get_flat_grad(kl, self.actor, create_graph=True)
                search_direction = -self._conjugate_gradients(
                    flat_grads, flat_kl_grad, nsteps=10)

                # step
                with torch.no_grad():
                    flat_params = torch.cat([param.data.view(-1)
                                             for param in self.actor.parameters()])
                    new_flat_params = flat_params + self._step_size * search_direction
                    self._set_from_flat_params(self.actor, new_flat_params)
                    new_dist = self(b).dist
                    kl = kl_divergence(old_dist, new_dist).mean()

                # optimize citirc
                for _ in range(self._optim_critic_iters):
                    value = self.critic(b.obs).flatten()
                    vf_loss = F.mse_loss(b.returns, value)
                    self.optim.zero_grad()
                    vf_loss.backward()
                    self.optim.step()

                actor_losses.append(actor_loss.item())
                vf_losses.append(vf_loss.item())
                kls.append(kl.item())

        # update learning rate if lr_scheduler is given
        if self.lr_scheduler is not None:
            self.lr_scheduler.step()

        return {
            "loss/actor": actor_losses,
            "loss/vf": vf_losses,
            "kl": kls,
        }

    def _MVP(self, v: torch.Tensor, flat_kl_grad: torch.Tensor) -> torch.Tensor:
        """Matrix vector product."""
        # caculate second order gradient of kl with respect to theta
        kl_v = (flat_kl_grad * v).sum()
        flat_kl_grad_grad = self._get_flat_grad(
            kl_v, self.actor, retain_graph=True).detach()
        return flat_kl_grad_grad + v * self._damping

    def _conjugate_gradients(
        self,
        b: torch.Tensor,
        flat_kl_grad: torch.Tensor,
        nsteps: int = 10,
        residual_tol: float = 1e-10
    ) -> torch.Tensor:
        x = torch.zeros_like(b)
        r, p = b.clone(), b.clone()
        # Note: should be 'r, p = b - MVP(x)', but for x=0, MVP(x)=0.
        # Change if doing warm start.
        rdotr = r.dot(r)
        for i in range(nsteps):
            z = self._MVP(p, flat_kl_grad)
            alpha = rdotr / p.dot(z)
            x += alpha * p
            r -= alpha * z
            new_rdotr = r.dot(r)
            if new_rdotr < residual_tol:
                break
            p = r + new_rdotr / rdotr * p
            rdotr = new_rdotr
        return x

    def _get_flat_grad(
        self, y: torch.Tensor, model: nn.Module, **kwargs: Any
    ) -> torch.Tensor:
        grads = torch.autograd.grad(y, model.parameters(), **kwargs)  # type: ignore
        return torch.cat([grad.reshape(-1) for grad in grads])

    def _set_from_flat_params(
        self, model: nn.Module, flat_params: torch.Tensor
    ) -> nn.Module:
        prev_ind = 0
        for param in model.parameters():
            flat_size = int(np.prod(list(param.size())))
            param.data.copy_(
                flat_params[prev_ind:prev_ind + flat_size].view(param.size()))
            prev_ind += flat_size
        return model


text:
"""Module implementing GORU Cell.
"""
import tensorflow as tf
import numpy as np
from tensorflow.python.framework import ops
from tensorflow.python.ops import init_ops
from tensorflow.python.ops import array_ops
from tensorflow.python.ops import math_ops
from tensorflow.python.ops import control_flow_ops
from tensorflow.python.ops import gen_math_ops
from tensorflow.python.ops import tensor_array_ops
from tensorflow.python.ops import variable_scope as vs
from tensorflow.python.ops.rnn_cell_impl import RNNCell
from modrelu import modrelu

def _eunn_param(hidden_size, capacity=2, fft=False, comp=True):
	"""
	Create parameters and do the initial preparations
	"""
	theta_phi_initializer = init_ops.random_uniform_initializer(-np.pi, np.pi)
	if fft:
		capacity = int(np.ceil(np.log2(hidden_size)))

		diag_list_0 = []
		off_list_0 = []
		varsize = 0
		for i in range(capacity):
			size = capacity - i
			normal_size = (hidden_size // (2 ** size)) * (2 ** (size - 1))
			extra_size = max(0, (hidden_size % (2 ** size)) - (2 ** (size - 1)))
			varsize += normal_size + extra_size

		params_theta = vs.get_variable("theta_0", [varsize], initializer=theta_phi_initializer)
		cos_theta = math_ops.cos(params_theta)
		sin_theta = math_ops.sin(params_theta)

		if comp:
			params_phi = vs.get_variable("phi_0", [varsize], initializer=theta_phi_initializer)
			cos_phi = math_ops.cos(params_phi)
			sin_phi = math_ops.sin(params_phi)

			cos_list_0 = math_ops.complex(cos_theta, array_ops.zeros_like(cos_theta))
			cos_list_1 = math_ops.complex(math_ops.multiply(cos_theta, cos_phi), math_ops.multiply(cos_theta, sin_phi))
			sin_list_0 = math_ops.complex(sin_theta, array_ops.zeros_like(sin_theta))
			sin_list_1 = math_ops.complex(-math_ops.multiply(sin_theta, cos_phi), -math_ops.multiply(sin_theta, sin_phi))

		last = 0
		for i in range(capacity):
			size = capacity - i
			normal_size = (hidden_size // (2 ** size)) * (2 ** (size - 1))
			extra_size = max(0, (hidden_size % (2 ** size)) - (2 ** (size - 1)))

			if comp:
				cos_list_normal = array_ops.concat([array_ops.slice(cos_list_0, [last], [normal_size]), array_ops.slice(cos_list_1, [last], [normal_size])], 0)
				sin_list_normal = array_ops.concat([array_ops.slice(sin_list_0, [last], [normal_size]), -array_ops.slice(sin_list_1, [last], [normal_size])], 0)
				last += normal_size

				cos_list_extra = array_ops.concat([array_ops.slice(cos_list_0, [last], [extra_size]), math_ops.complex(tf.ones([hidden_size - 2*normal_size - 2*extra_size]), tf.zeros([hidden_size - 2*normal_size - 2*extra_size])), array_ops.slice(cos_list_1, [last], [extra_size])], 0)
				sin_list_extra = array_ops.concat([array_ops.slice(sin_list_0, [last], [extra_size]), math_ops.complex(tf.zeros([hidden_size - 2*normal_size - 2*extra_size]), tf.zeros([hidden_size - 2*normal_size - 2*extra_size])), -array_ops.slice(sin_list_1, [last], [extra_size])], 0)
				last += extra_size

			else:
				cos_list_normal = array_ops.slice(cos_theta, [last], [normal_size])
				cos_list_normal = array_ops.concat([cos_list_normal, cos_list_normal], 0)
				cos_list_extra = array_ops.slice(cos_theta, [last+normal_size], [extra_size])
				cos_list_extra = array_ops.concat([cos_list_extra, tf.ones([hidden_size - 2*normal_size - 2*extra_size]), cos_list_extra], 0)

				sin_list_normal = array_ops.slice(sin_theta, [last], [normal_size])
				sin_list_normal = array_ops.concat([sin_list_normal, -sin_list_normal], 0)
				sin_list_extra = array_ops.slice(sin_theta, [last+normal_size], [extra_size])
				sin_list_extra = array_ops.concat([sin_list_extra, tf.zeros([hidden_size - 2*normal_size - 2*extra_size]), -sin_list_extra], 0)

				last += normal_size + extra_size

			if normal_size != 0:
				cos_list_normal = array_ops.reshape(array_ops.transpose(array_ops.reshape(cos_list_normal, [-1, 2*normal_size//(2**size)])), [-1])
				sin_list_normal = array_ops.reshape(array_ops.transpose(array_ops.reshape(sin_list_normal, [-1, 2*normal_size//(2**size)])), [-1])

			cos_list = array_ops.concat([cos_list_normal, cos_list_extra], 0)
			sin_list = array_ops.concat([sin_list_normal, sin_list_extra], 0)
			diag_list_0.append(cos_list)
			off_list_0.append(sin_list)

		diag_vec = array_ops.stack(diag_list_0, 0)
		off_vec = array_ops.stack(off_list_0, 0)

	else:
		capacity_b = capacity//2
		capacity_a = capacity - capacity_b

		hidden_size_a = hidden_size//2
		hidden_size_b = (hidden_size-1)//2

		params_theta_0 = vs.get_variable("theta_0", [capacity_a, hidden_size_a], initializer=theta_phi_initializer)
		cos_theta_0 = array_ops.reshape(math_ops.cos(params_theta_0), [capacity_a, -1, 1])
		sin_theta_0 = array_ops.reshape(math_ops.sin(params_theta_0), [capacity_a, -1, 1])

		params_theta_1 = vs.get_variable("theta_1", [capacity_b, hidden_size_b], initializer=theta_phi_initializer)
		cos_theta_1 = array_ops.reshape(math_ops.cos(params_theta_1), [capacity_b, -1, 1])
		sin_theta_1 = array_ops.reshape(math_ops.sin(params_theta_1), [capacity_b, -1, 1])

		if comp:
			params_phi_0 = vs.get_variable("phi_0", [capacity_a, hidden_size_a], initializer=theta_phi_initializer)
			cos_phi_0 = array_ops.reshape(math_ops.cos(params_phi_0), [capacity_a, -1, 1])
			sin_phi_0 = array_ops.reshape(math_ops.sin(params_phi_0), [capacity_a, -1, 1])

			cos_list_0_re = array_ops.reshape(array_ops.concat([cos_theta_0, math_ops.multiply(cos_theta_0, cos_phi_0)], 2), [capacity_a, -1])
			cos_list_0_im = array_ops.reshape(array_ops.concat([array_ops.zeros_like(cos_theta_0), math_ops.multiply(cos_theta_0, sin_phi_0)], 2), [capacity_a, -1])
			if hidden_size_a*2 != hidden_size:
				cos_list_0_re = array_ops.concat([cos_list_0_re, tf.ones([capacity_a, 1])], 1)
				cos_list_0_im = array_ops.concat([cos_list_0_im, tf.zeros([capacity_a, 1])], 1)
			cos_list_0 = math_ops.complex(cos_list_0_re, cos_list_0_im)

			sin_list_0_re = array_ops.reshape(array_ops.concat([sin_theta_0, - math_ops.multiply(sin_theta_0, cos_phi_0)], 2), [capacity_a, -1])
			sin_list_0_im = array_ops.reshape(array_ops.concat([array_ops.zeros_like(sin_theta_0), - math_ops.multiply(sin_theta_0, sin_phi_0)], 2), [capacity_a, -1])
			if hidden_size_a*2 != hidden_size:
				sin_list_0_re = array_ops.concat([sin_list_0_re, tf.zeros([capacity_a, 1])], 1)
				sin_list_0_im = array_ops.concat([sin_list_0_im, tf.zeros([capacity_a, 1])], 1)
			sin_list_0 = math_ops.complex(sin_list_0_re, sin_list_0_im)

			params_phi_1 = vs.get_variable("phi_1", [capacity_b, hidden_size_b], initializer=theta_phi_initializer)
			cos_phi_1 = array_ops.reshape(math_ops.cos(params_phi_1), [capacity_b, -1, 1])
			sin_phi_1 = array_ops.reshape(math_ops.sin(params_phi_1), [capacity_b, -1, 1])

			cos_list_1_re = array_ops.reshape(array_ops.concat([cos_theta_1, math_ops.multiply(cos_theta_1, cos_phi_1)], 2), [capacity_b, -1])
			cos_list_1_re = array_ops.concat([tf.ones((capacity_b, 1)), cos_list_1_re], 1)
			cos_list_1_im = array_ops.reshape(array_ops.concat([array_ops.zeros_like(cos_theta_1), math_ops.multiply(cos_theta_1, sin_phi_1)], 2), [capacity_b, -1])
			cos_list_1_im = array_ops.concat([tf.zeros((capacity_b, 1)), cos_list_1_im], 1)
			if hidden_size_b*2 != hidden_size-1:
				cos_list_1_re = array_ops.concat([cos_list_1_re, tf.ones([capacity_b, 1])], 1)
				cos_list_1_im = array_ops.concat([cos_list_1_im, tf.zeros([capacity_b, 1])], 1)
			cos_list_1 = math_ops.complex(cos_list_1_re, cos_list_1_im)

			sin_list_1_re = array_ops.reshape(array_ops.concat([sin_theta_1, -math_ops.multiply(sin_theta_1, cos_phi_1)], 2), [capacity_b, -1])
			sin_list_1_re = array_ops.concat([tf.zeros((capacity_b, 1)), sin_list_1_re], 1)
			sin_list_1_im = array_ops.reshape(array_ops.concat([array_ops.zeros_like(sin_theta_1), -math_ops.multiply(sin_theta_1, sin_phi_1)], 2), [capacity_b, -1])
			sin_list_1_im = array_ops.concat([tf.zeros((capacity_b, 1)), sin_list_1_im], 1)
			if hidden_size_b*2 != hidden_size-1:
				sin_list_1_re = array_ops.concat([sin_list_1_re, tf.zeros([capacity_b, 1])], 1)
				sin_list_1_im = array_ops.concat([sin_list_1_im, tf.zeros([capacity_b, 1])], 1)
			sin_list_1 = math_ops.complex(sin_list_1_re, sin_list_1_im)
		else:
			cos_list_0 = array_ops.reshape(array_ops.concat([cos_theta_0, cos_theta_0], 2), [capacity_a, -1])
			sin_list_0 = array_ops.reshape(array_ops.concat([sin_theta_0, -sin_theta_0], 2), [capacity_a, -1])
			if hidden_size_a*2 != hidden_size:
				cos_list_0 = array_ops.concat([cos_list_0, tf.ones([capacity_a, 1])], 1)
				sin_list_0 = array_ops.concat([sin_list_0, tf.zeros([capacity_a, 1])], 1)

			cos_list_1 = array_ops.reshape(array_ops.concat([cos_theta_1, cos_theta_1], 2), [capacity_b, -1])
			cos_list_1 = array_ops.concat([tf.ones((capacity_b, 1)), cos_list_1], 1)
			sin_list_1 = array_ops.reshape(array_ops.concat([sin_theta_1, -sin_theta_1], 2), [capacity_b, -1])
			sin_list_1 = array_ops.concat([tf.zeros((capacity_b, 1)), sin_list_1], 1)
			if hidden_size_b*2 != hidden_size-1:
				cos_list_1 = array_ops.concat([cos_list_1, tf.zeros([capacity_b, 1])], 1)
				sin_list_1 = array_ops.concat([sin_list_1, tf.zeros([capacity_b, 1])], 1)

		if capacity_b != capacity_a:
			if comp:
				cos_list_1 = array_ops.concat([cos_list_1, math_ops.complex(tf.zeros([1, hidden_size]), tf.zeros([1, hidden_size]))], 0)
				sin_list_1 = array_ops.concat([sin_list_1, math_ops.complex(tf.zeros([1, hidden_size]), tf.zeros([1, hidden_size]))], 0)
			else:
				cos_list_1 = array_ops.concat([cos_list_1, tf.zeros([1, hidden_size])], 0)
				sin_list_1 = array_ops.concat([sin_list_1, tf.zeros([1, hidden_size])], 0)

		diag_vec = tf.reshape(tf.concat([cos_list_0, cos_list_1], 1), [capacity_a*2, hidden_size])
		off_vec = tf.reshape(tf.concat([sin_list_0, sin_list_1], 1), [capacity_a*2, hidden_size])

		if capacity_b != capacity_a:
			diag_vec = tf.slice(diag_vec, [0, 0], [capacity, hidden_size])
			off_vec = tf.slice(off_vec, [0, 0], [capacity, hidden_size])

	def _toTensorArray(elems):

		elems = ops.convert_to_tensor(elems)
		n = array_ops.shape(elems)[0]
		elems_ta = tensor_array_ops.TensorArray(dtype=elems.dtype, size=n, dynamic_size=False, infer_shape=True, clear_after_read=False)
		elems_ta = elems_ta.unstack(elems)
		return elems_ta

	diag_vec = _toTensorArray(diag_vec)
	off_vec = _toTensorArray(off_vec)
	if comp:
		omega = vs.get_variable("omega", [hidden_size], initializer=theta_phi_initializer)
		diag = math_ops.complex(math_ops.cos(omega), math_ops.sin(omega))
	else:
		diag = None

	return diag_vec, off_vec, diag, capacity

def _eunn_loop(state, capacity, diag_vec_list, off_vec_list, diag, fft):
	"""
	EUNN main loop, applying unitary matrix on input tensor
	"""
	i = 0
	def layer_tunable(x, i):

		diag_vec = diag_vec_list.read(i)
		off_vec = off_vec_list.read(i)

		diag = math_ops.multiply(x, diag_vec)
		off = math_ops.multiply(x, off_vec)

		def even_input(off, size):

			def even_s(off, size):
				off = array_ops.reshape(off, [-1, size//2, 2])
				off = array_ops.reshape(array_ops.reverse(off, [2]), [-1, size])
				return off

			def odd_s(off, size):
				off, helper = array_ops.split(off, [size-1, 1], 1)
				size -= 1
				off = even_s(off, size)
				off = array_ops.concat([off, helper], 1)
				return off

			off = control_flow_ops.cond(gen_math_ops.equal(gen_math_ops.mod(size, 2), 0), lambda: even_s(off, size), lambda: odd_s(off, size))
			return off

		def odd_input(off, size):
			helper, off = array_ops.split(off, [1, size-1], 1)
			size -= 1
			off = even_input(off, size)
			off = array_ops.concat([helper, off], 1)
			return off

		size = int(off.get_shape()[1])
		off = control_flow_ops.cond(gen_math_ops.equal(gen_math_ops.mod(i, 2), 0), lambda: even_input(off, size), lambda: odd_input(off, size))

		layer_output = diag + off
		i += 1

		return layer_output, i

	def layer_fft(state, i):

		diag_vec = diag_vec_list.read(i)
		off_vec = off_vec_list.read(i)
		diag = math_ops.multiply(state, diag_vec)
		off = math_ops.multiply(state, off_vec)

		hidden_size = int(off.get_shape()[1])
		# size = 2**i
		dist = capacity - i
		normal_size = (hidden_size // (2**dist)) * (2**(dist-1))
		normal_size *= 2
		extra_size = tf.maximum(0, (hidden_size % (2**dist)) - (2**(dist-1)))
		hidden_size -= normal_size

		def modify(off_normal, dist, normal_size):
			off_normal = array_ops.reshape(array_ops.reverse(array_ops.reshape(off_normal, [-1, normal_size//(2**dist), 2, (2**(dist-1))]), [2]), [-1, normal_size])
			return off_normal

		def do_nothing(off_normal):
			return off_normal

		off_normal, off_extra = array_ops.split(off, [normal_size, hidden_size], 1)
		off_normal = control_flow_ops.cond(gen_math_ops.equal(normal_size, 0), lambda: do_nothing(off_normal), lambda: modify(off_normal, dist, normal_size))
		helper1, helper2 = array_ops.split(off_extra, [hidden_size-extra_size, extra_size], 1)
		off_extra = array_ops.concat([helper2, helper1], 1)
		off = array_ops.concat([off_normal, off_extra], 1)

		layer_output = diag + off
		i += 1

		return layer_output, i

	if fft:
		layer_function = layer_fft
	else:
		layer_function = layer_tunable
	output, _ = control_flow_ops.while_loop(lambda state, i: gen_math_ops.less(i, capacity), layer_function, [state, i])

	if not diag is None:
		output = math_ops.multiply(output, diag)

	return output

class GORUCell(RNNCell):
	"""Gated Orthogonal Recurrent Unit Cell
	The implementation is based on: http://arxiv.org/abs/1706.02761.

	"""

	def __init__(self, hidden_size, capacity=2, fft=False, activation=modrelu):
		super(GORUCell, self).__init__()
		self._hidden_size = hidden_size
		self._activation = activation
		self._capacity = capacity
		self._fft = fft

		self.diag_vec, self.off_vec, self.diag, self._capacity = _eunn_param(hidden_size, capacity, fft, False)

	@property
	def state_size(self):
		return self._hidden_size

	@property
	def output_size(self):
		return self._hidden_size

	@property
	def capacity(self):
		return self._capacity

	def __call__(self, inputs, state, scope=None):
		with vs.variable_scope(scope or "goru_cell"):

			U_init = init_ops.random_uniform_initializer(-0.01, 0.01)
			b_init = init_ops.constant_initializer(2.)
			mod_b_init = init_ops.constant_initializer(2.)

			U = vs.get_variable("U", [inputs.get_shape()[-1], self._hidden_size * 3], dtype=tf.float32, initializer = U_init)
			Ux = math_ops.matmul(inputs, U)
			U_cx, U_rx, U_gx = array_ops.split(Ux, 3, axis=1)

			W_r = vs.get_variable("W_r", [self._hidden_size, self._hidden_size], dtype=tf.float32, initializer = U_init)
			W_g = vs.get_variable("W_g", [self._hidden_size, self._hidden_size], dtype=tf.float32, initializer = U_init)
			W_rh = math_ops.matmul(state, W_r)
			W_gh = math_ops.matmul(state, W_g)

			bias_r = vs.get_variable("bias_r", [self._hidden_size], dtype=tf.float32, initializer = b_init)
			bias_g = vs.get_variable("bias_g", [self._hidden_size], dtype=tf.float32)
			bias_c = vs.get_variable("bias_c", [self._hidden_size], dtype=tf.float32, initializer = mod_b_init)

			r_tmp = U_rx + W_rh + bias_r
			g_tmp = U_gx + W_gh + bias_g
			r = math_ops.sigmoid(r_tmp)

			g = math_ops.sigmoid(g_tmp)

			Unitaryh = _eunn_loop(state, self._capacity, self.diag_vec, self.off_vec, self.diag, self._fft)
			c = modrelu(math_ops.multiply(r, Unitaryh) + U_cx, bias_c, False)
			new_state = math_ops.multiply(g, state) +  math_ops.multiply(1 - g, c)

		return new_state, new_state

max_forks_repo_name: ekcole/RUM-Tensorflow


text:
from skimage import io, transform, img_as_float
from skimage.color import rgb2gray
from skimage.feature import canny
from matplotlib import pyplot as plt
from sklearn.neighbors import NearestNeighbors
from sklearn.decomposition import PCA
import os
import numpy as np
import pandas as pd
import pymongo

MONGODB_CONNECTION_STRING = 'mongodb://localhost:27017/fashionbot'
MONGODB_SERVER = 'ds155411.mlab.com:55411/fashionbot'
MONGODB_USER = 'admin'
MONGODB_PASSWORD = 'azzaropourhome2'
MONGODB_PORT = 27017
MONGODB_DB = 'fashionbot'
MONGODB_COLLECTION = 'clothes'

LOCAL_PATH = '/Users/guilherme/Documents/Documents/Projects/Machine Learning/FashionBot/scrapper/spiders/images/'
IMAGE_SIZE = (100, 100)

connection = pymongo.MongoClient(MONGODB_CONNECTION_STRING)
db = connection[MONGODB_DB]
collection = db[MONGODB_COLLECTION]

## cut white background
def cut_white_background(path):
    image = img_as_float(io.imread(path))

    # Select all pixels almost equal to white
    # (almost, because there are some edge effects in jpegs
    # so the boundaries may not be exactly white)
    white = np.array([1, 1, 1])
    mask = np.abs(image - white).sum(axis=2) < 0.05

    # Find the bounding box of those pixels
    coords = np.array(np.nonzero(~mask))
    top_left = np.min(coords, axis=1)
    bottom_right = np.max(coords, axis=1)

    out = image[top_left[0]:bottom_right[0],
                top_left[1]:bottom_right[1]]
    return out
    # plt.imshow(out)
    # plt.show()

def apply_PCA(data):
    '''
    DeprecationWarning: Passing 1d arrays as data is deprecated in 0.17 and willraise ValueError in 0.19.
    Reshape your data either using X.reshape(-1, 1) if your data has a single feature or X.reshape(1, -1) if it contains a
    single sample.
    '''
    # Create a regular PCA model
    pca = PCA(n_components=2)

    # Fit and transform the data to the model
    reduced_data_pca = pca.fit_transform(data)

    # Inspect the shape
    reduced_data_pca.shape

    # Print out the data
    # print(data)
    # print(reduced_data_pca)
    return reduced_data_pca

shoes = collection.find({}).limit(200)

i = 0
clothIndexing = {}
dataset = []
for shoe in shoes :
    for image in shoe['images']:
        originImage = image
        path = LOCAL_PATH + image['path']
        image = cut_white_background(path)
        image = transform.resize(image, IMAGE_SIZE)
        '''Split the target image into its red, green and blue channels.
            image - a numpy array of shape (rows, columns, 3).
            output - three numpy arrays of shape (rows, columns) and dtype same as
            image, containing the corresponding channels.
        '''
        red = image[:,:,2]
        green = image[:,:,1]
        blue = image[:,:,0]
        red_mean = np.mean(red)
        red_std = np.std(red)
        green_mean = np.mean(green)
        green_std = np.std(green)
        blue_mean = np.mean(blue)
        blue_std = np.std(blue)
        features = [red_mean,red_std,green_mean,green_std,blue_mean,blue_std]
        # edges feature
        # image = rgb2gray(image)
        # edges = canny(image)
        # image_array = edges.reshape((1, edges.shape[0] * edges.shape[1]))[0]
        # features = np.append([red_mean,red_std,green_mean,green_std,blue_mean,blue_std], image_array.astype(int))
        dataset.append(features)
        clothIndexing[i] = {'shoe': shoe, 'image': originImage}
        i+=1
        print 'I: ',i

neigh = NearestNeighbors(2, 0.4)
neigh.fit(dataset)
kneighbors = neigh.kneighbors(dataset[0], 10)
print 'Distances: ', kneighbors[0]

for index in np.nditer(kneighbors[1]):
    print clothIndexing[int(index)]['image']
    i+=1

max_stars_repo_name: guilhermeKodama/Closetinn

max_issues_repo_name: guilhermeKodama/Closetinn

max_forks_repo_name: guilhermeKodama/Closetinn


text:
# import os
# import re
import sys

# import pandas as pd
import numpy as np
# from tqdm import tqdm
from nlpia.loaders import get_data

if len(sys.argv) > 1:
    lang = sys.argv[1][:3].lower()
else:
    lang = 'spa'

source_lang = 'eng'
max_text_len = 32  # 32: 62,829 spa examples (2 min/epoch), 64: 100k spa examples (6 min/epoch)

df = get_data(lang)
if lang not in df.columns:
    # print(df.columns)
    print(f"changing language name {lang} to {list(df.columns)[-1]}")
    lang = list(df.columns)[-1]

df.columns = [source_lang, lang]
df = df.dropna()
df[source_lang + '_len'] = df[source_lang].str.len()
df = df[df[source_lang + '_len'] < max_text_len]
# need to make sure all batches are composed of similar-length texts (see NLPIA ch 10 & ch 13)

input_texts, target_texts = [], []  # <1>
start_token, stop_token = '\t\n'  # <3>
input_vocab = set()  # <2>
output_vocab = set(start_token + stop_token)
n_samples = min(100000, len(df))  # <4>

df['target'] = start_token + df[lang] + stop_token
for statement in df[source_lang]:
    input_vocab.update(set(statement))
for reply in df[lang]:
    output_vocab.update(set(reply))
input_vocab = tuple(sorted(input_vocab))
output_vocab = tuple(sorted(output_vocab))

max_encoder_seq_len = df[source_lang].str.len().max()
# max_encoder_seq_len
# 100
max_decoder_seq_len = df.target.str.len().max()
# max_decoder_seq_len
# 102

""" Construct character sequence encoder-decoder training set
"""
import numpy as np  # <1> # noqa

encoder_input_onehot = np.zeros(
    (len(df), max_encoder_seq_len, len(input_vocab)),
    dtype='float32')  # <2>
decoder_input_onehot = np.zeros(
    (len(df), max_decoder_seq_len, len(output_vocab)),
    dtype='float32')
decoder_target_onehot = np.zeros(
    (len(df), max_decoder_seq_len, len(output_vocab)),
    dtype='float32')

for i, (input_text, target_text) in enumerate(
        zip(df[source_lang], df.target)):  # <3>
    for t, c in enumerate(input_text):  # <4>
        k = input_vocab.index(c)
        encoder_input_onehot[i, t, k] = 1.  # <5>
    k = np.array([output_vocab.index(c) for c in target_text])
    decoder_input_onehot[i, np.arange(len(target_text)), k] = 1.
    decoder_target_onehot[i, np.arange(len(target_text) - 1), k[1:]] = 1.
# <1> You use numpy for the matrix manipulations.
# <2> The training tensors are initialized as zero tensors with the shape of number of samples
#     (this number should be equal for the input and target samples) times the maximum number of sequence tokens
#     times the number of possible characters.
# <3> Loop over the training samples; input and target texts need to match.
# <4> Loop over each character of each sample.
# <5> Set the index for the character at each time step to one; all other indices remain at zero.
#     This creates the one-hot encoded representation of the training samples.
# <6> For the training data for the decoder, you create the `decoder_input_data` and `decoder_target_data`
#     (which is one time step behind the _decoder_input_data_).

"""Construct and train a character sequence encoder-decoder network
"""
from keras.models import Model  # noqa
from keras.layers import Input, LSTM, Dense  # noqa

batch_size = 64    # <1>
epochs = 15       # <2>
num_neurons = 256  # <3>

encoder_inputs = Input(shape=(None, len(input_vocab)))
encoder = LSTM(num_neurons, return_state=True)
encoder_outputs, state_h, state_c = encoder(encoder_inputs)
encoder_states = [state_h, state_c]

decoder_inputs = Input(shape=(None, len(output_vocab)))
decoder_lstm = LSTM(num_neurons, return_sequences=True,
                    return_state=True)
decoder_outputs, _, _ = decoder_lstm(decoder_inputs,
                                     initial_state=encoder_states)
decoder_dense = Dense(len(output_vocab), activation='softmax')
decoder_outputs = decoder_dense(decoder_outputs)
model = Model([encoder_inputs, decoder_inputs], decoder_outputs)

model.compile(optimizer='rmsprop', loss='categorical_crossentropy',
              metrics=['acc'])

model.fit([encoder_input_onehot, decoder_input_onehot],
          decoder_target_onehot, batch_size=batch_size, epochs=epochs,
          validation_split=0.10)  # <4>
# 57915/57915 [==============================] - 296s 5ms/step - loss: 0.7575 - acc: 0.1210 - val_loss: 0.6521 - val_acc: 0.1517
# Epoch 2/100
# 57915/57915 [==============================] - 283s 5ms/step - loss: 0.5924 - acc: 0.1613 - val_loss: 0.5738 - val_acc: 0.1734
# ...
# 57915/57915 [==============================] - 276s 5ms/step - loss: 0.4235 - acc: 0.2075 - val_loss: 0.4688 - val_acc: 0.2034
# Epoch 21/100
# 57915/57915 [==============================] - 277s 5ms/step - loss: 0.4217 - acc: 0.2080 - val_loss: 0.4680 - val_acc: 0.2037
# Epoch 22/100
# 57915/57915 [==============================] - 278s 5ms/step - loss: 0.4198 - acc: 0.2084 - val_loss: 0.4686 - val_acc: 0.2035
# ...
# Epoch 69/100                                                                                [1480/1902]
# 57915/57915 [==============================] - 276s 5ms/step - loss: 0.3830 - acc: 0.2191 - val_loss: 0.4912 - val_acc: 0.2008
# Epoch 70/100
# 57915/57915 [==============================] - 277s 5ms/step - loss: 0.3826 - acc: 0.2193 - val_loss: 0.4902 - val_acc: 0.2007
# Epoch 71/100
# ...
# Epoch 99/100
# 57915/57915 [==============================] - 277s 5ms/step - loss: 0.3738 - acc: 0.2220 - val_loss: 0.5000 - val_acc: 0.1994
# Epoch 100/100
# 57915/57915 [==============================] - 278s 5ms/step - loss: 0.3736 - acc: 0.2220 - val_loss: 0.5017 - val_acc: 0.1992

""" .Construct response generator model
>>> encoder_model = Model(encoder_inputs, encoder_states)
>>> thought_input = [
...     Input(shape=(num_neurons,)), Input(shape=(num_neurons,))]
>>> decoder_outputs, state_h, state_c = decoder_lstm(
...     decoder_inputs, initial_state=thought_input)
>>> decoder_states = [state_h, state_c]
>>> decoder_outputs = decoder_dense(decoder_outputs)

>>> decoder_model = Model(
...     inputs=[decoder_inputs] + thought_input,
...     output=[decoder_outputs] + decoder_states)
"""
encoder_model = Model(encoder_inputs, encoder_states)
thought_input = [
    Input(shape=(num_neurons,)), Input(shape=(num_neurons,))]
decoder_outputs, state_h, state_c = decoder_lstm(
    decoder_inputs, initial_state=thought_input)
decoder_states = [state_h, state_c]
decoder_outputs = decoder_dense(decoder_outputs)

decoder_model = Model(
    inputs=[decoder_inputs] + thought_input,
    output=[decoder_outputs] + decoder_states)

r"""
>>> def decode_sequence(input_seq):
...     thought = encoder_model.predict(input_seq)  # <1>

...     target_seq = np.zeros((1, 1, len(output_vocab)))  # <2>
...     target_seq[0, 0, target_token_index[stop_token]
...         ] = 1.  # <3>
...     stop_condition = False
...     generated_sequence = ''

...     while not stop_condition:
...         output_tokens, h, c = decoder_model.predict(
...             [target_seq] + thought) # <4>

...         generated_token_idx = np.argmax(output_tokens[0, -1, :])
...         generated_char = reverse_target_char_index[generated_token_idx]
...         generated_sequence += generated_char
...         if (generated_char == stop_token or
...                 len(generated_sequence) > max_decoder_seq_len
...                 ):  # <5>
...             stop_condition = True

...         target_seq = np.zeros((1, 1, len(output_vocab)))  # <6>
...         target_seq[0, 0, generated_token_idx] = 1.
...         thought = [h, c]  # <7>

...     return generated_sequence
"""
def decode_sequence(input_seq):
    thought = encoder_model.predict(input_seq)  # <1>

    target_seq = np.zeros((1, 1, len(output_vocab)))  # <2>
    target_seq[0, 0, output_vocab.index(stop_token)] = 1.  # <3>
    stop_condition = False
    generated_sequence = ''

    while not stop_condition:
        output_tokens, h, c = decoder_model.predict(
            [target_seq] + thought) # <4>

        # take only the most likely character (temperature = 0)
        generated_token_idx = np.argmax(output_tokens[0, -1, :])
        generated_char = output_vocab[generated_token_idx]
        generated_sequence += generated_char
        if (generated_char == stop_token or
                len(generated_sequence) > max_decoder_seq_len
                ):  # <5>
            stop_condition = True

        target_seq = np.zeros((1, 1, len(output_vocab)))  # <6>
        target_seq[0, 0, generated_token_idx] = 1.
        thought = [h, c]  # <7>

    return generated_sequence

def respond(input_text, input_vocab=input_vocab):
    input_text = input_text.lower()
    input_text = ''.join(c if c in input_vocab else ' ' for c in input_text)
    input_seq = np.zeros((1, max_encoder_seq_len, len(input_vocab)), dtype='float32')
    for t, c in enumerate(input_text):
        input_seq[0, t, input_vocab.index(c)] = 1.
    decoded_sentence = decode_sequence(input_seq)
    print('Human: {}'.format(input_text))
    print('Bot:', decoded_sentence)
    return decoded_sentence

max_stars_repo_name: byukan/nlpia

max_issues_repo_name: byukan/nlpia

max_forks_repo_name: byukan/nlpia


text:
from __future__ import print_function
import os
import os.path as osp
import numpy as np

ROOT_DIR = osp.abspath(osp.join(osp.dirname(__file__), '..'))

def get_output_dir(imdb_name, net_name=None,output_dir='output'):
    """Return the directory where experimental artifacts are placed.
    If the directory does not exist, it is created.

    A canonical path is built using the name from an imdb and a network
    (if not None).
    """

    outdir = osp.abspath(osp.join(ROOT_DIR, output_dir, imdb_name))
    print(outdir)
    if net_name is not None:
        outdir = osp.join(outdir, net_name)

    if not os.path.exists(outdir):
        os.makedirs(outdir)
    return outdir


text:
import pandas as pd
from scipy.io.arff import loadarff

def data_albrecht():
    raw_data = loadarff("./data/albrecht.arff")
    df_data = pd.DataFrame(raw_data[0])
    return df_data

def data_china():
    raw_data = loadarff("./data/china.arff")
    df_data = pd.DataFrame(raw_data[0])
    new_chin = df_data.drop(columns=['ID', 'N_effort'])
    return new_chin

def data_desharnais():
    raw_data = loadarff("./data/desharnais.arff")
    df_data = pd.DataFrame(raw_data[0])
    new_desh = df_data.drop(index=[37, 43, 65, 74],
                            columns=['Project', 'YearEnd', 'Envergure', 'PointsNonAjust', 'Language'])
    columnsTitles = ['TeamExp', 'ManagerExp', 'Length', 'Transactions', 'Entities', 'PointsAdjust', 'Effort']
    new_desh = new_desh.reindex(columns=columnsTitles)
    return new_desh

def data_finnish():
    raw_data = loadarff("./data/finnish.arff")
    df_data = pd.DataFrame(raw_data[0])
    new_finn = df_data.drop(columns=['ID'])
    columnsTitles = ['hw', 'at', 'FP', 'co', 'prod', 'lnsize', 'lneff', 'dev.eff.hrs.']
    new_finn = new_finn.reindex(columns=columnsTitles)
    return new_finn

def data_isbsg10():
    raw_data = [
        [1, 1, 1, 1, 1, 1, 1, 225, 1, 1, 1, 1856],
        [1, 1, 1, 1, 1, 2, 1, 599, 2, 1, 2, 10083],
        [1, 1, 1, 2, 1, 2, 1, 333, 2, 1, 3, 5208],
        [1, 1, 2, 3, 2, 3, 1, 748, 2, 2, 3, 1518],
        [1, 1, 1, 1, 1, 4, 1, 158, 1, 1, 4, 3376],
        [1, 1, 1, 1, 1, 2, 1, 427, 2, 1, 3, 5170],
        [2, 2, 3, 4, 3, 5, 1, 461, 2, 3, 4, 12149],
        [1, 1, 4, 3, 2, 3, 1, 257, 1, 2, 3, 452],
        [1, 1, 1, 2, 3, 6, 1, 115, 1, 1, 4, 441],
        [1, 1, 5, 3, 2, 3, 1, 116, 1, 4, 4, 112],
        [1, 1, 1, 2, 1, 7, 1, 323, 2, 1, 3, 1530],
        [1, 1, 1, 2, 1, 1, 1, 134, 1, 1, 3, 1807],
        [1, 1, 1, 2, 1, 14, 1, 292, 1, 1, 3, 1087],
        [2, 2, 4, 4, 1, 8, 1, 399, 2, 3, 3, 7037],
        [1, 1, 1, 1, 1, 2, 1, 44, 3, 1, 4, 784],
        [1, 1, 1, 2, 1, 9, 1, 298, 1, 1, 4, 3268],
        [1, 1, 1, 2, 1, 2, 1, 66, 3, 1, 3, 800],
        [1, 1, 6, 3, 2, 3, 1, 243, 1, 2, 4, 257],
        [1, 1, 1, 4, 1, 10, 1, 1105, 4, 1, 5, 14453],
        [1, 1, 4, 3, 2, 3, 1, 679, 2, 4, 4, 326],
        [2, 2, 7, 5, 1, 4, 1, 303, 2, 3, 4, 8490],
        [1, 1, 1, 2, 1, 1, 1, 147, 1, 1, 3, 672],
        [1, 1, 7, 3, 2, 3, 1, 143, 1, 2, 3, 98],
        [1, 1, 1, 2, 1, 11, 1, 614, 2, 1, 4, 3280],
        [2, 2, 7, 4, 3, 5, 1, 183, 1, 3, 4, 7327],
        [1, 1, 8, 3, 2, 3, 1, 138, 1, 2, 4, 87],
        [1, 1, 1, 2, 3, 12, 1, 129, 1, 1, 3, 1261],
        [1, 1, 1, 2, 1, 2, 1, 205, 1, 1, 3, 3272],
        [1, 1, 1, 2, 1, 1, 1, 471, 2, 1, 3, 1464],
        [1, 1, 1, 5, 1, 4, 1, 97, 3, 1, 3, 1273],
        [1, 1, 3, 3, 2, 3, 1, 1371, 4, 2, 3, 2274],
        [1, 1, 1, 4, 1, 2, 1, 291, 1, 1, 4, 1772],
        [1, 1, 9, 3, 2, 3, 1, 995, 2, 2, 4, 614],
        [1, 2, 4, 2, 3, 6, 2, 211, 1, 3, 4, 1021],
        [2, 2, 10, 2, 3, 13, 2, 192, 1, 3, 4, 1806],
        [2, 2, 10, 2, 3, 13, 2, 98, 3, 3, 4, 921],
        [2, 2, 7, 4, 1, 14, 1, 112, 1, 3, 4, 2134]
    ]
    df_isbsg10 = pd.DataFrame(raw_data, columns=['Data_Quality','UFP','IS','DP','LT','PPL','CA','FS','RS',
                                                 'Recording_Method','FPS','Effort'])
    return df_isbsg10

def data_kemerer():
    raw_data = loadarff("./data/kemerer.arff")
    df_data = pd.DataFrame(raw_data[0])
    new_keme = df_data.drop(columns=['ID'])
    return new_keme

def data_kitchenham():
    raw_data = [
      [1,1,107,101.65,495,3,485],
      [1,2,144,57.12,1365,1,990],
      [1,2,604,1010.88,8244,3,13635],
      [1,3,226,45.6,1595,2,1576],
      [1,2,326,1022.58,3828,1,3826],
      [1,3,294,77.04,879,3,1079],
      [1,1,212,159.6,2895,3,2224],
      [1,4,175,225.54,1800,1,1698],
      [1,5,584,144.72,1160,3,1173],
      [1,2,171,84.42,885,3,1401],
      [1,2,201,126.42,2125,3,2170],
      [1,2,195,392.16,1381,3,1122],
      [1,6,109,18.9,1142,2,1024],
      [1,3,263,112.14,1895,3,1832],
      [1,1,165,210.08,1339,3,1016],
      [1,2,46,260.95,447,3,322],
      [2,2,186,609.7,507,3,580],
      [2,2,189,169.85,952,3,1003],
      [2,3,95,56,380,3,380],
      [2,3,53,30,220,3,220],
      [2,3,365,241.86,2879,3,2356],
      [2,3,438,219.88,1483,3,1388],
      [2,3,109,229.71,1667,3,1066],
      [2,2,283,458.38,2125,1,2860],
      [2,3,137,177.63,1175,1,1143],
      [2,3,102,287.64,2213,1,1431],
      [2,3,103,343.54,2247,1,1868],
      [2,3,192,346.8,1926,1,2172],
      [2,2,219,1121.48,5641,3,8509],
      [2,3,484,761.08,3928,3,5927],
      [2,3,173,464,1995,1,2663],
      [2,3,169,203.01,2281,3,1425],
      [2,3,207,550.14,3305,3,3504],
      [2,3,61,86.45,797,3,652],
      [2,3,311,1362.11,3922,1,7649],
      [2,3,418,681,6809,1,5927],
      [2,3,410,485.1,4955,1,6607],
      [2,0,497,172.96,1294,3,2591],
      [2,3,259,2075.8,5688,3,4494],
      [2,2,234,756.25,5245,3,4824],
      [2,0,462,789.66,3930,3,5094],
      [2,3,291,357,2562,3,3088],
      [2,3,116,62.08,1526,4,892],
      [2,3,128,157.56,1018,3,750],
      [2,2,185,322.62,5646,4,5646],
      [2,3,207,81.34,1532,3,1532],
      [2,3,151,191,1532,3,1280],
      [2,3,99,121.52,314,3,313],
      [2,3,61,222.78,412,1,339],
      [2,3,101,113.52,738,3,583],
      [2,0,462,15.36,763,3,726],
      [2,3,133,320.12,1750,1,1939],
      [2,3,106,84.28,682,1,669],
      [2,3,68,248.88,1320,3,1413],
      [2,3,239,616.32,3573,3,4115],
      [2,3,330,515.07,2913,1,4009],
      [2,3,37,88.2,701,3,630],
      [2,3,187,115.14,725,1,718],
      [2,3,329,63.84,1826,1,1584],
      [2,3,120,1015.98,5000,3,5816],
      [2,3,85,359.64,2640,1,2037],
      [2,3,49,240.84,2534,1,1428],
      [2,3,152,285.12,2231,1,1252],
      [2,3,47,61.2,1000,3,655],
      [2,0,148,287.28,1645,2,1318],
      [2,2,141,172,1067,3,995],
      [2,2,235,144.06,2270,3,2265],
      [2,2,298,108.64,656,3,654],
      [2,0,99,165.36,121,1,718],
      [2,3,127,680.9,1685,3,2029],
      [2,2,163,409.4,2350,3,1650],
      [2,2,316,313.95,2308,3,2223],
      [2,2,388,1136.34,7850,1,8600],
      [2,3,152,177,2004,3,1884],
      [2,2,166,746.24,3715,5,5359],
      [2,3,114,274.92,1273,1,1159],
      [2,3,82,43.5,437,1,437],
      [2,2,123,54.75,813,3,570],
      [2,3,49,130,900,3,502],
      [2,3,183,525.96,2475,1,1877],
      [2,3,149,311.85,799,1,1693],
      [2,0,370,1185.08,2160,3,3319],
      [2,2,128,258.24,1770,3,1557],
      [2,3,126,60,760,3,557],
      [2,3,200,303.52,2588,1,3050],
      [2,2,76,98.9,1148,1,1113],
      [2,3,299,711.9,4064,3,5456],
      [2,2,131,182.4,933,3,763],
      [2,0,140,351.9,2096,3,2203],
      [2,3,169,401.98,3284,3,3483],
      [2,3,130,162.61,4576,1,2393],
      [2,2,389,1210.99,14226,3,15673],
      [2,2,166,156.42,6080,3,2972],
      [2,3,148,603.58,4046,3,4068],
      [2,3,131,73.92,649,3,698],
      [2,3,144,121.55,817,3,676],
      [2,0,369,1234.2,6340,3,6307],
      [2,3,155,35,300,3,219],
      [2,0,102,61.06,315,3,254],
      [2,3,149,162,750,3,324],
      [2,3,548,1285.7,898,3,874],
      [2,2,946,18137.48,79870,3,113930],
      [2,2,186,1020.6,1600,3,1722],
      [2,2,212,377,1702,3,1660],
      [2,3,84,210.45,592,3,693],
      [2,2,250,410,2158,3,1455],
      [2,2,86,279,994,3,988],
      [2,2,102,240,1875,3,1940],
      [2,3,137,230,2527,3,2408],
      [2,2,87,150.29,2606,3,1737],
      [2,2,291,1940.68,12694,3,12646],
      [2,2,392,401,4176,3,4414],
      [2,2,165,267,2240,3,2480],
      [2,2,88,102,980,3,980],
      [2,2,249,403,3720,3,3189],
      [2,2,186,857,2914,3,2895],
      [2,2,63,69,360,3,322],
      [2,1,192,980.95,3700,3,3555],
      [2,3,123,100.8,200,3,570],
      [2,3,123,105.28,578,3,464],
      [2,2,186,158.4,1652,3,1742],
      [2,2,119,219.88,780,1,896],
      [2,3,195,1292.56,8690,3,8656],
      [2,3,210,616.08,3748,3,3966],
      [2,2,180,103.4,710,3,989],
      [2,3,238,74.4,856,3,585],
      [2,3,144,356.31,2436,3,1860],
      [2,3,432,862,4101,3,5249],
      [2,3,392,791.84,5231,3,5192],
      [2,2,205,661.27,2853,1,1832],
      [2,2,49,179,1246,3,928],
      [3,3,205,518.4,2570,3,2570],
      [3,2,145,370,1328,3,1328],
      [3,2,172,839.05,3380,3,2964],
      [3,3,137,243.86,1522,3,1304],
      [4,2,371,557.28,2264,3,1631],
      [4,4,217,485.94,2790,3,955],
      [4,2,308,698.54,1312,3,286],
      [4,2,217,752.64,2210,1,1432],
      [5,2,40,809.25,337,3,321],
      [6,3,253,178.1,865,3,593],
      [6,3,405,81.48,441,3,302],
      [6,3,241,1093.86,2731,3,2634],
      [6,0,156,1002.76,1039,3,1040],
      [2,2,92,551.88,1393,1,887]
    ]
    df_kitch = pd.DataFrame(raw_data, columns=['code','type','duration','function_points',
                                               'estimate','estimate_method','effort'])
    return df_kitch

def data_maxwell():
    raw_data = loadarff("./data/maxwell.arff")
    df_data = pd.DataFrame(raw_data[0])
    new_maxw = df_data.drop(columns=['Syear'])
    return new_maxw

def data_miyazaki():
    raw_data = loadarff("./data/miyazaki94.arff")
    df_data = pd.DataFrame(raw_data[0])
    new_miya = df_data.drop(columns=['ID'])
    return new_miya

def latex_print_detail(func):
    s = func.__name__[5:]
    df = func()
    attr_nums = len(df.columns)
    print('\multirow{%d}{*}{\\begin{sideways}%s\\end{sideways}\\begin{sideways}(%d)\\end{sideways}}' % (
    attr_nums, s, df.shape[0]))
    for attr in df.columns:
        name = attr
        m = df[attr].min()
        M = df[attr].max()
        avg = df[attr].mean()
        sd = df[attr].std()
        print("& %s & %.0f & %.0f & %.1f & %.1f\\\\" % (name, m, M, avg, sd))
    print('\\hline')

def latex_print_all_details():
    latex_print_detail(data_albrecht)
    latex_print_detail(data_desharnais)
    latex_print_detail(data_finnish)
    latex_print_detail(data_kemerer)
    latex_print_detail(data_maxwell)
    latex_print_detail(data_miyazaki)
    latex_print_detail(data_china)
    latex_print_detail(data_isbsg10)
    latex_print_detail(data_kitchenham)

if __name__ == '__main__':
    latex_print_all_details()
    # print(data_china().shape)
    # latex_print_detail(data_albrecht)

reason: from scipy


text:
import logging
from PIL import Image, ImageDraw
import io
import numpy as np
from tensorflow import make_tensor_proto, make_ndarray
import cv2
import inferencing_pb2
import media_pb2
import extension_pb2
import os
import ovms
import time

from cascade.voe_to_ovms import load_voe_config_from_json, voe_config_to_ovms_config

import threading

def process_voe_config(processor):
    while True:
        if os.path.exists('/workspace/voe_config.json'):
            metadatas_json = open('/workspace/voe_config.json').read()
            if metadatas_json != processor.metadatas_json:
                print('Updating Metadatas...')
                voe_config = load_voe_config_from_json(metadatas_json)
                _, metadatas = voe_config_to_ovms_config(voe_config)

                processor.metadatas = metadatas
                processor.metadatas_json = metadatas_json

        time.sleep(3)

def process_response(response, img, metadatas):
    predictions = []
    if response is not None:
        coordinates = make_ndarray(response.outputs['coordinates'])
        confidences = make_ndarray(response.outputs['confidences'])
        attributes = []

        for k in response.outputs:
            if (metadatas is not None) and (k in metadatas):
                #print(k)
                #print(metadatas[k])
                metadata = metadatas[k]
                if metadata['type'] == 'classification':
                    ndarray = make_ndarray(response.outputs[k])
                    tag_indexes = np.argmax(ndarray, axis=2).flatten()
                    tags = list(metadata['labels'][tag_index]
                                for tag_index in tag_indexes)
                    confidences = np.max(ndarray, axis=2).flatten()
                    attributes.append({
                        'name': k,
                        'type': 'classification',
                        'values': tags,
                        'confidences': confidences
                    })
                if metadata['type'] == 'regression':
                    ndarray = make_ndarray(response.outputs[k])
                    scores = ndarray
                    if 'scale' in metadata:
                        scores *= metadata['scale']
                    scores = scores.flatten().astype('int').tolist()
                    attributes.append({
                        'name': k,
                        'type': 'regression',
                        'values': scores
                    })

        n = coordinates.shape[0]
        predictions = []
        for i in range(n):
            x1, y1, x2, y2 = coordinates[i, 0]
            prediction = {
                'tag': 'face',
                'attributes': [],
                'confidence': confidences[i],
                'box': {
                    'l': x1,
                    't': y1,
                    'w': x2-x1,
                    'h': y2-y1
                }
            }
            print(attributes, flush=True)
            for attribute in attributes:
                if attribute['type'] == 'regression':
                    prediction['attributes'].append({
                        'name': attribute['name'],
                        'value': str(attribute['values'][i]),
                        'confidence': -1})
                if attribute['type'] == 'classification':
                    prediction['attributes'].append({
                        'name': attribute['name'],
                        'value': str(attribute['values'][i]),
                        'confidence': attribute['confidences'][i]})
            prediction['attributes'].sort(key=lambda x:x['name'])
            predictions.append(prediction)

    # followings is for drawing
    #h, w, _ = img.shape
    #for prediction in predictions:
    #    x1 = int(prediction['box']['l'] * w)
    #    y1 = int(prediction['box']['t'] * h)
    #    x2 = int((prediction['box']['w'] + prediction['box']['l']) * w)
    #    y2 = int((prediction['box']['h'] + prediction['box']['t']) * h)
    #    cv2.rectangle(img, (x1, y1), (x2, y2), (0, 255, 255), 3)
    #    font = cv2.FONT_HERSHEY_SIMPLEX
    #    fontScale = 0.5
    #    color = (0, 255, 255)
    #    thickness = 1
    #    text = prediction['tag']
    #    for attribute in prediction['attributes']:
    #        text += ' / ' + str(attribute['value'])
    #    cv2.putText(img, text, (x1, y1-10), font,
    #                fontScale, color, thickness, cv2.LINE_AA)
    return img, predictions

class OVMSBatchImageProcessor():
    def __init__(self):
        self.stub = None
        self.metadatas = None
        self.metadatas_json = ''
        self.th = threading.Thread(target=process_voe_config, args=(self,))
        self.th.start()

    def process_images(self, mediaStreamMessage, rawBytes, size):

        #FIXME
        if self.stub is None:
            self.stub = ovms.connect_ovms('ovmsserver:9001')

        # Read image raw bytes
        im = Image.frombytes('RGB', size, rawBytes.tobytes())
        #predictions = OVMS.predict

        img = np.asarray(im)
        img = img.astype(np.float32)  # BGR color format, shape HWC

        img = cv2.resize(img, (416, 416))
        img_tensor = img.reshape(1, 416, 416, 3)

        #predictions = [{'tag': 'aa', 'confidence': 0.5}]
        response = ovms.predict(self.stub, img_tensor)
        #print('1', flush=True)
        img, predictions = process_response(response, img, self.metadatas)
        #print('2', flush=True)

        for prediction in predictions:
            inference = mediaStreamMessage.media_sample.inferences.add()
            inference.type = inferencing_pb2.Inference.InferenceType.ENTITY

            attributes = []
            #print('3', flush=True)
            for attribute in prediction['attributes']:
                attributes.append(inferencing_pb2.Attribute(name=attribute['name'], value=attribute['value'], confidence=attribute['confidence']))

            #print('4', flush=True)
            inference.entity.CopyFrom(
                inferencing_pb2.Entity(
                    tag=inferencing_pb2.Tag(
                        value=prediction['tag'],
                        confidence=prediction['confidence']
                    ),
                    box=inferencing_pb2.Rectangle(
                        l=prediction['box']['l'],
                        t=prediction['box']['t'],
                        w=prediction['box']['w'],
                        h=prediction['box']['h']
                    ),
                    attributes=attributes
                )
            )
        return mediaStreamMessage