text:
import os
import warnings
import matplotlib.pyplot as plt
import numpy as np
import torch
import torch.nn as nn
from bagnets.utils import *
from bagnets.clipping import *
import torchvision.transforms as transforms
import foolbox_alpha
import foolbox
from foolbox.attacks import ProjectedGradientDescentAttack as PGD
from foolbox.criteria import TopKMisclassification
from foolbox.criteria import TargetClass
from foolbox.distances import Distance
from foolbox.adversarial import Adversarial
import logging
from advertorch.attacks import LinfPGDAttack

#########################################
# Security Lower Bound
#########################################

def is_invariant(class_lower, class_upper, label, target=None, k=5):
    """Given the upper bound and lower bound of class logits of one image, determine
        whether the classifier could possibly misclassify this image in top-K prediction.
    Input:
    - class_upper (pytorch tensor): shape = (1000,) in CPU
    - class_lower (pytorch tensor): shape = (1000,) in CPU
    - label (int): image's label
    - targets (int): targeted class. None means untargeted attack
    - k (int): top-K prediction
    Output (output): whether the classifier is robust against the attack on this image
    """
    label_lower = class_lower[label].item()
    top6_values, top6_classes = torch.topk(class_upper, k=k+1)
    top6 = top6_values.numpy()
    if target is None:
        return label_lower >= top6_values[-1].item()
    else:
        return label_lower >= top6_values[-1].item() or target not in list(top6[:k])

