text:
import pandas as pd              # Пакет для работы с таблицами
import numpy as np               # Пакет для работы с векторами и матрицами

# Из библиотеки для работы с текстами вытащим
# методы для  предобработки и модели
from gensim import corpora, models
from gensim.models.callbacks import PerplexityMetric

# Пара дополнительных комбинаторных штук для картинок
from collections import defaultdict
import itertools

# Пакет, чтобы делать глубокие копии
import copy

# Пакет для работы со специфичными для питона данными
import pickle

# Косинусная метрика для рассчёта расстояний
from scipy.spatial.distance import cosine

import json
import pickle

path = '/Users/dmitrys/Yandex.Disk.localized/top_russian_music/comments/vk/'

print("Reading data")

with open(path + "vk_comments_full_1", 'rb') as f:
    comments = pickle.load(f)

with open(path + "vk_comments_full_2", 'rb') as f:
    comments.extend(pickle.load(f))

data = pd.DataFrame(comments)
print("Data shape:",data.shape)

data['text_bow'] = data['text'].str.split()

data['comment_len'] = data['text_bow'].apply(len)

data = data[data['comment_len']>4]
print("Reduced data shape:",data.shape)
data.to_csv("reduced_comments.csv")

ext: py

max_stars_repo_name: DmitrySerg/top-russian-music

max_issues_repo_name: DmitrySerg/top-russian-music

max_forks_repo_name: DmitrySerg/top-russian-music

reason: import numpy,from scipy


text:
__author__ = 'Varun Nayyar'

import numpy as np
from sklearn.metrics import confusion_matrix

from Utils.MFCCArrayGen import speakers

# emotions = origEmotions[1:] + [origEmotions[0]]
emotions = ["Bored", "Happy", "HotAnger", "Neutral", "Sad"]

# from fullDataResuMC import trueDict, predDict
from deciDataResMC import trueDict, predDict

def main():
    print emotions

    cmTotal = np.zeros((len(emotions), len(emotions)))

    for speakerID in speakers:

        try:
            cm = confusion_matrix(trueDict[speakerID], predDict[speakerID], labels = emotions)
            cmTotal+=cm
            print speakerID
            print cm
            print (100.0*cm.T/cm.sum(1)).T.round(2)
        except KeyError:
            pass

    print emotions
    print cmTotal
    cmtotper =  (100.0*cmTotal.T/cmTotal.sum(1)).T.round(2)
    print cmtotper
    print np.diag(cmtotper).mean()

if __name__ == "__main__":
    main()

max_stars_repo_name: nayyarv/MonteGMM

max_issues_repo_name: nayyarv/MonteGMM

max_forks_repo_name: nayyarv/MonteGMM

reason: import numpy


text:
"""
An implementation of AI vs AI pong in python
used for reference in writing the C code, and
for easier visualization.
"""
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation

xlim = [-82, 82]
ylim = [-35, 35]
paddle_width = 20
origin = np.array([82, 35])

def draw(ball_position, player_positions):
    b_x, b_y = ball_position + origin
    p_pos = player_positions + origin[1]
    board = np.zeros((71, 165), dtype=np.int8)

    # draw ball as a 3 by 3 grid
    board[b_y-1:b_y+2, b_x-1:b_x+2] = 1

    # draw players as 1 by 14
    pw = paddle_width // 2
    board[p_pos[0]-pw:p_pos[0]+pw+1,  0] = 1
    board[p_pos[1]-pw:p_pos[1]+pw+1, -1] = 1
    return board

def scored():
    board = np.ones((71, 165), dtype=np.int8)
    return [board, 0*board]

