text:
import pytest
import shutil
import tempfile

import numpy as np

from pathlib import Path
from minus80 import Project
from minus80.Freezable import FreezableAPI

from minus80.Exceptions import (
    TagDoesNotExistError,
    TagExistsError,
    UnsavedChangesInThawedError,
)

@pytest.fixture
def simpleProject(scope="module"):
    tmpdir = tempfile.TemporaryDirectory()
    x = Project("simpleProject")
    x.create_link(Path(tmpdir.name) / "tmp")
    yield x
    FreezableAPI.delete(x.m80.dtype, x.m80.name)

def test_create_link_path_exists(simpleProject):
    tmpdir = tempfile.TemporaryDirectory()
    with pytest.raises(ValueError):
        simpleProject.create_link(tmpdir.name)

def test_init_project(simpleProject):
    assert simpleProject

def test_slug(simpleProject):
    assert simpleProject.m80.slug == "MINUS80.Project.simpleProject"

def test_thawed_dir(simpleProject):
    assert (simpleProject.m80.thawed_dir).exists

def test_frozen_dir(simpleProject):
    assert (simpleProject.m80.frozen_dir).exists

# Create a bunch of tags
def test_create_tag_v1(simpleProject):
    # copy a file into the project dir
    shutil.copyfile(
        src="data/Sample1_ATGTCA_L007_R1_001.fastq",
        dst=simpleProject.m80.thawed_dir / "Sample1_ATGTCA_L007_R1_001.fastq",
    )
    # The parent tag shouldnt exists until after the first freeze
    with pytest.raises(TagDoesNotExistError):
        simpleProject.m80.parent_tag
    assert simpleProject
    simpleProject.m80.freeze("v1")
    assert "v1" in simpleProject.m80.tags

def test_duplicate_tag(simpleProject):
    # try to create a duplicate tag
    simpleProject.m80.freeze("v1")
    with pytest.raises(TagExistsError):
        simpleProject.m80.freeze("v1")

def test_create_tag_v2(simpleProject):
    # Create v1 tag
    shutil.copyfile(
        src="data/Sample1_ATGTCA_L007_R1_001.fastq",
        dst=simpleProject.m80.thawed_dir / "Sample1_ATGTCA_L007_R1_001.fastq",
    )
    simpleProject.m80.freeze("v1")
    # create v2 tag
    shutil.copyfile(
        src="data/Sample1_ATGTCA_L007_R2_001.fastq",
        dst=simpleProject.m80.thawed_dir / "Sample1_ATGTCA_L007_R2_001.fastq",
    )
    simpleProject.m80.freeze("v2")
    assert "v2" in simpleProject.m80.tags
    # check the the thawed tag is equal to the parent tag
    assert simpleProject.m80.thawed_tag["parent"] == simpleProject.m80.parent_tag["tag"]

    # Thaw back to v1
    simpleProject.m80.thaw("v1")
    assert "Sample1_ATGTCA_L007_R1_001.fastq" in simpleProject.m80.checksum["files"]
    assert "Sample1_ATGTCA_L007_R2_001.fastq" not in simpleProject.m80.checksum["files"]

def test_thaw_nonexistant(simpleProject):
    with pytest.raises(TagDoesNotExistError):
        simpleProject.m80.thaw("nope")

def test_thaw_with_unsaved_changes(simpleProject):
    # Create v1 tag
    shutil.copyfile(
        src="data/Sample1_ATGTCA_L007_R1_001.fastq",
        dst=simpleProject.m80.thawed_dir / "Sample1_ATGTCA_L007_R1_001.fastq",
    )
    simpleProject.m80.freeze("v1")
    # create v2 tag
    shutil.copyfile(
        src="data/Sample1_ATGTCA_L007_R2_001.fastq",
        dst=simpleProject.m80.thawed_dir / "Sample1_ATGTCA_L007_R2_001.fastq",
    )
    simpleProject.m80.freeze("v2")
    # Delete the file
    import os

    # remove a file
    os.remove(simpleProject.m80.thawed_dir / "Sample1_ATGTCA_L007_R2_001.fastq")
    # add a new file
    shutil.copyfile(
        src="data/Sample2_ATGTCA_L005_R1_001.fastq",
        dst=simpleProject.m80.thawed_dir / "Sample2_ATGTCA_L005_R1_001.fastq",
    )
    # change a file
    simpleProject.m80.col["x"] = np.array([1, 2, 3, 4])
    with pytest.raises(UnsavedChangesInThawedError):
        simpleProject.m80.thaw("v1")


text:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""

Test the pool functionality.

@author: David P. Fleming [University of Washington, Seattle], 2018
@email: dflemin3 (at) uw (dot) edu