def get_affected_patches(attack_size, location, ps=33):
    """
    """
    assert ps in [9, 17, 33]
    ps2bound = {9:27-1, 17:26-1, 33:24-1}
    boundary = ps2bound[ps] # max index in logit space
    x1, y1 = location # top-left coordinate of the sticker
    dx, dy = attack_size
    x2, y2 = x1 + dx - 1, y1 + dy - 1 # bottom-right coordinate of the sticker

    p1, q1 = max(0, x1 - ps + 1), max(0, y1 - ps + 1)
    p1, q1 = (p1 // 8) * 8, (q1 // 8) * 8 # align to the receptive field's top-left coordinate in image space
    p2, q2 = p1 + ps - 1, q1 + ps - 1
    while not x1 <= p2 and x2 >= p1:
        p1 += ps - 1
        p2 += ps - 1
    while not y1 <= q2 and y2 >= q1:
        q2 += ps - 1
        q1 += ps - 1
    tx, ty = p1 // 8, q1 // 8

    # repeat the procedure above for bottom right coordinate
    p1, q1 = min(8*boundary, x2 + ps - 1), min(8*boundary, y2 + ps - 1)
    p1, q1 = (p1 // 8) * 8, (q1 // 8) * 8
    p2, q2 = p1 + ps - 1, q1 + ps - 1
    while not p1 <= x2 and p2 >= x1:
        p1 -= ps + 1
        p2 -= ps + 1
    while not q1 >= y2 and q2 <= y1:
        q2 -= ps + 1
        q1 -= ps + 1

    bx, by = p1 // 8, q1 // 8
    return (tx, ty, bx, by)

def bound_patch_attack(patch_logits, affected_patch, bound=(-1, 1)):
    """Bound the effect of patch attack given affected patches
    Input:
    - patch_logits (pytorch tensor): patch logits returned by BagNet33
    - affected_patch (tuple): (x1, y1, x2, y2), where (x1, y1) is the top-left coordinate of top-left patch,
                                and (x2, y2) is the bottom-right coordinate of bottom-right patch.
    - bound (tuple): (lower bound, upper bound)
    Output:
    - class_logits_upper (pytorch tensor): upper bound of class logits, shape = (1000,)
    - class_logits_lower (pytorch tensor): lower bound of class logits, shape = (1000,)
    """
    x1, y1, x2, y2 = affected_patch
    upper, lower = patch_logits.clone(), patch_logits.clone()
    upper[x1:x2+1, y1:y2+1, :] = bound[1]
    lower[x1:x2+1, y1:y2+1, :] = bound[0]
    class_logits_upper = torch.mean(upper, dim=(0, 1))
    class_logits_lower = torch.mean(lower, dim=(0, 1))
    return (class_logits_lower, class_logits_upper)

def get_position_buffer(h, w, attack_size, stride, ps=33):
    """Buffer of all possible affected patch positions
    """
    buffer = set()
    last_affected_patches = (0, 0, 0, 0)
    for i in range(0, h - attack_size[0]+1, stride):
        for j in range(0, w - attack_size[1]+1, stride):
            affected_patches = get_affected_patches(attack_size, (i, j), ps=ps)
            buffer.add(affected_patches)
    return buffer

def pick_targeted_classes(logits, k=5, case='avg'):
    """
    Input:
    - logits (pytorch tensor): clipped logits (clipped patch logits after avg). Shape (N, 1000)
    - k (int): top-k prediction
    - case (string): ['best', 'avg', 'worst']
            'best': the class ranked at (k+1)-th
            'avg': randomly sample one class outside of the top-k predictions
            'worst': the class ranked the last
    """
    assert case in ['best', 'avg', 'worst'], 'case must be "best", "avg", or "worst"'
    if case == 'best':
        _, topk = torch.topk(logits, k=k+1, dim=1) # shape (N, k+1)
        return topk[:, -1]
    if case == 'avg':
        _, topk = torch.topk(logits, k=1000, dim=1) # shape (N, 1000)
        indices = torch.randint(low=5, high=999, size=(1,))
        return torch.index_select(topk, 1, indices.cuda()).reshape((-1,))
    else: # if case == 'worst'
        _, topk = torch.topk(logits, k=1000, dim=1) # shape (N, 1000)
        return topk[:, -1]

# pytorch data loader version
def get_security_lower_bound(bagnet, data_loader, attack_size, clip, stride=1, bound=(-1, 1), **kwargs):
    """ Take in a pytorch data loader and the size of adversarial sticker, loop over each of
            the possible position of the sticker to count how many images in the batch
            can still be correctly classified if the sticker is at that position.
    Input:
    - bagnet (pytorch model): BagNet-33 without avgerage pooling
    - data_loader (pythorch DataLoader): dataloader in CPU
    - attack_size (tuple): (h, w), height and width of the adversarial sticker
    - clip (function): clipping function
    - stride (int): stride of stickers
    - bound (tuple): tanh_linear: (-1, 1), sigmoid_linear: (-1, 1)
    - **kwargs: a, b, clipping parameters
    Output:
    - succ_prob (float): fraction of images whose prediction are invariant in the worst case
    """
    positions = get_position_buffer(224, 224, attack_size, stride)
    positions = sorted(positions)
    num_invariant, total_images = 0, 0
    for images, targets in data_loader:
        images, targets = images.cuda(), targets.numpy()
        with torch.no_grad():
            logits = bagnet(images)
        logits = clip_fn(logits, a, b).cpu()
        avg_logits = torch.mean(logits, dim=(1, 2))
        if targeted:
            targets = pick_targeted_classes(avg_logits, k=k, case=case)
            targets = targetes.numpy()
        else:
            targets = None
        n, c, h, w = images.shape
        total_images += n
        for k in range(n):
            img_patch_logits = logits[k] # shape (24, 24, 1000)
            label = labels[k]
            if targeted: target = targets[k]
            flag = True
            for p in positions:
                class_lower, class_upper = bound_patch_attack(img_patch_logits, p, bound=bound)
                if not is_invariant(class_lower, class_upper, label, target):
                    flag = False
                    break
            if flag:
                num_invariant += 1
    succ_prob = num_invariant / total_images
    return succ_prob

#########################################
# Security Upper Bound (Foolbox)
#########################################
def get_subimgs(images, loc, size):
    """Take an of IMAGES, location LOC and SIZE of stickers,
        return the sub-image that are for adversarial stickers
    Input:
    - image (numpy array): images, shape = (n, 3, 224, 224)
    - loc (tuple): (x1, y1) coordinate of the upper-left corner of stickers
    - size (tuple): (w, h) width and height of stickers
    Output:
    -subimage (numpy array): subimage shape = (n, 3, w, h)
    """
    x1, y1 = loc
    x2, y2 = x1 + size[0], y1 + size[1]
    return images[:, :, x1:x2, y1:y2]

def get_subimg(image, loc, size):
    """Take an of IMAGES, location LOC and SIZE of stickers,
        return the sub-image that are for adversarial stickers
    Input:
    - image (numpy array): images, shape = (3, 224, 224)
    - loc (tuple): (x1, y1) coordinate of the upper-left corner of stickers
    - size (tuple): (w, h) width and height of stickers
    Output:
    -subimage (numpy array): subimage shape = (3, w, h)
    """
    x1, y1 = loc
    x2, y2 = x1 + size[0], y1 + size[1]
    return image[:, x1:x2, y1:y2]

def foolbox_upper_bound(model, wrapper, data_loader, attack_size,
                        clip_fn, a=None, b=None, stride=1, k=5,
                        attack_alg=PGD, targeted=False, case='avg',
                        max_iter=40, eps=1., stepsize=1/40, return_early=True, random_start=True,
                        mean=np.array([0.485, 0.456, 0.406]).reshape((3, 1, 1)),
                        std=np.array([0.229, 0.224, 0.225]).reshape((3, 1, 1)),
                        output_root = './foolbox_results'):
    """ Take in a pytorch data loader, use the ATTACK_ALG to
        calculate the security upper bound.
    Input:
    - model (pytorch model): model wrapped for sticker attack
    - wrapper (pytorch model): model wrapper for sticker attack
    - data_loader (pytorch DataLoader): batchsize=1, pytorch dataloader in CPU, without normalization, pixel ~ [0, 1]
    - attack_size (iterable): sticker size
    - stride (int): stride of how stickers move
    - k (int): top-k misclassification criteria
    - clip_fn (callable): clipping function (None if don't apply)
    - attack_alg (foolbox.attack): attack algorithm implemented in foolbox
    - targeted (boolean): whether apply a targeted attack,
    - case (string): ['avg', 'worst', 'best']: sampling strategy for targeted attacks
    - max_iter (int): number of max iterations
    - eps (float): from 0 to 1, percentage of perturbation
    - stepsize (float): optimizer stepsize
    - return_early (boolean): apply early return in attack
    - random_start (boolean): apply random initialization in attack
    - mean, std (numpy array): mean and standard devication of dataset
    - output_path (str): directory for saving resulting plots
    Output:
    - succ_prob (float): fraction of images whose prediction are not affected by the attack
    """
    affected_imgs, total_images = 0, 0
    imagenet_mean = torch.Tensor([0.485, 0.456, 0.406]).view((3, 1, 1))
    imagenet_std = torch.Tensor([0.229, 0.224, 0.225]).view((3, 1, 1))
    for images, labels in data_loader:
        prep_images = (images - imagenet_mean) / imagenet_std
        _images = prep_images.cuda()
        if targeted: # if targeted, calculate logits for sampling targeted class
            with torch.no_grad():
                if clip_fn is None:
                    logits = model(_images)
                else:
                    logits = clip_fn(model(_images), a, b)
                    logits = torch.mean(logits, dim=(1, 2))

        image, label = images[0].numpy(), labels[0].item()
        c, h, w = image.shape
        total_images += 1
        flag = True
        targeted_class = pick_targeted_classes(logits, k=1, case=case)[0].item()
        for x in range(0, h - attack_size[0] + 1, stride):
            for y in range(0, w - attack_size[1] + 1, stride):
                wrapped_model = wrapper(model, prep_images, attack_size, (x, y), clip_fn, a, b)
                wrapped_model.eval()
                print('image {}, current location: {}'.format(total_images, (x, y)))
                fmodel = foolbox.models.PyTorchModel(wrapped_model, bounds=(0, 1), num_classes=1000, preprocessing=(mean, std))
                if not targeted:
                    criterion = TopKMisclassification(k)
                else:
                    #targeted_class = pick_targeted_classes(logits, k=1, case=case)[0].item()
                    criterion = TargetClass(targeted_class)
                attack = attack_alg(fmodel, criterion=criterion, distance=foolbox.distances.Linfinity)
                subimg = get_subimg(image, (x, y), attack_size)
                with warnings.catch_warnings():
                    warnings.simplefilter("ignore")
                    adversarial = attack(subimg, label, iterations = max_iter, epsilon=eps, stepsize=stepsize, random_start=random_start, return_early=return_early, binary_search=False)

                if adversarial is not None:
                    msg = 'Image {}, attack successfully, location {}'.format(total_images, (x, y))
                    print(msg)
                    logging.info(msg)
                    plt_name = '{}.png'.format(total_images)
                    save_path = os.path.join(output_root, str(label))
                    if not os.path.exists(save_path):
                        os.mkdir(save_path)
                    attacked_img = apply_sticker(adversarial, image, (x, y), attack_size).transpose([1, 2, 0])
                    plt.imsave(os.path.join(save_path, plt_name), attacked_img)
                    image = image.transpose([1, 2, 0])
                    if not targeted:
                        plt.imsave(os.path.join(save_path, '{}-{}.png'.format(label, total_images)), image)
                    else:
                        plt.imsave(os.path.join(save_path, '{}-{}-{}.png'.format(label, total_images, targeted_class)), image)

                    affected_imgs += 1
                    flag = False
                    break
                else:
                    print('Image {}: fail to find an adversarial sticker at {}'.format(total_images, (x, y)))
            if not flag:
                break
    num_invariant = total_images - affected_imgs
    succ_prob = num_invariant / total_images
    return succ_prob

def apply_sticker(adv, img, loc, size):
    """
    Input:
    - adv (np array): adversarial sticker, shape=(3, h, w), pixel ~ (0, 1)
    - img (np array): original image, shape=(3, 224, 224), pixel ~ (0, 1)
    - loc (list):
    - size (int):
    """
    x1, y1 = loc
    x2, y2 = x1 + size[0], y1 + size[1]
    sticker = np.zeros_like(img).astype(img.dtype)
    sticker[:, x1:x2, y1:y2] = adv
    mask = np.ones_like(img).astype(img.dtype)
    mask[:, x1:x2, y1:y2] = 0.
    img = img * mask
    return img + sticker

class PatchAttackWrapper(nn.Module):

    def __init__(self, model, img, size, loc, clip_fn=None, a=None, b=None):
        super(PatchAttackWrapper, self).__init__()
        self.batch_size = img.shape[0]
        self.model = model
        self.x1, self.y1 = loc
        self.x2, self.y2 = self.x1 + size[0], self.y1 + size[1]
        mask = torch.ones((1, 3, 224, 224))
        mask[:, :, self.x1:self.x2, self.y1:self.y2] = 0
        self.img = (mask*img).cuda()

    def forward(self, subimg):
        sticker = self._make_sticker(subimg)
        attacked_img = self.img + sticker
        logits = self.model(attacked_img)
        return logits

    def _make_sticker(self, subimg):
        sticker = torch.zeros((1, 3, 224, 224)).cuda()
        sticker[:, :, self.x1:self.x2, self.y1:self.y2] = subimg
        return sticker

class ClippedPatchAttackWrapper(PatchAttackWrapper):
    def __init__(self, model, img, size, loc, clip_fn, a=None, b=None):
        super().__init__(model, img, size, loc)
        self.clip_fn = clip_fn
        self.a = a
        self.b = b

    def forward(self, subimg):
        sticker = self._make_sticker(subimg)
        attacked_img = self.img + sticker
        logits = self.clip_fn(self.model(attacked_img), self.a, self.b)
        logits = torch.mean(logits, dim=(1, 2))
        return logits

######################################################
# Advertorch Metabatch Upper Bound
######################################################
class AdverTorchWrapper(nn.Module):
    def __init__(self, model, img, size, loc, clip, a, b):
        super(AdverTorchWrapper, self).__init__()
        self.batch_size = img.shape[0]
        self.model = model
        self.clip = clip
        self.a = a
        self.b = b
        self.x1, self.y1 = loc
        self.x2, self.y2 = self.x1 + size[0], self.y1 + size[1]
        self.img = img.contiguous().cuda(async=False)

    def forward(self, subimg):
        img = self.img.clone()
        img[:, :, self.x1:self.x2, self.y1:self.y2] = subimg
        clipped_patch_logits = self._clip_logits(img, a = self.a, b = self.b)
        clipped_class_logits = torch.mean(clipped_patch_logits, dim=(1, 2))
        return clipped_class_logits

    def _clip_logits(self, attacked_img, **kwargs):
        patch_logits = self.model(attacked_img)
        if self.clip:
            return self.clip(patch_logits, **kwargs)
        return patch_logits

class ScheduledParamAdverTorchWrapper(AdverTorchWrapper):
    def __init__(self, model, img, size, loc, nb_iter, clip=sigmoid_linear, a=1, b=-25, init_factor=0.1, max_factor=100.5):
        super().__init__(model, img, size, loc, clip, a, b)
        param_stepsize = (max_factor-init_factor) / nb_iter
        self.factor_list = np.arange(init_factor, max_factor, param_stepsize)
        self.curr_step = 0

    def forward(self, subimg):
        img = self.img.clone()
        img[:, :, self.x1:self.x2, self.y1:self.y2] = subimg
        curr_factor = self.factor_list[self.curr_step]
        clipped_patch_logits = self._clip_logits(img, a = curr_factor*self.a, b = curr_factor*self.b)
        self.curr_step += 1
        clipped_class_logits = torch.mean(clipped_patch_logits, dim=(1, 2))
        return clipped_class_logits

class UndefendedAdverTorchWrapper(nn.Module):
    def __init__(self, model, img, size, loc):
        super(UndefendedAdverTorchWrapper, self).__init__()
        self.batch_size = img.shape[0]
        self.model = model
        self.x1, self.y1 = loc
        self.x2, self.y2 = self.x1 + size[0], self.y1 + size[1]
        self.img = img.contiguous().cuda(async=False)

    def forward(self, subimg):
        img = self.img.clone()
        img[:, :, self.x1:self.x2, self.y1:self.y2] = subimg
        logits = self.model(img)
        return logits

class MetaBatch:
    def __init__(self, data_iter, size, max_iter):
        self.clean_acc = 1
        self.global_step = 0
        self.waitlist = data_iter # images waiting for attack, pytorch data iterator, batch size=1, in CPU
        self.exhaust = False
        self.size = size # size of the metabatch
        self.images = torch.zeros((size, 3, 224, 224)) # images under attack, in CPU
        self.record = np.zeros((self.size, 2), dtype=np.int64) # [(image ID 0, # duration 0), (image ID 1, # duration 1), ...]
        self.labels = torch.zeros((self.size, ), dtype=torch.int64)
        assert self.size <= len(data_iter), "Size of MetaBatch shouldn't larger than data iterator's size"
        self.orig_list = {} # {id: (image, label)}
        for idx in range(self.size):
            image, label = next(self.waitlist)
            self.orig_list[self.global_step] = (image[0].clone(), label.item())
            self.record[idx, 0] = self.global_step
            self.global_step += 1
            self.images[idx] = image[0].clone()
            self.labels[idx] = label.item()
        self.fail_list = {} # {id: (image, label, loc), ...}
        self.adv = {} # {id: adv_sticker, ...}
        self.targeted = {} # {id: targeted_class_index, ...}
        self.succ_list = [] # [id, ...]
        self.max_iter = max_iter
        self.location = None

    def update(self, indices, adv, targeted_labels=None):
        """ - Replace images that generate adversatial stickers successfully with new images from
                the wait list.
        Input:
        - indices (python list):
        - adv (pytorch tensor):
        - targeted_labels (python list)
        """
        # Replace failure cases with new images
        for idx in indices:
            if self.record[idx, 0] not in self.adv.keys(): # don't update images that are already failed.
                self.fail_list[self.record[idx, 0]] = (self.images[idx].clone(), self.labels[idx].item(), self.location) # (image, label, attack location)
                self.adv[self.record[idx, 0]] = adv[idx].clone()
                if targeted_labels is not None:
                    self.targeted[self.record[idx, 0]] = targeted_labels[idx]
                try:
                    image, label = next(self.waitlist)
                    self.orig_list[self.global_step] = (image[0].clone(), label.item())
                    self.images[idx] = image[0].clone()
                    self.record[idx, 0] = self.global_step # update image ID
                    self.record[idx, -1] = 0 # reset counter
                    self.labels[idx] = label.item()
                    self.global_step += 1
                except StopIteration:
                    self.exhaust = True
                    continue

        # Replace successful cases with new images
        for k in [i for i in range(self.size) if i not in indices]:
            if self.record[k, 0] not in self.adv.keys() and self.record[k, -1] < self.max_iter+1: self.record[k, -1] += 1
            if self.record[k, -1] == self.max_iter:
                self.succ_list.append(self.record[k, 0])
                try:
                    image, label = next(self.waitlist)
                    self.orig_list[self.global_step] = (image[0].clone(), label.item())
                    self.images[k] = image[0].clone()
                    self.labels[k] = label.item()
                    self.record[k, 0] = self.global_step # update image ID
                    self.record[k, -1] = 0 # reset counter
                    self.global_step += 1
                except StopIteration:
                    self.exhaust = True
                    continue
        print('metabatch record:\n {}'.format(self.record))
        logging.info('metabatch record:\n {}'.format(self.record))

    def get_succ_prob(self):
        return 1 - len(self.fail_list) / len(self.orig_list)

    def display_stickers(self, n=20):
        for i, (label, sticker) in enumerate(self.adv.items()):
            sticker = undo_imagenet_preprocess(sticker)
            plt.imshow(convert2channel_last(sticker.numpy()))
            plt.title('label {}'.format(label))
            plt.show()
            if i == n-1:
                break

    def display_failures(self, bagnet, idx2label, clip, a, b, n=20):
        label_list, topk_list = [], []
        for i, (key, value) in enumerate(self.fail_list.items()):
            image, label, loc = value
            x1, y1 = loc
            x2, y2 = x1+20, y1+20
            adv = self.adv[key].clone()
            image = image[None].clone()
            image[:, :, x1:x2, y1:y2] = adv[None]
            logits = bagnet(image.cuda())
            if clip:
                logits = clip(logits, a, b)
            logits = torch.mean(logits, dim=(1, 2))
            _, topk = torch.topk(logits, k=5, dim=1)
            image = undo_imagenet_preprocess(image[0]).cpu().numpy()
            image = convert2channel_last(image)
            plt.imshow(image)
            plt.title('{} {}'.format(label, idx2label[label]))
            plt.axis('off')
            plt.show()
            topk = topk[0].cpu().numpy()
            print('top-5 prediction: {}'.format(topk))
            topk_list.append(topk)
            label_list.append(label)
            if i == n-1:
                break
        topk_list = np.array(topk_list)
        label_list = np.array(label_list)
        return topk_list, label_list

    def display_successes(self, bagnet, idx2label, clip, a, b, n=20):
        label_list, topk_list = [], []
        for i, (key, value) in enumerate(self.orig_list.items()):
            if key not in self.adv.keys():
                image, label = value
                image = image[None].clone()
                with torch.no_grad():
                    logits = bagnet(image.cuda())
                if clip:
                    logits = clip(logits, a, b)
                logits = torch.mean(logits, dim=(1, 2))
                _, topk = torch.topk(logits, k=5, dim=1)
                image = undo_imagenet_preprocess(image[0])
                plt.imshow(convert2channel_last(image.cpu().numpy()))
                plt.title('{} {}'.format(label, idx2label[label]))
                plt.axis('off')
                plt.show()
                topk = topk[0].cpu().numpy()
                print('top-5 prediction: {}'.format(topk))
                label_list.append(label)
                topk_list.append(topk)
                if i == n-1:
                    break
        label_list = np.array(label_list)
        topk_list = np.array(topk_list)
        return topk_list, label_list

def undefended_batch_upper_bound(model, metabatch,
                      attack_size, stride, k=5,
                      attack_alg=LinfPGDAttack, eps=5., nb_iter=40, stepsize=0.5, rand_init=False):
    """
    Input:
    - model (pytorch model): bagnet model without avgerage pooling
    - images (pytorch tensor): a batch of preprocessed images, same context as model's
    - labels (pytorch tensor): image labels, same context as model's
    - clip (python function): clipping function
    - a, b (float): clipping parameters
    Output:
    - succ_prob (float):
    """
    earlystop = False
    while not earlystop:
        for x in range(0, 224 - attack_size[0] + 1, stride):
            for y in range(0, 224 - attack_size[1] + 1, stride):
                print('current location {}'.format((x, y)))
                logging.info('current location {}'.format((x, y)))
                metabatch.location = (x, y)
                adv_images = metabatch.images.clone()
                subimg = get_subimgs(adv_images, (x, y), attack_size)
                subimg = subimg.cuda()
                labels = metabatch.labels.cuda()
                adver_model = UndefendedAdverTorchWrapper(model, adv_images,
                                                attack_size, (x, y)).cuda()
                adver_model.eval()
                adversary = attack_alg(adver_model, loss_fn=nn.CrossEntropyLoss(reduction="sum"), eps=eps,
                              	       nb_iter=nb_iter, eps_iter=stepsize, rand_init=rand_init,
				       clip_min=-1.8044, clip_max=2.2489, # only for ImageNet data set.
                                       targeted=False)
                adv = adversary.perturb(subimg, labels).cpu()
                # apply stickers to image
                adv_images[:, :, adver_model.x1:adver_model.x2, adver_model.y1:adver_model.y2] = adv
                # evaluate attack
                with torch.no_grad():
                    logits = model(adv_images.cuda())
                    _, topk = torch.topk(logits, k=k, dim=1)
                    l, topk = labels.cpu().numpy(), topk.cpu().numpy()
                    mis_indices = [idx for idx in range(len(l)) if l[idx] not in topk[idx]]
                    print('misclassified indices: {}'.format(mis_indices))
                    logging.info('misclassified indices: {}'.format(mis_indices))
                    metabatch.update(mis_indices, adv)
                print("Defense (total: {}): \n succeed: {}, fail: {}".format(len(metabatch.waitlist), len(metabatch.succ_list), len(metabatch.fail_list)))
                if len(metabatch.succ_list) + len(metabatch.fail_list) == len(metabatch.waitlist): # Early stop if already determine success or failure of all the images
                    earlystop = True
                    break
            if earlystop:
                break
    return metabatch.get_succ_prob()

def batch_upper_bound(model, metabatch, clip, a, b,
                      attack_size, stride, k=5,
                      attack_alg=LinfPGDAttack, eps=5., nb_iter=40, stepsize=0.5, rand_init=False):
    """
    Input:
    - model (pytorch model): bagnet model without avgerage pooling
    - images (pytorch tensor): a batch of preprocessed images, same context as model's
    - labels (pytorch tensor): image labels, same context as model's
    - clip (python function): clipping function
    - a, b (float): clipping parameters
    Output:
    - succ_prob (float):
    """
    earlystop = False
    while not earlystop:
        for x in range(0, 224 - attack_size[0] + 1, stride):
            for y in range(0, 224 - attack_size[1] + 1, stride):
                print('current location {}'.format((x, y)))
                logging.info('current location {}'.format((x, y)))
                metabatch.location = (x, y)
                adv_images = metabatch.images.clone()
                subimg = get_subimgs(adv_images, (x, y), attack_size)
                subimg = subimg.cuda()
                labels = metabatch.labels.cuda()
                adver_model = AdverTorchWrapper(model, adv_images,
                                                attack_size, (x, y),
                                                clip, a, b).cuda()
                adver_model.eval()
                adversary = attack_alg(adver_model, loss_fn=nn.CrossEntropyLoss(reduction="sum"), eps=eps,
                              	       nb_iter=nb_iter, eps_iter=stepsize, rand_init=rand_init,
				       clip_min=-1.8044, clip_max=2.2489, # only for ImageNet data set.
                                       targeted=False)
                adv = adversary.perturb(subimg, labels).cpu()
                # apply stickers to image
                adv_images[:, :, adver_model.x1:adver_model.x2, adver_model.y1:adver_model.y2] = adv
                # evaluate attack
                with torch.no_grad():
                    logits = model(adv_images.cuda())
                    if clip:
                        logits = clip(logits, a, b)
                    logits = torch.mean(logits, dim=(1, 2))
                    _, topk = torch.topk(logits, k=k, dim=1)
                    l, topk = labels.cpu().numpy(), topk.cpu().numpy()
                    mis_indices = [idx for idx in range(len(l)) if l[idx] not in topk[idx]]
                    print('misclassified indices: {}'.format(mis_indices))
                    logging.info('misclassified indices: {}'.format(mis_indices))
                    metabatch.update(mis_indices, adv)
                print("Defense (total: {}): \n succeed: {}, fail: {}".format(len(metabatch.waitlist), len(metabatch.succ_list), len(metabatch.fail_list)))
                if len(metabatch.succ_list) + len(metabatch.fail_list) == len(metabatch.waitlist): # Early stop if already determine success or failure of all the images
                    earlystop = True
                    break
            if earlystop:
                break
    return metabatch.get_succ_prob()

def scheduled_upper_bound(model, metabatch,
                          attack_size, stride, k=5,
                          clip=binarize, a=25, b=None,
                          attack_alg=LinfPGDAttack, eps=5., nb_iter=40, stepsize=0.5, rand_init=False):
    """
    Input:
    - model (pytorch model): bagnet model without avgerage pooling
    - images (pytorch tensor): a batch of preprocessed images, same context as model's
    - labels (pytorch tensor): image labels, same context as model's
    - clip (python function): clipping function
    - a, b (float): clipping parameters
    Output:
    - succ_prob (float):
    """
    earlystop = False
    while not earlystop:
        for x in range(0, 224 - attack_size[0] + 1, stride):
            for y in range(0, 224 - attack_size[1] + 1, stride):
                print('current location {}'.format((x, y)))
                logging.info('current location {}'.format((x, y)))
                metabatch.location = (x, y)
                adv_images = metabatch.images.clone()
                subimg = get_subimgs(adv_images, (x, y), attack_size)
                subimg = subimg.cuda()
                labels = metabatch.labels.cuda()
                msg = "labels: {}".format(labels)
                print(msg)
                logging.info(msg)
                adver_model = ScheduledParamAdverTorchWrapper(model, adv_images, attack_size, (x, y), nb_iter=nb_iter).cuda()
                adver_model.eval()
                adversary = attack_alg(adver_model, loss_fn=nn.CrossEntropyLoss(reduction="sum"), eps=eps,
                              	       nb_iter=nb_iter, eps_iter=stepsize, rand_init=rand_init,
				       clip_min=-1.8044, clip_max=2.2489, # only for ImageNet data set.
                                       targeted=False)
                adv = adversary.perturb(subimg, labels).cpu()
                # apply stickers to image
                adv_images[:, :, adver_model.x1:adver_model.x2, adver_model.y1:adver_model.y2] = adv
                # evaluate attack
                with torch.no_grad():
                    logits = model(adv_images.cuda())
                    if clip:
                        logits = clip(logits, a, b)
                    logits = torch.mean(logits, dim=(1, 2))
                    _, topk = torch.topk(logits, k=k, dim=1)
                    l, topk = labels.cpu().numpy(), topk.cpu().numpy()
                    print("topk prediction with stickers: \n {}".format(topk))
                    mis_indices = [idx for idx in range(len(l)) if l[idx] not in topk[idx]]
                    print('misclassified indices: {}'.format(mis_indices))
                    logging.info('misclassified indices: {}'.format(mis_indices))
                    metabatch.update(mis_indices, adv)
                if len(metabatch.succ_list) + len(metabatch.fail_list) == len(metabatch.waitlist): # Early stop if already determine success or failure of all the images
                    earlystop = True
                    break
            if earlystop:
                break
    return metabatch.get_succ_prob()

def get_targeted_classes(model, images, clip, a, b, k=5, case='avg'):
    """
    Input:
    - model (pytorch model): bagnet model without avgerage pooling
    - images (pytorch tensor): a batch of preprocessed images, same context as model's images
    - k (int): get targeted classes based on top-K prediction
    - case (string): ['best', 'avg', 'worst']
        'best': the class ranked at (k+1)-th
        'avg': randomly sample one class outside of the top-k predictions
        'worst': the class ranked the last
    Output:
    - targeted class (pytorch tensor): targeted class for targeted attack, same context as model's.
    """
    assert case in ['best', 'avg', 'worst'], 'case must be "best", "avg", or "worst"'
    logits = model(images)
    if clip:
        logits = clip(logits, a, b)
    logits = torch.mean(logits, dim=(1, 2)) # shape (N, 1000)
    if case == 'best':
        _, topk = torch.topk(logits, k=k+1, dim=1) # shape (N, k+1)
        return topk[:, -1]
    if case == 'avg':
        _, topk = torch.topk(logits, k=1000, dim=1) # shape (N, 1000)
        indices = torch.randint(low=5, high=999, size=(1,))
        return torch.index_select(topk, 1, indices.cuda()).reshape((-1,))
    else: # if case == 'worst'
        _, topk = torch.topk(logits, k=1000, dim=1) # shape (N, 1000)
        return topk[:, -1]

def targeted_batch_upper_bound(model, metabatch, clip, a, b,
                               attack_size, stride, k=5, targeted=True, case='avg',
                               attack_alg=LinfPGDAttack, eps=5., nb_iter=40, stepsize=0.5, rand_init=True):
    """
    Input:
    - model (pytorch model): bagnet model without avgerage pooling
    - images (pytorch tensor): a batch of preprocessed images, same context as model's
    - labels (pytorch tensor): image labels, same context as model's
    - clip (python function): clipping function
    - a, b (float): clipping parameters
    Output:
    - succ_prob (float):
    """
    earlystop = False
    while not earlystop:
        for x in range(0, 224 - attack_size[0] + 1, stride):
            for y in range(0, 224 - attack_size[1] + 1, stride):
                print('current location {}'.format((x, y)))
                logging.info('current location {}'.format((x, y)))
                metabatch.location = (x, y)
                adv_images = metabatch.images.clone()
                orig_labels = metabatch.labels
                if targeted:
                    with torch.no_grad():
                        labels = get_targeted_classes(model, adv_images.cuda(), clip, a, b, k=k, case=case)
                        msg1, msg2 = "targeted labels: {}".format(labels), "original labels: {}".format(metabatch.labels)
                        print(msg1)
                        print(msg2)
                        logging.info(msg1)
                        logging.info(msg2)
                else:
                    labels = metabatch.labels.cuda()
                subimg = get_subimgs(adv_images, (x, y), attack_size)
                subimg = subimg.cuda()
                adver_model = AdverTorchWrapper(model, adv_images,
                                                attack_size, (x, y),
                                                clip, a, b).cuda()
                adver_model.eval()
                adversary = attack_alg(adver_model, loss_fn=nn.CrossEntropyLoss(reduction="sum"), eps=eps,
                              	       nb_iter=nb_iter, eps_iter=stepsize, rand_init=rand_init,
				       clip_min=-1.8044, clip_max=2.2489, # only for ImageNet data set.
                                       targeted=True)
                adv = adversary.perturb(subimg, labels).cpu()
                # apply stickers to image
                adv_images[:, :, adver_model.x1:adver_model.x2, adver_model.y1:adver_model.y2] = adv
                # evaluate attack
                with torch.no_grad():
                    logits = model(adv_images.cuda())
                    if clip:
                        logits = clip(logits, a, b)
                    logits = torch.mean(logits, dim=(1, 2))
                    _, topk = torch.topk(logits, k=k, dim=1)
                    l, topk = orig_labels.numpy(), topk.cpu().numpy()
                    print("topk prediction with stickers: \n {}".format(topk))
                    if targeted:
                        tl = labels.cpu().numpy()
                        mis_indices = [idx for idx in range(len(l)) if l[idx] not in topk[idx] and tl[idx] in topk[idx]]
                    else:
                        mis_indices = [idx for idx in range(len(l)) if l[idx] not in topk[idx]]
                    print('misclassified indices: {}'.format(mis_indices))
                    logging.info('misclassified indices: {}'.format(mis_indices))
                    metabatch.update(mis_indices, adv, targeted_labels=labels.cpu().numpy())
                if len(metabatch.succ_list) + len(metabatch.fail_list) == len(metabatch.waitlist): # Early stop if already determine success or failure of all the images
                    earlystop = True
                    break
            if earlystop:
                break
    return metabatch.get_succ_prob()

max_stars_repo_name: davidwagner/bagnet-patch-defense

max_issues_repo_name: davidwagner/bagnet-patch-defense

max_forks_repo_name: davidwagner/bagnet-patch-defense


text:
from . import utils
from . import colors
import cv2
from collections import namedtuple
import math
import numpy as np

def tree_mask(img, picker):
    """Returns a threshold mask of the tree"""
    mask = colors.apply_color_mask(img, 'green', picker)
    mask = utils.blur(utils.to_gray(mask), (11, 11))
    _, t_img = cv2.threshold(mask, 1, 200, cv2.THRESH_BINARY)
    return t_img

def extract_tree(img, contour):
    """Returns the tree masked out from the rest of the image"""
    mask = np.zeros_like(img)
    white = (255, 255, 255)
    mask = cv2.drawContours(mask, [contour], -1, white, -1)
    tree = np.zeros_like(img)
    tree[mask == white] = img[mask == white]
    return tree, mask

def tree_contours(mask):
    """Returns the contours of the tree"""
    _, cnts, _ = cv2.findContours(mask, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
    return sorted(cnts, key=cv2.contourArea, reverse=True)[0]

def contour_area_ratio(img_shape, contour):
    """Return the area of the contour"""
    return cv2.contourArea(contour) / (img_shape[0] * img_shape[1])

def height_width_ratio(corners):
    """Determines the ration between the height and width of the tree based on
    the corners of the tree"""
    width = corners.bottom_right[0] - corners.bottom_left[0]
    height = corners.bottom_mid[1] - corners.top[1]
    return height / width

def angle(corners):
    """Returns the angular skew of the tree, 0 is perfectly vertical"""
    left_x, left_y = corners.bottom_left
    right_x, right_y = corners.bottom_right
    top_x, top_y = corners.top

    theo_x = (right_x - left_x) / 2 + left_x
    offset = abs(theo_x - top_x)
    height = round(abs(left_y + right_x) / 2) - top_y
    return math.degrees(math.atan(offset / height))

def corners(img, contour):
    """Returns the three corners of the tree based on the points of the
    countour list passed in"""
    height, width, _ = img.shape
    bottom_right_dist = dist_calc(width, height)
    bottom_left_dist = dist_calc(0, height)

    # distancs to bottom_let, bottom_right, top
    dr, dl, dt = 9999, 9999, 9999

    # points for best guess for bottom_right, bottom_left, top corners
    br, bl, tp_lst = (None, None), (None, None), []

    for point in utils.points(contour):
        dst = bottom_right_dist(*point)
        if dst < dr:
            dr = dst
            br = point
        dst = bottom_left_dist(*point)
        if dst < dl:
            dl = dst
            bl = point
        if point[1] < dt:
            tp_lst = [point]
        elif point[1] == dt:
            tp_lst.append(point)

    # If there are multiple top points, then we use the average value for the
    # x and y coordinate
    top_x = int(round(sum([x for (x, y) in tp_lst]) / len(tp_lst)))
    top_y = int(round(sum([y for (x, y) in tp_lst]) / len(tp_lst)))

    # Calculate the bottom middle point
    bottom_mid_x = int(round(br[0] - bl[0]) / 2) + bl[0]
    bottom_mid_y = int(round(abs(br[1] - bl[1]) / 2)) + min(br[1], bl[1])

    Corners = namedtuple('Corners', 'bottom_left, bottom_right, bottom_mid, top')
    return Corners(bl, br, (bottom_mid_x, bottom_mid_y), (top_x, top_y))

def dist_calc(x, y):
    """Returns a distance calcultion fuction from a static (x, y)"""
    def func(x1, y1):
        return math.sqrt(math.pow(x1-x, 2) + math.pow(y1-y, 2))
    return func

def score(corners):
    ideal_ratio = 4
    ratio = height_width_ratio(corners)
    ang_degrees = angle(corners)
    raw_score = 100 - abs(ideal_ratio - ratio) * abs(ang_degrees)
    return max(int(round(raw_score)), 0)

ext: py

max_stars_repo_name: christianreimer/treescore

max_issues_repo_name: christianreimer/treescore

max_forks_repo_name: christianreimer/treescore

reason: import numpy


text:
import vtk
import numpy as np
from vtk.util.colors import tomato

cam_height = 28
scaling_cam = 1.2

def camera_structure(type):
    if type == "LSTCam":

        coords_pix = np.loadtxt("LSTCam_coords.txt")
        x_px = coords_pix[:, 0]*scaling_cam
        y_px = coords_pix[:, 1]*scaling_cam

        points = vtk.vtkPoints()
        colors = vtk.vtkUnsignedCharArray()
        colors.SetNumberOfComponents(3)

        for i in range(len(x_px)):
            points.InsertNextPoint(x_px[i], y_px[i], 0)
            # rgb = [0.0, 0.0, 0.0]
            # lut.GetColor(float(i) / (tableSize - 1), rgb)
            rgb = [0.0, 0.0, 1.0]
            ucrgb = list(map(int, [x * 255 for x in rgb]))
            colors.InsertNextTuple3(ucrgb[0], ucrgb[1], ucrgb[2])

        polydata = vtk.vtkPolyData()
        polydata.SetPoints(points)

        colors.SetNumberOfTuples(polydata.GetNumberOfPoints())
        polydata.GetPointData().SetScalars(colors)

        # # Now, let's control normal on each point composing or 'fake' surface
        # # We should say, let's give a direction to each point, a normal in strange for a point.
        # pointNormalsArray = vtkDoubleArray()
        # pointNormalsArray.SetNumberOfComponents(3)
        # pointNormalsArray.SetNumberOfTuples(polydata.GetNumberOfPoints())
        #
        # pN1 = [0.0, 1.0, 1.0]
        # pN2 = [0.0, 1.0, 1.0]
        # pN3 = [0.0, 1.0, 1.0]
        #
        # pointNormalsArray.SetTuple(0, pN1)
        # pointNormalsArray.SetTuple(1, pN2)
        # pointNormalsArray.SetTuple(2, pN3)
        #
        # polydata.GetPointData().SetNormals(pointNormalsArray)

        pixel_source = vtk.vtkCylinderSource()

        pixel_source.SetResolution(6)
        pixel_source.SetRadius(0.026*scaling_cam)
        pixel_source.SetHeight(0.8)
        pixel_source.Update()

        transform = vtk.vtkTransform()
        transform.RotateX(90)
        transform.RotateY(100.8+30)

        transformFilter = vtk.vtkTransformPolyDataFilter()
        transformFilter.SetTransform(transform)
        transformFilter.SetInputConnection(pixel_source.GetOutputPort())
        transformFilter.Update()

        glyph = vtk.vtkGlyph3D()

        glyph.SetInputData(polydata)
        glyph.SetSourceConnection(transformFilter.GetOutputPort())
        glyph.SetColorModeToColorByScalar()
        glyph.SetVectorModeToUseNormal()
        glyph.ScalingOff()
        glyph.Update()

        mapper = vtk.vtkPolyDataMapper()
        mapper.SetInputConnection(glyph.GetOutputPort())

        actor = vtk.vtkActor()
        actor.SetMapper(mapper)
        actor.RotateY(90)
        actor.SetPosition(28, 0, 0)

        return actor

def camera_frame(type):
    if type == "LSTCam":
        camera_frame_structure = vtk.vtkCubeSource()
        camera_frame_structure.SetXLength(3)
        camera_frame_structure.SetYLength(3)
        camera_frame_structure.SetZLength(0.75)

        # create a transform that rotates the cone
        transform = vtk.vtkTransform()
        transform_camera_frame_filter = vtk.vtkTransformPolyDataFilter()
        transform.RotateY(90)
        transform_camera_frame_filter.SetTransform(transform)
        transform_camera_frame_filter.SetInputConnection(camera_frame_structure.GetOutputPort())
        transform_camera_frame_filter.Update()

        camera_frame_mapper = vtk.vtkPolyDataMapper()
        camera_frame_mapper.SetInputConnection(transform_camera_frame_filter.GetOutputPort())

        camera_frame_actor = vtk.vtkActor()
        camera_frame_actor.SetMapper(camera_frame_mapper)
        camera_frame_actor.GetProperty().SetColor(tomato)
        camera_frame_actor.SetPosition(28, 0, 0)

        return camera_frame_actor


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

'''
REFERÊNCIAS:
	<http://acodigo.blogspot.com.br/2017/09/deteccion-de-lineas-y-circulos-usando.html>

@Author RodriguesFAS
@Date 26/09/2017
@Email <fasr@cin.ufpe.br> || <franciscosouzaacer@gmail.com>
@site <htpp://rodriguesfas.com.br>

'''

# Python 2/3 compatibility

import cv2
import numpy as np

img = cv2.imread('../src/sudoku.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray, 50, 150, apertureSize = 3)

lines = cv2.HoughLines(edges, 1, np.pi/180, 200)

for line in lines:
    rho, theta = line[0]
    a = np.cos(theta)
    b = np.sin(theta)
    x0 = a*rho
    y0 = b*rho
    x1 = int(x0 + 1000*(-b))
    y1 = int(y0 + 1000*(a))
    x2 = int(x0 - 1000*(-b))
    y2 = int(y0 - 1000*(a))

    cv2.line(img, (x1,y1), (x2,y2), (0, 0, 255), 1, cv2.LINE_AA)

cv2.imshow('Bordas da Imagem Canny', edges)
cv2.imshow('Detector de Linhas', img)
cv2.waitKey()

max_stars_repo_name: rodriguesfas/Vision-Computer

max_issues_repo_name: rodriguesfas/Vision-Computer

max_forks_repo_name: rodriguesfas/Vision-Computer


text:
import math
import yfinance as yf
from time import perf_counter
import json
from json import JSONEncoder
import redis
from datetime import datetime
import numpy as np

import numpy as np

from flask.helpers import make_response
from neo4j import GraphDatabase
from neo4j.time import DateTime
from neo4j.graph import Path, Node, Relationship
from flask import Flask, request, jsonify
from flask_cors import CORS

r = redis.Redis(host='7c00h.xyz', port=6379, password='tanzixuan', db=2)
driver = GraphDatabase.driver("neo4j://7c00h.xyz:7687", auth=("neo4j", "root"))

app = Flask(__name__)
CORS(app)

def ScoreforO(ticker: str) -> int:
    msft = yf.Ticker(ticker)
    try:
        df = msft.get_financials(proxy='http://7c00h.xyz:7890')
        if df.empty:
            print("lws")
            n=np.random.random(1)*3
            n = (math.exp(n)/(math.exp(n)+1)-0.5)*20
            return n
        items = ["Income Before Tax", ""]
        weight = [0.55, 0.15, 0.05, 0.05, 0.1, 0.1]
        fin = []
        i = df.loc["Income Before Tax"]
        fin.append((i.sum()-i.min()*4)/(i.max()-i.min())*5)
        #i=df.loc["Total Revenue"]/df.loc["Total Revenue"]
        return fin[0]
    except:
        n=np.random.random(1)*3
        n = (math.exp(n)/(math.exp(n)+1)-0.5)*20
        return n

@app.route('/api/oscore')
def api_oscore():
    ticker = request.args.get('ticker')
    return {'score': ScoreforO(ticker)}

def ScoreforI(tickers: list, edu: int) -> int:
    score = 0
    for i in tickers:
        score += ScoreforO(i)
    edu = edu/1.6
    score=score/6
    score = (math.exp(score)/(math.exp(score)+1)-0.5)*8
    lscore = (math.exp(edu)/(math.exp(edu)+1)-0.5)*2
    return {'score': score+lscore}

@app.route('/api/pscore')
def api_pscore():
    tickers = request.args.getlist('tickers[]')
    edu = int(request.args.get('edu'))
    return ScoreforI(tickers, edu)

def query(tx, cypher, **param):
    res = []
    for result in tx.run(cypher, **param):
        res.append(result.values())
    return res

def neo4j_query(cypher, **param):
    with driver.session() as session:
        return session.read_transaction(query, cypher, **param)

class CustomJSONEncoder(JSONEncoder):
    def default(self, o):
        if isinstance(o, DateTime):
            return o.iso_format()
        return super(CustomJSONEncoder, self).default(o)

class ProcessError(RuntimeError):
    pass

def preprocess(x):
    if isinstance(x, Node):
        labels = list(x.labels)
        if 'Resource' in labels:
            labels.remove('Resource')
        return {"id": x.id, 'labels': labels, 'properties': x._properties}
    elif isinstance(x, Relationship):
        return {'id': x.id,
                'type': x.type,
                'start_node': preprocess(x.start_node),
                'end_node': preprocess(x.end_node),
                'nodes': [preprocess(n) for n in x.nodes]}
    elif isinstance(x, Path):
        return {'start_node': preprocess(x.start_node),
                'end_node': preprocess(x.end_node),
                'nodes': [preprocess(n) for n in x.nodes],
                'relationships': [preprocess(r) for r in x.relationships]}
    else:
        raise ProcessError()

def timed_query(cypher, **param):
    if r.exists(cypher):
        print('cached!')
        data = r.get(cypher)
        resp = make_response(data)
        resp.headers['content-type'] = 'application/json; charset=utf-8'
        return resp

    start = perf_counter()
    res = neo4j_query(cypher, **param)
    time = 1000 * (perf_counter() - start)

    res = [preprocess(row[0]) for row in res]
    data = json.dumps(
        {"count": len(res), 'time': time, "data": res},
        ensure_ascii=False,
        separators=(',', ':'),
        cls=CustomJSONEncoder
    )
    r.setex(cypher, 10 * 60, data)  # cache for 10 mins
    resp = make_response(data)
    resp.headers['content-type'] = 'application/json; charset=utf-8'
    return resp

def extract_array(x):
    if isinstance(x, list):
        return x[0]
    return x

@app.route('/api/query', methods=['GET'])
def api_query():
    cypher = request.args.get("cypher")
    return timed_query(cypher)

if __name__ == "__main__":
    app.run(port=8080, debug=True)

max_stars_repo_name: FrezCirno/aibi

max_issues_repo_name: FrezCirno/aibi

max_forks_repo_name: FrezCirno/aibi


text:
import argparse
import glob
import logging
import os
import sys
import time

import numpy as np
import torch
import torch.backends.cudnn as cudnn
import torch.nn as nn
import torch.utils
from torch.autograd import Variable

import utils
from model import NetworkBathy as Network

parser = argparse.ArgumentParser("cifar")
parser.add_argument('--data', type=str, default='dataset', help='location of the data corpus')
parser.add_argument('--batch_size', type=int, default=64, help='batch size')
parser.add_argument('--learning_rate', type=float, default=0.025, help='init learning rate')
parser.add_argument('--momentum', type=float, default=0.9, help='momentum')
parser.add_argument('--weight_decay', type=float, default=3e-4, help='weight decay')
parser.add_argument('--report_freq', type=float, default=50, help='report frequency')
parser.add_argument('--gpu', type=int, default=0, help='gpu device id')
parser.add_argument('--epochs', type=int, default=100, help='num of training epochs')
parser.add_argument('--init_channels', type=int, default=36, help='num of init channels')
parser.add_argument('--layers', type=int, default=8, help='total number of layers')
parser.add_argument('--genotype_path', type=str, default='', help='path of search trial')
parser.add_argument('--model_path', type=str, default='saved_models', help='path to save the model')
parser.add_argument('--auxiliary', action='store_true', default=False, help='use auxiliary tower')
parser.add_argument('--auxiliary_weight', type=float, default=0.4, help='weight for auxiliary loss')
parser.add_argument('--cutout', action='store_true', default=False, help='use cutout')
parser.add_argument('--cutout_length', type=int, default=16, help='cutout length')
parser.add_argument('--drop_path_prob', type=float, default=0.0, help='drop path probability')
parser.add_argument('--save', type=str, default='EXP', help='experiment name')
parser.add_argument('--seed', type=int, default=0, help='random seed')
parser.add_argument('--arch', type=str, default='BATH', help='which architecture to use')
parser.add_argument('--grad_clip', type=float, default=5, help='gradient clipping')
parser.add_argument('--depth_normalization', type=float, default=0.1, help='depth normalization factor')
parser.add_argument('--min_energy', type=float, default=0.1, help='minimum energy')
parser.add_argument('--max_energy', type=float, default=4.0, help='maximum energy')
parser.add_argument('--max_depth', type=float, default=40.0, help='maximum unnormalized depth')

args = parser.parse_args()

if args.genotype_path is not None:
    args.save = os.path.join(utils.get_dir(), args.genotype_path,
                             'batheval-{}-{}'.format(os.getenv('SLURM_JOB_ID'), time.strftime("%Y%m%d-%H%M%S")))
else:
    args.save = os.path.join(utils.get_dir(),
                             'exp/batheval-{}-{}'.format(os.getenv('SLURM_JOB_ID'), time.strftime("%Y%m%d-%H%M%S")))
utils.create_exp_dir(args.save, scripts_to_save=glob.glob('src/*.py'))

log_format = '%(asctime)s %(message)s'
logging.basicConfig(stream=sys.stdout, level=logging.INFO,
                    format=log_format, datefmt='%m/%d %I:%M:%S %p')
fh = logging.FileHandler(os.path.join(args.save, 'log_eval.txt'))
fh.setFormatter(logging.Formatter(log_format))
logging.getLogger().addHandler(fh)

def main():
    if not torch.cuda.is_available():
        logging.info('no gpu device available')
        sys.exit(1)

    np.random.seed(args.seed)
    torch.cuda.set_device(args.gpu)
    cudnn.benchmark = True
    torch.manual_seed(args.seed)
    cudnn.enabled = True
    torch.cuda.manual_seed(args.seed)
    logging.info('gpu device = %d' % args.gpu)
    logging.info("args = %s", args)

    genotype_path = os.path.join(utils.get_dir(), args.genotype_path, 'genotype.txt')
    if os.path.isfile(genotype_path):
        with open(genotype_path, "r") as f:
            geno_raw = f.read()
            genotype = eval(geno_raw)
    else:
        genotype = eval("genotypes.%s" % args.arch)

    f = open(os.path.join(args.save, 'genotype.txt'), "w")
    f.write(str(genotype))
    f.close()

    model = Network(args.init_channels, 1, args.layers, args.auxiliary, genotype, input_channels=4)
    model = model.cuda()

    logging.info("param size = %fMB", utils.count_parameters_in_MB(model))

    criterion = nn.MSELoss()
    criterion = criterion.cuda()
    optimizer = torch.optim.SGD(
        model.parameters(),
        args.learning_rate,
        momentum=args.momentum,
        weight_decay=args.weight_decay
    )

    # train_transform, valid_transform = utils._data_transforms_cifar10(args)
    # datapath = os.path.join(utils.get_dir(), args.data)
    # train_data = dset.CIFAR10(root=datapath, train=True, download=True, transform=train_transform)
    # valid_data = dset.CIFAR10(root=datapath, train=False, download=True, transform=valid_transform)
    train_data = utils.BathymetryDataset(args, "../mixed_train.csv", to_filter=False)
    valid_data = utils.BathymetryDataset(args, "../mixed_validation.csv", to_filter=False)

    train_queue = torch.utils.data.DataLoader(
        train_data, batch_size=args.batch_size, shuffle=True, pin_memory=True, num_workers=2)

    valid_queue = torch.utils.data.DataLoader(
        valid_data, batch_size=args.batch_size, shuffle=False, pin_memory=True, num_workers=2)

    scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(optimizer, args.epochs)

    loggers = {"train": {"loss": [], "step": []}, "val": {"loss": [], "step": []}}

    for epoch in range(args.epochs):
        scheduler.step()
        logging.info('epoch %d lr %e', epoch, scheduler.get_last_lr()[0])
        model.drop_path_prob = args.drop_path_prob * epoch / args.epochs

        _ = train(train_queue, model, criterion, optimizer, loggers["train"])

        infer_loss = infer(valid_queue, model, criterion)
        utils.log_loss(loggers["val"], infer_loss, None, 1)

        utils.plot_loss_acc(loggers, args.save)

        utils.save(model, os.path.join(args.save, 'weights.pt'))
        if (epoch + 1) % 50 == 0:
            utils.save(model, os.path.join(args.save, 'checkpoint' + str(epoch) + 'weights.pt'))

def train(train_queue, model, criterion, optimizer, train_logger):
    objs = utils.AverageMeter()
    model.train()

    batches = len(train_queue)
    for step, (input, target) in enumerate(train_queue):
        input = Variable(input.float()).cuda()
        target = Variable(target.float()).cuda(non_blocking=True)

        optimizer.zero_grad()
        logits, logits_aux = model(input)
        loss = criterion(torch.squeeze(logits), target)
        if args.auxiliary:
            loss_aux = criterion(logits_aux, target)
            loss += args.auxiliary_weight * loss_aux
        loss.backward()
        nn.utils.clip_grad_norm_(model.parameters(), args.grad_clip)
        optimizer.step()

        n = input.size(0)
        objs.update(loss.item(), n)
        utils.log_loss(train_logger, loss.item(), None, 1 / batches)

        if step % args.report_freq == 0:
            logging.info('train %03d %e', step, objs.avg)

    return objs.avg

def infer(valid_queue, model, criterion):
    objs = utils.AverageMeter()
    model.eval()

    with torch.no_grad():
        for step, (input, target) in enumerate(valid_queue):
            input = Variable(input.float()).cuda()
            target = Variable(target.float()).cuda(non_blocking=True)

            logits, _ = model(input)
            loss = criterion(torch.squeeze(logits), target)

            n = input.size(0)
            objs.update(loss.item(), n)

            if step % args.report_freq == 0:
                logging.info('valid %03d %e', step, objs.avg)

    return objs.avg

if __name__ == '__main__':
    main()


text:
import torch
import torch.multiprocessing
from torch.utils.data import DataLoader
from torch import optim
from torch.optim.lr_scheduler import ReduceLROnPlateau
from copy import deepcopy

from sklearn.model_selection import train_test_split
from pathlib import Path
import pandas as pd
import numpy as np
import torch.nn as nn
import sys, os, shutil, pickle, argparse, pdb
import torch.nn.functional as F

sys.path.append(os.path.join(str(Path(os.path.realpath(__file__)).parents[1]), 'model'))
sys.path.append(os.path.join(str(Path(os.path.realpath(__file__)).parents[1]), 'utils'))

from training_tools import EarlyStopping, seed_worker, result_summary
from attack_model_forDefense import attack_model

from tqdm import tqdm

from art.attacks.evasion import ProjectedGradientDescent
from art.estimators.classification import PyTorchClassifier
from art.attacks.evasion.projected_gradient_descent.projected_gradient_descent import ProjectedGradientDescent

# some general mapping for this script
gender_dict = {'F': 0, 'M': 1}
leak_layer_dict = {'full': ['w0', 'b0', 'w1', 'b1', 'w2', 'b2'],
                   'first': ['w0', 'b0'], 'second': ['w1', 'b1'], 'last': ['w2', 'b2']}
leak_layer_idx_dict = {'w0': 0, 'w1': 2, 'w2': 4, 'b0': 1, 'b1': 3, 'b2': 5}

class WeightDataGenerator():
    def __init__(self, dict_keys, data_dict = None):
        self.dict_keys = dict_keys
        self.data_dict = data_dict

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

    def __getitem__(self, idx):
        data_file_str = self.dict_keys[idx]
        gender = gender_dict[self.data_dict[data_file_str]['gender']]
        if args.normalize_disable:
            weights = torch.from_numpy(np.ascontiguousarray(self.data_dict[data_file_str][weight_name]))
            bias = torch.from_numpy(np.ascontiguousarray(self.data_dict[data_file_str][bias_name]))
        else:
            tmp_data = (self.data_dict[data_file_str][weight_name] - weight_norm_mean_dict[weight_name]) / (weight_norm_std_dict[weight_name] + 0.00001)
            weights = torch.from_numpy(np.ascontiguousarray(tmp_data))
            tmp_data = (self.data_dict[data_file_str][bias_name] - weight_norm_mean_dict[bias_name]) / (weight_norm_std_dict[bias_name] + 0.00001)
            bias = torch.from_numpy(np.ascontiguousarray(tmp_data))
        return weights, bias, gender

def evaluate(model, data_loader, loss_func, privacy_preserve=False, privacy_generator=None, prob_0=0.5):

    model.eval()
    step_outputs = []
    for batch_idx, data_batch in enumerate(tqdm(data_loader)):
        weights, bias, y = data_batch
        weights, bias, y = weights.to(device), bias.to(device), y.to(device)
        if not privacy_preserve:
            weights_bias = torch.cat((weights,bias.unsqueeze(dim=2)),2)
            logits = model(weights_bias)
        else:
            weights = weights.to('cpu').float()
            bias = bias.to('cpu').float()
            weights_bias = torch.cat((weights,bias.unsqueeze(dim=2)),2)
            weights_bias = np.array(weights_bias)
            if privacy_generator.targeted:
                tar_labels = np.array(F.one_hot(torch.tensor(np.random.choice([0,1],size=weights.shape[0], p=[0.0,1.0])), num_classes=2))
                weights_bias = torch.tensor(privacy_generator.generate(weights_bias, tar_labels))
            else:
                weights_bias = torch.tensor(privacy_generator.generate(weights_bias))
            weights = weights_bias[:,:,:-1].to(device)
            bias = weights_bias[:,:,-1].to(device)
            weights_bias = torch.cat((weights,bias.unsqueeze(dim=2)),2)
            logits = model(weights_bias)

        loss = loss_func(logits, y)

        predictions = np.argmax(logits.detach().cpu().numpy(), axis=1)
        pred_list = [predictions[pred_idx] for pred_idx in range(len(predictions))]
        truth_list = [y.detach().cpu().numpy()[pred_idx] for pred_idx in range(len(predictions))]
        step_outputs.append({'loss': loss.item(), 'pred': pred_list, 'truth': truth_list})

        torch.cuda.empty_cache()
    result_dict = result_summary(step_outputs, mode='test', epoch=0)  # epoch=0 just as a dummy value. Not used anywhere

    return result_dict

if __name__ == '__main__':

    # argument parser
    parser = argparse.ArgumentParser(add_help=False)
    parser.add_argument('--dataset', default='iemocap')
    parser.add_argument('--adv_dataset', default='iemocap')
    parser.add_argument('--feature_type', default='apc')
    parser.add_argument('--learning_rate', default=0.001)
    parser.add_argument('--batch_size', default=10)
    parser.add_argument('--use_gpu', default=True)
    parser.add_argument('--num_epochs', default=200)
    parser.add_argument('--local_epochs', default=5)
    parser.add_argument('--device', default='0')
    parser.add_argument('--model_type', default='fed_sgd')
    parser.add_argument('--pred', default='emotion')
    parser.add_argument('--leak_layer', default='full')
    parser.add_argument('--dropout', default=0.2)
    parser.add_argument('--privacy_budget', default=None)
    parser.add_argument('--privacy_preserve_adversarial', default=False, action='store_true')
    parser.add_argument('--save_dir', default='/media/data/projects/speech-privacy')

    parser.add_argument('--eval_undefended', default=False, action='store_true')
    parser.add_argument('--perturb_norm', default='l_2')
    parser.add_argument('--eps', default=0.3)
    parser.add_argument('--eps_step', default=0.1)
    parser.add_argument('--max_iter', default=100)
    parser.add_argument('--prob_0', default=0.5)
    parser.add_argument('--targeted', default=False, action='store_true')
    parser.add_argument('--normalize_disable', default=False, action='store_true')

    args = parser.parse_args()
    seed_worker(8)
    device = torch.device("cuda:"+str(args.device)) if torch.cuda.is_available() else "cpu"
    if torch.cuda.is_available(): print('GPU available, use GPU')

    model_setting_str = 'local_epoch_'+str(args.local_epochs) if args.model_type == 'fed_avg' else 'local_epoch_1'
    model_setting_str += '_dropout_' + str(args.dropout).replace('.', '')
    model_setting_str += '_lr_' + str(args.learning_rate)[2:]
    if args.privacy_budget is not None: model_setting_str += '_udp_' + str(args.privacy_budget)

    torch.cuda.empty_cache()
    torch.multiprocessing.set_sharing_strategy('file_system')

    # 1. normalization tmp computations
    weight_norm_mean_dict, weight_norm_std_dict = {}, {}
    weight_sum, weight_sum_square = {}, {}
    for key in ['w0', 'w1', 'w2', 'b0', 'b1', 'b2']:
        weight_norm_mean_dict[key], weight_norm_std_dict[key] = 0, 0
        weight_sum[key], weight_sum_square[key] = 0, 0

    # the updates layer name and their idx in gradient file
    weight_name, bias_name = leak_layer_dict[args.leak_layer][0], leak_layer_dict[args.leak_layer][1]
    weight_idx, bias_idx = leak_layer_idx_dict[weight_name], leak_layer_idx_dict[bias_name]

    # 1.1 read all data and compute the tmp variables
    shadow_training_sample_size, shadow_data_dict = 0, {}
    print('reading file %s' % str(Path(args.save_dir).joinpath('tmp_model_params', args.model_type, args.pred, args.feature_type, args.adv_dataset, model_setting_str)))
    for shadow_idx in range(5):
        print('reading shadow model %d' % (shadow_idx))
        for epoch in range(int(args.num_epochs)):
            adv_federated_model_result_path = Path(args.save_dir).joinpath('tmp_model_params', args.model_type, args.pred, args.feature_type, args.adv_dataset, model_setting_str, 'fold'+str(int(shadow_idx+1)))
            file_str = str(adv_federated_model_result_path.joinpath('gradient_hist_'+str(epoch)+'.pkl'))
            # if shadow_idx == 0 and epoch < 10:
            with open(file_str, 'rb') as f:
                adv_gradient_dict = pickle.load(f)
            for speaker_id in adv_gradient_dict:
                data_key = str(shadow_idx)+'_'+str(epoch)+'_'+speaker_id
                gradients = adv_gradient_dict[speaker_id]['gradient']
                shadow_training_sample_size += 1

                # calculate running stats for computing std and mean
                shadow_data_dict[data_key] = {}
                shadow_data_dict[data_key]['gender'] = adv_gradient_dict[speaker_id]['gender']
                shadow_data_dict[data_key][weight_name] = gradients[weight_idx]
                shadow_data_dict[data_key][bias_name] = gradients[bias_idx]
                for layer_name in leak_layer_dict[args.leak_layer]:
                    weight_sum[layer_name] += gradients[leak_layer_idx_dict[layer_name]]
                    weight_sum_square[layer_name] += gradients[leak_layer_idx_dict[layer_name]]**2

    # 1.2 calculate std and mean
    for key in leak_layer_dict[args.leak_layer]:
        weight_norm_mean_dict[key] = weight_sum[key] / shadow_training_sample_size
        tmp_data = weight_sum_square[key] / shadow_training_sample_size - (weight_sum[key] / shadow_training_sample_size)**2
        weight_norm_std_dict[key] = np.sqrt(tmp_data)

    # 2. we evaluate the attacker performance on service provider training
    import pdb
    if args.normalize_disable:
        attack_model_result_path = Path(os.path.realpath(__file__)).parents[1].joinpath('results', 'attack', args.leak_layer, args.model_type, args.feature_type, model_setting_str+'_norm-disable_True')
    else:
        attack_model_result_path = Path(os.path.realpath(__file__)).parents[1].joinpath('results', 'attack', args.leak_layer, args.model_type, args.feature_type, model_setting_str)
    loss = nn.NLLLoss().to(device)
    save_result_df = pd.DataFrame()
    eval_model = attack_model(args.leak_layer, args.feature_type)
    eval_model.load_state_dict(torch.load(str(attack_model_result_path.joinpath('private_'+args.dataset+'.pt'))))
    eval_model = eval_model.to(device)
    pdb.set_trace()

    if args.eval_undefended is True: # Set to true to evaluate benign performance
        pdb.set_trace()
        print("Evaluating benign attacker performance")
        # 2.1 we perform 5 fold evaluation, since we also train the private data 5 times
        for fold_idx in range(5):
            test_data_dict = {}
            for epoch in range(int(args.num_epochs)):
                row_df = pd.DataFrame(index=['fold'+str(int(fold_idx+1))])

                # Model related
                federated_model_result_path = Path(args.save_dir).joinpath('tmp_model_params', args.model_type, args.pred, args.feature_type, args.dataset, model_setting_str, 'fold'+str(int(fold_idx+1)))
                weight_file_str = str(federated_model_result_path.joinpath('gradient_hist_'+str(epoch)+'.pkl'))

                with open(weight_file_str, 'rb') as f:
                    test_gradient_dict = pickle.load(f)
                for speaker_id in test_gradient_dict:
                    data_key = str(fold_idx)+'_'+str(epoch)+'_'+speaker_id
                    gradients = test_gradient_dict[speaker_id]['gradient']
                    test_data_dict[data_key] = {}
                    test_data_dict[data_key]['gender'] = test_gradient_dict[speaker_id]['gender']
                    test_data_dict[data_key][weight_name] = gradients[weight_idx]
                    test_data_dict[data_key][bias_name] = gradients[bias_idx]

            dataset_test = WeightDataGenerator(list(test_data_dict.keys()), test_data_dict)
            test_loader = DataLoader(dataset_test, batch_size=20, num_workers=0, shuffle=False)
            test_result = evaluate(eval_model, test_loader, loss)

            row_df['acc'], row_df['uar'] = test_result['acc'], test_result['uar']
            save_result_df = pd.concat([save_result_df, row_df])
            save_result_df.to_csv(str(attack_model_result_path.joinpath('private_' + str(args.dataset) + '_result.csv')))
            del dataset_test, test_loader

        print("Performance on benign samples\n")
        print("Average Accuracy = {}, Average UAR = {}".format(np.mean(save_result_df['acc']), np.mean(save_result_df['uar'])))
    if args.privacy_preserve_adversarial:
        print("Evaluating adversarial perturbed attacker performance")
        #model_setting_str +=  '_eps_' + str(args.eps)
        attack_model_result_path = attack_model_result_path.joinpath(
                                    'adversarial_privacy_preserve_norm={}_eps={}_epsstep={}_targeted={}'.format(args.perturb_norm,
                                    args.eps,
                                    args.eps_step,
                                    str(args.targeted)))
        os.makedirs(attack_model_result_path, exist_ok=True)

        # Wrap model for use in art framework to generate adversarial perturbations
        # To generate adversarial perturbations for weight gradients
        wrapped_model_for_weights = PyTorchClassifier(
                        eval_model,
                        loss=loss,
                        input_shape=(256,989),
                        nb_classes=2)
        if args.perturb_norm=='l_inf':
            l_norm = np.inf
        elif args.perturb_norm=='l_2':
            l_norm = 2
        elif args.perturb_norm=='l_1':
            l_norm = 1
        ############################# NEED TO REMOVE HARDCODING #######################################
        pdb.set_trace()
        if args.targeted:
            targeted = True
            prob_0 = args.prob_0
        else:
            targeted = False

        privacy_generator = ProjectedGradientDescent(wrapped_model_for_weights,
                                eps=float(args.eps),
                                eps_step=float(args.eps_step),
                                norm=l_norm,
                                max_iter=int(args.max_iter),
                                targeted=targeted)
        for fold_idx in range(5):
            print("Evaluating attacker model on fold {}".format(fold_idx))
            test_data_dict = {}
            for epoch in range(int(args.num_epochs)):
                row_df = pd.DataFrame(index=['fold'+str(int(fold_idx+1))])

                # Model related
                federated_model_result_path = Path(args.save_dir).joinpath('tmp_model_params', args.model_type, args.pred, args.feature_type, args.dataset, model_setting_str, 'fold'+str(int(fold_idx+1)))
                weight_file_str = str(federated_model_result_path.joinpath('gradient_hist_'+str(epoch)+'.pkl'))

                with open(weight_file_str, 'rb') as f:
                    test_gradient_dict = pickle.load(f)
                for speaker_id in test_gradient_dict:
                    data_key = str(fold_idx)+'_'+str(epoch)+'_'+speaker_id
                    gradients = test_gradient_dict[speaker_id]['gradient']
                    test_data_dict[data_key] = {}
                    test_data_dict[data_key]['gender'] = test_gradient_dict[speaker_id]['gender']
                    test_data_dict[data_key][weight_name] = gradients[weight_idx]
                    test_data_dict[data_key][bias_name] = gradients[bias_idx]

            dataset_test = WeightDataGenerator(list(test_data_dict.keys()), test_data_dict)
            test_loader = DataLoader(dataset_test, batch_size=64, num_workers=0, shuffle=False)
            if targeted:
                test_result = evaluate(eval_model, test_loader, loss, privacy_preserve=True, privacy_generator=privacy_generator, prob_0=args.prob_0)
            else:
                test_result = evaluate(eval_model, test_loader, loss, privacy_preserve=True, privacy_generator=privacy_generator)
            row_df['acc'], row_df['uar'] = test_result['acc'], test_result['uar']
            save_result_df = pd.concat([save_result_df, row_df])
            del dataset_test, test_loader

        pdb.set_trace()
        row_df = pd.DataFrame(index=['average'])
        row_df['acc'], row_df['uar'] = np.mean(save_result_df['acc']), np.mean(save_result_df['uar'])
        save_result_df = pd.concat([save_result_df, row_df])
        save_result_df.to_csv(str(attack_model_result_path.joinpath('private_' + str(args.dataset) + '_result.csv')))
        print("Performance on privacy preserved samples\n")
        print("Average Accuracy = {}, Average UAR = {}".format(np.mean(save_result_df['acc']), np.mean(save_result_df['uar'])))

max_stars_repo_name: rperi/fed-ser-leakage

max_issues_repo_name: rperi/fed-ser-leakage

max_forks_repo_name: rperi/fed-ser-leakage


text:
# -*- coding: utf-8 -*-
"""Untitled35.ipynb

Automatically generated by Colaboratory.

Original file is located at
    https://colab.research.google.com/drive/1fFQ2gD_1KUbFh57Mu95XBH8TUbN-ib6M
"""

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

dataset=pd.read_csv('heart.csv')
dataset=dataset.dropna(how='any')
X=dataset.iloc[:,:-1].values
y=dataset.iloc[:,-1].values

from sklearn.preprocessing import StandardScaler
sc=StandardScaler()
X[:,:1]=sc.fit_transform(X[:,:1])
X[:,3:4]=sc.fit_transform(X[:,3:4])
X[:,4:5]=sc.fit_transform(X[:,4:5])
X[:,7:8]=sc.fit_transform(X[:,7:8])

from sklearn.model_selection import train_test_split
X_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.2,random_state=0)

from sklearn.linear_model import LogisticRegression
LOR=LogisticRegression(random_state=0)
LOR.fit(X_train,y_train)

y_pred=LOR.predict(X_test)
print(np.concatenate((y_pred.reshape(len(y_pred),1), y_test.reshape(len(y_test),1)),1))

from sklearn.metrics import confusion_matrix,accuracy_score
cm = confusion_matrix(y_test, y_pred)
print(cm)
print(accuracy_score(y_test, y_pred)*100)


text:
import argparse
import numpy as np
import os

from utils import read_csv, read_json, write_csv, write_json, dump_json

if __name__ == '__main__':

	parser = argparse.ArgumentParser()
	parser.add_argument('--inputs', type=str, required=True, help='All input data folders, seperated by comma')
	parser.add_argument('--output', type=str, required=True, help='Output folder for all the annotations')
	parser.add_argument('--split', type=float, default=0.8, help='Train split')

	args = parser.parse_args()

	train_data = []
	test_data = []
	fldr_arr = args.inputs.split(',')
	for fldr in fldr_arr:
		compl_data, header = read_csv(fldr + '/annotation.csv', column_header=True)
		write_json(fldr + '/annotation_coco.json', compl_data, fldr)

		json_data = read_json(fldr + '/annotation_coco.json')
		json_data = json_data['data']
		np.random.shuffle(json_data)

		train_split = int(args.split*len(json_data))
		train_data.extend(json_data[:train_split])
		test_data.extend(json_data[train_split:])

	np.random.shuffle(train_data)
	np.random.shuffle(test_data)

	train_dict = {}
	train_dict['data'] = train_data
	dump_json(args.output + '/train_coco.json', train_dict)

	test_dict = {}
	test_dict['data'] = test_data
	dump_json(args.output + '/test_coco.json', test_dict)

	write_csv(args.output + '/train_frcnn.txt', train_data)

	write_csv(args.output + '/test_frcnn.txt', test_data)


text:
#!/usr/bin/env python
'''
http://glad-forest-alert.appspot.com/

115 tiles per date
58cores
29
15

np.where(d[2,:]>1.)

'''

from mpi4py import MPI

comm = MPI.COMM_WORLD
size = comm.Get_size()
rank = comm.Get_rank()

print (size, rank)

#global imports
import os
import numpy as np
from new_alerts import *
from functions import *
#master imports
if rank == 0:
    import tempfile,datetime,sys,time,h5py
    from new_alerts import gettif
#slave imports
else:
    tempdir = None
    filtered = None

#master setup
if rank == 0:
    print sys.argv
    year = False;

    print 'go'
    #create the directory

    tempdir = tempfile.gettempdir()
    tmpdir = comm.bcast( tempdir,root = 0 )

    now = datetime.datetime.now()
    if not year: year = str(now.year)

    select = sys.argv[1]
    filtered_urls = sys.argv[2].split(',')

    filtered = np.array_split(filtered_urls,size)
    start = time.time()
    print len(filtered),len(filtered_urls)

    print 'mp'

#res = pool.map(download,filtered[:10])

#for i in filtered:
    #res.append( download(i,tempdir) )
else:
    None

comm.barrier()
#send tempdir
tempdir = comm.bcast( tempdir,root = 0 )
# send chunks to everyone
filtered = comm.scatter( filtered,root = 0 )

#print( rank, tempdir, len(filtered))

res = []
for i in filtered:
    res.append( download(i,tempdir) )

filtered = np.concatenate(res,axis=1)

#return newly populates sparse array list
filtered = comm.gather(filtered,root=0)

#print ('.')
comm.barrier()
if rank == 0:
    print ('writing to file (slow)')

    data = np.concatenate(filtered,axis=1)

    #we need a row preserving sort
    data = data[np.argsort(data[:,1])]
    data = data[np.argsort(data[:,0])]

    #test the sorting problem
    '''
    import pandas as pd
    pd.DataFrame(data).to_csv('test.csv')
    print 'csv'
    '''

    '''
    f = tuple(open('test.csv'))
    plt.scatter(np.array(f[1].split(',')).astype(float),np.array(f[2].split(',')).astype(float))
    '''

    print max(data[0,:]), max(data[1,:]), max(data[2,:]),'\n', min(data[0,:]), min(data[1,:]),'\n',data[:,::50]

    with h5py.File('data/glad_data_%s.h5'%year,'a') as hf:
        try:      hf.attrs['created_on']
        except:   hf.attrs['created_on'] = str(now)

        if select in hf.keys() : group = hf[select]
        else : group = hf.create_group(select)

        group.attrs['urls'] = ','.join(filtered_urls)
        group.attrs['time'] = time.time() - start

        try: del group['data']
        except: None

        group.create_dataset('data', data.shape , data = data,dtype=float,compression='gzip',compression_opts=9)

    try: hf.close()
    except: None

    os.system('rm -rf %s/*.tif'%tempdir)
    sys.stdout.flush()
    print( 'finished ')

max_stars_repo_name: scpyork/GladAlert

max_issues_repo_name: scpyork/GladAlert

max_forks_repo_name: scpyork/GladAlert


text:
from sympy import *
from sympy.parsing.sympy_parser import parse_expr

#funtion to convert the input into a binary function:
def ToBin(domain, eq, var):
  numOfVars = len(var)
  for i in var:
    vars()[i] = symbols(i) #initiate the symbols variables

  print(eq)
  #change variable names to avoid mixups:
  for i in range(numOfVars):
    eq = eq.subs(vars()["v" + str(i)], "v" + str(i)+"temp") #temprarily swap the names of the variables to avoid mixups when substituting

  place = 0
  places = []

  for i in range(numOfVars): #loop over the number of variables
    upper = domain[i] #specify the upper bound of this variable

    power = 0

    while(2**power <= upper):
      power += 1 #find the number of binary place values required for the specified upper bound

    power -= 1
    places.append(power) #store this value in an array to be used later in converting back to decimal

    bina = "0.25*v" + str(place) + " + "

    place += 1

    bina += "0.5*v" + str(place) + " + "

    place += 1

    for j in range(power): #loop over eqch binary place value and specify its magnitude
      bina += "(2**" + str(j) + ")*v" + str(place) + " + "
      place += 1

    bina = bina[:-2:]

    #if the input equation is binary, we don't need to worry about the place values, so we can leave it as-is
    if(power == 0):
        eq = eq.subs(("v" + str(i) + "temp"), ("v" + str(i)))
    else:
        eq = eq.subs(("v" + str(i)+"temp"), bina) #substitute the binary vars in for the decimal ones

  eq = expand(eq)
  #replace fraction with decimal
  strEq = str(eq)

  strEq = strEq.replace("(", "")
  strEq = strEq.replace(")", "")

  termsPosNotSplit = []
  termsNegNotSplit = []
  termsPos = []
  termsNeg = []

  termsPls = strEq.split("+")
  for i in termsPls:
    ii = i.split("-") #split along negative as well
    for j in range(len(ii)):
      if(j > 0): #if not the first entry (not positive)
        if("*" in ii[j]): #if there is a "*" in this term
          termsNegNotSplit.append(ii[j])
        else:
          termsNeg.append(ii[j])

      else:
        if("*" in ii[j]): #if there is a "*" in this term
          termsPosNotSplit.append(ii[j])
        else:
          termsPos.append(ii[j])

  for i in termsPosNotSplit: #split along * and add to new array
    ii = i.split("*")
    for j in ii:
      if ("/" in j):
        divisor = float(j.split("/")[1]) #find the divisor
        j = j[:-(len(j) - j.find("/")):] #remove everything after "/" (included)
        j = str(1/divisor) + "*" + j #multiply by 1/divisor
    reCon = ""
    for j in ii:
      reCon += j + "*"
    reCon = reCon[:-2:]
    i = reCon

  for i in termsNegNotSplit: #split along * and add to new array
    ii = i.split("*")
    for j in ii:
      if ("/" in j):
        divisor = float(j.split("/")[1]) #find the divisor
        j = j[:-(len(j) - j.find("/")):] #remove everything after "/" (included)
        j = str(1/divisor) + "*" + j #multiply by 1/divisor
    reCon = ""
    for j in ii:
      reCon += j + "*"
    reCon = reCon[:-2:]
    i = reCon

  for i in range(len(termsPos)):
    if("/" in termsPos[i]): #for all terms containing "/""
      divisor = float(termsPos[i].split("/")[1]) #find the divisor
      termsPos[i] = termsPos[i][:-(len(termsPos[i]) - termsPos[i].find("/")):] #remove everything after "/" (included)
      termsPos[i] = str(1/divisor) + "*" + termsPos[i] #multiply by 1/divisor
  for i in range(len(termsNeg)):
    if("/" in termsNeg[i]): #for all terms containing "/""
      divisor = float(termsNeg[i].split("/")[1]) #find the divisor
      termsNeg[i] = termsNeg[i][:-(len(termsNeg[i]) - termsNeg[i].find("/")):] #remove everything after "/" (included)
      termsNeg[i] = str(1/divisor) + "*" + termsNeg[i] #multiply by 1/divisor
  strEq = ""
  for i in termsPos:
    strEq += i + " + "
  for i in termsPosNotSplit:
    strEq += i + " + "
  strEq = strEq[:-3]
  strEq += " - "
  for i in termsNeg:
    strEq += i + " - "
  for i in termsNegNotSplit:
    strEq += i + " - "
  strEq = strEq[:-3]
  eq = parse_expr(strEq, evaluate=False)
  strEq = str(eq)

  return (strEq, place, places) #return the string of the binary function, the number of binary vars, and the number of binary digits in each base 10 number, used to convert back to decimal

#function to convert the binary results into base 10
def ToDec(places, values):
  #first determine if the input equation was binary by seeing if places is full of all 0s
  boolean = True
  for i in places:
    if (i != 0):
      boolean = False

  if (boolean): #if the input is already boolean, then just return the results in base 2
    return (values)

  print(places)
  pos = 0 #keep track of the place value that we are on
  outVars = [] #an array to store the output values
  for i in places: #loop over the number of digits in each base ten var
    tot = 0
    i+= 2
    for j in range(i): #loop over the cooresponding binary place vals
      tot += 2**(j-2) * values[pos] #convert to decimal
      pos += 1
    outVars.append(tot) #append the result to the output array
  return(outVars)

max_stars_repo_name: cogrpar/qcpuWARE

max_issues_repo_name: cogrpar/qcpuWARE

max_forks_repo_name: cogrpar/qcpuWARE

reason: from sympy


text:
import os
import tensorflow as tf
import numpy as np
import imageio
import json

trans_t = lambda t : tf.convert_to_tensor([
    [1,0,0,0],
    [0,1,0,0],
    [0,0,1,t],
    [0,0,0,1],
], dtype=tf.float32)

rot_phi = lambda phi : tf.convert_to_tensor([
    [1,0,0,0],
    [0,tf.cos(phi),-tf.sin(phi),0],
    [0,tf.sin(phi), tf.cos(phi),0],
    [0,0,0,1],
], dtype=tf.float32)

rot_theta = lambda th : tf.convert_to_tensor([
    [tf.cos(th),0,-tf.sin(th),0],
    [0,1,0,0],
    [tf.sin(th),0, tf.cos(th),0],
    [0,0,0,1],
], dtype=tf.float32)

def pose_spherical(theta, phi, radius):
    c2w = trans_t(radius)
    c2w = rot_phi(phi/180.*np.pi) @ c2w
    c2w = rot_theta(theta/180.*np.pi) @ c2w
    c2w = np.array([[-1,0,0,0],[0,0,1,0],[0,1,0,0],[0,0,0,1]]) @ c2w
    return c2w

def load_blender_data(basedir, half_res=False, testskip=1):
    splits = ['train', 'val', 'test']
    metas = {}
    for s in splits:
        with open(os.path.join(basedir, 'transforms_{}.json'.format(s)), 'r') as fp:
            metas[s] = json.load(fp)

    all_imgs = []
    all_poses = []
    counts = [0]
    for s in splits:
        meta = metas[s]
        imgs = []
        poses = []
        if s=='train' or testskip==0:
            skip = 1
        else:
            skip = testskip

        for frame in meta['frames'][::skip]:
            fname = os.path.join(basedir, frame['file_path'] + '.png')
            imgs.append(imageio.imread(fname))
            poses.append(np.array(frame['transform_matrix']))
        imgs = (np.array(imgs) / 255.).astype(np.float32) # keep all 4 channels (RGBA)
        poses = np.array(poses).astype(np.float32)
        counts.append(counts[-1] + imgs.shape[0])
        all_imgs.append(imgs)
        all_poses.append(poses)

    i_split = [np.arange(counts[i], counts[i+1]) for i in range(3)]

    imgs = np.concatenate(all_imgs, 0)
    poses = np.concatenate(all_poses, 0)

    H, W = imgs[0].shape[:2]
    camera_angle_x = float(meta['camera_angle_x'])
    focal = .5 * W / np.tan(.5 * camera_angle_x)

    if False:
        num_poses = 24
        render_poses0 = tf.stack(
            [pose_spherical(angle, -30.0, 4.0) for angle in np.linspace(-180, 180, num_poses + 1)[:-1]], 0)
        render_poses1 = tf.stack(
            [pose_spherical(angle, -30.0, 3.0) for angle in np.linspace(-180, 180, num_poses + 1)[:-1]], 0)
        render_poses2 = tf.stack(
            [pose_spherical(angle, -30.0, 2.5) for angle in np.linspace(-180, 180, num_poses + 1)[:-1]], 0)
        render_poses = tf.concat([render_poses0, render_poses1, render_poses2], 0)

    if True:
        num_poses = 240
        max_dist = 4.0
        poses = []
        for i, angle in enumerate(np.linspace(-180, 180, num_poses + 1)[:-1]):
            poses.append(pose_spherical(angle, -30.0, max_dist*(0.5+0.5*(num_poses-i)/num_poses)))
        render_poses = tf.stack(poses, 0)
        #render_poses = tf.stack([pose_spherical(angle, -30.0, 4.0*(0.5+0.5*(num_poses-i)/num_poses)) for angle in np.linspace(-180, 180, num_poses + 1)[:-1]], 0)

    if half_res:
        imgs = tf.image.resize_area(imgs, [400, 400]).numpy()
        H = H//2
        W = W//2
        focal = focal/2.

    return imgs, poses, render_poses, [H, W, focal], i_split

max_stars_repo_name: arthurlirui/nerf

max_issues_repo_name: arthurlirui/nerf

max_forks_repo_name: arthurlirui/nerf


text:
import cv2 as cv
import numpy as np
import sys
import os

from maskrcnn_benchmark.config import cfg
from demo.predictor import COCODemo

config_file = '../../maskrcnn-benchmark/configs/caffe2/e2e_mask_rcnn_R_50_FPN_1x_caffe2.yaml'
cfg.merge_from_file(config_file)
# manual override some options
cfg.merge_from_list(["MODEL.DEVICE", 'cuda'])
coco_demo = COCODemo(
    cfg,
    min_image_size=800,
    confidence_threshold=0.7,
)

fb_params = dict(pyr_scale = 0.5,
                 levels = 3,
                 winsize = 3,
                 iterations = 3,
                 poly_n = 5,
                 poly_sigma = 1.2,
                 flags = 0)
sequence = sys.argv[1]
s = int(sys.argv[2])

iml = cv.imread('/storage/remote/atcremers17/linp/dataset/kittic/sequences/{}/'.format(sequence)+'image_2/{0:06}.png'.format(s))
a = coco_demo.compute_prediction(iml)
top = coco_demo.select_top_predictions(a)
masks = top.get_field("mask").numpy()
labels = top.get_field("labels").numpy()
h,w = iml.shape[:2]
p_color = (0, 0, 255) # BGR
o_color = (0, 255, 0)
res = []
biml = iml.copy()
for i in range(len(masks)):
    if labels[i] in {1,2,3,4,6,8}:
        box = top.bbox[i]
        x1,y1,x2,y2 = map(int,box)
        mask = masks[i].squeeze()
        res.append([mask,x1,y1,x2,y2])
        biml[mask] = o_color
cv.imwrite('dol/{0:06}.png'.format(s),biml)

old_gray = cv.cvtColor(iml, cv.COLOR_BGR2GRAY)

n = 5
for i in range(1,n):
    ci = s+i
    print('{} frame'.format(ci))
    cr = []
    iml = cv.imread('/storage/remote/atcremers17/linp/dataset/kittic/sequences/{}/'.format(sequence)+'image_2/{0:06}.png'.format(ci))
    frame_gray = cv.cvtColor(iml, cv.COLOR_BGR2GRAY)
    flow = cv.calcOpticalFlowFarneback(old_gray, frame_gray, None, **fb_params)
    # res, st, err = cv.calcOpticalFlowPyrLK(old_gray, frame_gray, res, None, **lk_params)
    for mask,  y1, x1, y2, x2 in res:
        dy, dx = np.mean(flow[mask],axis=0)
        cmp = np.where(mask==True)
        nmask = np.zeros_like(mask,dtype=np.bool)
        for x, y in zip(cmp[0],cmp[1]):
            cx = max(min(round(x+dx),h-1),0)
            cy = max(min(round(y+dy),w-1),0)
            nmask[cx,cy] = True
        cr.append([nmask,max(min(round(y1+dy),w-1),0),max(min(round(x1+dx),h-1),0),max(min(round(y2+dy),w-1),0),max(min(round(x2+dx),h-1),0)])
    res = cr
    old_gray = frame_gray
    a = coco_demo.compute_prediction(iml)
    top = coco_demo.select_top_predictions(a)
    masks = top.get_field("mask").numpy()
    labels = top.get_field("labels").numpy()
    biml = iml.copy()
    for i in range(len(masks)):
        if labels[i] in {1,2,3,4,6,8}:
            box = top.bbox[i]
            x1,y1,x2,y2 = map(int,box)
            biml = cv.circle(biml,(x1,y1),5,o_color,-1)
            biml = cv.circle(biml,(x2,y2),5,o_color,-1)
    for mask, x1, y1, x2, y2 in res:
        biml = cv.circle(biml,(x1,y1),5,p_color,-1)
        biml = cv.circle(biml,(x2,y2),5,p_color,-1)
        biml[mask] = p_color
    cv.imwrite('dol/{0:06}.png'.format(ci),biml)


text:
# Licensed under a 3-clause BSD style license - see LICENSE.rst

from __future__ import (absolute_import, division, print_function,
                        unicode_literals)

import inspect

import numpy as np

from ...extern import six
from ...tests.helper import catch_warnings, pytest
from ... import units as u

from ..nddata import NDData
from ..decorators import support_nddata

@support_nddata
def wrapped_function_1(data, wcs=None, unit=None):
    return data, wcs, unit

def test_pass_numpy():

    data_in = np.array([1, 2, 3])
    data_out, wcs_out, unit_out = wrapped_function_1(data=data_in)

    assert data_out is data_in
    assert wcs_out is None
    assert unit_out is None

def test_pass_all_separate():

    data_in = np.array([1, 2, 3])
    wcs_in = "the wcs"
    unit_in = u.Jy

    data_out, wcs_out, unit_out = wrapped_function_1(data=data_in, wcs=wcs_in, unit=unit_in)

    assert data_out is data_in
    assert wcs_out is wcs_in
    assert unit_out is unit_in

def test_pass_nddata():

    data_in = np.array([1, 2, 3])
    wcs_in = "the wcs"
    unit_in = u.Jy

    nddata_in = NDData(data_in, wcs=wcs_in, unit=unit_in)

    data_out, wcs_out, unit_out = wrapped_function_1(nddata_in)

    assert data_out is data_in
    assert wcs_out is wcs_in
    assert unit_out is unit_in

def test_pass_nddata_and_explicit():

    data_in = np.array([1, 2, 3])
    wcs_in = "the wcs"
    unit_in = u.Jy
    unit_in_alt = u.mJy

    nddata_in = NDData(data_in, wcs=wcs_in, unit=unit_in)

    with catch_warnings() as w:
        data_out, wcs_out, unit_out = wrapped_function_1(nddata_in, unit=unit_in_alt)

    assert data_out is data_in
    assert wcs_out is wcs_in
    assert unit_out is unit_in_alt

    assert len(w) == 1
    assert str(w[0].message) == ("Property unit has been passed explicitly and as "
                                 "an NDData property, using explicitly specified value")

def test_pass_nddata_ignored():

    data_in = np.array([1, 2, 3])
    wcs_in = "the wcs"
    unit_in = u.Jy

    nddata_in = NDData(data_in, wcs=wcs_in, unit=unit_in, mask=[0, 1, 0])

    with catch_warnings() as w:
        data_out, wcs_out, unit_out = wrapped_function_1(nddata_in)

    assert data_out is data_in
    assert wcs_out is wcs_in
    assert unit_out is unit_in

    assert len(w) == 1
    assert str(w[0].message) == ("The following attributes were set on the data "
                                 "object, but will be ignored by the function: mask")

def test_incorrect_first_argument():

    with pytest.raises(ValueError) as exc:
        @support_nddata
        def wrapped_function_2(something, wcs=None, unit=None):
            pass
    assert exc.value.args[0] == "Can only wrap functions whose first positional argument is `data`"

    with pytest.raises(ValueError) as exc:
        @support_nddata
        def wrapped_function_3(something, data, wcs=None, unit=None):
            pass
    assert exc.value.args[0] == "Can only wrap functions whose first positional argument is `data`"

    with pytest.raises(ValueError) as exc:
        @support_nddata
        def wrapped_function_4(wcs=None, unit=None):
            pass
    assert exc.value.args[0] == "Can only wrap functions whose first positional argument is `data`"

def test_wrap_function_no_kwargs():

    @support_nddata
    def wrapped_function_5(data, other_data):
        return data

    data_in = np.array([1, 2, 3])
    nddata_in = NDData(data_in)

    assert wrapped_function_5(nddata_in, [1, 2, 3]) is data_in

def test_wrap_function_repack_valid():

    @support_nddata(repack=True, returns=['data'])
    def wrapped_function_5(data, other_data):
        return data

    data_in = np.array([1, 2, 3])
    nddata_in = NDData(data_in)

    nddata_out = wrapped_function_5(nddata_in, [1, 2, 3])

    assert isinstance(nddata_out, NDData)
    assert nddata_out.data is data_in

def test_wrap_function_accepts():

    class MyData(NDData):
        pass

    @support_nddata(accepts=MyData)
    def wrapped_function_5(data, other_data):
        return data

    data_in = np.array([1, 2, 3])
    nddata_in = NDData(data_in)
    mydata_in = MyData(data_in)

    assert wrapped_function_5(mydata_in, [1, 2, 3]) is data_in

    with pytest.raises(TypeError) as exc:
        wrapped_function_5(nddata_in, [1, 2, 3])
    assert exc.value.args[0] == "Only NDData sub-classes that inherit from MyData can be used by this function"

def test_wrap_preserve_signature_docstring():

    @support_nddata
    def wrapped_function_6(data, wcs=None, unit=None):
        """
        An awesome function
        """
        pass

    if wrapped_function_6.__doc__ is not None:
        assert wrapped_function_6.__doc__.strip() == "An awesome function"

    if six.PY3:
        signature = inspect.formatargspec(
            *inspect.getfullargspec(wrapped_function_6))
    else:
        signature = inspect.formatargspec(
            *inspect.getargspec(wrapped_function_6))

    assert signature == "(data, wcs=None, unit=None)"

max_stars_repo_path: NASA SPACEAPPS CHALLENGE/Solution/Software part/Astronomical Data and Python Libraries/Astropy/astropy-1.1.2/astropy/nddata/tests/test_decorators.py

max_stars_repo_name: sahirsharma/Martian

max_issues_repo_name: sahirsharma/Martian

max_forks_repo_name: sahirsharma/Martian