text:
import os

import cv2
import pdb
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.backends.cudnn as cudnn
from collections import Counter

from lib.utils.tools.logger import Logger as Log
from .base import _BaseEvaluator
from . import tasks

def _parse_output_spec(spec):
    """
    Parse string like "mask, _, dir, ..., seg" into indices mapping
    {
        "mask": 0,
        "dir": 2,
        "seg": -1
    }
    """
    spec = [x.strip() for x in spec.split(',')]
    existing_task_names = set(tasks.task_mapping)

    # `spec` should not have invalid keys other than in `existing_task_names`
    assert set(spec) - ({'...', '_'} | existing_task_names) == set()
    # `spec` should have at least one key in `existing_task_names`
    assert set(spec) & existing_task_names != set()

    counter = Counter(spec)
    for task in tasks.task_mapping.values():
        task.validate_output_spec(spec, counter)
    assert counter['...'] <= 1

    length = len(spec)
    output_indices = {}
    negative_index = False
    for idx, name in enumerate(spec):
        if name not in ['_', '...']:
            index = idx - length if negative_index else idx
            output_indices[name] = index
        elif name == '...':
            negative_index = True

    return output_indices

class StandardEvaluator(_BaseEvaluator):

    def _output_spec(self):
        if self.configer.conditions.pred_dt_offset:
            default_spec = 'mask, dir'
        elif self.configer.conditions.pred_ml_dt_offset:
            default_spec = 'mask, ml_dir'
        else:
            default_spec = '..., seg'

        return os.environ.get('output_spec', default_spec)

    def _init_running_scores(self):
        self.output_indices = _parse_output_spec(self._output_spec())

        self.running_scores = {}
        for task in tasks.task_mapping.values():
            rss, main_key, metric = task.running_score(self.output_indices, self.configer)
            if rss is None:
                continue
            self.running_scores.update(rss)
            self.save_net_main_key = main_key
            self.save_net_metric = metric

    def update_score(self, outputs, metas):
        if isinstance(outputs, torch.Tensor):
            outputs = [outputs]

        # for i in range(len(outputs)):
        for i in range(1):
            ori_img_size = metas[i]['ori_img_size']
            border_size = metas[i]['border_size']

            outputs_numpy = {}
            for name, idx in self.output_indices.items():
                item = outputs[idx].permute(0, 2, 3, 1)
                if self.configer.get('dataset') == 'celeba':
                    # the celeba image is of size 1024x1024
                    item = cv2.resize(
                        item[i, :border_size[1], :border_size[0]].cpu().numpy(),
                        tuple(x // 2 for x in ori_img_size), interpolation=cv2.INTER_CUBIC
                    )
                else:
                    item = cv2.resize(
                        item[i, :border_size[1], :border_size[0]].cpu().numpy(),
                        tuple(ori_img_size), interpolation=cv2.INTER_CUBIC
                    )
                outputs_numpy[name] = item

            for name in outputs_numpy:
                tasks.task_mapping[name].eval(
                    outputs_numpy, metas[i], self.running_scores
                )


text:
def test_seed():
    import numpy as np
    from sklearn.datasets import load_iris

    from tsne import bh_sne

    iris = load_iris()

    X = iris.data
    # y = iris.target

    t1 = bh_sne(X, random_state=np.random.RandomState(0), copy_data=True)
    t2 = bh_sne(X, random_state=np.random.RandomState(0), copy_data=True)

    assert t1.shape[0] == 150
    assert t1.shape[1] == 2
    assert np.all(t1 == t2)

ext: py

max_stars_repo_name: danielfrg/tsne

max_issues_repo_name: danielfrg/tsne

max_forks_repo_name: danielfrg/tsne

reason: import numpy


text:
import numpy

from chainer import cuda
from chainer import function
from chainer.utils import type_check

class MeanSquaredError(function.Function):

    """Mean squared error (a.k.a. Euclidean loss) function."""

    def check_type_forward(self, in_types):
        type_check.expect(in_types.size() == 2)
        type_check.expect(
            in_types[0].dtype == numpy.float32,
            in_types[1].dtype == numpy.float32,
            in_types[0].shape == in_types[1].shape
        )

    def forward_cpu(self, inputs):
        x0, x1 = inputs
        self.diff = x0 - x1
        diff = self.diff.ravel()
        return numpy.array(diff.dot(diff) / diff.size, numpy.float32),

    def forward_gpu(self, inputs):
        x0, x1 = inputs
        ret = cuda.reduce(
            'const float* x0, const float* x1',
            '(x0[i] - x1[i]) * (x0[i] - x1[i])',
            'a+b', '0', 'mse_fwd', numpy.float32)(x0, x1)
        ret /= x0.size
        return ret,

    def backward_cpu(self, inputs, gy):
        coeff = 2. * gy[0] / self.diff.size
        gx0 = coeff * self.diff
        return gx0, -gx0

    def backward_gpu(self, inputs, gy):
        x0, x1 = inputs
        gx0 = cuda.empty_like(x0)
        gx1 = cuda.empty_like(x1)
        coeff = gy[0] * (2. / x0.size)
        cuda.elementwise(
            '''float* gx0, float* gx1, const float* x0, const float* x1,
               const float* coeff''',
            '''gx0[i] = *coeff * (x0[i] - x1[i]);
               gx1[i] = -gx0[i];''',
            'mse_bwd')(gx0, gx1, x0, x1, coeff)
        return gx0, gx1

def mean_squared_error(x0, x1):
    """Mean squared error function.

    This function computes mean squared error between two variables. The mean
    is taken over the minibatch. Note that the error is not scaled by 1/2.

    """
    return MeanSquaredError()(x0, x1)

max_stars_repo_name: woodshop/complex-chainer

max_issues_repo_name: woodshop/complex-chainer

max_forks_repo_name: woodshop/complex-chainer


text:
from typing import Dict, Set, List, Tuple, Type, Callable, Any, Awaitable, Union
from unittest import TestCase

import numpy

from typish import get_type, instance_of, NoneType, Unknown

class TestGetType(TestCase):
    def test_get_type_list(self):
        self.assertEqual(List[int], get_type([1, 2, 3]))
        self.assertEqual(List[object], get_type([1, 2, '3']))
        self.assertEqual(List[Unknown], get_type([]))

    def test_get_type_list_with_union(self):
        self.assertEqual(List[Union[int, str, float]], get_type([1, '2', 3.0], True))

    def test_get_type_set(self):
        self.assertEqual(Set[int], get_type({1, 2, 3}))
        self.assertEqual(Set[object], get_type({1, 2, '3'}))
        self.assertEqual(Set[Unknown], get_type(set([])))

    def test_get_type_dict(self):
        self.assertEqual(Dict[str, int], get_type({'a': 1, 'b': 2}))
        self.assertEqual(Dict[int, object], get_type({1: 'a', 2: 2}))
        self.assertEqual(Dict[Unknown, Unknown], get_type({}))

    def test_get_type_tuple(self):
        self.assertEqual(Tuple[int, int, int], get_type((1, 2, 3)))
        self.assertEqual(Tuple[int, str, str], get_type((1, '2', '3')))

    def test_get_type_callable(self):

        def func1(x: int, y: str) -> object:
            pass

        def func2() -> int:
            pass

        def func3(x: int):
            pass

        def func4():
            pass

        def func5(x):
            pass

        def func6(l: List[List[List[int]]]):
            pass

        self.assertEqual(Callable[[int, str], object], get_type((func1)))
        self.assertEqual(Callable[[], int], get_type(func2))
        self.assertEqual(Callable[[int], NoneType], get_type(func3))
        self.assertEqual(Callable, get_type(func4))
        self.assertEqual(Callable[[Any], NoneType], get_type(func5))
        self.assertEqual(Callable[[List[List[List[int]]]], NoneType], get_type(func6))

    def test_get_type_async(self):

        async def func(x: int) -> str:
            return '42'

        self.assertEqual(Callable[[int], Awaitable[str]], get_type(func))

    def test_get_type_lambda(self):
        self.assertEqual(Callable[[Unknown, Unknown], Unknown], get_type(lambda x, y: 42))
        self.assertEqual(Callable[[], Unknown], get_type(lambda: 42))

    def test_get_type_deep(self):
        self.assertEqual(List[List[int]], get_type([[1, 2], [3]]))
        self.assertEqual(List[List[object]], get_type([[1, 2], ['3']]))

        deep = [[[1, 2]], [[3]], [[4], [5, '6']]]

        self.assertEqual(List[List[List[object]]], get_type(deep))
        self.assertEqual(Dict[str, List[List[List[object]]]], get_type({'a': deep}))

    def test_get_type_type(self):
        self.assertEqual(Type[int], get_type(int))

    def test_get_type_some_class(self):
        class SomeRandomClass:
            pass

        self.assertEqual(SomeRandomClass, get_type(SomeRandomClass()))

    def test_get_type_any(self):
        any_type = get_type(Any)

        self.assertEqual(Any, any_type)

    def test_get_type_union(self):
        union_type = get_type(Union[int, str])

        # The following line should not raise.
        Union[(union_type,)]

        self.assertTrue(instance_of(Union[int, str], union_type))

    def test_get_type_of_ndarray(self):
        arr_type = get_type(numpy.array([1, 2, 3]))
        self.assertEqual(numpy.ndarray, arr_type)

max_stars_repo_name: georgeharker/typish

max_issues_repo_name: georgeharker/typish

max_forks_repo_name: georgeharker/typish


text:
"""
NCL_xy_3.py
===============
This script illustrates the following concepts:
   - Reversing the Y axis
   - Changing the line dash pattern in an XY plot
   - Creating your own line dash pattern for an XY plot
   - Changing the line color and thickness in an XY plot
   - Creating a vertical profile plot

See following URLs to see the reproduced NCL plot & script:
    - Original NCL script: https://www.ncl.ucar.edu/Applications/Scripts/xy_3.ncl
    - Original NCL plots: https://www.ncl.ucar.edu/Applications/Images/xy_3_1_lg.png and https://www.ncl.ucar.edu/Applications/Images/xy_3_2_lg.png
"""

###############################################################################
# Import packages:

import numpy as np
import xarray as xr
import matplotlib.pyplot as plt

import geocat.datafiles as gdf
from geocat.viz import util as gvutil

###############################################################################
# Read in data:

# Open a netCDF data file using xarray default engine and load the data into xarrays
ds = xr.open_dataset(gdf.get("netcdf_files/atmos.nc"), decode_times=False)
ds = ds.U
ds = ds.isel(time=0).drop('time')
ds = ds.isel(lon=0).drop('lon')
ds = ds.isel(lat=42).drop('lat')

###############################################################################
# Plot:

# Generate figure (set its size (width, height) in inches) and axes
plt.figure(figsize=(8, 8))
ax = plt.gca()

# Plot data
plt.plot(ds.data, ds.lev)

# Use geocat.viz.util convenience function to add minor and major tick lines
gvutil.add_major_minor_ticks(ax, x_minor_per_major=5, y_minor_per_major=4)

# Use geocat.viz.util convenience function to set axes parameters
gvutil.set_axes_limits_and_ticks(ax,
                                 ylim=(1000, 0),
                                 xticks=np.arange(-10, 30, 5))

# Use geocat.viz.util convenience function to set titles and labels
gvutil.set_titles_and_labels(ax,
                             maintitle="Profile Plot",
                             xlabel=ds.long_name,
                             ylabel=ds['lev'].long_name)

plt.show()

###############################################################################
# Plot:

# Generate figure (set its size (width, height) in inches) and axes
plt.figure(figsize=(8, 8))
ax = plt.gca()

# Plot data with custom line characterisitcs
# Use keyword `color` to change the line color
# Use keyword `linewidth` to change the line thickness
# Use keyword `dashes` to create a custom dash pattern
# Use keyword `dash_capstyle` to change the shape of the dash end
plt.plot(ds.data,
         ds.lev,
         color='red',
         linewidth=3,
         dashes=[3, 2, 1, 2, 1, 2, 1, 2],
         dash_capstyle='round')

# Use geocat.viz.util convenience function to add minor and major tick lines
gvutil.add_major_minor_ticks(ax, x_minor_per_major=5, y_minor_per_major=4)

# Use geocat.viz.util convenience function to set axes parameters
gvutil.set_axes_limits_and_ticks(ax,
                                 ylim=(1000, 0),
                                 xticks=np.arange(-10, 30, 5))

# Use geocat.viz.util convenience function to set titles and labels
gvutil.set_titles_and_labels(ax,
                             maintitle="Make your own dash pattern",
                             xlabel=ds.long_name,
                             ylabel=ds['lev'].long_name)

plt.show()

max_stars_repo_name: NCAR/GeoCAT-examples

max_issues_repo_name: netgodz/GeoCAT-examples

max_forks_repo_name: netgodz/GeoCAT-examples


text:
#%env CUDA_VISIBLE_DEVICES=2

import torch, torchvision
from torchvision import datasets, models, transforms
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import DataLoader
import time
from torchsummary import summary

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

from PIL import Image

# Applying Transforms to the Data
image_transforms = {
    'train': transforms.Compose([
        transforms.RandomResizedCrop(size=256, scale=(0.8, 1.0)),
        transforms.RandomRotation(degrees=15),
        transforms.RandomHorizontalFlip(),
        transforms.CenterCrop(size=224),
        transforms.ToTensor(),
        transforms.Normalize([0.485, 0.456, 0.406],
                             [0.229, 0.224, 0.225])
    ]),
    'valid': transforms.Compose([
        transforms.Resize(size=256),
        transforms.CenterCrop(size=224),
        transforms.ToTensor(),
        transforms.Normalize([0.485, 0.456, 0.406],
                             [0.229, 0.224, 0.225])
    ]),
    'test': transforms.Compose([
        transforms.Resize(size=256),
        transforms.CenterCrop(size=224),
        transforms.ToTensor(),
        transforms.Normalize([0.485, 0.456, 0.406],
                             [0.229, 0.224, 0.225])
    ])
}

# Load the Data

# Set train and valid directory paths

dataset = 'data'

train_directory = os.path.join(dataset, 'train')
valid_directory = os.path.join(dataset, 'valid')
test_directory = os.path.join(dataset, 'test')

# Batch size
bs = 32

# Number of classes
num_classes = len(os.listdir(valid_directory))  #10#2#257
print(num_classes)

# Load Data from folders
data = {
    'train': datasets.ImageFolder(root=train_directory, transform=image_transforms['train']),
    'valid': datasets.ImageFolder(root=valid_directory, transform=image_transforms['valid']),
    'test': datasets.ImageFolder(root=test_directory, transform=image_transforms['test'])
}

# Get a mapping of the indices to the class names, in order to see the output classes of the test images.
idx_to_class = {v: k for k, v in data['train'].class_to_idx.items()}
print(idx_to_class)

# Size of Data, to be used for calculating Average Loss and Accuracy
train_data_size = len(data['train'])
valid_data_size = len(data['valid'])
test_data_size = len(data['test'])

# Create iterators for the Data loaded using DataLoader module
train_data_loader = DataLoader(data['train'], batch_size=bs, shuffle=True)
valid_data_loader = DataLoader(data['valid'], batch_size=bs, shuffle=True)
test_data_loader = DataLoader(data['test'], batch_size=bs, shuffle=True)

# Define Optimizer and Loss Function
loss_func = nn.NLLLoss()

device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")

def computeTestSetAccuracy(model, loss_criterion):
    '''
    Function to compute the accuracy on the test set
    Parameters
        :param model: Model to test
        :param loss_criterion: Loss Criterion to minimize
    '''

    device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")

    test_acc = 0.0
    test_loss = 0.0

    # Validation - No gradient tracking needed
    with torch.no_grad():

        # Set to evaluation mode
        model.eval()

        # Validation loop
        for j, (inputs, labels) in enumerate(test_data_loader):
            inputs = inputs.to(device)
            labels = labels.to(device)

            # Forward pass - compute outputs on input data using the model
            outputs = model(inputs)

            # Compute loss
            loss = loss_criterion(outputs, labels)

            # Compute the total loss for the batch and add it to valid_loss
            test_loss += loss.item() * inputs.size(0)

            # Calculate validation accuracy
            ret, predictions = torch.max(outputs.data, 1)
            correct_counts = predictions.eq(labels.data.view_as(predictions))

            # Convert correct_counts to float and then compute the mean
            acc = torch.mean(correct_counts.type(torch.FloatTensor))

            # Compute total accuracy in the whole batch and add to valid_acc
            test_acc += acc.item() * inputs.size(0)

            print("Test Batch number: {:03d}, Test: Loss: {:.4f}, Accuracy: {:.4f}".format(j, loss.item(), acc.item()))

    # Find average test loss and test accuracy
    avg_test_loss = test_loss/test_data_size
    avg_test_acc = test_acc/test_data_size

    print("Test accuracy : " + str(avg_test_acc))

def predict(model, test_image_name):
    '''
    Function to predict the class of a single test image
    Parameters
        :param model: Model to test
        :param test_image_name: Test image

    '''

    transform = image_transforms['test']

    test_image = Image.open(test_image_name)
    plt.imshow(test_image)

    test_image_tensor = transform(test_image)

    if torch.cuda.is_available():
        test_image_tensor = test_image_tensor.view(1, 3, 224, 224).cuda()
    else:
        test_image_tensor = test_image_tensor.view(1, 3, 224, 224)

    with torch.no_grad():
        model.eval()
        # Model outputs log probabilities
        out = model(test_image_tensor)
        ps = torch.exp(out)
        topk, topclass = ps.topk(3, dim=1)
        for i in range(3):
            print("Predcition", i+1, ":", idx_to_class[topclass.cpu().numpy()[0][i]], ", Score: ", topk.cpu().numpy()[0][i])

# Test a particular model on a test image

dataset = 'data'
model = torch.load('data_model_29.pt')
predict(model, 'data/test/bottles/WIN_20190403_10_58_25_Pro.jpg')

# Load Data from folders
computeTestSetAccuracy(model, loss_func)


text:
import copy
import numpy as np
from menpo.landmark.base import LandmarkGroup
from menpo.landmark.exceptions import LabellingError

def imm_58_points(landmark_group):
    """
    Apply the 58 point semantic labels from the
    IMM dataset to the landmarks in the given landmark group.

    The label applied to this new manager will be 'imm_58_points'.

    The semantic labels applied are as follows:

      - chin
      - leye
      - reye
      - leyebrow
      - reyebrow
      - mouth
      - nose

    Parameters
    ----------
    landmark_group: :class:`menpo.landmark.base.LandmarkGroup`
        The landmark group to apply semantic labels to.

    Returns
    -------
    landmark_group: :class:`menpo.landmark.base.LandmarkGroup`
        New landmark group with group label 'imm_58_points'

    Raises
    ------
    :class:`menpo.landmark.exceptions.LabellingError`
        If the given landmark group contains less than 58 points

    References
    -----------
    .. [1] http://www2.imm.dtu.dk/~aam/
    """
    group_label = 'imm_58_points'
    n_points = landmark_group.lms.n_points

    if n_points != 58:
        raise LabellingError("{0} mark-up expects exactly 58 "
                             "points. However, the given landmark group only "
                             "has {1} points".format(group_label, n_points))

    new_landmark_group = LandmarkGroup(
        landmark_group._target, group_label,
        copy.deepcopy(landmark_group.lms),
        {'all': np.ones(n_points, dtype=np.bool)})

    new_landmark_group['chin'] = np.arange(13)
    new_landmark_group['leye'] = np.arange(13, 21)
    new_landmark_group['reye'] = np.arange(21, 29)
    new_landmark_group['leyebrow'] = np.arange(29, 34)
    new_landmark_group['reyebrow'] = np.arange(34, 39)
    new_landmark_group['mouth'] = np.arange(39, 47)
    new_landmark_group['nose'] = np.arange(47, 58)

    return new_landmark_group

def ibug_68_points(landmark_group):
    """
    Apply the ibug's "standard" 68 point semantic labels (based on the
    original semantic labels of multiPIE) to the landmark group.

    The group label will be 'ibug_68_points'.

    The semantic labels applied are as follows:

      - chin
      - leye
      - reye
      - leyebrow
      - reyebrow
      - mouth
      - nose

    Parameters
    ----------
    landmark_group: :class:`menpo.landmark.base.LandmarkGroup`
        The landmark group to apply semantic labels to.

    Returns
    -------
    landmark_group : :class:`menpo.landmark.base.LandmarkGroup`
        New landmark group with group label 'ibug_68_points'. The pointcloud
        is also copied.

    Raises
    ------
    :class:`menpo.landmark.exceptions.LabellingError`
        If the given landmark group contains less than 68 points

    References
    ----------
    .. [1] http://www.multipie.org/
    """
    # TODO: This should probably be some sort of graph that maintains the
    # connectivity defined by ibug (and thus not a PointCloud)
    group_label = 'ibug_68_points'
    n_points = landmark_group.lms.n_points

    if n_points != 68:
        raise LabellingError("{0} mark-up expects exactly 68 "
                             "points. However, the given landmark group only "
                             "has {1} points".format(group_label, n_points))

    new_landmark_group = LandmarkGroup(
        landmark_group._target, group_label,
        copy.deepcopy(landmark_group.lms),
        {'all': np.ones(n_points, dtype=np.bool)})

    new_landmark_group['chin'] = np.arange(17)
    new_landmark_group['leye'] = np.arange(36, 42)
    new_landmark_group['reye'] = np.arange(42, 48)
    new_landmark_group['leyebrow'] = np.arange(17, 22)
    new_landmark_group['reyebrow'] = np.arange(22, 27)
    new_landmark_group['mouth'] = np.arange(48, 68)
    new_landmark_group['nose'] = np.arange(27, 36)

    return new_landmark_group

def ibug_68_contour(landmark_group):
    """
    Apply the ibug's "standard" 68 point semantic labels (based on the
    original semantic labels of multiPIE) to the landmarks in
    the given landmark manager.

    The label applied to this new group will be 'ibug_68_contour'.

    The semantic labels applied are as follows:

      - contour

    Parameters
    ----------
    landmark_group: :class:`menpo.landmark.base.LandmarkGroup`
        The landmark group to apply semantic labels to.

    Returns
    -------
    landmark_group : :class:`menpo.landmark.base.LandmarkGroup`
        New landmark group with label 'ibug_68_contour'

    Raises
    ------
    :class:`menpo.landmark.exceptions.LabellingError`
        If the given landmark set contains less than 68 points

    References
    ----------
    .. [1] http://www.multipie.org/
    """
    # TODO: This should probably be some sort of graph that maintains the
    # connectivity defined by ibug (and thus not a PointCloud)
    group_label = 'ibug_68_contour'
    n_points = landmark_group.lms.n_points

    if n_points != 68:
        raise LabellingError("{0} mark-up expects exactly 68 "
                             "points. However, the given landmark group only "
                             "has {1} points".format(group_label, n_points))

    new_landmarks = copy.deepcopy(landmark_group.lms)
    ind = np.hstack((np.arange(17), np.arange(16, 21),
                     np.arange(21, 26), np.arange(1)))
    new_landmarks.points = new_landmarks.points[ind]
    new_landmark_group = LandmarkGroup(
        landmark_group._target, group_label, new_landmarks,
        {'all': np.ones(new_landmarks.n_points, dtype=np.bool)})

    return new_landmark_group

def ibug_68_trimesh(landmark_group):
    """
    Apply the ibug's "standard" 68 point semantic labels (based on the
    original semantic labels of multiPIE) to the landmarks in
    the given landmark group.

    The label applied to this new group will be 'ibug_68_trimesh'.

    The semantic labels applied are as follows:

      - tri

    Parameters
    ----------
    landmark_group: :class:`menpo.landmark.base.LandmarkGroup`
        The landmark group to apply semantic labels to.

    Returns
    -------
    landmark_group : :class:`menpo.landmark.base.LandmarkGroup`
        New landmark manager with label 'ibug_68_trimesh'

    Raises
    ------
    :class:`menpo.landmark.exceptions.LabellingError`
        If the given landmark group contains less than 68 points

    References
    ----------
    .. [1] http://www.multipie.org/
    """
    from menpo.shape import TriMesh

    group_label = 'ibug_68_trimesh'
    n_points = landmark_group.lms.n_points

    if n_points != 68:
        raise LabellingError("{0} mark-up expects exactly 68 "
                             "points. However, the given landmark group only "
                             "has {1} points".format(group_label, n_points))

    tri_list = np.array([[47, 29, 28], [44, 43, 23], [38, 20, 21], [47, 28,42],
                        [49, 61, 60], [40, 41, 37], [37, 19, 20], [28, 40, 39],
                        [38, 21, 39], [36,  1, 	0], [48, 59,  4], [49, 60, 48],
                        [67, 59, 60], [13, 53, 14], [61, 51, 62], [57,  8,  7],
                        [52, 51, 33], [61, 67, 60], [52, 63, 51], [66, 56, 57],
                        [35, 30, 29], [53, 52, 35], [37, 36, 17], [18, 37, 17],
                        [37, 38, 40], [38, 37, 20], [19, 37, 18], [38, 39, 40],
                        [28, 29, 40], [41, 36, 37], [27, 39, 21], [41, 31,  1],
                        [30, 32, 31], [33, 51, 50], [33, 30, 34], [31, 40, 29],
                        [36,  0, 17], [31,  2,  1], [31, 41, 40], [ 1, 36, 41],
                        [31, 49,  2], [ 2, 49,  3], [60, 59, 48], [ 3, 49, 48],
                        [31, 32, 50], [48,  4,  3], [59,  5,  4], [58, 67, 66],
                        [ 5, 59, 58], [58, 59, 67], [ 7,  6, 58], [66, 57, 58],
                        [13, 54, 53], [ 7, 58, 57], [ 6,  5, 58], [50, 61, 49],
                        [62, 67, 61], [31, 50, 49], [32, 33, 50], [30, 33, 32],
                        [34, 52, 33], [35, 52, 34], [53, 63, 52], [62, 63, 65],
                        [62, 51, 63], [66, 65, 56], [63, 53, 64], [62, 66, 67],
                        [62, 65, 66], [57, 56,  9], [65, 63, 64], [ 8, 57,  9],
                        [ 9, 56, 10], [10, 56, 11], [11, 56, 55], [11, 55, 12],
                        [56, 65, 55], [55, 64, 54], [55, 65, 64], [55, 54, 12],
                        [64, 53, 54], [12, 54, 13], [45, 46, 44], [35, 34, 30],
                        [14, 53, 35], [15, 46, 45], [27, 28, 39], [27, 42, 28],
                        [35, 29, 47], [30, 31, 29], [15, 35, 46], [15, 14, 35],
                        [43, 22, 23], [27, 21, 22], [24, 44, 23], [44, 47, 43],
                        [43, 47, 42], [46, 35, 47], [26, 45, 44], [46, 47, 44],
                        [25, 44, 24], [25, 26, 44], [16, 15, 45], [16, 45, 26],
                        [22, 42, 43], [50, 51, 61], [27, 22, 42]])
    new_landmark_group = LandmarkGroup(
        landmark_group._target, group_label,
        TriMesh(copy.deepcopy(landmark_group.lms.points), tri_list),
        {'all': np.ones(n_points, dtype=np.bool)})

    return new_landmark_group

# TODO: ibug_68_all? imports points, contour and trimesh?

def ibug_68_closed_mouth(landmark_group):
    """
    Apply the ibug's "standard" 68 point semantic labels (based on the
    original semantic labels of multiPIE) to the landmarks in
    the given landmark group - but ignore the 3 points that are coincident for
    a closed mouth. Therefore, there only 65 points are returned.

    The label applied to this new group will be 'ibug_68_closed_mouth'.

    The semantic labels applied are as follows:

      - tri

    Parameters
    ----------
    landmark_group: :class:`menpo.landmark.base.LandmarkGroup`
        The landmark group to apply semantic labels to.

    Returns
    -------
    landmark_group : :class:`menpo.landmark.base.LandmarkGroup`
        New landmark manager with label 'ibug_68_closed_mouth'

    Raises
    ------
    :class:`menpo.landmark.exceptions.LabellingError`
        If the given landmark group contains less than 65 points

    References
    ----------
    .. [1] http://www.multipie.org/
    """
    group_label = 'ibug_68_closed_mouth'
    n_points = landmark_group.lms.n_points

    if landmark_group.lms.n_points != 68:
        raise LabellingError("{0} mark-up expects exactly 68 "
                             "points. However, the given landmark group only "
                             "has {1} points".format(group_label, n_points))

    # Ignore the 3 coincident points (the last 3 points)
    new_landmarks = copy.deepcopy(landmark_group.lms)
    new_landmarks.points = new_landmarks.points[:-3]
    new_landmark_group = LandmarkGroup(
        landmark_group._target, group_label, new_landmarks,
        {'all': np.ones(new_landmarks.n_points, dtype=np.bool)})

    new_landmark_group['chin'] = np.arange(17)
    new_landmark_group['leye'] = np.arange(36, 42)
    new_landmark_group['reye'] = np.arange(42, 48)
    new_landmark_group['leyebrow'] = np.arange(17, 22)
    new_landmark_group['reyebrow'] = np.arange(22, 27)
    new_landmark_group['mouth'] = np.arange(48, 65)
    new_landmark_group['nose'] = np.arange(27, 36)

    return new_landmark_group

def ibug_66_points(landmark_group):
    """
    Apply the ibug's "standard" 66 point semantic labels (based on the
    original semantic labels of multiPIE but ignoring the 2 points
    describing the inner mouth corners) to the landmark group.

    The group label will be 'ibug_66_points'.

    The semantic labels applied are as follows:

      - chin
      - leye
      - reye
      - leyebrow
      - reyebrow
      - mouth
      - nose

    Parameters
    ----------
    landmark_group: :class:`menpo.landmark.base.LandmarkGroup`
        The landmark group to apply semantic labels to.

    Returns
    -------
    landmark_group : :class:`menpo.landmark.base.LandmarkGroup`
        New landmark group with group label 'ibug_66_points'. The pointcloud
        is also copied.

    Raises
    ------
    :class:`menpo.landmark.exceptions.LabellingError`
        If the given landmark group contains less than 68 points

    References
    ----------
    .. [1] http://www.multipie.org/
    """
    group_label = 'ibug_66_points'
    n_points = landmark_group.lms.n_points

    if landmark_group.lms.n_points != 68:
        raise LabellingError("{0} mark-up expects exactly 68 "
                             "points. However, the given landmark group only "
                             "has {1} points".format(group_label, n_points))

    # Ignore the 2 inner mouth corners
    new_landmarks = copy.deepcopy(landmark_group.lms)
    ind = np.hstack((np.arange(0, 60), np.arange(61, 64),
                     np.arange(65, 68)))
    new_landmarks.points = new_landmarks.points[ind]
    new_landmark_group = LandmarkGroup(
        landmark_group._target, group_label, new_landmarks,
        {'all': np.ones(new_landmarks.n_points, dtype=np.bool)})

    new_landmark_group['chin'] = np.arange(17)
    new_landmark_group['leye'] = np.arange(36, 42)
    new_landmark_group['reye'] = np.arange(42, 48)
    new_landmark_group['leyebrow'] = np.arange(17, 22)
    new_landmark_group['reyebrow'] = np.arange(22, 27)
    new_landmark_group['mouth'] = np.arange(48, 66)
    new_landmark_group['nose'] = np.arange(27, 36)

    return new_landmark_group

def ibug_51_points(landmark_group):
    """
    Apply the ibug's "standard" 51 point semantic labels (based on the
    original semantic labels of multiPIE but removing the annotations
    corresponding to the chin region) to the landmark group.

    The group label will be 'ibug_51_points'.

    The semantic labels applied are as follows:

      - leye
      - reye
      - leyebrow
      - reyebrow
      - mouth
      - nose

    Parameters
    ----------
    landmark_group: :class:`menpo.landmark.base.LandmarkGroup`
        The landmark group to apply semantic labels to.

    Returns
    -------
    landmark_group : :class:`menpo.landmark.base.LandmarkGroup`
        New landmark group with group label 'ibug_51_points'. The pointcloud
        is also copied.

    Raises
    ------
    :class:`menpo.landmark.exceptions.LabellingError`
        If the given landmark group contains less than 68 points

    References
    ----------
    .. [1] http://www.multipie.org/
    """
    group_label = 'ibug_51_points'
    n_points = landmark_group.lms.n_points

    if landmark_group.lms.n_points != 68:
        raise LabellingError("{0} mark-up expects exactly 68 "
                             "points. However, the given landmark group only "
                             "has {1} points".format(group_label, n_points))

    # Ignore the chin region
    new_landmarks = copy.deepcopy(landmark_group.lms)
    ind = np.arange(17, 68)
    new_landmarks.points = new_landmarks.points[ind]
    new_landmark_group = LandmarkGroup(
        landmark_group._target, group_label, new_landmarks,
        {'all': np.ones(new_landmarks.n_points, dtype=np.bool)})

    new_landmark_group['leye'] = np.arange(19, 25)
    new_landmark_group['reye'] = np.arange(25, 31)
    new_landmark_group['leyebrow'] = np.arange(0, 5)
    new_landmark_group['reyebrow'] = np.arange(5, 10)
    new_landmark_group['mouth'] = np.arange(31, 51)
    new_landmark_group['nose'] = np.arange(10, 19)

    return new_landmark_group

def ibug_49_points(landmark_group):
    """
    Apply the ibug's "standard" 49 point semantic labels (based on the
    original semantic labels of multiPIE but removing the annotations
    corresponding to the chin region and the 2 describing the inner mouth
    corners) to the landmark group.

    The group label will be 'ibug_49_points'.

    The semantic labels applied are as follows:

      - leye
      - reye
      - leyebrow
      - reyebrow
      - mouth
      - nose

    Parameters
    ----------
    landmark_group: :class:`menpo.landmark.base.LandmarkGroup`
        The landmark group to apply semantic labels to.

    Returns
    -------
    landmark_group : :class:`menpo.landmark.base.LandmarkGroup`
        New landmark group with group label 'ibug_49_points'. The pointcloud
        is also copied.

    Raises
    ------
    :class:`menpo.landmark.exceptions.LabellingError`
        If the given landmark group contains less than 68 points

    References
    ----------
    .. [1] http://www.multipie.org/
    """
    group_label = 'ibug_49_points'
    n_points = landmark_group.lms.n_points

    if landmark_group.lms.n_points != 68:
        raise LabellingError("{0} mark-up expects exactly 68 "
                             "points. However, the given landmark group only "
                             "has {1} points".format(group_label, n_points))

    # Ignore the chin region and the 2 inner mouth corners
    new_landmarks = copy.deepcopy(landmark_group.lms)
    ind = np.hstack((np.arange(17, 60), np.arange(61, 64),
                     np.arange(65, 68)))
    new_landmarks.points = new_landmarks.points[ind]
    new_landmark_group = LandmarkGroup(
        landmark_group._target, group_label, new_landmarks,
        {'all': np.ones(new_landmarks.n_points, dtype=np.bool)})

    new_landmark_group['leye'] = np.arange(19, 25)
    new_landmark_group['reye'] = np.arange(25, 31)
    new_landmark_group['leyebrow'] = np.arange(0, 5)
    new_landmark_group['reyebrow'] = np.arange(5, 10)
    new_landmark_group['mouth'] = np.arange(31, 49)
    new_landmark_group['nose'] = np.arange(10, 19)

    return new_landmark_group

def labeller(landmarkables, group_label, label_func):
    """
    Takes a list of landmarkable objects and a group label indicating which
    set of landmarks should have semantic meaning attached to them.
    The labelling function will add a new landmark group to each object that
    have been semantically annotated.

    Parameters
    ----------
    landmarkables: list of :class:`menpo.landmark.base.Landmarkable`
        List of landmarkable objects
    group_label: string
        The group label of the landmark group to apply semantic labels to.
    label_func: func
        A labelling function taken from this module. ``func`` should take a
        :class:`menpo.landmark.base.LandmarkGroup` and
        return a new LandmarkGroup with semantic labels applied.

    Returns
    -------
    landmarkables : list of :class:`menpo.landmark.base.Landmarkable`
        The list of modified landmarkables (this is just for convenience,
        the list will actually be modified in place)
    """
    landmark_groups = [label_func(landmarkable.landmarks[group_label])
                       for landmarkable in landmarkables]

    for (lanmarkable, group) in zip(landmarkables, landmark_groups):
        lanmarkable.landmarks[group.group_label] = group

    return landmarkables

max_stars_repo_name: ikassi/menpo

max_issues_repo_name: ikassi/menpo

max_forks_repo_name: ikassi/menpo


text:
import tensorflow as tf
import numpy as np
# === Create data and simulate results =====

N = 20000

def sigmoid(x):
    return 1 / (1 + np.exp(-x))

# === Create data and simulate results =====
x_data = np.random.randn(N,3)
w_real = [0.3,0.5,0.1]
b_real = -0.2
wxb = np.matmul(w_real,x_data.T) + b_real

y_data_pre_noise = sigmoid(wxb)
y_data = np.random.binomial(1,y_data_pre_noise)

NUM_STEPS = 50

g = tf.Graph()
wb_ = []
with g.as_default():
    x = tf.placeholder(tf.float32,shape=[None,3])
    y_true = tf.placeholder(tf.float32,shape=None)

    with tf.name_scope('inference') as scope:
        w = tf.Variable([[0,0,0]],dtype=tf.float32,name='weights')
        b = tf.Variable(0,dtype=tf.float32,name='bias')
        y_pred = tf.matmul(w,tf.transpose(x)) + b
        y_pred = tf.sigmoid(y_pred)
    with tf.name_scope('loss') as scope:
        loss = tf.nn.sigmoid_cross_entropy_with_logits(labels=y_true,logits=y_pred)
        loss = tf.reduce_mean(loss)

    with tf.name_scope('train') as scope:
        learning_rate = 0.5
        optimizer = tf.train.GradientDescentOptimizer(learning_rate)
        train = optimizer.minimize(loss)

    # Before starting, initialize the variables.  We will 'run' this first.
    init = tf.global_variables_initializer()
    with tf.Session() as sess:
        sess.run(init)
        for step in range(NUM_STEPS):
            sess.run(train,{x: x_data, y_true: y_data})
            if (step % 5 == 0):
                print(step, sess.run([w,b]))
                wb_.append(sess.run([w,b]))

        print(50, sess.run([w,b]))

max_stars_repo_name: kblok/TensorFlow-Example

max_issues_repo_name: kblok/TensorFlow-Example

max_forks_repo_name: kblok/TensorFlow-Example


text:
try:
    import unzip_requirements
except ImportError:
    pass

import base64
import boto3
import os
import io
import json
import numpy as np

import torch
import torchvision
import torchvision.transforms as transforms

from io import BytesIO
from PIL import Image
from requests_toolbelt.multipart import decoder

S3_BUCKET = os.environ["S3_BUCKET"] if "S3_BUCKET" in os.environ else "sgm-models"
MODEL_PATH = (
    os.environ["MODEL_PATH"] if "MODEL_PATH" in os.environ else "./mobilenet_v2.pt"
)

print(f"Donwloading model: {MODEL_PATH}")

# Loading the S3 client when Lambda execution context is created
s3 = boto3.client("s3")

def load_model_from_s3():
    try:
        if os.path.isfile(MODEL_PATH) != True:
            obj = s3.get_object(Bucket=S3_BUCKET, key=MODEL_PATH)
            print("Creating Byte stream")
            byte_stream = io.BytesIO(obj["Body"].read())
            print("Loading model.")
            model = torch.jit.load(byte_stream)
            print("Model loaded")
            return model
        else:
            print("Model loading failed.")
    except Exception as e:
        print(repr(e))
        raise (e)

model = load_model_from_s3()

def transform_image(image_bytes):
    try:
        transformations = transforms.Compose(
            [
                transforms.Resize(255),
                transforms.CenterCrop(224),
                transforms.ToTensor(),
                transforms.Normalize(
                    mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]
                ),
            ]
        )
        image = Image.open(io.BytesIO(image_bytes))
        return transformations(image).unsqueeze(0)
    except Exception as e:
        print(repr(e))
        raise (e)

