text:
import numpy as np

def selectOne(probabilities):
    max     = np.sum(probabilities)
    pick    = np.random.uniform(0, max)
    current = 0
    idx     = 0
    for prob in probabilities:
        current += prob
        if current > pick:
            break
        idx += 1

    return idx

class optimizer:
    def __init__(self, epoch, iterationCount, foodSourceSize, foodSourceCount, limitForScout, employedBeeCount, onlookerBeeCount, display, env):
        self.epoch = epoch
        self.iterationCount = iterationCount
        self.foodSourceSize = foodSourceSize
        self.foodSourceCount = foodSourceCount
        self.limitForScout = limitForScout
        self.employedBeeCount = employedBeeCount
        self.onlookerBeeCount = onlookerBeeCount
        self.display = display
        self.env = env

    def optimize(self):

        losses = []
        bests = []
        errors = []

        for e in np.arange(self.epoch):

            foodSources = np.random.random((self.foodSourceCount, self.foodSourceSize))
            trialScores = np.zeros(self.foodSourceCount)

            loss = []

            for iter in np.arange(self.iterationCount):

                # Employed Bees
                for idx in np.arange(self.employedBeeCount):

                    i = np.random.randint(self.foodSourceCount)
                    k = np.random.randint(self.foodSourceCount)
                    j = np.random.randint(self.foodSourceSize)
                    fi = np.random.uniform(-1, 1)

                    newFoodSource = np.copy(foodSources[i])
                    alternateFoodSource = foodSources[k]
                    newFoodSource[j] = newFoodSource[j] + fi * (newFoodSource[j] - alternateFoodSource[j])

                    if newFoodSource[j] < 0:
                        newFoodSource[j] = 0
                    if newFoodSource[j] > 1:
                        newFoodSource[j] = 1

                    oldScore = self.env.calculateFitness(foodSources[i])
                    newScore = self.env.calculateFitness(newFoodSource)

                    if newScore < oldScore:
                        foodSources[i] = newFoodSource
                        trialScores[i] = 0
                    else:
                        trialScores[i] += 1

                fitnessScores = []

                fitnessScores = [self.env.calculateFitness(foodSource) for foodSource in foodSources]
                probabilities = (fitnessScores - np.min(fitnessScores)) / np.ptp(fitnessScores)
                probabilities = [1 - prob for prob in probabilities]

                # Onlooker Bees
                for idx in np.arange(self.onlookerBeeCount):

                    i = selectOne(probabilities)
                    k = np.random.randint(self.foodSourceCount)
                    j = np.random.randint(self.foodSourceSize)
                    fi = np.random.uniform(-1, 1)

                    newFoodSource = np.copy(foodSources[i])
                    alternateFoodSource = foodSources[k]
                    newFoodSource[j] = newFoodSource[j] + fi * (newFoodSource[j] - alternateFoodSource[j])

                    if newFoodSource[j] < 0:
                        newFoodSource[j] = 0
                    if newFoodSource[j] > 1:
                        newFoodSource[j] = 1

                    oldScore = self.env.calculateFitness(foodSources[i])
                    newScore = self.env.calculateFitness(newFoodSource)

                    if newScore < oldScore:
                        foodSources[i] = newFoodSource
                        trialScores[i] = 0
                    else:
                        trialScores[i] += 1

                # Scout Bees
                for idx in np.arange(len(trialScores)):
                    if trialScores[idx] > self.limitForScout:
                        foodSources[idx] = np.random.random(self.foodSourceSize)
                        trialScores[idx] = 0

                bestIndexes = np.asarray(fitnessScores).argsort()
                bestFoodSource = foodSources[bestIndexes[0]]

                loss.append(fitnessScores[0])

                if self.display > 0 and iter > 0 and iter % self.display == 0:
                    print('Iteration:', iter, ' - Loss: ',  fitnessScores[0])
                    self.env.drawSolution(bestFoodSource)

            errors.append(self.env.calculateError(bestFoodSource))
            bests.append(bestFoodSource)
            losses.append(self.env.calculateFitness(bestFoodSource))

        sums = [sum(error) for error in errors]
        idx = np.asarray(sums).argsort()[0]
        bestFoodSource = bests[idx]
        bestFoodSourceLoss = losses[idx]

        if self.display > 0:
            print('Average of errors: ', np.average(sums))

        return(sums, bests, losses, bestFoodSource, bestFoodSourceLoss)


text:
import numpy as np
import xml.etree.ElementTree as ET
import matplotlib.pyplot as plt
ROOT = "/home/chuck/bittiger_course_mxnet/DATA/VOCdevkit/VOC2012/JPEGImages/"
N = 11540

def cp_alliamge(savimg=False):
    catlist = []
    with open("cat_val.txt", "r") as f:
        for x in f.readlines():
            split = x.split(' ')
            if len(split)==3:
                catlist.append(split[0])
                if savimg:
                    img_path = ROOT+split[0]+'.jpg'
                    img = plt.imread(img_path)
                    plt.imsave('DATA/'+split[0]+'.jpg', img)
    print ("There are {} cat image".format(len(catlist)))
    print (catlist)
    return catlist

def get_all_annotation(path, imglist, save=True):
    annotation_list = {}
    for name in imglist:
        filename = path + name + '.xml'
        tree = ET.parse(filename)
        root = tree.getroot()
        objs = root.findall('object')
        bboxes = []
        for _, obj in enumerate(objs):
            cls = obj.find('name').text.lower().strip()
            if cls == 'cat':
                bbox = obj.find('bndbox')
                # make pixel indexes 0-based
                x1 = int(bbox.find('xmin').text) - 1
                y1 = int(bbox.find('ymin').text) - 1
                x2 = int(bbox.find('xmax').text) - 1
                y2 = int(bbox.find('ymax').text) - 1

                cx = (x2+x1)/2.
                cy = (y2+y1)/2
                w = x2-x1
                h = y2-y1

                bboxes.append([cx, cy, w, h])
        annotation_list[name]=bboxes
    np.save("annotation_list.npy", annotation_list)
    return annotation_list

path = "/home/chuck/bittiger_course_mxnet/DATA/VOCdevkit/VOC2012/Annotations/"
catlist = cp_alliamge()
box = get_all_annotation(path, catlist)
print box.keys()
print [len(box[key]) for key in box.keys()]
print [len(box[key]) for key in box.keys()]

ext: py

reason: import numpy


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

"""
This module implements methods to handle datasets.
"""

__author__ = "Henrique Siqueira"
__email__ = "siqueira.hc@outlook.com"
__license__ = "MIT license"
__version__ = "1.0"

# External Libraries
from torchvision.transforms import ToTensor, Normalize
from torch.utils.data import Dataset
from skimage import io
from PIL import Image
import numpy as np
import pandas
import torch

# Standard Libraries
from os import path, listdir
import sys
import csv
import re

# Modules
from model.utils import uimage

# FER+ >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

class FERplus(Dataset):
    def __init__(self, idx_set=0, max_loaded_images_per_label=1000, transforms=None, base_path_to_FER_plus=None):
        """
            Code based on https://github.com/microsoft/FERPlus.

            :param idx_set: Labeled = 0, Validation = 1, Test = 2
            :param max_loaded_images_per_label: Maximum number of images per label
            :param transforms: transforms (callable, optional): Optional transform to be applied on a sample.
        """

        self.idx_set = idx_set
        self.max_loaded_images_per_label = max_loaded_images_per_label
        self.transforms = transforms
        self.base_path_to_FER_plus = base_path_to_FER_plus
        self.fer_sets = {0: 'FER2013Train/', 1: 'FER2013Valid/', 2: 'FER2013Test/'}

        # Default values
        self.num_labels = 8
        self.mean = [0.0, 0.0, 0.0]
        self.std = [1.0, 1.0, 1.0]

        # Load data
        self.loaded_data = self._load()
        print('Size of the loaded set: {}'.format(self.loaded_data[0].shape[0]))

    def __len__(self):
        return self.loaded_data[0].shape[0]

    def __getitem__(self, idx):
        sample = {'image': self.loaded_data[0][idx], 'emotion': self.loaded_data[1][idx]}
        sample['image'] = Image.fromarray(sample['image'])

        if not (self.transforms is None):
            sample['image'] = self.transforms(sample['image'])

        return Normalize(mean=self.mean, std=self.std)(ToTensor()(sample['image'])), sample['emotion']

    def online_normalization(self, x):
        return Normalize(mean=self.mean, std=self.std)(ToTensor()(x))

    def norm_input_to_orig_input(self, x):
        x_r = torch.zeros(x.size())
        x_r[0] = (x[2] * self.std[2]) + self.mean[2]
        x_r[1] = (x[1] * self.std[1]) + self.mean[1]
        x_r[2] = (x[0] * self.std[0]) + self.mean[0]
        return x_r

    @staticmethod
    def get_class(idx):
        classes = {
            0: 'Neutral',
            1: 'Happy',
            2: 'Sad',
            3: 'Surprise',
            4: 'Fear',
            5: 'Disgust',
            6: 'Anger',
            7: 'Contempt'}

        return classes[idx]

    @staticmethod
    def _parse_to_label(idx):
        """
        Parse labels to make them compatible with AffectNet.
        :param idx:
        :return:
        """
        emo_to_return = np.argmax(idx)

        if emo_to_return == 2:
            emo_to_return = 3
        elif emo_to_return == 3:
            emo_to_return = 2
        elif emo_to_return == 4:
            emo_to_return = 6
        elif emo_to_return == 6:
            emo_to_return = 4

        return emo_to_return

    @staticmethod
    def _process_data(emotion_raw):
        size = len(emotion_raw)
        emotion_unknown = [0.0] * size
        emotion_unknown[-2] = 1.0

        # remove emotions with a single vote (outlier removal)
        for i in range(size):
            if emotion_raw[i] < 1.0 + sys.float_info.epsilon:
                emotion_raw[i] = 0.0

        sum_list = sum(emotion_raw)
        emotion = [0.0] * size

        # find the peak value of the emo_raw list
        maxval = max(emotion_raw)
        if maxval > 0.5 * sum_list:
            emotion[np.argmax(emotion_raw)] = maxval
        else:
            emotion = emotion_unknown  # force setting as unknown

        return [float(i) / sum(emotion) for i in emotion]

    def _load(self):
        csv_label = []
        data, labels = [], []
        counter_loaded_images_per_label = [0 for _ in range(self.num_labels)]

        path_folders_images = path.join(self.base_path_to_FER_plus, 'Images', self.fer_sets[self.idx_set])
        path_folders_labels = path.join(self.base_path_to_FER_plus, 'Labels', self.fer_sets[self.idx_set])

        with open(path_folders_labels + '/label.csv') as csvfile:
            lines = csv.reader(csvfile)
            for row in lines:
                csv_label.append(row)

        # Shuffle training set
        if self.idx_set == 0:
            np.random.shuffle(csv_label)

        for l in csv_label:
            emotion_raw = list(map(float, l[2:len(l)]))
            emotion = self._process_data(emotion_raw)
            emotion = emotion[:-2]

            try:
                emotion = [float(i) / sum(emotion) for i in emotion]
                emotion = self._parse_to_label(emotion)
            except ZeroDivisionError:
                emotion = 9

            if (emotion < self.num_labels) and (counter_loaded_images_per_label[int(emotion)] < self.max_loaded_images_per_label):
                counter_loaded_images_per_label[int(emotion)] += 1

                img = np.array(uimage.read(path.join(path_folders_images, l[0])), np.uint8)

                box = list(map(int, l[1][1:-1].split(',')))

                if box[-1] != 48:
                    print("[INFO] Face is not centralized.")
                    print(path.join(path_folders_images, l[0]))
                    print(box)
                    exit(-1)

                img = img[box[0]:box[2], box[1]:box[3], :]
                img = uimage.resize(img, (96, 96))

                data.append(img)
                labels.append(emotion)

            has_loading_finished = (np.sum(counter_loaded_images_per_label) >= (self.max_loaded_images_per_label * self.num_labels))

            if has_loading_finished:
                break

        return [np.array(data), np.array(labels)]

