text:
from __future__ import division
from __future__ import absolute_import
from builtins import range
from past.utils import old_div
from . import geometry
from . import surface
import scipy
from . import beam

def XTOMOchip(temp):
    diodes = 38*[0]
    area = [4e-3,.94e-3]
    Vec = [geometry.Vecx((0.,1.,0.)),geometry.Vecx((0.,0.,1.))]
    spacing = 1e-3
    pos = scipy.mgrid[-18.5:18.5:38j]*spacing
    #pos = scipy.linspace(e-3,e-3,38)
    for i in range(len(diodes)):
        vecin = geometry.Vecx((0.,pos[i],0.))
        diodes[i] = surface.Rect(vecin,
                                 temp,
                                 area,
                                 vec=Vec)
    return diodes

def XTOMO1(temp, place1=(0.73674,0.,0.46479), place2=(.747,0.,.4947), angle=(0.,-(180.5*scipy.pi/180.),old_div(scipy.pi,2.))):
    # this is the hard part, tree storage values are not done in such a manner
    pos = geometry.Origin(place1,temp,angle=angle)
    area = [1e-3,3e-3]
    Vec = [geometry.Vecx((1.,0.,0.)),geometry.Vecx((0.,0.,1.))]
    aperture = surface.Rect((0.,0.,0.),pos,area,vec=Vec)
    offset = geometry.Origin(place2,temp,angle=angle)
    diodes = XTOMOchip(offset)
    diodes[0].redefine(temp)
    for i in diodes:
        i.redefine(temp)
    diodes.append(aperture)
    diodes[-1].redefine(temp)
    return diodes

def XTOMO1beam(plasma):
    temp = XTOMO1(plasma)
    output = beam.multiBeam(temp[:-1],temp[-1])
    plasma.trace(output)
    return output

#def XTOMO1(temp, place=(.73674,0,.46479),angle =(,,)):
#    # this is the hard part, tree storage values are not done in such a manner
#    pos = geometry.Origin(place,temp,angle=angle)
#    area = [3e-3,1e-3]
#    Vec = [geometry.Vecx((1.,0.,0.)),geometry.Vecx((0.,0.,1.))]
#    aperature = surface.Rect((0.,0.,0.),pos,area,Vec=Vec)
#    offset = goemetry.Origin((0.,0.,0.),aperature,Vec=Vec)
#    diodes = XTOMOchip(offset)
#    for i in diodes:
#        i.redefine(temp)
#        diodes.append(aperature)
#        diodes[-1].redefine(temp)
#    return diodes
#
#def XTOMO2(temp, place=(,0,),angle =(,,)):
#    # this is the hard part, tree storage values are not done in such a manner
#    pos = geometry.Origin(place,temp,angle=angle)
#    area = [3.6e-3,5e-4]
#    Vec = [geometry.Vecx((1.,0.,0.)),geometry.Vecx((0.,0.,1.))]
#    aperature = surface.Rect((0.,0.,0.),pos,area,Vec=Vec)
#    offset = goemetry.Origin((,,),aperature,Vec=Vec)
#    diodes = XTOMOchip(offset)
#    for i in diodes:
#        i.redefine(temp)
#        diodes.append(aperature)
#        diodes[-1].redefine(temp)
#    return diodes
#

def XTOMO3(temp, place=(1.0152,0.,0.),angle =(0.,old_div(scipy.pi,2.),old_div(scipy.pi,2.))):
    # this is the hard part, tree storage values are not done in such a manner
    pos = geometry.Origin(place,temp,angle=angle)
    area = [1e-3,3e-3]
    Vec = [geometry.Vecx((1.,0.,0.)),geometry.Vecx((0.,0.,1.))]
    aperture = surface.Rect((0.,0.,0.),pos,area,vec=Vec)
    offset = geometry.Origin((0.,0.,-1.8e-2),aperture,vec=Vec)
    diodes = XTOMOchip(offset)
    diodes[0].redefine(temp)
    for i in diodes:
        i.redefine(temp)
    diodes.append(aperture)
    diodes[-1].redefine(temp)
    return diodes

def XTOMO3beam(plasma):
    temp = XTOMO3(plasma)
    output = beam.multiBeam(temp[:-1],temp[-1])
    plasma.trace(output)
    return output

#def XTOMO4(temp, place=(,0,),angle =(,,)):
#    # this is the hard part, tree storage values are not done in such a manner
#    pos = geometry.Origin(place,temp,angle=angle)
#    area = [3.6e-3,5e-4]
#    Vec = [geometry.Vecx((1.,0.,0.)),geometry.Vecx((0.,0.,1.))]
#    aperature = surface.Rect((0.,0.,0.),pos,area,Vec=Vec)
#    offset = goemetry.Origin((,,),aperature,Vec=Vec)
#    diodes = XTOMOchip(offset)
#    for i in diodes:
#        i.redefine(temp)
#        diodes.append(aperature)
#        diodes[-1].redefine(temp)
#    return diodes

def XTOMO5(temp, place=(.8888,0,-.2794),angle =(0.,.79587,old_div(scipy.pi,2.))):
    # this is the hard part, tree storage values are not done in such a manner
    pos = geometry.Origin(place,temp,angle=angle)
    area = [1e-3,3e-3]
    Vec = [geometry.Vecx((1.,0.,0.)),geometry.Vecx((0.,0.,1.))]
    aperture = surface.Rect((0.,0.,0.),pos,area,vec=Vec)
    offset = geometry.Origin((0.,0.,-2.5e-2),aperture,vec=Vec)
    diodes = XTOMOchip(offset)
    diodes[0].redefine(temp)
    for i in diodes:
        i.redefine(temp)
    diodes.append(aperture)
    diodes[-1].redefine(temp)
    return diodes

def XTOMO5beam(plasma):
    temp = XTOMO5(plasma)
    output = beam.multiBeam(temp[:-1],temp[-1])
    traceout = 2*scipy.ones((len(output),),dtype=int)
    traceout[-3:] = 0
    for i in range(len(output)):
        plasma.trace(output[i],
                     limiter=traceout[i])
    return output

ext: py

max_stars_repo_name: fsciortino/TRIPPy

max_issues_repo_name: fsciortino/TRIPPy

max_forks_repo_name: fsciortino/TRIPPy

reason: import scipy


text:
import os
import json
import statistics
from imutils import face_utils
from copy import deepcopy

import numpy as np
import dlib
import cv2

from config import LANDS_PATH, RATIOS_PATH,\
    RATIOS_AVG_PATH, RATIOS_STDS_PATH, ANGLES_PATH, ANGLES_AVG_PATH
from helpers import throw, is_number
from utils import cumul, match_hists

NUM_OF_RATIOS = 100232
NUM_OF_ANGLES = 50116

def faces(img):
    """ Returns dlib.rectangles containing rectangles of faces """
    if not isinstance(img, np.ndarray):
        throw("Expected numpy.array got " + type(img).__name__)
    if not os.path.isfile(LANDS_PATH):
        throw(LANDS_PATH + " doesn't exist")

    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    face_detector = dlib.get_frontal_face_detector()
    faces = face_detector(gray, 1)

    return faces

def landmarks(img, face):
    if not isinstance(face, dlib.rectangle):
        throw("Expected dlib.rectangle got " + type(face).__name__)
    predictor = dlib.shape_predictor(LANDS_PATH)
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    dlib_shape = predictor(gray, face)
    return face_utils.shape_to_np(dlib_shape)

def all_distances(lands, as_dict=True, allow_repeats=False):
    """ Returns distances from each point of landmarks to every other point.
    Returning format:
    if as_dict is True
    {1: {2: 3}} - from 1st landmark to 2nd the distance is 3
    if as_dict is False
    [(1, 2, 3)]
    """
    if as_dict:
        distances = {}
    else:
        distances = []
    length = len(lands)
    for i in range(length):
        pfrom = lands[i]
        if as_dict:
            distances[i] = {}
        from_index = 0 if allow_repeats else i + 1
        for j in range(from_index, length):
            pto = lands[j]
            dis = _distance(pfrom, pto)
            if as_dict:
                distances[i][j] = dis
            else:
                distances.append((i, j, dis))
    return distances

def _distance(p1, p2):
    """ Returns distance between p1 point and p2 """
    return ((p1[0] - p2[0]) ** 2 + (p1[1] - p2[1]) ** 2) ** 0.5

def extract_rect(img, face):
    npf = _dlib_rect_to_np(face)
    return img[npf[1]:npf[1] + npf[3], npf[0]:npf[0] + npf[2]]

def stdeviation(data):
    """ Returns standard deviation of the data """
    return statistics.stdev(data)

def feed_ratio(val):
    """ Writes into ratio the new value - val
    The file is a line by line json objects
    """
    with open(RATIOS_PATH, 'a') as ratios:
        json.dump(val, ratios)
        ratios.write("\n")

def feed_angle(ang):
    """ Writes into angles the new angle - ang
    The file is a line by line json objects
    """
    with open(ANGLES_PATH, 'a') as angles:
        json.dump(ang, angles)
        angles.write("\n")

def ratios(all_dists):
    if not isinstance(all_dists, dict):
        throw("Expected dict got " + type(all_dists).__name__)

    length = len(all_dists)
    ratios = []
    for i in range(length):
        for j in range(i + 1, length):
            dist_i_j = all_dists[i][j]
            if i == j:
                continue
            for k in range(j + 1, length):
                if i == k or j == k:
                    continue
                dist_j_k = all_dists[j][k]
                dist_i_k = all_dists[i][k]

                by_j = dist_i_j / dist_j_k if dist_j_k != 0 else 0
                by_k = dist_j_k / dist_i_k if dist_i_k != 0 else 0

                ratios.append((i, k, j, by_k))
                ratios.append((i, j, k, by_j))
    return ratios

def calc_avg_angles():
    # TODO: To Be Continued...
    sums = np.zeros(NUM_OF_ANGLES)
    with open(ANGLES_PATH, 'r') as angles:
        for line_count, line in enumerate(angles):
            curr_angles = json.loads(line)
            for n, angle in enumerate(curr_angles):
                if is_number(angle[3]):
                    sums[n] += angle[3]
    avgs = []
    for i, j in enumerate(sums):
        avgs.append(j / (line_count + 1))
    with open(ANGLES_AVG_PATH, 'w') as avgs_file:
        json.dump(avgs, avgs_file)

def calc_avg_ratios():
    sums = np.zeros(NUM_OF_RATIOS)
    with open(RATIOS_PATH, 'r') as ratios:
        for line_count, line in enumerate(ratios):
            curr_ratios = json.loads(line)
            for n, ratio in enumerate(curr_ratios):
                sums[n] += ratio[3]

    avgs = [0 for i in range(NUM_OF_RATIOS)]
    for i, j in enumerate(sums):
        avgs[i] = j / (line_count + 1)
    with open(RATIOS_AVG_PATH, 'w') as avgs_file:
        json.dump(avgs, avgs_file)

def calc_ratio_stds():
    """ Calculate standard deviations of ratios and write them into file.
    Will be total (global NUM_OF_RATIOS)
    """
    data = {i: [] for i in range(NUM_OF_RATIOS)}
    with open(RATIOS_PATH, 'r') as ratios:
        for line_count, line in enumerate(ratios):
            curr_data = [x[3] for x in json.loads(line)]
            for n, ratio in enumerate(curr_data):
                data[n].append(ratio)
            print("Num of lines processed: " + str(line_count))
    print("Calculating standard deviations...")
    stds = [stdeviation(data[i]) for i in range(NUM_OF_RATIOS)]
    with open(RATIOS_STDS_PATH, 'w') as stds_file:
        json.dump(stds, stds_file)

