text:
# coding: utf-8

# In[1]:

get_ipython().run_line_magic('matplotlib', 'inline')

# In[2]:

#-------------------------------------------------------------------------------------------------------------------------------
# By Alexandra Lee (July 2018)
#
# Generate input files
#
# Dataset: Pseudomonas aeruginosa gene expression compendium referenced in https://www.ncbi.nlm.nih.gov/pmc/articles/PMC5069748/
#
# Use map_file to group samples into phenotype groups (condition A and B) based on experimental design annotations
# Example: control vs treatment with antibiotics
#
# Then group samples into training and test sets
#
# Generate offset vector using gene expression data in the original space (train_offset_original):
# average gene expression for condition A - average gene expression for condition B using all genes/dimensions
#-------------------------------------------------------------------------------------------------------------------------------
import os
import pandas as pd
import numpy as np
from scipy.stats import variation
import seaborn as sns
import matplotlib.pyplot as plt

randomState = 123
from numpy.random import seed
seed(randomState)

# In[3]:

# load arguments
data_file = os.path.join(os.path.dirname(os.getcwd()), "data", "all-pseudomonas-gene-normalized.zip")
map_file = os.path.join(os.path.dirname(os.getcwd()), "metadata", "mapping_cipro.txt")

# output files
fig_file = os.path.join(os.path.dirname(os.getcwd()), "viz", "cipro_treatment", "cv.png")
train_control_file = os.path.join(os.path.dirname(os.getcwd()), "data", "cipro_treatment", "train_control.txt")
train_treat_file = os.path.join(os.path.dirname(os.getcwd()), "data", "cipro_treatment", "train_treat.txt")
test_control_file = os.path.join(os.path.dirname(os.getcwd()), "data", "cipro_treatment","test_control.txt")
test_treat_file = os.path.join(os.path.dirname(os.getcwd()), "data", "cipro_treatment", "test_treat.txt")
train_input_file = os.path.join(os.path.dirname(os.getcwd()), "data", "cipro_treatment", "train_model_input.txt.xz")
original_offset_file = os.path.join(os.path.dirname(os.getcwd()), "data", "cipro_treatment", "train_offset_original.txt")

# In[4]:

# read in data
data = pd.read_table(data_file, header = 0, sep = '\t', index_col = 0, compression='zip')
X = data.transpose()
X.head(5)

# In[5]:

# read in metadata file containing grouping of each sample into training/test and phenotypic group
grp = pd.read_table(map_file, header=0, sep='\t', index_col=None)
grp

# In[6]:

# Group samples into condition A and B based on mapping file provided
control_all = pd.DataFrame()
treat_all = pd.DataFrame()

for index, row in grp.iterrows():
    if row['Group'] == 'control':
        sample = str(row['Sample ID'])
        control_all = control_all.append(X[X.index.str.contains(sample, regex=False)])
        #print('Training group A {}'.format(sample))
    else:
        sample = str(row['Sample ID'])
        treat_all = treat_all.append(X[X.index.str.contains(sample, regex=False)])
        #print('Training group B {}'.format(sample))

# Split 10% test set randomly
test_set_percent = 0.2
test_control = control_all.sample(frac=test_set_percent, random_state = randomState)
train_control = control_all.drop(test_control.index)

test_treat = treat_all.sample(frac=test_set_percent, random_state = randomState)
train_treat = treat_all.drop(test_treat.index)

#control_all
train_treat
#test_treat

# In[7]:

# Calculate Coefficient of Variance (CV) to determine variance between samples
# CV is the standardized measure of dispersion from the mean
treat_cv = variation(treat_all, axis = 0)
treat_cv = pd.DataFrame(treat_cv, columns = ['cv_treat'])
treat_cv.insert(0, 'gene_id', treat_all.columns)

control_cv = variation(control_all, axis = 0)
control_cv = pd.DataFrame(control_cv, columns = ['cv_control'])
control_cv.insert(0, 'gene_id', control_all.columns)

# Join
CV = pd.merge(treat_cv, control_cv, on = 'gene_id')
CV = pd.melt(CV, id_vars = 'gene_id', var_name = 'group', value_name = 'cv' )
CV.head(5)

# figure
fig = plt.figure()
fg = sns.boxplot(x = 'group', y='cv', hue='group',  data=CV, palette="Set3")
fig.savefig(fig_file)

# In[8]:

# Create input holding out test test
input_holdout = X.drop(test_control.index)
input_holdout = input_holdout.drop(test_treat.index)

input_holdout.head(5)
input_holdout.shape
#X.shape

# In[9]:

# Average gene expression across samples in training set
train_control_mean = train_control.mean(axis=0)
train_treat_mean = train_treat.mean(axis=0)

# Generate offset using average gene expression in original dataset
train_offset_original = train_treat_mean - train_control_mean
train_offset_original_df = pd.Series.to_frame(train_offset_original).transpose()
train_offset_original_df

# In[10]:

# Output
train_control.to_csv(train_control_file, sep='\t')
train_treat.to_csv(train_treat_file, sep='\t')

test_control.to_csv(test_control_file, sep='\t')
test_treat.to_csv(test_treat_file, sep='\t')

train_offset_original_df.to_csv(original_offset_file, sep='\t')

input_holdout.to_csv(train_input_file, sep='\t', compression='xz')

reason: import numpy,from numpy,from scipy


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

import torch
from torch.autograd import Variable
import torchvision.transforms as transforms
import torchvision.datasets as datasets

from deformation import ADef, Tnorm_vec
from vector_fields import draw_vector_field

from models_mnist import mnist_a, mnist_b

from write_results import write_results

def demo_mnist(adversarial, batch_size, sigma, l, mu, norm_type, plot_defs,with_gpu=True,max_norm= 3):

    # ADef config
    test_case= 'mnist'
    candidates = range(10)
    max_iter = 100
    max_norm = max_norm #np.inf
    overshoot = 1.2
    strong_targets = False
    verbose = False

    path_to_resources = 'resources/'
    if adversarial == 'affine':
        path_to_model = path_to_resources + 'mnist_a_e5b50_affine_model.pt'
    elif adversarial == 'translate':
        path_to_model = path_to_resources + 'mnist_a_e5b50_translate_model.pt'
    elif adversarial == 'clean':
        path_to_model = path_to_resources + 'mnist_a_e5b50_clean_model.pt'
    else:
        raise ValueError('neither clean nor affine model selected')

    net = mnist_a()
    if os.path.exists( path_to_model ):
        net.load_state_dict( torch.load(path_to_model, map_location=lambda storage, loc: storage) )
    else:
        print('Model not found. Run \'train_mnist.py\' first!\nDeforming images w.r.t. an untrained model.')
        verbose = False

    if with_gpu:
        net.to(torch.device("cuda"))
    net.eval()
    print('Model: ' + str(type(net)), 'sigma:',sigma,'l:',l,'mu:',mu)

    batch_size = batch_size
    mnist_test = datasets.MNIST( path_to_resources, train=False, download=True, transform=transforms.ToTensor() )
    test_loader = torch.utils.data.DataLoader(mnist_test, batch_size=batch_size, shuffle=True, pin_memory=False)

    # Get a random batch of images
    batch, labels = next(iter( test_loader ))

    #convert to gpu
    if with_gpu:
        batch = batch.cuda()
        labels = labels.cuda()

    x = Variable( batch)
    Fx = net.forward(x)
    maxval, pred_labels = torch.max( Fx.data, 1 )
    diff_labels = pred_labels != labels

    if (diff_labels).any():
        print('Misclassified digit(s).')

    # Deform image using ADef
    def_batch, def_data = ADef( batch, net, ind_candidates=candidates, norm_type=norm_type, l=l, mu=mu,
                                max_norm=max_norm, max_iter=max_iter,
                                smooth=sigma, overshoot=overshoot,
                                targeting=strong_targets, verbose=verbose )

    def_labels = def_data['deformed_labels']
    vector_fields = def_data['vector_fields']
    eikdiff, e_total = Tnorm_vec(vector_fields.cpu(), norm_type, l, mu)

    write_results(adversarial,test_case,norm_type,def_data,batch_size,sigma,l,mu, e_total)

    if plot_defs:
        fig, axs = plt.subplots(2, batch_size)
        if batch_size == 1:
            im = batch[ 0, 0 ].cpu().numpy()
            def_im = def_batch[ 0, 0 ].cpu().numpy()
            axs[0].imshow( im, cmap='Greys', vmin=0, vmax=1 )
            draw_vector_field( axs[ 0], vector_fields[ 0 ].cpu(), amp=3 )
            if not pred_labels[0] == labels[0]:
                axs[0].set_title( 'Misclf. as %d' % pred_labels[0], color='red' )
            else:
                axs[0].set_title( '%d' % pred_labels[0] )
            axs[1].imshow( def_im, cmap='Greys', vmin=0, vmax=1 )
            axs[1].set_title( '%d' % def_labels[0] )
            axs[0].set_ylabel('Original')
            axs[1].set_ylabel('Deformed')
            plt.show()
            eikdiff, e_total= Tnorm_vec(vector_fields.cpu(), norm_type, l, mu)
            print(e_total,l,mu)
            heatmap = plt.imshow(eikdiff[0], cmap='hot', interpolation='nearest')
            plt.colorbar(heatmap)
            plt.show()
        else:
            for im_no in range(batch_size):
                im = batch[ im_no, 0 ].numpy()
                def_im = def_batch[ im_no, 0 ].numpy()
                axs[ 0, im_no ].imshow( im, cmap='Greys', vmin=0, vmax=1 )
                draw_vector_field( axs[ 0, im_no ], vector_fields[ im_no ].cpu(), amp=3 )
                if not pred_labels[im_no] == labels[im_no]:
                    axs[ 0, im_no ].set_title( 'Misclf. as %d' % pred_labels[im_no], color='red' )
                else:
                    axs[ 0, im_no ].set_title( '%d' % pred_labels[im_no] )
                axs[ 1, im_no ].imshow( def_im, cmap='Greys', vmin=0, vmax=1 )
                axs[ 1, im_no ].set_title( '%d' % def_labels[im_no] )
                axs[0,0].set_ylabel('Original')
                axs[1,0].set_ylabel('Deformed')
            plt.show()