"""

import numpy as np
from approxposterior.pool import Pool

def _test_function(x):
    """
    Wastes a random amount of time, then
    returns the average of :py:obj:`x`.
    """

    for i in range(np.random.randint(99999)):
        j = i ** 2

    return np.sum(x) / float(len(x))

def test_pool():
    """
    Test the MultiPool functionality.

    Parameters
    ----------

    Returns
    -------
    """

    # Instantiate the pool
    with Pool(pool="MultiPool") as pool:

        # The iterable we'll apply ``_test_function`` to
        walkers = np.array([[i, i] for i in range(100)], dtype = 'float64')

        # Use the pool to map ``walkers`` onto the function
        res = pool.map(_test_function, walkers)

        # Check if the parallelization worked
        assert np.allclose(res, [_test_function(w) for w in walkers])
# end function

ext: py

max_stars_repo_name: jedbrown/approxposterior

max_issues_repo_name: jedbrown/approxposterior

max_forks_repo_name: jedbrown/approxposterior

reason: import numpy


text:
import random

import json
import pytest
import numpy as np
from pysharpen.methods import LinearBrightnessScale
SIZE = 7
# =================== utils for generation of the test and gt data ========================#

# Get the random test data. Must work with any data
def generate_data(dtype=np.uint8, channels=3, m=10, M=200):
    if np.issubdtype(dtype, np.integer):
        m = int(m)
        M = int(M)
        return np.random.randint(m, M, (10*SIZE, 10*SIZE), dtype),\
           np.random.randint(m, M, (channels, 10*SIZE, 10*SIZE), dtype)
    else:
        return (np.random.sample((10*SIZE, 10*SIZE))*(M-m) + m).astype(dtype),\
           (np.random.sample((channels, 10*SIZE, 10*SIZE))*(M-m) + m).astype(dtype)

def generate_batch():
    return [generate_data(np.uint8, channels=1),
     generate_data(np.uint8, channels=5, m=50, M=60),
     generate_data(np.uint16, channels=3, m=0, M=4500),
     generate_data(np.int32, channels=3, m=-50, M=4500),
     generate_data(np.float32, channels=4, m=0, M=1),
     generate_data(np.float32, channels=2, m=10., M=1000.)]

# Get the image statistics from the whole image to compare with tile-based variant
def get_setup_parameters(pan, ms, pan_separate=False, per_channel=False):

    if not pan_separate and not per_channel:
        all_img = np.concatenate([ms, np.expand_dims(pan, 0)], axis=0)
        total_min = all_img.min()
        total_max = all_img.max()
        total_mean = all_img.mean()
        total_std = all_img.std()
        return total_min, total_max, total_mean, total_std, total_min, total_max, total_mean, total_std
    #
    else:
        pan_min = pan.min()
        pan_max = pan.max()
        pan_std = pan.std()
        pan_mean = pan.mean()
        if not per_channel:
            ms_min = ms.min()
            ms_max = ms.max()
            ms_std = ms.std()
            ms_mean = ms.mean()
        else:
            ms_min = ms.min(axis=(1, 2))
            ms_max = ms.max(axis=(1, 2))
            ms_std = ms.std(axis=(1, 2))
            ms_mean = ms.mean(axis=(1, 2))
        return pan_min, pan_max, pan_mean, pan_std, ms_min, ms_max, ms_mean, ms_std

# Test of the patch-based functions
def test_meanstd_functional():

    test_data = generate_batch()

    for data in test_data:
        for img in data:
            if img.shape[-2:] != (10 * SIZE, 10 * SIZE):
                raise ValueError('Wrong size')
            gt_mean = img.mean()
            gt_std = img.std()
            means = []
            stds = []
            nums = []
            mes = []
            for i in range(10):
                for j in range(10):
                    if img.ndim == 2:
                        mean, std, num, me = LinearBrightnessScale._meanstdme(img[i * SIZE:(i + 1) * SIZE, j * SIZE:(j + 1) * SIZE])
                    else:
                        mean, std, num, me = LinearBrightnessScale._meanstdme(img[:, i * SIZE:(i + 1) * SIZE, j * SIZE:(j + 1) * SIZE])
                    means.append(mean)
                    stds.append(std)
                    nums.append(num)
                    mes.append(me)

            patch_mean, patch_std = LinearBrightnessScale._totalmeanstd(means, stds, nums, mes)

            assert np.allclose(patch_mean, gt_mean) and np.allclose(patch_std, gt_std)

# Processing of the image as a whole to compare with the tile-based approach
def linear_brightness_scale_whole(pan, ms, method,
                 per_channel=False, pan_separate=False, width=3, hist_cut=0.01, dtype=np.uint8):
    pan_min, pan_max, pan_mean, pan_std, ms_min, ms_max, ms_mean, ms_std = get_setup_parameters(pan, ms,
                                                                                pan_separate, per_channel)
    #print(pan_min, pan_max, pan_mean, pan_std, ms_min, ms_max, ms_mean, ms_std)

    if method == 'meanstd':
        axis=0 if per_channel else None
        pan_min = max(pan_min, pan_mean - width*pan_std)
        ms_min = np.max([ms_min, ms_mean - width * ms_std], axis=axis)
        pan_max = min(pan_max, pan_mean + width*pan_std)
        ms_max = np.min([ms_max, ms_mean + width * ms_std], axis=axis)

    elif method == 'histogram':
        # TODO: add test
        return pan, ms

    # MINMAX does not require any pre-setup so it goes right from here
    if np.issubdtype(dtype, np.integer):
        pan_res = (np.floor_divide(np.multiply((pan - pan_min),
                                               255,
                                               dtype=np.float64),
                                   (pan_max - pan_min))).clip(0, 255)
        if per_channel:
            ms_res = np.zeros_like(ms)
            for channel in range(ms.shape[0]):
                ms_res[channel] = (np.floor_divide(np.multiply((ms[channel] - ms_min[channel]),
                                                               255,
                                                               dtype=np.float64)
                                                   , (ms_max[channel] - ms_min[channel]))).clip(0, 255)
        else:
            ms_res = (np.floor_divide(np.multiply((ms - ms_min),
                                                  255,
                                                  dtype=np.float64),
                                      (ms_max - ms_min))).clip(0, 255)

    else:
        pan_res = (np.divide(np.multiply((pan - pan_min),
                                         255,
                                         dtype=np.float64),
                             (pan_max - pan_min))).clip(0, 255)
        if per_channel:
            ms_res = np.zeros_like(ms)
            for channel in range(ms.shape[0]):
                ms_res[channel] = (np.divide(np.multiply((ms[channel] - ms_min[channel]),
                                                         255,
                                                         dtype=np.float64),
                                             (ms_max[channel] - ms_min[channel]))).clip(0, 255)
        else:
            ms_res = (np.divide((ms - ms_min) * 255, (ms_max - ms_min))).clip(0, 255)

    return pan_res.astype(dtype), ms_res.astype(dtype)

# ============================ TEST CASES ===========================#
# Test that the initialization fails on incorrect params

def test_initialization_params_are_checked():

    # Test that it initializes with a set of correct parameters
    for method in ('minmax', 'meanstd', 'histogram'):
        for per_channel in (True, False):
            for pan_separate in (True, False):
                for width in (0.1, 1, 1.4, 3, 10):
                    for hist_cut in (0, 0.0001, 0.5, 0.99):
                        LinearBrightnessScale(method=method, per_channel=per_channel, pan_separate=pan_separate,
                                              std_width=width, hist_cut=hist_cut)

    # Check that exception is raised on incorrect parameters
    with pytest.raises(ValueError) as e_info:
        LinearBrightnessScale(method='WAT')
    with pytest.raises(ValueError) as e_info:
        LinearBrightnessScale(method=1)

    with pytest.raises(ValueError) as e_info:
        LinearBrightnessScale(std_width=-1)
    with pytest.raises(ValueError) as e_info:
        LinearBrightnessScale(std_width=0)
    with pytest.raises(ValueError) as e_info:
        LinearBrightnessScale(std_width='WAT')

    with pytest.raises(ValueError) as e_info:
        LinearBrightnessScale(hist_cut=1)
    with pytest.raises(ValueError) as e_info:
        LinearBrightnessScale(hist_cut=1.4)
    with pytest.raises(ValueError) as e_info:
        LinearBrightnessScale(hist_cut=-0.4)
    with pytest.raises(ValueError) as e_info:
        LinearBrightnessScale(hist_cut='WAT')

def test_properties():
    assert LinearBrightnessScale().processing_bound == 0
    assert LinearBrightnessScale().setup_bound == 0

# =========== Test of preparation function =================#
# Here we ensure that the tile-based preparation functions within our class work the same as a whole-image ones #

def test_preparation_minmax():
    for (pan, ms) in generate_batch():
        for pan_separate in [True, False]:
            for per_channel in [True, False]:
                pan_min, pan_max, pan_mean, pan_std, ms_min, ms_max, ms_mean, ms_std = get_setup_parameters(pan,
                                                                                ms,
                                                                                pan_separate,
                                                                                per_channel)

                method = LinearBrightnessScale('minmax', per_channel=per_channel, pan_separate=pan_separate)
                for i in range(10):
                    for j in range(10):
                        method.setup_from_patch(pan[i*SIZE:(i+1)*SIZE, j*SIZE:(j+1)*SIZE],
                                                        ms[:, i*SIZE:(i+1)*SIZE, j*SIZE:(j+1)*SIZE])
                #print(method._ms_maxs, method._ms_mins, method._pan_maxs, method._pan_mins)
                method.finalize_setup()
                assert pan_min == pytest.approx(method._pan_min_value)
                assert pan_max == pytest.approx(method._pan_max_value)
                assert np.allclose(ms_min, method._ms_min_value)
                assert np.allclose(ms_max, method._ms_max_value)

def test_preparation_meanstd():
    pan, ms = generate_data()
    print('Pan: ', pan.shape, pan.dtype)
    print('MS: ', ms.shape, ms.dtype)
    for pan_separate in [True, False]:
        for per_channel in [True, False]:
            # different widths to check. Actually,
            for width in (0.5, 1, 2, 10):
                pan_min, pan_max, pan_mean, pan_std, ms_min, ms_max, ms_mean, ms_std = get_setup_parameters(pan,
                                                                                ms,
                                                                                pan_separate,
                                                                                per_channel)

                method = LinearBrightnessScale('meanstd',
                                               per_channel=per_channel, pan_separate=pan_separate,
                                               std_width=width)
                for i in range(10):
                    for j in range(10):
                        method.setup_from_patch(pan[i * SIZE:(i + 1) * SIZE, j * SIZE:(j + 1) * SIZE],
                                                ms[:, i * SIZE:(i + 1) * SIZE, j * SIZE:(j + 1) * SIZE])

                #print(method._ms_means, method._ms_stds)
                #print(method._pan_means, method._pan_stds)
                #print(pan_min, pan_max, pan_mean, pan_std, ms_min, ms_max, ms_mean, ms_std)
                method.finalize_setup()

                assert np.max([pan_mean - width*pan_std, pan_min]) == pytest.approx(method._pan_min_value)
                assert np.min([pan_mean + width*pan_std, pan_max]) == pytest.approx(method._pan_max_value)
                assert np.allclose(np.max([ms_mean - width*ms_std, ms_min], axis = 0 if per_channel else None), (method._ms_min_value))
                assert np.allclose(np.min([ms_mean + width*ms_std, ms_max], axis = 0 if per_channel else None), (method._ms_max_value))

def test_preparation_historgram():
    pass

# ==================== Test of processing ==================== #

# Test all the methods at different data
def test_processing_minmax():
    random.seed(42)

    test_data = generate_batch()

    for img_num, data in enumerate(test_data):
        pan = data[0]
        ms = data[1]

        # MINMAX
        for pan_separate in [True, False]:
            for per_channel in [True, False]:
                pan_gt, ms_gt = linear_brightness_scale_whole(pan, ms, method='minmax', pan_separate=pan_separate,
                                                              per_channel=per_channel)
                pan_gt_float, ms_gt_float = linear_brightness_scale_whole(pan, ms, 'minmax', per_channel, pan_separate, dtype=np.float64)
                method = LinearBrightnessScale('minmax', dtype=np.uint8,
                                               per_channel=per_channel, pan_separate=pan_separate)
                for i in range(10):
                    for j in range(10):
                        method.setup_from_patch(pan[i * SIZE:(i + 1) * SIZE, j * SIZE:(j + 1) * SIZE],
                                                ms[:, i * SIZE:(i + 1) * SIZE, j * SIZE:(j + 1) * SIZE])
                method.finalize_setup()
                for i in range(10):
                    for j in range(10):
                        pan_res, ms_res = method.process(pan[i * SIZE:(i + 1) * SIZE, j * SIZE:(j + 1) * SIZE],
                                                ms[:, i * SIZE:(i + 1) * SIZE, j * SIZE:(j + 1) * SIZE])

                        # We test every output patch to be equal to the according patch of the processed as whole image
                        assert np.allclose(pan_res, pan_gt[i * SIZE:(i + 1) * SIZE, j * SIZE:(j + 1) * SIZE], atol=1)
                        """
                        res = True
                        for col in range(SIZE):
                            for row in range(SIZE):
                                if pan_res[row, col] != pan_gt[row+i*SIZE, col + j*SIZE]:
                                    print(row, col, pan[row+i*SIZE, col + j*SIZE], pan_res[row, col],
                                          pan_gt[row+i*SIZE, col + j*SIZE], pan_gt_float[row+i*SIZE, col + j*SIZE])
                                    print(json.dumps(float(method._pan_min_value)), json.dumps(float(method._pan_max_value)))
                                    print(json.dumps(float((pan.min()))), json.dumps(float(pan.max())))
                                    print(((pan[row+i*SIZE, col + j*SIZE] - pan.min())*255/(pan.max()-pan.min())))
                                    alt = np.floor_divide(np.multiply((pan[row+i*SIZE, col + j*SIZE] - pan.min()),
                                                          255,
                                                          dtype=np.float64),
                                        (pan.max() - pan.min()))
                                    alt = np.clip(alt, 0,255).astype(np.uint8)
                                    print(alt)
                                    res = False
                        assert res
                        """
                        assert np.allclose(ms_res, ms_gt[:, i * SIZE:(i + 1) * SIZE, j * SIZE:(j + 1) * SIZE], atol=1)

def test_processing_meanstd():
    test_data = [generate_data(np.uint8, channels=1),
                 generate_data(np.uint8, channels=5, m=50, M=60),
                 generate_data(np.uint16, channels=3, m=0, M=4500),
                 generate_data(np.int32, channels=3, m=-50, M=4500),
                 generate_data(np.float32, channels=4, m=0, M=1),
                 generate_data(np.float32, channels=2, m=10., M=1000.)]

    for data in test_data:
        pan = data[0]
        ms = data[1]
        print('Pan: ', pan.shape, pan.dtype)
        print('MS: ', ms.shape, ms.dtype)

        # MEANSTD
        for pan_separate in [True, False]:
            for per_channel in [True, False]:
                # different widths to check. Actually,
                for width in (0.5, 1, 2, 10):
                    pan_gt, ms_gt = linear_brightness_scale_whole(pan, ms, method='meanstd', pan_separate=pan_separate,
                                                                  per_channel=per_channel, width=width)

                    method = LinearBrightnessScale('meanstd', dtype=np.uint8,
                                                   per_channel=per_channel, pan_separate=pan_separate,
                                                   std_width=width)
                    for i in range(10):
                        for j in range(10):
                            method.setup_from_patch(pan[i * SIZE:(i + 1) * SIZE, j * SIZE:(j + 1) * SIZE],
                                                    ms[:, i * SIZE:(i + 1) * SIZE, j * SIZE:(j + 1) * SIZE])
                    method.finalize_setup()

                    for i in range(10):
                        for j in range(10):
                            pan_res, ms_res = method.process(pan[i * SIZE:(i + 1) * SIZE, j * SIZE:(j + 1) * SIZE],
                                                             ms[:, i * SIZE:(i + 1) * SIZE, j * SIZE:(j + 1) * SIZE])

                            # We test every output patch to be equal to the according patch of the processed as whole image
                            assert np.allclose(pan_res, pan_gt[i * SIZE:(i + 1) * SIZE, j * SIZE:(j + 1) * SIZE], atol=1)
                            assert np.allclose(ms_res, ms_gt[:, i * SIZE:(i + 1) * SIZE, j * SIZE:(j + 1) * SIZE], atol=1)

max_stars_repo_name: Vladimir-Kozub/pansharpen

max_issues_repo_name: Vladimir-Kozub/pansharpen

max_forks_repo_name: Vladimir-Kozub/pansharpen


text:
# -*-coding:utf-8-*-
# -*-coding:utf-8-*-
import numpy as np
def default_e(obs_n,action_n) :
    pos = [0, 0, 0]
    pos[0] = obs_n[1][3] #delta_x
    pos[1] = obs_n[1][4] #delta_y
    pos[2] = obs_n[1][2] #欧拉角
    ct = np.sqrt(np.square(pos[0]) + np.square((pos[1])))
    cost = pos[0] / ct
    theta = np.arccos(cost)  #相对位置夹角
    if pos[1] > 0:
        theta = -theta
    elif pos[1] <= 0:
        theta = theta
    pos[2] = theta - pos[2]
    if 0<pos[2] < 3.14/2:
        pos[2] -= 3.14-theta
    elif 0>pos[2] > -3.14/2:
        pos[2] += 3.14+theta
    # if pos[2] < 0:
    #     pos[2] = -1
    # if pos[2] > 0:
    #     pos[2] = 1
    # if pos[2] == 0:
    #     pos[2] = 0
    if ct<5:
        action_n[1] = [-pos[2]]
    return action_n[1]


text:
# Copyright 2021 Huawei Technologies Co., Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""eval the mxbase infer result via python"""
import os
import sys
import numpy as np

