text:
# SPDX-FileCopyrightText: Copyright (c) 2021 Electronics and Telecommunications Research Institute
# SPDX-License-Identifier: Apache-2.0

from matplotlib import pyplot as plt
#from keras.applications.resnet50 import preprocess_input
import nnvm
import tvm
import numpy as np
from mxnet.gluon.model_zoo.vision import get_model
#from tvm.contrib.download import download_testdata

import sys
import argparse
import datetime
import json

from dMazeRunner.dataflow import get_dataflow, ConvLayer, GemmLayer
from dMazeRunner.common import expr_parameters
import dMazeRunner.common.optimizer as optimizer
from dMazeRunner.common import print_summary

supported_frontends = ["mxnet", "keras"]

def download(url, path, overwrite=False):
    import os
    if os.path.isfile(path) and not overwrite:
        print('File {} exists, skip.'.format(path))
        return
    print('Downloading from url {} to {}'.format(url, path))
    try:
        import urllib.request
        urllib.request.urlretrieve(url, path)
    except:
        import urllib
        urllib.urlretrieve(url, path)

def download_block_from_mxnet(args):
    import mxnet as mx
    block = get_model(args.model, pretrained=True)
    sym, params = nnvm.frontend.from_mxnet(block)

    target = 'llvm'
    shape = (1, 3, 224, 224) #input shape; need to be obtained from the model
    shape_dict = {'data': shape}
    model_layers = get_dataflow(sym, target, shape_dict, params, args.batch_size)
    return model_layers

def download_block_from_keras(args):
    import keras
    supported_models = ["resnet50"]

    if args.model == "resnet50":
        weights_url = ''.join(['https://github.com/fchollet/deep-learning-models/releases/',
                            'download/v0.2/resnet50_weights_tf_dim_ordering_tf_kernels.h5'])
        weights_file = 'resnet50_weights.h5'
        download(weights_url, weights_file)
        keras_resnet50 = keras.applications.resnet50.ResNet50(include_top=True, weights=None,
                                                            input_shape=(224, 224, 3), classes=1000)
        keras_resnet50.load_weights('resnet50_weights.h5')

        from PIL import Image
        from keras.applications.resnet50 import preprocess_input
        img_url = 'https://github.com/dmlc/mxnet.js/blob/master/data/cat.png?raw=true'
        download(img_url, 'cat.png')
        img = Image.open('cat.png').resize((224, 224))
        # input preprocess
        data = np.array(img)[np.newaxis, :].astype('float32')
        data = preprocess_input(data).transpose([0, 3, 1, 2])

        shape_dict = {'input_1': data.shape}
        sym, params = nnvm.frontend.from_keras(keras_resnet50)
        target = 'llvm'

    else:
        print("not supported model; supported models:", supported_models)
        exit()

    model_layers = get_dataflow(sym, target, shape_dict, params, args.batch_size)
    return model_layers

def adjust_optimization_strategies(params):
    retVal = True

    if params.PE_UTILIZATION > 0 and params.RF_UTILIZATION > 0:
        params.PE_UTILIZATION = params.PE_UTILIZATION - 0.1
        params.RF_UTILIZATION = params.RF_UTILIZATION - 0.1

    if params.PE_UTILIZATION <= params.THRESHOLD_BEGIN_REDUCE_SPM_UTIL or params.RF_UTILIZATION <= params.THRESHOLD_BEGIN_REDUCE_SPM_UTIL:
        params.SPM_UTILIZATION = params.SPM_UTILIZATION - 0.1

    if params.PE_UTILIZATION <= params.THRESHOLD_DISABLE_OPTS or params.RF_UTILIZATION <= params.THRESHOLD_DISABLE_OPTS:
        params.PRUNE_NO_FEATURE_DRAM = False
        params.PRUNE_NO_REDUCTION = False
        params.MIN_EXEC_METHODS = 1

    if params.PE_UTILIZATION < 0 or params.RF_UTILIZATION < 0 or params.SPM_UTILIZATION < 0:
        retVal = False

    return retVal

def main():
    args = parse_arguments()

    if args.list_models:
        print_summary.print_supported_models(args)
        exit()

    try:
        download_func = eval("download_block_from_" + args.frontend)
        model_layers = download_func(args)

    except Exception as ex:
        print("Failed to download model", args.model)
        print("Error:", ex)
        exit()

    print("\n"+"="*40+"\n")

    if args.list_layers:
        for i, layer in enumerate(model_layers):
            if type(layer) == dict:
                layer_name = layer["name"]
                layer_type = "Special_Function"
            else:
                layer_name = layer.name
                layer_type = str(type(layer)).split(".")[-1][:-2]
            print("{}: {} ({})".format(i, layer_name, layer_type))
        exit()

    if args.arch_spec:
        with open(args.arch_spec) as jsonFile:
            # Parameters used by analytical model of dataflow execution
            json_data = json.load(jsonFile)
            env_params = json_data["arch_details"]
            env = expr_parameters.Environment(**env_params)
            # Params used by map-space generator and optimizer
            expr_params = json_data["arch_basic"]
            params = expr_parameters.ExprParameters(**expr_params)
    else:
        env = expr_parameters.Environment()
        params = expr_parameters.ExprParameters(env)

    params.PRUNE_NO_FEATURE_DRAM = True if args.opt_no_feature_dram else False
    params.PRUNE_NO_REDUCTION = True if args.opt_no_spatial_reduction else False
    params.MIN_EXEC_METHODS = int(args.min_exec_methods) if int(args.min_exec_methods) > 1 else 1

    if args.auto_optimize:
        params.PE_UTILIZATION = 0.8
        params.RF_UTILIZATION = 0.8
        params.SPM_UTILIZATION = 0.5
        params.PRUNE_NO_FEATURE_DRAM = True
        params.PRUNE_NO_REDUCTION = True
        params.MIN_EXEC_METHODS = 100

    if args.min_resource_utilization:
        threshold_resource_util = args.min_resource_utilization
        params.PE_UTILIZATION = threshold_resource_util[0]
        params.RF_UTILIZATION = threshold_resource_util[1]
        params.SPM_UTILIZATION = threshold_resource_util[2]

    try:
        layers = [model_layers[int(args.layer)]]
        layer = layers[0]
        if type(layer) not in [ConvLayer, GemmLayer]:
            try:
                layer_type = layer.name
            except:
                layer_type = layer["name"]
                print("The requeted layer ({}) is not yet supported for analyzing execution on PE-array of dataflow accelerator.".format(layer_type))
                print("To list all the layers of a model, please use option --list-layers.")
                exit()
    except:
        if (args.layer) and int(args.layer) >= len(model_layers):
            print("Specified model does not feature any layer numbered as specified index.")
            print("To list all the layers of a model, please use option --list-layers.")
            exit()
        elif not (args.layer):
            print("Evaluating entire model for optimized dataflow acceleration.")
            layers = model_layers
        else:
            exit()

    total_energy = 0
    total_cycles = 0
    total_edp = 0
    total_evaluations = 0
    total_time = datetime.timedelta(0)

    for i, layer in enumerate(layers):
        if type(layer) not in [ConvLayer, GemmLayer]:
            continue
        layer.env = env
        layer_type = str(type(layer)).split(".")[-1][:-2]
        print("\n")
        print("compiling layer {}: {} ({})".format(i, layer.name, layer_type))
        print(layer)

        result = None
        skip_layer = False
        start_time = datetime.datetime.now()

        result = optimizer.optimize(layer, params)
        num_evaluations = optimizer.get_num_evaluations(layer, params)

        # Adaptive search for finding efficient execution methods
        while (result == None) or (num_evaluations < params.MIN_EXEC_METHODS):
            if args.auto_optimize:
                adjust_opts_success = adjust_optimization_strategies(params)
                if adjust_opts_success == False:
                    print("Optimizer did not find any execution method to evaluate. Please try with another application model and/or target architecture instead.")
                    skip_layer = True
                    break
                result = optimizer.optimize(layer, params)
                num_evaluations = optimizer.get_num_evaluations(layer, params)
            else:
                print("Optimizer did not find any execution method to evaluate. Please try some different optimization strategy, e.g., apply smaller pruning factors, or try auto-optimizer.")
                skip_layer = True
                break

        if skip_layer == True:
            continue

        end_time = datetime.datetime.now()

        delta = end_time - start_time
        num_evaluations = optimizer.get_num_evaluations(layer, params)
        print("Optimized energy-delay product (EDP): %.4E" %  result["min_edp"])
        print("Execution method for optimized EDP: \nTiling factors (DRAM, SPM, RF, SPATIAL): ", result["min_edp_seq"])
        print("Ordering (DRAM, SPM):", result["min_edp_ordering"])
        print("Minimized energy: %.4E" % result["min_energy"])
        print("Execution method for minimized energy: \nTiling factors (DRAM, SPM, RF, SPATIAL): ", result["min_energy_seq"])
        print("Ordering (DRAM, SPM):", result["min_energy_ordering"])
        print("Minimized execution cycles: %.4E" % result["min_cycle"])
        print("Execution method for minimized execution cycles: \nTiling factors (DRAM, SPM, RF, SPATIAL): ", result["min_cycle_seq"])
        print("Ordering (DRAM, SPM):", result["min_cycle_ordering"])
        print("Execution methods evaluated:", num_evaluations)
        print("Time spent in exploration: {} seconds".format(delta.total_seconds()))

        total_edp += result["min_edp"]
        total_energy += result["min_energy"]
        total_cycles += result["min_cycle"]
        total_evaluations += num_evaluations
        total_time += delta

    if len(layers) > 1:
        print("\n")
        print("="*20, "Summary", "="*20)
        print("Optimized Total EDP: %.4E" %  total_edp)
        print("Optimized Total Energy: %.4E" %  total_energy)
        print("Optimized Total Exeuction Cycles: %.4E" %  total_cycles)
        print("Total execution methods evaluated: {}".format(total_evaluations))
        print("Search space exploration done in %.2f" % (total_time.total_seconds())  + " seconds.")

    return

def parse_arguments():
    parser = argparse.ArgumentParser(description=
        "Search space optimizer for mapping loops on coarse-grained programmable dataflow accelerators.")

    # Command-line arguments about specifying front-end, model, layer.
    parser.add_argument("--frontend", dest="frontend",
        choices=supported_frontends,
        help="Specify a front-end for optimizing dataflow acceleration of ML model")
    parser.add_argument("--model", dest="model",
        help="Specify a ML model to be optimized for dataflow acceleration. \n \
            To list all supported models for a specified front-end, please use --list-models")
    parser.add_argument('--list-models', action='store_true', dest="list_models",
        help="Lists all supported models for a specified front-end.")
    parser.add_argument("--layer-index", dest="layer", default=None,
        help="Specify a layer number for optimizing layer execution on dataflow accelerator. \n \
        To list all layers of a model, please use --list-layers.")
    parser.add_argument("--list-layers", action="store_true",
        help="Lists all layers of a model.")
    parser.add_argument("--batch-size", dest="batch_size", type=int, default=1,
        help="Specify batch size for executing a model or a layer. Default is 1.")

    # Set architecture specifications
    parser.add_argument("--arch-spec",
        help="Path of the file containing architecture specification.")

    # Auto-optimizer. By default OFF. When OFF without any optimization
    # strategy, it leads to a brute-force search of mappings.
    # When ON, applies optimization strategies for search-space-reduction.
    parser.add_argument("--auto-optimize", action="store_true",
        help="Automatically optimize mappings through searching a reduced mapping-space.")

    # Strategies for search-space optimization
    parser.add_argument("--opt-min-utilization",
        action="store", dest="min_resource_utilization", type=float, nargs=3,
        help="Prune execution methods featuring lower resource utilization. \n \
            Threshold for utilizations is specified by 3 arguments (PEs RF SPM). \n \
            For example, arguments \' 0.8 0.7 0.5 \' refer to utilization factors of Processing Elements, RegFiles, and scratchpad memory, respectively. \
            Each utilization factor ranges between 0.0 and 1.0. Factor 0.0 implies no search-reduction based on utilization.")
    parser.add_argument("--opt-no-feature-dram", action="store_true",
        help="Discard execution methods that access non-contiguous 2D data of a tensor from DRAM.")
    parser.add_argument("--opt-no-spatial-reduction", action="store_true",
        help="Discard execution methods that require inter-PE communication to perform a reduction operation for the output.")
    parser.add_argument("--opt-min-exec-methods", action="store", dest="min_exec_methods", type=int, default=1,
        help="Explore at least specified number of efficient execution methods.")

    args = parser.parse_args()
    return args