def _dlib_rect_to_np(rect):
    """ Format: (x, y, width, height) - tuple """
    return face_utils.rect_to_bb(rect)

def show_img(img, name='default'):
    cv2.imshow(name, img)
    cv2.wait()

def compare_with_avg(img):
    fs = faces(img)
    lands = landmarks(img, fs[0])
    all_dists = all_distances(lands, allow_repeats=True)
    rs = [x[3] for x in ratios(all_dists)]

    with open(RATIOS_AVG_PATH, 'r') as avgs_file:
        means = json.load(avgs_file)
    with open(RATIOS_STDS_PATH, 'r') as stds_file:
        stds = json.load(stds_file)
    z_scores = [z_score(rs[i], means[i], stds[i])
                for i in range(NUM_OF_RATIOS)]
    mean_score = sum(z_scores) / len(z_scores)
    return mean_score

def z_score(x, u, o):
    return abs((x - u) / o)

def lands_with_names(lands):
    return [{
        "chin": lands[0:17],
        "left_eyebrow": lands[17:22],
        "right_eyebrow": lands[22:27],
        "nose_bridge": lands[27:31],
        "nose_tip": lands[31:36],
        "left_eye": lands[36:42],
        "right_eye": lands[42:48],
        "top_lip": lands[48:55] + [lands[64]] + [lands[63]] +
        [lands[62]] + [lands[61]] + [lands[60]],
        "bottom_lip": lands[54:60] + [lands[48]] + [lands[60]] +
        [lands[67]] + [lands[66]] + [lands[65]] + [lands[64]]
    }]

def lands_from_img(img):
    dlib_face = faces(img)[0]
    return landmarks(img, dlib_face)

def angles(lands):
    """ Returns dict of angles made by joining
    all combinations of 3 points from landmarks
    """
    length = len(lands)  # 68
    angles = {}
    for i in range(length):
        for j in range(i + 1, length):
            for k in range(j + 1, length):
                ang = angle(lands[i], lands[j], lands[k])
                angles[(i, j, k)] = ang
    return angles

def angle(a, b, c):
    """ Returns angle in degrees between
    line 1 and line 2 created by joining p1 to p2 and p2 to p3
    """
    ba = a - b
    bc = c - b
    cosine_angle = np.dot(ba, bc) / (np.linalg.norm(ba) * np.linalg.norm(bc))
    angle = np.arccos(cosine_angle)
    return np.degrees(angle)

def to_hsv(img):
    return cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

def to_rgb(img):
    return cv2.cvtColor(img, cv2.COLOR_HSV2BGR)

def equalize_red(img1, img2):
    height1, width1, _ = img1.shape
    height2, width2, _ = img2.shape
    if height1 != height2 or width1 != width2:
        throw('img1 and img2 should have the same size')

    red_hist1 = [0 for _ in range(255)]
    red_hist2 = [0 for _ in range(255)]

    # compute red color histograms
    for i in range(height1):
        for j in range(width1):
            b1, g1, r1 = img1[i, j]
            b2, g2, r2 = img2[i, j]
            red_hist1[r1] += 1
            red_hist2[r2] += 1
            # hsv1 = to_hsv(img1)
            # h, s, v = hsv[i, j]
            # hsv[i, j] = np.array([h, s, v], dtype=np.uint8)
    cumul1 = cumul(red_hist1)
    cumul2 = cumul(red_hist2)

    adjusted = match_hists(cumul1, cumul2)

    return repaint_red(img1, adjusted)

def repaint_red(img, mapping):
    h, w, c = img.shape
    for i in range(h):
        for j in range(w):
            b, g, r = img[i, j]
            r = mapping[r]
            img[i, j] = b, g, r
    return img

def bot_0(x):
    return 0.9848 * x - 6.7474

def top_0(x):
    return - 0.0009 * x * x + 1.1917 * x - 4.0146

def bot_1(x):
    return - 0.0009 * x * x + 1.1917 * x - 4.0146

def top_1(x):
    return - 0.0011 * x * x + 1.2262 * x + 4.0264

def bot_2(x):
    return - 0.0011 * x * x + 1.2262 * x + 4.0264

def top_2(x):
    return - 0.0013 * x * x + 1.2608 * x + 12.067

def bot_3(x):
    return - 0.0013 * x * x + 1.2608 * x + 12.067

def top_3(x):
    return - 0.0026 * x * x + 1.5713 * x + 14.8

def layer(img, layer_no):
    tmp = deepcopy(img)
    top_func = eval('top_' + str(layer_no))
    bot_func = eval('bot_' + str(layer_no))
    h, w, c = tmp.shape
    for i in range(h):
        for j in range(w):
            px = tmp[i, j]
            b, g, r = px
            rb = (int(r) + int(b)) / 2

            if b < g and g < r and rb >= bot_func(g) and rb <= top_func(g):
                tmp[i, j] = 0, 0, 0
            else:
                tmp[i, j] = 255, 255, 255
    return tmp

max_stars_repo_name: GSargsyan/capstone

max_issues_repo_name: GSargsyan/capstone

max_forks_repo_name: GSargsyan/capstone

reason: import numpy


text:
"""Datasets"""

import os
from cv2 import transform

import torch
from torch._C import dtype
from torch.utils.data import DataLoader, Dataset
from torchvision import datasets
import torchvision.transforms as transforms
import torchvision
import glob
import PIL
import random
import math
import pickle
import numpy as np

class CelebA(Dataset):
    """CelelebA Dataset"""

    def __init__(self, dataset_path, img_size, **kwargs):
        super().__init__()

        self.data = glob.glob(dataset_path)
        assert len(self.data) > 0, "Can't find data; make sure you specify the path to your dataset"
        self.transform = transforms.Compose(
                    [transforms.Resize(320), transforms.CenterCrop(256), transforms.ToTensor(), transforms.Normalize([0.5], [0.5]), transforms.RandomHorizontalFlip(p=0.5), transforms.Resize((img_size, img_size), interpolation=0)])
        # self.transform = transforms.Compose(
        #             [transforms.Resize(320), transforms.CenterCrop(256), transforms.ToTensor(), transforms.Normalize([0.5], [0.5]), transforms.Resize((img_size, img_size), interpolation=0)])
        # self.transform = transforms.Compose(
        #             [transforms.Resize(320), transforms.CenterCrop(256), transforms.ToTensor(), transforms.Normalize([0.5], [0.5]), transforms.RandomHorizontalFlip(p=0.5), transforms.Resize((img_size, img_size), interpolation=PIL.Image.NEAREST)])
    def __len__(self):
        return len(self.data)

    def __getitem__(self, index):
        X = PIL.Image.open(self.data[index])
        X = self.transform(X)

        return X, 0

class Cats(Dataset):
    """Cats Dataset"""

    def __init__(self, dataset_path, img_size, **kwargs):
        super().__init__()

        self.data = glob.glob(dataset_path)
        assert len(self.data) > 0, "Can't find data; make sure you specify the path to your dataset"
        self.transform = transforms.Compose(
                    [transforms.Resize((img_size, img_size), interpolation=0), transforms.ToTensor(), transforms.Normalize([0.5], [0.5]), transforms.RandomHorizontalFlip(p=0.5)])
    def __len__(self):
        return len(self.data)

    def __getitem__(self, index):
        X = PIL.Image.open(self.data[index])
        X = self.transform(X)

        return X, 0

class Carla(Dataset):
    """Carla Dataset"""

    def __init__(self, dataset_path, img_size, **kwargs):
        super().__init__()

        self.data = glob.glob(dataset_path)
        assert len(self.data) > 0, "Can't find data; make sure you specify the path to your dataset"
        self.transform = transforms.Compose(
                    [transforms.Resize((img_size, img_size), interpolation=0), transforms.ToTensor(), transforms.Normalize([0.5], [0.5])])

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

    def __getitem__(self, index):
        X = PIL.Image.open(self.data[index])
        X = self.transform(X)

        return X, 0

def get_dataset(name, subsample=None, batch_size=1, **kwargs):
    dataset = globals()[name](**kwargs)

    dataloader = torch.utils.data.DataLoader(
        dataset,
        batch_size=batch_size,
        shuffle=True,
        drop_last=True,
        pin_memory=False,
        num_workers=4
    )
    return dataloader, 3

def get_dataset_distributed(name, world_size, rank, batch_size, **kwargs):
    dataset = globals()[name](**kwargs)

    sampler = torch.utils.data.distributed.DistributedSampler(
        dataset,
        num_replicas=world_size,
        rank=rank,
    )
    dataloader = torch.utils.data.DataLoader(
        dataset,
        sampler=sampler,
        batch_size=batch_size,
        shuffle=False,
        drop_last=True,
        pin_memory=True,
        num_workers=2,
    )

    return dataloader, 3

class CelebAMaskHQ(Dataset):
    def __init__(self, dataset_path, img_size, background_mask, return_label=True, **kwargs):
        img_base = 'celebahq_mask_img/*.jpg'
        label_base = 'celebahq_mask_mask/*.png'
        self.img_path = os.path.join(dataset_path, img_base)
        self.label_path = os.path.join(dataset_path,  label_base)
        self.transform_image = transforms.Compose(
                    [transforms.Resize(320, interpolation=PIL.Image.NEAREST),
                    transforms.CenterCrop(256),
                    transforms.ToTensor(),
                    transforms.Normalize([0.5], [0.5]),
                    # transforms.RandomHorizontalFlip(p=0.5),
                    transforms.Resize((img_size, img_size), interpolation=PIL.Image.NEAREST)])
        self.transform_label = transforms.Compose([
                    transforms.Resize(320, interpolation=PIL.Image.NEAREST),
                    transforms.CenterCrop(256),
                    transforms.ToTensor(),
                    # transforms.RandomHorizontalFlip(p=0.5),
                    transforms.Resize((img_size, img_size), interpolation=PIL.Image.NEAREST)])
        self.data_img = sorted(glob.glob(self.img_path))
        self.data_label = sorted(glob.glob(self.label_path))
        self.background_mask = background_mask
        self.return_label = return_label
        assert len(self.data_img) == len(self.data_label)

        self.color_map = {
            0: [0, 0, 0],
            1: [204, 0, 0],
            2: [76, 153, 0],
            3: [204, 204, 0],
            4: [51, 51, 255],
            5: [204, 0, 204],
            6: [0, 255, 255],
            7: [255, 204, 204],
            8: [102, 51, 0],
            9: [255, 0, 0],
            10: [102, 204, 0],
            11: [255, 255, 0],
            12: [0, 0, 153],
            13: [0, 0, 204],
            14: [255, 51, 153],
            15: [0, 204, 204],
            16: [0, 51, 0],
            17: [255, 153, 51],
            18: [0, 204, 0]}

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

    def _mask_labels(self, mask_np):
        label_size = len(self.color_map.keys())
        labels = np.zeros((label_size, mask_np.shape[0], mask_np.shape[1]))
        for i in range(label_size):
            labels[i][mask_np==i] = 1.0

        return labels

    def __getitem__(self, index):
        img = PIL.Image.open(self.data_img[index]).convert('RGB')
        label = PIL.Image.open(self.data_label[index]).convert('L')
        ### mask background of image ###
        if self.background_mask:
            trans = transforms.Compose([transforms.ToTensor()])
            trans_inv = transforms.Compose([transforms.ToPILImage()])
            i, l = trans(img), trans(label.resize((1024, 1024), resample=PIL.Image.NEAREST)) * 255.
            l = l.expand_as(i)
            i[l == 0] = 0
            img = trans_inv(i)
        ################################
        img = self.transform_image(img) # [-1, 1] after normalization
        label = self.transform_label(label)
        if random.random() > 0.5:
            img = transforms.functional.hflip(img)
            label = transforms.functional.hflip(label)

        label = self._mask_labels((label * 255.)[0])
        label = (label - 0.5) / 0.5 # 如何解决mask中有虚线: 更换插值的模式
        label = torch.tensor(label, dtype=torch.float)
        if not self.return_label:
            return img, 0
        return img, label, 0