from util.data_preprocess import SingleScaleTrans
from util.eval import gen_eval_result, eval_according_output
from util.eval_util import prepare_file_paths, get_data

def run_pipeline(dataset_path, mxbase_result_path):
    """
        enable comparison graph output
        :param: the path of dataset, the path of mxbase result
        :returns: null
        Output: the figure of accuracy
    """

    # record the eval times
    eval_times = 0
    # record the infer result
    det = {}
    # record the image size
    img_size = {}
    # record the image label
    img_anno = {}

    # loop through the image, start to output the results, and evaluate the accuracy
    print('=============FaceDetection start evaluating==================')
    image_files, anno_files, image_names = prepare_file_paths(dataset_path)
    dataset_size = len(anno_files)
    assert dataset_size == len(image_files)
    assert dataset_size == len(image_names)
    data_set = []
    for i in range(dataset_size):
        data_set.append(get_data(image_files[i], anno_files[i], image_names[i]))
    for data in data_set:
        single_trans = SingleScaleTrans()
        pre_data = single_trans.deal_ann_size_name_to_array(data['annotation'], data['image_name'], data['image_size'])
        labels, image_name, image_size = pre_data[0:3]
        # Judge the input picture whether is a jpg format

        # bin files directory for storing inference results
        file_path = os.path.join(os.path.join(mxbase_result_path, image_name[0]), 'output')
        # take 6 outcomes from the bin file: coords_0, cls_scores_0, coords_1, cls_scores_1, coords_2, cls_scores_2
        coords_0 = np.fromfile(file_path + '_0.bin', dtype=np.float32).reshape((1, 4, 84, 4))
        cls_scores_0 = np.fromfile(file_path + '_1.bin', dtype=np.float32).reshape((1, 4, 84))
        coords_1 = np.fromfile(file_path + '_2.bin', dtype=np.float32).reshape((1, 4, 336, 4))
        cls_scores_1 = np.fromfile(file_path + '_3.bin', dtype=np.float32).reshape((1, 4, 336))
        coords_2 = np.fromfile(file_path + '_4.bin', dtype=np.float32).reshape((1, 4, 1344, 4))
        cls_scores_2 = np.fromfile(file_path + '_5.bin', dtype=np.float32).reshape((1, 4, 1344))

        eval_according_output(coords_0, cls_scores_0, coords_1, cls_scores_1, coords_2, cls_scores_2, det,
                              img_anno, img_size, labels, image_name, image_size)
        eval_times += 1

    # generate the eval result
    gen_eval_result(eval_times, det, img_size, img_anno)

if __name__ == '__main__':
    arg_arr_input_output_path = []
    if len(sys.argv) != 3:
        print('Wrong parameter setting.')
        exit()
    run_pipeline(sys.argv[1], sys.argv[2])

max_stars_repo_name: mindspore-ai/models

max_issues_repo_name: mindspore-ai/models

max_forks_repo_name: mindspore-ai/models


text:
import pandas as pd
import numpy as np
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.metrics.pairwise import cosine_similarity
df = pd.read_csv("movie_dataset.csv")
features = ['keywords', 'cast', 'genres', 'director','vote_average','popularity']
def combine_features(row):
    return row['keywords'] + " "+row['cast']+" "+row["genres"]+" "+row["director"] +" "+str(row["vote_average"]) +" "+str(row["popularity"])
for feature in features:
    df[feature] = df[feature].fillna('')
df["combined_features"] = df.apply(combine_features, axis=1)
cv = CountVectorizer()
count_matrix = cv.fit_transform(df["combined_features"])
cosine_sim = cosine_similarity(count_matrix)
def get_title_from_index(index):
    return df[df.index == index]["title"].values[0]
def get_index_from_title(title):
    return df[df.title == title]["index"].values[0]
movie_user_likes = 'Interstellar'
movie_index = get_index_from_title(movie_user_likes)
similar_movies = list(enumerate(cosine_sim[movie_index]))
sorted_similar_movies = sorted(
    similar_movies, key=lambda x: x[1], reverse=True)[1:]
i = 0
print("Top 7 similar movies to "+movie_user_likes+" are:\n")
for element in sorted_similar_movies:
    print(get_title_from_index(element[0]))
    i = i+1
    if i >= 7:
        break


text:
# Licensed under an MIT open source license - see LICENSE

import pytest

import numpy as np
import numpy.testing as npt
import astropy.units as u
from astropy.wcs import WCS

from ..base_conversions import UnitConverter

from ._testing_data import hdr

def test_UnitConverter_nowcs():
    '''
    Only pixel to pixel conversions
    '''

    convert = UnitConverter()

    twopix = 2 * u.pix

    assert convert.to_pixel(twopix) == twopix
    assert convert.to_pixel_area(twopix**2) == twopix**2

    with pytest.raises(AttributeError):
        convert.ang_size

    with pytest.raises(AttributeError):
        convert.from_pixel(twopix, u.deg)

    with pytest.raises(AttributeError):
        convert.to_angular(twopix)

def test_UnitConverter_nodistance():

    mywcs = WCS(hdr)

    convert = UnitConverter(mywcs)

    assert convert.ang_size.value == np.abs(hdr['CDELT2'])
    assert convert.ang_size.unit == mywcs.wcs.cunit[0]

    twopix_ang = np.abs(hdr['CDELT2']) * 2 * u.deg
    assert convert.to_pixel(twopix_ang).value == 2.

    assert convert.to_pixel_area(twopix_ang**2) == 4 * u.pix**2

    assert convert.from_pixel(2 * u.pix, u.deg) == twopix_ang
    assert convert.to_angular(2 * u.pix, u.deg) == twopix_ang

    # Round trip
    assert convert.to_pixel(convert.from_pixel(2 * u.pix, u.deg)) == 2 * u.pix
    assert convert.to_pixel(convert.to_angular(2 * u.pix, u.deg)) == 2 * u.pix

    assert convert.from_pixel(convert.to_pixel(twopix_ang), u.deg) == twopix_ang
    assert convert.to_angular(convert.to_pixel(twopix_ang), u.deg) == twopix_ang

    # Physical conversions should fail
    with pytest.raises(AttributeError):
        convert.from_pixel(2 * u.pix, u.pc)
    with pytest.raises(AttributeError):
        assert convert.to_physical(2 * u.pix, u.pc)

def test_UnitConverter():

    mywcs = WCS(hdr)

    distance = 250 * u.pc

    convert = UnitConverter(mywcs, distance=distance)

    assert convert.ang_size.value == np.abs(hdr['CDELT2'])
    assert convert.ang_size.unit == mywcs.wcs.cunit[0]

    twopix_ang = np.abs(hdr['CDELT2']) * 2 * u.deg
    twopix_phys = twopix_ang.to(u.rad).value * distance
    assert convert.to_pixel(twopix_ang).value == 2.
    npt.assert_allclose(convert.to_pixel(twopix_phys).value, 2.)

    assert convert.to_pixel_area(twopix_ang**2) == 4 * u.pix**2
    npt.assert_allclose(convert.to_pixel_area(twopix_phys**2).value, 4.)

    assert convert.from_pixel(2 * u.pix, u.deg) == twopix_ang
    assert convert.to_angular(2 * u.pix, u.deg) == twopix_ang

    npt.assert_allclose(convert.from_pixel(2 * u.pix, u.pc), twopix_phys)
    npt.assert_allclose(convert.to_physical(2 * u.pix, u.pc), twopix_phys)

    # Round trip
    assert convert.to_pixel(convert.from_pixel(2 * u.pix, u.deg)) == 2 * u.pix
    assert convert.to_pixel(convert.to_angular(2 * u.pix, u.deg)) == 2 * u.pix

    assert convert.from_pixel(convert.to_pixel(twopix_ang), u.deg) == twopix_ang
    assert convert.to_angular(convert.to_pixel(twopix_ang), u.deg) == twopix_ang

    assert convert.to_pixel(convert.from_pixel(2 * u.pix, u.pc)) == 2 * u.pix
    assert convert.to_pixel(convert.to_physical(2 * u.pix, u.pc)) == 2 * u.pix

    assert convert.from_pixel(convert.to_pixel(twopix_phys), u.pc) == twopix_phys
    assert convert.to_physical(convert.to_pixel(twopix_phys), u.pc) == twopix_phys

@pytest.mark.xfail(raises=u.UnitConversionError)
def test_UnitConverter_bad_distance_unit():

    mywcs = WCS(hdr)

    distance = 250 * u.K

    UnitConverter(mywcs, distance=distance)

@pytest.mark.xfail(raises=TypeError)
def test_UnitConverter_bad_angular_unit():

    mywcs = WCS(hdr)

    convert = UnitConverter(mywcs)

    assert convert.to_pixel(np.abs(hdr['CDELT2']) * 2).value == 2.

reason: import numpy,import astropy,from astropy


text:
import torch
import torchvision.transforms as T
import numpy as np

'''
ImageNet normalize transform
'''
ImageNetNormalize = T.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])

def get_device():
    return torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')

def plot_confusion_matrix(
    predicted, target, label_names, filename='./cm.png',
    is_names=False, cmap='Blues', normalize='true',
):
    '''
    plot confusion matrix
    convert labels to their names if needed

    arg
        predicted: list of int or str
            predicted labels or names
        target: list of int or str
            target labels or names
        label_names: list of str
            list of names of labels
        filename: str (default: ./cm.png)
            filename for output picture of confusion matrix
        is_name: bool (default: False)
            if True, will skip index to name conversion
        cmap: str (default: 'Blues')
            color map of matplotlib
        normalize: str (default: 'true')
            'true' -> row, 'pred' -> column,
            'all'  -> all, None   -> no normalization
    '''

    import matplotlib
    matplotlib.use('agg')
    import matplotlib.pyplot as plt

    from sklearn.metrics import confusion_matrix, ConfusionMatrixDisplay

    if not is_names:
        predicted, target = label2name(
            predicted, target, label_names
        )

    predicted = np.array(predicted).reshape(-1, 1)
    target    = np.array(target).reshape(-1, 1)

    cm = confusion_matrix(
        predicted,
        target,
        labels=label_names,
        normalize=normalize
    )

    ConfusionMatrixDisplay(cm, display_labels=label_names).plot(cmap=cmap)
    plt.savefig(filename)