# FER+ <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

# AffectNet (Categorical) >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

class AffectNetCategorical(Dataset):
    def __init__(self, idx_set=0, max_loaded_images_per_label=1000, transforms=None, is_norm_by_mean_std=True,
                 base_path_to_affectnet=None):
        """
            This class follows the experimental methodology conducted by (Mollahosseini et al., 2017).

            Refs.
            Mollahosseini, A., Hasani, B. and Mahoor, M.H., 2017. Affectnet: A database for facial expression,
            valence, and arousal computing in the wild. IEEE Transactions on Affective Computing.

            :param idx_set: Labeled = 0, Unlabeled = 1, Validation = 2, Test = Not published by
                            (Mollahosseini et al., 2017)
            :param max_loaded_images_per_label: Maximum number of images per label
            :param transforms: transforms (callable, optional): Optional transform to be applied on a sample.
        """

        self.idx_set = idx_set
        self.max_loaded_images_per_label = max_loaded_images_per_label
        self.transforms = transforms
        self.base_path_to_affectnet = base_path_to_affectnet
        self.affectnet_sets = {'supervised': 'Training_Labeled/',
                               'unsupervised': 'Training_Unlabeled/',
                               'validation': 'Validation/'}

        # Default values
        self.num_labels = 8
        if is_norm_by_mean_std:
            self.mean = [149.35457 / 255., 117.06477 / 255., 102.67609 / 255.]
            self.std = [69.18084 / 255., 61.907074 / 255., 60.435623 / 255.]
        else:
            self.mean = [0.0, 0.0, 0.0]
            self.std = [1.0, 1.0, 1.0]

        # Load data
        self.loaded_data = self._load()
        print('Size of the loaded set: {}'.format(self.loaded_data[0].shape[0]))

    def __len__(self):
        return self.loaded_data[0].shape[0]

    def __getitem__(self, idx):
        sample = {'image': self.loaded_data[0][idx], 'emotion': self.loaded_data[1][idx]}
        sample['image'] = Image.fromarray(sample['image'])

        if not (self.transforms is None):
            sample['image'] = self.transforms(sample['image'])

        return Normalize(mean=self.mean, std=self.std)(ToTensor()(sample['image'])), sample['emotion']

    def online_normalization(self, x):
        return Normalize(mean=self.mean, std=self.std)(ToTensor()(x))

    def norm_input_to_orig_input(self, x):
        x_r = torch.zeros(x.size())
        x_r[0] = (x[2] * self.std[2]) + self.mean[2]
        x_r[1] = (x[1] * self.std[1]) + self.mean[1]
        x_r[2] = (x[0] * self.std[0]) + self.mean[0]
        return x_r

    @staticmethod
    def get_class(idx):
        classes = {
            0: 'Neutral',
            1: 'Happy',
            2: 'Sad',
            3: 'Surprise',
            4: 'Fear',
            5: 'Disgust',
            6: 'Anger',
            7: 'Contempt'}

        return classes[idx]

    @staticmethod
    def _parse_to_label(idx):
        """
            The file name follows this structure: 'ID_s_exp_s_val_s_aro_.jpg' Ex. '0000000s7s-653s653.jpg'.

            Documentation of labels adopted by AffectNet's authors:
            Expression: expression ID of the face (0: Neutral, 1: Happy, 2: Sad, 3: Surprise, 4: Fear, 5: Disgust, 6: Anger, 7: Contempt, 8: None, 9: Uncertain, 10: No-Face)
            Valence: valence value of the expression in interval [-1,+1] (for Uncertain and No-face categories the value is -2)
            Arousal: arousal value of the expression in interval [-1,+1] (for Uncertain and No-face categories the value is -2)

            :param idx: File's name
            :return: label
        """

        label_info = idx.split('s')
        discrete_label = np.int(label_info[1])

        return discrete_label if (discrete_label < 8) else -1

    def _load(self):
        data_affect_net, labels_affect_net = [], []
        counter_loaded_images_per_label = [0 for _ in range(self.num_labels)]

        if self.idx_set == 0:
            path_folders_affect_net = path.join(self.base_path_to_affectnet,
                                                self.affectnet_sets['supervised'])
        elif self.idx_set == 1:
            path_folders_affect_net = path.join(self.base_path_to_affectnet,
                                                self.affectnet_sets['unsupervised'])
        else:
            path_folders_affect_net = path.join(self.base_path_to_affectnet,
                                                self.affectnet_sets['validation'])

        folders_affect_net = sort_numeric_directories(listdir(path_folders_affect_net))
        # Randomize folders
        if self.idx_set < 2:
            np.random.shuffle(folders_affect_net)

        for f_af in folders_affect_net:
            path_images_affect_net = path.join(path_folders_affect_net, f_af)

            images_affect_net = np.sort(np.array(listdir(path_images_affect_net)))
            # Randomize images
            if self.idx_set < 2:
                np.random.shuffle(images_affect_net)

            for file_name_image_affect_net in images_affect_net:
                lbl = self._parse_to_label(file_name_image_affect_net)

                if (lbl >= 0) and (counter_loaded_images_per_label[int(lbl)] < self.max_loaded_images_per_label):
                    img = np.array(uimage.read(path.join(path_images_affect_net, file_name_image_affect_net)), np.uint8)

                    data_affect_net.append(img)
                    labels_affect_net.append(lbl)

                    counter_loaded_images_per_label[int(lbl)] += 1

                has_loading_finished = (np.sum(counter_loaded_images_per_label) >= (
                            self.max_loaded_images_per_label * self.num_labels))

                if has_loading_finished:
                    break

            if has_loading_finished:
                break

        return [np.array(data_affect_net), np.array(labels_affect_net)]

# AffectNet (Categorical) <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

# AffectNet (Dimensional) >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

class AffectNetDimensional(Dataset):
    def __init__(self, idx_set=0, max_loaded_images_per_label=1000, transforms=None, is_norm_by_mean_std=True,
                 base_path_to_affectnet=None):
        """
            This class follows the experimental methodology conducted by (Mollahosseini et al., 2017).

            Refs.
            Mollahosseini, A., Hasani, B. and Mahoor, M.H., 2017. Affectnet: A database for facial expression, valence, and arousal computing in the wild. IEEE Transactions on Affective Computing.

            :param idx_set: Labeled = 0, Unlabeled = 1, Validation = 2, Test = Not published by (Mollahosseini et al., 2017)
            :param max_loaded_images_per_label: Maximum number of images per label
            :param transforms: transforms (callable, optional): Optional transform to be applied on a sample.
        """

        self.idx_set = idx_set
        self.max_loaded_images_per_label = max_loaded_images_per_label
        self.transforms = transforms
        self.base_path_to_affectnet = base_path_to_affectnet
        self.affectnet_sets = {'supervised': 'Training_Labeled/',
                               'unsupervised': 'Training_Unlabeled/',
                               'validation': 'Validation/'}

        # Default values
        self.num_labels = 4
        if is_norm_by_mean_std:
            self.mean = [149.35457 / 255., 117.06477 / 255., 102.67609 / 255.]
            self.std = [69.18084 / 255., 61.907074 / 255., 60.435623 / 255.]
        else:
            self.mean = [0.0, 0.0, 0.0]
            self.std = [1.0, 1.0, 1.0]

        # Load data
        self.loaded_data = self._load()
        print('Size of the loaded set: {}'.format(self.loaded_data[0].shape[0]))

    def __len__(self):
        return self.loaded_data[0].shape[0]

    def __getitem__(self, idx):
        sample = {'image': self.loaded_data[0][idx], 'emotion': self.loaded_data[1][idx]}
        sample['image'] = Image.fromarray(sample['image'])

        if not (self.transforms is None):
            sample['image'] = self.transforms(sample['image'])

        return Normalize(mean=self.mean, std=self.std)(ToTensor()(sample['image'])), sample['emotion']

    def online_normalization(self, x):
        return Normalize(mean=self.mean, std=self.std)(ToTensor()(x))

    def norm_input_to_orig_input(self, x):
        x_r = torch.zeros(x.size())
        x_r[0] = (x[2] * self.std[2]) + self.mean[2]
        x_r[1] = (x[1] * self.std[1]) + self.mean[1]
        x_r[2] = (x[0] * self.std[0]) + self.mean[0]
        return x_r

    @staticmethod
    def get_class(idx):
        return idx

    @staticmethod
    def _parse_to_label(idx):
        """
            The file name follows this structure: 'ID_s_exp_s_val_s_aro_.jpg' Ex. '0000000s7s-653s653.jpg'.

            Documentation of labels adopted by AffectNet's authors:
            Expression: expression ID of the face (0: Neutral, 1: Happy, 2: Sad, 3: Surprise, 4: Fear, 5: Disgust, 6: Anger, 7: Contempt, 8: None, 9: Uncertain, 10: No-Face)
            Valence: valence value of the expression in interval [-1,+1] (for Uncertain and No-face categories the value is -2)
            Arousal: arousal value of the expression in interval [-1,+1] (for Uncertain and No-face categories the value is -2)

            :param idx: File's name
            :return: label
        """

        label_info = idx.split('s')

        discrete_label = np.int(label_info[1])
        valence = np.float32(label_info[2]) / 1000.0
        arousal = np.float32(label_info[-1].split('.')[0]) / 1000.0

        if (valence <= -2.0) or (arousal <= -2.0) or (discrete_label >= 8):
            quadrant = -1
        else:
            if (valence >= 0) and (arousal >= 0):
                quadrant = 0
            elif (valence < 0) and (arousal >= 0):
                quadrant = 1
            elif (valence < 0) and (arousal < 0):
                quadrant = 2
            else:
                quadrant = 3

        return valence, arousal, quadrant

    def _load(self):
        data_affect_net, labels_affect_net = [], []
        counter_loaded_images_per_label = [0 for _ in range(self.num_labels)]

        if self.idx_set == 0:
            path_folders_affect_net = path.join(self.base_path_to_affectnet,
                                                self.affectnet_sets['supervised'])
        elif self.idx_set == 1:
            path_folders_affect_net = path.join(self.base_path_to_affectnet,
                                                self.affectnet_sets['unsupervised'])
        else:
            path_folders_affect_net = path.join(self.base_path_to_affectnet,
                                                self.affectnet_sets['validation'])

        folders_affect_net = sort_numeric_directories(listdir(path_folders_affect_net))
        # Randomize folders
        if self.idx_set < 2:
            np.random.shuffle(folders_affect_net)

        for f_af in folders_affect_net:
            path_images_affect_net = path.join(path_folders_affect_net, f_af)

            images_affect_net = np.sort(np.array(listdir(path_images_affect_net)))
            # Randomize images
            if self.idx_set < 2:
                np.random.shuffle(images_affect_net)

            for file_name_image_affect_net in images_affect_net:
                valence, arousal, quadrant = self._parse_to_label(file_name_image_affect_net)

                if (quadrant >= 0) and (counter_loaded_images_per_label[int(quadrant)] < self.max_loaded_images_per_label):
                    img = np.array(uimage.read(path.join(path_images_affect_net, file_name_image_affect_net)), np.uint8)
                    data_affect_net.append(img)

                    labels_affect_net.append([np.float32(valence), np.float32(arousal)])

                    counter_loaded_images_per_label[int(quadrant)] += 1

                has_loading_finished = (np.sum(counter_loaded_images_per_label) >= (self.max_loaded_images_per_label * self.num_labels))

                if has_loading_finished:
                    break

            if has_loading_finished:
                break

        return [np.array(data_affect_net), np.array(labels_affect_net)]

# AffectNet (Dimensional) <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

# Exteded Cohn-Kanade >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