ext: py

reason: import numpy


text:
# Code from Chapter 7 of Machine Learning: An Algorithmic Perspective (2nd Edition)
# by Stephen Marsland (http://stephenmonika.net)

# You are free to use, change, or redistribute the code in any way you wish for
# non-commercial purposes, but please maintain the name of the original author.
# This code comes with no warranty of any kind.

# Stephen Marsland, 2008, 2014

import pylab as pl
import numpy as np

def GMM():
    """ Fits two Gaussians to data using the EM algorithm """
    N = 100
    pl.ion()

    y = 1. * np.zeros(N)
    # Set up data
    out1 = np.random.normal(6, 1, N)
    out2 = np.random.normal(1, 1, N)
    choice = np.random.rand(N)

    w = [choice >= 0.5]
    y[w] = out1[w]
    w = [choice < 0.5]
    y[w] = out2[w]

    pl.clf()
    pl.hist(y, fc='0.5')

    # Now do some learning

    # Initialisation
    mu1 = y[np.random.randint(0, N - 1, 1)]
    mu2 = y[np.random.randint(0, N - 1, 1)]
    s1 = np.sum((y - np.mean(y)) ** 2) / N
    s2 = s1
    pi = 0.5

    # EM loop
    count = 0
    gamma = 1. * np.zeros(N)
    nits = 20

    ll = 1. * np.zeros(nits)

    while count < nits:
        count = count + 1

        # E-step
        for i in range(N):
            gamma[i] = pi * np.exp(-(y[i] - mu1) ** 2 / (2 * s1)) / (
                pi * np.exp(-(y[i] - mu1) ** 2 / (2 * s1)) + (1 - pi) * np.exp(-(y[i] - mu2) ** 2 / 2 * s2))

        # M-step
        mu1 = np.sum((1 - gamma) * y) / np.sum(1 - gamma)
        mu2 = np.sum(gamma * y) / np.sum(gamma)
        s1 = np.sum((1 - gamma) * (y - mu1) ** 2) / np.sum(1 - gamma)
        s2 = np.sum(gamma * (y - mu2) ** 2) / np.sum(gamma)
        pi = np.sum(gamma) / N

        ll[count - 1] = np.sum(
            np.log(pi * np.exp(-(y[i] - mu1) ** 2 / (2 * s1)) + (1 - pi) * np.exp(-(y[i] - mu2) ** 2 / (2 * s2))))

    x = np.arange(-2, 8.5, 0.1)
    y = 35 * pi * np.exp(-(x - mu1) ** 2 / (2 * s1)) + 35 * (1 - pi) * np.exp(-(x - mu2) ** 2 / (2 * s2))

    pl.plot(x, y, 'k', linewidth=4)
    pl.figure(), pl.plot(ll, 'ko-')
    pl.show()

GMM()

max_stars_repo_name: quietcoolwu/MLCode

max_issues_repo_name: quietcoolwu/MLCode

max_forks_repo_name: quietcoolwu/MLCode


text:
# License: BSD 3 clause

from copy import copy
from datetime import datetime
from typing import List, Tuple

import numpy as np
import pyspark.sql.functions as sf
import pytz
from pyspark.ml.feature import Bucketizer
from pyspark.sql import DataFrame

from scalpel.core.cohort import Cohort
from scalpel.core.util import rename_df_columns

class BaseFeatureDriver(object):
    """Base class of for drivers, which should load data from cohorts to a specific
    format.

    Loaders should not be designed to do filtering or sanitizing. Users are responsible
    for the inputs: all event timestamps should be in (study_start, study_end) and
    age_groups should match the ages of the base_population contained in the studied
    cohort.

    These checks should be implemented in the `check_metadata` method.

    There are important considerations to be aware of when working with timestamps and
    dataframes, see pyspark documentation:
    https://spark.apache.org/docs/latest/sql-pyspark-pandas-with-arrow.html#timestamp-with-time-zone-semantics

    Parameters
    ----------
    base_population: `Cohort`
        Initial cohort containing the subjects for which we want to compute the data.
        This cohort should contain subjects, but its events are not used.

    followups: `Cohort`
        Cohort containing the follow up information. This cohort should contain subjects
        and events matching the FollowUp case class, that is the columns: 'patientID',
        'start', 'end', 'endReason'.

    study_start: `datetime`
        Date of the study start. Beware of timezones issues!

    study_end: `datetime`
        Date of the study end. Beware of timezones issues!

    age_reference_date: `datetime`
        Date used to compute the age of the base_population contained in the cohorts.

    age_groups: `List[int]`, default=None
        Bounds defining longitudinal age groups. If set to None, age related data are
        not computed. These bounds must be sorted. Beware: take into account the ageing
        of base_population when defining these. Minimum bound should be <= minimum age,
        maximum bound should be >= maximum age + study length.

    run_checks: `bool`, default=True
        Automated checks are performed on cohorts passed to the drivers. If you don't
        want these checks to be ran, set this option to False. Disabling the checks
        might increase performance, but use at your own risk!
    """

    def __init__(
        self,
        base_population: Cohort,
        followups: Cohort,
        study_start: datetime,
        study_end: datetime,
        age_reference_date: datetime,
        age_groups: list = None,
        run_checks: bool = True,
    ):
        self.run_checks = run_checks
        self._study_start = None
        self._study_end = None
        self._is_using_longitudinal_age_groups = False

        if not self._has_timezone(study_start):
            raise ValueError("study_start should have a timezone. Please use pytz.")
        if not self._has_timezone(study_end):
            raise ValueError("study_end should have a timezone. Please use pytz.")
        if study_start >= study_end:
            raise ValueError("study_start should be < study_end")
        self._study_start = study_start
        self._study_end = study_end

        if not self._has_timezone(age_reference_date):
            raise ValueError(
                "age_reference_date should have a timezone. " "Please use pytz."
            )
        if age_reference_date < self.study_start:
            raise ValueError("age_reference_date should be >= study_start.")
        self._age_reference_date = age_reference_date
        self._age_groups = None
        self.age_groups = age_groups
        self.n_age_groups = len(age_groups) - 1 if age_groups is not None else 0

        self._followups = None
        self._base_population = None
        self.followups = followups
        self.base_population = base_population

    def load(self):
        raise NotImplementedError("load method is not implemented in BaseLoader.")

    @property
    def study_start(self):
        return self._study_start

    @study_start.setter
    def study_start(self, value):
        raise PermissionError(
            "study_start should not be updated after loader initialisation"
        )

    @property
    def study_end(self):
        return self._study_end

    @study_end.setter
    def study_end(self, value):
        raise PermissionError(
            "study_end should not be updated after loader initialisation"
        )

    @property
    def age_reference_date(self) -> datetime:
        return self._age_reference_date

    @age_reference_date.setter
    def age_reference_date(self, value: datetime) -> None:
        raise PermissionError(
            "age_reference_date should not be updated after loader initialisation."
        )

    @property
    def age_groups(self) -> List[float]:
        return self._age_groups

    @age_groups.setter
    def age_groups(self, value: List[float]) -> None:
        if value != sorted(value):
            raise ValueError("age_groups bounds should be sorted.")
        self._age_groups = value

    @property
    def base_population(self) -> Cohort:
        return self._base_population

    @base_population.setter
    def base_population(self, value: Cohort) -> None:
        if self.run_checks and self.age_groups is not None:
            invalid = self._find_subjects_with_age_inconsistent_w_age_groups(value)
            if invalid.subjects.take(1):
                raise ValueError(
                    self._log_invalid_events_cohort(invalid, log_invalid_subjects=True)
                )
        self._base_population = value

    @property
    def followups(self) -> Cohort:
        return self._followups

    @followups.setter
    def followups(self, value: Cohort) -> None:
        if self.run_checks:
            invalid = self._find_events_not_in_study_dates(value)
            if invalid.events.take(1):
                raise ValueError(
                    self._log_invalid_events_cohort(invalid, log_invalid_events=True)
                )
            invalid = self._find_inconsistent_start_end_ordering(value)
            if invalid.events.take(1):
                raise ValueError(
                    self._log_invalid_events_cohort(invalid, log_invalid_events=True)
                )
        self._followups = value

    @property
    def is_using_longitudinal_age_groups(self) -> bool:
        return self._is_using_longitudinal_age_groups

    @is_using_longitudinal_age_groups.setter
    def is_using_longitudinal_age_groups(self, value: bool) -> None:
        raise PermissionError(
            "is_using_longitudinal_age_groups should not be set manually."
        )

    def _bucketize_age_column(
        self, dataframe: DataFrame, input_col: str, output_col: str
    ) -> Tuple[DataFrame, int, List[str]]:
        bucketizer = Bucketizer(
            splits=self.age_groups, inputCol=input_col, outputCol=output_col
        )
        output = bucketizer.setHandleInvalid("keep").transform(dataframe)
        splits = [s for s in bucketizer.getSplits()]
        mapping = [
            "[{}, {})".format(splits[i], splits[i + 1]) for i in range(len(splits) - 1)
        ]
        n_age_groups = len(mapping)
        return output, n_age_groups, mapping

    def _find_events_not_in_followup_bounds(self, cohort: Cohort) -> Cohort:
        fups = copy(self.followups)
        fups.events = rename_df_columns(fups.events, prefix="fup_")
        events = cohort.events.join(fups.events, "patientID")
        # between returns false when col is null
        invalid_events = events.where(
            ~(
                sf.col("start").between(sf.col("fup_start"), sf.col("fup_end"))
                & sf.col("end").between(sf.col("fup_start"), sf.col("fup_end"))
            )
        )
        return Cohort(
            cohort.name + "_inconsistent_w_followup_bounds",
            "events inconsistent with followup bounds",
            invalid_events.select("patientID").distinct(),
            invalid_events,
        )

    def _find_events_not_in_study_dates(self, cohort: Cohort) -> Cohort:
        # between returns false when col is null
        invalid_events = cohort.events.where(
            ~(
                sf.col("start").between(
                    sf.lit(self.study_start), sf.lit(self.study_end)
                )
                & sf.col("end").between(
                    sf.lit(self.study_start), sf.lit(self.study_end)
                )
            )
        )
        return Cohort(
            cohort.name + "_inconsistent_w_study_dates",
            "events inconsistent with study dates",
            invalid_events.select("patientID").distinct(),
            invalid_events,
        )

    def _find_subjects_with_age_inconsistent_w_age_groups(
        self, cohort: Cohort
    ) -> Cohort:
        """Check if min and max age_groups are consistent with subjects ages."""
        if not cohort.has_subject_information():
            raise ValueError("Cohort should have subject information.")
        duplicate = copy(cohort)
        duplicate.add_age_information(self.age_reference_date)  # add starting age
        study_length = (
            np.ceil((self.study_end - self.study_start).days / 365.25)
            if self.is_using_longitudinal_age_groups
            else 0
        )
        min_starting_age = min(self.age_groups)
        max_starting_age = max(self.age_groups) - np.ceil(study_length)
        invalid_subjects = duplicate.subjects.where(
            ~sf.col("age").between(min_starting_age, max_starting_age)
        )
        return Cohort(
            cohort.name + "_inconsistent_w_ages_and_age_groups",
            "subjects inconsistent with age groups",
            invalid_subjects,
        )

    @staticmethod
    def _find_inconsistent_start_end_ordering(cohort: Cohort) -> Cohort:
        events = cohort.events
        invalid_events = events.where(sf.col("start") >= sf.col("end"))
        return Cohort(
            cohort.name + "_inconsistent_w_start_end_ordering",
            "events where start >= end dates are inconsistent",
            invalid_events.select("patientID").distinct(),
            invalid_events,
        )

    @staticmethod
    def _log_invalid_events_cohort(
        cohort: Cohort,
        log_invalid_events: bool = False,
        log_invalid_subjects: bool = False,
    ) -> str:
        cohort_name, reference = cohort.name.split("_inconsistent_w_")
        n_subjects = cohort.subjects.count()
        msg = (
            "Found {n_subjects} subjects in cohort {cohort_name} inconsistent with"
            " {reference}.\n".format(
                n_subjects=n_subjects, cohort_name=cohort_name, reference=reference
            )
        )
        if log_invalid_events:
            msg += "Showing first 10 invalid events below:\n"
            msg += cohort.events.limit(10).toPandas().to_string(index=False)
            msg += "\n"
        if log_invalid_subjects:
            msg += "Showing first 10 invalid subjects below:\n"
            msg += cohort.subjects.limit(10).toPandas().to_string(index=False)
            msg += "\n"
        return msg

    @staticmethod
    def _has_timezone(date: pytz.datetime.datetime) -> bool:
        """Check if date has timezone."""
        return date.tzinfo is not None