def get_prediction(image_bytes):
    tensor = transform_image(image_bytes=image_bytes)
    return model(tensor).argmax().item()

def classify_image(event, context):
    try:
        content_type_header = event["headers"]["content-type"]
        print(f"Image content: {event['body']}")

        body = base64.b64decode(event["body"])
        print("Body Loaded.")

        img = decoder.MultipartDecoder(body, content_type_header).parts[0]
        prediction = get_prediction(image_bytes=img.content)
        print(f"Image classification code: {prediction}")

        file_name = (
            img.headers[b"Content-Disposition"].decode().split(";")[2].split("=")[1]
        )

        return {
            "statusCode": 200,
            "headers": {
                "Content-Type": "application/json",
                "Access-Control-Allow-Origin": "*",
                "Access-Control-Allow-Credentials": True,
            },
            "body": json.cump(
                {"file": file_name.replace('"', ""), "predicted": prediction}
            ),
        }

    except Exception as e:
        print(repr(e))
        return {
            "statusCode": 500,
            "headers": {
                "Content-Type": "application/json",
                "Access-Control-Allow-Origin": "*",
                "Access-Control-Allow-Credentials": True,
            },
            "body": json.dumps({"error": repr(e)}),
        }


text:
"""Provides data for the Cifar10 dataset, but randomises the labels

Loads the data using the new DataSet API. This completely
randomises the label by creating a label which corresponds
to the hash value of the image.
"""