def label2name(
    predicted,
    target,
    label_names
):
    '''
    convert labels to target

    arg
        predicted: list of int
            predicted labels
        target: list of int
            target labels
        label_names: list of str
            list of names of labels
    '''

    predicted_names = []
    target_names    = []

    for p, t in zip(predicted, target):
        predicted_names.append(label_names[p])
        target_names.append(label_names[t])

    return predicted_names, target_names


text:
# encoding: utf-8
"""
credit:
https://github.com/facebookresearch/detectron2/blob/master/detectron2/engine/train_loop.py
"""

import logging
import time
import weakref
from typing import Dict

import numpy as np
import torch
from apex import amp
from apex.parallel import DistributedDataParallel

import fastreid.utils.comm as comm
from fastreid.utils.events import EventStorage, get_event_storage

__all__ = ["HookBase", "TrainerBase", "SimpleTrainer"]

logger = logging.getLogger(__name__)

class HookBase:
    """
    Base class for hooks that can be registered with :class:`TrainerBase`.
    Each hook can implement 6 methods. The way they are called is demonstrated
    in the following snippet:
    .. code-block:: python
        hook.before_train()
        for _ in range(start_epoch, max_epoch):
            hook.before_epoch()
            for iter in range(start_iter, max_iter):
                hook.before_step()
                trainer.run_step()
                hook.after_step()
            hook.after_epoch()
        hook.after_train()
    Notes:
        1. In the hook method, users can access `self.trainer` to access more
           properties about the context (e.g., current iteration).
        2. A hook that does something in :meth:`before_step` can often be
           implemented equivalently in :meth:`after_step`.
           If the hook takes non-trivial time, it is strongly recommended to
           implement the hook in :meth:`after_step` instead of :meth:`before_step`.
           The convention is that :meth:`before_step` should only take negligible time.
           Following this convention will allow hooks that do care about the difference
           between :meth:`before_step` and :meth:`after_step` (e.g., timer) to
           function properly.
    Attributes:
        trainer: A weak reference to the trainer object. Set by the trainer when the hook is
            registered.
    """

    def before_train(self):
        """
        Called before the first iteration.
        """
        pass

    def after_train(self):
        """
        Called after the last iteration.
        """
        pass

    def before_epoch(self):
        """
        Called before each epoch.
        """
        pass

    def after_epoch(self):
        """
        Called after each epoch.
        """
        pass

    def before_step(self):
        """
        Called before each iteration.
        """
        pass

    def after_step(self):
        """
        Called after each iteration.
        """
        pass

class TrainerBase:
    """
    Base class for iterative trainer with hooks.
    The only assumption we made here is: the training runs in a loop.
    A subclass can implement what the loop is.
    We made no assumptions about the existence of dataloader, optimizer, model, etc.
    Attributes:
        iter(int): the current iteration.
        start_iter(int): The iteration to start with.
            By convention the minimum possible value is 0.
        max_iter(int): The iteration to end training.
        storage(EventStorage): An EventStorage that's opened during the course of training.
    """

    def __init__(self):
        self._hooks = []

    def register_hooks(self, hooks):
        """
        Register hooks to the trainer. The hooks are executed in the order
        they are registered.
        Args:
            hooks (list[Optional[HookBase]]): list of hooks
        """
        hooks = [h for h in hooks if h is not None]
        for h in hooks:
            assert isinstance(h, HookBase)
            # To avoid circular reference, hooks and trainer cannot own each other.
            # This normally does not matter, but will cause memory leak if the
            # involved objects contain __del__:
            # See http://engineering.hearsaysocial.com/2013/06/16/circular-references-in-python/
            h.trainer = weakref.proxy(self)
        self._hooks.extend(hooks)

    def train(self, start_epoch: int, max_epoch: int, iters_per_epoch: int):
        """
        Args:
            start_iter, max_iter (int): See docs above
        """
        logger = logging.getLogger(__name__)
        logger.info("Starting training from epoch {}".format(start_epoch))

        self.iter = self.start_iter = start_epoch * iters_per_epoch

        with EventStorage(self.start_iter) as self.storage:
            try:
                self.before_train()
                for self.epoch in range(start_epoch, max_epoch):
                    self.before_epoch()
                    for _ in range(iters_per_epoch):
                        self.before_step()
                        self.run_step()
                        self.after_step()
                        self.iter += 1
                    self.after_epoch()
            except Exception:
                logger.exception("Exception during training:")
                raise
            finally:
                self.after_train()

    def before_train(self):
        for h in self._hooks:
            h.before_train()

    def after_train(self):
        self.storage.iter = self.iter
        for h in self._hooks:
            h.after_train()

    def before_epoch(self):
        self.storage.epoch = self.epoch

        for h in self._hooks:
            h.before_epoch()

    def before_step(self):
        self.storage.iter = self.iter

        for h in self._hooks:
            h.before_step()

    def after_step(self):
        for h in self._hooks:
            h.after_step()

    def after_epoch(self):
        for h in self._hooks:
            h.after_epoch()

    def run_step(self):
        raise NotImplementedError

class SimpleTrainer(TrainerBase):
    """
    A simple trainer for the most common type of task:
    single-cost single-optimizer single-data-source iterative optimization.
    It assumes that every step, you:
    1. Compute the loss with a data from the data_loader.
    2. Compute the gradients with the above loss.
    3. Update the model with the optimizer.
    If you want to do anything fancier than this,
    either subclass TrainerBase and implement your own `run_step`,
    or write your own training loop.
    """

    def __init__(self, model, data_loader, optimizer):
        """
        Args:
            model: a torch Module. Takes a data from data_loader and returns a
                dict of heads.
            data_loader: an iterable. Contains data to be used to call model.
            optimizer: a torch optimizer.
        """
        super().__init__()

        """
        We set the model to training mode in the trainer.
        However it's valid to train a model that's in eval mode.
        If you want your model (or a submodule of it) to behave
        like evaluation during training, you can overwrite its train() method.
        """
        model.train()

        self.model = model
        self.data_loader = data_loader
        self._data_loader_iter = iter(data_loader)
        self.optimizer = optimizer

    def run_step(self):
        """
        Implement the standard training logic described above.
        """
        assert self.model.training, "[SimpleTrainer] model was changed to eval mode!"
        start = time.perf_counter()
        """
        If your want to do something with the data, you can wrap the dataloader.
        """
        data = next(self._data_loader_iter)
        data_time = time.perf_counter() - start

        """
        If your want to do something with the heads, you can wrap the model.
        """

        outs = self.model(data)

        # Compute loss
        if isinstance(self.model, DistributedDataParallel):
            loss_dict = self.model.module.losses(outs)
        else:
            loss_dict = self.model.losses(outs)

        losses = sum(loss_dict.values())

        """
        If you need accumulate gradients or something similar, you can
        wrap the optimizer with your custom `zero_grad()` method.
        """
        self.optimizer.zero_grad()
        losses.backward()

        self._write_metrics(loss_dict, data_time)

        """
        If you need gradient clipping/scaling or other processing, you can
        wrap the optimizer with your custom `step()` method.
        """
        self.optimizer.step()

    def _write_metrics(self, loss_dict: Dict[str, torch.Tensor], data_time: float):
        """
        Args:
            loss_dict (dict): dict of scalar losses
            data_time (float): time taken by the dataloader iteration
        """
        device = next(iter(loss_dict.values())).device

        # Use a new stream so these ops don't wait for DDP or backward
        with torch.cuda.stream(torch.cuda.Stream() if device.type == "cuda" else None):
            metrics_dict = {k: v.detach().cpu().item() for k, v in loss_dict.items()}
            metrics_dict["data_time"] = data_time

            # Gather metrics among all workers for logging
            # This assumes we do DDP-style training, which is currently the only
            # supported method in detectron2.
            all_metrics_dict = comm.gather(metrics_dict)

        if comm.is_main_process():
            storage = get_event_storage()

            # data_time among workers can have high variance. The actual latency
            # caused by data_time is the maximum among workers.
            data_time = np.max([x.pop("data_time") for x in all_metrics_dict])
            storage.put_scalar("data_time", data_time)

            # average the rest metrics
            metrics_dict = {
                k: np.mean([x[k] for x in all_metrics_dict]) for k in all_metrics_dict[0].keys()
            }
            total_losses_reduced = sum(metrics_dict.values())
            if not np.isfinite(total_losses_reduced):
                raise FloatingPointError(
                    f"Loss became infinite or NaN at iteration={self.iter}!\n"
                    f"loss_dict = {metrics_dict}"
                )

            storage.put_scalar("total_loss", total_losses_reduced)
            if len(metrics_dict) > 1:
                storage.put_scalars(**metrics_dict)

class AMPTrainer(SimpleTrainer):
    """
    Like :class:`SimpleTrainer`, but uses apex automatic mixed precision
    in the training loop.
    """
    def run_step(self):
        """
        Implement the AMP training logic.
        """
        assert self.model.training, "[AMPTrainer] model was changed to eval mode!"
        assert torch.cuda.is_available(), "[AMPTrainer] CUDA is required for AMP training!"

        start = time.perf_counter()
        data = next(self._data_loader_iter)
        data_time = time.perf_counter() - start

        outs = self.model(data)

        # Compute loss
        if isinstance(self.model, DistributedDataParallel):
            loss_dict = self.model.module.losses(outs)
        else:
            loss_dict = self.model.losses(outs)

        losses = sum(loss_dict.values())

        self.optimizer.zero_grad()

        with amp.scale_loss(losses, self.optimizer) as scaled_loss:
            scaled_loss.backward()

        self._write_metrics(loss_dict, data_time)

        self.optimizer.step()

max_stars_repo_name: ckmessi/fast-reid

max_issues_repo_name: ckmessi/fast-reid

max_forks_repo_name: ckmessi/fast-reid


text:
import argparse
import cv2
from datetime import datetime
from PIL import Image
import numpy as np
import os
import time
import torch
import torch.backends.cudnn as cudnn
import traceback
from image_complete import auto
from yolact import Yolact
from data import cfg, set_cfg
from layers.output_utils import postprocess
from utils import timer
from utils.augmentations import FastBaseTransform
from wai.annotations.core import ImageInfo
from wai.annotations.roi import ROIObject
from wai.annotations.roi.io import ROIWriter
from wai.annotations.image_utils import mask_to_polygon, polygon_to_minrect, polygon_to_lists, lists_to_polygon, polygon_to_bbox

SUPPORTED_EXTS = [".jpg", ".jpeg", ".png", ".bmp"]
""" supported file extensions (lower case). """

MAX_INCOMPLETE = 3
""" the maximum number of times an image can return 'incomplete' status before getting moved/deleted. """