class CohnKanade(Dataset):
    def __init__(self, idx_test_fold, set_id, num_folds_supervised_training=None,
                 idx_training_labeled_validation=None, transforms=None, base_path_to_dataset=None):
        """
        This class follows the subject-independent 10-fold cross-validation (Liu et al., 2013). With this methodology, we assess the generalization to unseen subjects, which more
        reliably indicates how the network would work in a real-world application than if the validation fold contained the same subjects as the training folds.
        The folds are populated by sampling subjects with a step size of ten after sorting them in ascending order based on their IDs. The first frame is labeled
        as neutral, whereas the last three frames are labeled as one of seven facial expressions, which results in eight categories. In each trial, one fold is
        used for test, one for validation, and eight for training.

        Refs.
        Liu, M., Li, S., Shan, S. and Chen, X. (2013). Au-aware deep networks for facial expression recognition. IEEE International Conference Workshops on Automatic
        Face and Gesture Recognition (FG), 1-6.

        :param idx_test_fold: index of the test set
        :param set_id: 'training_labeled', 'training_labeled_validation', 'training_unlabeled', 'validation', 'test'
        :param num_folds_supervised_training: Number of folds used for the supervised learning phase
        :param transforms: transforms (callable, optional): Optional transform to be applied on a sample.
        """

        self.idx_test_fold = idx_test_fold
        self.base_path_to_dataset = base_path_to_dataset

        if set_id == 'training_labeled' or set_id == 'training_labeled_validation' or set_id == 'training_unlabeled' or set_id == 'validation' or set_id == 'test':
            self.set = set_id
        else:
            raise RuntimeError("The 'set' variable must be 'training_labeled', 'training_unlabeled', 'validation' or 'test'.")

        self.num_folds_supervised_training = num_folds_supervised_training
        self.idx_training_labeled_validation = idx_training_labeled_validation
        self.transforms = transforms
        self.loaded_data = self._load()
        # Default values
        self.mean = 0.0
        self.std = 1.0

    def __len__(self):
        return self.loaded_data[0].shape[0]

    def __getitem__(self, idx):
        sample = {'image': self.loaded_data[0][idx], 'emotion': self.loaded_data[1][idx]}
        sample['image'] = Image.fromarray(sample['image'])

        if not (self.transforms is None):
            sample['image'] = self.transforms(sample['image'])

        return Normalize(mean=[self.mean], std=[self.std])(ToTensor()(sample['image'])), sample['emotion']

    def online_normalization(self, x):
        return Normalize(mean=[self.mean], std=[self.std])(ToTensor()(x))

    def norm_input_to_orig_input(self, x):
        return (x * self.std) + self.mean

    @staticmethod
    def get_class(idx):
        classes = {
            0: 'Anger',
            1: 'Disgust',
            2: 'Fear',
            3: 'Happiness',
            4: 'Sadness',
            5: 'Surprise',
            6: 'Contempt',
            7: 'Neutral'}

        return classes[idx]

    @staticmethod
    def _parse_to_label(idx):
        to_return = np.zeros(8, dtype=np.float32)

        if idx == 0:
            to_return[0] = 1.
        elif idx == 1:
            to_return[6] = 1.
        elif idx == 2:
            to_return[1] = 1.
        elif idx == 3:
            to_return[2] = 1.
        elif idx == 4:
            to_return[3] = 1.
        elif idx == 5:
            to_return[4] = 1.
        elif idx == 6:
            to_return[5] = 1.
        elif idx == -1:
            to_return[7] = 1.

        return np.argmax(to_return)

    def _load(self):
        # Setting the training, validation and tes indexes
        validation_fold_index = (self.idx_test_fold + 1) % 10
        training_fold_index = set(range(10))

        training_fold_index.remove(self.idx_test_fold)
        training_fold_index.remove(validation_fold_index)

        supervised_training_fold_index = list(training_fold_index)[:self.num_folds_supervised_training]

        if not (self.idx_training_labeled_validation is None):
            supervised_training_validation_fold_index = supervised_training_fold_index[self.idx_training_labeled_validation]
            training_fold_index.remove(supervised_training_validation_fold_index)

        training_fold_index = list(training_fold_index)

        supervised_training_fold_index = np.array(training_fold_index[:self.num_folds_supervised_training - 1], dtype=np.uint8)
        unsupervised_training_fold_index = np.array(training_fold_index[self.num_folds_supervised_training - 1:], dtype=np.uint8)

        # Setting the absolute path to the dataset
        path_images = self.base_path_to_dataset + 'cohn-kanade-images/'
        path_labels = self.base_path_to_dataset + 'Emotion/'
        # Setting general variables
        folds_data_index = [[], [], [], [], [], [], [], [], [], []]
        data, labels = [], []
        subject_step = -1
        index_image = 0
        data_file_name = []

        # Loading the dataset
        subjects = np.sort(np.array(listdir(path_labels)))
        for subject in subjects:
            subject_not_counted = True
            path_sessions_images = path_images + subject + '/'
            path_sessions_labels = path_labels + subject + '/'
            sessions = np.sort(np.array(listdir(path_sessions_labels)))

            for session in sessions:
                path_frames = path_sessions_images + session + '/'
                path_classes = path_sessions_labels + session + '/'
                file_class = np.sort(np.array(listdir(path_classes)))
                frames = np.sort(np.array(listdir(path_frames)))

                # Has expression, then load image
                if len(file_class) > 0:
                    if subject_not_counted:
                        subject_not_counted = False
                        subject_step = subject_step + 1 if subject_step < 9 else 0

                    # Adding the facial expression category
                    label_file = 0
                    with open(path_classes + file_class[-1]) as f:
                        for line in f:
                            label_file = np.int16(np.float32(line.split()[0])) - 1
                    for i in range(3):
                        folds_data_index[subject_step].append(index_image)
                        index_image += 1
                        data.append(io.imread(path_frames + frames[-(i + 1)]))
                        data_file_name.append(path_frames + frames[-(i + 1)])
                        labels.append(self._parse_to_label(label_file))

                    # Adding the neutral category example (index equals 7)
                    folds_data_index[subject_step].append(index_image)
                    index_image += 1
                    data.append(io.imread(path_frames + frames[-4]))
                    data_file_name.append(path_frames + frames[-4])
                    labels.append(self._parse_to_label(-1))

        data = np.array(data)
        labels = np.array(labels)
        folds_data_index = np.array(folds_data_index)

        # Setting the training, validation and test data and labels
        # The following line set to_return as training_labeled temporally to get mean and std
        to_return = [np.array(data[[item for sublist in folds_data_index[supervised_training_fold_index] for item in sublist]]),
                     np.array(labels[[item for sublist in folds_data_index[supervised_training_fold_index] for item in sublist]])]

        # Save mean and std from supervised training set for normalization
        self.mean = np.mean(data / 255.)
        self.std = np.std(data / 255.)

        if self.set == 'test':
            to_return = [np.array(data[folds_data_index[self.idx_test_fold]]),
                         np.array(labels[folds_data_index[self.idx_test_fold]])]
        elif self.set == 'validation':
            to_return = [np.array(data[folds_data_index[validation_fold_index]]),
                         np.array(labels[folds_data_index[validation_fold_index]])]
        elif self.set == 'training_labeled_validation':
            to_return = [np.array(data[folds_data_index[supervised_training_validation_fold_index]]),
                         np.array(labels[folds_data_index[supervised_training_validation_fold_index]])]
        elif self.set == 'training_unlabeled':
            to_return = [np.array(data[[item for sublist in folds_data_index[unsupervised_training_fold_index] for item in sublist]]),
                         np.array(labels[[item for sublist in folds_data_index[unsupervised_training_fold_index] for item in sublist]])]
        elif self.set != 'training_labeled':
            to_return = None

        return to_return

# Exteded Cohn-Kanade <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

# Other methods >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

def sort_numeric_directories(dir_names):
    return sorted(dir_names, key=lambda x: (int(re.sub("\D", "", x)), x))

def _generate_single_file_name(img_id, expression, valence, arousal):
    valence = int(valence * 1000)
    arousal = int(arousal * 1000)
    return '%07ds%ds%ds%d.jpg' % (img_id, expression, valence, arousal)