class CelebAMaskHQ_debug(Dataset):
    """
    2. 去掉了convert("RGB")
    3. 去掉了interpolation=0
    """
    def __init__(self, dataset_path, img_size, background_mask, return_label=True, **kwargs):
        img_base = 'celebahq_mask_img/*.jpg'
        label_base = 'celebahq_mask_mask/*.png'
        self.img_path = os.path.join(dataset_path, img_base)
        self.label_path = os.path.join(dataset_path,  label_base)
        self.transform_image = transforms.Compose(
                    [transforms.Resize(320),
                    transforms.CenterCrop(256),
                    transforms.ToTensor(),
                    transforms.Normalize([0.5], [0.5]),
                    # transforms.RandomHorizontalFlip(p=0.5),
                    transforms.Resize((img_size, img_size))])
        self.transform_label = transforms.Compose([
                    transforms.Resize(320, interpolation=PIL.Image.NEAREST),
                    transforms.CenterCrop(256),
                    transforms.ToTensor(),
                    # transforms.RandomHorizontalFlip(p=0.5),
                    transforms.Resize((img_size, img_size), interpolation=PIL.Image.NEAREST)])
        self.data_img = sorted(glob.glob(self.img_path))
        self.data_label = sorted(glob.glob(self.label_path))
        self.background_mask = background_mask
        self.return_label = return_label
        assert len(self.data_img) == len(self.data_label)

        self.color_map = {
            0: [0, 0, 0],
            1: [204, 0, 0],
            2: [76, 153, 0],
            3: [204, 204, 0],
            4: [51, 51, 255],
            5: [204, 0, 204],
            6: [0, 255, 255],
            7: [255, 204, 204],
            8: [102, 51, 0],
            9: [255, 0, 0],
            10: [102, 204, 0],
            11: [255, 255, 0],
            12: [0, 0, 153],
            13: [0, 0, 204],
            14: [255, 51, 153],
            15: [0, 204, 204],
            16: [0, 51, 0],
            17: [255, 153, 51],
            18: [0, 204, 0]}

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

    def _mask_labels(self, mask_np):
        label_size = len(self.color_map.keys())
        labels = np.zeros((label_size, mask_np.shape[0], mask_np.shape[1]))
        for i in range(label_size):
            labels[i][mask_np==i] = 1.0

        return labels

    def __getitem__(self, index):
        # img = PIL.Image.open(self.data_img[index]).convert('RGB')
        img = PIL.Image.open(self.data_img[index])
        label = PIL.Image.open(self.data_label[index]).convert('L')
        ### mask background of image ###
        if self.background_mask:
            trans = transforms.Compose([transforms.ToTensor()])
            trans_inv = transforms.Compose([transforms.ToPILImage()])
            i, l = trans(img), trans(label.resize((1024, 1024), resample=PIL.Image.NEAREST)) * 255.
            l = l.expand_as(i)
            i[l == 0] = 0
            img = trans_inv(i)
        ################################
        img = self.transform_image(img) # [-1, 1] after normalization
        label = self.transform_label(label)
        if random.random() > 0.5:
            img = transforms.functional.hflip(img)
            label = transforms.functional.hflip(label)

        label = self._mask_labels((label * 255.)[0])
        label = (label - 0.5) / 0.5 # 如何解决mask中有虚线: 更换插值的模式
        label = torch.tensor(label, dtype=torch.float)
        if not self.return_label:
            return img, 0
        return img, label, 0

class CelebAMaskHQ_debug_2(Dataset):
    """
    1. 调整了预处理的顺序，和baseline一致
    2. 去掉了convert("RGB")
    3. 去掉了interpolation=0
    """
    def __init__(self, dataset_path, img_size, background_mask, return_label=True, **kwargs):
        img_base = 'celebahq_mask_img/*.jpg'
        label_base = 'celebahq_mask_mask/*.png'
        self.img_path = os.path.join(dataset_path, img_base)
        self.label_path = os.path.join(dataset_path,  label_base)
        self.transform_image = transforms.Compose(
                    [transforms.Resize(320),
                    transforms.CenterCrop(256),
                    transforms.ToTensor(),
                    transforms.Normalize([0.5], [0.5]),
                    # transforms.RandomHorizontalFlip(p=0.5)
                    ])
        self.transform_label = transforms.Compose([
                    transforms.Resize(320, interpolation=PIL.Image.NEAREST),
                    transforms.CenterCrop(256),
                    transforms.ToTensor(),
                    # transforms.RandomHorizontalFlip(p=0.5),
                    transforms.Resize((img_size, img_size), interpolation=PIL.Image.NEAREST)])
        self.data_img = sorted(glob.glob(self.img_path))
        self.data_label = sorted(glob.glob(self.label_path))
        self.background_mask = background_mask
        self.return_label = return_label
        assert len(self.data_img) == len(self.data_label)
        self.resize_img = transforms.Resize((img_size, img_size))
        self.resize_label = transforms.Resize((img_size, img_size), interpolation=PIL.Image.NEAREST)
        self.color_map = {
            0: [0, 0, 0],
            1: [204, 0, 0],
            2: [76, 153, 0],
            3: [204, 204, 0],
            4: [51, 51, 255],
            5: [204, 0, 204],
            6: [0, 255, 255],
            7: [255, 204, 204],
            8: [102, 51, 0],
            9: [255, 0, 0],
            10: [102, 204, 0],
            11: [255, 255, 0],
            12: [0, 0, 153],
            13: [0, 0, 204],
            14: [255, 51, 153],
            15: [0, 204, 204],
            16: [0, 51, 0],
            17: [255, 153, 51],
            18: [0, 204, 0]}

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

    def _mask_labels(self, mask_np):
        label_size = len(self.color_map.keys())
        labels = np.zeros((label_size, mask_np.shape[0], mask_np.shape[1]))
        for i in range(label_size):
            labels[i][mask_np==i] = 1.0

        return labels

    def __getitem__(self, index):
        # img = PIL.Image.open(self.data_img[index]).convert('RGB')
        img = PIL.Image.open(self.data_img[index])
        label = PIL.Image.open(self.data_label[index]).convert('L')
        ### mask background of image ###
        if self.background_mask:
            trans = transforms.Compose([transforms.ToTensor()])
            trans_inv = transforms.Compose([transforms.ToPILImage()])
            i, l = trans(img), trans(label.resize((1024, 1024), resample=PIL.Image.NEAREST)) * 255.
            l = l.expand_as(i)
            i[l == 0] = 0
            img = trans_inv(i)
        ################################
        img = self.transform_image(img) # [-1, 1] after normalization
        label = self.transform_label(label)
        if random.random() > 0.5:
            img = transforms.functional.hflip(img)
            label = transforms.functional.hflip(label)
        img = self.resize_img(img)
        label = self.resize_label(label)
        label = self._mask_labels((label * 255.)[0])
        label = (label - 0.5) / 0.5 # 如何解决mask中有虚线: 更换插值的模式
        label = torch.tensor(label, dtype=torch.float)
        if not self.return_label:
            return img, 0
        return img, label, 0

class CelebAMaskHQ_wo_background(Dataset):
    """
    1. 将background类设成0
    2. 去掉了convert("RGB")
    3. 去掉了interpolation=0
    """
    def __init__(self, dataset_path, img_size, background_mask, return_label=True, **kwargs):
        img_base = 'celebahq_mask_img/*.jpg'
        label_base = 'celebahq_mask_mask/*.png'
        self.img_path = os.path.join(dataset_path, img_base)
        self.label_path = os.path.join(dataset_path,  label_base)
        self.transform_image = transforms.Compose(
                    [transforms.Resize(320),
                    transforms.CenterCrop(256),
                    transforms.ToTensor(),
                    transforms.Normalize([0.5], [0.5]),
                    # transforms.RandomHorizontalFlip(p=0.5),
                    transforms.Resize((img_size, img_size))])
        self.transform_label = transforms.Compose([
                    transforms.Resize(320, interpolation=PIL.Image.NEAREST),
                    transforms.CenterCrop(256),
                    transforms.ToTensor(),
                    # transforms.RandomHorizontalFlip(p=0.5),
                    transforms.Resize((img_size, img_size), interpolation=PIL.Image.NEAREST)])
        self.data_img = sorted(glob.glob(self.img_path))
        self.data_label = sorted(glob.glob(self.label_path))
        self.background_mask = background_mask
        self.return_label = return_label
        assert len(self.data_img) == len(self.data_label)

        self.color_map = {
            0: [0, 0, 0],
            1: [204, 0, 0],
            2: [76, 153, 0],
            3: [204, 204, 0],
            4: [51, 51, 255],
            5: [204, 0, 204],
            6: [0, 255, 255],
            7: [255, 204, 204],
            8: [102, 51, 0],
            9: [255, 0, 0],
            10: [102, 204, 0],
            11: [255, 255, 0],
            12: [0, 0, 153],
            13: [0, 0, 204],
            14: [255, 51, 153],
            15: [0, 204, 204],
            16: [0, 51, 0],
            17: [255, 153, 51],
            18: [0, 204, 0]}

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

    def _mask_labels(self, mask_np):
        label_size = len(self.color_map.keys())
        labels = np.zeros((label_size, mask_np.shape[0], mask_np.shape[1]))
        for i in range(label_size):
            labels[i][mask_np==i] = 1.0
        labels[0] = 0. # set background class to zero
        return labels

    def __getitem__(self, index):
        # img = PIL.Image.open(self.data_img[index]).convert('RGB')
        img = PIL.Image.open(self.data_img[index])
        label = PIL.Image.open(self.data_label[index]).convert('L')
        ### mask background of image ###
        if self.background_mask:
            trans = transforms.Compose([transforms.ToTensor()])
            trans_inv = transforms.Compose([transforms.ToPILImage()])
            i, l = trans(img), trans(label.resize((1024, 1024), resample=PIL.Image.NEAREST)) * 255.
            l = l.expand_as(i)
            i[l == 0] = 0
            img = trans_inv(i)
        ################################
        img = self.transform_image(img) # [-1, 1] after normalization
        label = self.transform_label(label)
        if random.random() > 0.5:
            img = transforms.functional.hflip(img)
            label = transforms.functional.hflip(label)

        label = self._mask_labels((label * 255.)[0])
        label = (label - 0.5) / 0.5 # 如何解决mask中有虚线: 更换插值的模式
        label = torch.tensor(label, dtype=torch.float)
        if not self.return_label:
            return img, 0
        return img, label, 0