def predictions_to_rois(dets_out, width, height, top_k, score_threshold,
                        output_polygons, mask_threshold, mask_nth, output_minrect,
                        view_margin, fully_connected, fit_bbox_to_polygon, bbox_as_fallback,
                        scale, output_mask_image):
    """
    Turns the predictions into ROI objects
    :param dets_out: the predictions
    :param width: the width of the image
    :type width: int
    :param height: the height of the image
    :type height: int
    :param top_k: the maximum number of top predictions to use
    :type top_k: int
    :param score_threshold: the minimum score predictions have to have
    :type score_threshold: float
    :param output_polygons: whether the model predicts masks and polygons should be stored in the CSV files
    :type output_polygons: bool
    :param mask_threshold: the threshold to use for determining the contour of a mask
    :type mask_threshold: float
    :param mask_nth: to speed up polygon computation, use only every nth row and column from mask
    :type mask_nth: int
    :param output_minrect: when predicting polygons, whether to output the minimal rectangles around the objects as well
    :type output_minrect: bool
    :param view_margin: the margin in pixels to use around the masks
    :type view_margin: int
    :param fully_connected: whether regions of 'high' or 'low' values should be fully-connected at isthmuses
    :type fully_connected: str
    :param fit_bbox_to_polygon: whether to fit the bounding box to the polygon
    :type fit_bbox_to_polygon: bool
    :param bbox_as_fallback: if ratio between polygon-bbox and bbox is smaller than this value, use bbox as fallback polygon, ignored if < 0
    :type bbox_as_fallback: float
    :param scale: the scale to use for the image (0-1)
    :type scale: float
    :param output_mask_image: when generating masks, whether to output a combined mask image as well
    :type output_mask_image: bool
    :return: the list of ROIObjects and output_mask image
    :rtype: tuple
    """

    result = []
    mask_comb = None

    with timer.env('Postprocess'):
        save = cfg.rescore_bbox
        cfg.rescore_bbox = True
        t = postprocess(dets_out, width, height, crop_masks=False, score_threshold=score_threshold)
        cfg.rescore_bbox = save

    with timer.env('Copy'):
        idx = t[1].argsort(0, descending=True)[:top_k]
        if output_polygons or output_mask_image:
            classes, scores, boxes, masks = [x[idx].cpu().numpy() for x in t]
        else:
            classes, scores, boxes = [x[idx].cpu().numpy() for x in t[:3]]

    num_dets_to_consider = min(top_k, classes.shape[0])
    for j in range(num_dets_to_consider):
        if scores[j] < score_threshold:
            num_dets_to_consider = j
            break

    # the class labels
    if isinstance(cfg.dataset.class_names, list):
        class_labels = cfg.dataset.class_names
    elif isinstance(cfg.dataset.class_names, tuple):
        class_labels = list(cfg.dataset.class_names)
    else:
        class_labels = [cfg.dataset.class_names]

    if num_dets_to_consider > 0:
        # After this, mask is of size [num_dets, h, w, 1]
        if output_polygons or output_mask_image:
            masks = masks[:num_dets_to_consider, :, :, None]
            mask = masks[j,:,:][:,:,0]

        for j in range(num_dets_to_consider):
            x0, y0, x1, y1 = boxes[j, :]
            x0n = x0 / width
            y0n = y0 / height
            x1n = x1 / width
            y1n = y1 / height
            if scale != 1.0:
                x0 = int(x0 / scale)
                y0 = int(y0 / scale)
                x1 = int(x1 / scale)
                y1 = int(y1 / scale)
            label = classes[j]
            score = scores[j]
            label_str = class_labels[classes[j]]
            px = None
            py = None
            pxn = None
            pyn = None
            bw = None
            bh = None
            if output_polygons:
                px = []
                py = []
                pxn = []
                pyn = []
                poly = mask_to_polygon(mask, mask_threshold=mask_threshold, mask_nth=mask_nth, view=(int(x0 * scale), int(y0 * scale), int(x1 * scale), int(y1 * scale)), view_margin=view_margin, fully_connected=fully_connected)
                if len(poly) > 0:
                    px, py = polygon_to_lists(poly[0], swap_x_y=True, normalize=False)
                    if scale != 1.0:
                        px = [x / scale for x in px]
                        py = [y / scale for y in py]
                    pxn, pyn = polygon_to_lists(poly[0], swap_x_y=True, normalize=True, img_width=width, img_height=height)
                    if output_minrect:
                        bw, bh = polygon_to_minrect(poly[0])
                        if scale != 1.0:
                            bw = bw / scale
                            bh = bh / scale
                    if bbox_as_fallback >= 0:
                        if len(px) >= 3:
                            p_x0n, p_y0n, p_x1n, p_y1n = polygon_to_bbox(lists_to_polygon(pxn, pyn))
                            p_area = (p_x1n - p_x0n) * (p_y1n - p_y0n)
                            b_area = (x1n - x0n) * (y1n - y0n)
                            if (b_area > 0) and (p_area / b_area < bbox_as_fallback):
                                px = [float(i) for i in [x0, x1, x1, x0]]
                                py = [float(i) for i in [y0, y0, y1, y1]]
                                pxn = [float(i) for i in [x0n, x1n, x1n, x0n]]
                                pyn = [float(i) for i in [y0n, y0n, y1n, y1n]]
                        else:
                            px = [float(i) for i in [x0, x1, x1, x0]]
                            py = [float(i) for i in [y0, y0, y1, y1]]
                            pxn = [float(i) for i in [x0n, x1n, x1n, x0n]]
                            pyn = [float(i) for i in [y0n, y0n, y1n, y1n]]
                        if output_minrect:
                            bw = x1 - x0 + 1
                            bh = y1 - y0 + 1
                    if fit_bbox_to_polygon:
                        if len(px) >= 3:
                            x0, y0, x1, y1 = polygon_to_bbox(lists_to_polygon(px, py))
                            x0n, y0n, x1n, y1n = polygon_to_bbox(lists_to_polygon(pxn, pyn))

            if output_mask_image:
                mask_img = mask.copy()
                # apply threshold
                mask_img[mask_img < mask_threshold] = 0
                # mask out everything outside detected box
                m = np.zeros(mask.shape)
                s = np.ones((y1 - y0, x1 - x0))
                m[y0:y0+s.shape[0], x0:x0+s.shape[1]] = s
                mask_img = np.where(m == 1, mask_img, 0)
                # use label for color
                mask_img[mask_img < mask_threshold] = 0
                mask_img[mask_img >= mask_threshold] = label+1  # first label is 0
                if mask_comb is None:
                    mask_comb = mask_img
                else:
                    tmp = np.where(mask_comb==0, mask_img, mask_comb)
                    mask_comb = tmp

            roiobj = ROIObject(x0, y0, x1, y1, x0n, y0n, x1n, y1n, label, label_str, score=score,
                               poly_x=px, poly_y=py, poly_xn=pxn, poly_yn=pyn,
                               minrect_w=bw, minrect_h=bh)
            result.append(roiobj)

    return result, mask_comb

def predict_image(model, img_path, img=None, top_k=5, score_threshold=0.0,
            output_polygons=False, mask_threshold=0.1, mask_nth=1, output_minrect=False,
            view_margin=2, fully_connected='high', fit_bbox_to_polygon=False,
            bbox_as_fallback=False, scale=1.0, debayer_int=None, output_mask_image=False):
    """
    Detects objects in an image.

    :param img_path: the path to the image to detect objects in, ignored if img not None
    :type img_path: str
    :param img: the image to detect the objects in; uses img_path if None.
    :type img: ndarray
    :param model: the model to use for the predictions
    :type model: Yolact
    :param top_k: the top X predictions (= objects) to parse
    :type top_k: int
    :param score_threshold: the score threshold to use
    :type score_threshold: float
    :param output_polygons: whether the model predicts masks and polygons should be stored in the CSV files
    :type output_polygons: bool
    :param mask_threshold: the threshold to use for determining the contour of a mask
    :type mask_threshold: float
    :param mask_nth: to speed up polygon computation, use only every nth row and column from mask
    :type mask_nth: int
    :param output_minrect: when predicting polygons, whether to output the minimal rectangles around the objects as well
    :type output_minrect: bool
    :param view_margin: the margin in pixels to use around the masks
    :type view_margin: int
    :param fully_connected: whether regions of 'high' or 'low' values should be fully-connected at isthmuses
    :type fully_connected: str
    :param fit_bbox_to_polygon: whether to fit the bounding box to the polygon
    :type fit_bbox_to_polygon: bool
    :param bbox_as_fallback: if ratio between polygon-bbox and bbox is smaller than this value, use bbox as fallback polygon, ignored if < 0
    :type bbox_as_fallback: float
    :param scale: the scale to use for the image (0-1)
    :type scale: float
    :param debayer_int: the debayering constant (cv2.COLOR_BAYER_XYZ) or None if not to debayer
    :type debayer_int: int
    :param output_mask_image: when generating masks, whether to output a combined mask image as well
    :type output_mask_image: bool
    :return: tuple of ImageInfo, list of ROIObject instances and combined mask image
    :rtype: tuple
    """

    if img is None:
        # debayer image?
        if debayer_int is None:
            img = cv2.imread(img_path)
        else:
            raw = cv2.imread(img_path, cv2.IMREAD_GRAYSCALE | cv2.IMREAD_ANYDEPTH)
            img = cv2.cvtColor(raw, debayer_int)

    # scale image
    if scale != 1.0:
        img = cv2.resize(img, (0,0), fx=scale, fy=scale)

    height, width, _ = img.shape
    frame = torch.from_numpy(img).cuda().float()
    batch = FastBaseTransform()(frame.unsqueeze(0))
    preds = model(batch)
    roiobjs, mask_comb = predictions_to_rois(preds, width, height, top_k, score_threshold,
                                  output_polygons, mask_threshold, mask_nth, output_minrect,
                                  view_margin, fully_connected, fit_bbox_to_polygon, bbox_as_fallback,
                                  scale, output_mask_image)

    info = ImageInfo(os.path.basename(img_path))
    return info, roiobjs, mask_comb