import numpy as np
import tensorflow as tf
import functools

import sys
sys.path.append("../datasets");
sys.path.append("../../")
sys.path.append("../../nnet")

from datasets import DatasetFactory

_FILE_PATTERN = 'cifar10_%s.tfrecord'

SPLITS_TO_SIZES = {'train': 50000, 'test': 10000}

_NUM_CLASSES = 10

_ITEMS_TO_DESCRIPTIONS = {
    'image': 'A [32 x 32 x 3] color image.',
    'label': 'A single integer between 0 and 9',
}

def _get_random_labels_and_probability(num_labels, seed):
    random = np.random.RandomState(seed)

    random_labels = random.randint(num_labels, size=50000)
    random_probabilities = random.random_sample(size=50000)

    data = tf.data.Dataset.from_tensor_slices(
        {'random_label': random_labels, 'random_prob': random_probabilities})

    return data

def _parse_record(record, random_data, random_prob=1):
    keys_to_features = {
        'image/encoded': tf.FixedLenFeature((), tf.string, default_value=''),
        'image/format': tf.FixedLenFeature((), tf.string, default_value='png'),
        'image/class/label': tf.FixedLenFeature(
            [], tf.int64, default_value=tf.zeros([], dtype=tf.int64)),
    }

    parsed = tf.parse_single_example(record, keys_to_features)

    label = tf.where(
        tf.less_equal(random_data['random_prob'], random_prob),
        tf.cast(random_data['random_label'], dtype=tf.int64),
        parsed['image/class/label'],
        name='label')

    image = tf.image.decode_png(parsed['image/encoded'], channels=3)

    return {
        'image': image,
        'image/class/label': label,
        'image/class/original_label': parsed['image/class/label']
    }