class CelebAMaskHQ_wo_background_seg_18(Dataset):
    """
    1. 将background类设成0
    2. 去掉了convert("RGB")
    3. 去掉了interpolation=0
    """
    def __init__(self, dataset_path, img_size, background_mask, return_label=True, **kwargs):
        img_base = 'celebahq_mask_img/*.jpg'
        label_base = 'celebahq_mask_mask/*.png'
        self.img_path = os.path.join(dataset_path, img_base)
        self.label_path = os.path.join(dataset_path,  label_base)
        self.transform_image = transforms.Compose(
                    [transforms.Resize(320),
                    transforms.CenterCrop(256),
                    transforms.ToTensor(),
                    transforms.Normalize([0.5], [0.5]),
                    # transforms.RandomHorizontalFlip(p=0.5),
                    transforms.Resize((img_size, img_size))])
        self.transform_label = transforms.Compose([
                    transforms.Resize(320, interpolation=PIL.Image.NEAREST),
                    transforms.CenterCrop(256),
                    transforms.ToTensor(),
                    # transforms.RandomHorizontalFlip(p=0.5),
                    transforms.Resize((img_size, img_size), interpolation=PIL.Image.NEAREST)])
        self.data_img = sorted(glob.glob(self.img_path))
        self.data_label = sorted(glob.glob(self.label_path))
        self.background_mask = background_mask
        self.return_label = return_label
        assert len(self.data_img) == len(self.data_label)

        self.color_map = {
            1: [204, 0, 0],
            2: [76, 153, 0],
            3: [204, 204, 0],
            4: [51, 51, 255],
            5: [204, 0, 204],
            6: [0, 255, 255],
            7: [255, 204, 204],
            8: [102, 51, 0],
            9: [255, 0, 0],
            10: [102, 204, 0],
            11: [255, 255, 0],
            12: [0, 0, 153],
            13: [0, 0, 204],
            14: [255, 51, 153],
            15: [0, 204, 204],
            16: [0, 51, 0],
            17: [255, 153, 51],
            18: [0, 204, 0]}

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

    def _mask_labels(self, mask_np):
        label_size = len(self.color_map.keys())
        labels = np.zeros((label_size, mask_np.shape[0], mask_np.shape[1]))
        for i in range(label_size):
            labels[i][mask_np==i+1] = 1.0
        return labels

    def __getitem__(self, index):
        # img = PIL.Image.open(self.data_img[index]).convert('RGB')
        img = PIL.Image.open(self.data_img[index])
        label = PIL.Image.open(self.data_label[index]).convert('L')
        ### mask background of image ###
        if self.background_mask:
            trans = transforms.Compose([transforms.ToTensor()])
            trans_inv = transforms.Compose([transforms.ToPILImage()])
            i, l = trans(img), trans(label.resize((1024, 1024), resample=PIL.Image.NEAREST)) * 255.
            l = l.expand_as(i)
            i[l == 0] = 0
            img = trans_inv(i)
        ################################
        img = self.transform_image(img) # [-1, 1] after normalization
        label = self.transform_label(label)
        if random.random() > 0.5:
            img = transforms.functional.hflip(img)
            label = transforms.functional.hflip(label)

        label = self._mask_labels((label * 255.)[0])
        label = (label - 0.5) / 0.5 # 如何解决mask中有虚线: 更换插值的模式
        label = torch.tensor(label, dtype=torch.float)
        if not self.return_label:
            return img, 0
        return img, label, 0

class CelebAMaskHQ_single_image_wo_background_seg_18(Dataset):
    """
    1. 将background类设成0
    2. 去掉了convert("RGB")
    3. 去掉了interpolation=0
    """
    def __init__(self, dataset_path, img_size, background_mask, return_label=True, **kwargs):
        img_base = 'demo_mask_img/97.jpg'
        label_base = 'demo_mask_mask/97.png'
        self.img_path = os.path.join(dataset_path, img_base)
        self.label_path = os.path.join(dataset_path,  label_base)
        self.transform_image = transforms.Compose(
                    [transforms.Resize(320),
                    transforms.CenterCrop(256),
                    transforms.ToTensor(),
                    transforms.Normalize([0.5], [0.5]),
                    # transforms.RandomHorizontalFlip(p=0.5),
                    transforms.Resize((img_size, img_size))])
        self.transform_label = transforms.Compose([
                    transforms.Resize(320, interpolation=PIL.Image.NEAREST),
                    transforms.CenterCrop(256),
                    transforms.ToTensor(),
                    # transforms.RandomHorizontalFlip(p=0.5),
                    transforms.Resize((img_size, img_size), interpolation=PIL.Image.NEAREST)])
        self.data_img = [self.img_path]
        self.data_label = [self.label_path]
        self.background_mask = background_mask
        self.return_label = return_label
        assert len(self.data_img) == len(self.data_label)

        self.color_map = {
            1: [204, 0, 0],
            2: [76, 153, 0],
            3: [204, 204, 0],
            4: [51, 51, 255],
            5: [204, 0, 204],
            6: [0, 255, 255],
            7: [255, 204, 204],
            8: [102, 51, 0],
            9: [255, 0, 0],
            10: [102, 204, 0],
            11: [255, 255, 0],
            12: [0, 0, 153],
            13: [0, 0, 204],
            14: [255, 51, 153],
            15: [0, 204, 204],
            16: [0, 51, 0],
            17: [255, 153, 51],
            18: [0, 204, 0]}

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

    def _mask_labels(self, mask_np):
        label_size = len(self.color_map.keys())
        labels = np.zeros((label_size, mask_np.shape[0], mask_np.shape[1]))
        for i in range(label_size):
            labels[i][mask_np==i+1] = 1.0
        return labels

    def __getitem__(self, index):
        # img = PIL.Image.open(self.data_img[index]).convert('RGB')
        img = PIL.Image.open(self.data_img[index])
        label = PIL.Image.open(self.data_label[index]).convert('L')
        ### mask background of image ###
        if self.background_mask:
            trans = transforms.Compose([transforms.ToTensor()])
            trans_inv = transforms.Compose([transforms.ToPILImage()])
            i, l = trans(img), trans(label.resize((1024, 1024), resample=PIL.Image.NEAREST)) * 255.
            l = l.expand_as(i)
            i[l == 0] = 0
            img = trans_inv(i)
        ################################
        img = self.transform_image(img) # [-1, 1] after normalization
        label = self.transform_label(label)
        if random.random() > 0.5:
            img = transforms.functional.hflip(img)
            label = transforms.functional.hflip(label)

        label = self._mask_labels((label * 255.)[0])
        label = (label - 0.5) / 0.5 # 如何解决mask中有虚线: 更换插值的模式
        label = torch.tensor(label, dtype=torch.float)
        if not self.return_label:
            return img, 0
        return img, label, 0


text:
#!/usr/bin/env python3
# encoding: utf-8

# Copyright 2020 Thai-Son Nguyen
# Licensed under the Apache License, Version 2.0 (the "License")

import argparse
import matplotlib.pyplot as plt
import numpy as np
import torch

from pynn.util import load_object
from pynn.decoder.s2s import beam_search
from pynn.decoder.ctc import greedy_search, greedy_align, viterbi_align
from pynn.util.text import load_dict, load_label
from pynn.io.audio_seq import SpectroDataset

parser = argparse.ArgumentParser(description='pynn')
parser.add_argument('--s2s-model', help='s2s model dictionary', default=None)
parser.add_argument('--model-dic', help='model dictionary', required=True)

parser.add_argument('--dict', help='dictionary file', default=None)
parser.add_argument('--data-scp', help='path to data scp', required=True)
parser.add_argument('--data-lbl', help='label file', default=None)
parser.add_argument('--downsample', help='concated frames', type=int, default=1)
parser.add_argument('--mean-sub', help='mean subtraction', action='store_true')
parser.add_argument('--label', help='label file', default=None)

parser.add_argument('--len-norm', help='length normalization', action='store_true')
parser.add_argument('--batch-size', help='batch size', type=int, default=80)
parser.add_argument('--alg-beam', help='beam size', type=int, default=20)
parser.add_argument('--blank', help='blank field', type=int, default=0)
parser.add_argument('--blank-scale', help='blank scale', type=float, default=1.0)
parser.add_argument('--max-len', help='max len', type=int, default=200)
parser.add_argument('--space', help='space token', type=str, default='<space>')

if __name__ == '__main__':
    args = parser.parse_args()

    dic = load_dict(args.dict)[0]
    lbl = None if args.data_lbl is None else load_label(args.data_lbl)

    use_gpu = torch.cuda.is_available()
    device = torch.device('cuda' if use_gpu else 'cpu')

    mdic = torch.load(args.model_dic)
    model = load_object(mdic['class'], mdic['module'], mdic['params'])
    model = model.to(device)
    model.load_state_dict(mdic['state'])
    model.eval()

    if args.s2s_model is not None:
        mdic = torch.load(args.s2s_model)
        s2s = load_object(mdic['class'], mdic['module'], mdic['params'])
        s2s.load_state_dict(mdic['state'])
        s2s = s2s.to(device)
        s2s.eval()
    else:
        s2s = model

    reader = SpectroDataset(args.data_scp, mean_sub=args.mean_sub, downsample=args.downsample)
    bs, blank, scale = args.alg_beam, args.blank, args.blank_scale
    fout = open('label.lbl', 'w')
    with torch.no_grad():
        while True:
            src, mask, utts = reader.read_batch_utt(args.batch_size)
            if utts is None or len(utts) == 0: break
            src, mask = src.to(device), mask.to(device)
            if lbl is None:
                hypos = beam_search(s2s, src, mask, device, 8, args.max_len, len_norm=args.len_norm)[0]
                lbs = []
                for hypo in hypos:
                    hp = []
                    for token in hypo:
                        if token == 2: break
                        hp.append(token)
                    lbs.append(hp)
            else:
                lbs = [[el+2 for el in lbl[utt]] for utt in utts]
            l = max(len(lb) for lb in lbs)
            tgt = [[1] + lb + [2] + [0]*(l-len(lb)) for lb in lbs]
            tgt = torch.LongTensor(tgt).to(device)

            mask, probs = model.align(s2s, src, mask, tgt)[1:3]
            probs = probs.log_softmax(dim=-1).cpu()
            lens = mask.sum(-1).cpu()

            for utt, l, prob, lb in zip(utts, lens, probs, lbs):
                prob, lb = prob[:l], torch.LongTensor(lb)
                #alg = greedy_align(prob, lb)
                #if len(alg) != len(lb):
                alg = viterbi_align(prob, lb, bs, blank, scale)
                if len(alg) != len(lb):
                    print(utt)
                    print(lb.tolist())
                    print([a[0] for a in alg])
                    continue
                tg = np.array([2] * l)
                for tk, st, et in alg: tg[st:et] = tk
                fout.write('%s %s\n' % (utt, ' '.join(map(str, tg))))
    fout.close()

max_stars_repo_name: enesyugan/yapay-nn

max_issues_repo_name: enesyugan/yapay-nn

max_forks_repo_name: enesyugan/yapay-nn