def pre_process_affect_net(base_path_to_images, base_path_to_annotations, set_index):
    """
    Pre-process the AffectNet dataset. Faces are cropped and resized to 96 x 96 pixels.
    The images are organized in folders with 500 images each. The test set had not been released
    when this experiment was carried out.

    :param base_path_to_images: (string) Path to images.
    :param base_path_to_annotations: (string) Path to annotations.
    :param set_index: (int = {0, 1, 2}) set_index = 0 process the automatically annotated images.
                                        set_index = 1 process the manually annotated images: training set.
                                        set_index = 2 process the manually annotated images: validation set.
    :return: (void)
    """

    assert ((set_index < 3) and (set_index >= 0)), "set_index must be 0, 1 or 2."

    annotation_folders = ['Automatically_Annotated_Images/', 'Manually_Annotated_Images/',
                          'Manually_Annotated_Images/']
    destination_set_folders = ['AffectNet/Training_Unlabeled/', 'AffectNet/Training_Labeled/',
                               'AffectNet/Validation/']
    annotation_file_names = ['automatically_annotated.csv', 'Manually_training.csv', 'Manually_validation.csv']

    image_id = 0
    error_image_id = []
    img_size = (96, 96)
    num_images_per_folder = 500

    annotation_file = pandas.read_csv(path.join(base_path_to_annotations, annotation_file_names[set_index]))

    for line in range(image_id, annotation_file.shape[0]):
        try:
            # Read image
            img_file_name = annotation_file.get('subDirectory_filePath')[line]
            img_full_path = path.join(base_path_to_images, annotation_folders[set_index], img_file_name)
            img = uimage.read(img_full_path)

            # Crop face
            x = int(annotation_file.get('face_x')[line])
            y = int(annotation_file.get('face_y')[line])
            w = int(annotation_file.get('face_width')[line])
            h = int(annotation_file.get('face_height')[line])
            img = img[x:x + w, y:y + h, :]

            # Resize image
            img = uimage.resize(img, img_size)

            # Save image
            folder = str(image_id // num_images_per_folder)
            exp = annotation_file.get('expression')[line]
            val = annotation_file.get('valence')[line]
            aro = annotation_file.get('arousal')[line]
            file_name = _generate_single_file_name(image_id, exp, val, aro)
            uimage.write(img, path.join(base_path_to_images, destination_set_folders[set_index], folder), file_name)
            image_id += 1
        except Exception:
            print('ERROR: The image ID %d is corrupted.' % image_id)
            error_image_id.append(image_id)

    print('Dataset has been processed.')
    print('Images successfully processed: %d' % (image_id - len(error_image_id)))
    print('Images processed with error: %d' % len(error_image_id))
    print('Image IDs processed with error: %s' % error_image_id)

# Other methods <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<


text:
# -*- coding: utf-8 -*-
import random
import numpy as np

def rand(length, black_list):
    index = random.randint(1, length - 1)
    while index in black_list:
        index = random.randint(1, length - 1)
    return index

def load_embedding(filename):
    embeddings = list()
    word2idx = dict()
    print('start loading embedding')
    with open(filename, mode='r', encoding='utf-8') as f:
        for line in f:
            arr = line.strip().split(' ')
            embedding = [float(val) for val in arr[1:len(arr)]]
            word2idx[arr[0]] = len(word2idx)
            embeddings.append(embedding)

    embedding_size = len(arr) - 1
    word2idx['UNKNOWN'] = len(word2idx)
    embeddings.append([0] * embedding_size)

    print('embedding loaded')
    return embeddings, word2idx

def load_vocab(filename):
    voc = {}
    for line in open(filename):
        word, _id = line.strip().split('\t')
        voc[word] = _id
    return voc

def load_answers(filename, voc):
    answers = ['<None>']
    for line in open(filename):
        _, sent = line.strip().split('\t')
        answers.append([voc[wid] for wid in sent.split(' ')])
    return answers

def encode_sent(sentence, word2idx, size):
    unknown = word2idx.get("UNKNOWN", 0)
    x = [unknown] * size
    for i, word in enumerate(sentence):
        if word in word2idx:
            x[i] = word2idx[word]
        else:
            x[i] = unknown
        if i >= size - 1:
            break
    return x

def load_train_data(filename, all_answers, voc, word2idx, seq_size):
    questions, pos_answers, neg_answers = [], [], []
    for line in open(filename):
        qsent, ids = line.strip().split('\t')
        ids = [int(_id) for _id in ids.split(' ')]
        question = [voc[idx] for idx in qsent.split(' ')]
        question = encode_sent(question, word2idx, seq_size)
        for _id in ids:
            for i in range(50):
                questions.append(question)
                pos_answer = encode_sent(all_answers[_id], word2idx, seq_size)
                pos_answers.append(pos_answer)
                # Negative samples are randomly sampled
                neg_answer = encode_sent(all_answers[rand(len(all_answers), ids)], word2idx, seq_size)
                neg_answers.append(neg_answer)
    return np.array(questions), np.array(pos_answers), np.array(neg_answers)

def load_test_data(filename, all_answers, voc, word2idx, seq_size):
    questions, answers, labels, qids, aids = [], [], [], [], []
    qid = 0
    for line in open(filename):
        apids, qsent, anids = line.strip().split('\t')
        apids = [int(_id) for _id in apids.split(' ')]
        anids = [int(_id) for _id in anids.split(' ')]
        question = [voc[idx] for idx in qsent.split(' ')]
        question = encode_sent(question, word2idx, seq_size)
        aid = 0
        for _id in apids:
            questions.append(question)
            answer = encode_sent(all_answers[_id], word2idx, seq_size)
            answers.append(answer)
            labels.append(1)
            qids.append(qid)
            aids.append(aid)
            aid += 1
        for _id in anids:
            questions.append(question)
            answer = encode_sent(all_answers[_id], word2idx, seq_size)
            answers.append(answer)
            labels.append(0)
            qids.append(qid)
            aids.append(aid)
            aid += 1
        qid += 1
    return np.array(questions), np.array(answers), np.array(labels), np.array(qids), np.array(aids)

def batch_iter(questions, pos_answers, neg_answers, batch_size):
    data_size = len(questions)
    batch_num = int(data_size / batch_size)
    for batch in range(batch_num):
        result_questions, result_pos_answers, result_neg_answers = [], [], []
        for i in range(batch * batch_size, min((batch + 1) * batch_size, data_size)):
            result_questions.append(questions[i])
            result_pos_answers.append(pos_answers[i])
            result_neg_answers.append(neg_answers[i])
        yield result_questions, result_pos_answers, result_neg_answers

def test_batch_iter(questions, answers, batch_size):
    data_size = len(questions)
    batch_num = int(data_size / batch_size)
    for batch in range(batch_num):
        result_questions, result_answers = [], []
        for i in range(batch * batch_size, min((batch + 1) * batch_size, data_size)):
            result_questions.append(questions[i])
            result_answers.append(answers[i])
        yield result_questions, result_answers

if __name__ == '__main__':
    embeddings, word2idx = load_embedding('vectors.nobin')
    voc = load_vocab('D:\\DataMining\\Datasets\\insuranceQA\\V1\\vocabulary')
    all_answers = load_answers('D:\\DataMining\\Datasets\\insuranceQA\\V1\\answers.label.token_idx', voc)
    # questions, pos_answers, neg_answers = load_train_data('D:\\DataMining\\Datasets\\insuranceQA\\V1\\question.train.token_idx.label', all_answers, voc, word2idx, 100)
    questions, answers, labels, qids = load_test_data('D:\\DataMining\\Datasets\\insuranceQA\\V1\\question.test1.label.token_idx.pool', all_answers, voc, word2idx, 100)


text:
import io
import sentencepiece as spm
from scipy.stats import spearmanr
from scipy.stats import pearsonr
from utils import Example, unk_string
from sacremoses import MosesTokenizer

def get_sequences(p1, p2, model, params, fr0=0, fr1=0):
    wp1 = Example(p1)
    wp2 = Example(p2)

    if fr0==1 and fr1==1 and not model.share_vocab:
        wp1.populate_embeddings(model.vocab_fr, model.zero_unk, params.ngrams)
        wp2.populate_embeddings(model.vocab_fr, model.zero_unk, params.ngrams)
        if len(wp1.embeddings) == 0:
            wp1.embeddings.append(model.vocab_fr[unk_string])
        if len(wp2.embeddings) == 0:
            wp2.embeddings.append(model.vocab_fr[unk_string])
    elif fr0==0 and fr1==1 and not model.share_vocab:
        wp1.populate_embeddings(model.vocab, model.zero_unk, params.ngrams)
        wp2.populate_embeddings(model.vocab_fr, model.zero_unk, params.ngrams)
        if len(wp1.embeddings) == 0:
            wp1.embeddings.append(model.vocab[unk_string])
        if len(wp2.embeddings) == 0:
            wp2.embeddings.append(model.vocab_fr[unk_string])
    else:
        wp1.populate_embeddings(model.vocab, model.zero_unk, params.ngrams)
        wp2.populate_embeddings(model.vocab, model.zero_unk, params.ngrams)
        if len(wp1.embeddings) == 0:
            wp1.embeddings.append(model.vocab[unk_string])
        if len(wp2.embeddings) == 0:
            wp2.embeddings.append(model.vocab[unk_string])

    return wp1, wp2

def get_correlation(model, f, params, tok1, tok2, fr0=0, fr1=0):
    f = io.open(f, 'r', encoding='utf-8')
    lines = f.readlines()
    preds = []
    golds = []
    seq1w = []
    seq2w = []

    ct = 0
    for n,i in enumerate(lines):

        i = i.split("\t")
        if len(i) != 3 or len(i[-1].strip()) == 0:
            continue

        p1, p2, score = (i[0], i[1], float(i[2]))
        p1 = " ".join(tok1.tokenize(p1, escape=False))
        p2 = " ".join(tok2.tokenize(p2, escape=False))
        p1 = p1.lower()
        p2 = p2.lower()
        if model.sp is not None:
            p1 = model.sp.EncodeAsPieces(p1)
            p2 = model.sp.EncodeAsPieces(p2)
            p1 = " ".join(p1)
            p2 = " ".join(p2)

        wX1, wX2 = get_sequences(p1, p2, model, params, fr0, fr1)

        seq1w.append(wX1)
        seq2w.append(wX2)

        ct += 1
        if ct % 100 == 0:
            wx1, wl1 = model.torchify_batch(seq1w)
            wx2, wl2 = model.torchify_batch(seq2w)
            scores = model.scoring_function(wx1, wl1, wx2, wl2, fr0, fr1)
            preds.extend(scores.data.cpu().numpy().tolist())
            seq1w = []
            seq2w = []

        golds.append(score)

    if len(seq1w) > 0:
        wx1, wl1 = model.torchify_batch(seq1w)
        wx2, wl2 = model.torchify_batch(seq2w)
        scores = model.scoring_function(wx1, wl1, wx2, wl2, fr0, fr1)
        preds.extend(scores.data.cpu().numpy().tolist())

    return pearsonr(preds, golds)[0], spearmanr(preds, golds)[0]

def evaluate(model, params):
    assert not model.training

    entok = MosesTokenizer(lang='en')
    estok = MosesTokenizer(lang='es')
    artok = MosesTokenizer(lang='ar')
    trtok = MosesTokenizer(lang='tr')

    f = "STS/STS17-test/STS.input.track1.ar-ar.txt"
    p,s = get_correlation(model, f, params, artok, artok, fr0=1, fr1=1)
    print("track1.ar-ar.txt\tpearson: {:.3f}\tspearman: {:.3f}".format(p*100, s*100))

    f = "STS/STS17-test/STS.input.track2.ar-en.txt"
    p,s = get_correlation(model, f, params, entok, artok, fr0=0, fr1=1)
    print("track2.ar-en.txt\tpearson: {:.3f}\tspearman: {:.3f}".format(p*100, s*100))

    f = "STS/STS17-test/STS.input.track3.es-es.txt"
    p,s = get_correlation(model, f, params, estok, estok, fr0=0, fr1=1)
    print("track3.es-es.txt\tpearson: {:.3f}\tspearman: {:.3f}".format(p*100, s*100))

    f = "STS/STS17-test/STS.input.track4a.es-en.txt"
    p,s = get_correlation(model, f, params, entok, estok, fr0=0, fr1=1)
    print("track4a.es-en.txt\tpearson: {:.3f}\tspearman: {:.3f}".format(p*100, s*100))

    f = "STS/STS17-test/STS.input.track5.en-en.txt"
    p,s = get_correlation(model, f, params, entok, entok, fr0=0, fr1=0)
    print("track5.en-en.txt\tpearson: {:.3f}\tspearman: {:.3f}".format(p*100, s*100))

    f = "STS/STS17-test/STS.input.track6.tr-en.txt"
    p,s = get_correlation(model, f, params, entok, trtok, fr0=0, fr1=1)
    print("track6.tr-en.txt\tpearson: {:.3f}\tspearman: {:.3f}".format(p*100, s*100))

reason: from scipy


text:
"""
Greedy layerwise training for weakly-supervised solar panel segmentation.
We need to train first layer in the branch and then train the second layer
but fix the first layer. FLAGS.two_layers determines which mode to run.
"""

import sys
import copy
from datetime import datetime
import os.path
import re
import time

import numpy as np
import tensorflow as tf
import skimage
import skimage.io
import skimage.transform
import random
import pickle
from collections import deque

from inception import inception_model as inception
from inception.slim import slim

FLAGS = tf.app.flags.FLAGS

tf.app.flags.DEFINE_string('two_layers', False,
                           """ If true, two layers in the segmentation branch,
                           else one layer. """)

tf.app.flags.DEFINE_string('ckpt_save_dir', 'ckpt/inception_segmentation',
                           """Directory for saving model checkpoint. """)

tf.app.flags.DEFINE_string('ckpt_restore_dir', 'ckpt/inception_segmentation',
                           """Directory for restoring first layer. """)

tf.app.flags.DEFINE_string('classification_ckpt_restore_dir', 'ckpt/inception_classification',
                           """ Directory for restoring parameters of classification model. """)

tf.app.flags.DEFINE_integer('max_steps', 15000,
                            """Number of batches to run.""")

tf.app.flags.DEFINE_float('learning_rate', 0.005,
                          """learning rate.""")

# basic parameters
BATCH_SIZE = 64
IMAGE_SIZE = 299
NUM_CLASSES = 2

def load_image(path):
    # load and prepocess image
    rotate_angle_list = [0, 90, 180, 270]
    img = skimage.io.imread(path)
    resized_img = skimage.transform.resize(img, (IMAGE_SIZE, IMAGE_SIZE))
    if resized_img.shape[2] != 3:
        resized_img = resized_img[:, :, 0:3]
    rotate_angle = random.choice(rotate_angle_list)
    image = skimage.transform.rotate(resized_img, rotate_angle)
    return image

def train():
    # load train set list and transform it to queue. For time concern, we recommmend use a subset of training set
    # to fine-tune the segmentation branch.
    try:
        with open('train_set_list.pickle', 'r') as f:
            train_set_list = pickle.load(f)
    except:
        raise EnvironmentError('Data list not existed. Please run generate_data_list.py first.')
    random.shuffle(train_set_list)
    train_set_queue = deque(train_set_list)
    train_set_size = len(train_set_list)
    del train_set_list
    print ('Training set built. Size: '+str(train_set_size))

    # build the tensorflow graph
    with tf.Graph().as_default() as g:
        images = tf.placeholder(tf.float32, shape=[BATCH_SIZE, IMAGE_SIZE, IMAGE_SIZE, 3])

        labels = tf.placeholder(tf.int32, shape=[BATCH_SIZE])

        # get the feature map after layer 'mixed_35x35x288b' from classification model.
        _, _, feature_map = inception.inference(images, NUM_CLASSES)

        with tf.name_scope('conv_aux_1') as scope:
            kernel1 = tf.Variable(tf.truncated_normal([3, 3, 288, 512], dtype=tf.float32, stddev=1e-4), name='weights')
            conv = tf.nn.conv2d(feature_map, kernel1, [1, 1, 1, 1], padding='SAME')
            biases1 = tf.Variable(tf.constant(0.1, shape=[512], dtype=tf.float32), trainable=True, name='biases')
            bias = tf.nn.bias_add(conv, biases1)
            conv_aux = tf.nn.relu(bias, name=scope)

        if FLAGS.two_layers:
            with tf.name_scope('conv_aux_2') as scope:
                kernel2 = tf.Variable(tf.truncated_normal([3, 3, 512, 512], dtype=tf.float32, stddev=1e-4), name='weights')
                conv = tf.nn.conv2d(conv_aux, kernel2, [1, 1, 1, 1], padding='SAME')
                biases2 = tf.Variable(tf.constant(0.1, shape=[512], dtype=tf.float32), trainable=True, name='biases')
                bias = tf.nn.bias_add(conv, biases2)
                conv_aux = tf.nn.relu(bias, name=scope)

        # global average pooling.
        GAP = tf.reduce_mean(conv_aux, [1, 2])

        # linear classifier.
        W = tf.get_variable(name='W', shape=[512, 2], initializer=tf.random_normal_initializer(0., 0.01))
        logits = tf.matmul(GAP, W)

        # compute loss.
        cross_entropy = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(logits, labels))

        if FLAGS.two_layers:
            train_step = tf.train.GradientDescentOptimizer(FLAGS.learning_rate).minimize(cross_entropy,
                                                                                var_list=[W, kernel2, biases2])
            saver = tf.train.Saver(var_list=[W, kernel2, biases2, kernel1, biases1])
        else:
            train_step = tf.train.GradientDescentOptimizer(FLAGS.learning_rate).minimize(cross_entropy,
                                                                                var_list=[W, kernel1, biases1])
            saver = tf.train.Saver(var_list=[W, kernel1, biases1])

        init = tf.global_variables_initializer()

        # open session and initialize
        sess = tf.Session(config=tf.ConfigProto(
            log_device_placement=True))
        sess.run(init)

        # restore old checkpoint
        variables_to_restore = tf.get_collection(
            slim.variables.VARIABLES_TO_RESTORE)
        restorer1 = tf.train.Saver(variables_to_restore)

        checkpoint1 = tf.train.get_checkpoint_state(FLAGS.classification_ckpt_restore_dir)
        if checkpoint1 and checkpoint1.model_checkpoint_path:
            restorer1.restore(sess, checkpoint1.model_checkpoint_path)
            print("Successfully loaded:", checkpoint1.model_checkpoint_path)
        else:
            print("Could not find old network weights")

        # If adding second layer, then the first layer should be restored.
        if FLAGS.two_layers:
            restorer2 = tf.train.Saver(var_list=[kernel1, biases1])
            checkpoint2 = tf.train.get_checkpoint_state(FLAGS.ckpt_restore_dir)
            if checkpoint2 and checkpoint2.model_checkpoint_path:
                restorer2.restore(sess, checkpoint2.model_checkpoint_path)
                print("Successfully loaded:", checkpoint2.model_checkpoint_path)
            else:
                print("Could not find old network weights")

        step = 1
        train_record = []
        while step <= FLAGS.max_steps:
            start_time = time.time()
            # construct image batch and label batch for one step train.
            minibatch = []
            for count in xrange(0, BATCH_SIZE):
                element = train_set_queue.pop()
                minibatch.append(element)
                train_set_queue.appendleft(element)

            image_list = [load_image(d[0]) for d in minibatch]
            label_list = [d[1] for d in minibatch]

            image_batch = np.array(image_list)
            label_batch = np.array(label_list)

            image_batch = np.reshape(image_batch, [BATCH_SIZE, IMAGE_SIZE, IMAGE_SIZE, 3])
            label_batch = np.reshape(label_batch, [BATCH_SIZE])

            _, loss_value = sess.run([train_step, cross_entropy], feed_dict={images: image_batch, labels: label_batch})

            duration = time.time() - start_time

            assert not np.isnan(loss_value), 'Model diverged with loss = NaN'

            if step == 1 or step % 10 == 0:
                num_examples_per_step = BATCH_SIZE
                examples_per_sec = num_examples_per_step / duration
                sec_per_batch = float(duration)

                format_str = ('%s: step %d, loss = %.2f (%.1f examples/sec; %.3f '
                              'sec/batch)')

                print(format_str % (datetime.now(), step, loss_value,
                                    examples_per_sec, sec_per_batch))

            # Save the model checkpoint periodically.
            if step == 1 or step % 1000 == 0:
                checkpoint_path = os.path.join(FLAGS.ckpt_save_dir, 'model.ckpt')
                saver.save(sess, checkpoint_path, global_step=step)

            step += 1