def predict(model, input_dir, output_dir, tmp_dir=None, top_k=5, score_threshold=0.0, delete_input=False,
            output_polygons=False, mask_threshold=0.1, mask_nth=1, output_minrect=False,
            view_margin=2, fully_connected='high', fit_bbox_to_polygon=False, output_width_height=False,
            bbox_as_fallback=False, scale=1.0, debayer="", continuous=False, output_mask_image=False):
    """
    Loads the model/config and performs predictions.

    :param model: the model to use
    :type model: Yolact
    :param input_dir: the directory to check for images to use for prediction
    :type input_dir: str
    :param output_dir: the directory to store the results in (predictions and/or images)
    :type output_dir: str
    :param tmp_dir: the directory to store the results initially, before moving them into the actual output directory
    :type tmp_dir: str
    :param top_k: the top X predictions (= objects) to parse
    :type top_k: int
    :param score_threshold: the score threshold to use
    :type score_threshold: float
    :param delete_input: whether to delete the images from the input directory rather than moving them to the output directory
    :type delete_input: bool
    :param output_polygons: whether the model predicts masks and polygons should be stored in the CSV files
    :type output_polygons: bool
    :param mask_threshold: the threshold to use for determining the contour of a mask
    :type mask_threshold: float
    :param mask_nth: to speed up polygon computation, use only every nth row and column from mask
    :type mask_nth: int
    :param output_minrect: when predicting polygons, whether to output the minimal rectangles around the objects as well
    :type output_minrect: bool
    :param view_margin: the margin in pixels to use around the masks
    :type view_margin: int
    :param fully_connected: whether regions of 'high' or 'low' values should be fully-connected at isthmuses
    :type fully_connected: str
    :param fit_bbox_to_polygon: whether to fit the bounding box to the polygon
    :type fit_bbox_to_polygon: bool
    :param output_width_height: whether to output x/y/w/h instead of x0/y0/x1/y1
    :type output_width_height: bool
    :param bbox_as_fallback: if ratio between polygon-bbox and bbox is smaller than this value, use bbox as fallback polygon, ignored if < 0
    :type bbox_as_fallback: float
    :param scale: the scale to use for the image (0-1)
    :type scale: float
    :param debayer: the OpenCV2 debayering type to use, eg COLOR_BAYER_BG2BGR; ignored if empty string or None
    :type debayer: str
    :param continuous: whether to delete the images from the input directory rather than moving them to the output directory
    :type continuous: bool
    :param output_mask_image: when generating masks, whether to output a combined mask image as well
    :type output_mask_image: bool
    """

    # counter for keeping track of images that cannot be processed
    incomplete_counter = dict()
    num_imgs = 1

    # evaluate debayering constant
    debayer_int = None
    if (debayer is not None) and debayer.startswith("COLOR_BAYER_"):
        debayer_int = int(eval("cv2." + debayer))

    while True:
        start_time = datetime.now()
        im_list = []
        # Loop to pick up images equal to num_imgs or the remaining images if less
        for image_path in os.listdir(input_dir):
            # Load images only
            ext_lower = os.path.splitext(image_path)[1]
            if ext_lower in SUPPORTED_EXTS:
                full_path = os.path.join(input_dir, image_path)
                if auto.is_image_complete(full_path):
                    im_list.append(full_path)
                else:
                    if not full_path in incomplete_counter:
                        incomplete_counter[full_path] = 1
                    else:
                        incomplete_counter[full_path] = incomplete_counter[full_path] + 1

            # remove images that cannot be processed
            remove_from_blacklist = []
            for k in incomplete_counter:
                if incomplete_counter[k] == MAX_INCOMPLETE:
                    print("%s - %s" % (str(datetime.now()), os.path.basename(k)))
                    remove_from_blacklist.append(k)
                    try:
                        if delete_input:
                            print("  flagged as incomplete {} times, deleting\n".format(MAX_INCOMPLETE))
                            os.remove(k)
                        else:
                            print("  flagged as incomplete {} times, skipping\n".format(MAX_INCOMPLETE))
                            os.rename(k, os.path.join(output_dir, os.path.basename(k)))
                    except:
                        print(traceback.format_exc())

            for k in remove_from_blacklist:
                del incomplete_counter[k]

            if len(im_list) == num_imgs:
                break

        if len(im_list) == 0:
            if continuous:
                time.sleep(1)
                continue
            else:
                break
        else:
            print("%s - %s" % (str(datetime.now()), ", ".join(os.path.basename(x) for x in im_list)))

        try:
            for i in range(len(im_list)):
                roi_path = "{}/{}-rois.csv".format(output_dir, os.path.splitext(os.path.basename(im_list[i]))[0])
                img_path = "{}/{}-mask.png".format(output_dir, os.path.splitext(os.path.basename(im_list[i]))[0])
                if tmp_dir is not None:
                    roi_path_tmp = "{}/{}-rois.tmp".format(tmp_dir, os.path.splitext(os.path.basename(im_list[i]))[0])
                    img_path_tmp = "{}/{}-mask.tmp".format(tmp_dir, os.path.splitext(os.path.basename(im_list[i]))[0])
                else:
                    roi_path_tmp = "{}/{}-rois.tmp".format(output_dir, os.path.splitext(os.path.basename(im_list[i]))[0])
                    img_path_tmp = "{}/{}-mask.tmp".format(output_dir, os.path.splitext(os.path.basename(im_list[i]))[0])

                info, roiobjs, mask_comb = predict_image(model, im_list[i], top_k=top_k, score_threshold=score_threshold,
                                       output_polygons=output_polygons, mask_threshold=mask_threshold,
                                       mask_nth=mask_nth, output_minrect=output_minrect, view_margin=view_margin,
                                       fully_connected=fully_connected, fit_bbox_to_polygon=fit_bbox_to_polygon,
                                       bbox_as_fallback=bbox_as_fallback, scale=scale, debayer_int=debayer_int,
                                       output_mask_image=output_mask_image)
                roiext = (info, roiobjs)
                options = ["--output", str(tmp_dir if tmp_dir is not None else output_dir), "--no-images"]
                if output_width_height:
                    options.append("--size-mode")
                roiwriter = ROIWriter(options)
                roiwriter.save([roiext])
                if tmp_dir is not None:
                    os.rename(roi_path_tmp, roi_path)

                if mask_comb is not None:
                    im = Image.fromarray(np.uint8(mask_comb), 'P')
                    im.save(img_path_tmp, "PNG")
                    os.rename(img_path_tmp, img_path)
        except:
            print("Failed processing images: {}".format(",".join(im_list)))
            print(traceback.format_exc())

        # Move finished images to output_path or delete it
        for i in range(len(im_list)):
            if delete_input:
                os.remove(im_list[i])
            else:
                os.rename(im_list[i], os.path.join(output_dir, os.path.basename(im_list[i])))

        end_time = datetime.now()
        inference_time = end_time - start_time
        inference_time = int(inference_time.total_seconds() * 1000)
        print("  Inference + I/O time: {} ms\n".format(inference_time))

def main(argv=None):
    """
    Parses the parameters or, if None, sys.argv and starts prediction mode.

    :param argv: the command-line parameters to parse (list of strings)
    :type: argv: list
    """

    parser = argparse.ArgumentParser(description='YOLACT Prediction')
    parser.add_argument('--model', required=True, type=str,
                        help='The trained model to use (.pth file).')
    parser.add_argument('--config', default="external_config",
                        help='The name of the configuration to use.')
    parser.add_argument('--top_k', default=5, type=int,
                        help='Further restrict the number of predictions (eg objects) to parse')
    parser.add_argument('--score_threshold', default=0, type=float,
                        help='Detections with a score under this threshold will not be considered.')
    parser.add_argument('--fast_nms', action="store_false",
                        help='Whether to use a faster, but not entirely correct version of NMS.')
    parser.add_argument('--cross_class_nms', action="store_true",
                        help='Whether compute NMS cross-class or per-class.')
    parser.add_argument('--prediction_in', default=None, type=str, required=True,
                        help='The directory in which to look for images for processing.')
    parser.add_argument('--prediction_out', default=None, type=str, required=True,
                        help='The directory to store the results in.')
    parser.add_argument('--prediction_tmp', default=None, type=str, required=False,
                        help='The directory to store the results in first, before moving them to the actual output directory.')
    parser.add_argument('--continuous', action="store_true",
                        help='Whether to continuously poll the input directory or exit once all initial images have been processed.')
    parser.add_argument('--delete_input', action="store_true",
                        help='Whether to delete the input images rather than moving them to the output directory.')
    parser.add_argument('--output_polygons', action='store_true',
                        help='Whether to masks are predicted and polygons should be output in the ROIS CSV files', required=False, default=False)
    parser.add_argument('--fit_bbox_to_polygon', action='store_true',
                        help='When outputting polygons whether to fit the bounding box to the polygon', required=False, default=False)
    parser.add_argument('--bbox_as_fallback', default=-1.0, type=float,
                        help='When outputting polygons the bbox can be used as fallback polygon. This happens if the ratio '
                             + 'between the surrounding bbox of the polygon and the bbox is smaller than the specified value. '
                             + 'Turned off if < 0.', required=False)
    parser.add_argument('--mask_threshold', type=float,
                        help='The threshold (0-1) to use for determining the contour of a mask', required=False, default=0.1)
    parser.add_argument('--mask_nth', type=int,
                        help='To speed polygon detection up, use every nth row and column only', required=False, default=1)
    parser.add_argument('--output_minrect', action='store_true',
                        help='When outputting polygons whether to store the minimal rectangle around the objects in the CSV files as well', required=False, default=False)
    parser.add_argument('--view_margin', default=2, type=int, required=False,
                        help='The number of pixels to use as margin around the masks when determining the polygon')
    parser.add_argument('--fully_connected', default='high', choices=['high', 'low'], required=False,
                        help='When determining polygons, whether regions of high or low values should be fully-connected at isthmuses')
    parser.add_argument('--output_width_height', action='store_true', help="Whether to output x/y/w/h instead of x0/y0/x1/y1 in the ROI CSV files",
                        required=False, default=False)
    parser.add_argument('--scale', type=float,
                        help='The scale factor to apply to the image (0-1) before processing. Output will be in original dimension space.', required=False, default=1.0)
    parser.add_argument('--debayer', default="", type=str,
                        help='The OpenCV2 debayering method to use, eg "COLOR_BAYER_BG2BGR"', required=False)
    parser.add_argument('--output_mask_image', action='store_true', default=False,
                        help="Whether to output a mask image (PNG) when predictions generate masks (independent of outputting polygons)", required=False)
    parsed = parser.parse_args(args=argv)

    if parsed.fit_bbox_to_polygon and (parsed.bbox_as_fallback >= 0):
        raise Exception("Options --fit_bbox_to_polygon and --bbox_as_fallback cannot be used together!")
    if (parsed.debayer is not None) and not (parsed.debayer == "") and not parsed.debayer.startswith("COLOR_BAYER_"):
        raise Exception("Expected debayering type to start with COLOR_BAYER_, instead got: " + str(parsed.debayer))

    with torch.no_grad():
        # initializing cudnn
        print('Initializing cudnn', end='')
        cudnn.fastest = True
        torch.set_default_tensor_type('torch.cuda.FloatTensor')
        print(' Done.')

        # load configuration and model
        print('Loading config %s' % parsed.config, end='')
        set_cfg(parsed.config)
        cfg.mask_proto_debug = False
        print(' Done.')

        print('Loading model: %s' % parsed.model, end='')
        net = Yolact()
        net.load_weights(parsed.model)
        net.eval()
        net = net.cuda()
        net.detect.use_fast_nms = parsed.fast_nms
        net.detect.use_cross_class_nms = parsed.cross_class_nms
        print(' Done.')

        predict(model=net, input_dir=parsed.prediction_in, output_dir=parsed.prediction_out, tmp_dir=parsed.prediction_tmp,
                top_k=parsed.top_k, score_threshold=parsed.score_threshold, delete_input=parsed.delete_input,
                output_polygons=parsed.output_polygons, mask_threshold=parsed.mask_threshold, mask_nth=parsed.mask_nth,
                output_minrect=parsed.output_minrect, view_margin=parsed.view_margin, fully_connected=parsed.fully_connected,
                fit_bbox_to_polygon=parsed.fit_bbox_to_polygon, output_width_height=parsed.output_width_height,
                bbox_as_fallback=parsed.bbox_as_fallback, scale=parsed.scale, debayer=parsed.debayer,
                continuous=parsed.continuous, output_mask_image=parsed.output_mask_image)