def get_split(record_file, num_classes: int=10,
              proportion_random: float=1,
              seed=42) -> DatasetFactory:
    """Gets a dataset of CIFAR-10 data with randomized labels.

    This gets a dataset of images from the given record, with the labels
    corresponding to the images being completely randomized.

    Parameters
    ----------
    record_file: The path to the record file containing the images.
    num_classes: The number of classes to split the data into.
    proportion_random: The proportion of instances that are randomised.
    seed: The random seed to use.

    Returns
    -------
    dataset: A `tensorflow.data.Dataset` containing the images with
        randomized labels.
    """
    def cifar_dataset_factory():
        parse_fn = functools.partial(_parse_record,
                                     random_prob=proportion_random)

        dataset = tf.data.TFRecordDataset(record_file)
        random_data = _get_random_labels_and_probability(num_classes, seed)

        return (tf.data.Dataset.zip((dataset, random_data))
                .map(parse_fn, num_parallel_calls=4)
                .cache())

    return cifar_dataset_factory


text:
from os import listdir, rename, getcwd
from os.path import isfile, join
import numpy as np
import tensorflow as tf
import time
def read_tensor_from_image_file(file_name,
                                input_height=224,
                                input_width=224,
                                input_mean=0,
                                input_std=255):
    input_name = "file_reader"
    output_name = "normalized"
    file_reader = tf.read_file(file_name, input_name)
    if file_name.endswith(".png"):
        image_reader = tf.image.decode_png(
            file_reader, channels=3, name="png_reader")
    elif file_name.endswith(".gif"):
        image_reader = tf.squeeze(
            tf.image.decode_gif(file_reader, name="gif_reader"))
    elif file_name.endswith(".bmp"):
        image_reader = tf.image.decode_bmp(file_reader, name="bmp_reader")
    else:
        image_reader = tf.image.decode_jpeg(
            file_reader, channels=3, name="jpeg_reader")
    float_caster = tf.cast(image_reader, tf.float32)
    dims_expander = tf.expand_dims(float_caster, 0)
    resized = tf.image.resize_bilinear(dims_expander, [input_height, input_width])
    normalized = tf.divide(tf.subtract(resized, [input_mean]), [input_std])
    sess = tf.Session()
    result = sess.run(normalized)

    return result