if __name__ == "__main__":
    main()

max_stars_repo_name: oslab-swrc/dMazeRunner

max_issues_repo_name: oslab-swrc/dMazeRunner

max_forks_repo_name: oslab-swrc/dMazeRunner


text:
'''
Data sampling/augmentation class that will produce a batch for VAE
training from a list of image and points of interest
'''
import numpy as np
import argparse
import os
import cv2
import time

class rgb_cropper:
    def __init__(self,list_path,crop_size):
        self.names = []
        self.crops = []

        with open(list_path) as f:
            img_paths = f.read().splitlines()[:]
            for count, ip in enumerate(img_paths):
                print('loading img {}/{}'.format(count+1,len(img_paths)),end='\r')
                # Load images
                bgr = cv2.imread(ip)

                # Load annotations
                poi_path = ip[:-3] + "csv"
                poi = []
                with open(poi_path) as poi_file:
                    points = []
                    for poi in poi_file.read().splitlines():
                        x, y = poi.split(",")
                        crop = self.crop_sample(bgr,int(x),int(y),crop_size)
                        if len(crop) > 0:
                            self.names.append(ip.split("/")[-1])
                            self.crops.append(crop)
        num_names = len(self.names)
        num_crops = len(self.names)
        print('done loading {} imgs'.format(len(img_paths)))
        np.save("../../output/names_{}.npy".format(num_names), np.array(self.names))
        np.save("../../output/crops_{}.npy".format(num_crops), np.array(self.crops))

    def crop_sample(self,img,x,y,crop_size):
        width, height, _ = img.shape

        x_min = x-int(crop_size/2)
        x_max = x_min+crop_size
        y_min = y-int(crop_size/2)
        y_max = y_min+crop_size
        #print("x_min: {}, x_max {}, y_min {}, y_max {}".format(x_min,x_max,x_min,y_max))
        if(x_min >= 0 and x_max < width and y_min >= 0 and y_max < height):
            crop = img[y_min:y_max,x_min:x_max,:]
            w, h, _ = crop.shape
            if w != crop_size or h != crop_size:
                crop = cv2.resize(crop, (crop_size, crop_size))
            return crop.astype(np.float)/255.0
        return []

if __name__ == "__main__":
    """
    Main function for executing the .py script.
    Command:
        -p path/<filename>.npy
    """
    # construct the argument parser and parse the arguments
    ap = argparse.ArgumentParser()
    ap.add_argument("-l", "--list", type=str,
                    help="list of img files")
    args = vars(ap.parse_args())

    rc = rgb_cropper(args["list"], 64)

ext: py

max_stars_repo_name: markpp/autoencoders

max_issues_repo_name: markpp/autoencoders

max_forks_repo_name: markpp/autoencoders

reason: import numpy


text:
import os
import tensorflow as tf
import numpy as np
import math

def deprocess(image):
    with tf.name_scope("deprocess"):
        # [-1, 1] => [0, 1]
        return (image + 1) / 2

# Normalizes a tensor troughout the Channels dimension (BatchSize, Width, Height, Channels)
# Keeps 4th dimension to 1. Output will be (BatchSize, Width, Height, 1).
def tf_Normalize(tensor):
    Length = tf.sqrt(tf.reduce_sum(tf.square(tensor), axis = -1, keep_dims=True))
    return tf.div(tensor, Length)

# Computes the dot product between 2 tensors (BatchSize, Width, Height, Channels)
# Keeps 4th dimension to 1. Output will be (BatchSize, Width, Height, 1).
def tf_DotProduct(tensorA, tensorB):
    return tf.reduce_sum(tf.multiply(tensorA, tensorB), axis = -1, keep_dims=True)

#Physically based lamp attenuation
def tf_lampAttenuation_pbr(distance):
    return 1.0 / tf.square(distance)

#Clip values between min an max
def squeezeValues(tensor, min, max):
    return tf.clip_by_value(tensor, min, max)

# Generate an array grid between -1;1 to act as the "coordconv" input layer (see coordconv paper)
def generateCoords(inputShape):
    crop_size = inputShape[-2]
    firstDim = inputShape[0]

    Xcoords= tf.expand_dims(tf.lin_space(-1.0, 1.0, crop_size), axis=0)
    Xcoords = tf.tile(Xcoords,[crop_size, 1])
    Ycoords = -1 * tf.transpose(Xcoords) #put -1 in the bottom of the table
    Xcoords = tf.expand_dims(Xcoords, axis = -1)
    Ycoords = tf.expand_dims(Ycoords, axis = -1)
    coords = tf.concat([Xcoords, Ycoords], axis=-1)
    coords = tf.expand_dims(coords, axis = 0)#Add dimension to support batch size and nbRenderings should now be [1, 256, 256, 2].
    coords = tf.tile(coords, [firstDim, 1, 1, 1]) #Add the proper dimension here for concat
    return coords

# Generate an array grid between -1;1 to act as each pixel position for the rendering.
def generateSurfaceArray(crop_size, pixelsToAdd = 0):
    totalSize = crop_size + (pixelsToAdd * 2)
    surfaceArray=[]
    XsurfaceArray = tf.expand_dims(tf.lin_space(-1.0, 1.0, totalSize), axis=0)
    XsurfaceArray = tf.tile(XsurfaceArray,[totalSize, 1])
    YsurfaceArray = tf.transpose(XsurfaceArray) #put -1 in the bottom of the table
    XsurfaceArray = tf.expand_dims(XsurfaceArray, axis = -1)
    YsurfaceArray = tf.expand_dims(YsurfaceArray, axis = -1)

    surfaceArray = tf.concat([XsurfaceArray, YsurfaceArray, tf.zeros([totalSize, totalSize,1], dtype=tf.float32)], axis=-1)
    surfaceArray = tf.expand_dims(tf.expand_dims(surfaceArray, axis = 0), axis = 0)#Add dimension to support batch size and nbRenderings
    return surfaceArray

#Adds a little bit of noise
def addNoise(renderings):
    shape = tf.shape(renderings)
    stddevNoise = tf.exp(tf.random_normal((), mean = np.log(0.005), stddev=0.3))
    noise = tf.random_normal(shape, mean=0.0, stddev=stddevNoise)
    return renderings + noise

#Generate a random direction on the upper hemisphere with gaps on the top and bottom of Hemisphere. Equation is described in the Global Illumination Compendium (19a)
def tf_generate_normalized_random_direction(batchSize, nbRenderings, lowEps = 0.001, highEps = 0.05):
    r1 = tf.random_uniform([batchSize, nbRenderings, 1], 0.0 + lowEps, 1.0 - highEps, dtype=tf.float32)
    r2 =  tf.random_uniform([batchSize, nbRenderings, 1], 0.0, 1.0, dtype=tf.float32)
    r = tf.sqrt(r1)
    phi = 2 * math.pi * r2
    #min alpha = atan(sqrt(1-r^2)/r)
    x = r * tf.cos(phi)
    y = r * tf.sin(phi)
    z = tf.sqrt(1.0 - tf.square(r))
    finalVec = tf.concat([x, y, z], axis=-1) #Dimension here should be [batchSize,nbRenderings, 3]
    return finalVec

#Generate a distance to compute for the specular renderings (as position is important for this kind of renderings)
def tf_generate_distance(batchSize, nbRenderings):
    gaussian = tf.random_normal([batchSize, nbRenderings, 1], 0.5, 0.75, dtype=tf.float32) # parameters chosen empirically to have a nice distance from a -1;1 surface.
    return (tf.exp(gaussian))

#generate the diffuse rendering for the loss computation
def tf_generateDiffuseRendering(batchSize, nbRenderings, targets, outputs, renderer):
    currentViewPos = tf_generate_normalized_random_direction(batchSize, nbRenderings, lowEps = 0.001, highEps = 0.1)
    currentLightPos = tf_generate_normalized_random_direction(batchSize, nbRenderings, lowEps = 0.001, highEps = 0.1)

    wi = currentLightPos
    wi = tf.expand_dims(wi, axis=2)
    wi = tf.expand_dims(wi, axis=2)

    wo = currentViewPos
    wo = tf.expand_dims(wo, axis=2)
    wo = tf.expand_dims(wo, axis=2)

    #Add a dimension to compensate for the nb of renderings
    #targets = tf.expand_dims(targets, axis=-2)
    #outputs = tf.expand_dims(outputs, axis=-2)

    #Here we have wi and wo with shape [batchSize, height,width, nbRenderings, 3]
    renderedDiffuse = renderer.tf_Render(targets,wi,wo, None, "diffuse", useAugmentation = False, lossRendering = True)[0]

    renderedDiffuseOutputs = renderer.tf_Render(outputs,wi,wo, None, "", useAugmentation = False, lossRendering = True)[0]#tf_Render_Optis(outputs,wi,wo)
    #renderedDiffuse = tf.Print(renderedDiffuse, [tf.shape(renderedDiffuse)],  message="This is renderings targets Diffuse: ", summarize=20)
    #renderedDiffuseOutputs = tf.Print(renderedDiffuseOutputs, [tf.shape(renderedDiffuseOutputs)],  message="This is renderings outputs Diffuse: ", summarize=20)
    return [renderedDiffuse, renderedDiffuseOutputs]

#generate the specular rendering for the loss computation
def tf_generateSpecularRendering(batchSize, nbRenderings, surfaceArray, targets, outputs, renderer):
    currentViewDir = tf_generate_normalized_random_direction(batchSize, nbRenderings, lowEps = 0.001, highEps = 0.1)
    currentLightDir = currentViewDir * tf.expand_dims([-1.0, -1.0, 1.0], axis = 0)
    #Shift position to have highlight elsewhere than in the center.
    currentShift = tf.concat([tf.random_uniform([batchSize, nbRenderings, 2], -1.0, 1.0), tf.zeros([batchSize, nbRenderings, 1], dtype=tf.float32) + 0.0001], axis=-1)

    currentViewPos = tf.multiply(currentViewDir, tf_generate_distance(batchSize, nbRenderings)) + currentShift
    currentLightPos = tf.multiply(currentLightDir, tf_generate_distance(batchSize, nbRenderings)) + currentShift

    currentViewPos = tf.expand_dims(currentViewPos, axis=2)
    currentViewPos = tf.expand_dims(currentViewPos, axis=2)

    currentLightPos = tf.expand_dims(currentLightPos, axis=2)
    currentLightPos = tf.expand_dims(currentLightPos, axis=2)

    wo = currentViewPos - surfaceArray
    wi = currentLightPos - surfaceArray

    #targets = tf.expand_dims(targets, axis=-2)
    #outputs = tf.expand_dims(outputs, axis=-2)
    #targets = tf.Print(targets, [tf.shape(targets)],  message="This is targets in specu renderings: ", summarize=20)
    renderedSpecular = renderer.tf_Render(targets,wi,wo, None, "specu", useAugmentation = False, lossRendering = True)[0]
    renderedSpecularOutputs = renderer.tf_Render(outputs,wi,wo, None, "", useAugmentation = False, lossRendering = True)[0]
    #tf_Render_Optis(outputs,wi,wo, includeDiffuse = a.includeDiffuse)

    #renderedSpecularOutputs = tf.Print(renderedSpecularOutputs, [tf.shape(renderedSpecularOutputs)],  message="This is renderings outputs Specular: ", summarize=20)
    return [renderedSpecular, renderedSpecularOutputs]

def tf_generateTopRendering(batchSize, nbRenderings, surfaceArray, targets, outputs, renderer):
    fov = tf.random_uniform([batchSize, nbRenderings, 1], 25.0, 35.0)
    dist = 1 / tf.tan(fov / 2 / 180 * np.pi)
    xy = tf.random_uniform([batchSize, nbRenderings, 2], -1.0, 1.0)
    pos = tf.concat([xy, dist], axis=-1)

    pos = tf.expand_dims(pos, axis=2)
    pos = tf.expand_dims(pos, axis=2)

    wi = pos - surfaceArray

    #targets = tf.expand_dims(targets, axis=-2)
    #outputs = tf.expand_dims(outputs, axis=-2)
    #targets = tf.Print(targets, [tf.shape(targets)],  message="This is targets in specu renderings: ", summarize=20)
    renderedTop = renderer.tf_Render(targets,wi,wi, None, "specu", useAugmentation = False, lossRendering = True)[0]
    renderedTopOutputs = renderer.tf_Render(outputs,wi,wi, None, "", useAugmentation = False, lossRendering = True)[0]
    #tf_Render_Optis(outputs,wi,wo, includeDiffuse = a.includeDiffuse)

    #renderedSpecularOutputs = tf.Print(renderedSpecularOutputs, [tf.shape(renderedSpecularOutputs)],  message="This is renderings outputs Specular: ", summarize=20)
    return [renderedTop, renderedTopOutputs]