if __name__ == '__main__':
    main()

max_stars_repo_name: aegean-ai/yolact-original

max_issues_repo_name: aegean-ai/yolact-original

max_forks_repo_name: aegean-ai/yolact-original


text:
"""
Like image_sample.py, but use a noisy image classifier to guide the sampling
process towards more realistic images.
"""

import argparse
import os

import numpy as np
import torch as th
import torch.distributed as dist
import torch.nn.functional as F
from guided_diffusion.image_datasets import load_data
import matplotlib.pyplot as plt

from guided_diffusion import dist_util, logger
from guided_diffusion.script_util import (
    model_defaults,
    diffusion_defaults,
    classifier_defaults,
    create_model,
    create_gaussian_diffusion,
    create_classifier,
    add_dict_to_argparser,
    args_to_dict,
)
from guided_diffusion.sample_util import save_samples
from tqdm import tqdm

def get_gathered_item(x):
    gathered_x = [th.zeros_like(x) for _ in range(dist.get_world_size())]
    dist.all_gather(gathered_x, x)
    return gathered_x

def main():
    args = create_argparser().parse_args()

    visible_gpus_list = []
    if args.gpus:
        visible_gpus_list = [str(gpu_id) for gpu_id in args.gpus.split(",")]
    dist_util.setup_dist(visible_gpu_list=visible_gpus_list, local_rank=args.local_rank)

    logger.configure(dir=os.path.join(args.log_root, args.save_name))
    logger.log(args)

    logger.log("creating model and diffusion...")
    model = create_model(
        **args_to_dict(args, model_defaults().keys())
    )
    diffusion = create_gaussian_diffusion(
        **args_to_dict(args, diffusion_defaults().keys())
    )
    if args.model_path:
        model.load_state_dict(
            dist_util.load_state_dict(args.model_path, map_location="cpu"),
            strict=True
        )
    model.to(dist_util.dev())
    if args.use_fp16:
        model.convert_to_fp16()
    model.eval()

    classifier = create_classifier(
        **args_to_dict(args, classifier_defaults().keys())
    )

    if args.classifier_path:
        logger.log("loading classifier from {}".format(args.classifier_path))
        classifier.load_state_dict(
            dist_util.load_state_dict(args.classifier_path, map_location="cpu"),
            strict=True
        )
    classifier.to(dist_util.dev())
    if args.classifier_use_fp16:
        classifier.convert_to_fp16()
    classifier.eval()

    step_size = 25 if args.timestep_respacing == 'ddim25' else args.timestep_respacing
    batch_grad_norm = th.zeros((step_size, args.batch_size,), device=dist_util.dev())
    batch_updated_grad_norm = th.zeros((step_size, args.batch_size,), device=dist_util.dev())
    batch_probability = th.zeros((step_size, args.batch_size,), device=dist_util.dev())
    batch_entropy = th.zeros((step_size, args.batch_size,), device=dist_util.dev())
    batch_entropy_scale = th.zeros((step_size, args.batch_size,), device=dist_util.dev())
    batch_probability_distribution = th.zeros((step_size, args.batch_size, args.classifier_out_channels,), device=dist_util.dev())

    def cond_fn(x, t, y=None, prior_variance=1.0, t_range_start=0, t_range_end=1000):
        assert y is not None

        step_id = t[0].item() // 40
        with th.enable_grad():
            x_in = x.detach().requires_grad_(True)
            output = classifier(x_in, t)
            logits = output

            log_probs = F.log_softmax(logits, dim=-1)  # (B, C)
            selected = log_probs[range(len(logits)), y.view(-1)]  # (B, )
            cond_grad = th.autograd.grad(selected.sum(), x_in)[0]
            cond_grad = cond_grad * args.classifier_scale

        with th.no_grad():
            if args.use_cond_range_scale:
                if t[0] >= 600 and t[0] < 800:
                    cond_grad = cond_grad * 3

            probs = F.softmax(logits, dim=-1)  # (B, C)
            entropy = (-log_probs * probs).sum(dim=-1)  # (B, )
            entropy_scale = 1.0 / (entropy / np.log(args.classifier_out_channels))  # (B, )
            original_grad_norm = th.norm(cond_grad, p=2, dim=(1, 2, 3), dtype=th.float32).detach()

            batch_probability[step_id] = probs[range(len(logits)), y.view(-1)]
            batch_probability_distribution[step_id] = probs
            batch_grad_norm[step_id] = original_grad_norm
            batch_entropy[step_id] = entropy
            batch_entropy_scale[step_id] = entropy_scale

            logger.log(
                '\n',
                't = ', t[0].detach(), '\n',
                '\t\t mean std median', '\n',
                '\t\t grad_norm =', original_grad_norm.mean(-1).detach(),
                original_grad_norm.std(-1).detach(), original_grad_norm.median(-1).values, '\n',
                '\t\t logits = ', selected.mean(-1).detach(),
                selected.std(-1).detach(), selected.median(-1).values, '\n',
                '\t\t entropy = ', entropy.mean(-1).detach(), entropy.std(-1).detach(), entropy.median(-1).values, '\n',
                '\t\t entropy_scale = ', entropy_scale.mean(-1).detach(), entropy_scale.std(-1).detach(), entropy_scale.median(-1).values, '\n',
            )

            if args.use_entropy_scale and (t[0] >= t_range_start and t[0] < t_range_end):
                cond_grad = cond_grad * entropy_scale.reshape(-1, 1, 1, 1).repeat(1, *cond_grad[0].shape)
                updated_grad_norm = th.norm(cond_grad, p=2, dim=(1, 2, 3), dtype=th.float32).detach()
                batch_updated_grad_norm[step_id].append(updated_grad_norm)

                logger.log(
                    '\t\t updated_grad_norm = ', updated_grad_norm.mean(-1).detach(), updated_grad_norm.std(-1).detach(), updated_grad_norm.median(-1).values, '\n',
                    '\n'
                )

            return cond_grad

    def model_fn(x, t, y=None, t_range_start=0, t_range_end=1000):
        assert y is not None
        return model(x, t, y if args.class_cond else None)

    data_loader = load_data(
        data_dir=args.data_dir,
        batch_size=args.batch_size,
        image_size=args.classifier_image_size,
        class_cond=True,
        random_crop=True,
        dataset_type=args.dataset_type,
        used_attributes=args.used_attributes,
        tot_class=args.tot_class,
        imagenet200_class_list_file_path=args.imagenet200_class_list_file_path,
        celeba_attribures_path=args.celeba_attribures_path
    )

    logger.log("sampling...")
    all_images, all_labels = [], []
    all_grad_norm = []
    all_updated_grad_norm = []
    all_probability = []
    all_entropy = []
    all_entropy_scale = []
    all_probability_distribution = []

    id = 0

    while len(all_images) * args.batch_size < args.num_samples:
        id += 1
        batch, extra = next(data_loader)
        labels = extra["y"].to(dist_util.dev())  # labels: (B, )
        batch = batch.to(dist_util.dev())  # batch:  (B, C, H, W)

        for t in range(24, -1, -1):
            batch_t = th.ones(batch.shape[0], dtype=th.long, device=dist_util.dev()) * t
            diffused_batch = diffusion.q_sample(batch, batch_t)

            cond_grad = cond_fn(
                x=diffused_batch,
                t=batch_t * 40,
                y=labels,
                prior_variance=1.0,
                t_range_start=0,
                t_range_end=1000
            )

        gathered_samples = get_gathered_item(diffused_batch)
        all_images.extend([sample.cpu().numpy() for sample in gathered_samples])

        gathered_labels = get_gathered_item(labels)
        all_labels.extend([labels.cpu().numpy() for labels in gathered_labels])

        logger.log(f"created {len(all_images) * args.batch_size} / {args.num_samples} samples")

        gathered_batch_grad_norm = get_gathered_item(batch_grad_norm)
        gathered_batch_updated_grad_norm = get_gathered_item(batch_updated_grad_norm)
        gathered_batch_probability = get_gathered_item(batch_probability)
        gathered_batch_entropy = get_gathered_item(batch_entropy)
        gathered_batch_entropy_scale = get_gathered_item(batch_entropy_scale)
        gathered_batch_probability_distribution = get_gathered_item(batch_probability_distribution)

        all_grad_norm.extend([x.cpu().numpy() for x in gathered_batch_grad_norm])
        all_updated_grad_norm.extend([x.cpu().numpy() for x in gathered_batch_updated_grad_norm])
        all_probability.extend([x.cpu().numpy() for x in gathered_batch_probability])
        all_entropy.extend([x.cpu().numpy() for x in gathered_batch_entropy])
        all_entropy_scale.extend([x.cpu().numpy() for x in gathered_batch_entropy_scale])
        all_probability_distribution.extend([x.cpu().numpy() for x in gathered_batch_probability_distribution])

    if dist.get_rank() == 0:
        arr = np.concatenate(all_images, axis=0)
        arr = arr[: args.num_samples]
        label_arr = np.concatenate(all_labels, axis=0)
        label_arr = label_arr[: args.num_samples]

        all_grad_norm = np.concatenate(all_grad_norm, axis=1)[:args.num_samples]
        all_updated_grad_norm = np.concatenate(all_updated_grad_norm, axis=1)[:args.num_samples]
        all_probability = np.concatenate(all_probability, axis=1)[:args.num_samples]
        all_entropy = np.concatenate(all_entropy, axis=1)[:args.num_samples]
        all_entropy_scale = np.concatenate(all_entropy_scale, axis=1)[:args.num_samples]
        all_probability_distribution = np.concatenate(all_probability_distribution, axis=1)[:args.num_samples]

        shape_str = "x".join([str(x) for x in arr.shape])

        out_path = os.path.join(logger.get_dir(), "scale{}_steps{}_class0-{}_samples_{}.npz".format(args.classifier_scale, args.timestep_respacing, args.tot_class - 1, shape_str))

        logger.log(f"saving to {out_path}")
        np.savez(out_path, arr, label_arr)

        metainfo_out_path = os.path.join(logger.get_dir(), "metainfo_scale{}_steps{}_class0-{}_samples_{}.npz".format(args.classifier_scale, args.timestep_respacing, args.tot_class - 1, shape_str))

        np.savez(metainfo_out_path, all_grad_norm=all_grad_norm, all_updated_grad_norm=all_updated_grad_norm,
                 all_probability=all_probability, all_entropy=all_entropy, all_entropy_scale=all_entropy_scale,
                 all_probability_distribution=all_probability_distribution)

        # sample_dir = "images_" + "scale{}_steps{}_sample{}".format(args.classifier_scale, args.timestep_respacing, args.num_samples)
        # save_samples(arr, label_arr, args.classifier_out_channels, sample_dir=sample_dir)

    dist.barrier()
    logger.log("sampling complete")