if __name__ == "__main__":
    currentDir = getcwd()
    imagesDir = currentDir + "/images"
    modelFile = join(currentDir, "graph_optimized.pb")
    input_layer = "input"
    output_layer = "final_result"

    images = [join(imagesDir, f) for f in listdir(imagesDir) if isfile(join(imagesDir, f))]
    graph = tf.Graph()
    graph_def = tf.GraphDef()

    with open(modelFile, "rb") as f:
        graph_def.ParseFromString(f.read())
    with graph.as_default():
        tf.import_graph_def(graph_def)

    input_name = "import/" + input_layer
    output_name = "import/" + output_layer
    input_operation = graph.get_operation_by_name(input_name)
    output_operation = graph.get_operation_by_name(output_name)
    with tf.Session(graph=graph) as sess:
        start = time.time()
        imageTensor = read_tensor_from_image_file(images[0])
        results = sess.run(output_operation.outputs[0],
                        {input_operation.outputs[0]: imageTensor})
        end=time.time()
        print(end-start)
    with tf.Session(graph=graph) as sess:
        start = time.time()
        for image in images:
            imageTensor = read_tensor_from_image_file(image)
            results = sess.run(output_operation.outputs[0],
                            {input_operation.outputs[0]: imageTensor})
        end=time.time()
        print(end-start)
    print(len(images))

max_stars_repo_name: raulGX/bt-tf-training

max_issues_repo_name: raulGX/bt-tf-training

max_forks_repo_name: raulGX/bt-tf-training


text:
import opengm
import numpy
#---------------------------------------------------------------
# MinSum  with SelfFusion
#---------------------------------------------------------------
numpy.random.seed(42)

gm=opengm.loadGm("/home/tbeier/models/mrf-inpainting/house-gm.h5","gm")
#---------------------------------------------------------------
# Minimize
#---------------------------------------------------------------
#get an instance of the optimizer / inference-algorithm

inf = opengm.inference.CheapInitialization(gm)
inf.infer()
arg = inf.arg()
print gm.evaluate(arg)

infParam = opengm.InfParam(
    numIt=500,
    generator='random'
)
inf=opengm.inference.FusionBased(gm, parameter=infParam)
inf.setStartingPoint(arg)
# start inference (in this case verbose infernce)
visitor=inf.verboseVisitor(printNth=1,multiline=True)
inf.infer(visitor)
arg = inf.arg()

infParam = opengm.InfParam(
    numIt=2000,
    generator='upDown'
)
inf=opengm.inference.FusionBased(gm, parameter=infParam)
inf.setStartingPoint(arg)
# start inference (in this case verbose infernce)
visitor=inf.verboseVisitor(printNth=1,multiline=True)
inf.infer(visitor)
arg = inf.arg()

max_stars_repo_name: jasjuang/opengm

max_issues_repo_name: jasjuang/opengm

max_forks_repo_name: jasjuang/opengm


text:
from __future__ import print_function
import numpy as np
import tensorflow as tf
import argparse
import importlib
import os
from datetime import datetime
import json
import glob

print("Tensorflow version:", tf.__version__)

parser = argparse.ArgumentParser(description='U network for segmentation', formatter_class=argparse.ArgumentDefaultsHelpFormatter)

in_group = parser.add_mutually_exclusive_group(required=True)

in_group.add_argument('--args', help='JSON file with arguments.', type=str)
in_group.add_argument('--json', type=str, help='json file with the description of the inputs, generate it with tfRecords.py')

output_param_group = parser.add_argument_group('Output')
output_param_group.add_argument('--out', help='Output dirname for the model', default="./out")
output_param_group.add_argument('--model', help='Output modelname, the output name will be <out directory>/model-<num step>', default="model")

train_param_group = parser.add_argument_group('Training parameters')
train_param_group.add_argument('--nn', type=str, help='Type of neural network to use', default='u_nn')
train_param_group.add_argument('--keep_prob', help='The probability that each element is kept during training', type=float, default=0.5)
train_param_group.add_argument('--learning_rate', help='Learning rate, default=1e-5', type=float, default=1e-5)
train_param_group.add_argument('--learning_rate_discriminator_mul', help='Factor for the learning rate for the discriminator (to make it slower/faster)', type=float, default=1)
train_param_group.add_argument('--decay_rate', help='decay rate, default=0.96', type=float, default=0.96)
train_param_group.add_argument('--decay_steps', help='decay steps, default=10000', type=int, default=1000)
train_param_group.add_argument('--staircase', help='staircase decay', type=bool, default=False)
train_param_group.add_argument('--batch_size', help='Batch size for evaluation', type=int, default=8)
train_param_group.add_argument('--num_epochs', help='Number of epochs', type=int, default=10)
train_param_group.add_argument('--buffer_size', help='Shuffle buffer size', type=int, default=1000)
train_param_group.add_argument('--ps_device', help='Process device', type=str, default='/cpu:0')
train_param_group.add_argument('--w_device', help='Worker device', type=str, default='/cpu:0')

continue_param_group = parser.add_argument_group('Continue training', 'Use a previously saved model to continue the training.')
continue_param_group.add_argument('--in_model', help='Input model name', default=None)
continue_param_group.add_argument('--in_step', type=int, help='Set the step number to start', default=0)

args = parser.parse_args()

json_filename = args.json
neural_network = args.nn
outvariablesdirname = args.out
modelname = args.model
k_prob = args.keep_prob
learning_rate = args.learning_rate
learning_rate_discriminator_mul = args.learning_rate_discriminator_mul
decay_rate = args.decay_rate
decay_steps = args.decay_steps
staircase = args.staircase
batch_size = args.batch_size
num_epochs = args.num_epochs
buffer_size = args.buffer_size
ps_device = args.ps_device
w_device = args.w_device

in_model = args.in_model
in_step = args.in_step

loss_value_g = 0

if(args.args):
  with open(args.args, "r") as jsf:
    json_args = json.load(jsf)

    json_filename = json_args["json"] if json_args["json"] else args.json
    neural_network = json_args["nn"] if json_args["nn"] else args.nn
    outvariablesdirname = json_args["out"] if json_args["out"] else args.out
    modelname = json_args["model"] if json_args["model"] else args.model
    k_prob = json_args["keep_prob"] if json_args["keep_prob"] else args.keep_prob
    learning_rate = json_args["learning_rate"] if json_args["learning_rate"] else args.learning_rate
    decay_rate = json_args["decay_rate"] if json_args["decay_rate"] else args.decay_rate
    decay_steps = json_args["decay_steps"] if json_args["decay_steps"] else args.decay_steps
    batch_size = json_args["batch_size"] if json_args["batch_size"] else args.batch_size
    num_epochs = json_args["num_epochs"] if json_args["num_epochs"] else args.num_epochs
    buffer_size = json_args["buffer_size"] if json_args["buffer_size"] else args.buffer_size
    ps_device = json_args["ps_device"] if json_args["ps_device"] else args.ps_device
    w_device = json_args["w_device"] if json_args["w_device"] else args.w_device

nn = importlib.import_module("nn." + neural_network).NN()
is_gan = "gan" in neural_network and "gan_encoder" not in neural_network
is_gan_encoder = "gan_encoder" in neural_network
is_ed = "ed_nn" in neural_network

if(in_step > 0):
  nn.set_global_step(in_step)

print('json', json_filename)
print('neural_network', neural_network)
if is_gan:
  print('using gan optimization scheme')
if is_gan_encoder:
  print('using gan_encoder optimization scheme')
print('out', outvariablesdirname)
print('keep_prob', k_prob)
print('learning_rate', learning_rate)
print('decay_rate', decay_rate)
print('decay_steps', decay_steps)
print('batch_size', batch_size)
print('num_epochs', num_epochs)
print('buffer_size', buffer_size)
print('ps_device', ps_device)
print('w_device', w_device)

graph = tf.Graph()