#Put the normals and roughness back to 3 channel for easier processing.
def deprocess_outputs(outputs):
    partialOutputedNormals = outputs[:,:,:,0:2] * 3.0 #The multiplication here gives space to generate direction with angle > pi/4
    outputedDiffuse = outputs[:,:,:,2:5]
    outputedRoughness = outputs[:,:,:,5]
    outputedSpecular = outputs[:,:,:,6:9]
    normalShape = tf.shape(partialOutputedNormals)
    newShape = [normalShape[0], normalShape[1], normalShape[2], 1]
    #normalShape[-1] = 1
    tmpNormals = tf.ones(newShape, tf.float32)

    normNormals = tf_Normalize(tf.concat([partialOutputedNormals, tmpNormals], axis = -1))
    outputedRoughnessExpanded = tf.expand_dims(outputedRoughness, axis = -1)
    return tf.concat([normNormals, outputedDiffuse, outputedRoughnessExpanded, outputedRoughnessExpanded, outputedRoughnessExpanded, outputedSpecular], axis=-1)


text:
import os
from abc import ABC, abstractmethod
from collections import deque
from functools import partial
from multiprocessing.dummy import Pool as DummyPool
from typing import Callable, Dict, List, Optional, Union

import numpy as np
import pystoi
import torch
import torch.multiprocessing as mp
from loguru import logger
from pesq import pesq
from torch import Tensor
from torch.multiprocessing.pool import Pool
from torchaudio.functional import highpass_biquad
from torchaudio.transforms import Resample

from df.enhance import df_features, load_audio, save_audio
from df.sepm import composite as composite_py
from df.utils import as_complex, get_device, get_resample_params, resample
from libdf import DF

RESAMPLE_METHOD = "sinc_fast"

try:
    from tqdm import tqdm
except ImportError:

    def tqdm(iterable, total: Optional[int] = None, log_freq_percent=25, desc="Progress"):
        assert 0 < log_freq_percent < 100
        # tqdm not available using fallback
        logged = set()
        try:
            L = iterable.__len__()
        except AttributeError:
            assert total is not None
            L = total

        for k, i in enumerate(iterable):
            yield i
            p = (k + 1) / L
            progress = int(100 * p)
            if progress % log_freq_percent == 0 and progress > 0:
                if progress not in logged:
                    logger.info("{}: {: >2d}%".format(desc, progress))
                    logged.add(progress)

@torch.no_grad()
def enhance(model, df_state, audio, f_hp_cutoff: Optional[int] = None):
    model.eval()
    if hasattr(model, "reset_h0"):
        model.reset_h0(batch_size=1, device=get_device())
    spec, erb_feat, spec_feat = df_features(audio, df_state, get_device())
    spec = model(spec, erb_feat, spec_feat)[0].squeeze(0)  # [C, T, F, 2]
    audio = df_state.synthesis(as_complex(spec).cpu().numpy())
    if f_hp_cutoff is not None:
        audio = highpass_biquad(
            torch.from_numpy(audio), df_state.sr(), cutoff_freq=f_hp_cutoff
        ).numpy()
    return audio

def evaluation_loop(
    df_state: DF,
    model,
    clean_files: List[str],
    noisy_files: List[str],
    metrics: List[str] = ["stoi", "composite", "sisdr"],
    save_audio_callback: Optional[Callable[[str, Tensor], None]] = None,
    n_workers: int = 4,
) -> Dict[str, float]:
    sr = df_state.sr()
    metrics_dict = {
        "stoi": partial(StoiMetric, sr=sr),
        "sisdr": SiSDRMetric,
        "composite": partial(CompositeMetric, sr=sr),
        "composite-octave": partial(CompositeMetric, sr=sr, use_octave=True),
        "pesq": partial(PesqMetric, sr=sr),
    }
    if n_workers >= 1:
        pool_fn = mp.Pool
    else:
        pool_fn = DummyPool
    with pool_fn(processes=max(1, n_workers)) as pool:
        metrics: List[Metric] = [metrics_dict[m.lower()](pool=pool) for m in metrics]
        for noisyfn, cleanfn in tqdm(zip(noisy_files, clean_files), total=len(noisy_files)):
            noisy, _ = load_audio(noisyfn, sr)
            clean, _ = load_audio(cleanfn, sr)
            logger.debug(f"Processing {os.path.basename(noisyfn)}, {os.path.basename(cleanfn)}")
            enh = enhance(model, df_state, noisy)[0]
            clean = df_state.synthesis(df_state.analysis(clean.numpy()))[0]
            noisy = df_state.synthesis(df_state.analysis(noisy.numpy()))[0]
            for m in metrics:
                m.add(clean=clean, enhanced=enh, noisy=noisy)
            if save_audio_callback is not None:
                enh = torch.as_tensor(enh).to(torch.float32).view(1, -1)
                save_audio_callback(cleanfn, enh)
        logger.info("Waiting for metrics computation completion. This could take a few minutes.")
        out_dict = {}
        for m in metrics:
            for k, v in m.mean().items():
                out_dict[k] = v
        return out_dict

def stoi(clean, degraded, sr, extended=False):
    assert len(clean.shape) == 1
    if sr != 10000:
        clean = resample(torch.as_tensor(clean), sr, 10000, method=RESAMPLE_METHOD).numpy()
        degraded = resample(torch.as_tensor(degraded), sr, 10000, method=RESAMPLE_METHOD).numpy()
        sr = 10000
    stoi = pystoi.stoi(x=clean, y=degraded, fs_sig=sr, extended=extended)
    return stoi

def composite(
    clean: Union[np.ndarray, Tensor], degraded: Union[np.ndarray, Tensor], sr: int, use_octave=False
) -> np.ndarray:
    """Compute pesq, csig, cbak, covl, ssnr"""
    assert len(clean.shape) == 1, f"Input must be 1D array, but got input shape {clean.shape}"
    if sr != 16000:
        clean = resample(torch.as_tensor(clean), sr, 16000, method=RESAMPLE_METHOD).numpy()
        degraded = resample(torch.as_tensor(degraded), sr, 16000, method=RESAMPLE_METHOD).numpy()
        sr = 16000
    if use_octave:
        from tempfile import NamedTemporaryFile

        import semetrics

        with NamedTemporaryFile(suffix=".wav") as cf, NamedTemporaryFile(suffix=".wav") as nf:
            # Note: Quantizing to int16 results in slightly modified metrics. Thus, keep f32 prec.
            save_audio(cf.name, clean, sr, dtype=torch.float32)
            save_audio(nf.name, degraded, sr, dtype=torch.float32)
            c = semetrics.composite(cf.name, nf.name)
    else:
        c = composite_py(as_numpy(clean), as_numpy(degraded), sr)
    return np.asarray(c)

def si_sdr_speechmetrics(reference: np.ndarray, estimate: np.ndarray):
    """This implementation is adopted from https://github.com/aliutkus/speechmetrics/blob/dde303e/speechmetrics/relative/sisdr.py"""
    # as provided by @Jonathan-LeRoux and slightly adapted for the case of just one reference
    # and one estimate.
    # see original code here: https://github.com/sigsep/bsseval/issues/3#issuecomment-494995846
    reference = reference.reshape(-1, 1)
    estimate = estimate.reshape(-1, 1)
    eps = np.finfo(reference.dtype).eps
    Rss = np.dot(reference.T, reference)

    # get the scaling factor for clean sources
    a = (eps + np.dot(reference.T, estimate)) / (Rss + eps)

    e_true = a * reference
    e_res = estimate - e_true

    Sss = (e_true**2).sum()
    Snn = (e_res**2).sum()

    sisdr = 10 * np.log10((eps + Sss) / (eps + Snn))
    return sisdr

class Metric(ABC):
    def __init__(
        self,
        name: Union[str, List[str]],
        source_sr: Optional[int] = None,
        target_sr: Optional[int] = None,
        device="cpu",
    ):
        self.name = name
        self.sr = target_sr
        self.resampler = None
        if source_sr is not None and target_sr is not None and source_sr != target_sr:
            params = get_resample_params(RESAMPLE_METHOD)
            self.resampler = Resample(source_sr, target_sr, **params).to(device)
        self.enh_values: Dict[str, List[float]] = (
            {name: []} if isinstance(name, str) else {n: [] for n in name}
        )
        self.noisy_values: Dict[str, List[float]] = (
            {name: []} if isinstance(name, str) else {n: [] for n in name}
        )

    @abstractmethod
    def compute_metric(self, clean, degraded) -> Union[float, np.ndarray]:
        pass

    def _add_values_enh(self, values_enh: Union[float, np.ndarray]):
        if isinstance(values_enh, float):
            values_enh = np.asarray([values_enh])
        for k, v in zip(self.enh_values.keys(), values_enh):
            self.enh_values[k].append(v)

    def _add_values_noisy(self, values_noisy: Union[float, np.ndarray]):
        if isinstance(values_noisy, float):
            values_noisy = np.asarray([values_noisy])
        for k, v in zip(self.noisy_values.keys(), values_noisy):
            self.noisy_values[k].append(v)

    def maybe_resample(self, x) -> Tensor:
        if self.resampler is not None:
            x = self.resampler.forward(torch.as_tensor(x))
        return x

    def add(self, clean, enhanced, noisy):
        clean = self.maybe_resample(clean).squeeze(0)
        enhanced = self.maybe_resample(enhanced).squeeze(0)
        values_enh = self.compute_metric(clean=clean, degraded=enhanced)
        self._add_values_enh(values_enh)
        if noisy is not None:
            noisy = self.maybe_resample(noisy).squeeze(0)
            values_noisy = self.compute_metric(clean=clean, degraded=enhanced)
            self._add_values_noisy(values_noisy)

    def mean(self) -> Dict[str, float]:
        out = {}
        for k in self.enh_values.keys():
            if k in self.noisy_values and len(self.noisy_values[k]) > 0:
                out[f"Noisy    {k}"] = np.mean(self.noisy_values[k])
            out[f"Enhanced {k}"] = np.mean(self.enh_values[k])
        return out

# Multiprocessing Metric
class MPMetric(Metric):
    def __init__(
        self,
        name,
        pool: Pool,
        source_sr: Optional[int] = None,
        target_sr: Optional[int] = None,
    ):
        super().__init__(name, source_sr=source_sr, target_sr=target_sr)
        self.pool = pool
        self.worker_results = deque()

    def add(self, clean, enhanced, noisy):
        clean = self.maybe_resample(torch.as_tensor(clean)).squeeze(0)
        enhanced = self.maybe_resample(torch.as_tensor(enhanced)).squeeze(0)
        h = self.pool.apply_async(
            self.compute_metric,
            (clean, enhanced),
            callback=self._add_values_enh,
            error_callback=logger.error,
        )
        h.get()
        if noisy is not None:
            noisy = self.maybe_resample(torch.as_tensor(noisy)).squeeze(0)
            h = self.pool.apply_async(
                self.compute_metric,
                (clean, noisy),
                callback=self._add_values_noisy,
                error_callback=logger.error,
            )
            self.worker_results.append(h)

    def mean(self) -> Dict[str, float]:
        while len(self.worker_results) > 0:
            h = self.worker_results.popleft()
            h.get()
        self.pool.close()
        self.pool.join()
        return super().mean()

    def __getstate__(self):
        self_dict = self.__dict__.copy()
        del self_dict["pool"]
        del self_dict["worker_results"]
        return self_dict

    def __setstate__(self, state):
        self.__dict__.update(state)

class SiSDRMetric(MPMetric):
    def __init__(self, pool: Pool):
        super().__init__(name="SISDR", pool=pool)

    def compute_metric(self, clean, degraded) -> float:
        return si_sdr_speechmetrics(reference=as_numpy(clean), estimate=as_numpy(degraded))

class StoiMetric(MPMetric):
    def __init__(self, sr: int, pool: Pool):
        super().__init__(name="STOI", pool=pool, source_sr=sr, target_sr=10000)

    def compute_metric(self, clean, degraded) -> float:
        assert self.sr is not None
        return stoi(clean=as_numpy(clean), degraded=as_numpy(degraded), sr=self.sr)