text:
"""

Optimal control problem with custom objective and free terminal condition.

Authors: John D'Angelo, Shreyas Sudhaman

"""

import sys
sys.path.insert(0, '..')

import numpy as np
from sympy import symbols

from pydcol.CollocMethods import *
from pydcol.ProblemDefinition import CollocationProblem
from pydcol.Objective import CustomObjective

if __name__ == "__main__":
	# collocation type
	colloc_method = EF

	# define variables
	y1, y2 = symbols("y1 y2")
	u = symbols("u")
	state_vars = [y1, y2]
	control_vars = [u]

	# Given system equations
	ode = [0.5*y1 + u, y1**2 + 0.5*u**2]

	t0_ = 0
	tf_ = 1
	N_ = 10

	X_start = np.array([1, 0]) # known initial state
	# free terminal state

	# bounds
	bounds = [[None, None],[None, None],[None, None]]
	tspan = np.linspace(t0_, tf_, N_)

	# Custom objective
	def my_eval(arg):
		# we want to minimize y2(1)
		return arg[N_ * 3 - 2]
	def my_jac(arg):
		jac = np.zeros(arg.size, dtype=float)
		jac[N_ * 3 - 2] = 1
		return jac

	def my_hess(arg):
		return np.zeros((arg.size, arg.size), dtype=float)

	my_objective = CustomObjective()
	my_objective.eval = my_eval
	my_objective.jac = my_jac
	my_objective.hess = my_hess

	# Define problem
	print("Setup")
	problem = CollocationProblem(state_vars, control_vars, ode, tspan, X_start, colloc_method=colloc_method, custom_objective=my_objective)

	# solve problem
	print("Solve")
	sol_c = problem.solve(bounds=bounds, solver='scipy')

	# evaluate solution
	problem.evaluate(ivp_method='Radau')

reason: import numpy,from sympy


text:
# Author: An Jiaoyang
# 2018/9/7 23:31
# =============================
"""Style Transfer Net
"""
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
from nets.vgg19_net import VGG19
from tensorflow.contrib.eager.python import tfe

class StyleTransferNet:
    """Style Transfer Net"""
    def __init__(self, vgg19_npy_path, alpha=0.001, beta=1., optimizer='adam', learning_rate=1., momentum=0.9):
        self.vgg19 = VGG19(vgg19_npy_path, use_avg_pool=True)
        self.vgg19.init_all_variables()
        self.content = None
        self.style = None
        self.output = None
        optimizer_list = {'adam': tf.train.AdamOptimizer,
                          'sgd': tf.train.GradientDescentOptimizer,
                          'momentum': lambda lr: tf.train.MomentumOptimizer(lr, momentum)}
        self.optimizer = optimizer_list[optimizer](learning_rate)
        self.alpha = alpha
        self.beta = beta
        self.VGG_MEAN = [103.939, 116.779, 123.68]

    def set_images(self, content_path, style_path):
        self.content = self.read_image(content_path)
        self.style = self.read_image(style_path, output_size=self.content.get_shape().as_list()[1: 3])
        self.output = tfe.Variable(tf.random_normal(self.content.get_shape(), dtype=tf.float32))

    def _loss(self):
        content_layers = self.vgg19(self.content, return_layers=True)
        style_layer = self.vgg19(self.style, return_layers=True)
        output_layer = self.vgg19(self.output, return_layers=True)

        # content loss
        content_layers_name = ['conv4_2']
        losses = []
        for L in content_layers_name:
            losses.append(0.5 * tf.reduce_mean(tf.square(content_layers[L] - output_layer[L])))
        content_loss = tf.add_n(losses)

        # style loss
        style_layer_name = ['conv1_1', 'conv2_1', 'conv3_1', 'conv4_1', 'conv5_1']
        layer_weight = [1 / 5, 1 / 5, 1 / 5, 1 / 5, 1 / 5]
        losses = []
        for L, w in zip(style_layer_name, layer_weight):
            style = style_layer[L]
            output = output_layer[L]
            shape = style.get_shape().as_list()
            new_shape = [shape[0], shape[1] * shape[2], shape[3]]
            style = tf.reshape(style, new_shape)
            output = tf.reshape(output, new_shape)
            # gram matrix
            g_style = tf.matmul(style, style, transpose_a=True)
            g_output = tf.matmul(output, output, transpose_a=True)
            loss = w * 1 / (4 * (new_shape[1] ** 2) * (new_shape[2] ** 2)) * tf.reduce_mean(tf.square(g_style - g_output))
            losses.append(loss)
        style_loss = tf.add_n(losses)

        # total loss
        total_loss = self.alpha * content_loss + self.beta * style_loss
        return total_loss

    def train(self):
        if self.content is None:
            raise ValueError('Input image is empty.')
        with tf.GradientTape() as g:
            g.watch(self.output)
            loss = self._loss()
        grad = g.gradient(loss, self.output)
        self.optimizer.apply_gradients([(grad, self.output)], global_step=tf.train.get_or_create_global_step())
        return loss

    def read_image(self, path, output_size=None):
        image = tf.image.decode_image(tf.read_file(path))
        if output_size is not None:
            image = tf.image.resize_images(image, output_size)
        image = tf.to_float(image)
        r, g, b = tf.split(image, 3, axis=2)
        image = tf.concat([b - self.VGG_MEAN[0], g - self.VGG_MEAN[1], r - self.VGG_MEAN[2]], axis=2)
        return tf.expand_dims(image, axis=0)

    def get_output_image(self):
        image = self.output[0]
        b, g, r = tf.split(image, 3, axis=2)
        image = tf.concat([r + self.VGG_MEAN[2], g + self.VGG_MEAN[1], b + self.VGG_MEAN[0]], axis=2)
        image = np.clip(image.numpy(), 0, 255).astype(np.uint8)
        return image

    def save_image(self, output_path):
        plt.imsave(output_path, self.get_output_image())

max_stars_repo_name: DHZS/image-style-transfer-tensorflow

max_issues_repo_name: DHZS/image-style-transfer-tensorflow

max_forks_repo_name: DHZS/image-style-transfer-tensorflow


text:
import os, re
import glob
import numpy as np
import string

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from GEN_Utils import FileHandling

from analysis_scripts_HTT_edited.pixel_functions import image_processor, sorted_nicely, scattbar_plotter, pixel_cleaner, roi_matcher, track_plotter

from loguru import logger

logger.info("Import OK")

#This functionality has been moved to this 'master' file to make processing simpler
# collect list of files in the input_folder that have .csv extension
input_folder = ''
output_path = ''

trans_threshold = 700 # raw mean intensity in mCherry channel
agg_threshold = 20 # percentage of pixels in a cell needed to be marked as aggregate
GFP_threshold = 1500

# Check output folder exists - if not, create it
if not os.path.exists(output_path):
    os.mkdir(output_path)

file_list = [filename for filename in os.listdir(input_folder) if 'pixels.csv' in filename]
logger.info(f'The following files were detected from {input_folder}:\n {file_list}')

# collect list of files in the input_folder that have .csv extension
image_names = sorted_nicely(list(set([("_".join(name.split("_")[0:5])) for name in file_list if 'cells' not in name])))
logger.info(f'The following image names were detected:\n {image_names}')

mutants = list(set([name.split('_')[0] for name in image_names]))

# Clean ROI pixels for first timepoint to simple dataframe for each image
pixel_dict = {}
summary_dict = {}
for image_name in image_names:
    if 't_0' in image_name:
        image = image_name + '_mCherry'
        cleaned_pixels = image_processor(input_folder, image, output_path,  save_file=False)
        pixel_dict[image_name] = cleaned_pixels

        summary_nuclei = cleaned_pixels['nuclei_pixels'].groupby('ROI_name').mean()
        summary_nuclei['image_name'] = image_name
        summary_nuclei.rename(columns={'mCherry_nuclei' : 'Intensity'}, inplace=True)

        summary_cyto = cleaned_pixels['cyto'].groupby('ROI_name').mean()
        # Calculate % of saturated pixels as a way to mark cells with aggregates
        sat_pixels = []
        for ROI_name, df in cleaned_pixels['cyto'].groupby('ROI_name'):
            sat_pixels.append(df['mCherry_cells'].mask(df['mCherry_cells'] < 65535).count() / df.shape[0] * 100)
        summary_cyto['saturated'] = sat_pixels
        summary_cyto['agg'] = [1 if sat_percent > agg_threshold else 0 for sat_percent in summary_cyto['saturated']]
        summary_cyto['image_name'] = image_name
        # Filter cell ROIs according to transfected
        summary_cyto = summary_cyto[summary_cyto['mCherry_cells'] > trans_threshold]

        summary_dict[image_name] = {'nuclei':summary_nuclei, 'cyto':summary_cyto}
    else:
        nuclear_path = input_folder+image_name+'_mCherry_nuclei_pixels.csv'
        logger.info(f'Processing nuclear pixels from {image_name}')
        nuclei_pixels = pixel_cleaner(nuclear_path, image_name)
        pixel_dict[image_name] = {'nuclei_pixels':nuclei_pixels}

        summary_nuclei = nuclei_pixels.groupby('ROI_name').mean()
        summary_nuclei.rename(columns={'mCherry_nuclei' : 'Intensity'}, inplace=True)
        summary_nuclei['image_name'] = image_name

        summary_dict[image_name] = {'nuclei':summary_nuclei}

logger.info('All images processed successfully.')

# Additional processing for the GFP channel
GFP_summary_dict = {}
t1_images = [image for image in image_names if 't_1' in image]
for image_name in t1_images:
    image = image_name + '_GFP'
    cleaned_pixels = image_processor(input_folder, image, output_path,  save_file=False)
    summary_cyto = cleaned_pixels['cyto'].groupby('ROI_name').mean()
    # Calculate % of saturated pixels as a way to mark cells with aggregates
    sat_pixels = []
    for ROI_name, df in cleaned_pixels['cyto'].groupby('ROI_name'):
        sat_pixels.append(df['GFP_cells'].mask(df['GFP_cells'] < 65535).count() / df.shape[0] * 100)
    summary_cyto['saturated'] = sat_pixels
    summary_cyto['agg'] = [1 if sat_percent > agg_threshold else 0 for sat_percent in summary_cyto['saturated']]
    summary_cyto['image_name'] = image_name
    # Filter cell ROIs according to transfected
    summary_cyto = summary_cyto[summary_cyto['GFP_cells'] > GFP_threshold]

    GFP_summary_dict[image_name] = {'nuclei':summary_nuclei, 'cyto':summary_cyto}

# Determine cotransfected cells, generate summary cyto dataframe
cyto_summary = {}
for mutant in mutants:
    pos_list = list(set([name.split('_')[2] for name in image_names if mutant in name]))
    position_cyto = {}
    for pos in pos_list:
        mCherry_image_name = f'{mutant}_pos_{pos}_t_0'
        mCherry_summary = summary_dict[mCherry_image_name]['cyto']
        GFP_image_name = f'{mutant}_pos_{pos}_t_1'
        GFP_summary = GFP_summary_dict[GFP_image_name]['cyto']

        cotrans_summary = pd.merge(mCherry_summary, GFP_summary.drop(['X_pos', 'Y_pos'], axis=1), how='outer', left_index=True, right_index=True, suffixes=('_mCherry', '_GFP'))

        cotrans_summary['cotrans'] = [1 if roi_name in cotrans_summary.dropna().index else 0 for roi_name in cotrans_summary.index]

        cyto_summary[mCherry_image_name] = cotrans_summary