with graph.as_default():

  nn.set_data_description(json_filename=json_filename)
  iterator = nn.inputs(batch_size=batch_size,
    num_epochs=num_epochs,
    buffer_size=buffer_size)

  data_tuple = iterator.get_next()

  keep_prob = tf.placeholder(tf.float32)

  if is_gan:
    # THIS IS THE GAN GENERATION NETWORK SCHEME
    # run the generator network on the 'fake/bad quality' input images (encode/decode)
    gen_x = nn.inference(data_tuple, keep_prob=keep_prob, is_training=True, ps_device=ps_device, w_device=w_device)

    images = data_tuple[0]

    real_y = nn.discriminator(images=images, keep_prob=keep_prob, num_labels=2, is_training=True, ps_device=ps_device, w_device=w_device)
    fake_y = nn.discriminator(images=gen_x, keep_prob=1, num_labels=2, is_training=True, reuse=True, ps_device=ps_device, w_device=w_device)

    # calculate the loss for the fake/generated images
    real_y_ = tf.one_hot(tf.ones([tf.shape(real_y)[0]], tf.int32), 2)
    fake_y_ = tf.one_hot(tf.zeros([tf.shape(fake_y)[0]], tf.int32), 2)

    # calculate the loss for the discriminator
    loss_d_real = nn.loss(real_y, real_y_)
    loss_d_fake = nn.loss(fake_y, fake_y_)
    loss_d = loss_d_real + loss_d_fake

    tf.summary.scalar("loss_d_real", loss_d_real)
    tf.summary.scalar("loss_d_fake", loss_d_fake)
    tf.summary.scalar("loss_d", loss_d)

    # calculate the loss for the generator, i.e., trick the discriminator
    loss_g = nn.loss(fake_y, real_y_)
    tf.summary.scalar("loss_g", loss_g)

    tf.summary.image('generated', gen_x)
    tf.summary.image('real', images)

    # setup the training operations
    with tf.variable_scope("train_discriminator") as scope:
      train_op_d = nn.training(loss_d, learning_rate * learning_rate_discriminator_mul, decay_steps, decay_rate, staircase, var_list=nn.get_discriminator_vars())
    with tf.variable_scope("train_generator") as scope:
      train_op_g = nn.training(loss_g, learning_rate, decay_steps, decay_rate, staircase, var_list=nn.get_generator_vars())

    metrics_eval = nn.metrics(fake_y, real_y)

    summary_op = tf.summary.merge_all()

    with tf.Session() as sess:

      sess.run([tf.global_variables_initializer(), tf.local_variables_initializer()])
      saver = tf.train.Saver()

      if(in_model):
        vars_restore = nn.restore_variables()
        for var in vars_restore:
          print('res', var.name)
        saver_in = tf.train.Saver(vars_restore)
        saver_in.restore(sess, in_model)
      # specify where to write the log files for import to TensorBoard
      now = datetime.now()

      summary_path = os.path.join(outvariablesdirname, modelname + "-" + now.strftime("%Y%m%d-%H%M%S"))
      summary_writer = tf.summary.FileWriter(summary_path, sess.graph)
      outmodelname = summary_path

      sess.run([iterator.initializer])
      step = nn.get_global_step()

      while True:
        try:

          _d, _g, loss_value_d, loss_value_g, loss_value_d_fake, summary, metrics = sess.run([train_op_d, train_op_g, loss_d, loss_g, loss_d_fake, summary_op, metrics_eval], feed_dict={keep_prob: k_prob})

          if step % 100 == 0:
            print('OUTPUT: Step %d: loss_g = %.3f, loss_d = %.3f, loss_d_fake = %.3f' % (step, loss_value_g, loss_value_d, loss_value_d_fake))

            # output some data to the log files for tensorboard
            summary_writer.add_summary(summary, step)
            summary_writer.flush()

            metrics_str = '|'

            for metric in metrics:
              metrics_str += " %s = %.3f |" % (metric, metrics[metric][0])

            print(metrics_str)

            # less frequently output checkpoint files.  Used for evaluating the model
          if step % 1000 == 0:
            save_path = saver.save(sess, os.path.join(outvariablesdirname, modelname), global_step=step)
            print('Saving model:', outmodelname + "-" + str(step))
            with open(os.path.join(outvariablesdirname, modelname + ".json"), "w") as f:
              args_dict = vars(args)
              args_dict["model"] = os.path.basename(outmodelname) + "-" + str(step)
              args_dict["description"] = nn.get_data_description()
              if 'args' in args_dict:
                del args_dict['args']
              f.write(json.dumps(args_dict))

          step += 1

        except tf.errors.OutOfRangeError:
          break

      print('Step:', step)
      print('Saving model:', os.path.join(outvariablesdirname, modelname + ".json"))
      saver.save(sess, outmodelname, global_step=step)

      with open(os.path.join(outvariablesdirname, modelname + ".json"), "w") as f:
        args_dict = vars(args)
        args_dict["model"] = os.path.basename(modelname) + "-" + str(step)
        args_dict["description"] = nn.get_data_description()
        if 'args' in args_dict:
          del args_dict['args']
        f.write(json.dumps(args_dict))

  else:
    # THIS IS THE STANDARD OPIMIZATION SCHEME FOR NETWORKS SUCH AS UNET, CLASSIFICATION OR LABEL MAPS

    if(is_gan_encoder):
      # with tf.variable_scope("encoder"):
      #   with tf.variable_scope("encoder_bn"):
      #     images = tf.layers.batch_normalization(data_tuple[0], training=True, trainable=False)
      images = data_tuple[0]

      encoded_x = nn.inference(images=images, keep_prob=keep_prob, is_training=True, ps_device=ps_device, w_device=w_device)
      y_conv = nn.generator(images=encoded_x, ps_device=ps_device, w_device=w_device)

      loss = nn.loss(y_conv, images, encoded_x)

      tf.summary.scalar("loss", loss)

      train_step = nn.training(loss, learning_rate, decay_steps, decay_rate, staircase)

      metrics_eval = nn.metrics(y_conv, images)

      tf.summary.image('generated', y_conv)
      tf.summary.image('real', images)

    else:
      y_conv = nn.inference(data_tuple, keep_prob=keep_prob, is_training=True, ps_device=ps_device, w_device=w_device)

      loss = nn.loss(y_conv, data_tuple)

      tf.summary.scalar("loss", loss)

      train_step = nn.training(loss, learning_rate, decay_steps, decay_rate, staircase)

      metrics_eval = nn.metrics(y_conv, data_tuple)

      if(is_ed):
        tf.summary.image('generated', y_conv)
        tf.summary.image('real', data_tuple[0])

    summary_op = tf.summary.merge_all()

    with tf.Session() as sess:

      sess.run([tf.global_variables_initializer(), tf.local_variables_initializer()])
      saver = tf.train.Saver(None)

      if(in_model):
        vars_restore = nn.restore_variables()
        for var in vars_restore:
          print('res', var.name)
        saver_in = tf.train.Saver(vars_restore)
        saver_in.restore(sess, in_model)
      # specify where to write the log files for import to TensorBoard
      now = datetime.now()

      summary_path = os.path.join(outvariablesdirname, modelname + "-" + now.strftime("%Y%m%d-%H%M%S"))
      summary_writer = tf.summary.FileWriter(summary_path, sess.graph)
      outmodelname = summary_path

      sess.run([iterator.initializer])
      step = nn.get_global_step()

      while True:
        try:

          _, loss_value, summary, metrics = sess.run([train_step, loss, summary_op, metrics_eval], feed_dict={keep_prob: k_prob})

          if step % 100 == 0:
            print('OUTPUT: Step %d: loss = %.5f' % (step, loss_value))

            # output some data to the log files for tensorboard
            summary_writer.add_summary(summary, step)
            summary_writer.flush()

            metrics_str = '|'

            for metric in metrics:
              metrics_str += " %s = %.3f |" % (metric, metrics[metric][0])

            print(metrics_str)

            # less frequently output checkpoint files.  Used for evaluating the model
          if step % 1000 == 0:
            save_path = saver.save(sess, os.path.join(outvariablesdirname, modelname), global_step=step)
            print('Saving model:', outmodelname + "-" + str(step))
            with open(os.path.join(outvariablesdirname, modelname + ".json"), "w") as f:
              args_dict = vars(args)
              args_dict["model"] = os.path.basename(modelname) + "-" + str(step)
              args_dict["description"] = nn.get_data_description()
              if 'args' in args_dict:
                del args_dict['args']
              f.write(json.dumps(args_dict))

          step += 1

        except tf.errors.OutOfRangeError:
          break

      print('Step:', step)
      print('Saving model:', os.path.join(outvariablesdirname, modelname + ".json"))
      saver.save(sess, outmodelname, global_step=step)

      with open(os.path.join(outvariablesdirname, modelname + ".json"), "w") as f:
        args_dict = vars(args)
        args_dict["model"] = os.path.basename(outmodelname) + "-" + str(step)
        args_dict["description"] = nn.get_data_description()
        if 'args' in args_dict:
          del args_dict['args']
        f.write(json.dumps(args_dict))

max_stars_repo_name: hina-shah/US-famli

max_issues_repo_name: hina-shah/US-famli

max_forks_repo_name: hina-shah/US-famli


text:
from utils import Utilities as utils
import numpy as np
from tqdm import *

##
## @brief      Provides methods to interact with the Recommender Objects in order
##             to retrieve results in the format required by the challenge
##
class RecommenderAdapter(object):

    ##
    ## @brief      Constructs the object.
    ##
    ## @param      self         The object
    ## @param      recommender  (Recommender) An instance of a fitted Recommender Object
    ## @param      dataset      (DatasetPrerocessing) An intance of DatasetPreprocessing Object
    ##
    def __init__(self, recommender, dataset):
        self.recommender = recommender
        self.dataset = dataset
        self.playlists = dataset.get_playlists()
        self.tracks = dataset.get_tracks()
        self.predictions = None

    ##
    ## @brief      Predicts Top N track recomendation for all the requested playlists
    ##
    ## @param      self          The object
    ## @param      to_predict    (Dataframe) A dataframe containing identifiers of the playlists
    ## @param      targets       (Dataframe) A dataframe containing identifiers of possible tracks
    ## @param      top_n         (Integer) The number of items to include in the top predictions
    ## @param      remove_known  (Boolean) Whether to remove known interactions
    ## @param      mask          (Boolean) Whether to remove predictions not in the set of target tracks
    ##
    ## @return     A list of the ids of the top N predicted tracks
    ##
    def predict(self, to_predict, targets, top_n=5, remove_known=True, mask=True):
        targets = targets['track_id'].unique()
        test = to_predict['playlist_id'].unique()
        predictions = []
        for i in tqdm(test):
            playlist_index = utils.get_target_index(i, self.playlists)
            current_ratings = self.recommender.urm[playlist_index, :]
            ratings = self.recommender.predict(current_ratings, remove_known)
            if mask:
                not_selected = np.where(~np.in1d(self.tracks, targets))[0]
                ratings[not_selected] = 0
            top_n_indexes = utils.get_n_best_indexes(ratings, top_n)
            top_n_predictions = self.tracks[top_n_indexes]
            predictions.append((i, top_n_predictions))
        self.predictions = predictions

    ##
    ## @brief      Computed the MAP5 score on the targets
    ##
    ## @param      self     The object
    ## @param      targets  (Dataframe) a dataframe containing future interactions for each playlist
    ##
    ## @return     the MAP5 score
    ##
    def evaluate(self, targets):
        labels = list(targets.groupby('playlist_id', as_index=True).apply(lambda x: list(x['track_id'])))
        predictions = [p[1] for p in sorted(self.predictions, key=lambda x: x[0])]
        return utils.map5(predictions, labels)

    ##
    ## @brief      Computes predictions for a submission
    ##
    ## @param      self  The object
    ##
    ## @return     The predictions for the full dataset
    ##
    def predict_submission(self):
        test = self.dataset.dataset["sample_submission"]
        targets = self.dataset.dataset["target_tracks"]
        self.predict(test, targets)
        return self.predictions

max_stars_repo_name: gmenchetti/Hybrid-Recommender

max_issues_repo_name: gmenchetti/Hybrid-Recommender

max_forks_repo_name: gmenchetti/Hybrid-Recommender


text:
import numpy as np

from .Base_Plotter import Base_Plotter

class F3_Plotter(Base_Plotter):

    def __init__(self, garden, plotaxes=None):
        Base_Plotter.__init__(self, garden, plotaxes)

    def new(self):
        if self._plotaxes is not None:
            self._default_axes()

    def done(self):
        if self._plotaxes is not None:
            self.sync(0.1)

    def bee(self, X):
        if self._plotaxes is not None:
            self._ax.scatter(X[self._plotaxes[0]], X[self._plotaxes[1]], marker='o', c='yellow', edgecolors='black')

    def fly(self, gender, X, old_X, best_X):
        if self._plotaxes is not None:
            if gender == 'M':
                color='red'
            elif gender == 'F':
                color='blue'
            else:
                color='green'

            if best_X is not None:
                self._ax.scatter(best_X[self._plotaxes[0]], best_X[self._plotaxes[1]], marker='*', c=color)
                self._ax.plot([X[self._plotaxes[0]], best_X[self._plotaxes[0]]], [X[self._plotaxes[1]], best_X[self._plotaxes[1]]], ':', marker=None, c=color)

            if old_X is not None:
                self._ax.plot([X[self._plotaxes[0]], old_X[self._plotaxes[0]]], [X[self._plotaxes[1]], old_X[self._plotaxes[1]]], '-', marker=None, c=color)

            self._ax.scatter(X[self._plotaxes[0]], X[self._plotaxes[1]], marker='X', c=color, edgecolors='black')