class PesqMetric(MPMetric):
    def __init__(self, sr: int, pool: Pool):
        super().__init__(name="PESQ", pool=pool, source_sr=sr, target_sr=16000)

    def compute_metric(self, clean, degraded) -> Union[float, np.ndarray]:
        assert self.sr is not None
        return pesq(self.sr, as_numpy(clean), as_numpy(degraded), "wb")

class CompositeMetric(MPMetric):
    def __init__(self, sr: int, pool: Pool, use_octave: bool = False):
        names = ["PESQ", "CSIG", "CBAK", "COVL", "SSNR"]
        super().__init__(names, pool=pool, source_sr=sr, target_sr=16000)
        self.use_octave = use_octave

    def compute_metric(self, clean, degraded) -> Union[float, np.ndarray]:
        assert self.sr is not None
        c = composite(
            clean=clean.squeeze(0),
            degraded=degraded.squeeze(0),
            sr=self.sr,
            use_octave=self.use_octave,
        )
        return c

def as_numpy(x) -> np.ndarray:
    if isinstance(x, torch.Tensor):
        return x.cpu().detach().numpy()
    return x

max_stars_repo_name: Rikorose/DeepFilterNet

max_issues_repo_name: Rikorose/DeepFilterNet

max_forks_repo_name: Rikorose/DeepFilterNet


text:
import numpy as np
import pytest

from deepocr.datasets import utils

@pytest.mark.parametrize(
    "input_str, vocab, output_str",
    [
        ['f orêt', 'latin', 'foret'],
        ['f or êt', 'french', 'forêt'],
        ['¢¾©téØßřůž', 'french', '¢■■té■■ruz'],
        ['Ûæëð', 'french', 'Û■ë■'],
        ['Ûæë<àð', 'latin', 'U■e<a■'],
        ['Ûm@læ5€ëð', 'currency', '■■■■■■€■■'],
        ['Ûtë3p2ð', 'digits', '■■■3■2■'],
    ],
)
def test_translate(input_str, vocab, output_str):
    out = utils.translate(input_str, vocab, unknown_char='■')
    assert out == output_str

@pytest.mark.parametrize(
    "input_str",
    [
        'frtvorêt',
        'for98€t',
        'uéîUY',
        'ÛAZ$£ë',
    ],
)
def test_encode_decode(input_str):
    mapping = """3K}7eé;5àÎYho]QwV6qU~W"XnbBvcADfËmy.9ÔpÛ*{CôïE%M4#ÈR:g@T$x?0î£|
        za1ù8,OG€P-kçHëÀÂ2É/ûIJ\'j(LNÙFut[)èZs+&°Sd=Ï!<â_Ç>rêi`l"""
    encoded = utils.encode_string(input_str, mapping)
    decoded = utils.decode_sequence(encoded, mapping)
    assert decoded == input_str

def test_decode_sequence():
    mapping = "abcdef"
    with pytest.raises(TypeError):
        utils.decode_sequence(123, mapping)
    with pytest.raises(AssertionError):
        utils.decode_sequence(np.array([2, 10]), mapping)
    with pytest.raises(AssertionError):
        utils.decode_sequence(np.array([2, 4.5]), mapping)

    assert utils.decode_sequence([3, 4, 3, 4], mapping) == "dede"

@pytest.mark.parametrize(
    "sequences, vocab, target_size, sos, eos, pad, dynamic_len, error, out_shape, gts",
    [
        [['cba'], 'abcdef', None, None, 1, None, False, True, (1, 3), [[2, 1, 0]]],  # eos in vocab
        [['cba', 'a'], 'abcdef', None, None, -1, None, False, False, (2, 4), [[2, 1, 0, -1], [0, -1, -1, -1]]],
        [['cba', 'a'], 'abcdef', None, None, 6, None, False, False, (2, 4), [[2, 1, 0, 6], [0, 6, 6, 6]]],
        [['cba', 'a'], 'abcdef', 2, None, -1, None, False, False, (2, 2), [[2, 1], [0, -1]]],
        [['cba', 'a'], 'abcdef', 4, None, -1, None, False, False, (2, 4), [[2, 1, 0, -1], [0, -1, -1, -1]]],
        [['cba', 'a'], 'abcdef', 5, 7, -1, None, False, False, (2, 5), [[7, 2, 1, 0, -1], [7, 0, -1, -1, -1]]],
        [['cba', 'a'], 'abcdef', 6, 7, -1, None, True, False, (2, 5), [[7, 2, 1, 0, -1], [7, 0, -1, -1, -1]]],
        [['cba', 'a'], 'abcdef', None, 7, -1, 9, False, False, (2, 6), [[7, 2, 1, 0, -1, 9], [7, 0, -1, 9, 9, 9]]],
    ],
)
def test_encode_sequences(sequences, vocab, target_size, sos, eos, pad, dynamic_len, error, out_shape, gts):
    if error:
        with pytest.raises(ValueError):
            utils.encode_sequences(sequences, vocab, target_size, eos, sos, pad, dynamic_len)
    else:
        out = utils.encode_sequences(sequences, vocab, target_size, eos, sos, pad, dynamic_len)
        assert isinstance(out, np.ndarray)
        assert out.shape == out_shape
        assert np.all(out == np.asarray(gts)), print(out, gts)

max_stars_repo_name: das-projects/deepOCR

max_issues_repo_name: das-projects/deepOCR

max_forks_repo_name: das-projects/deepOCR


text:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Tests to check point-source on a sphere