max_stars_repo_name: X-DataInitiative/SCALPEL-Analysis

max_issues_repo_name: X-DataInitiative/SCALPEL-Analysis

max_forks_repo_name: X-DataInitiative/SCALPEL-Analysis


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

import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np
import json
import argparse

labels = ["PinLock", "Camera", "Animation", "FatFs-uSD", "LCD-uSD", "TCP-Echo", "CoreMark", "Average"]

apps = ["PinLock", "Camera", "Animation", "FatFs-uSD", "LCD-uSD", "TCP-Echo", "CoreMark"]

app_name_mappings = {
    "PinLock": "PinLock",
    "Animation": "LCD_AnimatedPictureFromSDCard",
    "FatFs-uSD": "FatFs_uSD",
    "LCD-uSD": "LCD_PicturesFromSDCard",
    "TCP-Echo": "LwIP_TCP_Echo_Server",
    "Camera": "Camera_To_USBDisk",
    "CoreMark": "CoreMark"
}

def load_json_from_file(filename):
    with open(filename, "r") as f:
        json_to_load = json.load(f)
    return json_to_load

def get_runtime_overhead(runtime_overhead_file):
    result = load_json_from_file(runtime_overhead_file)
    runtime_overhead = []
    for app in apps:
        app_complex = app_name_mappings[app]
        runtime_overhead.append(result[app_complex]["ratio"])

    average = round(sum(runtime_overhead)*1.0/len(runtime_overhead), 2)
    runtime_overhead.append(average)

    return runtime_overhead

def get_sram_overhead(sram_overhead_file):
    results = load_json_from_file(sram_overhead_file)
    sram_overhead = []
    _sram_overhead = []
    for app in apps:
        app_complex = app_name_mappings[app]
        _sram_overhead.append(results[app_complex]["ratio"])

    average = sum(_sram_overhead) / len(_sram_overhead)
    _sram_overhead.append(average)

    for i in _sram_overhead:
        sram_overhead.append(round(i, 2))

    return sram_overhead

def get_flash_overhead(flash_overhead_file):
    results = load_json_from_file(flash_overhead_file)
    flash_overhead = []
    _flash_overhead = []
    for app in labels:
        if app == "Average":
            continue
        app_complex = app_name_mappings[app]
        _flash_overhead.append(results[app_complex]["ratio"])

    average = round(sum(_flash_overhead) / len(_flash_overhead), 2)
    _flash_overhead.append(average)

    for i in _flash_overhead:
        flash_overhead.append(round(i, 2))

    return flash_overhead

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('-d', '--cur_dir', dest='CUR_DIR', default="", help='The path of current figure directory')
    args = parser.parse_args()
    dir_name = args.CUR_DIR

    runtime_overhead_file = dir_name + "/" + "OPEC_runtime_overhead_json"
    runtime_overhead = get_runtime_overhead(runtime_overhead_file)

    flash_overhead_file = dir_name + "/" + "flash_overhead.json"
    flash_overhead = get_flash_overhead(flash_overhead_file)

    sram_overhead_file = dir_name + "/" + "sram_overhead.json"
    sram_overhead = get_sram_overhead(sram_overhead_file)

    plt.style.use('seaborn-colorblind')
    # plt.style.use('seaborn-paper')

    mpl.rcParams["font.sans-serif"] = ["Times New Roman"]
    mpl.rcParams["axes.unicode_minus"] = False
    mpl.rcParams["font.size"] = 28
    mpl.rcParams['figure.figsize'] = (24.0, 9.0)
    mpl.rcParams["font.weight"] = "bold"

    x = np.arange(len(labels))  # the label locations
    width = 0.31  # the width of the bars

    fig, ax = plt.subplots()
    rects1 = ax.bar(x - width, runtime_overhead, width, label='Runtime Overhead')
    rects2 = ax.bar(x, flash_overhead, width, label='Flash Overhead')
    rects3 = ax.bar(x + width, sram_overhead, width, label='SRAM Overhead')

    # Add some text for labels, title and custom x-axis tick labels, etc.
    ax.set_ylabel('Overhead (%)', fontweight="bold")
    ax.set_title('')
    ax.set_xticks(x)
    ax.set_xticklabels(labels)
    ax.legend(loc="upper left", prop={'size': 24})
    # ax.grid(True)

    def autolabel(rects):
        """Attach a text label above each bar in *rects*, displaying its height."""
        for rect in rects:
            height = rect.get_height()
            ax.annotate('{}'.format(height),
                        xy=(rect.get_x() + rect.get_width() / 2, height-0.1),
                        xytext=(0, 3),  # 3 points vertical offset
                        textcoords="offset points",
                        ha='center', va='bottom')

    autolabel(rects1)
    autolabel(rects2)
    autolabel(rects3)

    fig.tight_layout()

    fig.savefig("Figure9_Performance_Overhead.pdf", dpi=600, format="pdf", bbox_inches='tight')
    # plt.show()