max_stars_repo_name: FJFranklin/BeesEtAl

max_issues_repo_name: FJFranklin/BeesEtAl

max_forks_repo_name: FJFranklin/BeesEtAl


text:
from numbers import Number
from typing import Tuple, Union

import numpy as np
import pytest

from starfish.io import Stack
from starfish.test.dataset_fixtures import merfish_stack
from starfish.pipeline.filter import gaussian_high_pass

@pytest.mark.parametrize('sigma', (1, (1, 1)))
def test_gaussian_high_pass(merfish_stack: Stack, sigma: Union[Number, Tuple[Number]]):
    """high pass is subtractive, sum of array should be less after running

    Parameters
    ----------
    merfish_stack : Stack
        pytest fixture that exposes a starfish.io.Stack object containing MERFISH testing data
    sigma : Union[int, Tuple[int]]
        the standard deviation of the gaussian kernel
    """
    ghp = gaussian_high_pass.GaussianHighPass(sigma=sigma)
    sum_before = np.sum(merfish_stack.image.numpy_array)
    ghp.filter(merfish_stack.image)
    assert np.sum(merfish_stack.image.numpy_array) < sum_before

@pytest.mark.parametrize('sigma', (1, (1, 1)))
def test_gaussian_high_pass_apply(merfish_stack: Stack, sigma: Union[Number, Tuple[Number]]):
    """same as test_gaussian_high_pass, but tests apply loop functionality"""
    sum_before = np.sum(merfish_stack.image.numpy_array)
    ghp = gaussian_high_pass.GaussianHighPass(sigma=sigma)
    ghp.filter(merfish_stack.image)
    assert np.sum(merfish_stack.image.numpy_array) < sum_before

@pytest.mark.parametrize('sigma', (1, (1, 0, 1)))
def test_gaussian_high_pass_apply_3d(merfish_stack: Stack, sigma: Union[Number, Tuple[Number]]):
    """same as test_gaussian_high_pass, but tests apply loop functionality"""
    sum_before = np.sum(merfish_stack.image.numpy_array)
    ghp = gaussian_high_pass.GaussianHighPass(sigma=sigma, is_volume=True)
    ghp.filter(merfish_stack.image)
    assert np.sum(merfish_stack.image.numpy_array) < sum_before

max_stars_repo_name: Xiaojieqiu/starfish

max_issues_repo_name: Xiaojieqiu/starfish

max_forks_repo_name: Xiaojieqiu/starfish


text:
# MIT License
#
# Copyright (c) 2020 Anderson Vitor Bento
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

import numpy as np
from simublocks.element import Workspace
from simublocks.simulation.tools import simulationTools
from simublocks.simulation.plot import Plot
from simublocks.dialog import Dialog

class Self(simulationTools):

    def __init__(self,T,tf):
        conns = Workspace.connections
        blocks = Workspace.blocks
        s = {
            't': np.arange(0,tf+T,T),
            'T': T,
            'inputs': {},
            'systems': {},
            'functions': {},
            'sums': {}
        }

        try:
            exec(Workspace.importCode,s)
        except Exception as e:
            print(e)
            Dialog.alert("Alert", [
                "Error importing your code",
                str(e)
            ])

        s = self.loadBlocks(s)
        s['graphs'] = Workspace.graphs

        for i in s['inputs']:

            try:
                exec(s['inputs'][i].code,s)
            except Exception as e:
                print(e)
                Dialog.alert("Alert", [
                    "Error in the input block '" + s['inputs'][i].name + "'",
                    str(e)
                ])

            for k in range(len(s['t'])):
                try:
                    s['inputs'][i].input[k] = s[s['inputs'][i].name](s['t'][k],k)
                except Exception as e:
                    print(e)
                    Dialog.alert("Alert", [
                        "Error in the input block '" + s['inputs'][i].name + "'",
                        "Remember that the function and the block must have the same name"
                    ])

        for i in s['functions']:
            try:
                func = s['functions'][i]
                exec(func.code,s)
                s['functions'][i].func = s[func.name]
            except Exception as e:
                print(e)
                Dialog.alert("Alert", [
                    "Error in the function block '" + s['inputs'][i].name + "'",
                    str(e)
                ])

        # Simulation

        for k in range(len(s['t']) -1):

            for i in s['systems']:
                b = s['systems'][i]

                if 'otherblock' in b.conn[0]:
                    first = b.conn[0]['otherblock']
                    _input = self.search(first, k)
                else:
                    _input = 0
                b.x[k+1] = b.ss[0]@b.x[k] + b.ss[1]*_input
                b.y[k] = b.ss[2]@b.x[k] + b.ss[3]*_input

            for i in s['functions']:
                b = s['functions'][i]
                if np.isnan(b.y[k]):
                    if 'otherblock' in b.conn[0]:
                        first = b.conn[0]['otherblock']
                        _input = self.search(first, k)
                    else:
                        _input = 0
                    b.y[k] = b.func(_input)

            for i in s['sums']:
                b = s['sums'][i]
                if np.isnan(b.y[k]):
                    soma = 0
                    if 'otherblock' in b.conn[0]:
                        next0 = b.conn[0]['otherblock']
                        if b.code[0] == "+":  soma += self.search(next0, k)
                        else:  soma-= self.search(next0, k)

                    if 'otherblock' in b.conn[2]:
                        next2 = b.conn[2]['otherblock']
                        if b.code[1] == "+": soma += self.search(next2, k)
                        else: soma-= self.search(next2, k)

                    b.y[k] = soma

        Plot.run(s)

max_stars_repo_name: bentoavb/simublocks

max_issues_repo_name: bentoavb/simublocks

max_forks_repo_name: bentoavb/simublocks


text:
from flask import Flask, render_template, url_for, request, session, redirect, flash
import pickle
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.feature_extraction.text import TfidfTransformer
from sklearn.feature_extraction.text import CountVectorizer
import numpy as np
import os
import pyrebase
import bcrypt
from google_trans_new import google_translator
tr = google_translator()

# from googletrans import Translator
# tr = Translator()

print(os.listdir())
# so in heroku , it is running this file from the root but not from the server folder
# Hence all the paths will have to be from the root instead from the actual server paths
# So all the paths in this code are like that.

# Flask Config
# But, for Flask Paths below, it runs from inside the server folder hence it follows normal path conventions.
application = app = Flask(__name__, static_folder='../client/static',
                          template_folder="../client/templates")

app.config["SECRET_KEY"] = "ursecretkey"

ENV = 'dev'

if ENV == 'dev':
    app.debug = True
else:
    app.debug = False

# Your web app's Firebase configuration
firebaseConfig = {
    # your api config:here
}

# Firebase config
firebase = pyrebase.initialize_app(firebaseConfig)
db = firebase.database()

# Custom stopwords

with open('server/helper/english-new', 'r') as f:
    stop = f.readlines()
    f.close()
stop = [i.rstrip() for i in stop]

def update_in_db(user, sentence, sentiment):
    try:
        user_data = db.child("users").child(str(user)).get()
        for i in user_data.each():
            if(i.key() == 'sentiments'):
                i.val().append([sentence, sentiment])
                val = i.val()
        db.child("users").child(user).update({"sentiments": val})
        return True
    except Exception as e:
        return("Something Went Wrong !!")

def create_user(user, data):
    try:
        db.child("users").child(str(user)).set(data)
        return (f"{user} Created !")
    except Exception as e:
        return("Something Went Wrong !!")

def fetch_data(user):
    user_Data = db.child("users").child(str(user)).get()
    for i in user_Data.each():
        if(i.key() == 'sentiments'):
            ss = i.val()
    del ss[0]
    ss.reverse()
    return ss

@app.route('/')
def index():
    return render_template('log_reg.html', res=3)

@app.route('/landing')
def landing():
    if('uname' not in session):
        return(render_template('log_reg.html'))
    else:
        ss = fetch_data(session['uname'])
        return render_template('index.html', res=3, ss=ss, lenss=len(ss))

@app.route('/detect_sentiment', methods=['GET', 'POST'])
def detect_sentiment():
    if "uname" in session:
        inp = request.form["inp"]
        print(inp)
        # print(stop)
        # eng = tr.translate(inp).text
        eng = tr.translate(inp)
        eng = eng.lower().replace('\W+', " ").replace("'", " ")
        removed_stopword = []
        for word in eng.split():
            if word not in stop:
                removed_stopword.append(word)
        # eng = np.array([" ".join(removed_stopword)])
        eng = [" ".join(removed_stopword)]
        print(eng)
        tfidfconverter = TfidfVectorizer(
            max_features=200, min_df=1, max_df=0.10)
        tfidf_model = pickle.load(open('training/models/tfidf.pkl', 'rb'))
        x = tfidf_model.transform(eng).toarray()
        models = ['lr.pkl', 'dt.pkl', 'gnb.pkl',
                  'knn.pkl', 'rfc.pkl', 'svm.pkl']
        for i in models:
            model = pickle.load(open('training/models/'+i, 'rb'))
            print(i, " ", model.predict(x))
        model = pickle.load(open('training/models/rfc.pkl', 'rb'))
        pred = model.predict(x)
        print(pred)

        res = 0
        if(pred[0] == 1):
            res = 1
            db_pred = "Happy"
        elif(pred[0] == 0):
            res = 0
            db_pred = "Neutral"
        elif(pred[0] == -1):
            res = -1
            db_pred = "Sad"
        update_in_db(session['uname'], str(inp), str(db_pred))
        ss = fetch_data(session['uname'])
        return render_template('index.html', res=res, ss=ss, lenss=len(ss))
    else:
        return(render_template('log_reg.html'))

@app.route('/login', methods=['GET', 'POST'])
def login():
    if(request.method == "POST"):
        uname = request.form['uname']
        passw = request.form['pass']
        if(db.child('users').child(uname).get().val() is not None):

            user_pass = db.child("users").child(
                str(uname)).child("password").get().val()
            if bcrypt.hashpw(passw.encode('utf-8'), bytes(user_pass.replace("b'", "").replace("'", ""), 'utf-8')) == bytes(user_pass.replace("b'", "").replace("'", ""), 'utf-8'):
                session['uname'] = request.form['uname']
                return redirect(url_for('landing'))
            else:
                print("Please enter correct password")
                return(render_template('log_reg.html', err_flag_pass=True))
        else:
            print("User Not found")
            return(render_template('log_reg.html', err_flag_unf=True))
    else:
        return(render_template('log_reg.html'))

@app.route('/register', methods=['GET', 'POST'])
def register():
    if(request.method == "POST"):
        uname = request.form['uname']
        passw = request.form['pass']
        email = request.form['email']
        if(db.child('users').child(uname).get().val() is not None):
            print("User existing Please try something else!")
            return(render_template('log_reg.html', err_flag=True))
        else:
            hashpass = bcrypt.hashpw(
                request.form['pass'].encode('utf-8'), bcrypt.gensalt())
            data = {"email": email, "username": uname, "password": str(
                hashpass), "sentiments": [('Sentences', 'Sentiment_val')]}
            create_user(uname, data)
            return redirect(url_for('login'))
    else:
        return(render_template('log_reg.html'))