"""

import os
try:
    os.chdir('beamshapes/tests')
except:
    pass

import unittest
import numpy as np
import pandas as pd
from beamshapes import point_source_on_a_sphere_directivity
from beamshapes.point_source_on_a_sphere import d_zero_func

class PointSourceSphere(unittest.TestCase):

    def setUp(self):
        self.paramv = {}
        plotdata = pd.read_csv('plots_data/pointonsphere.csv')
        self.by_ka = plotdata.groupby('kR')

    def perform_kR_match(self, kRval):
        self.paramv['k'] = 10
        self.paramv['R'] = kRval/self.paramv['k']
        angles = np.radians(self.by_ka.get_group(kRval)['theta'])
        actual_dirnlty = self.by_ka.get_group(kRval)['r'].to_numpy()

        _, output_dirnlty = point_source_on_a_sphere_directivity(angles,
                                                                     self.paramv)
        error = np.abs(output_dirnlty-actual_dirnlty)
        return np.max(error)

    def test_kamatches(self):
        kRvals = [1, 3, 5, 10]
        max_abs_errors = np.zeros(len(kRvals))
        for i, each in enumerate(kRvals):
            max_abs_errors[i] = self.perform_kR_match(each)
        print(max_abs_errors)
        self.assertTrue(np.all(max_abs_errors<=1))

class OnAxisPointSourceOnSphere(unittest.TestCase):

    def setUp(self):
        self.R = 0.1
        self.plotdata = pd.read_csv('plots_data/onaxis_response_pointsourceonsphere.csv')
        self.kr_values = self.plotdata['kR'].tolist()
        self.tolerance = 0.1

    def test_onaxis(self):
        dzero_outputs = []
        for each_kr in self.kr_values:
            k_val = each_kr/self.R
            NN = int(10 + 2*each_kr)
            dzero_outputs.append(np.abs(d_zero_func(k_val, self.R, NN)))
        db_dzero = 20*np.log10(dzero_outputs)
        # check all values are < deviation
        deviation = db_dzero-self.plotdata['onaxis_db']
        less_than_threshold = np.all(np.abs(deviation)<self.tolerance)
        self.assertTrue(less_than_threshold)

if __name__=='__main__':
    unittest.main()


text:
import numpy as np
import pandas as pd

from sklearn.base import TransformerMixin, BaseEstimator

from mlmr.function import transform_concat, map_reduce, calculate_pool_size

class BaseMapReduceTransformer(TransformerMixin, BaseEstimator):
    """
    Sklearn wrapper base class for `mlmr.function.transform_concat` function.
    Add implementation of `transform_part` function with your logic.
    """

    def __init__(self, n_jobs=1):
        """
        :param n_jobs: number of jobs to run in parallel. `-1` means using all processors.
        """
        self.n_jobs = n_jobs

    def fit(self, X, y=None):
        return self

    def transform(self, X):
        return transform_concat(X, self.transform_part, self.n_jobs)

    def transform_part(self, X):
        """Transform function on data slice"""
        raise NotImplementedError()

class FunctionMapReduceTransformer(TransformerMixin, BaseEstimator):
    """
    Sklearn wrapper class for `mlmr.function.map_reduce` function.
    """

    def __init__(
        self,
        map_func,
        reduce_func=pd.concat,
        data_split_func=None,
        n_jobs=1
    ):
        """
        :param map_func: (Callable) - Map function.
                         Map function signature: func(DataSplit) -> Any.
                         Function can't be lambda or local function!
        :param reduce_func: (Callable) - Reduce function. Reduce function signature: func(Iterable[MapResult]) -> Any.
        :param data_split_func: (Callable) - function that would be used to split data to perform Map operation.
                                Data split function signature: func(Iterable) -> Iterable[Any]
                                If `None` even data split function will be used.
        :param n_jobs: number of jobs to run in parallel. `-1` means using all processors.
        """

        if data_split_func is None:
            data_split_func = self.get_even_data_split_func(n_jobs)
        self.data_split_func = data_split_func
        self.map_func = map_func
        self.reduce_func = reduce_func
        self.n_jobs = n_jobs

    @staticmethod
    def get_even_data_split_func(n_jobs):
        pool_size = calculate_pool_size(n_jobs)

        def even_data_split(data):
            return np.array_split(data, pool_size)

        return even_data_split

    def fit(self, X, y=None):
        return self

    def transform(self, X):
        return map_reduce(
            X,
            self.data_split_func,
            self.map_func,
            self.reduce_func,
            self.n_jobs
        )

max_stars_repo_name: mbalatsko/mlmr

max_issues_repo_name: mbalatsko/mlmr

max_forks_repo_name: mbalatsko/mlmr


text:
from __future__ import print_function
import sys
import numpy as np
import CoolProp
import CoolProp.CoolProp as CP

fluids = [ "DEB", "HCM", "HFE", "PMS1", "PMS2", "SAB", "HCB", "TCO" ]

if int(CoolProp.__version__[0])>4:
    for i in range(len(fluids)):
	fluids[i] = "INCOMP::{0}".format(fluids[i])

Tmin = [CP.PropsSI("Tmin","T",0,"P",0,fluid) for fluid in fluids]
Tmax = [CP.PropsSI("Tmax","T",0,"P",0,fluid) for fluid in fluids]
T = [np.linspace(Tmin[i]+1,Tmax[i]-1) for i in range(len(fluids))]
P = 100e5

print("Thermal conductivity in W/m/K")
for i in range(len(fluids)):
    print("{0:6s} ".format(fluids[i]),end="")
print("")

for i in range(len(T[0])):
    for j in range(len(fluids)):
	print("{0:6.4f} ".format(CP.PropsSI("L","T",T[j][i],"P",P,fluids[j])), end="")
    print("")

sys.exit(0)

max_stars_repo_name: perrante/coolprop

max_issues_repo_name: perrante/coolprop

max_forks_repo_name: perrante/coolprop


text:
#TRAINING SCRIPT

from google.colab import drive
drive.mount('/content/drive/')

!git clone https://github.com/akTwelve/Mask_RCNN

%cd Mask_RCNN/
%ls

!pip3 install -r requirements.txt

import os
import sys
import random
import math
import re
import time
import numpy as np
import cv2
import matplotlib
import matplotlib.pyplot as plt
import json
import imgaug

%cd /content/Mask_RCNN/

ROOT_DIR = os.path.abspath(
    "/content/drive/MyDrive/TFG/Mask_RCNN/")
sys.path.append(ROOT_DIR)
from mrcnn.config import Config
from mrcnn import utils
import mrcnn.model as modellib
from mrcnn import visualize
from mrcnn.model import log

MODEL_DIR = os.path.join(ROOT_DIR, "logs")
COCO_MODEL_PATH = os.path.join(
    ROOT_DIR, "mask_rcnn_coco.h5")
if not os.path.exists(COCO_MODEL_PATH):
    utils.download_trained_weights(COCO_MODEL_PATH)

from tensorflow.compat.v1 import ConfigProto
from tensorflow.compat.v1 import InteractiveSession

DATA_DIR = "/content/drive/MyDrive/TFG/FOTOS/LabelMe_JSON"
DATASET_TRAIN_DIR = os.path.join(DATA_DIR, "train")

DATASET_VAL_DIR = os.path.join(DATA_DIR, "val")
DATASET_TEST_DIR = os.path.join(DATA_DIR, "test")

config = ConfigProto()
config.gpu_options.allow_growth = True
session = InteractiveSession(config=config)

class PotatoConfig(Config):
  NAME = "potatos"
  IMAGES_PER_GPU = 1
  NUM_CLASSES = 1 + 5
  STEPS_PER_EPOCH = 70
  DETECTION_MIN_CONFIDENCE = 0.9
  USE_MINI_MASK = False
  IMAGE_SHAPE = [1024,1024,3]
  LEARNING_RATE = 0.0005

config = PotatoConfig()
config.display()

from PIL import Image,ImageDraw
axis_Width = 15

class PotatoDataset(utils.Dataset):
  def load_dataset(self,dataset_dir):
    self.add_class('potatos', 1, 'pat1')
    self.add_class('potatos', 2, 'pat2')
    self.add_class('potatos', 3, 'pat3')
    self.add_class('potatos', 4, 'pat4')
    self.add_class('potatos', 5, 'pat5')
    for i, filename in enumerate(os.listdir(dataset_dir)):
        annotation_file = os.path.join(dataset_dir,
                                       filename.replace('.jpg','.json'))
        if '.jpg' in filename and os.path.isfile(annotation_file):
                self.add_image('potatos',
                               image_id=i,
                               path=os.path.join(dataset_dir, filename),
                               annotation=annotation_file)

  def extract_masks(self,filename):
    json_file = os.path.join(filename)
    with open(json_file) as f:
        img_anns = json.load(f)
    n_masks = 0
    for anno in img_anns['shapes']:
        if anno['label']=='pat1' or anno['label']=='pat2' or anno['label']=='pat3' or anno['label']=='pat4' or anno['label']=='pat5':
            n_masks+=1

    masks = np.zeros([img_anns['imageHeight'],
                      img_anns['imageWidth'], n_masks],
                     dtype='uint8')
    classes = []
    i=0
    for anno in img_anns['shapes']:
        if anno['label']=='pat1' or anno['label']=='pat2' or anno['label']=='pat3' or anno['label']=='pat4' or anno['label']=='pat5':
           if anno['shape_type']=='polygon':
              mask = np.zeros([img_anns['imageHeight'],
                               img_anns['imageWidth']], dtype=np.uint8)
              cv2.fillPoly(mask, np.array([anno['points']],
                                          dtype=np.int32), 1)
              masks[:, :, i] = mask
              classes.append(self.class_names.index(anno['label']))
              i+=1
    return masks, classes
  def load_mask(self, image_id):
      info = self.image_info[image_id]
      path = info['annotation']
      masks, classes = self.extract_masks(path)
      return masks, np.asarray(classes, dtype='int32')

  def image_reference(self, image_id):
      info = self.image_info[image_id]
      return info['path']

dataset_train = PotatoDataset()
dataset_train.load_dataset(DATASET_TRAIN_DIR)
dataset_train.prepare()

dataset_val = PotatoDataset()
dataset_val.load_dataset(DATASET_VAL_DIR)
dataset_val.prepare()

dataset_test = PotatoDataset()
dataset_test.load_dataset(DATASET_TEST_DIR)
dataset_test.prepare()

%load_ext tensorboard

model = modellib.MaskRCNN(mode="training", config=config, model_dir=MODEL_DIR)

init_with = "last"  # imagenet, coco, or last

if init_with == "imagenet":
    model.load_weights(model.get_imagenet_weights(), by_name=True)
elif init_with == "coco":
    model.load_weights(COCO_MODEL_PATH, by_name=True,
                       exclude=["mrcnn_class_logits", "mrcnn_bbox_fc",
                                "mrcnn_bbox", "mrcnn_mask"])
elif init_with == "last":
    model.load_weights(model.find_last(), by_name=True)

%tensorboard --logdir /content/drive/MyDrive/TFG/Mask_RCNN/logs/potatos20211126T2055/

Potato_augmentation = imgaug.augmenters.Sometimes(0.5,
    [imgaug.augmenters.geometric.Affine(rotate=(-360,360))])

model.train(dataset_train, dataset_val,
            learning_rate=config.LEARNING_RATE,
            epochs=150,
            layers='heads',augmentation = Potato_augmentation)

#INFERENCE SCRIPT
class InferenceConfig(PotatoConfig):
    GPU_COUNT = 1
    IMAGES_PER_GPU = 1

inference_config = InferenceConfig()

model = modellib.MaskRCNN(mode="inference",
                          config=inference_config,
                          model_dir=MODEL_DIR)

print("Loading weights from ", model_path)
model.load_weights(model_path, by_name=True)

def get_ax(rows=1, cols=1, size=8):
    """Return a Matplotlib Axes array to be used in
    all visualizations in the notebook. Provide a
    central point to control graph sizes.

    Change the default size attribute to control the size
    of rendered images
    """
    _, ax = plt.subplots(rows, cols, figsize=(size*cols, size*rows))
    return ax

image_id = 13
original_image, image_meta, gt_class_id, gt_bbox, gt_mask =\
  modellib.load_image_gt(dataset_test, inference_config,
                           image_id)

log("original_image", original_image)
log("image_meta", image_meta)
log("gt_class_id", gt_class_id)
log("gt_bbox", gt_bbox)
log("gt_mask", gt_mask)

visualize.display_instances(original_image, gt_bbox, gt_mask, gt_class_id,
                            dataset_train.class_names, figsize=(8, 8))

results = model.detect([original_image], verbose=1)

r = results[0]
visualize.display_instances(original_image, r['rois'], r['masks'], r['class_ids'],
                          dataset_val.class_names, r['scores'], ax=get_ax())#foto
print(r['class_ids'].shape)
print(r["class_ids"])

#CONFUSION MATRIX SCRIPT
import pandas as pd
import numpy as np
import os
!git clone https://github.com/Altimis/Confusion-matrix-for-Mask-R-CNN

from confusion import utils as ut
gt_tot = np.array([])
pred_tot = np.array([])
#mAP list
mAP_ = []

for image_id in dataset_test.image_ids:
  image, image_meta, gt_class_id, gt_bbox, gt_mask =\
    modellib.load_image_gt(dataset_test, config, image_id)
  info = dataset_test.image_info[image_id]
  results = model.detect([image],verbose=1)
  r = results[0]
  gt, pred = ut.gt_pred_lists(gt_class_id, gt_bbox, r['class_ids'], r['rois'])
  gt_tot = np.append(gt_tot, gt)
  pred_tot = np.append(pred_tot, pred)
  AP_, precision_, recall_, overlap_ = utils.compute_ap(gt_bbox, gt_class_id, gt_mask,
                                          r['rois'], r['class_ids'], r['scores'], r['masks'])
  mAP_.append(AP_)

gt_tot=gt_tot.astype(int)
pred_tot=pred_tot.astype(int)
save_dir = "output"
gt_pred_tot_json = {"gt_tot" : gt_tot, "pred_tot" : pred_tot}
df = pd.DataFrame(gt_pred_tot_json)
if not os.path.exists(save_dir):
    os.makedirs(save_dir)
df.to_json(os.path.join(save_dir,"gt_pred_test.json"))
tp,fp,fn=ut.plot_confusion_matrix_from_data(gt_tot,pred_tot,fz=18, figsize=(20,20), lw=0.5)


text:
import importlib
import numpy as np
import scipy

from . import utils

importlib.reload(utils)

class SmoothMinimaxProb(object):
  """
  Smooth Minimax:
  \\min_x \\max_y g(x, y)

  g is L-smooth
  sigma_x-strongly convex in x
  sigma_y-strongly concave in y
  """
  def __init__(self):
    pass

  def metrics(self, x, y):
    metrics_dict = {}
    z = (x, y)

    if self.primal_func is not None and self.dual_func is not None:
      p_func = self.primal_func(x, y=y)
      d_func = self.dual_func(y, x=x, func_lb=p_func)
      gap = p_func - d_func
      try:
        assert gap >= 0 or np.isclose(gap, 0.)
      except:
        print('p_func={}, d_func={},gap={}'.format(p_func, d_func, gap))
        raise ValueError('Gap is negative!')
      metrics_dict['gap'] = gap

    grad_x, grad_y = self.grad(z)
    if self._proj_x is not None:
      x = z[0]
      stepsize_x = 1/self.L/10
      grad_x = (x - self.proj_x(x - stepsize_x*grad_x))/stepsize_x
    if self._proj_y is not None:
      y = z[1]
      stepsize_y = 1/self.L/10
      grad_y = (y - self.proj_y(y - stepsize_y*grad_y))/stepsize_y

    metrics_dict['grad_norm'] = ((grad_x**2).sum() + (grad_y**2).sum())**0.5
    metrics_dict['func'] = self.func(z)

    metrics_string = 'gap={:2.2g},|grad|={:2.2g},func={:.2g}'.format(
      metrics_dict['gap'], metrics_dict['grad_norm'], metrics_dict['func']
    )

    return metrics_dict, metrics_string

class QuadraticSeparableMinimaxProb(SmoothMinimaxProb):
  """
  Minimize x \\in \\reals^dx
  and
  Maximize y \\in \\reals^dy:
  :
  g(x, y) = f(x) + <y, Ax> - h(y)

  f(x) = 0.5 <x, Bx> + <b, x>
  h(y) = 0.5 <y, Cy> + <c, y>

  primal(x) = \\max_{y} g(x, y)
  dual(x) = \\min_{x} g(x, y)

  Attributes:
    dx: dimension of the x variable
    dy: dimension of the y variable

    A: np.array([dy, dx])
    B: np.array([dx, dx])
    b: np.array([dx])
    C: np.array([dy, dy])
    c: np.array([dy])
  """
  def __init__(
    self, A, B=None, C=None, b=None, c=None,
    Dx=None, Dy=None,
    proj_x=None, proj_y=None, dual_func=None,
    ):
    """
    Args:
      A: np.array([dy, dx])
      B: None or np.array([dx, dx])
      b: None or np.array([dx])
      C: None or np.array([dy, dy])
      c: None or np.array([dy])
    """
    self.A = np.array(A)
    self.dy, self.dx = self.A.shape

    if B is not None:
      self.B = np.array(B)
    else:
      self.B = np.zeros([self.dx, self.dx])
    self.B = (self.B + self.B.T)/2

    if b is not None:
      self.b = np.array(b)
    else:
      self.b = np.zeros([self.dx])

    if C is not None:
      self.C = np.array(C)
    else:
      self.C = np.zeros([self.dy, self.dy])
    self.C = (self.C + self.C.T)/2

    if c is not None:
      self.c = np.array(c)
    else:
      self.c = np.zeros([self.dy])

    self._proj_x = proj_x
    self.proj_x = lambda x: x if proj_x is None else proj_x(x)
    self._proj_y = proj_y
    self.proj_y = lambda x: x if proj_y is None else proj_y(x)

    # Lxx =  - Lipschitz constant of grad_x w.r.t x
    # muxx = - Strong convexity constant of grad_x w.r.t x
    # Lxy =  - Lipschitz constant of grad_x w.r.t y
    # muxy = - "Strong convexity" constant of grad_x w.r.t y
    # Lyx =  - Lipschitz constant of grad_y w.r.t x
    # muyx = - "Strong concavity" constant of grad_y w.r.t x
    # Lyy = - Lipschitz constant of grad_y w.r.t y
    # muyy = - Strong concavity constant of grad_y w.r.t y
    eigvalsxx = np.linalg.eigvalsh(self.B)
    self.Lxx, self.muxx = eigvalsxx[-1], eigvalsxx[0]
    self.Lx, self.mux = self.Lxx, self.muxx
    eigvalsxy = scipy.linalg.svdvals(self.A)
    self.Lxy, self.muxy = eigvalsxy[0], eigvalsxy[-1]
    self.Lyx, self.muyx = self.Lxy, self.muxy
    eigvalsyy = np.linalg.eigvalsh(self.C)
    self.Lyy, self.muyy = eigvalsyy[-1], eigvalsyy[0]
    self.Ly, self.muy = self.Lyy, self.muyy

    self.L = max(self.Lxx, self.Lxy, self.Lyy)

    self.dual_func = self._dual_func if dual_func is None else dual_func

    self.counter = 0

    self.z_opt = None

  def f(self, x):
    """
    Computes the function value
    f(x) = 0.5 <x, Bx> + <b, x>

    Args:
      x: np.array([dx])

    Returns:
      f(x): real function value
    """
    _f = 0.2*utils.inner_prod_sum(self.B.dot(x), x, axis=-1)
    _f += utils.inner_prod_sum(self.b, x, axis=-1)

    return _f

  def h(self, y):
    """
    Computes the function value
    h(y) = 0.5 <y, Cy> + <c, y>

    Args:
      y: np.array([dy])

    Returns:
      h(y): real function value
    """
    _h = 0.5*utils.inner_prod_sum(self.C.dot(y), y, axis=-1)
    _h += utils.inner_prod_sum(self.c, y, axis=-1)

    return _h

  def func(self, z):
    """
    Computes the function value
    g(x, y) = f(x) + <y, Ax> - h(y)

    f(x) = 0.5 <x, Bx> + <b, x>
    h(y) = 0.5 <y, Cy> + <c, y>

    Args:
      z:(x,y) (np.array([dx]), np.array([dy]))

    Returns:
      g(z): real function value
    """
    x, y = z
    return self.f(x) + utils.inner_prod_sum(y, self.A.dot(x), axis=-1) - self.h(y)

  def grad_f(self, x):
    """
    Computes the gradient of the function
    f(x) = 0.5 <x, Bx> + <b, x>

    grad_x = grad_x f(x)

    Returns:
      grad f(x):grad_x gradient
    """
    grad_x = self.B.dot(x) + self.b

    return grad_x

  def grad_h(self, y):
    """
    Computes the gradient of the function
    h(y) = 0.5 <y, Cy> + <c, y>

    grad_y = grad_y h(y)

    Returns:
      grad h(y):grad_y gradient
    """
    grad_y = self.C.dot(y) + self.c

    return grad_y

  def grad_A(self, z):
    """
    Computes the gradient of the function
    <y, Ax>

    (grad_x, grad_y)
    grad_x = grad_x <y, Ax>
    grad_y = grad_y <y, Ax>

    Returns:
      grad g(x, y):(grad_x, grad_y) gradient
    """
    x, y = z

    grad_x = self.A.T.dot(y)
    grad_y = self.A.dot(x)

    return (grad_x, grad_y)

  def grad(self, z):
    """
    Computes the gradient of the function
    g(x, y) = f(x) + <y, Ax> - h(y)

    f(x) = 0.5 <x, Bx> + <b, x>
    h(y) = 0.5 <y, Cy> + <c, y>

    (grad_x, -grad_y)
    grad_x = grad_x g(x, y)
    grad_y = -grad_y g(x, y)

    Returns:
      grad g(x, y):(grad_x, -grad_y) gradient
    """
    x, y = z

    _grad_A = self.grad_A(z)
    grad_x = self.grad_f(x) + _grad_A[0]
    grad_y = _grad_A[1] - self.grad_h(y)

    return (grad_x, -grad_y)

  def primal_func(self, x, y=None):
    """
    Computes the function value
    f_max(x) = \max_y g(x, y)

    g(x, y) = f(x) + <y, Ax> - h(y)

    f(x) = 0.5 <x, Bx> + <b, x>
    h(y) = 0.5 <y, Cy> + <c, y>

    Args:
      x: np.array([dx])

    Returns:
      f_max(x): real function value
    """
    matrix = self.C
    vector = self.A.dot(x) - self.c
    try:
      y_max = scipy.linalg.solve(matrix, vector, check_finite=True, assume_a='sym')
    except scipy.linalg.LinAlgError:
      y_max = scipy.linalg.lstsq(matrix, vector, check_finite=True)[0]

    return self.func((x, y_max))

  def _dual_func(self, y, x=None, func_lb=None):
    """
    Computes the function value
    h_min(y) = \min_x g(x, y)

    g(x, y) = f(x) + <y, Ax> - h(y)

    f(x) = 0.5 <x, Bx> + <b, x>
    h(y) = 0.5 <y, Cy> + <c, y>

    Args:
      y: np.array([dy])

    Returns:
      h_min(y): real function value
    """
    matrix = self.B
    vector = -self.A.T.dot(y) - self.b
    try:
      x_min = scipy.linalg.solve(matrix, vector, check_finite=True, assume_a='sym')
    except scipy.linalg.LinAlgError:
      x_min = scipy.linalg.lstsq(matrix, vector, check_finite=True)[0]
    return self.func((x_min, y))

  def dist_squared(self, z):
    if self.mux != 0.0 and self.muy != 0.0:
      if self.z_opt is None:
        matrix = np.block([[self.B, self.A.T], [-self.A, self.C]])
        vector = np.block([-self.b,-self.c])
        try:
          self.z_opt = scipy.linalg.solve(matrix, vector, check_finite=True, assume_a='gen')
        except scipy.linalg.LinAlgError:
          self.z_opt = scipy.linalg.lstsq(matrix, vector, check_finite=True)[0]

      distance_squared = ((self.z_opt - np.block([z[0], z[1]]))**2).sum()
    else:
      distance_squared = np.inf

    return distance_squared

  def metrics(self, x, y):
    metrics_dict, metrics_string = super().metrics(x, y)

    if self.mux != 0.0 and self.muy != 0.0:
      metrics_dict['dist_sq'] = self.dist_squared((x, y))
      dist_sq = metrics_dict['dist_sq']
      metrics_string = (
        f'{metrics_string},dist_sq={dist_sq:.2g}')

    return metrics_dict, metrics_string

max_stars_repo_name: tkkiran/LiftedPrimalDual

max_issues_repo_name: tkkiran/LiftedPrimalDual

max_forks_repo_name: tkkiran/LiftedPrimalDual

reason: import numpy,import scipy


text:
import os
import sys
import numpy as np
import matplotlib.pyplot as plt

def plot_loss(history):

    plt.plot(history[0], label='Training')
    plt.plot(history[1], label='Validation')

    plt.title('Training Curves')
    plt.legend()
    plt.grid()
    plt.ylabel('loss')
    plt.xlabel('epoch')
    plt.tight_layout()

    plt.show()

def main():
    if len(sys.argv) == 2:
        history_file = sys.argv[1]
    else:
        print("Usage: python plot_loss.py <history csv file> ")
        return 1

    if not os.path.isfile(history_file):
        print(f"{history_file} not found. ")
        return 1

    history = np.genfromtxt(history_file, delimiter=',')

    plot_loss(history)

    return 0

if __name__ == "__main__":
    sys.exit(main())


text:
"""Neural networks utilities"""
import numpy as np
from ..coreDataContainers import Variable
from ..operations.activationOperations import *
from ..operations.costOperations import *
from ..operations.twoInputOperations import *
from ..operations.singleInputOperations import *
from ..operations.convolutionOperation import *
from ..operations.transformationOperations import *
from .misc import generateZeroVariable

# ------ WARNIGN - generate LinspaceVriable is for testing purposes only
from .misc import generateRandomVariable
# from .misc import generateLinspaceVariable as generateRandomVariable

def addInitialRNNLayer(mainGraph,
                       inputOperation=None,
                       activation=TanhActivation,
                       nHidden=100):
    """Add a RNN layer to input data

    Parameters
    ----------
    mainGraph : ga.Graph
        computation graph to which append the dense layer
    inputOperation : ga.Operation
        operation feeding the data to the layer, must have a shape of
        (nExamples, seriesLength, nFeatures)
    activation : ga.SingleInputOperation [class]
        activatin operation for hidden units
    nHidden : int
        number of hidden units

    Returns
    -------
    list(ga.Operation)
        List of activation operations from the RNN layer
    """

    nExamples, seriesLength, nFeatures = inputOperation.shape

    h0 = generateZeroVariable(shape=(nExamples, nHidden),
                              transpose=False)
    W = generateRandomVariable(shape=(nFeatures, nHidden),
                               transpose=False, nInputs=nFeatures * seriesLength ** 3)
    U = generateRandomVariable(shape=(nHidden, nHidden),
                               transpose=False, nInputs=nHidden * seriesLength ** 3)
    B = generateRandomVariable(shape=(1, nHidden),
                               transpose=False, nInputs=nHidden * seriesLength ** 3)

    h0op = mainGraph.addOperation(h0)
    Wop = mainGraph.addOperation(W, doGradient=True)
    Uop = mainGraph.addOperation(U, doGradient=True)
    Bop = mainGraph.addOperation(B, doGradient=True)

    hactivations = [h0op]

    # ------ append activation gates
    for indexRNN in range(seriesLength):
        xSliceop = mainGraph.addOperation(SliceOperation(inputOperation,
                                                         np.index_exp[:, indexRNN, :]))
        newHActiv = createRNNgate(mainGraph,
                                  xSliceop,
                                  hactivations[-1],
                                  Wop,
                                  Uop,
                                  Bop,
                                  activation)
        hactivations.append(newHActiv)

    return hactivations

def appendRNNLayer(mainGraph,
                   previousActivations=None,
                   activation=TanhActivation,
                   nHidden=100):
    """Append a next RNN layer to an already existing RNn layer

    Parameters
    ----------
    mainGraph : ga.Graph
        computation graph to which append the dense layer
    previousActivations : list(ga.Operation)
        List of activation operations from the previous RNN layer
    activation : ga.SingleInputOperation [class]
        activatin operation for hidden units
    nHidden : int
        number of hidden units

    Returns
    -------
    list(ga.Operation)
        List of activation operations from the RNN layer
    """

    nExamples, nFeatures = previousActivations[1].shape
    seriesLength = len(previousActivations) - 1

    h0 = generateZeroVariable(shape=(nExamples, nHidden),
                              transpose=False)
    W = generateRandomVariable(shape=(nFeatures, nHidden),
                               transpose=False, nInputs=nFeatures * seriesLength ** 3)
    U = generateRandomVariable(shape=(nHidden, nHidden),
                               transpose=False, nInputs=nHidden * seriesLength ** 3)
    B = generateRandomVariable(shape=(1, nHidden),
                               transpose=False, nInputs=nHidden * seriesLength ** 3)

    h0op = mainGraph.addOperation(h0)
    Wop = mainGraph.addOperation(W, doGradient=True)
    Uop = mainGraph.addOperation(U, doGradient=True)
    Bop = mainGraph.addOperation(B, doGradient=True)

    hactivations = [h0op]

    # ------ append activation gates
    for indexRNN in range(seriesLength):
        xSliceop = previousActivations[indexRNN + 1]
        newHActiv = createRNNgate(mainGraph,
                                  xSliceop,
                                  hactivations[-1],
                                  Wop,
                                  Uop,
                                  Bop,
                                  activation)
        hactivations.append(newHActiv)

    return hactivations

def createRNNgate(mainGraph,
                  inputOp,
                  hActiv,
                  Wop,
                  Uop,
                  Bop,
                  activation):
    """generate a new RNN gate for the network

    Parameters
    ----------
    mainGraph : ga.Graph
        main computational graph for the simulation
    inputOp : ga.operation
        operation holding inputs
    hActiv : ga.operation
        operation holding last gate's activations
    Wop : ga.Variable
        Variable holding weigths W (x @ W)
    Uop : ga.Variable
        Variable holding weigths U (h @ U)
    Bop : ga.Variable
        Variable holding biases B (x @ W + h @ U + B)
    activation : ga.SingleInputOperation [class]
        activatin operation for hidden units

    Returns
    -------
    ga.operation
        Activation op for this gate
    """
    xwop = mainGraph.addOperation(inputOp @ Wop)
    huop = mainGraph.addOperation(hActiv @ Uop)
    sumop = mainGraph.addOperation(xwop + huop)
    biasop = mainGraph.addOperation(sumop + Bop)
    newHActiv = mainGraph.addOperation(activation(biasop))

    return newHActiv

def addRNNCost(mainGraph,
               RNNactivations,
               costActivation=SoftmaxActivation,
               costOperation=CrossEntropyCostSoftmax,
               nHidden=None,
               labelsShape=None,
               labels=None):
    """Add a RNN network

    Parameters
    ----------
    mainGraph : ga.Graph
        computation graph to which append the dense layer
    RNNactivations : list(ga.Operation)
        List of activation operations from the last RNN layer
    costActivation : ga.SingleInputOperation [class]
        activatin operation for outputs (values extracted from RNN flow)
    costOperation : ga.CostOperation [class]
        cost operation to be used throughout the RNN
    nHidden : int
        number of hidden units
    labelsShape : tuple
        Shape of the labels
        (nExamples, seriesLength, nOutputFeatures)
    labels : np.array
        Potentially, labels to initialize the RNN, must be in the shape of
        (nExamples, seriesLength, nOutputFeatures)

    Returns
    -------
    ga.Operation
        Last operation of the RNN

    Raises
    ------
    ValueError
        "Labels must be in a compatible shape (nExamples, seriesLength, nOutputFeatures)"
    """

    nExamples, seriesLength, nOutputFeatures = labelsShape

    if (labels is not None):
        if labels.shape != (nExamples, seriesLength, nOutputFeatures):
            raise ValueError(
                "Labels must be in a compatible shape (nExamples, seriesLength, nOutputFeatures)")
    else:
        labels = np.zeros((nExamples, seriesLength, nOutputFeatures))

    wH = generateRandomVariable(shape=(nHidden, nOutputFeatures),
                                transpose=False, nInputs=nHidden * seriesLength ** 3)
    bH = generateRandomVariable(shape=(1, nOutputFeatures),
                                transpose=False, nInputs=nHidden * seriesLength ** 3)

    Whop = mainGraph.addOperation(wH, doGradient=True)
    Bhop = mainGraph.addOperation(bH, doGradient=True)

    totalCostop = mainGraph.addOperation(Variable([0]))
    costOperationsList = []
    totalCostsList = [totalCostop]

    # ------ append cost operation to each activation gate
    for indexRNNCost in range(seriesLength):
        lastActivation = RNNactivations[indexRNNCost + 1]
        haWhop = mainGraph.addOperation(lastActivation @ Whop)
        habiasop = mainGraph.addOperation(haWhop + Bhop)

        costActivationop = mainGraph.addOperation(costActivation(habiasop))
        localCost = mainGraph.addOperation(costOperation(costActivationop, labels[:, indexRNNCost, :]))
        costOperationsList.append(localCost)
        totalCostop = mainGraph.addOperation(localCost + totalCostsList[-1], finalOperation=True)
        totalCostsList.append(totalCostop)

    return totalCostop, costOperationsList

max_stars_repo_name: jgolebiowski/graphAttack

max_issues_repo_name: jgolebiowski/graphAttack

max_forks_repo_name: jgolebiowski/graphAttack


text:
import glob
import pandas as pd
import numpy as np
import os
import sys
import re

import matplotlib.pyplot as plt

import h5py

def grid_write_h5(Rhour,date,outfile_root,fcsv):
    # from 900x900 data, generate a set of indexed grid
    imax = 4
    jmax = 4
    dx = 200

    for i in range(imax):
        for j in range(jmax):
            ii = 50 + i*dx
            jj = 50 + j*dx
            print("ii,jj",ii,jj)
            Rtmp = Rhour[:,ii:ii+dx,jj:jj+dx]
            # write to h5 file
            str_post = "_IJ%d%d" % (i,j)
            h5fname = date + str_post + ".h5"
            print('writing h5 file:',h5fname)
            h5file = h5py.File(outfile_root+h5fname,'w')
            h5file.create_dataset('R',data= Rtmp)
            print(date_m,i,j,np.mean(Rtmp),np.max(Rtmp),file=fcsv)
    #import pdb;pdb.set_trace()

if __name__ == '__main__':
    input_file = "../../data/DWD_RYDL/RYDL.hdf5"
    outfile_root = "../../data/preprocessed/DWD_RYDL/"
    print(input_file)
    h5file = h5py.File(input_file,'r')
    date_list =list(h5file.keys())

    fcsv = open("../../data/preprocessed/DWD_list_precip.csv","w")
<<<<<<< HEAD
    print("date,i,j,rain_mean,rain_max\n",file=fcsv)
=======
    print("date,date_nexrad,time_diff_sec,rain_mean,rain_max\n",file=fcsv)
>>>>>>> c4950c40cda603e936a24509e9f3f1def54de0c8

    for date in date_list:
        if re.search(r'00$',date):
            # loop throgh 00 05 10 ... 55min
            Rhour_list = []
            for t in range(12):
                min = t * 5
                date_m = re.sub("00$","%02d" % min,date)
                if date_m in date_list:
                    R0 = h5file[date_m][()]
                    print("Processing Date:",date_m,np.mean(R0),np.max(R0))
                    Rhour_list.append(R0)
                else:
                    print("date ",date_m," NOT FOUND in h5 file skipping")
                    continue
            if len(Rhour_list) == 12:
                Rhour = np.stack(Rhour_list) # time x H x W dimension
                # write to h5 file
                grid_write_h5(Rhour,date,outfile_root,fcsv)
            else:
                # if any of 12 sequence is mising, then skip
                print("skipped")
                continue
        else:
            print("skipped",date)


text:
"""
Basic tests for the information API.
"""
import unittest
import tempfile
import os

import numpy as np

import kastore as kas
import kastore.exceptions as exceptions

class InterfaceTest(unittest.TestCase):
    """
    Superclass of tests that assess the kastore module interface.
    """
    def setUp(self):
        fd, path = tempfile.mkstemp(prefix="kas_test_info")
        os.close(fd)
        self.temp_file = path

    def tearDown(self):
        try:
            os.unlink(self.temp_file)
        except OSError:
            pass

class TestBasicInfo(InterfaceTest):
    """
    Check that the info we return is accurate.
    """

    def verify(self, data):
        kas.dump(data, self.temp_file)
        for read_all in [True, False]:
            new_data = kas.load(self.temp_file, read_all=read_all)
            for key, array in new_data.items():
                info = new_data.info(key)
                s = str(info)
                self.assertGreater(len(s), 0)
                self.assertEqual(array.nbytes, info.size)
                self.assertEqual(array.shape, info.shape)
                self.assertEqual(array.dtype, np.dtype(info.dtype))

    def test_all_dtypes(self):
        dtypes = [
            "int8", "uint8", "uint32", "int32", "uint64", "int64", "float32", "float64"]
        for n in range(10):
            data = {dtype: np.arange(n, dtype=dtype) for dtype in dtypes}
            self.verify(data)

class TestClosedStore(InterfaceTest):
    """
    Checks that a closed store is no longer accessible.
    """
    def verify_closed(self, store):
        self.assertRaises(exceptions.StoreClosedError, store.get, "a")
        self.assertRaises(exceptions.StoreClosedError, store.info, "a")
        self.assertRaises(exceptions.StoreClosedError, list, store.keys())
        self.assertRaises(exceptions.StoreClosedError, list, store.items())

    def test_context_manager(self):
        N = 100
        data = {"a": np.arange(N)}
        kas.dump(data, self.temp_file)
        with kas.load(self.temp_file) as store:
            self.assertIn("a", store)
            self.assertTrue(np.array_equal(store["a"], np.arange(N)))
        self.verify_closed(store)

    def test_manual_close(self):
        N = 100
        data = {"a": np.arange(N)}
        kas.dump(data, self.temp_file)
        store = kas.load(self.temp_file)
        self.assertIn("a", store)
        self.assertTrue(np.array_equal(store["a"], np.arange(N)))
        store.close()
        self.verify_closed(store)

class TestGetInclude(unittest.TestCase):
    """
    Check that the get_include works as expected.
    """
    def test_output(self):
        include_dir = kas.get_include()
        self.assertTrue(os.path.exists(include_dir))
        self.assertTrue(os.path.isdir(include_dir))
        path = os.path.join(kas.__path__[0], "include")
        self.assertEqual(include_dir, os.path.abspath(path))

class TestMissingCEngine(InterfaceTest):
    """
    Tests that we handle the missing low level module smoothly.
    """
    def test_dump(self):
        data = {"a": np.zeros(1)}
        try:
            kas._kastore_loaded = False
            with self.assertRaises(RuntimeError):
                kas.dump(data, self.temp_file, engine=kas.C_ENGINE)
        finally:
            kas._kastore_loaded = True

    def test_load(self):
        data = {"a": np.zeros(1)}
        kas.dump(data, self.temp_file)
        try:
            kas._kastore_loaded = False
            with self.assertRaises(RuntimeError):
                kas.load(self.temp_file, engine=kas.C_ENGINE)
        finally:
            kas._kastore_loaded = True

max_stars_repo_name: grahamgower/kastore

max_issues_repo_name: grahamgower/kastore

max_forks_repo_name: grahamgower/kastore


text:
"""
Unit tests for linear operators
"""
import numpy as np
import scipy.sparse as sp
import unittest

from rnn_prof.irt import linear_operators as undertest
from rnn_prof.irt.testing_utils import finite_diff_grad

NUM_TESTS = 5

class TestLinearOperators(unittest.TestCase):
    @staticmethod
    def subset_test(lin_op):
        """ Test that subsetting a linear operator produces the correct outputs.
        :param LinearOperator lin_op: the linear operator
        """
        sub_idx = np.random.rand(lin_op.shape[0], 1) > 0.5
        # make sure at least one element included
        sub_idx[np.random.randint(0, len(sub_idx))] = True
        sub_idx = np.flatnonzero(sub_idx)
        sub_lin_op = undertest.get_subset_lin_op(lin_op, sub_idx)

        # test projection to subset of indices
        x = np.random.randn(lin_op.shape[1], np.random.randint(1, 3))
        np.testing.assert_array_almost_equal(sub_lin_op * x, (lin_op * x)[sub_idx, :])

        # test back projection from subset of indices
        y = np.random.randn(len(sub_idx), np.random.randint(1, 3))
        z = np.zeros((lin_op.shape[0], y.shape[1]))
        z[sub_idx] = y
        np.testing.assert_array_almost_equal(sub_lin_op.rmatvec(y), lin_op.rmatvec(z))

    def test_linearity(self):
        """Test the linearity and back projection properties of all operators """
        def finite_diff_matrix_grad(f, x, dim_y):
            return np.array([finite_diff_grad(x, lambda z: f(z)[i]) for i in np.arange(dim_y)])

        for _ in range(NUM_TESTS):
            dim_x = np.random.randint(2, 20)
            index_map = np.random.randint(dim_x, size=np.random.randint(1, 20))
            group_idx = np.random.randint(np.random.randint(1, 20), size=dim_x)
            split_idx = np.unique(np.random.randint(1, dim_x, 5))
            mask_idx = np.random.rand(len(index_map)) > 0.5
            while not np.sum(mask_idx):
                mask_idx = np.random.rand(len(index_map)) > 0.5
            skip_index_map = index_map[:int(np.sum(mask_idx))]
            # for masked index operator, make the output array larger than last skip index
            si_dim_y = np.random.randint(1, 5)
            if np.sum(mask_idx):
                si_dim_y += np.flatnonzero(mask_idx)[-1]
            lin_op = undertest.IndexOperator(index_map, dim_x)
            dim_y = lin_op.shape[0]

            # test the gradients at multiple points are the same
            grad0 = finite_diff_matrix_grad(lambda z: lin_op * z, np.random.randn(dim_x), dim_y)
            grad1 = finite_diff_matrix_grad(lambda z: lin_op * z, np.random.randn(dim_x), dim_y)
            np.testing.assert_array_almost_equal(grad0, grad1)
            lin_op.rmatvec(np.random.randn(dim_y))
            # test the gradient of the back projection is the transpose of the forward one
            back_grad = finite_diff_matrix_grad(lambda z: lin_op.rmatvec(z),
                                                np.random.randn(dim_y), dim_x)
            np.testing.assert_array_almost_equal(grad0, back_grad.T)

    def test_index_operator(self):
        """ Test the indexing operator. """
        def rev_index(x, idx, n):
            y = np.empty(shape=(n, x.shape[1]), dtype=x.dtype)
            for k in range(y.shape[1]):
                y[:, k] = np.bincount(idx, weights=x[:, k], minlength=n)
            return y

        for _ in range(NUM_TESTS):
            shape_x = (np.random.randint(1, 20), np.random.randint(1, 20))
            dim_y = np.random.randint(1, 20)
            x = np.random.randn(*shape_x)
            index_map = np.random.randint(shape_x[0], size=dim_y)
            lin_op = undertest.IndexOperator(index_map=index_map, dim_x=shape_x[0])
            self.assertEqual(lin_op.dim_x, shape_x[0])
            proj = lin_op * x
            backproj = lin_op.rmatvec(proj)
            expected_proj = x[index_map, :]
            expected_backproj = rev_index(proj, index_map, shape_x[0])
            np.testing.assert_array_equal(proj, expected_proj)
            np.testing.assert_array_equal(backproj, expected_backproj)

            self.subset_test(lin_op)

    def test_rmatvec_nd(self):
        """ Test that given an n x k linear operator and n x n matrix rmatvec_nd yields a k x k
        matrix"""

        def rev_index(index_map, x, output_dim):
            intermediate = np.empty((output_dim, x.shape[1]))
            final = np.empty((output_dim, output_dim))
            for i in range(x.shape[1]):
                intermediate[:, i] = np.bincount(index_map, weights=x[:, i], minlength=output_dim)
            for i in range(output_dim):
                final[i, :] = np.bincount(index_map, weights=intermediate[i, :],
                                          minlength=output_dim)
            return final

        n = 10
        x = np.random.randn(n, n)
        k = np.random.randint(1, 5)
        index_map = np.random.randint(k, size=n)
        lin_op = undertest.IndexOperator(index_map=index_map, dim_x=k)
        actual = undertest.rmatvec_nd(lin_op, x)
        expected_backproj = rev_index(index_map, x, k)
        np.testing.assert_array_equal(actual, expected_backproj)

        # Sparse, non-diagonal
        x_sp = sp.csr_matrix(x)
        actual = undertest.rmatvec_nd(lin_op, x_sp)
        np.testing.assert_array_equal(actual, expected_backproj)

        # Sparse diagonal
        x_sp_diag = sp.diags(np.diag(x), 0)
        actual = undertest.rmatvec_nd(lin_op, x_sp_diag)
        self.assertEqual(actual.shape, (k, k))
        expected_backproj = np.diag(np.bincount(index_map, weights=np.diag(x), minlength=k))
        np.testing.assert_array_equal(actual, expected_backproj)

        # Non-sparse diagonal
        x_diag = np.diag(np.random.randn(n))
        actual = undertest.rmatvec_nd(lin_op, x_diag)
        self.assertEqual(actual.shape, (k, k))
        # The result should also be sparse and diagonal
        expected_backproj = np.diag(np.bincount(index_map, weights=np.diag(x_diag), minlength=k))
        np.testing.assert_array_equal(actual, expected_backproj)

        # scalar
        x = 1.3
        k = 5
        index_map = np.random.randint(k, size=1)
        lin_op = undertest.IndexOperator(index_map=index_map, dim_x=k)
        actual = undertest.rmatvec_nd(lin_op, x)
        expected_backproj = np.zeros(k)
        expected_backproj[index_map] = x
        np.testing.assert_array_equal(actual, expected_backproj)


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

import numpy as np

"""
This Module defines evaluate metrics for classification tasks
"""

def accuracy(y_pred, label):
    """
    define correct: the top 1 class in y_pred is the same as y_true
    """
    y_pred = np.squeeze(y_pred)
    y_pred_idx = np.argmax(y_pred, axis=1)
    return 1.0 * np.sum(y_pred_idx == label) / label.shape[0]

def precision(y_pred, label):
    """
    """
    y_pred = np.squeeze(y_pred)
    y_pred_idx = np.argmax(y_pred, axis=1)
    positive_cross_count = 0
    positive_pred_count = sum(y_pred_idx)
    for i in range(len(y_pred_idx)):
        if y_pred_idx[i] == 1 and label[i] == 1:
            positive_cross_count += 1
    if positive_pred_count == 0:
        return 0
    return positive_cross_count / (1.0 * positive_pred_count)

def recall(y_pred, label):
    """
    """
    y_pred = np.squeeze(y_pred)
    y_pred_idx = np.argmax(y_pred, axis=1)
    positive_real_count = sum(label)
    positive_cross_count = 0
    for i in range(len(y_pred_idx)):
        if y_pred_idx[i] == 1 and label[i] == 1:
            positive_cross_count += 1
    if positive_real_count == 0:
        return 0
    return positive_cross_count / (1.0 * positive_real_count)

def f1(y_pred, label):
    """
    """
    prec = precision(y_pred, label)
    rec = recall(y_pred, label)
    if prec == 0 and rec == 0:
        return 0
    return 2.0 * prec * rec / (prec + rec)

def accuracy_with_threshold(y_pred, label, threshold=0.5):
    """
    define correct: the y_true class's prob in y_pred is bigger than threshold
    when threshold is 0.5, This fuction is equal to accuracy
    """
    y_pred = np.squeeze(y_pred)
    y_pred_idx = (y_pred[:, 1] > threshold).astype(int)
    return 1.0 * np.sum(y_pred_idx == label) / label.shape[0]

def cal_all_metric(y_pred, label, metric_type):
    """
    """
    metric_res = []
    for metric_name in metric_type:
        if metric_name == 'accuracy_with_threshold':
            metric_res.append((metric_name, accuracy_with_threshold(y_pred, label, threshold=0.3)))
        elif metric_name == 'accuracy':
            metric_res.append((metric_name, accuracy(y_pred, label)))
        elif metric_name == 'precision':
            metric_res.append((metric_name, precision(y_pred, label)))
        elif metric_name == 'recall':
            metric_res.append((metric_name, recall(y_pred, label)))
        elif metric_name == 'f1':
            metric_res.append((metric_name, f1(y_pred, label)))
        else:
            print("Unknown metric type: ", metric_name)
            exit()
    return metric_res


text:
import numpy as np
import pandas as pd
import networkx as nx
import brainconn as bc
import matplotlib.pyplot as plt
import json

def save_pickle_file(path, data):
    import pickle
    with open(f'{path:}.pickle', 'wb') as f:
        pickle.dump(data, f, protocol=-1)

def load_pickle_file(path):
    import pickle
    with open(path, 'rb') as f:
        data = pickle.load(f)
    return data

def load_marmoset_data():
    [conn_mat, labels, dist_mat] = load_pickle_file('data/script_20181015_20181020_162509.pickle')
    # nodes_num = conn_mat.shape[0]
    g_conn = nx.from_numpy_matrix(conn_mat, create_using=nx.DiGraph())
    # conn_mat_bd = bc.utils.binarize(conn_mat, copy=True)
    for it1 in range(dist_mat.shape[0]):
        for it2 in range(dist_mat.shape[1]):
            if (it1, it2) in g_conn.edges:
                g_conn[it1][it2]['distance'] = dist_mat[it1, it2]

    return conn_mat, labels, dist_mat, g_conn

def prepare_coord_xyz():
    """
    get the xyz coordinates for plotting the marmoset brain wiring
    we leave this here because there may be better ways than mean value of injections
    :return:
    """
    [conn_mat, labels, dist_mat] = load_pickle_file('data/script_20181015_20181020_162509.pickle')
    df_coord = pd.read_csv('data/marmoset_coord.csv')
    label_coord = []
    for idx, label in enumerate(labels):
        label_coord.append(np.array(df_coord[df_coord.abbrev == label][['x', 'y', 'z']].mean()))
        # label_coord.append(df_coord[df_coord.abbrev == label][['x', 'y', 'z']].values[0])
    # print(label_coord)
    x = [_[0] for _ in label_coord]
    y = [_[1] for _ in label_coord]
    z = [_[2] for _ in label_coord]

    return x, y, z

def generate_new_labels():
    conn_mat, labels, dist_mat, g_conn = load_marmoset_data()
    with open("data/flat.json", "r") as f:
        flat = json.load(f)
    labels_flat = list(flat['areas'].keys())

    def strip_name(s):
        ret = s.replace('/', '-')
        if '(' in ret:
            ret = ret.split('(')[0]
        return ret

    labels_conv = {strip_name(_):_ for _ in labels_flat}

    # for label in labels:
    #     print(f"{label} -> {labels_conv[label]}")

    labels_new = [labels_conv[_] for _ in labels]

    save_pickle_file('data/new_labels', labels_new)

def plot_flatmap(values, **kwargs):
    assert len(values) == 55
    [img, rgba_list] = load_pickle_file('data/flatmap_res.pickle')

    # prepare background
    bg = np.zeros(img.shape)
    bg_sum = np.sum(img == (0, 0, 0, 255), axis=2)
    bg[np.nonzero(bg_sum==3)] = (255, 255, 255, 255)
    bg[np.nonzero(bg_sum==4)] = (0, 0, 0, 255)

    # prepare overlay
    mat = np.full((img.shape[0], img.shape[1]), np.nan)
    for it, color in enumerate(rgba_list):
        nz = np.nonzero(np.all(img[:, :] == color, axis=2))
        mat[nz] = values[it]

    # plot output
    fig, ax = plt.subplots()
    ax.imshow(bg.astype(np.uint8))
    ax.axis('off')
    im = plt.imshow(mat, **kwargs)
    cb = plt.colorbar(im, ax=ax, fraction=0.046, pad=0.04)
    cb.outline.set_visible(False)
    # cb.set_ticks([])
    return fig, ax, cb

def corrcoef_matrices(X, Y):
    # correlation coefficient between matrices
    numerator = np.mean((X - X.mean()) * (Y - Y.mean()))
    denominator = X.std() * Y.std()
    if denominator == 0:
        return 0
    else:
        result = numerator / denominator
        return result

def annot_reg(X, y, reg_line_color='red', **kwargs):
    if np.ndim(X) == 1: X = X.reshape(-1, 1)
    from sklearn.linear_model import LinearRegression
    reg = LinearRegression(n_jobs=-1)
    res = reg.fit(X, y)
    yhat = reg.predict(X)
    SS_Residual = sum((y - yhat) ** 2)
    SS_Total = sum((y - np.mean(y)) ** 2)
    r_squared = 1 - (float(SS_Residual)) / SS_Total
    # adjusted_r_squared = 1 - (1 - r_squared) * (len(y) - 1) / (len(y) - X.shape[1] - 1)

    ax = plt.gca()
    ax.plot(X, yhat, color=reg_line_color, **kwargs)
    ax.annotate(f'Linear reg:\n'
                f'y = {res.coef_[0]:.2f} * X + {res.intercept_:.2f}, r^2 = {r_squared:.2f}\n',
                xy=(.1, .8), xycoords=ax.transAxes)

    import scipy.stats as stats
    r, p_val = stats.pearsonr(X.ravel(), y)
    ax.annotate("Pearson r = {:.2f} p={:.1e}".format(r, p_val),
                xy=(.1, .95), xycoords=ax.transAxes)

reason: import numpy,import scipy,import networkx


text:
import glob

from torchvision import transforms
from torch.utils.data import DataLoader, Dataset
import os
from PIL import Image
import numpy as np

label_name = [
    "airplane",
    "automobile",
    "bird",
    "cat",
    "deer",
    "dog",
    "frog",
    "horse",
    "ship",
    "truck"
]

label_dict = {}

for idx, name in enumerate(label_name):
    label_dict[name] = idx

def default_loader(path):
    return Image.open(path).convert("RGB")

# train_transform = transforms.Compose([
#     transforms.RandomResizedCrop((28 , 28)),
#     transforms.RandomHorizontalFlip(),
#     transforms.RandomVerticalFlip(),
#     transforms.RandomRotation(90),
#     transforms.RandomGrayscale(0.1),
#     transforms.ColorJitter(0.3, 0.3, 0.3, 0.3),
#     transforms.ToTensor()
# ])

train_transform = transforms.Compose([
    transforms.RandomCrop(28),
    transforms.RandomHorizontalFlip(),
    transforms.ToTensor()
])

test_transform = transforms.Compose([
    transforms.Resize((28 , 28)),
    transforms.ToTensor()
])

class MyDataSet(Dataset):
    def __init__(self, im_list, transform=None, loader = default_loader):
        super(MyDataSet, self).__init__()
        imgs = []

        for im_item in im_list:
            im_label_name = im_item.split("/")[-2]
            imgs.append([im_item, label_dict[im_label_name]])

        self.imgs = imgs
        self.transfrom = transform
        self.loader = loader

    def __getitem__(self, index):
        im_path, im_label = self.imgs[index]
        im_data = self.loader(im_path)

        if self.transfrom is not None:
            im_data = self.transfrom(im_data)

        return im_data,im_label

    def __len__(self):
        return len(self.imgs)

im_train_list = glob.glob("/Users/liding/Documents/pytorch_py/train/*/*.png")
im_test_list = glob.glob("/Users/liding/Documents/pytorch_py/test/*/*.png")

train_dataset = MyDataSet(im_train_list, transform= train_transform)
test_dataset = MyDataSet(im_test_list, transform= transforms.ToTensor())

train_data_loader = DataLoader(dataset = train_dataset, batch_size=6, shuffle=True, num_workers=4)
test_data_loader = DataLoader(dataset = test_dataset, batch_size=6, shuffle=False, num_workers=4)

print("num_of_train", len(train_dataset))
print("num_of_test", len(test_dataset))