# Generate compiled summary_df for all images, after adding descriptive nuc_id to each ROI
summary_list = []
for key, summary_df in cyto_summary.items():
    # Add additional info from image names
    summary_df = summary_df.dropna()
    summary_df['mutant'] = summary_df['image_name_mCherry'].str.split('_').str[0]
    summary_df['position'] = summary_df['image_name_mCherry'].str.split('_').str[2]
    summary_df['number'] = [str(number) for number in range(0, summary_df.shape[0])]
    summary_df['cell_id'] = list(zip(summary_df['mutant'], summary_df['position'], summary_df['number']))
    summary_df['cell_id'] = ['_'.join(tuple) for tuple in summary_df['cell_id']]
    summary_dict[key]['cyto'] = summary_df
    summary_list.append(summary_df)
mean_cell_summary = pd.concat(summary_list)

FileHandling.df_to_excel(output_path+f'mean_cell_summary.xlsx', sheetnames=['summary'], data_frames=[mean_cell_summary.reset_index()])

# # Checking out the summary results
# pixel_dict.keys() # shows the image names that were processed i.e. key 1
# pixel_dict['barnaseWT+Q25_pos_001_t_0']['cyto'].keys() # Shows the available dataframes for each image i.e. key 2
# pixel_dict['Ex2_pos_1_t_0']['nuclei_pixels'] # shows the cytoplasm pixel summary for the first image
# summary_dict.keys()
# summary_dict['Ex2_pos_1_t_0'].keys()
#
# summary_dict['barnaseWT+Q25_pos_001_t_0']['cyto']

# plot individual image for t0 to test segmentation with ROI name overlayed
for image_name in [name for name in image_names if 't_0' in name]:
    pixel_cyto = pixel_dict[image_name]['cyto']
    pixel_nuc = pixel_dict[image_name]['nuclei_pixels']
    summary_df = pd.DataFrame(summary_dict[image_name]['cyto'])

    fig, ax = plt.subplots(figsize=(10.24, 10.24))
    plt.scatter(pixel_nuc['X_pos'], pixel_nuc['Y_pos'], c=pixel_nuc['mCherry_nuclei'], cmap='Blues', s=1)

    plt.scatter(pixel_cyto['X_pos'], pixel_cyto['Y_pos'], c=pixel_cyto['mCherry_cells'], cmap='Reds', s=1)

    for roi_name in summary_df.index.tolist():
        plt.annotate(roi_name, (summary_df.loc[roi_name, 'X_pos'], summary_df.loc[roi_name, 'Y_pos']))
    plt.xlim(0, 1024)
    plt.ylim(0, 1024)

    plt.savefig(f'{output_path}{image_name}_image.png')

# Generate compiled summary_df for all images, after adding descriptive nuc_id to each ROI
summary_list = []
for image_name in image_names:
    summary_df = summary_dict[image_name]['nuclei']
    # Add additional info from image names
    summary_df['mutant'] = summary_df['image_name'].str.split('_').str[0]
    summary_df['position'] = summary_df['image_name'].str.split('_').str[2]
    summary_df['time'] = summary_df['image_name'].str.split('_').str[4]
    summary_df['number'] = [str(number) for number in range(0, summary_df.shape[0])]
    summary_df['nuc_id'] = list(zip(summary_df['mutant'], summary_df['position'], summary_df['time'], summary_df['number']))
    summary_df['nuc_id'] = ['_'.join(tuple) for tuple in summary_df['nuc_id']]
    summary_dict[image_name]['nuclei'] = summary_df
    summary_list.append(summary_df)
mean_nuclei_summary = pd.concat(summary_list)

# Save relevant df to excel, hdf5
# FileHandling.df_to_excel(output_path+f'mean_summary.xlsx', ['summary_sorted'], data_frames=[mean_nuclei_summary])

# Generate nuclei tracks according to maximum moved distance, assign new name
mutant_mapped = {}
for mutant in mutants:
    pos_list = list(set([name.split('_')[2] for name in image_names if mutant in name]))
    position_mapped = {}
    for pos in pos_list:

        # Get list of nuclei for cells in t0 (only keep these nuclei and track these through time)
        #---> There are some cells without nuclei in t0, some cells that are too close to many nuclei and some that have only one. Optimising the tolerance is necessary to best capture the relevant types.
        df_a = cyto_summary[f'{mutant}_pos_{pos}_t_0'][cyto_summary[f'{mutant}_pos_{pos}_t_0']['cotrans'] == 1].copy()
        # Filter only cotransfected cells
        df_a['coords'] = list(zip(round(df_a['X_pos'], 0), round(df_a['Y_pos'], 0)))

        df_b = summary_dict[f'{mutant}_pos_{pos}_t_0']['nuclei'].copy()
        df_b['coords'] = list(zip(round(df_b['X_pos'], 0), round(df_b['Y_pos'], 0)))

        t0_cell_nuclei = roi_matcher(df_a, df_b.set_index('nuc_id'), col='coords', x_tolerance=40, y_tolerance=40)

        timepoint_names = sorted_nicely(list(set(mean_nuclei_summary['time'])))
        logger.info(f'The following timepoints were detected:\n {timepoint_names}')
        #define empty df to store tracking info, set column 1 to be cell_ids from t0
        roi_mapped = pd.DataFrame()
        roi_mapped[timepoint_names[0]] = list(t0_cell_nuclei.values())
        for x, timepoint in enumerate(timepoint_names[:-1]):
            # select ROI in first timepoint --> find matching ROI in second timepoint
            # repeat up to second last timepoint i.e. x matched with x+1 up to (len(timepoints)-1)
            df_a = summary_dict[f'{mutant}_pos_{pos}_t_{timepoint_names[x]}']['nuclei']
            df_a['coords'] = list(zip(round(df_a['X_pos'], 0), round(df_a['Y_pos'], 0)))

            df_b = summary_dict[f'{mutant}_pos_{pos}_t_{timepoint_names[x+1]}']['nuclei']
            df_b['coords'] = list(zip(round(df_b['X_pos'], 0), round(df_b['Y_pos'], 0)))
            # find matching ROIs between two timepoints - can play with the tolerance here!
            matched_rois = roi_matcher(df_a.set_index('nuc_id'), df_b.set_index('nuc_id'), col='coords', x_tolerance=30, y_tolerance=30)
            #Add new column to the dataframe for new timepoint by mapping the previous timepoint with the matched rois
            roi_mapped[timepoint_names[x+1]] = roi_mapped[timepoint_names[x]].map(matched_rois)
        # add new column with updated nuclei id for a single track
        roi_mapped['track_id'] = [f'{mutant}_pos_{pos}_nuc_{x}' for x in range(0, len(roi_mapped))]
        position_mapped[pos] = roi_mapped
    mutant_mapped[mutant] = position_mapped

# generate a dictionary mapping the cell_id to cell_track_id
track_dict = {}
for mutant in mutants:
    pos_list = list(set([name.split('_')[2] for name in image_names if mutant in name]))
    position_mapped = mutant_mapped[mutant]
    for pos in pos_list:
        mapped_rois = position_mapped[pos]
        for timepoint in timepoint_names:
            track_dict.update(dict(zip(mapped_rois[timepoint], mapped_rois['track_id'])))
# remove nan key, update background key as unnecessary
track_dict.pop(np.nan, None)

mean_nuclei_summary['track_id'] = mean_nuclei_summary['nuc_id'].map(track_dict)
mean_nuclei_summary['time'] = mean_nuclei_summary['time'].astype('int')
summary_sorted = mean_nuclei_summary.sort_values(['position','track_id', 'time']).dropna()
FileHandling.df_to_excel(output_path+f'mean_nuclei_summary.xlsx', sheetnames=['summary_sorted'], data_frames=[summary_sorted.reset_index()])

# Generate grouped lists per mutant
summary_dfs = []
summary_names = []
for group, df in summary_sorted.groupby('mutant'):
    summary_dfs.append(df.reset_index())
    summary_names.append(group)
FileHandling.df_to_excel(output_path+f'summary_per_mutant.xlsx', sheetnames=summary_names, data_frames=summary_dfs)

plottable = summary_sorted[['track_id', 'time', 'Intensity']]
plottable = plottable.pivot(index='time', columns='track_id', values='Intensity').reset_index().sort_values('time', axis=0)
# Generate grouped lists per mutant
plottable_dfs = []
plottable_names = []
for mutant in mutants:
    plottable_dfs.append(plottable[[col for col in plottable.columns.tolist() if mutant in col]].reset_index())
    plottable_names.append(group)
FileHandling.df_to_excel(output_path+f'plottable_per_mutant.xlsx', plottable_names, data_frames=plottable_dfs)

# to plot image of cyto/nucleo pixels for first timepoint, with track_id labelled
for mutant in mutants:
    for pos in pos_list:
        image_name = f'{mutant}_pos_{pos}_t_0'
        pixel_cyto = pixel_dict[image_name]['cyto']
        pixel_nuc = pixel_dict[image_name]['nuclei_pixels']
        summary_df = pd.DataFrame(summary_dict[image_name]['cyto'])
        # generate list of ROIs to annotate
        label_df = summary_sorted[summary_sorted['time'] == int(timepoint_names[0])]
        label_df = label_df[['track_id', 'X_pos', 'Y_pos']].set_index('track_id')

        fig, ax = plt.subplots(figsize=(10.24, 10.24))
        plt.scatter(pixel_nuc['X_pos'], pixel_nuc['Y_pos'], c=pixel_nuc['mCherry_nuclei'], cmap='Blues', s=1)
        plt.scatter(pixel_cyto['X_pos'], pixel_cyto['Y_pos'], c=pixel_cyto['mCherry_cells'], cmap='Reds', s=1)

        for label in label_df.index.tolist():
            text_label = label.split('_')[-1]
            plt.annotate(text_label, (round(label_df.loc[label,'X_pos'], 0), round(label_df.loc[label,'Y_pos'], 0)))
        plt.xlim(0, 1024)
        plt.ylim(0, 1024)
        plt.title(image_name)
        plt.savefig(f'{output_path}{image_name}_trackID.png')

# To plot track coords over time
for mutant in mutants:
    image_df = summary_sorted[summary_sorted['mutant'] == mutant]
    for pos in pos_list:
        image_df = image_df[image_df['position'] == pos]
        fig = track_plotter(image_df, track_col='track_id')
        plt.title(f'{mutant}_pos_{pos}')
        plt.savefig(f'{output_path}{mutant}_pos_{pos}_track_movement.png')

# To plot individual intensities for each position over time
for x, df in enumerate(plottable_dfs):
    fig, ax = plt.subplots()
    plt.plot(df)
    plt.xlabel('Timepoint')
    plt.ylabel('Mean Nuclear mCherry Intensity (A.U.)')
    plt.title(plottable_names[x])


text:
# third party
import numpy as np

# syft absolute
import syft as sy
from syft.core.adp.data_subject_ledger import DataSubjectLedger

def test_data_subject_ledger_serde() -> None:
    rdp_constants = np.array([1, 2, 3], np.float64)

    dsl = DataSubjectLedger()
    dsl._rdp_constants = rdp_constants

    ser = sy.serialize(dsl, to_bytes=True)
    de = sy.deserialize(ser, from_bytes=True)

    assert de == dsl
    assert all(de._rdp_constants == rdp_constants)