if __name__ == "__main__":
    main()

max_stars_repo_name: XiaZhouZero/OPEC

max_issues_repo_name: XiaZhouZero/OPEC

max_forks_repo_name: XiaZhouZero/OPEC


text:
import numpy as np
import torch
import torch.nn as nn
import torchvision
from torchvision import models
from torch.autograd import Variable
import math
import pdb
import torch.nn.utils.weight_norm as weightNorm
from collections import OrderedDict
# from efficientnet_pytorch import EfficientNet
# from eff_utils import (
#     round_filters,
#     round_repeats,
#     drop_connect,
#     get_same_padding_conv2d,
#     get_model_params,
#     efficientnet_params,
#     load_pretrained_weights,
#     Swish,
#     MemoryEfficientSwish,
# )

def grl_hook(coeff):
    def fun1(grad):
        return -coeff*grad.clone()
    return fun1

def calc_coeff(iter_num, high=1.0, low=0.0, alpha=10.0, max_iter=10000.0):
    return np.float(2.0 * (high - low) / (1.0 + np.exp(-alpha*iter_num / max_iter)) - (high - low) + low)

def init_weights(m):
    classname = m.__class__.__name__
    if classname.find('Conv2d') != -1 or classname.find('ConvTranspose2d') != -1:
        nn.init.kaiming_uniform_(m.weight)
        nn.init.zeros_(m.bias)
    elif classname.find('BatchNorm') != -1:
        nn.init.normal_(m.weight, 1.0, 0.02)
        nn.init.zeros_(m.bias)
    elif classname.find('Linear') != -1:
        nn.init.xavier_normal_(m.weight)
        nn.init.zeros_(m.bias)

class RandomLayer(nn.Module):
    def __init__(self, input_dim_list=[], output_dim=1024):
        super(RandomLayer, self).__init__()
        self.input_num = len(input_dim_list)
        self.output_dim = output_dim
        self.random_matrix = [torch.randn(input_dim_list[i], output_dim) for i in range(self.input_num)]

    def forward(self, input_list):
        return_list = [torch.mm(input_list[i], self.random_matrix[i]) for i in range(self.input_num)]
        return_tensor = return_list[0] / math.pow(float(self.output_dim), 1.0/len(return_list))
        for single in return_list[1:]:
            return_tensor = torch.mul(return_tensor, single)
        return return_tensor

    def cuda(self):
        super(RandomLayer, self).cuda()
        self.random_matrix = [val.cuda() for val in self.random_matrix]

# For SVHN dataset
class DTN(nn.Module):
    def __init__(self):
        super(DTN, self).__init__()
        self.conv_params = nn.Sequential (
                nn.Conv2d(3, 64, kernel_size=5, stride=2, padding=2),
                nn.BatchNorm2d(64),
                nn.Dropout2d(0.1),
                nn.ReLU(),
                nn.Conv2d(64, 128, kernel_size=5, stride=2, padding=2),
                nn.BatchNorm2d(128),
                nn.Dropout2d(0.3),
                nn.ReLU(),
                nn.Conv2d(128, 256, kernel_size=5, stride=2, padding=2),
                nn.BatchNorm2d(256),
                nn.Dropout2d(0.5),
                nn.ReLU()
                )
        self.fc_params = nn.Sequential (
                nn.Linear(256*4*4, 512),
                nn.BatchNorm1d(512),
                nn.ReLU(),
                nn.Dropout()
                )
        self.classifier = nn.Linear(512, 10)
        self.__in_features = 512

    def forward(self, x):
        x = self.conv_params(x)
        x = x.view(x.size(0), -1)
        x = self.fc_params(x)
        y = self.classifier(x)
        return x, y

    def output_num(self):
        return self.__in_features

class LeNet(nn.Module):
    def __init__(self):
        super(LeNet, self).__init__()
        self.conv_params = nn.Sequential(
                nn.Conv2d(1, 20, kernel_size=5),
                nn.MaxPool2d(2),
                nn.ReLU(),
                nn.Conv2d(20, 50, kernel_size=5),
                nn.Dropout2d(p=0.5),
                nn.MaxPool2d(2),
                nn.ReLU(),
                )
        self.fc_params = nn.Sequential(nn.Linear(50*4*4, 500), nn.ReLU(), nn.Dropout(p=0.5))
        self.classifier = nn.Linear(500, 10)
        self.__in_features = 500

    def forward(self, x):
        x = self.conv_params(x)
        x = x.view(x.size(0), -1)
        x = self.fc_params(x)
        y = self.classifier(x)
        return x, y

    def output_num(self):
        return self.__in_features

class AdversarialNetwork(nn.Module):
  def __init__(self, in_feature, hidden_size, max_iter=10000):
    super(AdversarialNetwork, self).__init__()
    self.ad_layer1 = nn.Linear(in_feature, hidden_size)
    self.ad_layer2 = nn.Linear(hidden_size, hidden_size)
    self.ad_layer3 = nn.Linear(hidden_size, 1)
    self.relu1 = nn.ReLU()
    self.relu2 = nn.ReLU()
    self.dropout1 = nn.Dropout(0.5)
    self.dropout2 = nn.Dropout(0.5)
    self.sigmoid = nn.Sigmoid()
    self.apply(init_weights)
    self.iter_num = 0
    self.alpha = 10
    self.low = 0.0
    self.high = 1.0
    self.max_iter = max_iter

  def forward(self, x):
    if self.training:
        self.iter_num += 1
    coeff = calc_coeff(self.iter_num, self.high, self.low, self.alpha, self.max_iter)
    x = x * 1.0
    x.register_hook(grl_hook(coeff))
    x = self.ad_layer1(x)
    x = self.relu1(x)
    # x = self.dropout1(x)
    # x = self.ad_layer2(x)
    # x = self.relu2(x)
    # x = self.dropout2(x)
    y = self.ad_layer3(x)
    y = self.sigmoid(y)
    return y

  def output_num(self):
    return 1
  def get_parameters(self):
    return [{"params":self.parameters(), "lr_mult":10, 'decay_mult':2}]

class ssl_classifier(nn.Module):
  def __init__(self, in_feature, hidden_size):
    super(ssl_classifier, self).__init__()
    self.ad_layer1 = nn.Linear(in_feature, hidden_size)
    # self.ad_layer2 = nn.Linear(hidden_size, hidden_size)
    self.ad_layer3 = nn.Linear(hidden_size, 4)
    self.relu1 = nn.ReLU()
    self.relu2 = nn.ReLU()
    self.dropout1 = nn.Dropout(0.5)
    self.dropout2 = nn.Dropout(0.5)
    self.sigmoid = nn.Sigmoid()
    self.apply(init_weights)

  def forward(self, x):
    x = self.ad_layer1(x)
    x = self.relu1(x)
    x = self.dropout1(x)
    # x = self.ad_layer2(x)
    # x = self.relu2(x)
    # x = self.dropout2(x)
    y = self.ad_layer3(x)
    # y = self.sigmoid(y)
    return y

class Identity(nn.Module):

    def __init__(self, sub=1.0):
        super(Identity, self).__init__()
        self.sub=sub

    def forward(self,x):
        return x*self.sub

class AlexNet_BVLC(nn.Module):
    def __init__(self, dropout=False):
        super(AlexNet_BVLC, self).__init__()
        self.features = nn.Sequential(OrderedDict([
		 ("conv1", nn.Conv2d(3, 96, kernel_size=11, stride=4)),
		 ("relu1", nn.ReLU(inplace=True)),
		 ("pool1", nn.MaxPool2d(kernel_size=3, stride=2, ceil_mode=True)),
		 ("norm1", nn.LocalResponseNorm(5, 1.e-4, 0.75)),
		 ("conv2", nn.Conv2d(96, 256, kernel_size=5, padding=2, groups=2)),
		 ("relu2", nn.ReLU(inplace=True)),
		 ("pool2", nn.MaxPool2d(kernel_size=3, stride=2, ceil_mode=True)),
		 ("norm2", nn.LocalResponseNorm(5, 1.e-4, 0.75)),
		 ("conv3", nn.Conv2d(256, 384, kernel_size=3, padding=1)),
		 ("relu3", nn.ReLU(inplace=True)),
		 ("conv4", nn.Conv2d(384, 384, kernel_size=3, padding=1, groups=2)),
		 ("relu4", nn.ReLU(inplace=True)),
		 ("conv5", nn.Conv2d(384, 256, kernel_size=3, padding=1, groups=2)),
		 ("relu5", nn.ReLU(inplace=True)),
		 ("pool5", nn.MaxPool2d(kernel_size=3, stride=2, ceil_mode=True)),
	    ]))
        self.classifier = nn.Sequential(OrderedDict([
		 ("fc6", nn.Linear(256 * 6 * 6, 4096)),
		 ("relu6", nn.ReLU(inplace=True)),
		 ("drop6", nn.Dropout() if dropout else Identity(sub=0.5)),
		 ("fc7", nn.Linear(4096, 4096)),
		 ("relu7", nn.ReLU(inplace=True)),
		 ("drop7", nn.Dropout() if dropout else Identity(sub=0.5)),
		 # ("fc8", nn.Linear(4096, 1000))
	    ]))