@app.route('/logout')
def logout():
    session.pop('uname', None)
    return redirect('/')

if __name__ == '__main__':
    # app.run(debug=True)
    app.run()


text:
from MountainCarEnv import MountainCarEnv
import math
from numpy.random import choice
import numpy as np
from functools import reduce
from operator import add
import sys
import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
from torch.autograd import Variable

def h(s, a, theta):
    return theta[0]*s[0] + theta[1]*s[1] + theta[2]*a

def pi_softmax (s, current_action, theta, action_space):
    d = 0
    for a in range(action_space.n):
        d += math.e**h(s,a,theta)
    return math.e**h(s,current_action,theta) / d

# implementation of the algorithm
def REINFORCE(n_episodes, pi, env, gamma=1, alpha=0.1, max_steps=1000):
    theta = (0,0,0)
    possible_actions = np.array(range(env.action_space.n))
    for _ in range(n_episodes):
        env.reset()
        episode = []
        for _ in range(max_steps):
            env.render()
            prev_state = env.state
            probabilities = pi(env.state, possible_actions, theta, env.action_space)
            action = choice(possible_actions, p=probabilities)
            new_state, reward, done, _ = env.step(action)
            episode.append((prev_state, action, reward, new_state))
            if done:
                break
        # for t, step in enumerate(episode):
        #     G = reduce(lambda acc, t: gamma*(acc + t[1]), episode, 0)
        #     theta += alpha*(gamma**t)*G*
        print (episode)
    env.close()

env = MountainCarEnv()
REINFORCE(1, pi_softmax, env)

# print (env.action_space.n)
# env.reset()
# a = np.array(range(env.action_space.n))
# print(a)
# probs = pi_softmax(env.state, a, (0,0,1), env.action_space)
# print(probs)
# print(choice(a, p=probs))
# print (env.step(1))

reason: import numpy,from numpy


text:
import ntpath
import sys
# resolve module import error in PyCharm
sys.path.append(ntpath.dirname(ntpath.dirname(ntpath.abspath(__file__))))

# 匯入必要套件
import argparse
import time

import cv2
import imutils
import numpy as np
from imutils import build_montages
from matplotlib import pyplot as plt
from skimage.exposure import rescale_intensity
from sklearn.decomposition import PCA
from sklearn.metrics import classification_report
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder
from sklearn.svm import SVC

from dataset.load_dataset import images_to_faces

def main():
    # 初始化arguments
    ap = argparse.ArgumentParser()
    ap.add_argument("-i", "--input", type=str, required=True, help="the input dataset path")
    ap.add_argument("-n", "--components", type=int, default=25, help="number of components")
    args = vars(ap.parse_args())

    print("[INFO] loading dataset....")
    (faces, labels) = images_to_faces(args["input"])
    print(f"[INFO] {len(faces)} images in dataset")

    # 進行主成分分析時需要將資料轉成一維陣列
    pca_faces = np.array([face.flatten() for face in faces])

    # 將名稱從字串轉成整數 (在做訓練時時常會用到這個方法：label encoding)
    le = LabelEncoder()
    labels = le.fit_transform(labels)

    # 將資料拆分訓練用與測試用；測試資料佔總資料1/4 (方便後續我們判斷這個方法的準確率)
    split = train_test_split(faces, pca_faces, labels, test_size=0.25, stratify=labels, random_state=9527)
    (oriTrain, oriTest, trainX, testX, trainY, testY) = split

    print("[INFO] creating eigenfaces...")
    pca = PCA(svd_solver="randomized", n_components=args["components"], whiten=True)
    start = time.time()
    pca_trainX = pca.fit_transform(trainX)
    end = time.time()
    print(f"[INFO] computing eigenfaces for {round(end - start, 2)} seconds")

    # 確認使用的主成分可以解釋多少資料的變異
    cum_ratio = np.cumsum(pca.explained_variance_ratio_)
    plt.plot(cum_ratio)
    plt.xlabel("number of components")
    plt.ylabel("cumulative explained variance")
    plt.draw()
    plt.waitforbuttonpress(0)

    # 來用"圖像"看一下PCA的結果
    vis_images = []
    for (i, component) in enumerate(pca.components_):
        component = component.reshape((50, 50))
        component = rescale_intensity(component, out_range=(0, 255))
        component = np.dstack([component.astype("uint8")] * 3)
        vis_images.append(component)
    montage = build_montages(vis_images, (50, 50), (5, 5))[0]
    montage = imutils.resize(montage, width=250)

    mean = pca.mean_.reshape((50, 50))
    mean = rescale_intensity(mean, out_range=(0, 255)).astype("uint8")
    mean = imutils.resize(mean, width=250)

    cv2.imshow("Mean", mean)
    cv2.imshow("Components", montage)
    cv2.waitKey(0)

    # 建立SVM模型來訓練
    model = SVC(kernel="rbf", C=10.0, gamma=0.001, random_state=9527)
    model.fit(pca_trainX, trainY)

    # 驗證模型的準確度 (記得將測試資料轉成PCA的格式)
    pca_testX = pca.transform(testX)
    predictions = model.predict(pca_testX)
    print(classification_report(testY, predictions, target_names=le.classes_))

    # Optional: 你也可以直接用OpenCV內建的模型來訓練；下面的程式碼可以取代前面的SVM模型訓練
    # recognizer = cv2.face_EigenFaceRecognizer().create(num_components=args["components"])
    # recognizer.train(trainX, trainY)
    # predictions = []
    # for i in range(0, len(testX)):
    #     (prediction, _) = recognizer.predict(testX[i])
    #     predictions.append(prediction)
    # print(classification_report(testY, predictions, target_names=le.classes_))

    # 隨機挑選測試資料來看結果
    idxs = np.random.choice(range(0, len(testY)), size=10, replace=False)
    for i in idxs:
        predName = le.inverse_transform([predictions[i]])[0]
        actualName = le.classes_[testY[i]]

        face = np.dstack([oriTest[i]] * 3)
        face = imutils.resize(face, width=250)

        cv2.putText(face, f"pred:{predName}", (5, 25), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 255, 0), 2)
        cv2.putText(face, f"actual:{actualName}", (5, 60), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 255), 2)

        print(f"[INFO] prediction: {predName}, actual: {actualName}")
        cv2.imshow("Face", face)
        cv2.waitKey(0)
    plt.close()

if __name__ == '__main__':
    main()

max_stars_repo_name: saivirtue/easy-eye-app

max_issues_repo_name: saivirtue/easy-eye-app

max_forks_repo_name: saivirtue/easy-eye-app


text:
import numpy as np
from wire import Wire

class Wirepath:
    # This is not the best performing solution ;)
    PATH_SIZE = (20000, 20000)
    PATH_ZERO = (10000, 10000)

    grid = []
    _current_row = PATH_ZERO[0]
    _current_col = PATH_ZERO[1]

    def set_wires(self, instructions: list):
        for instruction in instructions:
            wire = Wire(instruction)
            self._set_wire_points(wire)

    def _set_wire_points(self, wire: Wire):
        for _ in range(wire.size):
            if wire.direction == "U":
                self._current_row -= 1
            elif wire.direction == "D":
                self._current_row += 1
            elif wire.direction == "L":
                self._current_col -= 1
            elif wire.direction == "R":
                self._current_col += 1
            else:
                print("Error. Unknown direction", wire.direction, "skipping wire")
                return

            self.grid[self._current_row, self._current_col] = 1

    def __init__(self) -> None:
        self.grid = np.zeros([self.PATH_SIZE[0], self.PATH_SIZE[1]], dtype=int)
        super().__init__()


text:
#
#   RawIO
#   Copyright (c) 2021 Yusuf Olokoba.
#

from cv2 import drawMatches
from imageio import imwrite
from numpy import asarray
from pathlib import Path
from PIL import Image
from pytest import fixture, mark

from rawio.grouping import group_exposures, timestamp_similarity, wavelet_similarity

@fixture
def similarity_fn ():
    timestamp_fn = timestamp_similarity()
    wavelet_fn = wavelet_similarity(hamming_threshold=0.16)
    def fn (path_a, path_b):
        if timestamp_fn(path_a, path_b):
            return True
        if wavelet_fn(path_a, path_b):
            return True
        return False
    return fn

def test_group_cascade_ambient_a (similarity_fn): # fails, on brightest exposure
    exposure_paths = [
        "test/media/group/6.jpg",
        "test/media/group/7.jpg",
        "test/media/group/8.jpg",
        "test/media/group/9.jpg",
        "test/media/group/10.jpg",
    ]
    groups = group_exposures(exposure_paths, similarity_fn)
    assert len(groups) == 1 and len(groups[0]) == 5

def test_group_cascade_ambient_b (similarity_fn):
    exposure_paths = [
        "test/media/group/14.jpg",
        "test/media/group/15.jpg",
        "test/media/group/16.jpg",
    ]
    groups = group_exposures(exposure_paths, similarity_fn)
    assert len(groups) == 1 and len(groups[0]) == 3

def test_group_cascade_flash_a (similarity_fn):
    exposure_paths = [
        "test/media/group/1.jpg",
        "test/media/group/2.jpg",
        "test/media/group/3.jpg",
        "test/media/group/4.jpg",
        "test/media/group/5.jpg",
    ]
    groups = group_exposures(exposure_paths, similarity_fn)
    assert len(groups) == 1 and len(groups[0]) == 5

def test_group_cascade_flash_b (similarity_fn):
    exposure_paths = [
        "test/media/group/17.jpg",
        "test/media/group/18.jpg",
    ]
    groups = group_exposures(exposure_paths, similarity_fn)
    assert len(groups) == 1 and len(groups[0]) == 2

def test_group_cascade_flash_c (similarity_fn): # fails, between dark and flash exposures
    exposure_paths = [
        "test/media/group/11.jpg",
        "test/media/group/12.jpg",
        "test/media/group/13.jpg",
    ]
    groups = group_exposures(exposure_paths, similarity_fn)
    assert len(groups) == 1 and len(groups[0]) == 3

def test_group_cascade_aerial (similarity_fn):
    exposure_paths = [
        "test/media/group/19.jpg",
        "test/media/group/20.jpg",
        "test/media/group/21.jpg",
        "test/media/group/22.jpg",
        "test/media/group/23.jpg",
        "test/media/group/24.jpg",
    ]
    groups = group_exposures(exposure_paths, similarity_fn)
    assert len(groups) == 2 and all([len(group) == 3 for group in groups])

def test_group_cascade_full_shoot (similarity_fn):
    #exposure_paths = [str(path) for path in (Path.home() / "Desktop" / "Margaret" / "brackets").glob("*.jpg")]
    exposure_paths = [str(path) for path in (Path.home() / "Downloads").glob("*.jpg")]
    groups = group_exposures(exposure_paths, similarity_fn)
    print(len(groups))

max_stars_repo_name: hdkai/Raw-IO

max_issues_repo_name: hdkai/Raw-IO

max_forks_repo_name: hdkai/Raw-IO

reason: from numpy