if __name__ == '__main__':
    train()

max_stars_repo_name: jdailey/solarpanelcount

max_issues_repo_name: jdailey/solarpanelcount

max_forks_repo_name: jdailey/solarpanelcount


text:
import numpy as np
import random

from src.ac_agent import MAgentDDPG, GaussianProcess
from src.utils import transpose_to_tensor

def instanciate():
    #

    RND_SEED = 123
    # Problem
    N_EPISODES = 1
    MAX_STEPS = 20
    SOLVED_AT = -.5
    GAMMA = 1  # Discount factor

    # Noise
    NOISE_MU = 0
    NOISE_SIGMA = 0.1
    NOISE_DECAY = 1.
    NOISE_MIN_WEIGHT = 1.

    # Agent
    ACT_HID_LAYERS = (8, 4)
    CRIT_HID_LAYERS = (8, 4)
    ACT_ADD_BN = (False)
    CRIT_ADD_BN = (False)
    GRAD_CLIP = (False, False)  # (actor, critic)
    BATCH_SIZE = 1
    LEARNING_RATES = (1e-3, 1e-3)  # (actor, critic)
    WEIGHT_DECAY = (0, 0)  # (actor, critic)
    SOFT_UPD_PARAM = 1e-3
    UPDATE_EVERY = 1
    BUFFER_SIZE = int(1e6)
    LEARN_EVERY = 1
    LEARN_NUM = 1

    g_noise = GaussianProcess(action_size, RND_SEED, mu=NOISE_MU, sigma=NOISE_SIGMA)

    maddpg = MAgentDDPG(num_agents=num_agents, state_size=state_size, action_size=action_size, gamma=GAMMA,
                         actor_hidden_layers=num_agents*[ACT_HID_LAYERS],
                         critic_hidden_layers=num_agents*[CRIT_HID_LAYERS],
                         actor_add_bn=num_agents*[ACT_ADD_BN],
                         critic_add_bn=num_agents*[CRIT_ADD_BN],
                         grad_clipping=num_agents*[GRAD_CLIP],
                         learning_rates=num_agents*[LEARNING_RATES],
                         weight_decay=num_agents*[WEIGHT_DECAY],
                         batch_size=BATCH_SIZE,
                         soft_upd_param=SOFT_UPD_PARAM,
                         update_every=UPDATE_EVERY,
                         buffer_size=BUFFER_SIZE,
                         noise=g_noise,
                         learn_every=LEARN_EVERY,
                         learn_num=LEARN_NUM,
                         seed=RND_SEED)

    return maddpg

def act(obs):
    actions = maddpg.act(obs, add_noise=True, noise_weight=1.)
    return actions

def step(states, actions, rewards, next_states, dones, t_step=1):
    maddpg.step(states, actions, rewards, next_states, dones, t_step)

def tensor_to_lst(obs_full, num_agents, space_size):
    obs_full_lst = []
    for agent_idx in range(num_agents):
        col_idx_left = agent_idx * space_size
        col_idx_right = (agent_idx + 1) * space_size
        obs_full_lst.append(obs_full[:, col_idx_left:col_idx_right])

    return obs_full_lst

class Env(object):
    def __init__(self, num_agents, state_size, action_size, seed=123):
        self.num_agents = num_agents
        self.state_size = state_size
        self.action_size = action_size
        self.seed = seed
        random.seed(seed)

        self.t_steps = 0
        self.n_episodes = 0

    def step(self, actions):
        next_states = np.random.randn(self.num_agents, self.state_size)
        rewards = self.num_agents*[0.]
        dones = self.num_agents*[False]
        env_info = (next_states, rewards, dones)

        return env_info

    def reset(self):
        return np.random.randn(self.num_agents, self.state_size)

if __name__ == '__main__':
    num_agents = 2
    action_size = 2
    state_size = 24

    env = Env(num_agents, state_size, action_size)
    single_obs = env.reset()
    maddpg = instanciate()

    ag1_local_params, ag2_local_params = [], []
    trans_hist = []

    t_step = 0
    actions = act(single_obs)
    single_next_obs, rewards, dones = env.step(actions)
    trans_hist.append((single_obs, actions))
    step(single_obs, actions, rewards, single_next_obs, dones, t_step)
    single_obs = single_next_obs

    t_step = 1
    actions = act(single_obs)
    single_next_obs, rewards, dones = env.step(actions)
    trans_hist.append((single_obs, actions))

    ###
    buffer = maddpg.shared_memory
    experiences = buffer.sample_shared()

    obs_full, actions_full, rewards, next_obs_full, dones = experiences

    agent_idx = 0
    obs_full_lst = tensor_to_lst(obs_full, num_agents, state_size)
    np.testing.assert_almost_equal(
        obs_full.numpy()[:, :state_size].reshape(-1),
        trans_hist[0][0][agent_idx, :]
    )
    np.testing.assert_almost_equal(
        obs_full_lst[0].view(-1),
        trans_hist[0][0][agent_idx, :]
    )
    np.testing.assert_almost_equal(
        actions_full.numpy()[:, :action_size].reshape(-1),
        trans_hist[0][1][agent_idx, :]
    )

    agent_idx = 1
    np.testing.assert_almost_equal(
        obs_full_lst[1].view(-1),
        trans_hist[0][0][agent_idx, :]
    )
    np.testing.assert_almost_equal(
        obs_full.numpy()[:, state_size:].reshape(-1),
        trans_hist[0][0][agent_idx, :]
    )
    np.testing.assert_almost_equal(
        actions_full.numpy()[:, action_size:].reshape(-1),
        trans_hist[0][1][agent_idx, :]
    )

    ###

    step(single_obs, actions, rewards, single_next_obs, dones, t_step)
    single_obs = single_next_obs

    t_step = 2
    actions = act(single_obs)
    single_next_obs, rewards, dones = env.step(actions)
    step(single_obs, actions, rewards, single_next_obs, dones, t_step)
    single_obs = single_next_obs

    t_step = 3
    actions = act(single_obs)
    single_next_obs, rewards, dones = env.step(actions)
    step(single_obs, actions, rewards, single_next_obs, dones, t_step)
    single_obs = single_next_obs
    ag1_local_params.append(maddpg.maddpg_agent[0].actor_local.fc1.weight)
    ag2_local_params.append(maddpg.maddpg_agent[1].actor_local.fc1.weight)

max_stars_repo_name: manualrg/deep-reinforcement-learning

max_issues_repo_name: manualrg/deep-reinforcement-learning

max_forks_repo_name: manualrg/deep-reinforcement-learning


text:
import os
import numpy as np
from ..utils import download_dataset
import time
import io
from tqdm import tqdm
import matplotlib.image as mpimg
from zipfile import ZipFile

_source = "https://github.com/mloey/Arabic-Handwritten-Digits-Dataset"

cite = """
@inproceedings{el2016cnn,
  title={CNN for handwritten arabic digits recognition based on LeNet-5},
  author={El-Sawy, Ahmed and Hazem, EL-Bakry and Loey, Mohamed},
  booktitle={International conference on advanced intelligent systems and informatics},
  pages={566--575},
  year={2016},
  organization={Springer}
}"""