def create_argparser():
    defaults = dict(
        clip_denoised=True,
        num_samples=10000,
        batch_size=16,
        use_ddim=False,
        model_path="",
        classifier_path="",
        classifier_scale=1.0,
        log_root="",
        save_name="",
        gpus="",
        t_range_start=0,
        t_range_end=1000,

        use_entropy_scale=False,

        expected_classifier_gradient_value=-1.0,

        selected_class=-1,
        use_cond_range_scale=False,

        data_dir="",
        val_data_dir="",
        tot_class=1000,
        dataset_type='imagenet-1000',
        used_attributes="",
        imagenet200_class_list_file_path="",
        celeba_attribures_path="",
    )
    defaults.update(diffusion_defaults())
    defaults.update(model_defaults())
    defaults.update(classifier_defaults())
    parser = argparse.ArgumentParser()
    parser.add_argument("--local_rank", type=int, default=0)
    add_dict_to_argparser(parser, defaults)
    return parser

if __name__ == "__main__":
    main()

max_stars_repo_name: ZGCTroy/guided-diffusion

max_issues_repo_name: ZGCTroy/guided-diffusion

max_forks_repo_name: ZGCTroy/guided-diffusion


text:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""

"""

# libraries
import matplotlib.pyplot as plt
import numpy as np
from scipy.integrate import simps
import scipy.constants as cte
from scipy.sparse import diags
from scipy.linalg import inv
from scipy.fftpack import fft, ifft, fftfreq
import scipy.special as sp
from scipy.signal import gaussian

# matplotlib defaults setup
plt.rcParams['figure.dpi'] = 100
#plt.rcParams['savefig.dpi'] = 75
plt.rcParams['figure.autolayout'] = False
plt.rcParams['figure.figsize'] = 14, 8
plt.rcParams['axes.labelsize'] = 18
plt.rcParams['axes.titlesize'] = 20
plt.rcParams['font.size'] = 16
plt.rcParams['lines.linewidth'] = 2.0
plt.rcParams['lines.markersize'] = 8
plt.rcParams['legend.fontsize'] = 14
plt.rcParams['font.family'] = "serif"
plt.rcParams['font.serif'] = "computer modern sans serif"
plt.rcParams['text.usetex']=True
plt.rcParams['text.latex.unicode']=True

# These are the "Tableau 20" colors as RGB.
tableau20 = [(31, 119, 180), (174, 199, 232), (255, 127, 14), (255, 187, 120),
             (44, 160, 44), (152, 223, 138), (214, 39, 40), (255, 152, 150),
             (148, 103, 189), (197, 176, 213), (140, 86, 75), (196, 156, 148),
             (227, 119, 194), (247, 182, 210), (127, 127, 127), (199, 199, 199),
             (188, 189, 34), (219, 219, 141), (23, 190, 207), (158, 218, 229)]

# Scale the RGB values to the [0, 1] range, which is the format matplotlib accepts.
for i in range(len(tableau20)):
    r, g, b = tableau20[i]
    tableau20[i] = (r / 255., g / 255., b / 255.)

# grandezas de interesse em unidades atomicas
au_l = cte.value('atomic unit of length')
au_t = cte.value('atomic unit of time')
au_e = cte.value('atomic unit of energy')

# outras relacoes de interesse
ev = cte.value('electron volt')
c = cte.value('speed of light in vacuum')
hbar_si = cte.value('Planck constant over 2 pi')
me = cte.value('electron mass')
au2ang = au_l / 1e-10
au2ev = au_e / ev

# constantes do problema
E0 = 150.0 # eV
delta_x = 5.0 # angstron
x0 = -30.0 # angstron
xf = -40.0 # angstron
l = 8.1e-6 # m

# otimizando
L = 200 # angstron
N = 4096
hN = int(N/2)
dt = 1e-19 # s

# unidades atomicas
L_au = L / au2ang
dt_au = -1j * dt / au_t
E0_au = E0 / au2ev
delta_x_au = delta_x / au2ang
x0_au = x0 / au2ang
xf_au = xf / au2ang
k0_au = np.sqrt(2 * E0_au)

# malhas direta e reciproca
dx = L / (N-1)
x_au = np.linspace(-L_au/2.0, L_au/2.0, N)
dx_au = np.abs(x_au[1] - x_au[0])
k_au = fftfreq(N, d=dx_au)

# props do material
eg = lambda x: 0.7 * (1.519 + 1.447 * x - 0.15 * x**2) # eV
me_x = lambda x: 0.067+0.083*x
algaas_x = 0.4
Eg = eg(algaas_x)
me_algaas = me_x(algaas_x)
me_gaas = me_x(0.0)
Vb_au = Eg / au2ev
a = 100 # angstron
a_au = a / au2ang
v_au = np.vectorize(lambda x: Vb_au if np.abs(x) > a_au/2 else 0.0)(x_au)
me_eff = np.vectorize(lambda x: me_algaas if np.abs(x) > a_au/2 else me_gaas)(x_au)

# # runge-kutta ordem 4
# alpha = 1j / (2 * me_eff * dx_au ** 2)
# beta = -1j * (v_au + 1.0 / (me_eff * dx_au ** 2))
# diagonal_1 = beta
# diagonal_2 = alpha[1:]
# diagonal_3 = alpha[:-1]
# diagonais = [diagonal_1, diagonal_2, diagonal_3]
# D = diags(diagonais, [0, 1, -1]).toarray()
# def propagador(p):
#     k1 = D.dot(p)
#     k2 = D.dot(p + dt_au * k1 / 2)
#     k3 = D.dot(p + dt_au * k2 / 2)
#     k4 = D.dot(p + dt_au * k3)
#     return p + dt_au * (k1 + 2 * k2 + 2 * k3 + k4) / 6
# propagador_titulo = "Runge-Kutta Ordem 4"

# # crank-nicolson
# alpha = - dt_au * (1j / (2 * me_eff * dx_au ** 2))/2.0
# beta = 1.0 - dt_au * (-1j * (v_au + 1.0 / (me_eff * dx_au ** 2)))/2.0
# gamma = 1.0 + dt_au * (-1j * (v_au + 1.0 / (me_eff * dx_au ** 2)))/2.0
# diagonal_1 = beta
# diagonal_2_1 = alpha[1:]
# diagonal_2_2 = alpha[:-1]
# diagonais = [diagonal_1, diagonal_2_1, diagonal_2_2]
# invB = inv(diags(diagonais, [0, 1, -1]).toarray())
# diagonal_3 = gamma
# diagonal_4_1 = -alpha[1:]
# diagonal_4_2 = -alpha[:-1]
# diagonais_2 = [diagonal_3, diagonal_4_1, diagonal_4_2]
# C = diags(diagonais_2, [0, 1, -1]).toarray()
# D = invB.dot(C)
# propagador = lambda p: D.dot(p)
# propagador_titulo = "Crank-Nicolson"

# split step
me_eff = np.ones(N) * me_gaas
exp_v2 = np.exp(- 0.5j * v_au * dt_au)
exp_t = np.exp(- 0.5j * (2 * np.pi * k_au) ** 2 * dt_au / me_eff)
propagador = lambda p: exp_v2 * ifft(exp_t * fft(exp_v2 * p))
propagador_titulo = "Split-Step"

# chutes iniciais
n = 6
short_grid = np.linspace(-1, 1, N)
g = gaussian(N, std=int(N/50))
estados = np.array([g * sp.legendre(i)(short_grid) for i in range(n)],dtype=np.complex_)
valores = np.zeros(n)
contadores = np.zeros(n)

valores_analiticos_ev = [0.044280126, 0.176480128, 0.394408742, 0.693159988, 1.060713269, 1.438011481]

texto_x_l = -L/2
texto_x_r = 0.7 * L/2

for s in range(n):
    v_ant = 1.0
    while True:
        contadores[s] += 1

        estados[s] = propagador(estados[s])

        # gram-shimdt
        for m in range(s):
            proj = simps(estados[s] * np.conjugate(estados[m]), x_au)
            estados[s] -= proj * estados[m]

        # normaliza
        A = np.sqrt(simps(np.abs(estados[s])**2, x_au))
        estados[s] /= np.sqrt(simps(np.abs(estados[s])**2, x_au))

        if contadores[s] % 1000 == 0:
            # calcula autoestados
            # derivada segunda
            derivada2 = (estados[s][:-2] - 2 * estados[s][1:-1] + estados[s][2:]) / dx_au**2
            psi = estados[s][1:-1]
            psi_conj = np.conjugate(psi)
            # <Psi|H|Psi>
            p_h_p = simps(psi_conj * (-0.5 * derivada2 / me_eff[1:-1] + v_au[1:-1] * psi), x_au[1:-1])
            # divide por <Psi|Psi>
            p_h_p /= A
            valores[s] = p_h_p.real * au2ev # eV

            # especificos do grafico
            fig = plt.figure()
            ax = fig.add_subplot(1, 1, 1)
            ax.set_ylim([-0.1,1.5])
            ax.spines["top"].set_visible(False)
            ax.spines["right"].set_visible(False)
            plt.title("Autoestados/Autovalores - Poço Quântico de $GaAs/Al_{0.4}Ga_{0.6}As$ (%s)" % (propagador_titulo), fontsize=18)
            plt.xlabel("x (\AA)", fontsize=16)
            plt.ylabel(r'$E \, (eV)$', fontsize=16)

            psif = [estados[m] for m in range(s+1)]
            psif = [0.1 * p / np.ptp(p) + valores[m] for m, p in enumerate(psif)]
            lines = []
            for i, p in enumerate(psif):
                line, = plt.plot(x_au * au2ang, p, lw=1.0, color=tableau20[i], label=r'$|\Psi_{%d} (x,t)|^2$' % i)
                lines.append(line)
                ax.text(texto_x_l, valores[i] + 0.02, r"$E_{%d} = %.4f$ eV" % (i, valores[i]))
                ax.text(texto_x_r, valores[i] + 0.02, r"$%d$ K iterações" % (contadores[i]))

            linev, = plt.plot(x_au * au2ang, v_au * au2ev, lw=1.0, color=tableau20[n], label='$V(x)$')
            lines.append(linev)
            plt.legend(handles=lines, loc=9, bbox_to_anchor=(0.5, -0.1), ncol=4)
            plt.show()

            print("%d >>> %.4e / %.4e" % (contadores[s], valores[s], valores_analiticos_ev[s]))

            #if np.abs(1-valores[s]/valores_analiticos_ev[s]) < 0.001:
            if np.abs(1-valores[s]/v_ant) < 0.0001:
                break
            else:
                v_ant = valores[s]

max_stars_repo_name: thiagolcmelo/benchmark

max_issues_repo_name: thiagolcmelo/benchmark

max_forks_repo_name: thiagolcmelo/benchmark

reason: import numpy,import scipy,from scipy