max_stars_repo_name: jackbandy/PySyft

max_issues_repo_name: jackbandy/PySyft

max_forks_repo_name: jackbandy/PySyft


text:
from Model import Model
import numpy as np
from pysgpp.extensions.datadriven.uq.uq_setting.UQBuilder import UQBuilder

class Parabola(Model):

    def buildUQSetting(self):
        f = lambda x, **kws: np.prod([4 * xi * (1 - xi) for xi in x])
        self.uqBuilder.withSimulation(f)

class Atan(Model):

    def buildUQSetting(self):
        f = lambda x, **kws: np.atan(50 * (x[0] - .35)) + pi / 2 + 4 * x[1] ** 3 + exp(x[0] * x[1] - 1)
        self.uqBuilder.withSimulation(f)

max_stars_repo_name: valentjn/thesis

max_issues_repo_name: valentjn/thesis

max_forks_repo_name: valentjn/thesis


text:
import cv2
import numpy as np

from libs.boxes.bbox_transform import clip_boxes

def _factor_closest(num, factor):
    num = int(np.ceil(float(num) / factor) * factor)
    return num

def _im_list_to_blob(ims):
    """Convert a list of images into a network input.

    Assumes images are already prepared (means subtracted, BGR order, ...).
    """
    max_shape = np.array([im.shape for im in ims]).max(axis=0)
    num_images = len(ims)
    blob = np.zeros((num_images, max_shape[0], max_shape[1], 3),
                    dtype=np.float32)
    for i in xrange(num_images):
        im = ims[i]
        blob[i, 0:im.shape[0], 0:im.shape[1], :] = im

    return blob

def im_preprocess(im, inp_size=(600,), max_size=1000):
    """Converts an image into a network input.
    Arguments:
        im (ndarray): a color image in BGR order
        inp_size (tuple): (600,)
        max_size (int): 1000
    Returns:
        im_data (ndarray): a data blob holding an image pyramid
        im_info (ndarray): [ [ h, w, scale] ]
    """
    im_orig = im.astype(np.float32, copy=True)
    # im_orig -= PIXEL_MEANS

    im_shape = im_orig.shape
    im_size_min = np.min(im_shape[0:2])
    im_size_max = np.max(im_shape[0:2])

    processed_ims = []
    im_scale_xfactors = []
    im_scale_yfactors = []

    for target_size in inp_size:
        im_scale = float(target_size) / float(im_size_min)
        # Prevent the biggest axis from being more than MAX_SIZE
        if np.round(im_scale * im_size_max) > max_size:
            im_scale = float(max_size) / float(im_size_max)

        # factor 32
        im_scale_x = _factor_closest(im_scale * im_shape[1], 32) / float(im_shape[1])
        im_scale_y = _factor_closest(im_scale * im_shape[0], 32) / float(im_shape[0])

        im = cv2.resize(im_orig, None, None, fx=im_scale_x, fy=im_scale_y,
                        interpolation=cv2.INTER_LINEAR)
        im_scale_xfactors.append(im_scale_x)
        im_scale_yfactors.append(im_scale_y)
        processed_ims.append(im)

    # Create a blob to hold the input images
    im_data = _im_list_to_blob(processed_ims)
    # im_scales = np.array(im_scale_factors)
    im_info = np.array(
        [
            [im_data.shape[1], im_data.shape[2], im_scale_yfactors[i], im_scale_xfactors[i]]
            for i in range(len(im_scale_xfactors))
            ],
        dtype=np.float)

    return im_data, im_info

def imcv2_recolor(im, a=.1):
    t = np.random.uniform(-1, 1, 3)

    # random amplify each channel
    im = im.astype(np.float)
    im *= (1 + t * a)
    mx = 255. * (1 + a)
    up = np.random.uniform(-1, 1)
    im = np.power(im / mx, 1. + up * .5)
    return np.array(im * 255., np.uint8)

def imcv2_affine_trans(im):
    # Scale and translate
    h, w, c = im.shape
    scale = np.random.uniform() / 10. + 1.
    max_offx = (scale - 1.) * w
    max_offy = (scale - 1.) * h
    offx = int(np.random.uniform() * max_offx)
    offy = int(np.random.uniform() * max_offy)

    im = cv2.resize(im, (0, 0), fx=scale, fy=scale)
    im = im[offy: (offy + h), offx: (offx + w)]
    flip = np.random.uniform() > 0.5
    if flip:
        im = cv2.flip(im, 1)

    return im, [scale, [offx, offy], flip]

def _offset_boxes(boxes, im_shape, scale, offs, flip):
    if len(boxes) == 0:
        return boxes
    boxes = np.asarray(boxes, dtype=np.float)
    boxes *= scale
    boxes[:, 0::2] -= offs[0]
    boxes[:, 1::2] -= offs[1]
    boxes = clip_boxes(boxes, im_shape)

    if flip:
        boxes_x = np.copy(boxes[:, 0])
        boxes[:, 0] = im_shape[1] - boxes[:, 2]
        boxes[:, 2] = im_shape[1] - boxes_x

    return boxes

def preprocess_train(im, boxes):
    im, trans_param = imcv2_affine_trans(im)
    scale, offs, flip = trans_param

    boxes = _offset_boxes(boxes, im.shape, scale, offs, flip)
    # boxes = np.asarray(boxes, dtype=np.int)

    im = imcv2_recolor(im)

    return im, boxes


text:
from functools import partial
from pathlib import Path
import argparse

import numpy as np
import pandas as pd

from sigtestv.stats import ResultPopulationPair, ResultPopulationSingle, StudentsTTest, ASDTest, MannWhitneyUTest, \
    SDBootstrapTest, QuantileTest, order_stochastic, order_quantile

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('--files', '--file', '-f', type=Path, nargs='+', required=True)
    parser.add_argument('--num-samples', '-n', type=int, required=True)
    parser.add_argument('--result-column-name', '-c', type=str, required=True)
    parser.add_argument('--num-samples2', '-n2', type=int)
    parser.add_argument('--result-column-name2', '-c2', type=str)
    parser.add_argument('--num-iters', '-it', type=int, default=500)
    parser.add_argument('--alpha', type=float, default=0.05)
    parser.add_argument('--asd-threshold', '--asd-th', type=float, default=0.5)
    parser.add_argument('--test', type=str, default='power', choices=['power', 'type1'])
    parser.add_argument('--quantile', type=float, default=0.2)
    parser.add_argument('--quantity',
                        type=str,
                        default='stochastic-order',
                        choices=['stochastic-order', 'quantile', 'mean'])
    args = parser.parse_args()

    if args.result_column_name2 is None:
        args.result_column_name2 = args.result_column_name
    if args.num_samples2 is None:
        args.num_samples2 = args.num_samples

    if args.quantity == 'stochastic-order':
        ttest = StudentsTTest(dict(equal_var=False))
        asd_test = ASDTest(dict(threshold=args.asd_threshold))
        mwu_test = MannWhitneyUTest()
        bs_test = SDBootstrapTest()
        tests = (ttest, asd_test, mwu_test, bs_test)
        order_fn = order_stochastic
    elif args.quantity == 'quantile':
        options = dict(quantile=args.quantile, estimate_method='harrelldavis')
        q_test_hd = QuantileTest(options=options)
        options = options.copy()
        options['estimate_method'] = 'direct'
        q_test_direct = QuantileTest(options=options)
        tests = (q_test_hd, q_test_direct)
        order_fn = partial(order_quantile, quantile=args.quantile)

    pop_x = pd.read_csv(args.files[0], sep='\t', quoting=3)[args.result_column_name]
    if args.test == 'power':
        pop_y = pd.read_csv(args.files[1], sep='\t', quoting=3)[args.result_column_name2]
        pop_pair = ResultPopulationPair(pop_x, pop_y)
        results = pop_pair.simulate_statistical_power(args.num_samples,
                                                      tests,
                                                      use_tqdm=True,
                                                      n2=args.num_samples2,
                                                      iters=args.num_iters,
                                                      alpha=args.alpha,
                                                      order_fn=order_fn)
    else:
        pop_single = ResultPopulationSingle(pop_x)
        results = pop_single.simulate_type1_error(args.num_samples,
                                                  tests,
                                                  use_tqdm=True,
                                                  iters=args.num_iters,
                                                  alpha=args.alpha)

    print('name\tresult')
    for test_name, result in results.items():
        print(f'{test_name}\t{result:.4f}')

if __name__ == '__main__':
    main()

max_stars_repo_name: castorini/sigtestv

max_issues_repo_name: castorini/sigtestv

max_forks_repo_name: castorini/sigtestv


text:
"""!
"""

import os
import cv2
import natsort
import argparse
import numpy as np
import pandas as pd
from feature_extractor import FeatureExtractor
from mtm import get_best_features, template_matching, build_pyramid
from postprocessing import postprocessing
from utils import get_statistical_measures, show_output_images

def get_args():
    """!@brief
    -------------------------------------------------------------------------
    | Video | Avg_size | Radius |        Template       | BS_1 | BS_2 | BS_3 |
    |-------|----------|--------|-----------------------|------|------|------|
    | B1    | 7        | 3.5    | [312, 159, 323, 172]  | 13   | 13   | 15   |
    | B2    | 7        | 3.5    | [113, 178, 122, 187]  | 9    | 9    | 19   |
    | C1    | 25       | 12.5   | [251, 120, 308, 181]  | 61   | 59   | 63   |
    | C2    | 5        | 2.5    | [994, 578, 1009, 595] | 17   | 17   | 15   |
    | SC    | 5        | 2.5    | [219, 178, 230, 193]  | 15   | 15   | 15   |
    | ME    | 11       | 5.5    | [245, 216, 268, 237]  | 23   | 29   | 25   |
    -------------------------------------------------------------------------

    Labels:
        - Avg_size: The average size of all cells in the video.
        - Radius: Avg_size / 2.
        - Template: The main (first) template coordinates.
        - BS_x: The block size value for the number x of templates used.
    """
    parser = argparse.ArgumentParser()
    parser.add_argument("-v", "--verbosity", help="Increase output verbosity.", action="store_false")
    parser.add_argument("-f", "--folder-path", type=str, help="Folder path for the input images.")
    parser.add_argument("-m", "--mask-img", type=str, help="Mask image file.")
    parser.add_argument("-g", "--ground-truth", type=str, help="Ground truth text file.")
    parser.add_argument("-t", "--template", type=str, help="Text file with templates positions.")
    parser.add_argument("-d", "--dcnn-model", type=str, help="DCNN model to extract features.", default="resnet101")
    parser.add_argument("-o", "--output", type=str, help="Output verbosity file.", default=None)
    parser.add_argument("-p", "--output_points", type=str, help="Output detections file.", default=None)

    parser.add_argument("-mins", "--min-side", type=int, help="Image min side.", default=1000)
    parser.add_argument("-maxs", "--max-side", type=int, help="Image max side.", default=1400)
    parser.add_argument("-tfea", "--thres-feature", type=float, help="TM threshold for features selection.", default=0.9)
    parser.add_argument("-ret", "--retained-value", type=float, help="Retained value for features selection.", default=0.1)
    parser.add_argument("-rfea", "--radius-feature", type=float, help="Radius value for features selection.", default=5.0)
    parser.add_argument("-tbin", "--thres-binary", type=float, help="Threshold value for binarization.", default=0.9)
    parser.add_argument("-npyr", "--pyramid-levels", type=int, help="Number of template pyramid levels.", default=1)
    parser.add_argument("-tecc", "--thres-ecc", type=float, help="Threshold value for eccentricity post-processing.", default=0.62)
    parser.add_argument("-cons", "--constant", type=float, help="Constant value for Adaptive Thresholding.", default=-25.5)

    return parser.parse_args()