_name = "arabic_digits"

_urls = {
    "https://github.com/mloey/Arabic-Handwritten-Digits-Dataset/raw/master/Test%20Images.zip": "TestImages.zip",
    "https://github.com/mloey/Arabic-Handwritten-Digits-Dataset/raw/master/Train%20Images.zip": "TrainImages.zip",
}

def load(path=None):
    """Arabic Handwritten Digits Dataset

    Parameters
    ----------

        path: str (optional)
            default ($DATASET_PATH), the path to look for the data and
            where the data will be downloaded if not present

    Returns
    -------

        train_images: array

        train_labels: array

        valid_images: array

        valid_labels: array

        test_images: array

        test_labels: array

    """

    if path is None:
        path = os.environ["DATASET_PATH"]

    t0 = time.time()
    download_dataset(path, _name, _urls)
    train_images = []
    test_images = []
    train_labels = []
    test_labels = []
    with ZipFile(os.path.join(path, _name, "TestImages.zip")) as archive:
        for entry in tqdm(archive.infolist()):
            if ".png" not in entry.filename:
                continue
            content = archive.read(entry)
            test_images.append(mpimg.imread(io.BytesIO(content), "png"))
            test_labels.append(int(entry.filename.split("_")[-1][:-4]))

    with ZipFile(os.path.join(path, _name, "TrainImages.zip")) as archive:
        for entry in tqdm(archive.infolist(), ascii=True):
            if ".png" not in entry.filename:
                continue
            content = archive.read(entry)
            train_images.append(mpimg.imread(io.BytesIO(content), "png"))
            train_labels.append(int(entry.filename.split("_")[-1][:-4]))

    data = {
        "train_set/images": np.array(train_images),
        "train_set/labels": np.array(train_labels),
        "test_set/images": np.array(test_images),
        "test_set/labels": np.array(test_labels),
    }

    print("Dataset arabic_digits loaded in {0:.2f}s.".format(time.time() - t0))

    return data

max_stars_repo_name: RandallBalestriero/numpy-datasets

max_issues_repo_name: RandallBalestriero/numpy-datasets

max_forks_repo_name: RandallBalestriero/numpy-datasets


text:
import pyautogui as pag
import os
import numpy as np
import cv2
from functools import reduce
import operator
from .positions import get_position_dict
import platform

system = platform.system()
if system == "Darwin":
    from mss.darwin import MSS as mss  # MacOS X
elif system == "Windows":
    from mss.windows import MSS as mss  # Microsoft Windows
elif system == "Linux":
    from mss.linux import MSS as mss  # GNU/Linux
else:
    raise OSError(f"You are using {system}, an os I wasn't expecting. Let me know.")

pag.PAUSE = 0.2
pag.FAILSAFE = True

class BaseGame:
    """
    Base class whenever a macro session starts.
    Instantiates all button locations in the form
    self.coords[<menu_title>][<button_name>] (self.coords["Inventory"]["Helm"])

    Most pressing TODO's are Adventure Attack Buttons and Blood Magic Buttons
    """

    def __init__(self, firefox=True, locate=True):
        self.mac = system == "Darwin"
        self.firefox = firefox
        self.reference = [0, 0]
        if locate:
            self._locate_reference()
        _, _, self.coords = get_position_dict()

    def _shift(self, coord):
        return list(np.add(coord, self.reference))

    def _focus_window(self):
        os.system("open -a firefox")
        pag.move([10, 10])
        pag.move([-10, -10])

    def _click_focus(self):
        pag.click(self.reference)

    def _locate_reference(self):
        if self.firefox:
            self._focus_window()
        with mss() as sct:
            filename = sct.shot(mon=-1)

        file = os.path.join(os.path.dirname(__file__), "top_left.png")
        needle = cv2.imread(file, cv2.IMREAD_COLOR)
        needleHeight, needleWidth = needle.shape[:2]
        haystack = cv2.imread(filename, cv2.IMREAD_COLOR)
        result = cv2.matchTemplate(needle, haystack, cv2.TM_CCOEFF_NORMED)
        match_indices = np.arange(result.size)[(result > 0.95).flatten()]
        matches = np.unravel_index(match_indices[:100], result.shape)
        os.remove(filename)

        if len(matches[0]) > 1:  # multiple matches, we take the first one
            matchx = np.array(matches[1][0]) + needleWidth / 2
            matchy = np.array(matches[0][0]) + needleHeight / 2
        else:
            matchx = np.array(matches[1]) + needleWidth / 2
            matchy = np.array(matches[0]) + needleHeight / 2
        if self.mac:  # double resolution for some reason
            matchx /= 2
            matchy /= 2
        self.reference = [
            int(matchx) - 10,
            int(matchy) - 10,
        ]
        if not self.firefox:
            self._click_focus()
        # print(self.reference)

    def _search(self, haystack, needle, path=None):
        if path is None:
            path = []
        if needle in haystack:
            path.append(needle)
            return path
        for k, v in haystack.items():
            if isinstance(v, dict):
                result = self._search(v, needle, path + [k])
                if result is not None:
                    return result

    def _search_value(self, haystack, needle, path=None):
        if path is None:
            path = []
        if needle in haystack:
            path.append(haystack[needle]["Button"])
            return path
        for k, v in haystack.items():
            if isinstance(v, dict):
                result = self._search_value(v, needle, path + [v["Button"]])
                if result is not None:
                    return result

    def _getFromDict(self, mapList):
        return reduce(operator.getitem, mapList, self.coords)

    def click(self, x, traverse=False):
        path = self._search(self.coords, x)
        if traverse:
            for i in range(1, len(path)):
                ppath = path[:i]
                pag.click(self._shift(self._getFromDict(ppath)["Button"]))
        pag.click(self._shift(self._getFromDict(path)["Button"]))

    def move(self, x, traverse=False):
        path = self._search(self.coords, x)
        if traverse:
            for i in range(1, len(path)):
                ppath = path[:i]
                pag.click(self._shift(self._getFromDict(ppath)["Button"]))
        pag.moveTo(self._shift(self._getFromDict(path)["Button"]))

max_stars_repo_name: bvarjavand/ngl-bot

max_issues_repo_name: bvarjavand/ngl-bot

max_forks_repo_name: bvarjavand/ngubot


text:
#!/usr/bin/env python

"train and validate logistic regression and random forest"

import numpy as np
import pandas as pd

from sklearn import cross_validation as CV

from sklearn.linear_model import LogisticRegression as LR
from sklearn.ensemble import RandomForestClassifier as RF

from sklearn.metrics import roc_auc_score as AUC

#

train = pd.read_csv( 'data/train.csv' )

x = train.drop( [ 'TARGET', 'ID' ], axis = 1 )
y = train.TARGET

x_train, x_test, y_train, y_test = CV.train_test_split( x, y, test_size = 0.2 )

#

print "LR..."

clf = LR()
clf.fit( x_train, y_train )

# predict

p = clf.predict_proba( x_test )[:,1]
auc = AUC( y_test, p )
print "AUC: {:.2%}".format( auc )

# AUC: 58.30%

#

print "RF..."

clf = RF( n_estimators = 100, verbose = True, n_jobs = -1 )
clf.fit( x_train, y_train )

p = clf.predict_proba( x_test )[:,1]
auc = AUC( y_test, p )
print "AUC: {:.2%}".format( auc )

# AUC: 75.32%

max_stars_repo_name: zygmuntz/adversarial-validation

max_issues_repo_name: zygmuntz/adversarial-validation

max_forks_repo_name: zygmuntz/adversarial-validation


text:
import streamlit as st
import pydeck as pdk
import numpy as np
import pandas as pd
import plotly.express as px

# internal modules
from data_proc import procees_df

import datetime

# App layout #
st.title('La Floresta Tree Finder')

# Possible Filters

#Attribute               Filter implementation

#Código	             	Yes
#Código de Especie	 	Yes
#Especie 	         	Yes
#Nombre común	    	Yes
#Altura (m)	         	Yes (Height Range?)
#Circunferencia (m)   	Yes (Circunference range)
#DAP (m)				 	Yes (DAP range)
#D. de copa N-S (m)   	?¿
#D. de copa E-O (m)	 	?¿
#Fecha	          	 	Yes (Date range)
#Latitud	Longitud	 	Unnecesary
#Dirección			 	Unnecesary (Hard to query)
#Observaciones	     	Depending on the consistency
#ID Foto Tronco	     	Maybe
#ID Foto Hojas		 	Maybe
#ID Foto inflorescencia	Maybe
#ID Foto copa			Maybe

# Query the trees data

# Getting the data from Google Drive and processing it to add new needed columns.

# The following lines may change
#---------------------------------------------------------------------------------------#
raw_data = pd.read_csv("results.csv")

raw_data["Altura (m)"] = raw_data["Altura (m)"].apply(
													lambda x: float(x))

raw_data["Circunferencia (m)"] = raw_data["Circunferencia (m)"].apply(
												lambda x: float(x))

raw_data["DAP (m)"] = raw_data["DAP (m)"].apply(
												lambda x: float(x))

raw_data['Fecha'] = pd.to_datetime(raw_data['Fecha'],format='%d/%m/%Y').dt.date

df_display = raw_data

map_data, species_data, obs_list = procees_df(df_display)
#---------------------------------------------------------------------------------------#

print(map_data)
Atributes = ["lat",
			"lon",
			"Código",
			'Tronco ID',
			'Hojas ID',
			'Copa ID'
			]

map_data = map_data[Atributes].dropna()

# Pydeck
#----------------------------------------------------------------------------------------#
# Display the Map Data
#st.map(map_data) <- Simpler function but almost no customization
# Adding code so we can have map default to the center of the data

#https://drive.google.com/file/d/19Xmp0UlcfBU85EdLLV8eQDsJ_5KpLWRu/view?usp=sharing

#----------------------------------------------------------------------------------------#
# FORM

query = {}
with st.form(key='tree_query'):
	query_expr = []
	st.header('Opciones de Búsqueda')
	#

	col1, col2 = st.beta_columns(2)

	codigo = col1.text_input("Código")
	if codigo != "":
		query_expr.append("Código == @codigo")

	col1.subheader('Especie')

	# The next 4 fields should be linked (The user should only be able to select one)

	codigo_de_especie = col1.multiselect("Código de Especie",
											species_data["Código"] + [],
											default = [] )

	especie = col1.multiselect("Especies", species_data["Nombre"] + [],
										 default = [])

	nombre_comun = col1.multiselect("Nombre común",
									species_data["Nombre Común"] + [],
									default = [])

	if 	nombre_comun != [] or especie !=[] or nombre_comun != []:

		query_expr.append("`Código de Especie` == @codigo_de_especie or "
						" Especie == @especie or "
						"`Nombre común` == @nombre_comun")

	col2.subheader('Dimensiones')
	#
	altura = col2.slider('Rango de altura', 0.0, 50.0, (0., 50.0))
	circunferencia = col2.slider('Rango de circunferencia',
										0.0, 5.0, (0., 5.0))

	dap = col2.slider('Rango de DAP',
										0.0, 50.0, (0., 50.0))

	query_expr.append(" @altura[0] <= `Altura (m)` <= @altura[1] and "
					  " @circunferencia[0] <= `Circunferencia (m)` <= @circunferencia[1] "
					  "and @dap[0] <= `DAP (m)` <= @dap[1]")

	col2.subheader('Observaciones')

	observaciones = col2.multiselect("Observaciones", obs_list + [],
										 default = [])

	for obs in observaciones:
		query_expr.append("`"+str(obs) + "`" + "== 1")

	st.subheader('Fecha de Registro')

	fecha_inicial = st.date_input("Fecha inicial",
								datetime.date(2021, 7, 6))

	fecha_final = st.date_input("Fecha final")

	query_expr.append("@fecha_inicial < Fecha < @fecha_final")

	st.form_submit_button()