class DecafBase(nn.Module):
  def __init__(self):
    super(DecafBase, self).__init__()
    model = AlexNet_BVLC()
    model_old = torch.load('/Checkpoint/liangjian/tran/icml20/alexnet_caffe.pth.tar')
    st = model.state_dict()
    model_dict = {k:v for k, v in model_old.items() if k in st.keys()}

    # model_old = torch.load('./caffe_places205.dat')['state_dict']
    # model_dict = {}
    # for k, v in model_old.items():
    #     if k.__contains__('fc'):
    #         model_dict['classifier.'+k] = v
    #     else:
    #         model_dict['features.'+k] = v
    # # pdb.set_trace()
    # model_dict = {k:v for k, v in model_dict.items() if k in st.keys()}
    st.update(model_dict)
    model.load_state_dict(st)

    self.features = model.features
    self.classifier = model.classifier
    self.in_features = 4096

  def forward(self, x):
    x = self.features(x)
    x = x.view(x.size(0), -1)
    x = self.classifier(x)
    return x

class LRN(nn.Module):
    def __init__(self, local_size=1, alpha=1.0, beta=0.75, ACROSS_CHANNELS=True):
        super(LRN, self).__init__()
        self.ACROSS_CHANNELS = ACROSS_CHANNELS
        if ACROSS_CHANNELS:
            self.average=nn.AvgPool3d(kernel_size=(local_size, 1, 1),
                    stride=1,
                    padding=(int((local_size-1.0)/2), 0, 0))
        else:
            self.average=nn.AvgPool2d(kernel_size=local_size,
                    stride=1,
                    padding=int((local_size-1.0)/2))
        self.alpha = alpha
        self.beta = beta

    def forward(self, x):
        if self.ACROSS_CHANNELS:
            div = x.pow(2).unsqueeze(1)
            div = self.average(div).squeeze(1)
            div = div.mul(self.alpha).add(1.0).pow(self.beta)
        else:
            div = x.pow(2)
            div = self.average(div)
            div = div.mul(self.alpha).add(1.0).pow(self.beta)
        x = x.div(div)
        return x

class AlexNet(nn.Module):
    def __init__(self, num_classes=1000):
        super(AlexNet, self).__init__()
        self.features = nn.Sequential(
            nn.Conv2d(3, 96, kernel_size=11, stride=4, padding=0),
            nn.ReLU(inplace=True),
            LRN(local_size=5, alpha=0.0001, beta=0.75),
            nn.MaxPool2d(kernel_size=3, stride=2),
            nn.Conv2d(96, 256, kernel_size=5, padding=2, groups=2),
            nn.ReLU(inplace=True),
            LRN(local_size=5, alpha=0.0001, beta=0.75),
            nn.MaxPool2d(kernel_size=3, stride=2),
            nn.Conv2d(256, 384, kernel_size=3, padding=1),
            nn.ReLU(inplace=True),
            nn.Conv2d(384, 384, kernel_size=3, padding=1, groups=2),
            nn.ReLU(inplace=True),
            nn.Conv2d(384, 256, kernel_size=3, padding=1, groups=2),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(kernel_size=3, stride=2),
        )
        self.classifier = nn.Sequential(
            nn.Linear(256 * 6 * 6, 4096),
            nn.ReLU(inplace=True),
            nn.Dropout(),
            nn.Linear(4096, 4096),
            nn.ReLU(inplace=True),
            nn.Dropout(),
            nn.Linear(4096, num_classes),
        )

    # def forward(self, x):
    #     x = self.features(x)
    #     x = x.view(x.size(0), 256 * 6 * 6)
    #     x = self.classifier(x)
    #     return x

def alexnet(pretrained=False, **kwargs):
    r"""AlexNet model architecture from the
    `"One weird trick..." <https://arxiv.org/abs/1404.5997>`_ paper.
    Args:
        pretrained (bool): If True, returns a model pre-trained on ImageNet
    """
    model = AlexNet(**kwargs)
    if pretrained:
        model_path = '/Checkpoint/liangjian/tran/icml20/alexnet.pth.tar'
        pretrained_model = torch.load(model_path)
        model.load_state_dict(pretrained_model['state_dict'])
    return model

class AlexBase(nn.Module):
  def __init__(self):
    super(AlexBase, self).__init__()
    model_alexnet = models.alexnet(pretrained=True)
    # model_alexnet = alexnet(pretrained=True)
    self.features = model_alexnet.features
    self.classifier = nn.Sequential()
    for i in range(6):
      self.classifier.add_module("classifier"+str(i), model_alexnet.classifier[i])
    self.in_features = model_alexnet.classifier[6].in_features

  def forward(self, x):
    x = self.features(x)
    x = x.view(x.size(0), -1)
    x = self.classifier(x)
    return x

vgg_dict = {"vgg11":models.vgg11, "vgg13":models.vgg13, "vgg16":models.vgg16, "vgg19":models.vgg19,
"vgg11bn":models.vgg11_bn, "vgg13bn":models.vgg13_bn, "vgg16bn":models.vgg16_bn, "vgg19bn":models.vgg19_bn}
class VGGBase(nn.Module):
  def __init__(self, vgg_name):
    super(VGGBase, self).__init__()
    model_vgg = vgg_dict[vgg_name](pretrained=True)
    self.features = model_vgg.features
    self.classifier = nn.Sequential()
    for i in range(6):
        self.classifier.add_module("classifier"+str(i), model_vgg.classifier[i])
    self.in_features = model_vgg.classifier[6].in_features

  def forward(self, x):
    x = self.features(x)
    x = x.view(x.size(0), -1)
    x = self.classifier(x)
    return x

res_dict = {"resnet18":models.resnet18, "resnet34":models.resnet34, "resnet50":models.resnet50,
"resnet101":models.resnet101, "resnet152":models.resnet152, "resnext50":models.resnext50_32x4d, "resnext101":models.resnext101_32x8d}

class ResBase(nn.Module):
    def __init__(self, res_name):
        super(ResBase, self).__init__()
        model_resnet = res_dict[res_name](pretrained=True)
        self.conv1 = model_resnet.conv1
        self.bn1 = model_resnet.bn1
        self.relu = model_resnet.relu
        self.maxpool = model_resnet.maxpool
        self.layer1 = model_resnet.layer1
        self.layer2 = model_resnet.layer2
        self.layer3 = model_resnet.layer3
        self.layer4 = model_resnet.layer4
        self.avgpool = model_resnet.avgpool
        self.in_features = model_resnet.fc.in_features

    def forward(self, x):
        x = self.conv1(x)
        x = self.bn1(x)
        x = self.relu(x)
        x = self.maxpool(x)
        x = self.layer1(x)
        x = self.layer2(x)
        x = self.layer3(x)
        x = self.layer4(x)
        x = self.avgpool(x)
        x = x.view(x.size(0), -1)
        return x

class ResSSLBase(nn.Module):
    def __init__(self):
        super(ResSSLBase, self).__init__()

        model_resnet = models.resnet50()
        # model_old = torch.load('in22k_jigsaw.dat')['state_dict']
        model_old = torch.load('/Checkpoint/liangjian/tran/icml20/places205.dat')['state_dict']
        st = model_resnet.state_dict()
        model_dict = {k:v for k, v in model_old.items() if k in st.keys()}
        st.update(model_dict)
        model_resnet.load_state_dict(st)

        # model_resnet = res_dict[res_name](pretrained=True)
        self.conv1 = model_resnet.conv1
        self.bn1 = model_resnet.bn1
        self.relu = model_resnet.relu
        self.maxpool = model_resnet.maxpool
        self.layer1 = model_resnet.layer1
        self.layer2 = model_resnet.layer2
        self.layer3 = model_resnet.layer3
        self.layer4 = model_resnet.layer4
        self.avgpool = model_resnet.avgpool
        self.in_features = model_resnet.fc.in_features

    def forward(self, x):
        x = self.conv1(x)
        x = self.bn1(x)
        x = self.relu(x)
        x = self.maxpool(x)
        x = self.layer1(x)
        x = self.layer2(x)
        x = self.layer3(x)
        x = self.layer4(x)
        x = self.avgpool(x)
        x = x.view(x.size(0), -1)
        return x

class EfficientBase(nn.Module):
    def __init__(self, eff_name='efficientnet-b0'):
        super(EfficientBase, self).__init__()
        model = EfficientNet.from_pretrained(eff_name)
        # pdb.set_trace()
        # self.extract_features = model.extract_features
        self._swish = MemoryEfficientSwish()
        self._conv_stem = model._conv_stem
        self._bn0 = model._bn0
        self._blocks = model._blocks
        self._global_params = model._global_params
        self._bn1 = model._bn1
        self._conv_head = model._conv_head

        self._avg_pooling = model._avg_pooling
        self._dropout = model._dropout
        self.in_features = model._fc.in_features

    def forward(self, x):
        x = self.extract_features(x)
        x = self._avg_pooling(x)
        x = x.view(x.size(0), -1)
        x = self._dropout(x)
        return x

    def extract_features(self, inputs):
        """ Returns output of the final convolution layer """
        # Stem
        x = self._swish(self._bn0(self._conv_stem(inputs)))

        # Blocks
        for idx, block in enumerate(self._blocks):
            drop_connect_rate = self._global_params.drop_connect_rate
            if drop_connect_rate:
                drop_connect_rate *= float(idx) / len(self._blocks)
            x = block(x, drop_connect_rate=drop_connect_rate)
        # Head
        x = self._swish(self._bn1(self._conv_head(x)))
        return x