def update(ball_position, p_pos, ball_velocity):
    b_x, b_y = ball_position
    v_x, v_y = ball_velocity

    # update the ball
    if b_y + v_y >= ylim[1]:
        # reflect at the top
        b_y = ylim[1]
        v_y *= -1
    elif b_y + v_y <= ylim[0]:
        # reflect at the bottom
        b_y = ylim[0]
        v_y *= -1
    else:
        b_y += v_y
    if b_x + v_x >= xlim[1]:
        # reflect at the right
        b_x = xlim[1]
        v_x *= -1
    elif b_x + v_x <= xlim[0]:
        # reflect at the bottom
        b_x = xlim[0]
        v_x *= -1
    else:
        b_x += v_x

    p_pos[np.logical_and(p_pos > b_y, p_pos > (ylim[0]+paddle_width//2))] -= 1
    p_pos[np.logical_and(p_pos < b_y, p_pos < (ylim[1]-paddle_width//2))] += 1

    has_scored = (False, 0)  # has someone scored, who

    return np.array([b_x, b_y]), p_pos, np.array([v_x, v_y]), \
        has_scored

def main():
    frames = []  # a list of every produced frame.

    num_score_frames = 6

    scores = [np.array([0, 0])]  # the score of player 0 and 1 respectively
    ball_position = np.array([0, 0])
    ball_velocity = np.array([1, 1])

    player_positions = np.array([0, 0])  # y coordinates

    for _ in range(1000):
        frames.append(draw(ball_position, player_positions))
        ball_position, player_positions, ball_velocity, has_scored = update(
            ball_position, player_positions, ball_velocity)

        # update scores
        if has_scored[0]:
            frames += scored()*num_score_frames
            score_inc = np.zeros((2,))
            score_inc[has_scored[0]] = 1
            scores += [scores[-1]+score_inc]*num_score_frames*2
        else:
            scores += [scores[-1]]

    # initial plots
    im = plt.imshow(frames[0])
    x = np.array([-82, -27, 27, 82])+origin[0]
    y = np.array([35, 10, 10, 35])
    plt.plot(x, y+origin[1], 'r')
    plt.plot(x, -y+origin[1], 'r')

    def animate(i):
        plt.title(f"{scores[i][0]} | {scores[i][1]}")
        im.set_array(frames[i])
        return im,

    anim = FuncAnimation(plt.gcf(), animate, frames=len(frames), interval=30)
    plt.show()

if __name__ == '__main__':
    main()

max_stars_repo_name: t-makaro/pong-tie

max_issues_repo_name: t-makaro/pong-tie

max_forks_repo_name: t-makaro/pong-tie


text:
from __future__ import print_function
import gym
import random
from gym import spaces
import numpy as np
from keras.datasets import cifar10, mnist, cifar100
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten
from keras.layers import Convolution2D, MaxPooling2D
from keras.optimizers import SGD
from keras.utils import np_utils
from keras.regularizers import WeightRegularizer
from keras import backend as K

from itertools import cycle
import math

class CNNClassifierTraining(gym.Env):
    """Environment where agent learns to select training parameters and
    architecture of a deep convolutional neural network

    Training parameters that the agent can adjust are learning
    rate, learning rate decay, momentum, batch size, L1 / L2 regularization.

    Agent can select up to 5 cnn layers and up to 2 fc layers.

    Agent is provided with feedback on validation accuracy, as well as on
    the size of a dataset.
    """

    metadata = {"render.modes": ["human"]}

    def __init__(self, natural=False):
        """
        Initialize environment
        """

        # I use array of len 1 to store constants (otherwise there were some errors)
        self.action_space = spaces.Tuple((
            spaces.Box(-5.0, 0.0, 1),  # learning rate
            spaces.Box(-7.0, -2.0, 1),  # decay
            spaces.Box(-5.0, 0.0, 1),  # momentum
            spaces.Box(2, 8, 1),  # batch size
            spaces.Box(-6.0, 1.0, 1),  # l1 reg
            spaces.Box(-6.0, 1.0, 1),  # l2 reg
            spaces.Box(0.0, 1.0, (5, 2)),  # convolutional layer parameters
            spaces.Box(0.0, 1.0, (2, 2)),  # fully connected layer parameters
        ))

        # observation features, in order: num of instances, num of labels,
        # validation accuracy after training with given parameters
        self.observation_space = spaces.Box(-1e5, 1e5, 2)  # validation accuracy

        # Start the first game
        self._reset()

    def _step(self, action):
        """
        Perform some action in the environment
        """
        assert (self.action_space.contains(action))

        lr, decay, momentum, batch_size, l1, l2, convs, fcs = action

        # map ranges of inputs
        lr = (10.0 ** lr[0]).astype('float32')
        decay = (10.0 ** decay[0]).astype('float32')
        momentum = (10.0 ** momentum[0]).astype('float32')

        batch_size = int(2 ** batch_size[0])

        l1 = (10.0 ** l1[0]).astype('float32')
        l2 = (10.0 ** l2[0]).astype('float32')

        """
        names = ["lr", "decay", "mom", "batch", "l1", "l2"]
        values = [lr, decay, momentum, batch_size, l1, l2]

        for n,v in zip(names, values):
            print(n,v)
        """

        diverged, acc = self.train_blueprint(lr, decay, momentum, batch_size, l1, l2, convs, fcs)

        # save best validation. If diverged, acc is zero
        if acc > self.best_val:
            self.best_val = acc

        self.previous_acc = acc

        self.epoch_idx += 1
        done = self.epoch_idx == 10

        reward = self.best_val

        # as for number of labels increases, learning problem becomes
        # more difficult for fixed dataset size. In order to avoid
        # for the agent to ignore more complex datasets, on which
        # accuracy is low and concentrate on simple cases which bring bulk
        # of reward, reward is normalized by number of labels in dataset
        reward *= self.nb_classes

        # formula below encourages higher best validation
        reward += reward ** 2

        return self._get_obs(), reward, done, {}

    def _render(self, mode="human", close=False):

        if close:
            return

        print(">> Step ", self.epoch_idx, "best validation:", self.best_val)

    def _get_obs(self):
        """
        Observe the environment. Is usually used after the step is taken
        """
        # observation as per observation space
        return np.array([self.nb_inst,
                         self.previous_acc])

    def data_mix(self):

        # randomly choose dataset
        dataset = random.choice(['mnist', 'cifar10', 'cifar100'])  #

        n_labels = 10

        if dataset == "mnist":
            data = mnist.load_data()

        if dataset == "cifar10":
            data = cifar10.load_data()

        if dataset == "cifar100":
            data = cifar100.load_data()
            n_labels = 100

        # Choose dataset size. This affects regularization needed
        r = np.random.rand()

        # not using full dataset to make regularization more important and
        # speed up testing a little bit
        data_size = int(2000 * (1 - r) + 40000 * r)

        # I do not use test data for validation, but last 10000 instances in dataset
        # so that trained models can be compared to results in literature
        (CX, CY), (CXt, CYt) = data

        if dataset == "mnist":
            CX = np.expand_dims(CX, axis=1)

        data = CX[:data_size], CY[:data_size], CX[-10000:], CY[-10000:]

        return data, n_labels

    def _reset(self):

        self.generate_data()

        # initial accuracy values
        self.best_val = 0.0
        self.previous_acc = 0.0
        self.epoch_idx = 0

        return self._get_obs()

    def generate_data(self):
        self.data, self.nb_classes = self.data_mix()
        # zero index corresponds to training inputs
        self.nb_inst = len(self.data[0])

    def train_blueprint(self, lr, decay, momentum, batch_size, l1, l2, convs, fcs):

        X, Y, Xv, Yv = self.data
        nb_classes = self.nb_classes

        reg = WeightRegularizer()

        # a hack to make regularization variable
        reg.l1 = K.variable(0.0)
        reg.l2 = K.variable(0.0)

        # input square image dimensions
        img_rows, img_cols = X.shape[-1], X.shape[-1]
        img_channels = X.shape[1]

        # convert class vectors to binary class matrices
        Y = np_utils.to_categorical(Y, nb_classes)
        Yv = np_utils.to_categorical(Yv, nb_classes)

        # here definition of the model happens
        model = Sequential()

        has_convs = False
        # create all convolutional layers
        for val, use in convs:

            # Size of convolutional layer
            cnvSz = int(val * 127) + 1

            if use < 0.5:
                continue
            has_convs = True
            model.add(Convolution2D(cnvSz, 3, 3, border_mode='same',
                                    input_shape=(img_channels, img_rows, img_cols),
                                    W_regularizer=reg,
                                    b_regularizer=reg))
            model.add(Activation('relu'))

            model.add(MaxPooling2D(pool_size=(2, 2)))
            # model.add(Dropout(0.25))

        if has_convs:
            model.add(Flatten())
        else:
            model.add(Flatten(input_shape=(img_channels, img_rows, img_cols)))  # avoid excetpions on no convs

        # create all fully connected layers
        for val, use in fcs:

            if use < 0.5:
                continue

            # choose fully connected layer size
            densesz = int(1023 * val) + 1

            model.add(Dense(densesz,
                            W_regularizer=reg,
                            b_regularizer=reg))
            model.add(Activation('relu'))
            # model.add(Dropout(0.5))

        model.add(Dense(nb_classes,
                        W_regularizer=reg,
                        b_regularizer=reg))
        model.add(Activation('softmax'))

        # let's train the model using SGD + momentum (how original).
        sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
        model.compile(loss='categorical_crossentropy',
                      optimizer=sgd,
                      metrics=['accuracy'])

        X = X.astype('float32')
        Xv = Xv.astype('float32')
        X /= 255
        Xv /= 255

        model = model
        sgd = sgd
        reg = reg

        # set parameters of training step

        sgd.lr.set_value(lr)
        sgd.decay.set_value(decay)
        sgd.momentum.set_value(momentum)

        reg.l1.set_value(l1)
        reg.l2.set_value(l2)

        # train model for one epoch_idx
        H = model.fit(X, Y,
                      batch_size=int(batch_size),
                      nb_epoch=10,
                      shuffle=True)

        diverged = math.isnan(H.history['loss'][-1])
        acc = 0.0

        if not diverged:
            _, acc = model.evaluate(Xv, Yv)

        return diverged, acc


text:
import os
import numpy as np

from wzk.mpl.backend import plt
from wzk.mpl.move_figure import move_fig

from wzk.math2 import get_mean_divisor_pair, golden_ratio
from wzk.dicts_lists_tuples import atleast_tuple

ieee1c = [3+1/2, (3+1/2)/golden_ratio]
ieee2c = [7+1/16, (7+1/12)/golden_ratio]

def figsize_wrapper(width, height=None, height_ratio=1/golden_ratio):
    # https://www.ieee.org/content/dam/ieee-org/ieee/web/org/pubs/eic-guide.pdf
    if isinstance(width, str):
        if width.lower() == 'ieee1c':
            width = ieee1c[0]
        elif width.lower() == 'ieee2c':
            width = ieee2c[0]
        else:
            raise ValueError

    elif isinstance(width, (float, int)):
        pass
    else:
        raise ValueError

    height = width * height_ratio if height is None else height

    return (width, height)

def new_fig(*, width=ieee2c[0], height=None, h_ratio=1/golden_ratio,
            n_dim=2,
            n_rows=1, n_cols=1,
            share_x='none', share_y='none',  # : bool or {'none', 'all', 'row', 'col'},
            aspect='auto',
            title=None,
            position=None, monitor=-1,
            **kwargs):

    fig = plt.figure(figsize=figsize_wrapper(width=width, height=height, height_ratio=h_ratio), **kwargs)

    if n_dim == 2:
        ax = fig.subplots(nrows=n_rows, ncols=n_cols, sharex=share_x, sharey=share_y)

        if isinstance(ax, np.ndarray):
            for i in np.ndindex(np.shape(ax)):
                ax[i].set_aspect(aspect)  # Not implemented for 3D
        else:
            ax.set_aspect(aspect)

    else:
        import mpl_toolkits.mplot3d.art3d as art3d  # noqa: F401 unused import
        ax = fig.gca(projection='3d')

    if title is not None:
        fig.suptitle(title)

    move_fig(fig=fig, position=position, monitor=monitor)
    return fig, ax

def save_fig(filename=None, fig=None, formats=('png',),
             dpi=600, bbox='tight', pad=0.1,
             save=True, replace=True, view=False, copy2cb=False,
             verbose=1, **kwargs):
    """
    Adaption of the matplotlib 'savefig' function with some added convenience.
    bbox = tight / standard (standard does not crop but saves the whole figure)
    pad: padding applied to the thigh bounding box in inches
    """

    if not save:
        return

    if fig is None:
        fig = plt.gcf()

    if filename is None:
        filename = get_fig_suptitle(fig=fig)

    dir_name = os.path.dirname(filename)
    if dir_name != '':
        safe_create_directory(directory=dir_name)

    formats = atleast_tuple(formats, convert=False)
    for f in formats:
        file = f'{filename}.{f}'

        if replace or not os.path.isfile(path=file):
            fig.savefig(file, format=f, bbox_inches=bbox, pad_inches=pad,  dpi=dpi, **kwargs)
            if verbose >= 1:
                print(f'{file} saved')
        else:
            print(f'{file} already exists')

    if view:
        start_open(file=f"{filename}.{formats[0]}")

    if copy2cb:
        copy2clipboard(file=f"{filename}.{formats[0]}")

def save_all(directory=None, close=False, **kwargs):
    if directory is None:
        directory = input('Directory to which the figures should be saved:')

    fig_nums = plt.get_fignums()
    for n in fig_nums:
        fig = plt.figure(num=n)
        title = get_fig_suptitle(fig=fig)
        title = "" if title is None else title
        save_fig(filename=f"{directory}N{n}_{title}", fig=fig, **kwargs)

    if close:
        close_all()

def get_fig_suptitle(fig):
    try:
        return fig._suptitle._text
    except AttributeError:
        return ''

def close_all():
    plt.close('all')

def test_pdf2latex():
    fig, ax = new_fig(scale=0.5)
    ax.plot(np.random.random(20))
    ax.set_xlabel('Magnetization')
    save_fig(filename='/Users/jote/Documents/Vorlagen/LaTeX Vorlagen/IEEE-open-journal-template/aaa', fig=fig,
             formats='pdf')

def subplot_grid(n, squeeze=False, **kwargs):
    n_rows, n_cols = get_mean_divisor_pair(n)

    if n >= 7 and n_rows == 1:
        n_rows, n_cols = get_mean_divisor_pair(n+1)

    _, ax = new_fig(n_rows=n_rows, n_cols=n_cols, **kwargs)

    if not squeeze:
        ax = np.atleast_2d(ax)
    return ax


text:
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np
import k_means as km

IMAGE = mpimg.imread('image.jpg')  # pick up the image as a matrix of pixel
pixels = np.concatenate(IMAGE[:][:])  # all the pixels of the image in an array

EPSILON = 1000  # to test convergence
K_TO_TEST = np.array([1, 2, 3, 4])  # k to test have to be at number of 4 with this script !!

fig = plt.figure(figsize=(8, 8))

print(np.unique(pixels, axis=0).shape[0])  # number of different colors on the image
for i, k in enumerate(K_TO_TEST):
    iteration = 0

    centroids = np.random.rand(k, 3) * 256
    # Initialize a value to keep the last cost value to know when there is a convergence
    last_cost = 0
    cost = EPSILON + 1  # make sure to start the while loop
    # Update the centroid while not convergence
    while not abs(cost - last_cost) < EPSILON:
        print('iteration  {0}... '.format(iteration + 1))
        # keep the current cost before the adjustments to know if there is a convergence
        last_cost = cost
        # pick up the nearest centroid indexes of each samples
        nearest = km.nearest_centroid(pixels, centroids)
        # adjust the current centroids
        k_centroids = km.adjust_centroid(pixels, nearest, centroids)
        # calculation of the current cost
        cost = km.calculate_cost(pixels, centroids)
        print('cost : {0}'.format(cost))
        print('centroids : \n{0}'.format(centroids))
        iteration += 1
    # reassociate each color to its nearest centroid
    nearest = km.nearest_centroid(pixels, centroids)
    # create the pixel array
    new_image = np.array([centroids[number] for _, number in enumerate(nearest)])
    # reform the image
    new_image = new_image.reshape(IMAGE.shape).astype(int)
    # plot it
    fig.add_subplot(2, 2, i + 1)
    plt.imshow(new_image)

plt.show()

max_stars_repo_name: AxelThevenot/K-Means

max_issues_repo_name: AxelThevenot/K-Means

max_forks_repo_name: AxelThevenot/K-Means


text:
#
# PixTable.py -- Pixel Table plugin for Ginga reference viewer
#
# This is open-source software licensed under a BSD license.
# Please see the file LICENSE.txt for details.
#
import numpy

from ginga.gw import Widgets, Viewers
from ginga import GingaPlugin, colors

class PixTable(GingaPlugin.LocalPlugin):

    def __init__(self, fv, fitsimage):
        # superclass defines some variables for us, like logger
        super(PixTable, self).__init__(fv, fitsimage)

        self.layertag = 'pixtable-canvas'
        self.pan2mark = False

        self.dc = self.fv.getDrawClasses()
        canvas = self.dc.DrawingCanvas()
        canvas.set_callback('cursor-down', self.btndown_cb)
        canvas.set_callback('none-move', self.motion_cb)
        canvas.setSurface(self.fitsimage)
        self.canvas = canvas

        # For pixel table
        self.pixtbl_radius = 2
        self.txt_arr = None
        self.sum_arr = None
        self.sizes = [ 1, 2, 3, 4 ]
        self.maxdigits = 9
        self.lastx = 0
        self.lasty = 0
        self.font = 'fixed'
        self.fontsize = 14
        self.row_ht = self.fontsize + 2

        # For "marks" feature
        self.mark_radius = 10
        self.mark_style = 'cross'
        self.mark_color = 'purple'
        self.select_color = 'cyan'
        self.marks = ['None']
        self.mark_index = 0
        self.mark_selected = None
        self.tw = None

    def build_gui(self, container):
        top = Widgets.VBox()
        top.set_border_width(4)

        vbox, sw, orientation = Widgets.get_oriented_box(container,
                                                         fill=True)
        vbox.set_border_width(4)
        vbox.set_spacing(2)

        self.msgFont = self.fv.getFont("sansFont", 12)
        tw = Widgets.TextArea(wrap=True, editable=False)
        tw.set_font(self.msgFont)
        self.tw = tw

        fr = Widgets.Expander("Instructions")
        fr.set_widget(tw)
        vbox.add_widget(fr, stretch=0)

        fr = Widgets.Frame("Pixel Values")

        # We just use a ginga widget to implement the pixtable
        pixview = Viewers.CanvasView(logger=self.logger)
        width, height = 300, 300
        pixview.set_desired_size(width, height)
        bg = colors.lookup_color('#202030')
        pixview.set_bg(*bg)

        bd = pixview.get_bindings()

        self.pixview = pixview
        self.pix_w = Widgets.wrap(pixview.get_widget())
        self.pix_w.resize(width, height)
        fr.set_widget(self.pix_w)
        vbox.add_widget(fr, stretch=1)

        self._rebuild_table()

        btns = Widgets.HBox()
        btns.set_border_width(4)
        btns.set_spacing(4)

        cbox1 = Widgets.ComboBox()
        index = 0
        for i in self.sizes:
            j = 1 + i*2
            name = "%dx%d" % (j, j)
            cbox1.append_text(name)
            index += 1
        index = self.sizes.index(self.pixtbl_radius)
        cbox1.set_index(index)
        cbox1.add_callback('activated', self.set_cutout_size_cb)
        cbox1.set_tooltip("Select size of pixel table")
        btns.add_widget(cbox1, stretch=0)

        # control for selecting a mark
        cbox2 = Widgets.ComboBox()
        for tag in self.marks:
            cbox2.append_text(tag)
        if self.mark_selected is None:
            cbox2.set_index(0)
        else:
            cbox2.show_text(self.mark_selected)
        cbox2.add_callback('activated', self.mark_select_cb)
        self.w.marks = cbox2
        cbox2.set_tooltip("Select a mark")
        #cbox2.setMinimumContentsLength(8)
        btns.add_widget(cbox2, stretch=0)

        btn1 = Widgets.Button("Delete")
        btn1.add_callback('activated', lambda w: self.clear_mark_cb())
        btn1.set_tooltip("Delete selected mark")
        btns.add_widget(btn1, stretch=0)

        btn2 = Widgets.Button("Delete All")
        btn2.add_callback('activated', lambda w: self.clear_all())
        btn2.set_tooltip("Clear all marks")
        btns.add_widget(btn2, stretch=0)
        btns.add_widget(Widgets.Label(''), stretch=1)

        vbox2 = Widgets.VBox()
        vbox2.add_widget(btns, stretch=0)

        btns = Widgets.HBox()
        btns.set_border_width(4)
        btns.set_spacing(4)

        btn3 = Widgets.CheckBox("Pan to mark")
        btn3.set_state(self.pan2mark)
        btn3.add_callback('activated', self.pan2mark_cb)
        btn3.set_tooltip("Pan follows selected mark")
        btns.add_widget(btn3)
        btns.add_widget(Widgets.Label(''), stretch=1)

        vbox2.add_widget(btns, stretch=0)
        vbox2.add_widget(Widgets.Label(''), stretch=1)
        vbox.add_widget(vbox2, stretch=1)

        ## spacer = Widgets.Label('')
        ## vbox.add_widget(spacer, stretch=1)

        top.add_widget(sw, stretch=1)

        btns = Widgets.HBox()
        btns.set_border_width(4)
        btns.set_spacing(4)

        btn = Widgets.Button("Close")
        btn.add_callback('activated', lambda w: self.close())
        btns.add_widget(btn)
        btns.add_widget(Widgets.Label(''), stretch=1)

        top.add_widget(btns, stretch=0)
        container.add_widget(top, stretch=1)

    def select_mark(self, tag, pan=True):
        # deselect the current selected mark, if there is one
        if self.mark_selected is not None:
            try:
                obj = self.canvas.getObjectByTag(self.mark_selected)
                obj.setAttrAll(color=self.mark_color)
            except:
                # old object may have been deleted
                pass

        self.mark_selected = tag
        if tag is None:
            self.w.marks.show_text('None')
            self.canvas.redraw(whence=3)
            return

        self.w.marks.show_text(tag)
        obj = self.canvas.getObjectByTag(tag)
        obj.setAttrAll(color=self.select_color)
        self.lastx = obj.objects[0].x
        self.lasty = obj.objects[0].y
        if self.pan2mark and pan:
            self.fitsimage.panset_xy(self.lastx, self.lasty)
        self.canvas.redraw(whence=3)

        self.redo()

    def mark_select_cb(self, w, index):
        tag = self.marks[index]
        if index == 0:
            tag = None
        self.select_mark(tag)

    def pan2mark_cb(self, w, val):
        self.pan2mark = val

    def clear_mark_cb(self):
        tag = self.mark_selected
        if tag is None:
            return
        self.canvas.deleteObjectByTag(tag)
        self.w.marks.delete_alpha(tag)
        self.marks.remove(tag)
        self.w.marks.set_index(0)
        self.mark_selected = None

    def clear_all(self):
        self.canvas.deleteAllObjects()
        for name in self.marks:
            self.w.marks.delete_alpha(name)
        self.marks = ['None']
        self.w.marks.append_text('None')
        self.w.marks.set_index(0)
        self.mark_selected = None

    def plot(self, data, x1, y1, x2, y2, data_x, data_y, radius,
             maxv=9):

        width, height = self.fitsimage.get_dims(data)
        if self.txt_arr is None:
            return

        maxval = numpy.nanmax(data)
        minval = numpy.nanmin(data)
        avgval = numpy.average(data)

        fmt_cell = '%%%d.2f' % self.maxdigits

        # can we do this with a numpy.vectorize() fn call and
        # speed things up?
        for i in range(width):
            for j in range(height):
                self.txt_arr[i][j].text = fmt_cell % data[i][j]

        # append statistics line
        fmt_stat = "  Min: %s  Max: %s  Avg: %s" % (fmt_cell, fmt_cell,
                                                    fmt_cell)
        self.sum_arr[0].text = fmt_stat % (minval, maxval, avgval)

        # update the pixtable
        self.pixview.redraw(whence=3)

    def close(self):
        self.fv.stop_local_plugin(self.chname, str(self))
        return True

    def instructions(self):
        self.tw.set_text("""Move cursor around to see surrounding pixel values.
""")

    def start(self):
        self.instructions()
        # insert layer if it is not already
        p_canvas = self.fitsimage.get_canvas()
        try:
            obj = p_canvas.getObjectByTag(self.layertag)

        except KeyError:
            # Add canvas layer
            p_canvas.add(self.canvas, tag=self.layertag)
        self.resume()

    def stop(self):
        # remove the canvas from the image
        self.canvas.ui_setActive(False)
        p_canvas = self.fitsimage.get_canvas()
        try:
            p_canvas.deleteObjectByTag(self.layertag)
        except:
            pass
        self.tw = None

    def pause(self):
        self.canvas.ui_setActive(False)

    def resume(self):
        # turn off any mode user may be in
        self.modes_off()

        self.canvas.ui_setActive(True)
        self.redo()

    def redo(self):
        if self.tw is None:
            return
        # cut out and set the pixel table data
        image = self.fitsimage.get_image()

        if image is None:
            return

        # We report the value across the pixel, even though the coords
        # change halfway across the pixel
        data_x, data_y = int(self.lastx+0.5), int(self.lasty+0.5)

        # cutout image data
        data, x1, y1, x2, y2 = image.cutout_radius(data_x, data_y,
                                                   self.pixtbl_radius)
        self.plot(data, x1, y1, x2, y2, self.lastx, self.lasty,
                  self.pixtbl_radius, maxv=9)

    def _rebuild_table(self):
        canvas = self.pixview.get_canvas()
        canvas.delete_all_objects(redraw=False)

        Text = canvas.get_draw_class('text')
        font_wd = int(self.row_ht * 0.666)
        max_wd = self.maxdigits

        rows = []
        objs = []
        for row in range(self.pixtbl_radius*2+1):
            cols = []
            for col in range(self.pixtbl_radius*2+1):
                x = (font_wd) * max_wd * col + 4
                y = (self.row_ht) * (row + 1) + 4

                color = 'lightgreen'
                if (row == col) and (row == self.pixtbl_radius):
                    color = 'pink'

                text_obj = Text(x, y, text='', font=self.font,
                                color=color, fontsize=self.fontsize,
                                coord='canvas')
                objs.append(text_obj)
                cols.append(text_obj)

            rows.append(cols)

        self.txt_arr = numpy.array(rows)

        # add summary row(s)
        x = (font_wd + 2) + 4
        y += self.row_ht+20
        s1 = Text(x, y, text='', font=self.font,
                  color=color, fontsize=self.fontsize,
                  coord='canvas')
        objs.append(s1)
        self.sum_arr = numpy.array([s1])

        # add all of the text objects to the canvas as one large
        # compound object
        CompoundObject = canvas.get_draw_class('compoundobject')
        canvas.add(CompoundObject(*objs), redraw=False)

    def set_cutout_size_cb(self, w, val):
        index = w.get_index()
        self.pixtbl_radius = self.sizes[index]
        self._rebuild_table()

    def motion_cb(self, canvas, event, data_x, data_y):
        if self.mark_selected is not None:
            return False
        if self.tw is None:
            return

        self.lastx, self.lasty = data_x, data_y

        self.redo()
        return False

    def btndown_cb(self, canvas, event, data_x, data_y):
        self.add_mark(data_x, data_y)
        return True

    def add_mark(self, data_x, data_y, radius=None, color=None, style=None):
        if not radius:
            radius = self.mark_radius
        if not color:
            color = self.mark_color
        if not style:
            style = self.mark_style

        self.logger.debug("Setting mark at %d,%d" % (data_x, data_y))
        self.mark_index += 1
        tag = 'mark%d' % (self.mark_index)
        tag = self.canvas.add(self.dc.CompoundObject(
            self.dc.Point(data_x, data_y, self.mark_radius,
                          style=style, color=color,
                          linestyle='solid'),
            self.dc.Text(data_x + 10, data_y, "%d" % (self.mark_index),
                         color=color)),
                              tag=tag)
        self.marks.append(tag)
        self.w.marks.append_text(tag)
        self.select_mark(tag, pan=False)

    def __str__(self):
        return 'pixtable'

#END

max_stars_repo_name: saimn/ginga

max_issues_repo_name: saimn/ginga

max_forks_repo_name: saimn/ginga


text:
import glob, re
import numpy as np
import pandas as pd
from sklearn.naive_bayes import MultinomialNB
from sklearn.feature_extraction.text import CountVectorizer
from naive_bayes import MultinomialNaiveBayes

def preprocessing():
    path = 'data/*/*'
    X = []
    y = []

    for email in glob.glob(path):
        with open(email, errors='ignore') as file:
            for line in file:
                if line.startswith("Subject:"):
                    text = line.lstrip("Subject: ").strip('\n')
                    X.append(text)
                    y.append(1 if "spam" in email else 0)
                    break

    vect = CountVectorizer(stop_words='english')
    vect.fit(X)
    dtm = vect.transform(X)
    dtm = pd.DataFrame(dtm.todense(), columns=vect.get_feature_names())
    X = dtm.values
    X_test_data = []
    with open('test.txt', errors='ignore') as file:
        for line in file:
            X_test_data.append(line.strip('\n'))
    X_test = vect.transform(X_test_data)
    X_test = pd.DataFrame(X_test.todense(), columns=vect.get_feature_names())
    X_test = X_test.values
    return X, y, X_test

def main():
    # TOY DATASET
    rng = np.random.RandomState(1)
    X = rng.randint(5, size=(6, 10))
    y = np.array([1, 2, 3, 1, 2, 3])
    X_test = np.array([5, 5, 4, 5, 3, 2, 1, 3, 1, 4])
    # personal implementation
    print("Personal Implementation (Toy Dataset)")
    print("-"*50)
    mnb = MultinomialNaiveBayes()
    mnb.conditional_probabilities(X, y)
    mnb.fit(X, y)
    mnb.predict(X_test)
    mnb.get_proba()

    # scikit-learn implementation
    print("Scikit-learn Implementation (Toy Dataset)")
    print("-"*50)
    nb = MultinomialNB(alpha=0.5)
    nb.fit(X, y)
    X_test = X_test.reshape(1, -1)
    print(nb.predict(X_test))
    print(nb.predict_proba(X_test))

    # GATHER DATA FROM SPAM DATASET
    X, y, X_test = preprocessing()

    # personal implementation
    print("Personal Implementation (Spam Dataset)")
    print("-"*50)
    mnb = MultinomialNaiveBayes()
    mnb.conditional_probabilities(X, y)
    mnb.fit(X, y)
    mnb.predict(X_test)
    mnb.get_proba()

    # scikit-learn implementation
    print("Scikit-learn Implementation (Spam Dataset)")
    print("-"*50)
    nb = MultinomialNB(alpha=0.5)
    nb.fit(X, y)
    print(nb.predict(X_test))
    print(nb.predict_proba(X_test))

if __name__ == "__main__": main()


text:
import os, re
import argparse
import numpy as np
import pickle as pl
from os import walk
from gensim.models import Word2Vec
from nltk.tokenize import RegexpTokenizer
import pandas as pd
from tensorflow.contrib.keras import preprocessing

from tqdm import tqdm
from konlpy.tag import Twitter

twitter = Twitter()

from libs import hangle, mongo, analytics

seungwon_user_id = '5b856c9d995fc115c6659c04'

def build_emb_matrix_and_vocab(embedding_model, keep_in_dict=100000, embedding_size=200):
    # 0 th element is the default vector for unknowns.

    emb_matrix = np.zeros((keep_in_dict + 2, embedding_size))
    word2index = {}
    index2word = {}
    for k in range(1, keep_in_dict + 1):
        word = embedding_model.wv.index2word[k - 1]
        # print('word: {}'.format(word))

        emb_matrix[k] = embedding_model[word]
        word2index[word] = k
        index2word[k] = word
    word2index['UNK'] = 0
    index2word[0] = 'UNK'
    word2index['STOP'] = keep_in_dict + 1
    index2word[keep_in_dict + 1] = 'STOP'
    return emb_matrix, word2index, index2word

def sent2index(sent, word2index):
    words = sent.strip().split(' ')
    sent_index = [word2index[word] if word in word2index else 0 for word in words]
    return sent_index

def get_sentence(index2word, sen_index):
    return ' '.join([index2word[index] for index in sen_index])

def gen_data(word2index, type='tension'):
    data = []

    db = mongo.get_db()
    videos = mongo.to_dicts(db.video.find({}))
    pbar = tqdm(total=len(videos))

    for video in videos:
        video = mongo.to_dict(db.video.find_one({'video_id': video['video_id']}))
        subs = mongo.to_dicts(db.sub.find({'video_id': video['video_id']}).sort('index', 1))
        users = mongo.to_dicts(db.user.find({}))

        video['sub_total'] = len(subs)

        target_users = []
        for user in users:
            context_no_label_total = db.label.find({
                'video_id': video['video_id'],
                'context': 0,
                'user_id': user['_id'],
            }).count()

            context_yes_label_total = db.label.find({
                'video_id': video['video_id'],
                'context': 1,
                'user_id': user['_id'],
            }).count()

            if video['sub_total'] == context_yes_label_total and context_no_label_total == context_yes_label_total:
                target_users.append(user)

        if len(target_users) < 2:
            pbar.update(1)
            continue

        offset = 1

        for i, sub in enumerate(subs):
            sub['category'] = 0

            if type == 'tension':
                for user in target_users:
                    label1 = db['label'].find_one({
                        'video_id': video['video_id'],
                        'context': 0,
                        'sub_index': sub['index'],
                        'user_id': user['_id'],
                    })

                    label2 = db['label'].find_one({
                        'video_id': video['video_id'],
                        'context': 1,
                        'sub_index': sub['index'],
                        'user_id': user['_id'],
                    })

                    sub['category'] = analytics.get_category(label1, label2)
            elif type == 'sentiment':
                label = db['label'].find_one({
                    'video_id': video['video_id'],
                    'context': 1,
                    'sub_index': sub['index'],
                    'user_id': seungwon_user_id,
                })

                sentiment = label['sentiment_label']
                if sentiment == '부정':
                    sub['category'] = 0
                elif sentiment == '중립':
                    sub['category'] = 1
                elif sentiment == '긍정':
                    sub['category'] = 2

            elif type == 'intent':
                label = db['label'].find_one({
                    'video_id': video['video_id'],
                    'context': 1,
                    'sub_index': sub['index'],
                    'user_id': seungwon_user_id,
                })

                sentiment = label['intent_label']
                if sentiment == '호응유도':
                    sub['category'] = 0
                elif sentiment == '의견':
                    sub['category'] = 1
                elif sentiment == '인용':
                    sub['category'] = 2
                elif sentiment == '일화':
                    sub['category'] = 3
                elif sentiment == '팩트':
                    sub['category'] = 4

            def text_to_indexes(text):
                n_text = hangle.normalize(text, english=True, number=True, punctuation=True)
                tokens = twitter.pos(n_text, stem=True)

                indexes = []
                for token in tokens:
                    word = '{}/{}'.format(token[0], token[1])

                    if word in word2index:
                        indexes.append(word2index[word])
                    else:
                        print('no word : {}'.format(word))
                        indexes.append(0)

                return indexes

            doc = []
            for j in range(max(0, i - offset), i):
                doc.append(text_to_indexes(subs[j]['text']))
            doc.append(text_to_indexes(sub['text']))
            for j in range(i + 1, min(i + offset + 1, len(subs))):
                doc.append(text_to_indexes(subs[j]['text']))

            # print('doc :', doc)

            data.append({
                'doc': doc,
                'start_ts': sub['start_ts'],
                'end_ts': sub['end_ts'],
                'category': sub['category'],
            })

        pbar.update(1)
    pbar.close()
    return data

def preprocess_sub(data, sent_length, max_rev_len, keep_in_dict=10000):
    ## As the result, each review will be composed of max_rev_len sentences. If the original review is longer than that, we truncate it, and if shorter than that, we append empty sentences to it. And each sentence will be composed of sent_length words. If the original sentence is longer than that, we truncate it, and if shorter, we append the word of 'UNK' to it. Also, we keep track of the actual number of sentences each review contains.
    data_formatted = []
    review_lens = []
    for i, item in enumerate(data):
        review = item['doc']
        review_formatted = preprocessing.sequence.pad_sequences(review, maxlen=sent_length, padding="post", truncating="post", value=keep_in_dict + 1)
        review_len = review_formatted.shape[0]
        review_lens.append(review_len if review_len <= max_rev_len else max_rev_len)
        lack_len = max_rev_length - review_len
        review_formatted_right_len = review_formatted
        if lack_len > 0:
            # extra_rows = np.zeros([lack_len, sent_length], dtype=np.int32)
            extra_rows = np.full((lack_len, sent_length), keep_in_dict + 1)
            review_formatted_right_len = np.append(review_formatted, extra_rows, axis=0)
        elif lack_len < 0:
            row_index = [max_rev_length + i for i in list(range(0, -lack_len))]
            review_formatted_right_len = np.delete(review_formatted, row_index, axis=0)
        data_formatted.append(review_formatted_right_len)

        data[i]['doc'] = review_formatted_right_len
        data[i]['sub_lens'] = review_formatted.shape[0]

def divide(data, train_prop):
    import random
    # 예시
    # x = [1, 2, 3, 4, 5]
    # y = [0, 0, 1, 0, 0]
    # train_prop = 0.2

    random.seed(1234)

    # tmp: [3 4 1 0 2], 랜덤순열
    random.shuffle(data)

    train = data[:round(train_prop * len(data))]
    test = data[-(len(data) - round(train_prop * len(data))):]
    return train, test

def get_df(data):
    label = []
    doc = []
    length = []

    for item in data:
        label.append(item['category'])
        doc.append(item['doc'])
        length.append(item['sub_lens'])

    return pd.DataFrame({'label': label, 'doc': doc, 'length': length})

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description='Process some important parameters.')
    parser.add_argument('-s', '--sent_length', type=int, default=20,
                        help='fix the sentence length in all reviews')
    parser.add_argument('-r', '--max_rev_length', type=int, default=5,
                        help='fix the maximum review length')

    args = parser.parse_args()
    sent_length = args.sent_length
    max_rev_length = args.max_rev_length

    print('sent length is set as {}'.format(sent_length))
    print('rev length is set as {}'.format(max_rev_length))
    working_dir = "./data/"
    fname = os.path.join(working_dir, "embedding.bin")
    embedding_model = Word2Vec.load(fname)

    print("generate word to index dictionary and inverse dictionary...")

    keep_in_dict = len(embedding_model.wv.vocab)
    emb_matrix, word2index, index2word = build_emb_matrix_and_vocab(embedding_model, keep_in_dict=keep_in_dict)
    print("format each review into sentences, and also represent each word by index...")

    print("preprocess each sub...")

    print("save word embedding matrix ...")
    emb_filename = os.path.join(working_dir, "emb_matrix.bin")
    # emb_matrix.dump(emb_filename)
    pl.dump([emb_matrix, word2index, index2word], open(emb_filename, "wb"))

    data = gen_data(word2index, type='intent')
    preprocess_sub(data, keep_in_dict=keep_in_dict, sent_length=sent_length, max_rev_len=max_rev_length)

    train, test = divide(data, 0.9)

    df_train = get_df(train)
    df_train_filename = os.path.join(working_dir, "df_train.bin")
    df_train.to_pickle(df_train_filename)

    df_test = get_df(test)
    df_test_filename = os.path.join(working_dir, "df_test.bin")
    df_test.to_pickle(df_test_filename)

max_stars_repo_name: nlpcl-lab/automatic-tension-detection

max_issues_repo_name: nlpcl-lab/automatic-tension-detection

max_forks_repo_name: nlpcl-lab/automatic-tension-detection


text:
'''
    Automatic bias removal for Face Recognition
    Author: Omar U. Florez
    November, 2018
'''

import numpy as np
import matplotlib.pyplot as plt
import random
import os
import shutil
import imageio

import keras.utils as utils
from keras.layers import Input, Dense, Conv2D, MaxPooling2D, UpSampling2D, Flatten, Reshape, Lambda, Conv2DTranspose, BatchNormalization
from keras.models import Model, model_from_json
from keras import metrics
import keras.backend as K

from keras.callbacks import Callback

from src.align_face_vae import face_alignment
from src.align_face_vae.dataset_celeba import CelebaDatasetSimple
import ipdb
import warnings
warnings.filterwarnings('ignore')
import argparse
from keras.activations import softmax, relu, sigmoid
from keras.utils import plot_model

# saved_folder    = './saved'
# image_path      = data_folder + 'data/images-dpmcrop-test/'
# metadata_path   = data_folder + 'data/list_attr_celeba.txt'
#image_size      = (224, 224)
# num_workers     = 1
# batch_size      = 64
# img_rows        = 256
# img_cols        = 256
# img_chans       = 3
data_folder     = '/Users/ost437/Documents/OneDrive/workspace/datasets/celebrity/'

parser = argparse.ArgumentParser()
parser.add_argument('--saved_folder',   default='./saved')
parser.add_argument('--image_path',     default=data_folder + 'data/images-dpmcrop-test/')
parser.add_argument('--metadata_path',  default=data_folder + 'data/list_attr_celeba.txt')
parser.add_argument('--img_rows',       default=256, type=int)
parser.add_argument('--img_cols',       default=256, type=int)
parser.add_argument('--img_chans',      default=3, type=int)
parser.add_argument('--batch_size',     default=64, type=int)
parser.add_argument('--load_model',     default=False, action='store_true')
args = parser.parse_args()

# load dataset
## male_images = np.load(os.path.join(args.saved_folder, 'input', 'male_images.npy'))
# female_images = np.load(os.path.join(saved_folder, 'input', 'female_images.npy'))
male_proto = np.load(os.path.join(args.saved_folder, 'input', 'male_images_proto.npy'))
female_proto = np.load(os.path.join(args.saved_folder, 'input', 'female_images_proto.npy'))

def load_external_model():
    #10000/10000 - 4031s 403ms/step - loss: 0.3303 - acc: 0.8574 - val_loss: 0.2131 - val_acc: 0.9062
    json_file = open(os.path.join(args.saved_folder, "model", "model_gender.json"), "r")
    loaded_model_json = json_file.read()
    json_file.close()
    model_gender = model_from_json(loaded_model_json)
    model_gender.load_weights(os.path.join(args.saved_folder, "model", "model_gender.h5"))
    return model_gender

def vae_unbias_images(vae, gender, images, file_name=None):
    indices = np.random.choice(len(images), 10)
    predicted_images = vae.predict(images[indices])
    actual_images = images[indices]

    plt.figure(figsize=(30, 8))
    for i, index in enumerate(indices):
        plt.subplot(2, len(indices), i+1), plt.xticks(()), plt.yticks(())
        predicted_image = predicted_images[i]
        prediction = gender.predict(np.array([predicted_image]))
        plt.imshow(predicted_image)
        #plt.title('Predicted class: %s Distribution: %s' % (i2label(np.argmax(prediction)), str(prediction)))
        plt.title('Distribution:\n%s' % (str(prediction)))

        plt.subplot(2, len(indices), i + 1 + len(indices)), plt.xticks(()), plt.yticks(())
        actual_image = actual_images[i]
        prediction = gender.predict(np.array([actual_image]))
        plt.imshow(actual_image)
        # plt.title('Predicted class: %s Distribution: %s' % (i2label(np.argmax(prediction)), str(prediction)))
        plt.title('Distribution:\n%s' % (str(prediction)))
    plt.savefig(os.path.join(args.saved_folder, 'output', file_name))

dataset = CelebaDatasetSimple(args.image_path, args.metadata_path)

def i2label(class_id):
    return "male" if class_id == 0 else "female"

def l2_norm(x):
    return x/K.sqrt(K.sum(x**2)+0.000001)

def var_sampling(args):
    z_mean, z_log_var = args
    batch = K.shape(z_mean)[0]
    dim = K.shape(z_mean)[1]
    epsilon = K.random_normal(shape=(batch, dim))
    return z_mean + K.exp(0.5 * z_log_var) * epsilon

#-----------------------------------------------------------------------------------------------------------------------
    return z_mean + K.exp(0.5 * z_log_var) * epsilon
# Callbacks:
from keras.callbacks import Callback, CSVLogger, ModelCheckpoint, EarlyStopping
class ComputeMetrics(Callback):
    def on_epoch_end(self, epoch, logs):
        logs['val_metric'] = epoch ** 2  # replace it with your metrics
        if (epoch + 1) % 10 == 0:
            logs['test_metric'] = epoch ** 3  # same
        else:
            logs['test_metric'] = np.nan

class Histories(Callback):
    def on_train_begin(self, logs={}):
        self.aucs = []
        self.losses = []

    def on_train_end(self, logs={}):
        return

    def on_epoch_begin(self, epoch, logs={}):
        return

    def on_epoch_end(self, epoch, logs={}):
        #ipdb.set_trace()
        # self.losses.append(logs.get('loss'))
        # y_pred = self.model.predict(self.model.validation_data[0])
        # #self.aucs.append(roc_auc_score(self.model.validation_data[1], y_pred))
        test_images = dataset.get_batch(batch_size=256)[0]
        file_name = 'unbias_gender_recognition_%s-%s.png'%(str(epoch), self.params['steps'])
        gender_model = load_external_model()
        vae_unbias_images(self.model, gender_model, test_images, file_name=file_name)
        del gender_model
        return

    def on_batch_begin(self, batch, logs={}):
        return

    def on_batch_end(self, batch, logs={}):
        return

# Convolutional Variational Auto Encoder
def ConvolutionalVAE(img_rows, img_cols, img_chans, mse=True, external_model=None):
    filters             = 16
    kernel_size         = 3
    latent_dim          = 100
    original_img_size   = (img_rows, img_cols, img_chans)

    # ------------------------------------------------------------------------------------------------------------------
    # Encoder:
    inputs = Input(shape=original_img_size, name='encoder_input')
    x = inputs
    for i in range(2):
        filters *= 2
        x = Conv2D(filters=filters,
                   kernel_size=kernel_size,
                   activation='relu',
                   strides=2,
                   padding='same')(x)

    encoder_shape = K.int_shape(x)
    x = Flatten()(x)
    # z_mean = z_log_var = z = (None, latent_dim)
    z_mean = Dense(latent_dim, name='z_mean')(x)
    z_log_var = Dense(latent_dim, name='z_log_var')(x)
    ## use reparameterization trick to push the sampling out as input
    z = Lambda(var_sampling, output_shape=(latent_dim,), name='z')([z_mean, z_log_var])

    #encoder = Model(inputs=inputs, outputs = [z_mean, z_log_var, z], name='encoder')
    encoder = Model(inputs=inputs, outputs=z, name='encoder')
    encoder.summary()

    # ------------------------------------------------------------------------------------------------------------------
    # Decoder:
    latent_inputs = Input(shape=(latent_dim, ), name='z_sampling')
    x = Dense(encoder_shape[1]*encoder_shape[2]*encoder_shape[3], activation='relu')(latent_inputs)
    x = Reshape((encoder_shape[1], encoder_shape[2], encoder_shape[3]))(x)
    for i in range(2):
        x = Conv2DTranspose(filters=filters,
                            kernel_size=kernel_size,
                            activation='relu',
                            strides=2,
                            padding='same')(x)
        filters //= 2
    outputs = Conv2DTranspose(filters=img_chans,
                              kernel_size=kernel_size,
                              activation='sigmoid',
                              padding='same',
                              name='decoder_output')(x)

    decoder = Model(inputs=latent_inputs, outputs=outputs, name='decoder')
    decoder.summary()

    pred_outputs = decoder(encoder(inputs))

    reconstruction_loss = metrics.mse(K.flatten(pred_outputs), K.flatten(inputs)) * img_rows * img_cols
    # KL(p(z|x), p(z  = -1/2*(e^d*u^2)/d))
    kl_loss             = K.sum(1 + z_log_var - K.square(z_mean) - K.exp(z_log_var), axis=-1) * -0.5
    #vae_loss            = K.mean(sigmoid(reconstruction_loss) + sigmoid(kl_loss))
    vae_loss = K.mean(reconstruction_loss + kl_loss)

    external_loss = 0
    if external_model:
        external_model.summary()

        # entropy_loss: our goal is to maxmimize entropy for gender prediction,
        # so we negate its original definition
        # p_x = external_model(pred_outputs)
        # entropy_gender_loss = -K.mean(-p_x * K.log(p_x))
        # vae_loss += entropy_gender_loss

        # penalize similarity between gender predictions for original and reconstructed images
        external_loss = K.mean(metrics.mse(external_model(pred_outputs), (1. - external_model(inputs))))

        #reranking loss as in Adversarial Transformation Networks
        #external_l = external_loss(external_model)

        vae_loss = vae_loss + 100.*external_loss

    vae = Model(inputs=inputs, outputs=pred_outputs, name='vae')
    vae.add_loss(vae_loss)
    vae.compile(optimizer='adam')
    return vae, encoder, decoder

def save_models(vae, encoder, decoder, context=''):
    vae_json = vae.to_json()
    with open(os.path.join(args.saved_folder, "model", "vae%s.json"%context), "w") as json_file:
        json_file.write(vae_json)
    vae.save_weights(os.path.join(args.saved_folder, "model", "vae%s.h5"%context))

    encoder_json = encoder.to_json()
    with open(os.path.join(args.saved_folder, "model", "encoder%s.json"%context), "w") as json_file:
        json_file.write(encoder_json)
        encoder.save_weights(os.path.join(args.saved_folder, "model", "encoder%s.h5"%context))

    decoder_json = decoder.to_json()
    with open(os.path.join(args.saved_folder, "model", "decoder%s.json"%context), "w") as json_file:
        json_file.write(decoder_json)
    decoder.save_weights(os.path.join(args.saved_folder, "model", "decoder%s.h5"%context))
    print("Saved models to disk")

def load_models(context=''):
    json_file = open(os.path.join(args.saved_folder, "model", "vae%s.json"%context), 'r')
    loaded_model_json = json_file.read()
    json_file.close()
    vae = model_from_json(loaded_model_json)
    vae.load_weights(os.path.join(args.saved_folder, "model", "vae%s.h5"%context))

    json_file = open(os.path.join(args.saved_folder, "model", "encoder%s.json"%context), 'r')
    loaded_model_json = json_file.read()
    json_file.close()
    encoder = model_from_json(loaded_model_json)
    encoder.load_weights(os.path.join(args.saved_folder, "model", "encoder%s.h5"%context))

    json_file = open(os.path.join(args.saved_folder, "model", "decoder%s.json"%context), 'r')
    loaded_model_json = json_file.read()
    json_file.close()
    decoder = model_from_json(loaded_model_json)
    decoder.load_weights(os.path.join(args.saved_folder, "model", "decoder%s.h5"%context))

    #10000/10000 - 4031s 403ms/step - loss: 0.3303 - acc: 0.8574 - val_loss: 0.2131 - val_acc: 0.9062
    json_file = open(os.path.join(args.saved_folder, "model", "model_gender.json"), "r")
    loaded_model_json = json_file.read()
    json_file.close()
    model_gender = model_from_json(loaded_model_json)
    model_gender.load_weights(os.path.join(args.saved_folder, "model", "model_gender.h5"))

    return vae, encoder, decoder, model_gender

def unbias_images_original(encoder, decoder, gender_model, images, file_name=None):
    def softmax(x):
        return np.exp(x) / (0.00001 + np.sum(np.exp(x)))

    indices = np.arange(len(images))[:10]
    #predicted_images = decoder.predict(encoder.predict(images[indices])[2])
    predicted_images = decoder.predict(encoder.predict(images[indices]))
    actual_images = images[indices]

    plt.figure(figsize=(30, 8))
    for i, index in enumerate(indices):
        plt.subplot(2, len(indices), i+1), plt.xticks(()), plt.yticks(())
        predicted_image = predicted_images[i]
        prediction = softmax(gender_model.predict(np.array([predicted_image])))
        plt.imshow(predicted_image)
        #plt.title('Predicted class: %s Distribution: %s' % (i2label(np.argmax(prediction)), str(prediction)))
        plt.title('Classification:\nFemale: %.2f%%\nMale: %.2f%%' % (100. * prediction[0][1], 100. * prediction[0][0]))

        plt.subplot(2, len(indices), i + 1 + len(indices)), plt.xticks(()), plt.yticks(())
        actual_image = actual_images[i]
        prediction = softmax(gender_model.predict(np.array([actual_image])))
        plt.imshow(actual_image)
        # plt.title('Predicted class: %s Distribution: %s' % (i2label(np.argmax(prediction)), str(prediction)))
        plt.title('Classification:\nFemale: %.2f%%\nMale: %.2f%%' % (100. * prediction[0][1], 100. * prediction[0][0]))
    plt.savefig(os.path.join(args.saved_folder, 'output', file_name))
    return

def vae_morph(images_):
    faces_len = np.min([20,images_.shape[0]])
    pb = display.ProgressBar(faces_len)
    pb.display()
    os.remove('movie.gif')
    with imageio.get_writer('movie.gif', mode='I') as writer:
        for f in range(faces_len - 1):
            A = encoder.predict(images_[[f,f+1]]/256.)
            for i,a in enumerate(np.linspace(0,1,7)):
                blend_latent = A[0][np.newaxis,0] * (1. - a) + A[0][np.newaxis,1] * a
                blended = np.squeeze(generator.predict(blend_latent))
                writer.append_data((blended * 255.).astype(np.uint8))
            pb.progress = f+1
    display.clear_output(wait=True)
    return HTML('<img src="movie.gif?%s">'%(random.randint(0,1000)))

def morph_random_faces(encoder, decoder, male_images, file_name=None):
    # generate one embeddeding per observation: len(idx)=2
    # A = 3x(2, latent_dim)
    idx = np.random.randint(male_images.shape[0], size=(2,))
    A = encoder.predict(male_images[idx])

    # morph between faces
    plt.figure(figsize=(30, 8))
    plt.subplot(1, 7, 1), plt.xticks(()), plt.yticks(()), plt.imshow(male_images[idx[0]])
    plt.subplot(1, 7, 7), plt.xticks(()), plt.yticks(()), plt.imshow(male_images[idx[1]])
    ipdb.set_trace()
    # blend the embedding vectors with different proportions
    for i, alpha in enumerate(np.linspace(0, 1, 5)):
        embedding_a = A[0][np.newaxis, 0]
        embedding_b = A[0][np.newaxis, 1]
        blend = embedding_a * (alpha) + embedding_b * (1. - alpha)
        # reconstruction = (1, 256, 256, 3)
        reconstruction = decoder.predict(blend)
        plt.subplot(1, 7, i + 2), plt.xticks(()), plt.yticks(())
        plt.imshow(np.squeeze(reconstruction))
    file_name = file_name if file_name else 'morph_random_faces.png'
    plt.savefig(os.path.join(args.saved_folder, 'output', file_name))

def morph_two_faces(encoder, decoder, face_a, face_b, file_name=None):
    faces = np.array([face_a, face_b])
    A = encoder.predict(faces)

    # morph between faces
    plt.figure(figsize=(30, 8))
    plt.subplot(1, 7, 1), plt.xticks(()), plt.yticks(()), plt.imshow(face_a)
    plt.subplot(1, 7, 7), plt.xticks(()), plt.yticks(()), plt.imshow(face_b)
    # blend the embedding vectors with different proportions
    for i, alpha in enumerate(np.linspace(0, 1, 5)):
        embedding_a = A[0][np.newaxis, 0]
        embedding_b = A[0][np.newaxis, 1]
        blend = embedding_a * (1.-alpha) + embedding_b * (alpha)
        # reconstruction = (1, 256, 256, 3)
        reconstruction = decoder.predict(blend)
        plt.subplot(1, 7, i + 2), plt.xticks(()), plt.yticks(())
        plt.imshow(np.squeeze(reconstruction))
    file_name = file_name if file_name else 'morph_two_faces.png'
    plt.savefig(os.path.join(args.saved_folder, 'output', file_name))

def run_autoencoder():
    # check for male images
    #for i in range(min(28, male_images.shape[0])):
    #    plt.subplot(4, 7, i+1), plt.xticks(()), plt.yticks(())
    #    plt.imshow(male_images[i])
    #plt.savefig(os.path.join(args.saved_folder, 'input', 'male_images.png'))

    ckpt_dir = './'
    checkpoint_path = os.path.join(ckpt_dir, "checkpoint-{epoch:02d}-{val_loss:.6f}.hdf5")
    #checkpoint = ModelCheckpoint(checkpoint_path, monitor='val_loss', verbose=1, save_best_only=True, mode="min")
    stahp = EarlyStopping(min_delta=0.0001, patience=5)
    histories = Histories()

    K.clear_session()
    if args.load_model:
        print("Load json and create model")
        vae, encoder, decoder, gender_model = load_models(context='_reranking')
    else:
        gender_model = load_external_model()
        # freeze gender_model layers
        for l in gender_model.layers: l.trainable = False
        vae, encoder, decoder = ConvolutionalVAE(args.img_rows, args.img_cols,
                                                 args.img_chans, external_model=gender_model)

        ##vae.fit(male_images, epochs=20, batch_size=32, validation_split=0.1)
        history = vae.fit_generator(dataset.reconstruction_generator(is_validation=False),
                                    steps_per_epoch=40,
                                    epochs=100,
                                    validation_data=dataset.reconstruction_generator(is_validation=True),
                                    validation_steps=1,
                                    callbacks=[histories])
        ipdb.set_trace()
        save_models(vae, encoder, decoder, context='_reranking')
        #plot_model(vae, to_file="./model.svg", show_layer_names=True, show_shapes=True, rankdir="TB")

    #test autoencoder with different faces
    test_images = dataset.get_batch(batch_size=256)[0]
    ipdb.set_trace()
    morph_random_faces(encoder, decoder, test_images)
    unbias_images_original(encoder, decoder, gender_model, test_images, file_name='unbias_gender_recognition.png')
    vae_unbias_images(vae, gender_model, test_images, file_name='unbias_gender_recognition.png')
    #morph_two_faces(encoder, decoder, male_images[0], female_proto, file_name='morph_male_femaleproto.png')
    #morph_two_faces(encoder, decoder, male_images[0], male_proto, file_name='morph_male_maleproto.png')

    indices = np.random.choice(len(test_images), 10)
    run_gender_detection(gender_model, test_images[indices], file_name='gender_prediction.png')
    return

def run_gender_detection(model, images, file_name='gender_prediction.png'):
    plt.figure(figsize=(30, 8))
    for index in range(len(images)):
        plt.subplot(1, len(images), index+1), plt.xticks(()), plt.yticks(())
        prediction = model.predict(np.array([images[index]]))
        plt.imshow(images[index])
        #plt.title('Predicted class: %s\nDistribution: ' %(i2label(np.argmax(prediction)), str(prediction)))
        plt.title('Distribution:\n%s' %str(prediction))
    plt.savefig(os.path.join(args.saved_folder, 'output', file_name))

#images contains two faces used as start/end states of the transition
def gif_vae_morph(images):
    faces_len = np.min(20, len(images))

# http://www.morethantechnical.com/2018/05/20/aligning-faces-with-py-opencv-dlib-combo/
# Run:
#       python src/align_face_vae/model_conv_vae.py --load_model
if __name__ == '__main__':
    run_autoencoder()
    #run_gender_detection(male_images, file_name='gender_prediction_males.png')