df_display = raw_data.copy()
if query_expr!=[]:
	for expr in query_expr:
		df_display.query(expr, inplace=True)

my_placeholder = st.empty()

#

st.write(map_data)

#df_display["Especie"] = df_display.Especie.astype('category')

st.write(df_display.groupby('Nombre común')["Código"].count())#.plot.pie(subplots=True, autopct='%1.1f%%')

df_display.groupby("Especie")["Código"].count().plot.pie(subplots=True, autopct='%1.1f%%')

st.write(px.pie(df_display, names='Nombre común', title='Distribución de Especies'))

st.line_chart(df_display["Altura (m)"])

st.write(df_display.describe()[["Altura (m)",  "Circunferencia (m)", "DAP (m)"]])

# Map display

id ="19Xmp0UlcfBU85EdLLV8eQDsJ_5KpLWRu"

img_src = "https://drive.google.com/thumbnail?id="

tooltip = {
   "html" : "Especie:  {Especie}
 "
			"Altura (m):  {Altura (m)}
 "
			"Circunferencia (m):  {Circunferencia (m)}
 "
			"DAP (m):  {DAP (m)}
 "
		   ""
		   " "
		   " "
		   "Código:  {Código}
 ",
   "style": {
        "backgroundColor": "steelblue",
        "color": "white"
   }
}
print("meme1")

print("meme2")
st.header('Mapa')
print("meme3")
query_layer = pdk.Layer(
				"ScatterplotLayer",
				data = map_data.dropna(),
				get_position=['lon', 'lat'],
				radiusScale = 1,
				radius_min_pixels = 4, # Modify to match the scale of the park trees
				radius_max_pixels = 10,
				getFillColor = [0, 158, 96],
				pickable=True,
				)
print("memoso")
st.pydeck_chart(pdk.Deck(
			initial_view_state={"latitude": 10.4945, # La Floresta coordinates
			"longitude": -66.8456, "zoom": 15, "pitch":30},
			layers=[query_layer],
			tooltip=tooltip,
		))
print("meme4")


text:
# Copyright 2018 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License.  You may obtain a copy of
# the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
# License for the specific language governing permissions and limitations under
# the License.
# ==============================================================================
"""Tests for ParquetDataset."""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import os
import pytest
import numpy as np

import tensorflow as tf
tf.compat.v1.disable_eager_execution()
import tensorflow_io.parquet as parquet_io # pylint: disable=wrong-import-position

# Note: The sample file is generated from:
# `parquet-cpp/examples/low-level-api/reader_writer`
# This test extracts columns of [0, 1, 2, 4, 5]
# with column data types of [bool, int32, int64, float, double].
# Please check `parquet-cpp/examples/low-level-api/reader-writer.cc`
# to find details of how records are generated:
# Column 0 (bool): True for even rows and False otherwise.
# Column 1 (int32): Equal to row_index.
# Column 2 (int64): Equal to row_index * 1000 * 1000 * 1000 * 1000.
# Column 4 (float): Equal to row_index * 1.1.
# Column 5 (double): Equal to row_index * 1.1111111.
def test_parquet():
  """Test case for ParquetDataset."""
  filename = os.path.join(
      os.path.dirname(os.path.abspath(__file__)),
      "test_parquet",
      "parquet_cpp_example.parquet")
  filename = "file://" + filename

  columns = [
      'boolean_field',
      'int32_field',
      'int64_field',
      'float_field',
      'double_field']
  dtypes = [
      tf.bool,
      tf.int32,
      tf.int64,
      tf.float32,
      tf.double]

  dataset = tf.compat.v2.data.Dataset.zip(
      tuple([parquet_io.ParquetDataset(
          filename, column, dtype=dtype,
          start=0, stop=500) for (
              column, dtype) in zip(columns, dtypes)])).apply(
                  tf.data.experimental.unbatch())

  iterator = tf.compat.v1.data.make_initializable_iterator(dataset)
  init_op = iterator.initializer
  get_next = iterator.get_next()
  with tf.compat.v1.Session() as sess:
    sess.run(init_op)
    for i in range(500):
      v0 = ((i % 2) == 0)
      v1 = i
      v2 = i * 1000 * 1000 * 1000 * 1000
      v4 = 1.1 * i
      v5 = 1.1111111 * i
      p0, p1, p2, p4, p5 = sess.run(get_next)
      assert v0 == p0
      assert v1 == p1
      assert v2 == p2
      assert np.isclose(v4, p4)
      assert np.isclose(v5, p5)
    with pytest.raises(tf.errors.OutOfRangeError):
      sess.run(get_next)

if __name__ == "__main__":
  test.main()

max_stars_repo_name: petergtz/io

max_issues_repo_name: petergtz/io

max_forks_repo_name: petergtz/io


text:
#!/usr/bin/env python

# --------------------------------------------------------
# Faster R-CNN
# Copyright (c) 2015 Microsoft
# Licensed under The MIT License [see LICENSE for details]
# Written by Ross Girshick
# --------------------------------------------------------

"""
Demo script showing detections in sample images.

See README.md for installation instructions before running.
"""

import _init_paths
from fast_rcnn.config import cfg
from fast_rcnn.test import im_detect
from fast_rcnn.nms_wrapper import nms
from utils.timer import Timer
import numpy as np
import scipy.io as sio
import caffe, os, sys, cv2
import argparse

import matplotlib
matplotlib.use('Agg')
# matplotlib.use('pdf')
import matplotlib.pyplot as plt

# CLASSES = ('__background__', 'handbags', 'shoes', 'up_body', 'down_body', \
#          'all_body', 'boots', 'bra', 'underwear', 'skirt', 'dress')
# CLASSES = ('__background__', 'handbags', 'shoes', 'up_body', 'down_body', \
#          'all_body', 'boots', 'bra', 'underwear', 'skirt', 'dress', 'makeup')
# CLASSES = ('__background__', 'handbags', 'shoes', 'up_body', 'down_body', 'all_body', 'boots', 'bra', 'underwear', 'skirt', 'dress', 'lipstick', 'mascara', 'mascara_with_bbox','single_blusher', 'multi_blusher', 'pen', 'nail_polish', 'perfume', 'tin_shape', 'bottle_shape', 'pipe_shape', 'mouse_shape', 'square_shape', 'bucket_shape', 'tools')
CLASSES = ('__background__', 'upbody', 'downbody', 'fullbody')

# METHOD #1: OpenCV, NumPy, and urllib
def url_to_image(url):
    import numpy as np
    import urllib
    import cv2
    # download the image, convert it to a NumPy array, and then read
    # it into OpenCV format
    resp = urllib.urlopen(url)
    image = np.asarray(bytearray(resp.read()), dtype="uint8")
    image = cv2.imdecode(image, cv2.IMREAD_COLOR)

    # return the image
    return image

def bag_demo(net, image_name):

    """Detect object classes in an image using pre-computed object proposals."""
    # im = cv2.imread(image_name)
    im = url_to_image(image_name)
    print im.shape
    # Detect all object classes and regress object bounds
    timer = Timer()
    timer.tic()
    scores, boxes = im_detect(net, im)
    # print scores
    timer.toc()
    print ('Detection took {:.3f}s for '
           '{:d} object proposals').format(timer.total_time, boxes.shape[0])

    # Visualize detections for each class
    im = im[:, :, (2, 1, 0)]
    colors = plt.cm.hsv(np.linspace(0, 1, len(CLASSES))).tolist()
    plt.imshow(im)
    currentAxis = plt.gca()
    CONF_THRESH = 0.7
    NMS_THRESH = 0.3
    for cls_ind, cls in enumerate(CLASSES[1:]):
        cls_ind += 1 # because we skipped background
        cls_boxes = boxes[:, 4*cls_ind:4*(cls_ind + 1)]
        cls_scores = scores[:, cls_ind]
        dets = np.hstack((cls_boxes,
                          cls_scores[:, np.newaxis])).astype(np.float32)
        keep = nms(dets, NMS_THRESH)

        dets = dets[keep, :]
        keep_final = np.where(dets[:, 4]>CONF_THRESH)[0]
        for i in keep_final:
            xmin = dets[i, 0]
            ymin = dets[i, 1]
            xmax = dets[i, 2]
            ymax = dets[i, 3]
            score = dets[i, 4]
            label_name = cls
            display_txt = '%s: %.2f'%(label_name, score)
            coords = (xmin, ymin), xmax-xmin+1, ymax-ymin+1
            print xmin*1.0/im.shape[1],xmax*1.0/im.shape[1],ymin*1.0/im.shape[0],ymax*1.0/im.shape[0]
            color = colors[cls_ind]
            currentAxis.add_patch(plt.Rectangle(*coords, fill=False, edgecolor=color, linewidth=2))
            currentAxis.text(xmin, ymin, display_txt, bbox={'facecolor':color, 'alpha':0.5})
    plt.show()

def bag_demo_double(net, image_name, cat_ids, bboxes):

    """Detect object classes in an image using pre-computed object proposals."""
    im = cv2.imread(image_name)
    # im = url_to_image(image_name)
    # Detect all object classes and regress object bounds
    timer = Timer()
    timer.tic()
    scores, boxes = im_detect(net, im)
    # print scores
    timer.toc()
    print ('Detection took {:.3f}s for '
           '{:d} object proposals').format(timer.total_time, boxes.shape[0])

    # Visualize detections for each class
    im = im[:, :, (2, 1, 0)]
    #######
    fig = plt.figure()
    ax1 = fig.add_subplot(1,2,1)
    #######
    colors = plt.cm.hsv(np.linspace(0, 1, len(CLASSES))).tolist()
    ax1.imshow(im)
    currentAxis = plt.gca()
    CONF_THRESH = 0.6
    NMS_THRESH = 0.3
    for cls_ind, cls in enumerate(CLASSES[1:]):
        cls_ind += 1 # because we skipped background
        cls_boxes = boxes[:, 4*cls_ind:4*(cls_ind + 1)]
        cls_scores = scores[:, cls_ind]
        dets = np.hstack((cls_boxes,
                          cls_scores[:, np.newaxis])).astype(np.float32)
        keep = nms(dets, NMS_THRESH)

        dets = dets[keep, :]
        keep_final = np.where(dets[:, 4]>CONF_THRESH)[0]
        for i in keep_final:
            xmin = dets[i, 0]
            ymin = dets[i, 1]
            xmax = dets[i, 2]
            ymax = dets[i, 3]
            score = dets[i, 4]
            label_name = cls
            display_txt = '%s: %.2f'%(label_name, score)
            coords = (xmin, ymin), xmax-xmin+1, ymax-ymin+1
            color = colors[cls_ind]
            currentAxis.add_patch(plt.Rectangle(*coords, fill=False, edgecolor=color, linewidth=2))
            currentAxis.text(xmin, ymin, display_txt, bbox={'facecolor':color, 'alpha':0.5})
    ax2 = fig.add_subplot(1,2,2)
    ax2.imshow(im)
    currentAxis = plt.gca()
    for idx, cls_ind in enumerate(cat_ids):
        cls = CLASSES[cls_ind]
        xmin = bboxes[idx, 0]
        ymin = bboxes[idx, 1]
        xmax = xmin + bboxes[idx, 2] - 1
        ymax = ymin + bboxes[idx, 3] - 1
        label_name = cls
        display_txt = '%s: %.2f'%(label_name, 1)
        coords = (xmin, ymin), xmax-xmin+1, ymax-ymin+1
        color = colors[cls_ind]
        currentAxis.add_patch(plt.Rectangle(*coords, fill=False, edgecolor=color, linewidth=2))
        currentAxis.text(xmin, ymin, display_txt, bbox={'facecolor':color, 'alpha':0.5})
    plt.show()