def dcnn_mtm(verbosity=False,
             folder_path=None,
             mask_img=None,
             ground_truth=None,
             template=None,
             dcnn_model=None,
             output=None,
             output_points=None,
             min_side=1000,
             max_side=1400,
             thres_feature=0.9,
             retained_value=0.1,
             radius_feature=5.0,
             thres_binary=0.9,
             pyramid_levels=1,
             thres_ecc=0.62,
             constant=-25.5):
    """!@brief
    """
    # Read params
    mask_img = cv2.imread(mask_img, 0)
    gt = pd.read_csv(ground_truth, sep='\t', header=None, names=['frame', 'x', 'y'])

    # Extract image features from a DCNN model
    extractor = FeatureExtractor(dcnn_model)

    # ================================================
    #               FOR THE FIRST FRAME
    #
    #   1. Get output model feature images
    #   2. Find the best set of feature images
    #   3. Extract templates from ROIs pre-defined
    #   4. Compute and save all templates accordingly
    # ================================================

    # Complete image path
    img_path = folder_path + natsort.natsorted(os.listdir(folder_path))[0]
    first_frame = cv2.imread(img_path)

    # Select ROI for template images
    if template is None:
        # Manual templates selection
        templates = cv2.selectROIs("First frame", first_frame)
    else:
        # Previous selected templates
        templates = np.loadtxt(template, delimiter=' ').astype(int)

    # Adjust templates shape
    if len(templates.shape) == 1:
        templates = [templates]

    # Get the block size value from template sizes
    block_size = 0.0
    for t in templates:
        distX = np.abs(t[0]-t[2])
        distY = np.abs(t[1]-t[3])
        block_size = np.max([block_size, distX, distY])

    # Get image features and format them
    ext_outputs = extractor.get_output(img=first_frame,
                                       min_side=min_side,
                                       max_side=max_side,
                                       resize=True)

    # Find best features from model output
    # ====================================
    # n_ext_outputs = ext_outputs.shape[2]
    # print("Number of model features:", n_ext_outputs)

    feature_idxs = get_best_features(features=ext_outputs,
                                     threshold=thres_feature,
                                     retained_value=retained_value,
                                     radius=radius_feature,
                                     template=templates[0],
                                     mask_img=mask_img,
                                     gt=gt.copy())

    # print("Number of selected features:", len(feature_idxs))
    # print("Feature image indexes:", feature_idxs)

    # ======================================================================
    # # DEBUG
    # for feature_img in np.rollaxis(ext_outputs[:, :, feature_idxs], axis=2):
    #     import matplotlib.pyplot as plt
    #     plt.matshow(feature_img, cmap='gray')
    #     plt.show()
    # ======================================================================

    # Get template images accordingly
    # @NOTE
    #   templates_pool[feat][templ][scale]
    # ====================================
    templates_pool = []

    # Loop for each feature of first frame
    for feature_img in np.rollaxis(ext_outputs[:, :, feature_idxs], axis=2):
        tmpl_feature = []

        # Normalize template image
        feature_img = cv2.normalize(src=feature_img,
                                    dst=None,
                                    alpha=0,
                                    beta=255,
                                    norm_type=cv2.NORM_MINMAX,
                                    dtype=cv2.CV_8U)

        # Loop for each template image
        for tmpl_pos in templates:
            tmpl = feature_img[tmpl_pos[1]:tmpl_pos[3],
                               tmpl_pos[0]:tmpl_pos[2]] # [y0:y1, x0:x1]

            # Get template scales (pyramid)
            tmpl_scale = build_pyramid(tmpl, pyramid_levels)

            tmpl_feature.append(tmpl_scale)
        templates_pool.append(tmpl_feature)

    # Do the same previous task to the original image channels
    # ========================================================

    # When image has only one channel -> reshape
    if len(first_frame.shape) == 2:
        img_aux = first_frame.reshape((first_frame.shape[0], first_frame.shape[1], 1))
    else:
        img_aux = first_frame.copy()

    # Loop in each channel
    for channel_img in np.rollaxis(img_aux, axis=2):
        tmpl_feature = []

        # Normalize template image
        channel_img = cv2.normalize(src=channel_img,
                                    dst=None,
                                    alpha=0,
                                    beta=255,
                                    norm_type=cv2.NORM_MINMAX,
                                    dtype=cv2.CV_8U)

        # Loop for each template image
        for tmpl_pos in templates:
            tmpl = channel_img[tmpl_pos[1]:tmpl_pos[3],
                               tmpl_pos[0]:tmpl_pos[2]] # [y0:y1, x0:x1]

            # Get template scales (pyramid)
            tmpl_scale = build_pyramid(tmpl, pyramid_levels)

            tmpl_feature.append(tmpl_scale)
        templates_pool.append(tmpl_feature)

    # ==============================================================
    # # DEBUG
    # for ff in range(len(templates_pool)):
    #     for tt in range(len(templates)):
    #         for ss in range(pyramid_levels):
    #             # Show all templates
    #             import matplotlib.pyplot as plt
    #             plt.matshow(templates_pool[ff][tt][ss], cmap='gray')
    #             plt.show()
    # ==============================================================

    # ========================================
    # Read all images in the folder
    # @TODO
    #      Read frames directly from a video.
    # ========================================

    # Open a text file to save output values
    if output is not None:
        F = open(output, 'a')

    all_det = pd.DataFrame() # all detection points
    # rows = ext_outputs[:, :, 0].shape[0]
    # cols = ext_outputs[:, :, 0].shape[1]
    # fourcc = cv2.VideoWriter_fourcc(*'WMV1')
    # out = cv2.VideoWriter('output_map.avi', fourcc, 16.0, (rows, cols))

    for i, f in enumerate(natsort.natsorted(os.listdir(folder_path))):
        if f.endswith('.jpg') or f.endswith('.png') or f.endswith('.jpeg'):

            if verbosity:
                # print("------------------------------------")
                print("Processing frame", f)
                # print("------------------------------------")

            # Complete image path
            img_path = folder_path + f
            img = cv2.imread(img_path)

            # Get image features and format them
            feature_imgs = extractor.get_output(img)[:, :, feature_idxs]

            # When image has only one channel -> reshape
            if len(img.shape) == 2:
                img_aux = img.reshape((img.shape[0], img.shape[1], 1))
            else:
                img_aux = img.copy()

            # Append original frame to the feature images
            feature_imgs = np.append(feature_imgs, img_aux, axis=2)

            # Initialize fusion map for all feature images after TM
            output_tm_map = np.zeros_like(feature_imgs[:, :, 0], dtype=np.float32)

            # Loop for each feature image
            # ===========================
            for j in range(feature_imgs.shape[2]):

                # Apply Multiple Template Matching
                # ================================
                out_map = template_matching(image=feature_imgs[:, :, j],
                                            templates=templates_pool[j],
                                            scale_fusion='sum',
                                            tmpl_fusion='sum')

                # ================================================
                #              FUSION FEATURE MAPS
                #
                # @NOTE: Another approach could be used to fusion
                #        pyramid level results
                #           - sum
                #           - max value
                #           - pixel-wise multiplication
                #
                #  Acumulate TM resulting maps for feature images
                # ================================================
                output_tm_map = cv2.add(out_map, output_tm_map)

            # # Save output video
            # # =================
            # # Normalize output map
            # out_img = cv2.normalize(src=output_tm_map,
            #                         dst=None,
            #                         alpha=0,
            #                         beta=255,
            #                         norm_type=cv2.NORM_MINMAX,
            #                         dtype=cv2.CV_8U)
            # out_img_rgb = cv2.cvtColor(src=out_img,
            #                            code=cv2.COLOR_GRAY2BGR)
            # out.write(out_img_rgb)

            # # Show resulting image after fusion of feature maps
            # import matplotlib.pyplot as plt
            # plt.matshow(output_tm_map, cmap='gray')
            # plt.show()

            # ======================================
            #           POST-PROCESSING
            #   Get eccentricity of points in the
            #   template matching resulting maps.
            # ======================================
            det_frame = postprocessing(img=output_tm_map,
                                       mask=mask_img,
                                       threshold=thres_binary)

            if len(det_frame) > 0:
                for det in det_frame:
                    det['frame'] = i+1

                all_det = all_det.append(det_frame)

        # if i == 2:
        #     break

    # # Release video writer
    # out.release()

    # ===================================================
    # @IMPORTANT
    #       As the dataframe "all_det" is appended and
    #       has its item values changed by index inside
    #       postprocessing function, we need to reset it.
    # ===================================================
    all_det = all_det.reset_index(drop=True)

    # Get statistical measures
    #   meas = [TP, FP, FN, P, R, F1]
    # ================================
    meas, out_gt, out_det = get_statistical_measures(gt=gt.copy(),
                                                     det=all_det.copy(),
                                                     r=block_size/2)

    # Print on terminal
    print("TP = {}, FP = {}, FN = {}, P = {:.2f}, R = {:.4f}, " \
        "F1 = {:.4f} | THRESH: {:.2f}".format(meas[0], meas[1], meas[2],
                                              meas[3], meas[4], meas[5], thres_binary))

    if output is not None:
        # Print on file
        F.write("TP = {}, FP = {}, FN = {}, P = {:.2f}, R = {:.4f}, " \
                "F1 = {:.4f} | THRESH: {:.2f}\n".format(meas[0], meas[1], meas[2],
                                                        meas[3], meas[4], meas[5], thres_binary))

    # ==================================
    # Show all frames with corresponding
    # annotations and detections
    # ==================================
    if verbosity:
        show_output_images(folder=folder_path,
                           gt=out_gt,
                           det=out_det)

    # Save output detection points in text file
    if output_points:
        out_det[['frame', 'x', 'y']].to_csv(output_points,
                                            index=False,
                                            header=False,
                                            sep=' ')

    if output is not None:
        F.close()

    # Clear memory and graphs from keras
    if extractor is not None:
        extractor.clean_model()

if __name__ == "__main__":
    args = get_args()
    dcnn_mtm(verbosity=args.verbosity,
             folder_path=args.folder_path,
             mask_img=args.mask_img,
             ground_truth=args.ground_truth,
             template=args.template,
             dcnn_model=args.dcnn_model,
             output=args.output,
             output_points=args.output_points,
             min_side=args.min_side,
             max_side=args.max_side,
             thres_feature=args.thres_feature,
             retained_value=args.retained_value,
             radius_feature=args.radius_feature,
             thres_binary=args.thres_binary,
             pyramid_levels=args.pyramid_levels,
             thres_ecc=args.thres_ecc,
             constant=args.constant)

max_stars_repo_name: brunoggregorio/DCNN-feature-extraction

max_issues_repo_name: brunoggregorio/DCNN-feature-extraction

max_forks_repo_name: brunoggregorio/DCNN-feature-extraction