class feat_bootleneck(nn.Module):
    def __init__(self, feature_dim, bottleneck_dim=256, type="ori"):
        super(feat_bootleneck, self).__init__()
        self.bn = nn.BatchNorm1d(bottleneck_dim, affine=True)
        self.relu = nn.ReLU(inplace=True)
        self.dropout = nn.Dropout(p=0.5)
        self.bottleneck = nn.Linear(feature_dim, bottleneck_dim)
        self.bottleneck.apply(init_weights)
        self.type = type

    def forward(self, x):
        x = self.bottleneck(x)
        if self.type == "bn" or self.type == "bn_relu" or self.type == "bn_relu_drop":
            x = self.bn(x)
        if self.type == "bn_relu" or self.type == "bn_relu_drop":
            x = self.relu(x)
        if self.type == "bn_relu_drop":
            x = self.dropout(x)
        return x

class feat_classifier(nn.Module):
    def __init__(self, class_num, bottleneck_dim=256, type="linear"):
        super(feat_classifier, self).__init__()
        self.type = type
        if type == 'wn':
            self.fc = weightNorm(nn.Linear(bottleneck_dim, class_num), name="weight")
            self.fc.apply(init_weights)
        elif type == 'linear':
            self.fc = nn.Linear(bottleneck_dim, class_num)
            self.fc.apply(init_weights)
        else:
            self.fc = nn.Linear(bottleneck_dim, class_num, bias=False)
            nn.init.xavier_normal_(self.fc.weight)

    def forward(self, x):
        if not self.type in {'wn', 'linear'}:
            w = self.fc.weight
            w = torch.nn.functional.normalize(w, dim=1, p=2)

            x = torch.nn.functional.normalize(x, dim=1, p=2)
            x = torch.nn.functional.linear(x, w)
        else:
            x = self.fc(x)
        return x