def bag_demo_origin(image_name, cat_ids, bboxes):
    im = cv2.imread(image_name)
    im = im[:, :, (2, 1, 0)]
    colors = plt.cm.hsv(np.linspace(0, 1, len(CLASSES))).tolist()
    plt.imshow(im)
    currentAxis = plt.gca()

    for idx, cls_ind in enumerate(cat_ids):
        cls = CLASSES[cls_ind]
        xmin = bboxes[idx, 0]
        ymin = bboxes[idx, 1]
        xmax = xmin + bboxes[idx, 2] - 1
        ymax = ymin + bboxes[idx, 3] - 1
        label_name = cls
        display_txt = '%s: %.2f'%(label_name, 1)
        coords = (xmin, ymin), xmax-xmin+1, ymax-ymin+1
        color = colors[cls_ind]
        currentAxis.add_patch(plt.Rectangle(*coords, fill=False, edgecolor=color, linewidth=2))
        currentAxis.text(xmin, ymin, display_txt, bbox={'facecolor':color, 'alpha':0.5})
    plt.show()
    plt.draw()

def parse_args():
    """Parse input arguments."""
    parser = argparse.ArgumentParser(description='Faster R-CNN demo')
    parser.add_argument('--gpu', dest='gpu_id', help='GPU device id to use [0]',
                        default=0, type=int)
    parser.add_argument('--cpu', dest='cpu_mode',
                        help='Use CPU mode (overrides --gpu)',
                        action='store_true')
    # parser.add_argument('--net', dest='demo_net', help='Network to use [vgg16]',
    #                     choices=NETS.keys(), default='vgg16')

    args = parser.parse_args()

    return args

def index_to_fullPath_dict(filename):
        dic = {}
        with open(filename, 'rb') as f:
            for line in f.readlines():
                words = line.split(' ')
                full_path = words[0]
                index = full_path.split('/')[-1]
                dic[index.split('.')[0]] = full_path
        return dic

def sample_test_images(annot_path, test_path, sample_num=10):
    ## return the sampled full path list
    sampled_path = []
    index_to_path_dict = index_to_fullPath_dict(annot_path)
    test_index = []
    with open(test_path, 'rb') as f:
        for line in f.readlines():
            if line[-1] == '\n':
                test_index.append(line[:-1])
            else:
                test_index.append(line)
    if sample_num > len(test_index):
        print "Too many samples for testing!!!"
        return
    permu = np.random.permutation(len(test_index))
    sampled_index = np.array(test_index)[permu[:sample_num]].tolist()
    sampled_path = [index_to_path_dict[index] for index in sampled_index]
    return sampled_path

def load_images(test_path):
    test_index = []
    with open(test_path, 'rb') as f:
        for line in f.readlines():
            if line[-1] == '\n':
                test_index.append(line[:-1])
            else:
                test_index.append(line)
    return test_index

if __name__ == '__main__':
    cfg.TEST.HAS_RPN = True  # Use RPN for proposals

    args = parse_args()
    # # prototxt = 'models/all_cats_25cls_before0307_and_after0311/VGG16/faster_rcnn_end2end/test.prototxt'
    # prototxt = 'models/all_categories/ResNet101/faster_rcnn_end2end/test.prototxt'
    # caffemodel = 'output/faster_rcnn_end2end/2007_trainval_all_categories/ResNet101_all_categories_frcnn_iter_100000.caffemodel'
    # # caffemodel = 'output/faster_rcnn_end2end/voc_2007_trainval_all_cats_25cls_before0307_and_after0311/VGG16_all_cats_25cls_before0307_and_after0311_voc_frcnn_iter_700000.caffemodel'

    # prototxt = 'models/all_categories_0307/VGG16/faster_rcnn_end2end/test.prototxt'
    # caffemodel = 'output/faster_rcnn_end2end/2007_trainval_all_categories_0307/VGG16_all_categories_0307_frcnn_iter_300000.caffemodel'

    # prototxt = '/data/home/liuhuawei/detection/py-R-FCN-multiGPU/models/all_cats_11cls_train_0427_plus_2000handbags/VGG16/faster_rcnn_end2end/test.prototxt'
    # caffemodel = '/data/home/liuhuawei/detection/py-R-FCN-multiGPU/VGG16_all_cats_11cls_train_0425_plus_2000handbags_voc_frcnn_iter_150000.caffemodel'
    # prototxt = '/core/data/deploy/server_image/clothing_detection/test.prototxt'
    # caffemodel = '/core/data/deploy/server_image/clothing_detection/test.caffemodel'

    prototxt = '/core/data/deploy/server_image/test_clothing.prototxt'
    caffemodel = '/core/data/deploy/server_image/vgg16_clothing_240000.caffemodel'
    if not os.path.isfile(caffemodel):
        raise IOError(('{:s} not found.\nDid you run ./data/script/'
                       'fetch_faster_rcnn_models.sh?').format(caffemodel))

    if args.cpu_mode:
        caffe.set_mode_cpu()
    else:
        caffe.set_mode_gpu()
        caffe.set_device(args.gpu_id)
        cfg.GPU_ID = args.gpu_id
    net = caffe.Net(prototxt, caffemodel, caffe.TEST)
    # print("blobs {}\nparams {}".format(net.blobs.keys(), net.params.keys()))

    print '\n\nLoaded network {:s}'.format(caffemodel)

    # Warmup on a dummy image
    im = 128 * np.ones((300, 500, 3), dtype=np.uint8)
    for i in xrange(2):
        _, _= im_detect(net, im)

    im_names = ["http://img.haishentech.com/6pm/25ed7e5ed3b025a796658a9df436226f.jpg"]
    im_names = ['http://img.haishentech.com/theoutnet/01f18ce3306860c14231bcccc18841fd.jpg']
    for im_name in im_names:
        print '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'
        print 'Demo for data/demo/{}'.format(im_name)
        bag_demo(net, im_name)
        # bag_demo_origin(im_name, )

max_stars_repo_name: samson-wang/lwh-py-faster-rcnn

max_issues_repo_name: samson-wang/lwh-py-faster-rcnn

max_forks_repo_name: samson-wang/lwh-py-faster-rcnn

reason: import numpy,import scipy


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

from azureml.core.model import Model
from azureml.monitoring import ModelDataCollector
from azureml.contrib.services.aml_request import AMLRequest, rawhttp
from io import BytesIO
from PIL import Image

def load_image_into_numpy_array(image):
  (im_width, im_height) = image.size
  return np.array(image.getdata()).reshape(
    (im_height, im_width, 3)).astype(np.uint8)

def reframe_box_masks_to_image_masks(box_masks, boxes, image_height,
                                     image_width):
  def reframe_box_masks_to_image_masks_default():
    def transform_boxes_relative_to_boxes(boxes, reference_boxes):
      boxes = tf.reshape(boxes, [-1, 2, 2])
      min_corner = tf.expand_dims(reference_boxes[:, 0:2], 1)
      max_corner = tf.expand_dims(reference_boxes[:, 2:4], 1)
      transformed_boxes = (boxes - min_corner) / (max_corner - min_corner)
      return tf.reshape(transformed_boxes, [-1, 4])

    box_masks_expanded = tf.expand_dims(box_masks, axis=3)
    num_boxes = tf.shape(box_masks_expanded)[0]
    unit_boxes = tf.concat(
      [tf.zeros([num_boxes, 2]), tf.ones([num_boxes, 2])], axis=1)
    reverse_boxes = transform_boxes_relative_to_boxes(unit_boxes, boxes)
    return tf.image.crop_and_resize(
      image=box_masks_expanded,
      boxes=reverse_boxes,
      box_ind=tf.range(num_boxes),
      crop_size=[image_height, image_width],
      extrapolation_value=0.0)
  image_masks = tf.cond(
    tf.shape(box_masks)[0] > 0,
    reframe_box_masks_to_image_masks_default,
    lambda: tf.zeros([0, image_height, image_width, 1], dtype=tf.float32))
  return tf.squeeze(image_masks, axis=3)

def run_inference_for_single_image(image, graph):
  with graph.as_default():
    with tf.Session() as sess:
      # Get handles to input and output tensors
      ops = tf.get_default_graph().get_operations()
      all_tensor_names = {output.name for op in ops for output in op.outputs}
      tensor_dict = {}
      for key in [
        'num_detections', 'detection_boxes', 'detection_scores',
        'detection_classes', 'detection_masks'
      ]:
        tensor_name = key + ':0'
        if tensor_name in all_tensor_names:
          tensor_dict[key] = tf.get_default_graph().get_tensor_by_name(
            tensor_name)
      if 'detection_masks' in tensor_dict:
        # The following processing is only for single image
        detection_boxes = tf.squeeze(tensor_dict['detection_boxes'], [0])
        detection_masks = tf.squeeze(tensor_dict['detection_masks'], [0])
        # Reframe is required to translate mask from box coordinates to image coordinates and fit the image size.
        real_num_detection = tf.cast(tensor_dict['num_detections'][0], tf.int32)
        detection_boxes = tf.slice(detection_boxes, [0, 0], [real_num_detection, -1])
        detection_masks = tf.slice(detection_masks, [0, 0, 0], [real_num_detection, -1, -1])
        detection_masks_reframed = reframe_box_masks_to_image_masks(
          detection_masks, detection_boxes, image.shape[0], image.shape[1])
        detection_masks_reframed = tf.cast(
          tf.greater(detection_masks_reframed, 0.5), tf.uint8)
        # Follow the convention by adding back the batch dimension
        tensor_dict['detection_masks'] = tf.expand_dims(
          detection_masks_reframed, 0)
      image_tensor = tf.get_default_graph().get_tensor_by_name('image_tensor:0')

      # Run inference
      output_dict = sess.run(tensor_dict,
                             feed_dict={image_tensor: np.expand_dims(image, 0)})

      # all outputs are float32 numpy arrays, so convert types as appropriate
      output_dict['num_detections'] = int(output_dict['num_detections'][0])
      output_dict['detection_classes'] = output_dict[
        'detection_classes'][0].astype(np.uint8)
      output_dict['detection_boxes'] = output_dict['detection_boxes'][0]
      output_dict['detection_scores'] = output_dict['detection_scores'][0]
      if 'detection_masks' in output_dict:
        output_dict['detection_masks'] = output_dict['detection_masks'][0]
  return output_dict

# Load the model
def init():
  global detection_graph

  # Retrieve the path to the model file using the model name
  model_path = Model.get_model_path('frozen_inference_graph.pb')

  detection_graph = tf.Graph()
  with detection_graph.as_default():
    od_graph_def = tf.GraphDef()
    with tf.gfile.GFile(model_path, 'rb') as fid:
      serialized_graph = fid.read()
      od_graph_def.ParseFromString(serialized_graph)
      tf.import_graph_def(od_graph_def, name='')

  # Setup data collectors
  global inputs_dc, prediction_dc
  inputs_dc = ModelDataCollector('best_model', designation='inputs', feature_names=['image'])
  prediction_dc = ModelDataCollector('best_model', designation='predictions', feature_names=['num_detections', 'detection_classes', 'detection_boxes', 'detection_scores'])

# Passes data to the model and returns the prediction
@rawhttp
def run(request):
  body = request.get_data(False)
  image = Image.open(BytesIO(body))
  image_np = load_image_into_numpy_array(image)

  output_dict = run_inference_for_single_image(image_np, detection_graph)
  num_detections = output_dict['num_detections']
  detection_classes = output_dict['detection_classes'].tolist()
  detection_boxes = output_dict['detection_boxes'].tolist()
  detection_scores = output_dict['detection_scores'].tolist()

  output = {}
  output['num_detections'] = num_detections
  output['detection_classes'] = detection_classes
  output['detection_boxes'] = detection_boxes
  output['detection_scores'] = detection_scores
  if 'detection_masks' in output_dict:
    output['detection_masks'] = output_dict['detection_masks'].tolist()

  inputs_dc.collect([base64.b64encode(body)])
  prediction_dc.collect([num_detections, json.dumps(detection_classes), json.dumps(detection_boxes), json.dumps(detection_scores)])

  return output