class feat_classifier2(nn.Module):
    def __init__(self, class_num, bottleneck_dim=256):
        super(feat_classifier2, self).__init__()
        self.fc1 = nn.Linear(bottleneck_dim, bottleneck_dim//2)
        self.fc1.apply(init_weights)
        self.fc2 = nn.Linear(bottleneck_dim//2, class_num)
        self.fc2.apply(init_weights)

        self.relu = nn.ReLU(inplace=True)

    def forward(self, x):
        x = self.fc1(x)
        x = self.relu(x)
        x = self.fc2(x)
        return x

# class feat_classifier_two(nn.Module):
#     def __init__(self, class_num, input_dim, bottleneck_dim=256):
#         super(feat_classifier_two, self).__init__()
#         self.type = type
#         self.fc0 = nn.Linear(input_dim, bottleneck_dim)
#         self.fc0.apply(init_weights)
#         self.fc1 = nn.Linear(bottleneck_dim, class_num)
#         self.fc1.apply(init_weights)

#     def forward(self, x):
#         x = self.fc0(x)
#         x = self.fc1(x)
#         return x

class feat_classifier_twin(nn.Module):
    def __init__(self, class_num, bottleneck_dim=256, type="linear"):
        super(feat_classifier_twin, self).__init__()
        self.type = type
        # if type == 'wn':
        #     self.fc1 = weightNorm(nn.Linear(bottleneck_dim, class_num), name="weight")
        #     self.fc2 = weightNorm(nn.Linear(bottleneck_dim, class_num), name="weight")
        #     self.fc1.apply(init_weights)
        #     self.fc2.apply(init_weights)
        # else:
        #     self.fc1 = nn.Linear(bottleneck_dim, class_num)
        #     self.fc2 = nn.Linear(bottleneck_dim, class_num)
        #     self.fc1.apply(init_weights)
        #     self.fc2.apply(init_weights)

        # self.fc0_a = nn.Linear(bottleneck_dim, bottleneck_dim)
        # self.fc0_b = nn.Linear(bottleneck_dim, bottleneck_dim)

        # self.relu_a = nn.ReLU(inplace=True)
        # self.relu_b = nn.ReLU(inplace=True)

        # self.dropout_a = nn.Dropout(0.5)
        # self.dropout_b = nn.Dropout(0.5)

        if type == 'wn':
            self.fc_a = weightNorm(nn.Linear(bottleneck_dim, class_num), name="weight")
            self.fc_b = weightNorm(nn.Linear(bottleneck_dim, class_num), name="weight")
        else:
            self.fc_a = nn.Linear(bottleneck_dim, class_num)
            self.fc_b = nn.Linear(bottleneck_dim, class_num)

        self.fc_a.apply(init_weights)
        self.fc_b.apply(init_weights)

    def forward(self, x):
        # x1 = self.fc0_a(x)
        # x1 = self.relu_a(self.dropout_a(x1))
        f1 = self.fc_a(x)
        # x2 = self.fc0_b(x)
        # x2 = self.relu_b(self.dropout_b(x2))
        f2 = self.fc_b(x)
        return f1, f2

class rot_classifier(nn.Module):
    def __init__(self, class_num, feature_dim=2048, bottleneck_dim=256, type="linear"):
        super(rot_classifier, self).__init__()
        self.type = type
        self.fc0 = nn.Linear(feature_dim, bottleneck_dim)
        self.fc0.apply(init_weights)
        self.relu = nn.ReLU(inplace=True)
        self.fc = nn.Linear(bottleneck_dim, class_num)
        self.fc.apply(init_weights)

    def forward(self, x):
        x = self.fc0(x)
        # x = self.relu(x)
        x = self.fc(x)
        return x

class feat_classifier_nonlinear(nn.Module):
    def __init__(self, class_num, bottleneck_dim=256, type="linear"):
        super(feat_classifier_nonlinear, self).__init__()
        self.type = type
        self.fc0 = nn.Linear(bottleneck_dim, bottleneck_dim)
        self.fc0.apply(init_weights)
        # self.relu = nn.ReLU(inplace=True)
        if type == 'wn':
            self.fc = weightNorm(nn.Linear(bottleneck_dim, class_num), name="weight")
            self.fc.apply(init_weights)
        elif type == 'linear':
            self.fc = nn.Linear(bottleneck_dim, class_num)
            self.fc.apply(init_weights)
        else:
            self.fc = nn.Linear(bottleneck_dim, class_num, bias=False)
            nn.init.xavier_normal_(self.fc.weight)

    def forward(self, x):
        x = self.fc0(x)
        # x = self.relu(x)
        if not self.type in {'wn', 'linear'}:
            w = self.fc.weight
            w = torch.nn.functional.normalize(w, dim=1, p=2)

            x = torch.nn.functional.normalize(x, dim=1, p=2)
            x = torch.nn.functional.linear(x, w)
        else:
            x = self.fc(x)
        return x

class feat_cosine_classifier(nn.Module):
    def __init__(self, class_num, bottleneck_dim=256, type="cosine"):
        super(feat_cosine_classifier, self).__init__()
        self.type = type
        self.fc = nn.Linear(bottleneck_dim, class_num, bias=False)
        nn.init.xavier_normal_(self.fc.weight)

    def forward(self, x):
        if self.type in {'cosine'}:
            w = self.fc.weight
            w = torch.nn.functional.normalize(w, dim=1, p=2)
            x = torch.nn.functional.normalize(x, dim=1, p=2)
            # pdb.set_trace()
            x = torch.nn.functional.linear(x, w)
        else:
            x = self.fc(x)
        return x

class Res50(nn.Module):
    def __init__(self):
        super(Res50, self).__init__()
        model_resnet = models.resnet50(pretrained=True)
        self.conv1 = model_resnet.conv1
        self.bn1 = model_resnet.bn1
        self.relu = model_resnet.relu
        self.maxpool = model_resnet.maxpool
        self.layer1 = model_resnet.layer1
        self.layer2 = model_resnet.layer2
        self.layer3 = model_resnet.layer3
        self.layer4 = model_resnet.layer4
        self.avgpool = model_resnet.avgpool
        self.in_features = model_resnet.fc.in_features
        self.fc = model_resnet.fc

    def forward(self, x):
        x = self.conv1(x)
        x = self.bn1(x)
        x = self.relu(x)
        x = self.maxpool(x)
        x = self.layer1(x)
        x = self.layer2(x)
        x = self.layer3(x)
        x = self.layer4(x)
        x = self.avgpool(x)
        x = x.view(x.size(0), -1)
        y = self.fc(x)
        return x, y

class DTNBase(nn.Module):
    def __init__(self):
        super(DTNBase, self).__init__()
        self.conv_params = nn.Sequential(
                nn.Conv2d(3, 64, kernel_size=5, stride=2, padding=2),
                nn.BatchNorm2d(64),
                nn.Dropout2d(0.1),
                nn.ReLU(),
                nn.Conv2d(64, 128, kernel_size=5, stride=2, padding=2),
                nn.BatchNorm2d(128),
                nn.Dropout2d(0.3),
                nn.ReLU(),
                nn.Conv2d(128, 256, kernel_size=5, stride=2, padding=2),
                nn.BatchNorm2d(256),
                nn.Dropout2d(0.5),
                nn.ReLU()
                )
        self.in_features = 256*4*4

    def forward(self, x):
        x = self.conv_params(x)
        x = x.view(x.size(0), -1)
        return x

class LeNetBase(nn.Module):
    def __init__(self):
        super(LeNetBase, self).__init__()
        self.conv_params = nn.Sequential(
                nn.Conv2d(1, 20, kernel_size=5),
                nn.MaxPool2d(2),
                nn.ReLU(),
                nn.Conv2d(20, 50, kernel_size=5),
                nn.Dropout2d(p=0.5),
                nn.MaxPool2d(2),
                nn.ReLU(),
                )
        self.in_features = 50*4*4

    def forward(self, x):
        x = self.conv_params(x)
        x = x.view(x.size(0), -1)
        return x

max_stars_repo_name: tim-learn/SHOT-plus

max_issues_repo_name: tim-learn/SHOT-plus

max_forks_repo_name: tim-learn/SHOT-plus


text:
import math

class Pos:
  def __init__(self, x, y, z):
    self.x = x
    self.y = y
    self.z = z

def multiplicationLineColumn(line,column):
    try:
        sizeLine=len(line)
        sizeColumn=len(column)
        if(sizeLine!=sizeColumn):
            raise ValueError("Exception")
        res = sum([line[i] * column[i] for i in range(sizeLine)])
        return res
    except ValueError:
        print("sould have the same len line & column")

def  getColumn(matrix,numColumn):
    size=len(matrix)
    column= [matrix[i][numColumn] for i in range(size)]
    return column

def getLine(matrix,numLine):
    line = matrix[numLine]
    return line

def mdot(A, B):
    result_matrix=[]
    for i in range(len(A)):
        result_matrix.append([])
        for j in range(len(B)):
            result_matrix[i].append(multiplicationLineColumn(getLine(A,i),getColumn(B,j)))
    return result_matrix

def py_euler_to_rotmat(yaw, pitch, roll):
    Rz_yaw = [
        [math.cos(yaw), -math.sin(yaw), 0],
        [math.sin(yaw),  math.cos(yaw), 0],
        [            0,              0, 1]]
    Ry_pitch = [
        [ math.cos(pitch), 0, math.sin(pitch)],
        [               0, 1,               0],
        [-math.sin(pitch), 0, math.cos(pitch)]]
    Rx_roll = [
        [1,              0,               0],
        [0, math.cos(roll), -math.sin(roll)],
        [0, math.sin(roll),  math.cos(roll)]]
    # R = RzRyRx
    rotMat = mdot(Rz_yaw, mdot(Ry_pitch, Rx_roll)) #Original
    #rotMat = mdot(Rx_roll, mdot(Ry_pitch, Rz_yaw))#test
    #rotMat = mdot(Rx_roll, mdot(Rz_yaw, Ry_pitch))#test
    #rotMat = mdot(mdot(Rz_yaw, Ry_pitch), Rx_roll)#test 13 dec
    return rotMat

def euler2quat(ai, aj, ak, axes='sxyz'):
    """Return `quaternion` from Euler angles and axis sequence `axes`
    Parameters
    ----------
    ai : float
        First rotation angle (according to `axes`).
    aj : float
        Second rotation angle (according to `axes`).
    ak : float
        Third rotation angle (according to `axes`).
    axes : str, optional
        Axis specification; one of 24 axis sequences as string or encoded
        tuple - e.g. ``sxyz`` (the default).
    Returns
    -------
    quat : array shape (4,)
       Quaternion in w, x, y z (real, then vector) format
    Examples
    --------
    >>> q = euler2quat(1, 2, 3, 'ryxz')
    >>> np.allclose(q, [0.435953, 0.310622, -0.718287, 0.444435])
    True
    """
    #axes: syzx
    firstaxis = 1
    parity = 0
    repetition = 0
    frame = 0
    _NEXT_AXIS = [1, 2, 0, 1]

    i = firstaxis + 1
    j = _NEXT_AXIS[i+parity-1] + 1
    k = _NEXT_AXIS[i-parity] + 1

    if frame:
        ai, ak = ak, ai
    if parity:
        aj = -aj

    ai /= 2.0
    aj /= 2.0
    ak /= 2.0
    ci = math.cos(ai)
    si = math.sin(ai)
    cj = math.cos(aj)
    sj = math.sin(aj)
    ck = math.cos(ak)
    sk = math.sin(ak)
    cc = ci*ck
    cs = ci*sk
    sc = si*ck
    ss = si*sk

    q = [0,0,0,0]
    if repetition:
        q[0] = cj*(cc - ss)
        q[i] = cj*(cs + sc)
        q[j] = sj*(cc + ss)
        q[k] = sj*(cs - sc)
    else:
        q[0] = cj*cc + sj*ss
        q[i] = cj*sc - sj*cs
        q[j] = cj*ss + sj*cc
        q[k] = cj*cs - sj*sc
    if parity:
        q[j] *= -1.0

    return q

def qmult(q1, q2):
    ''' Multiply two quaternions
    Parameters
    ----------
    q1 : 4 element sequence
    q2 : 4 element sequence
    Returns
    -------
    q12 : shape (4,) array
    Notes
    -----
    See : http://en.wikipedia.org/wiki/Quaternions#Hamilton_product
    '''
    w1, x1, y1, z1 = q1
    w2, x2, y2, z2 = q2
    w = w1*w2 - x1*x2 - y1*y2 - z1*z2
    x = w1*x2 + x1*w2 + y1*z2 - z1*y2
    y = w1*y2 + y1*w2 + z1*x2 - x1*z2
    z = w1*z2 + z1*w2 + x1*y2 - y1*x2
    return [w, x, y, z]

def quat2mat(q):
    ''' Calculate rotation matrix corresponding to quaternion
    Parameters
    ----------
    q : 4 element array-like
    Returns
    -------
    M : (3,3) array
      Rotation matrix corresponding to input quaternion *q*
    Notes
    -----
    Rotation matrix applies to column vectors, and is applied to the
    left of coordinate vectors.  The algorithm here allows quaternions that
    have not been normalized.
    References
    ----------
    Algorithm from http://en.wikipedia.org/wiki/Rotation_matrix#Quaternion
    Examples
    --------
    >>> import numpy as np
    >>> M = quat2mat([1, 0, 0, 0]) # Identity quaternion
    >>> np.allclose(M, np.eye(3))
    True
    >>> M = quat2mat([0, 1, 0, 0]) # 180 degree rotn around axis 0
    >>> np.allclose(M, np.diag([1, -1, -1]))
    True
    '''
    w, x, y, z = q
    Nq = w*w + x*x + y*y + z*z
    if Nq < 2.220446049250313e-16:
        return([[1, 0, 0],[0, 1, 0],[0, 0, 1]])
    s = 2.0/Nq
    X = x*s
    Y = y*s
    Z = z*s
    wX = w*X; wY = w*Y; wZ = w*Z
    xX = x*X; xY = x*Y; xZ = x*Z
    yY = y*Y; yZ = y*Z; zZ = z*Z
    return(
           [[ 1.0-(yY+zZ), xY-wZ, xZ+wY ],
            [ xY+wZ, 1.0-(xX+zZ), yZ-wX ],
            [ xZ-wY, yZ+wX, 1.0-(xX+yY) ]])

#tpy_rotmat_to_euler this might be wrong since i verifed it against a version of py_euler_to_rotmat that seams to malfunction at certain times(ie some type of gimbal lock)
def tpy_rotmat_to_euler(M):
    _EPS4=1e-6

    #axes: syzx
    firstaxis = 1
    parity = 0
    repetition = 0
    frame = 0
    _NEXT_AXIS = [1, 2, 0, 1]

    i = firstaxis
    j = _NEXT_AXIS[i+parity]
    k = _NEXT_AXIS[i-parity+1]

    if repetition:
        sy = math.sqrt(M[i][j]*M[i][j] + M[i][k]*M[i][k])
        if sy > _EPS4:
            ax = math.atan2( M[i][j],  M[i][k])
            ay = math.atan2( sy,       M[i][i])
            az = math.atan2( M[j][i], -M[k][i])
        else:
            ax = math.atan2(-M[j][k],  M[j][j])
            ay = math.atan2( sy,       M[i][i])
            az = 0.0
    else:
        cy = math.sqrt(M[i][i]*M[i][i] + M[j][i]*M[j][i])
        if cy > _EPS4:
            ax = math.atan2( M[k][j],  M[k][k])
            ay = math.atan2(-M[k][i],  cy)
            az = math.atan2( M[j][i],  M[i][i])
        else:
            ax = math.atan2(-M[j][k],  M[j][j])
            ay = math.atan2(-M[k][i],  cy)
            az = 0.0
    if parity:
        ax, ay, az = -ax, -ay, -az
    if frame:
        ax, az = az, ax
    return Pos(math.degrees(ay), math.degrees(ax), math.degrees(az))#switch the axles here,is it right? Dont know, if it wors, it works...???

def py_rotmat_to_euler(R):

    sy = math.sqrt(R[0][0] * R[0][0] +  R[1][0] * R[1][0])
    singular = sy < 1e-6
    if not singular:
        x = math.atan2(R[2][1] , R[2][2])
        y = math.atan2(-R[2][0], sy)
        z = math.atan2(R[1][0], R[0][0])
    else:
        x = math.atan2(-R[1][2], R[1][1])
        y = math.atan2(-R[2][0], sy)
        z = 0
    return Pos(math.degrees(x),math.degrees(y),math.degrees(z))


text:
"""
Kernel functions for point configurations.

The functions below are not computing a kernel matrix; rather, each  computes
  a single evaluation of the kernel function for two point-sets.

Only the MMD-based kernel was used in the paper.
"""
from __future__ import division
from scipy.stats import wasserstein_distance
from util import *
import ot  # Optimal transport

def euclidean_kernel(X, Y, l=1.):
    """
    Kernel based on squared Euclidean distances.

    Args:
        X, Y: 2D-arrays, each containing a collection of points.
        l: float, length-scale.
    """
    if X.size == 0 and Y.size == 0:
        return 1.
    elif X.size == 0 or Y.size == 0:
        return 0.

    dists = cdist(X, Y, metric="sqeuclidean")
    return np.exp(-np.mean(dists) / l)

def emd_kernel(X, Y, l=1.):
    """
    Kernel based on the earth-mover's distance.
    This is not a positive-definite beyond one dimension!

    Args:
        X, Y: 2D-arrays, each containing a collection of points.
        l: float, length-scale.
    """
    if X.size == 0 and Y.size == 0:
        return 1.
    elif X.size == 0 or Y.size == 0:
        return 0.

    M = ot.dist(X, Y)  # Equivalent to cdist with metric sqeuclidean
    emd = ot.emd2([], [], M)  # Square of EMD; assumes equal weights

    return np.exp(-np.sqrt(emd) / l)

def mmd_kernel(X, Y, rbf_h=1., l=1., use_ustat=False):
    """
    MMD-based kernel ('M-kernel') defined in Eq.(15) of the paper.

    Uses a Gaussian RBF kernel on the ground space.
    """
    if X.size == 0 and Y.size == 0:
        return 1.
    elif X.size == 0 or Y.size == 0:
        return 0.

    # Compute ground kernel
    dists = squareform(pdist(np.vstack([X, Y]), metric="sqeuclidean"))

    K = np.exp(-dists / rbf_h)

    m, n = len(X), len(Y)  # Number of samples
    assert_shape(K, (m+n, m+n))
    kxx = K[:m, :m]
    kyy = K[m:, m:]
    kxy = K[:m, m:]
    assert is_symmetric(kxx)
    assert is_symmetric(kyy)

    if use_ustat:  # U-statistic estimate
        term_xx = (kxx.sum() - np.diag(kxx).sum()) / (m*(m-1))
        term_yy = (kyy.sum() - np.diag(kyy).sum()) / (n*(n-1))
        term_xy = kxy.sum() / (m*n)
    else:  # V-statistic estimate
        term_xx = kxx.sum() / (m**2)
        term_yy = kyy.sum() / (n**2)
        term_xy = kxy.sum() / (m*n)

    mmd = term_xx + term_yy - 2*term_xy

    return np.exp(-mmd / l)

max_stars_repo_name: jiaseny/sp-gof

max_issues_repo_name: jiaseny/sp-gof

max_forks_repo_name: jiaseny/sp-gof

reason: from scipy


text:
"""
Compile GPU Inference
=====================
**Author**: `Yuwei Hu <https://huyuwei.github.io/>`_

This is an example of using NNVM to compile MobileNet/ResNet model and deploy its inference on GPU.

To begin with, we import nnvm(for compilation) and TVM(for deployment).
"""
import tvm
import numpy as np
from tvm.contrib import nvcc, graph_runtime
import nnvm.compiler
import nnvm.testing

######################################################################
# Register the NVCC Compiler Option
# ---------------------------------
# NNVM optimizes the graph and relies on TVM to generate fast GPU code.
# To get the maximum performance, we need to enable nvcc's compiler hook.
# This usually gives better performance than nvrtc mode.

@tvm.register_func("tvm_callback_cuda_compile", override=True)
def tvm_callback_cuda_compile(code):
    ptx = nvcc.compile_cuda(code, target="ptx")
    return ptx

######################################################################
# Prepare the Benchmark
# ---------------------
# We construct a standard imagenet inference benchmark.
# NNVM needs two things to compile a deep learning model:
#
# - net: the graph representation of the computation
# - params: a dictionary of str to parameters
#
# We use nnvm's testing utility to produce the model description and random parameters
# so that the example does not depend on a specific front-end framework.
#
# .. note::
#
#   In a typical workflow, we can get this pair from :any:`nnvm.frontend`
#
target = "cuda"
ctx = tvm.gpu(0)
batch_size = 1
num_classes = 1000
image_shape = (3, 224, 224)
data_shape = (batch_size,) + image_shape
out_shape = (batch_size, num_classes)
# To use ResNet to do inference, run the following instead
#net, params = nnvm.testing.resnet.get_workload(
#    batch_size=1, image_shape=image_shape)
net, params = nnvm.testing.mobilenet.get_workload(
    batch_size=1, image_shape=image_shape)

######################################################################
# Compile the Graph
# -----------------
# To compile the graph, we call the build function with the graph
# configuration and parameters.
# When parameters are provided, NNVM will pre-compute certain part of the graph if possible (e.g. simplify batch normalization to scale shift),
# and return the updated parameters.

graph, lib, params = nnvm.compiler.build(
    net, target, shape={"data": data_shape}, params=params)

######################################################################
# Run the Compiled Module
# -----------------------
#
# To deploy the module, we call :any:`tvm.contrib.graph_runtime.create` passing in the graph, the lib, and context.
# Thanks to TVM, we can deploy the compiled module to many platforms and languages.
# The deployment module is designed to contain minimum dependencies.
# This example runs on the same machine.
#
# Note that the code below no longer depends on NNVM, and only relies TVM's runtime to run(deploy).
data = np.random.uniform(-1, 1, size=data_shape).astype("float32")
module = graph_runtime.create(graph, lib, ctx)
# set input
module.set_input(**params)
module.set_input("data", data)
# run
module.run()
# get output
out = module.get_output(0, tvm.nd.empty(out_shape))
# convert to numpy
out.asnumpy()

max_stars_repo_name: Aimledge/tvm

max_issues_repo_name: Aimledge/tvm

max_forks_repo_name: Aimledge/tvm


text:
# -*- coding: utf-8 -*-
"""
Defines unit tests for :mod:`colour.volume.mesh` module.
"""

import numpy as np
import unittest
from itertools import permutations

from colour.volume import is_within_mesh_volume
from colour.utilities import ignore_numpy_errors

__author__ = 'Colour Developers'
__copyright__ = 'Copyright (C) 2013-2021 - Colour Developers'
__license__ = 'New BSD License - https://opensource.org/licenses/BSD-3-Clause'
__maintainer__ = 'Colour Developers'
__email__ = 'colour-developers@colour-science.org'
__status__ = 'Production'

__all__ = ['TestIsWithinMeshVolume']

class TestIsWithinMeshVolume(unittest.TestCase):
    """
    Defines :func:`colour.volume.mesh.is_within_mesh_volume` definition unit
    tests methods.
    """

    def setUp(self):
        """
        Initialises common tests attributes.
        """

        self._mesh = np.array([
            [-1.0, -1.0, 1.0],
            [1.0, -1.0, 1.0],
            [1.0, -1.0, -1.0],
            [-1.0, -1.0, -1.0],
            [0.0, 1.0, 0.0],
        ])

    def test_is_within_mesh_volume(self):
        """
        Tests :func:`colour.volume.mesh.is_within_mesh_volume` definition.
        """

        self.assertTrue(
            is_within_mesh_volume(
                np.array([0.0005, 0.0031, 0.0010]), self._mesh))

        self.assertFalse(
            is_within_mesh_volume(
                np.array([0.3205, 0.4131, 0.5100]), self._mesh))

        self.assertTrue(
            is_within_mesh_volume(
                np.array([0.0025, 0.0088, 0.0340]), self._mesh))

        self.assertFalse(
            is_within_mesh_volume(
                np.array([0.4325, 0.3788, 0.1034]), self._mesh))

    def test_n_dimensional_is_within_mesh_volume(self):
        """
        Tests :func:`colour.volume.mesh.is_within_mesh_volume` definition
        n-dimensional arrays support.
        """

        a = np.array([0.0005, 0.0031, 0.0010])
        b = is_within_mesh_volume(a, self._mesh)

        a = np.tile(a, (6, 1))
        b = np.tile(b, 6)
        np.testing.assert_almost_equal(is_within_mesh_volume(a, self._mesh), b)

        a = np.reshape(a, (2, 3, 3))
        b = np.reshape(b, (2, 3))
        np.testing.assert_almost_equal(is_within_mesh_volume(a, self._mesh), b)

    @ignore_numpy_errors
    def test_nan_is_within_mesh_volume(self):
        """
        Tests :func:`colour.volume.mesh.is_within_mesh_volume` definition nan
        support.
        """

        cases = [-1.0, 0.0, 1.0, -np.inf, np.inf, np.nan]
        cases = set(permutations(cases * 3, r=3))
        for case in cases:
            is_within_mesh_volume(case, self._mesh)

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

max_stars_repo_name: gutenzwerg/colour

max_issues_repo_name: jedypod/colour

max_forks_repo_name: jedypod/colour