text:
# coding=utf-8
# Copyleft 2019 project LXRT.
from typing import List

import json
import os
import pickle

import numpy as np
import torch
from torch.utils.data import Dataset

from param import args
from utils import load_obj_tsv
from frcnn.processing_image import Preprocess

from glob import glob
from tqdm import tqdm

# Load part of the dataset for fast checking.
# Notice that here is the number of images instead of the number of data,
# which means all related data to the images would be used.
TINY_IMG_NUM = 512
FAST_IMG_NUM = 5000

# The path to data and image features.
IMG_DIR = 'img_dir/'
VQA_DATA_ROOT = 'data/vqa/'
MSCOCO_IMGFEAT_ROOT = 'data/mscoco_imgfeat/'
SPLIT2NAME = {
    'dogs_mut_excl': 'dogs_mut_excl',
    'fewshot_train': 'fewshot_train',
    'train': 'train2014',
    'valid': 'val2014',
    'minival': 'val2014',
    'nominival': 'val2014',
    'test': 'test2015',
}

class VQADataset:
    """
    A VQA data example in json file:
        {
            "answer_type": "other",
            "img_id": "COCO_train2014_000000458752",
            "label": {
                "net": 1
            },
            "question_id": 458752000,
            "question_type": "what is this",
            "sent": "What is this photo taken looking through?"
        }
    """
    def __init__(self, splits: str, data: list = None):
        self.name = splits
        self.splits = splits.split(',')

        # Loading datasets
        if data:
            self.data = data
        else:
            self.data = []
            for split in self.splits:
                self.data.extend(json.load(open("data/vqa/%s.json" % split)))
        # print("Load %d data from split(s) %s." % (len(self.data), self.name))

        # Convert list to dict (for evaluation)
        self.id2datum = {
            datum['question_id']: datum
            for datum in self.data
        }

        # Answers
        self.ans2label = json.load(open("data/vqa/trainval_ans2label.json"))
        self.label2ans = json.load(open("data/vqa/trainval_label2ans.json"))
        assert len(self.ans2label) == len(self.label2ans)

    @property
    def num_answers(self):
        return len(self.ans2label)

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

"""
Returns non-featurized images
"""
class VQARawTorchDataset(Dataset):
    def __init__(self, dataset: VQADataset, frcnn_cfg = None):
        super().__init__()
        self.raw_dataset = dataset

        if args.tiny:
            topk = TINY_IMG_NUM
        elif args.fast:
            topk = FAST_IMG_NUM
        else:
            topk = None

        # Loading raw images to img_data
        img_data = []
        image_preprocess = Preprocess(frcnn_cfg, device='cpu')
        # TODO change this loading...
        for split in dataset.splits:
            for fn in tqdm(glob(f"{os.path.join(IMG_DIR, SPLIT2NAME[split])}/*")):
                # for fn in tqdm(glob(f"{data_dir}/*.jpg")):
                # ckpt_save_dir = data_dir+"_tensorized"
                ckpt_save_dir = None
                if ckpt_save_dir and not os.path.exists(ckpt_save_dir):
                    os.makedirs(ckpt_save_dir, exist_ok=True)
                if not ckpt_save_dir or not os.path.exists(os.path.join(ckpt_save_dir, 'image_tensors.pt')):
                    img_ids, images, sizes, scales_yx = image_preprocess(fn, ckpt_save_dir=ckpt_save_dir)
                else:
                    img_ids, images, sizes, scales_yx = torch.load(os.path.join(ckpt_save_dir, 'image_tensors.pt'))
                # img_id = os.path.split(fn)[1].replace('.jpg', '')
                assert len(img_ids) == len(images) == len(sizes) == len(scales_yx)
                for i in range(len(img_ids)):
                    img_datum = {
                        'img_id': img_ids[i],
                        'images': images[i],
                        'sizes': sizes[i],
                        'scales_yx': scales_yx[i],
                    }
                    img_data.append(img_datum)
        # Convert img list to dict
        self.imgid2img = {}
        for img_datum in img_data:
            self.imgid2img[img_datum['img_id']] = img_datum

        self.data = []
        for datum in self.raw_dataset.data:
            if datum['img_id'] in self.imgid2img:
                self.data.append(datum)

        print("Use %d data in torch dataset" % (len(self.data)))
        print()

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

    def __getitem__(self, item: int):
        datum = self.data[item]

        img_id = datum['img_id']
        ques_id = datum['question_id']
        ques = datum['sent']

        img_info = self.imgid2img[img_id]
        image = img_info['images']
        size = img_info['sizes']
        scale_yx = img_info['scales_yx']

        # Provide label (target)
        if 'label' in datum:
            label = datum['label']
            target = torch.zeros(self.raw_dataset.num_answers)
            for ans, score in label.items():
                target[self.raw_dataset.ans2label[ans]] = score
            return ques_id, image, size, scale_yx, ques, target
        else:
            return ques_id, image, size, scale_yx, ques

"""
An example in obj36 tsv:
FIELDNAMES = ["img_id", "img_h", "img_w", "objects_id", "objects_conf",
              "attrs_id", "attrs_conf", "num_boxes", "boxes", "features"]
FIELDNAMES would be keys in the dict returned by load_obj_tsv.
"""
class VQATorchDataset(Dataset):
    def __init__(self, dataset: VQADataset, imgfeat_dir: str = MSCOCO_IMGFEAT_ROOT, image_features: list = None):
        super().__init__()
        self.raw_dataset = dataset

        if args.tiny:
            topk = TINY_IMG_NUM
        elif args.fast:
            topk = FAST_IMG_NUM
        else:
            topk = None

        # Loading detection features to img_data
        if not image_features:
            img_data = []
            for split in dataset.splits:
                # Minival is 5K images in MS COCO, which is used in evaluating VQA/LXMERT-pre-training.
                # It is saved as the top 5K features in val2014_***.tsv
                load_topk = 5000 if (split == 'minival' and topk is None) else topk
                img_data.extend(load_obj_tsv(
                    os.path.join(imgfeat_dir, '%s_obj36.tsv' % (SPLIT2NAME[split])),
                    topk=load_topk))
            # import pdb; pdb.set_trace()
        else:
            img_data = image_features

        # Convert img list to dict
        self.imgid2img = {}
        for img_datum in img_data:
            self.imgid2img[img_datum['img_id']] = img_datum

        # Only kept the data with loaded image features
        self.data = []
        for datum in self.raw_dataset.data:
            if datum['img_id'] in self.imgid2img:
                self.data.append(datum)
            # else:
            #     import pdb; pdb.set_trace()

        # print("Use %d data in torch dataset" % (len(self.data)))
        # print()

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

    def __getitem__(self, item: int):
        datum = self.data[item]

        img_id = datum['img_id']
        ques_id = datum['question_id']
        ques = datum['sent']

        # Get image info
        img_info = self.imgid2img[img_id]
        obj_num = img_info['num_boxes']
        feats = img_info['features'].copy()

        # Normalize the boxes (to 0 ~ 1)
        if 'normalized_boxes' not in img_info:
            boxes = img_info['boxes'].copy()
            img_h, img_w = img_info['img_h'], img_info['img_w']
            boxes[:, (0, 2)] /= img_w
            boxes[:, (1, 3)] /= img_h
        else:
            boxes = img_info['normalized_boxes'].copy()
        try:
            np.testing.assert_array_less(boxes, 1+1e-5)
            np.testing.assert_array_less(-boxes, 0+1e-5)
        except:
            import pdb; pdb.set_trace()
        assert obj_num == len(boxes) == len(feats)

        # Provide label (target)
        if 'label' in datum:
            label = datum['label']
            target = torch.zeros(self.raw_dataset.num_answers)
            for ans, score in label.items():
                target[self.raw_dataset.ans2label[ans]] = score
            return ques_id, feats, boxes, ques, target
        else:
            return ques_id, feats, boxes, ques

class VQAEvaluator:
    def __init__(self, dataset: VQADataset):
        self.dataset = dataset

    def evaluate(self, quesid2ans: dict):
        score = 0.
        for quesid, ans in quesid2ans.items():
            datum = self.dataset.id2datum[quesid]
            label = datum['label']
            if ans in label:
                score += label[ans]
        return score / len(quesid2ans)

    def dump_result(self, quesid2ans: dict, path, human_readable: bool = False):
        """
        Dump results to a json file, which could be submitted to the VQA online evaluation.
        VQA json file submission requirement:
            results = [result]
            result = {
                "question_id": int,
                "answer": str
            }

        :param quesid2ans: dict of quesid --> ans
        :param path: The desired path of saved file.
        """
        with open(path, 'w') as f:
            results = []
            for ques_id, ans in quesid2ans.items():
                result = {
                    'question_id': ques_id,
                    'answer': ans
                }
                if human_readable:
                    question_datum = self.dataset.id2datum[ques_id]
                    result['question'] = question_datum
                results.append(result)
            json.dump(results, f, indent=4, sort_keys=True)

class MetaVQADataset:
    def __init__(self, support_tuples: list, query_tuples: list):
        self.support_tuples = support_tuples
        self.query_tuples = query_tuples
        # self.support_loader = [tup[1] for tup in support_tuples]
        # self.query_loader = [tup[1] for tup in query_tuples]
        # self.support_set = self.support_loader.dataset.data
        # self.query_set = self.query_loader.dataset.data

    def __item__(self, i):
        return self.support_tuples[i][1].dataset.data, self.query_tuples[i][1].dataset.data


text:
#!/usr/bin/python
# -*- coding: UTF-8 -*-
"""
author: Y. F. Zhang
"""

import numpy as np
import matplotlib.pyplot as plt

class GWO:
    def __init__(self):
        self.wolf_num = 15
        self.max_iter = 150
        self.dim = 30
        self.lb = -30*np.ones((self.dim,))
        self.ub = 30*np.ones((self.dim,))
        self.alpha_pos = np.zeros((1,self.dim))
        self.beta_pos = np.zeros((1, self.dim))
        self.delta_pos = np.zeros((1, self.dim))
        self.alpha_score = np.inf
        self.beta_score = np.inf
        self.delta_score = np.inf
        self.convergence_curve = np.zeros((self.max_iter,))
        self.position = np.zeros((self.wolf_num,self.dim))

    def run(self):
        count = 0
        self.init_pos()
        while count < self.max_iter:
            for i in range(self.wolf_num):
                flag_ub = self.position[i,:] > self.ub
                flag_lb = self.position[i,:] < self.lb
                self.position[i,:] = self.position[i,:]*(~(flag_lb+flag_ub))+flag_ub*self.ub+flag_lb*self.lb
                fitness = self.func(self.position[i,:])
                if fitness < self.alpha_score:
                    self.alpha_score = fitness
                    self.alpha_pos = self.position[i,:]
                elif fitness < self.beta_score:
                    self.beta_score = fitness
                    self.beta_pos = self.position[i,:]
                elif fitness < self.delta_score:
                    self.delta_score = fitness
                    self.delta_pos = self.position[i,:]
            a = 2 - count*(2/self.max_iter)
            for i in range(self.wolf_num):
                for j in range(self.dim):
                    alpha = self.update_pos(self.alpha_pos[j],self.position[i,j],a)
                    beta = self.update_pos(self.beta_pos[j], self.position[i, j], a)
                    delta = self.update_pos(self.delta_pos[j], self.position[i, j], a)
                    self.position[i, j] = sum(np.array([alpha, beta, delta]) * np.array([1/3,1/3,1/3]))
            count += 1
            self.convergence_curve[count-1] = self.alpha_score
        self.plot_results()

    def init_pos(self):
        for i in range(self.wolf_num):
            for j in range(self.dim):
                self.position[i,j] = np.random.rand()*(self.ub[j]-self.lb[j])+self.lb[j]

    @staticmethod
    def update_pos(v1,v2,a):
        A = 2*np.random.rand()*a-a
        C = 2*np.random.rand()
        temp = np.abs(C*v1-v2)
        return v1 - A*temp

    def plot_results(self):
        plt.style.use('seaborn-darkgrid')
        plt.plot(range(1,self.max_iter+1),self.convergence_curve,'g.--')
        plt.xlabel('iteration')
        plt.ylabel('fitness')
        plt.title('GWO fitness curve')
        plt.show()

    @staticmethod
    def func(x):
        dim, s = 30, 0
        for i in range(len(x)-1):
            s += 100*(x[i+1]-x[i]**2)**2+(x[i]-1)**2
        return s

if __name__ == "__main__":
    gwo = GWO()
    gwo.run()

ext: py

max_stars_repo_name: ZYunfeii/GreyWolfOptimization-GWO

max_issues_repo_name: ZYunfeii/GreyWolfOptimization-GWO

max_forks_repo_name: ZYunfeii/GreyWolfOptimization-GWO

reason: import numpy


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

# ## One-way ANOVA
#
# https://www.kaggle.com/lakshmi25npathi/bike-sharing-dataset

# In[1]:

import pandas as pd
import numpy as np

import matplotlib.pyplot as plt
from scipy import stats
import researchpy as rp

from statsmodels.formula.api import ols

# In[2]:

bike_sharing_data = pd.read_csv('datasets/bike_sharing_data_processed.csv')

bike_sharing_data.head()

# In[17]:

bike_sharing_data.shape

# In[18]:

bike_sharing_data['weathersit'].unique()

# In[19]:

bike_sharing_data.groupby('weathersit')['cnt'].describe().T

# In[20]:

bike_sharing_data.boxplot(column=['cnt'], by='weathersit', figsize=(12, 8))

# ## The hypothesis being tested
# https://statisticsbyjim.com/anova/post-hoc-tests-anova/

# * __H0: No difference between means, i.e. ?x1 = ?x2 = ?x3__
# * __Ha: Difference between means exist somewhere, i.e. ?x1 ? ?x2 ? ?x3, or ?x1 = ?x2 ? ?x3, or ?x1 ? ?x2 = ?x3__

# ### ANOVA with `scipy.stats`

# In[21]:

stats.f_oneway(bike_sharing_data['cnt'][bike_sharing_data['weathersit'] == 1],
               bike_sharing_data['cnt'][bike_sharing_data['weathersit'] == 2],
               bike_sharing_data['cnt'][bike_sharing_data['weathersit'] == 3],)

# ### ANOVA with `statsmodels`
# https://www.statsmodels.org/stable/examples/notebooks/generated/interactions_anova.html

# In[22]:

result = ols('cnt ~ C(weathersit)', data = bike_sharing_data).fit()

# In[23]:

print(result.summary())

# ### Post - hoc test by using `Tukey's method`
# https://www.statisticshowto.datasciencecentral.com/tukey-test-honest-significant-difference/

# In[24]:

from statsmodels.stats.multicomp import MultiComparison

mul_com = MultiComparison(bike_sharing_data['cnt'], bike_sharing_data['weathersit'])

mul_result = mul_com.tukeyhsd()

print(mul_result)

# In[ ]:

max_stars_repo_name: hossainlab/statswithpy

max_issues_repo_name: hossainlab/statswithpy

max_forks_repo_name: hossainlab/statswithpy

reason: import numpy,from scipy,from statsmodels


text:
import os
import glob
import astropy.io.fits as fits

def calibfitslist(caliblist):
    oklist,nolist=[],[]
    currentdir=os.getcwd()
    currentdir_split=currentdir.split('/')
    for i, j in enumerate(caliblist):
        # sample='Calib-DOAO-NGC3367-20180330-111526-B-60.fits'
        # print(i,j)
        filename = j.split('.')
        components=j.split('.')[0].split('-')
        if len(components) !=7 :
            print (i,j)
            nolist.append(j)
        if  components[0] != 'Calib' :
            os.system('rename Calibrated Calib '+j)
            print (i,j,'rename filename Calib-')
            nolist.append(j)
        if components[1] not in currentdir_split:
            print (i,j,'Different OBS')
            nolist.append(j)
        if components[2] not in currentdir_split:
            print (i,j,'Different Object')
            nolist.append(j)
        if len(components[3]) != 8 or  components[3][:2] !='20' :
            print(i,j,'Date value is strange')
            nolist.append(j)
        if len(components[4]) != 6 :
            print(i,j,'Time value is strange')
            nolist.append(j)
        if  'DATE-OBS' not in list(fits.getheader(j).keys()):
            print('DATE-OBS keyword missing.')
        if  'EXPTIME' not in list(fits.getheader(j).keys()):
            print('EXPTIME keyword missing.')
        else: oklist.append(j)
    return oklist, nolist

#hdr.get('DATEOBS',default=False)
#list(hdr.keys())
caliblist=glob.glob("Calib*.fits")
oklist, nolist=calibfitslist(caliblist)
oklist.sort()
nolist.sort()
print ('caliblist',len(caliblist),'oklist', len(oklist),'nolist', len(nolist))

max_stars_repo_name: changsuchoi/cspy

max_issues_repo_name: changsuchoi/cspy

max_forks_repo_name: changsuchoi/cspy

reason: import astropy


text:
#This class initilazises 3 threads to deal with taks in paralllel to CNN inference of the Edge TPU
import sys
import socket
import traceback
import cv2
from imutils.video import VideoStream
import imagezmq
import threading
import numpy as np
# from time import sleep
import time
import cv2
from nacl.signing import VerifyKey
from nacl.signing import SigningKey
from parameters import Parameters

import Responder as re
from merkletools import MerkleTools

import json

from utilities.render import Render

class ThreadHandler:

    def __init__(self, hostname, port, merkle_tree_interval, contractHash, minimum_receive_rate_from_contractor, vk_Bytes, input_size, sendingPort):
        self.hostname = hostname
        self.port = port
        self._stop = False
        self._stop_message = ''
        self._data = ''
        self._data2 = ''
        self._data3 = ''
        self._data_ready = threading.Event()
        self._data2_ready = threading.Event()
        self._image_count = 0

        self._received = threading.Event()
        self._readyToReceive = threading.Event()

        self._thread2 = threading.Thread(target=self._run, args=())
        self._thread3 = threading.Thread(target=self._run2, args=(
            merkle_tree_interval, contractHash, minimum_receive_rate_from_contractor, vk_Bytes, input_size))
        self._thread4 = threading.Thread(target=self._run3, args=(
            merkle_tree_interval, contractHash, hostname, sendingPort))
        self._thread2.daemon = True
        self._thread3.daemon = True
        self._thread4.daemon = True
        self._thread2.start()
        self._thread3.start()
        self._thread4.start()

    # Thread 2 (receiving) section

    def receive(self, timeout=15.0):

        flag = self._data_ready.wait(timeout=timeout)
        if not flag:
            if self._stop:
                    sys.exit()
            else:
                    self._stop = True
                    self._stop_message =  "Contract aborted in Thread2 waiting for new images: Outsourcer timed out. Possible Consquences for Outsourcer: Blacklist, Bad Review"
                    print(self._stop_message)
                    raise TimeoutError(
                    "Contract aborted in Thread2 waiting for new images: Outsourcer timed out. Possible Consquences for Outsourcer: Blacklist, Bad Review")

        self._data_ready.clear()

        return self._data

    def _run(self):
        receiver = imagezmq.ImageHub(
            "tcp://{}:{}".format(self.hostname, self.port), REQ_REP=False)

        while not self._stop:

            self._data = receiver.recv_jpg()
            self._data_ready.set()

        receiver.close()

    # Thread 3 (decompressing, verifying) section

    def _run2(self, merkle_tree_interval, contractHash, minimum_receive_rate_from_contractor, vk_Bytes, input_size):
        vk = VerifyKey(vk_Bytes)
        acknowledged_frames = 0
        while not self._stop:
            name, compressed = self.receive()

            decompressedImage = cv2.imdecode(
                np.frombuffer(compressed, dtype='uint8'), -1)

            if name == 'abort':
                if self._stop:
                    sys.exit(self._stop_message)
                else:
                    self._stop = True
                    self._stop_message = 'Contract aborted by outsourcer according to custom'
                    print(self._stop_message)
                    sys.exit(self._stop_message)

            if merkle_tree_interval == 0:
                try:
                    vk.verify(bytes(compressed) + contractHash +
                              bytes(name[-2]) + bytes(name[-1]), bytes(name[:-2]))
                except:
                    if self._stop:
                        sys.exit(self._stop_message)
                    else:
                        self._stop = True
                        self._stop_message = 'Contract aborted: Outsourcer signature does not match input. Possible Consquences for Outsourcer: Blacklist, Bad Review'
                        print(self._stop_message)
                        sys.exit(self._stop_message)

                if name[-1] < (self._image_count-2)*minimum_receive_rate_from_contractor or name[-1] < acknowledged_frames:
                    sys.exit(
                        'Contract aborted: Outsourcer did not acknowledge enough ouputs. Possible Consquences for Outsourcer: Blacklist, Bad Review')
                acknowledged_frames = name[-1]

            else:
                # verify if signature matches image, contract hash, and image count, and number of intervals, and random number
                try:
                    vk.verify(bytes(compressed) + contractHash +
                              bytes(name[-5]) + bytes(name[-4]) + bytes(name[-3]) + bytes(name[-2]) + bytes(name[-1]),  bytes(name[:-5]))
                except:
                    if self._stop:
                        sys.exit(self._stop_message)
                    else:
                        self._stop = True
                        self._stop_message =  'Contract aborted: Outsourcer signature does not match input. Possible Consquences for Outsourcer: Blacklist, Bad Review'
                        print(self._stop_message)
                        sys.exit(self._stop_message)

                if name[-4] < (self._image_count-2)*minimum_receive_rate_from_contractor or name[-4] < acknowledged_frames:
                    sys.exit(
                        'Contract aborted: Outsourcer did not acknowledge enough ouputs. Possible Consquences for Outsourcer: Blacklist, Bad Review')
                acknowledged_frames = name[-4]

            self._data2 = (decompressedImage, name)

            self._data2_ready.set()

    def receive2(self, timeout=15.0):
        flag = self._data2_ready.wait(timeout=timeout)
        if not flag:
            if self._stop:
                sys.exit()
            else:
                if self._stop:
                    sys.exit(self._stop_message)
                else:
                    self._stop = True
                    self._stop_message = "Contract aborted in Thread3 receving from Thread2: Outsourcer timed out. Possible Consquences for Outsourcer: Blacklist, Bad Review"
                    print(self._stop_message)
                    raise TimeoutError(
                        "Contract aborted in Thread3 receving from Thread2: Outsourcer timed out. Possible Consquences for Outsourcer: Blacklist, Bad Review")

        self._data2_ready.clear()

        return self._data2

# Thread 3 (postprocessing, decompressing, verifying, preporcessing) section

    def putData(self, data, timeout=15):
        # print('ready2')
        flag = self._readyToReceive.wait(timeout=timeout)
        if not flag:
            if self._stop:
                sys.exit(self._stop_message)
            else:
                self._stop = True
                self._stop_message = "Contract aborted in Thread1 waiting for Thread4: Outsourcer probably timed out. Possible Consquences for Outsourcer: Blacklist, Bad Review"
                print(self._stop_message)
                raise TimeoutError(
                    "Contract aborted in Thread3 receving from Thread2: Outsourcer timed out. Possible Consquences for Outsourcer: Blacklist, Bad Review")
        self._readyToReceive.clear()

        self._data3 = data

        self._received.set()

    def _run3(self, merkle_tree_interval, contractHash, hostname, sendingPort):
        self._readyToReceive.set()

        sk = SigningKey(Parameters.private_key_self)
        dont_show = Parameters.dont_show

        render = Render()

        responder = re.Responder(hostname, sendingPort)

        if Parameters.is_contractor == True:
            display_name = 'Contractor'
        else:
            display_name = 'Verifier'

        if merkle_tree_interval > 0:
            mt = MerkleTools()
            mtOld = MerkleTools()
            interval_count = 0
            mtOld_leaf_indices = {}
            mt_leaf_indices = {}

            current_challenge = 1
            merkle_root = ''
            last_challenge = 0

        while not self._stop:
            self._received.wait()
            self._received.clear()

            decompressedImage = self._data3[0]
            name = self._data3[1]
            self._image_count = self._data3[2]
            labels = self._data3[3]
            class_ids = self._data3[4]
            boxes = self._data3[5]
            render.set_image(decompressedImage)
            boxtext = render.render_detection(labels, class_ids, boxes, decompressedImage.shape[1], decompressedImage.shape[0], (45, 227, 227), 3)

            if merkle_tree_interval == 0:
                boxtext = 'Image' + str(name[-2]) + ':;' + boxtext
            else:
                boxtext = 'Image' + str(name[-5]) + ':;' + boxtext

            if merkle_tree_interval == 0:
                    sig = sk.sign(boxtext.encode('latin1') +
                                  contractHash).signature
                    sig = sig.decode('latin1')

                    # send reply

                    responder.respond(boxtext + ';--' + sig)

            else:
                image_count = self._image_count
                outsorucer_signature = name[:-5]
                outsourcer_image_count = name[-5]
                outsourcer_number_of_outputs_received = name[-4]
                outsourcer_random_number = name[-3]
                outsourcer_interval_count = name[-2]
                outsourcer_time_to_challenge = bool(name[-1])

                mt.add_leaf(boxtext, True)
                    # remember indices for challenge
                mt_leaf_indices[outsourcer_image_count] = image_count % merkle_tree_interval

                response = boxtext

                    # time to send a new merkle root
                    # e.g. if inervall = 128 then all respones from 0-127 are added to the merkle tree
                if image_count > 1 and (image_count+1) % merkle_tree_interval == 0:

                    mt.make_tree()
                    merkle_root = mt.get_merkle_root()

                    sig = sk.sign(merkle_root.encode(
                            'latin1') + bytes(interval_count) + contractHash).signature  # sign merkle root

                        # resond with merkle root
                    response += ';--' + str(merkle_root) + \
                        ';--' + sig.decode('latin1')

                    interval_count += 1
                    mtOld = mt  # save old merkle tree for challenge
                        # mtOld_leaf_indices.clear() # clear old indices
                    mtOld_leaf_indices.clear()
                    mtOld_leaf_indices = mt_leaf_indices.copy()  # save old indices for challenge

                    mt_leaf_indices.clear()  # clear for new indices

                    mt = MerkleTools()  # construct new merkle tree for next interval

                else:
                        # if this is true then the outsourcer has not received the merkle root yet -> send again
                    if interval_count > outsourcer_image_count:

                        sig = sk.sign(merkle_root.encode(
                            'latin1') + bytes(interval_count) + contractHash).signature  # sign merkle root

                        response += ';--' + str(merkle_root) + \
                            ';--' + sig.decode('latin1')

                    else:  # in this case outsourcer has confirmed to have recieved the merkle root

                            # in this case outsourcer has sent a challenge to meet with the old merkle tree, give outsourcer 3 frames time to confirm challenge received before sending again
                        if outsourcer_time_to_challenge and image_count - last_challenge > 3:
                            last_challenge = image_count
                            if outsourcer_random_number in mtOld_leaf_indices:
                                    # if challenge can be found, send proof back
                                outsourcer_random_number_index = mtOld_leaf_indices[
                                    outsourcer_random_number]

                            else:
                                    # if challenge index cannot be found return leaf 0
                                outsourcer_random_number_index = 0
                                    # print('proof index not found')

                            proofs = mtOld.get_proof(
                                    outsourcer_random_number_index)

                            stringsend = ''
                            for proof in proofs:
                                stringsend += ';--'  # indicate start of proof
                                stringsend += proof.__str__()  # send proof

                            stringsend += ';--'
                                # send leaf
                            stringsend += mtOld.get_leaf(
                                outsourcer_random_number_index)
                            stringsend += ';--'
                            stringsend += mtOld.get_merkle_root()  # send root

                            stringarr = []
                            stringarr = stringsend.split(';--')

                            leaf_node = stringarr[-2]
                            root_node = stringarr[-1]
                            proof_string = stringarr[0:-2]

                            sig = sk.sign(str(stringarr[1:]).encode('latin1') + bytes(
                                interval_count-1) + contractHash).signature  # sign proof and contract details

                                # attach signature
                            response += ';--' + sig.decode('latin1')
                            response += stringsend  # attach challenge response to response

                responder.respond(response)

                # display image

            if not dont_show:

                cv2.imshow(display_name, decompressedImage)

                if cv2.waitKey(1) == ord('q'):
                    responder.respond('abort12345:6')
                    if self._stop:
                        sys.exit(self._stop_message)
                    else:
                        self._stop = True
                        self._stop_message = 'Contract aborted: Contractor ended contract according to custom'
                        print(self._stop_message)
                        sys.exit(self._stop_message)

            self._readyToReceive.set()

    def close(self):
        self._stop = True

# Simulating heavy processing load
def limit_to_2_fps():
    sleep(0.5)


text:
import gc
import os
import sys
from copy import deepcopy
from shutil import copyfile

os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'

import tensorflow as tf
import pandas as pd
import pathlib
import argparse
import utils
from functools import partial
from data_generator import _parse_function, audio_frequency_masking, audio_time_masking
from tqdm import tqdm
import random, math
import models
import numpy as np
import tensorflow_addons as tfa
import configparser
from sklearn.model_selection import StratifiedKFold, KFold
import joblib

emo_dict = ['hap', 'sad', 'ang', 'sur', 'dis', 'fea', 'neu']

def get_out_dict(args, is_test=False):
    """

    :param args:
    :return:
    """
    out_dict = [] if not is_test else ['file_id']
    config = configparser.ConfigParser()
    config.read('models_config.cfg')

    if 'audio' in args.feature_type:
        out_dict.append('audio')
        out_dict.append('speech_emo')
    if 'video' in args.feature_type:
        out_dict.append('video')
        out_dict.append('face_emo')
    if 'text' in args.feature_type:
        out_dict.append('text')
        if 'speech_emo' not in out_dict:
            out_dict.append('text_emo')

    if 'mm' in args.feature_type:
        if config['multimodal'].getboolean('multi_outputs'):
            out_dict = out_dict + ['audio', 'video', 'text', 'int_emo', 'speech_emo', 'face_emo', 'text_emo']
        else:
            out_dict = out_dict + ['audio', 'video', 'text', 'int_emo']

    return out_dict

def get_by_ids(use_ids, list_ids_tfrecords):
    """

    :param use_data: list of path to tfrecords
    :param use_ids: list of ids
    :param list_ids_tfrecords: videos corresponding to each ids
    :return:
    """

    ret_data = []
    for ids in use_ids:
        ret_data = ret_data + list_ids_tfrecords[ids].tolist()

    return sorted(ret_data)

def get_cv_data(n_splits=5, seed=12, byParticipantID=False):
    """
    Merge train and validation, and random split for cross validation
    :return:
    """
    if not byParticipantID:
        emo_dict = ['hap', 'sad', 'ang', 'sur', 'dis', 'fea', 'neu']
        train_csv = pd.read_csv('2020-1/train.csv').values
        val_csv = pd.read_csv('2020-1/val.csv').values

        train_data = [('2020-1/tfrecord/train/{}.tfrecord'.format(train_csv[idx, 0]), emo_dict.index(train_csv[idx, 1]))
                      for
                      idx in range(train_csv.shape[0])]
        val_data = [('2020-1/tfrecord/val/{}.tfrecord'.format(val_csv[idx, 0]), emo_dict.index(val_csv[idx, 1])) for
                    idx in range(val_csv.shape[0])]
        merged_data = np.concatenate([train_data, val_data])

        merged_id = merged_data[:, 0].reshape(-1, 1)
        merged_label = merged_data[:, 1].astype(np.int8)

        skf = StratifiedKFold(n_splits=n_splits, shuffle=True, random_state=seed)
        fold_indexing = []
        for train_index, val_index in skf.split(merged_id, merged_label):
            fold_indexing.append((merged_id[train_index, 0].flatten(), merged_id[val_index, 0].flatten()))

    else:
        # Split by participant ID
        participant_ids = np.load('train_val_ids_dict.npy', allow_pickle=True).item()
        list_ids = np.array(sorted(list(participant_ids.keys())))
        skf = KFold(n_splits=n_splits, shuffle=True, random_state=seed)
        fold_indexing = []
        for train_index, val_index in skf.split(list_ids):
            train_ids = get_by_ids(list_ids[train_index], participant_ids)
            val_ids = get_by_ids(list_ids[val_index], participant_ids)
            fold_indexing.append((train_ids, val_ids))

    return fold_indexing

def load_data(args, train_id=None, val_id=None, load_test=True, load_train=True):
    """

    :param args:
    :param train_id:
    :param val_id:
    :param load_test:
    :return:
    """

    autotune = tf.data.experimental.AUTOTUNE
    batch_size = args.batch_size

    out_dict = get_out_dict(args)

    add_lb_in = ('mm' in args.feature_type and is_multi_outputs())

    parse_func = partial(_parse_function, out_dict=out_dict, audio_length=args.audio_time,
                         video_frames=args.video_frames, add_lb_in=add_lb_in)

    audio_time_masking_func = partial(audio_time_masking, p=args.aag_audio)
    audio_frequency_masking_func = partial(audio_frequency_masking, p=args.aag_audio)

    if load_train:
        if train_id is None:
            train_list = sorted(
                ['2020-1/tfrecord/train/{}.tfrecord'.format(int_id) for int_id in
                 pd.read_csv('2020-1/train.csv').values[:, 0]])
        else:
            train_list = sorted(train_id)

        random.shuffle(train_list)

        if val_id is None:
            val_list = sorted(
                ['2020-1/tfrecord/val/{}.tfrecord'.format(int_id) for int_id in pd.read_csv('2020-1/val.csv').values[:, 0]])
        else:
            val_list = sorted(val_id)

        loaders = {
            'train': tf.data.TFRecordDataset(train_list).shuffle(args.buffer_size, reshuffle_each_iteration=True,
                                                                 seed=args.seed).repeat().map(parse_func,
                                                                                num_parallel_calls=autotune).map(
                audio_frequency_masking_func, num_parallel_calls=autotune).map(audio_time_masking_func,
                                                                               num_parallel_calls=autotune).batch(
                batch_size).prefetch(autotune),
            'val': tf.data.TFRecordDataset(val_list).map(parse_func, num_parallel_calls=autotune).batch(
                batch_size).prefetch(autotune),
        }
        loaders_len = {'train': len(train_list), 'val': len(val_list)}
    else:
        loaders = {}
        loaders_len = {}

    if load_test:
        test1_list = sorted([x.__str__() for x in pathlib.Path('2020-1/tfrecord/test1/').glob('*.tfrecord')])
        test2_list = sorted([x.__str__() for x in pathlib.Path('2020-1/tfrecord/test2/').glob('*.tfrecord')])
        test3_list = sorted([x.__str__() for x in pathlib.Path('2020-1/tfrecord/test3/').glob('*.tfrecord')])
        test_out_dict = get_out_dict(args, is_test=True)
        test_parse_func = partial(_parse_function, out_dict=test_out_dict, audio_length=args.audio_time,
                                  video_frames=args.video_frames, add_lb_in=add_lb_in)

        loaders.update({
            'test1': tf.data.TFRecordDataset(test1_list).map(test_parse_func, num_parallel_calls=autotune).batch(
                batch_size).prefetch(autotune),
            'test2': tf.data.TFRecordDataset(test2_list).map(test_parse_func, num_parallel_calls=autotune).batch(
                batch_size).prefetch(autotune),
            'test3': tf.data.TFRecordDataset(test3_list).map(test_parse_func, num_parallel_calls=autotune).batch(
                batch_size).prefetch(autotune)
        })

        loaders_len.update({'test1': len(test1_list), 'test2': len(test2_list), 'test3': len(test3_list)})

    return loaders, loaders_len

def get_base_model(args):
    """
    Create model
    :param args:
    :return:
    """
    if 'audio' in args.feature_type:
        base_model = models.audio_model(args)
    elif 'video' in args.feature_type:
        base_model = models.video_model(args)
    elif 'text' in args.feature_type:
        base_model = models.text_model(args)
    else:
        base_model = models.multimodal_model(args)

    return base_model

def is_multi_outputs():
    config = configparser.ConfigParser()
    config.read('models_config.cfg')

    return config['multimodal'].getboolean('multi_outputs')

def train(args, base_model=None, loaders=None, loaders_len=None, is_fold=-1, is_train=True):
    """

    :param args:
    :return:
    """
    if loaders is None:
        loaders, loaders_len = load_data(args)

    print(loaders_len)

    postfix = '' if is_fold == -1 else '-kf{}'.format(is_fold)
    # if args.batch_size == 1:
    #     for dm in loaders['train']:
    #         print(dm[0].keys(), dm[1].keys())
    #         return
    #         continue
    #     return
    if base_model is None:
        base_model = get_base_model(args)

    train_steps_per_epoch = int(args.steps_per_epoch * (loaders_len['train'] // args.batch_size))
    val_steps_per_epoch = math.ceil(loaders_len['val'] / args.batch_size)

    lr_schedule = utils.CusLRScheduler(initial_learning_rate=args.lr_init, min_lr=args.min_lr, lr_start_warmup=0.,
                                       warmup_steps=5 * train_steps_per_epoch, num_constant=0 * train_steps_per_epoch,
                                       T_max=args.use_min_lr * train_steps_per_epoch, num_half_cycle=1)

    if args.opt == 'adam':
        opt = tf.keras.optimizers.Adam(learning_rate=lr_schedule)
    else:
        opt = tf.keras.optimizers.SGD(learning_rate=lr_schedule, momentum=0.9)

    opt_swa = tfa.optimizers.SWA(opt, start_averaging=int(args.use_min_lr * train_steps_per_epoch),
                                 average_period=int(args.use_min_lr * 1.))

    loss_obj = None if 'mm' in args.feature_type else tf.keras.losses.CategoricalCrossentropy(label_smoothing=0.3,
                                                                                from_logits=True)
    metric_obj = tf.keras.metrics.CategoricalAccuracy(name='acc')

    base_model.compile(optimizer=opt_swa, loss=[loss_obj], metrics=[metric_obj])

    monitor_name = 'val_acc'  #
    pathlib.Path(args.dir).mkdir(parents=True, exist_ok=True)
    best_ckpt = tf.keras.callbacks.ModelCheckpoint(
        os.path.join(args.dir, '{}_best_checkpoint{}.h5'.format(args.feature_type[0], postfix)),
        monitor=monitor_name,
        verbose=0, save_best_only=True, save_weights_only=True,
        mode='max')
    tsb_callback = tf.keras.callbacks.TensorBoard(log_dir=os.path.join(args.dir, 'tsb'))
    cbacks = [tsb_callback]  # [best_ckpt, ]

    if is_train:
        base_model.fit(loaders['train'], validation_data=loaders['val'], epochs=args.epoch,
                       steps_per_epoch=train_steps_per_epoch,
                       validation_steps=val_steps_per_epoch,
                       callbacks=cbacks, verbose=1)

        opt_swa.assign_average_vars(base_model.variables)
        base_model.save_weights(os.path.join(args.dir, '{}_best_checkpoint{}.h5'.format(args.feature_type[0], postfix)))
    else:
        postfix = postfix + '_testOnly'

    base_model.load_weights(os.path.join(args.dir, '{}_best_checkpoint{}.h5'.format(args.feature_type[0], postfix)))
    res = base_model.evaluate(loaders['val'])
    print("Evaluating ", res)

    return base_model

def run_cv_train(args, n_splits=5):
    """

    :param args:
    :param n_splits:
    :return:
    """
    load_test = True
    data_loaders = {}
    data_loaders_len = {}
    base_model = None
    byParticipantID = False
    for idx in range(n_splits):
        cv_data_indexing = get_cv_data(n_splits=n_splits, seed=args.seed + 11 * idx + 1,
                                       byParticipantID=byParticipantID)

        cur_train_id, cur_val_id = cv_data_indexing[idx]
        cur_loaders, cur_loaders_len = load_data(args, train_id=cur_train_id, val_id=cur_val_id, load_test=load_test)
        load_test = False

        data_loaders.update(cur_loaders)
        data_loaders_len.update(cur_loaders_len)

        base_model = train(args, base_model=base_model, loaders=data_loaders, loaders_len=data_loaders_len, is_fold=idx,
                           is_train=True)

        # Reset session
        base_model = None

        data_loaders['train'] = None
        data_loaders['val'] = None
        gc.collect()

        utils.set_seed(args.seed, reset_session=True)

    test(args, is_fold=True)

def generate_predictions_kfold(list_models, data_loader, is_test='val', write_name='data_write.txt'):
    """

    :param list_models:
    :param data_loader:
    :param is_test:
    :param write_name:
    :return:
    """
    if len(list_models) < 1:
        raise ValueError('Need to Specify models')

    out_res = []
    for data in data_loader:
        data_pred_list = []
        for idx in range(len(list_models)):
            cur_preds = list_models[idx].predict(data[0])  # ['int_emo']
            if isinstance(cur_preds, dict):
                data_pred_list.append(cur_preds['int_emo'])
            else:
                data_pred_list.append(cur_preds)
        data_pred = np.mean(np.stack(data_pred_list), axis=0)
        data_pred = np.argmax(data_pred, axis=-1)

        for idx in range(len(list_models)):
            data_pred_list[idx] = np.argmax(data_pred_list[idx], axis=-1)

        data_id = data[0]['file_id'].numpy()

        cur_res = []
        for idx in range(data_id.shape[0]):
            cur_tuple = [data_id[idx], emo_dict[data_pred[idx]]]
            for idx_m in range(len(list_models)):
                cur_tuple.append(emo_dict[data_pred_list[idx_m][idx]])

            cur_res.append(cur_tuple)

        out_res = out_res + cur_res

    out_res = np.array(out_res)
    cols = ['FileID', 'Emotion']
    for idx in range(len(list_models)):
        cols.append('Emotion{}'.format(idx))
    pd.DataFrame(out_res,
                 columns=cols).to_csv(
        write_name[:-4] + '_all.csv', sep=',', index=False)
    pd.DataFrame(out_res[:, [0, 1]],
                 columns=['FileID', 'Emotion']).to_csv(write_name, sep=',', index=False)

def generate_predictions(model, data_loader, is_test='val', write_name='data_write.txt'):
    """
    Generate prediction
    :param model:
    :param loader:
    :return:
    """

    emo_dict = ['hap', 'sad', 'ang', 'sur', 'dis', 'fea', 'neu']
    out_res = []
    for data in data_loader:
        data_pred = model.predict(data[0])  # ['int_emo']
        data_pred = np.argmax(data_pred, axis=-1)

        if 'test' in is_test:
            data_id = data[0]['file_id'].numpy()
            cur_res = [(data_id[idx], emo_dict[data_pred[idx]]) for idx in range(data_id.shape[0])]
        else:
            data_emo = np.argmax(data[1]['int_emo'].numpy(), axis=-1)
            # cur_res = [(emo_dict.index(data_emo[idx]), data_pred[idx]) for idx in range(data_emo.shape[0])]
            cur_res = [(data_emo[idx], data_pred[idx]) for idx in range(data_emo.shape[0])]

        out_res = out_res + cur_res

    out_res = np.array(out_res)
    pd.DataFrame(out_res, columns=['FileID', 'Emotion']).to_csv(write_name, sep=',', index=False)

def write_predictions(base_model, data_loaders, postfix=''):
    """

    :param model:
    :param data_loaders:
    :return:
    """
    # print('Generate val predictions')
    # generate_predictions(base_model, data_loaders['val'], is_test='val',
    #                      write_name='{}/val{}.csv'.format(args.dir, postfix))
    print('Generate test1 predictions')
    generate_predictions(base_model, data_loaders['test1'], is_test='test1',
                         write_name='{}/test1{}.csv'.format(args.dir, postfix))

    if 'test2' in data_loaders.keys():
        print('Generate test2 predictions')
        generate_predictions(base_model, data_loaders['test2'], is_test='test2',
                             write_name='{}/test2{}.csv'.format(args.dir, postfix))
    if 'test3' in data_loaders.keys():
        print('Generate test3 predictions')
        generate_predictions(base_model, data_loaders['test3'], is_test='test2',
                             write_name='{}/test3{}.csv'.format(args.dir, postfix))

def test(args, is_fold=True):
    """

    :param args:
    :return:
    """
    loaders, _ = load_data(args, load_train=False)
    num_models = 5
    if not is_fold:
        base_model = get_base_model(args)
        base_model.compile(metrics=tf.keras.metrics.CategoricalAccuracy(name='acc'))
        base_model.load_weights('{}/{}_best_checkpoint.h5'.format(args.dir, 'mm'))

        val_res = base_model.evaluate(loaders['val'])
        print('Evaluate val res: ', val_res)

        write_predictions(base_model, loaders)

        print('Completed generate text prediction')
    else:
        list_models = []
        for kf in range(num_models):
            cur_model = get_base_model(args)
            cur_model.load_weights(
                os.path.join(args.dir, '{}_best_checkpoint-kf{}.h5'.format(args.feature_type[0], kf)))
            list_models.append(cur_model)

        postfix = 'kf_fuse_mean'
        generate_predictions_kfold(list_models, loaders['test1'], is_test='test1',
                                   write_name='{}/test1-{}.csv'.format(args.dir, postfix))
        generate_predictions_kfold(list_models, loaders['test2'], is_test='test2',
                                   write_name='{}/test2-{}.csv'.format(args.dir, postfix))
        if 'test3' in loaders.keys():
            generate_predictions_kfold(list_models, loaders['test3'], is_test='test3',
                                       write_name='{}/test3-{}.csv'.format(args.dir, postfix))

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='Multimodal Emotion Recognition')
    parser.add_argument('--seed', type=int, default=1, help='seed (default: 1)')
    parser.add_argument('--batch_size', type=int, default=32, help='batch size (default: 32)')
    parser.add_argument('--buffer_size', type=int, default=10240, help='buffer size (default: 4096)')
    parser.add_argument('--audio_time', type=int, default=4, help='time of audio to be use (default: 4 seconds)')
    parser.add_argument('--video_frames', type=int, default=16, help='Number of frames to be used in each video')
    parser.add_argument('--feature_type', nargs='+', help='Feature to be used', required=True)
    parser.add_argument('--steps_per_epoch', type=float, default=1., help='Number of steps per epoch (default: 1.)')
    parser.add_argument('--epoch', type=int, default=10, help='Number of epochs (default: 10)')

    parser.add_argument('--aag_audio', type=float, default=0., help='Audio augmentation probability (default: 0.)')

    parser.add_argument('--lr_init', type=float, default=1e-3, help='Initial learning rate (default: 1e-3)')
    parser.add_argument('--min_lr', type=float, default=1e-5, help='Initial learning rate (default: 1e-5)')
    parser.add_argument('--use_min_lr', type=int, default=15, help='Start to use min lr (default: 20)')
    parser.add_argument('--opt', type=str, default='adam', help='Optimizer (default: adam)')

    parser.add_argument('--test', type=str, default='', help='Test (multi-modal) checkpoint (default: "")')
    parser.add_argument('--ckpt', type=str, default='', help='Root model checkpoint (default: "")')

    parser.add_argument('--dir', type=str, default='./tmp', help='Training logs directory (default: tmp)')

    args = parser.parse_args()

    print("Feature: ", args.feature_type[0])
    utils.set_gpu_growth_or_cpu(use_cpu=False, write_info=True)
    utils.set_seed(args.seed)
    tf.config.optimizer.set_jit(True)
    # Mixed precision
    # policy = mixed_precision.Policy('mixed_float16')
    # mixed_precision.set_policy(policy)
    # tf.logging.set_verbosity(tf.logging.ERROR)

    # tf.compat.v1.disable_eager_execution()
    pathlib.Path(os.path.join(args.dir, "src")).mkdir(parents=True, exist_ok=True)
    with open(os.path.join(args.dir, 'command_{}.sh'.format(args.feature_type[0])), 'w') as f:
        f.write(' '.join(sys.argv))
        f.write('\n')
        f.write('\n')
    try:
        copyfile('main.py', os.path.join(args.dir, "src", "main_{}.py".format(args.feature_type[0])))
        copyfile('./utils.py', os.path.join(args.dir, "src", "utils_{}.py".format(args.feature_type[0])))
        copyfile('./models.py', os.path.join(args.dir, "src", "models_{}.py".format(args.feature_type[0])))
        copyfile('./models_config.cfg',
                 os.path.join(args.dir, "src", "models_config_{}.cfg".format(args.feature_type[0])))
        copyfile('./data_generator.py',
                 os.path.join(args.dir, "src", "data_generator_{}.py".format(args.feature_type[0])))
        copyfile('./face_extractor.py',
                 os.path.join(args.dir, "src", "face_extractor_{}.py".format(args.feature_type[0])))
        copyfile('./train.sh', os.path.join(args.dir, "src", "train_{}.sh".format(args.feature_type[0])))

    except:
        print("Can not copy files")

    with open('{}/{}_arg.txt'.format(args.dir, args.feature_type[0]), 'w') as fd:
        fd.write(str(vars(args)))

    if args.test != '':
        print('Testing progress')
        test(args)
    else:
        # tf.compat.v1.disable_eager_execution()
        print("TF version ", tf.__version__)
        # train(args)
        run_cv_train(args)
        print('Training progress')


text:
#!/usr/bin/env python
"""
This is the custom class for an NLCG optimization schema.
It supercedes the `seisflows.optimize.base` class
"""
import sys
import numpy as np

from seisflows.config import custom_import
from seisflows.plugins import optimize

PAR = sys.modules['seisflows_parameters']
PATH = sys.modules['seisflows_paths']

class NLCG(custom_import("optimize", "base")):
    """
    Nonlinear conjugate gradient method
    """
    def __init__(self):
        """
        These parameters should not be set by the user.
        Attributes are initialized as NoneTypes for clarity and docstrings.
        """
        super().__init__()
        self.NLCG = None
        self.restarted = None

    def check(self):
        """
        Checks parameters, paths, and dependencies
        """
        # Line search algorithm
        if "LINESEARCH" not in PAR:
            setattr(PAR, "LINESEARCH", "Bracket")

        # NLCG periodic restart interval
        if "NLCGMAX" not in PAR:
            setattr(PAR, "NLCGMAX", np.inf)

        # NLCG conjugacy restart threshold
        if "NLCGTHRESH" not in PAR:
            setattr(PAR, "NLCGTHRESH", np.inf)

        super().check()

    def setup(self):
        """
        Set up the NLCG optimization schema
        """
        super().setup()
        self.NLCG = getattr(optimize, 'NLCG')(path=PATH.OPTIMIZE,
                                              maxiter=PAR.NLCGMAX,
                                              thresh=PAR.NLCGTHRESH,
                                              precond=self.precond,
                                              verbose=PAR.VERBOSE)

    def compute_direction(self):
        """
        Overwrite the Base compute direction class
        """
        # g_new = self.load('g_new')
        p_new, self.restarted = self.NLCG()
        self.save('p_new', p_new)

    def restart(self):
        """
        Overwrite the Base restart class and include a restart of the NLCG
        """
        super().restart()
        self.NLCG.restart()

max_stars_repo_name: evcano/seisflows

max_issues_repo_name: evcano/seisflows

max_forks_repo_name: evcano/seisflows


text:
import numpy as np
import numpy.linalg as la
from scipy import signal

def arx(na, nb, u, y, nk=0):
    N = np.size(y)
    M = na + nb

    n_max = np.amax([na, nb + nk])

    phi = np.zeros((N - n_max, M))

    k = 0

    for i in range(0, na):
        phi[:, i] = -y[n_max - i-1:N - i-1]

    for i in range(0, nb):
        phi[:, i+na] = u[n_max - i-nk:N - i-nk]

    y = y[n_max:N]

    R = np.dot(phi.T, phi)
    S = np.dot(phi.T, y)
    theta = la.solve(R, S)

    return [theta]

def armax(na, nb, nc, u, y, nk=0, n=10):
    N = np.size(y)
    M = na + nb
    n_max = np.amax([na, nb + nk, nc])
    phi = np.zeros((N - n_max, M))

    for i in range(0, na):
        phi[:, i] = -y[n_max - i-1:N - i-1]

    for i in range(0, nb):
        phi[:, i+na] = u[n_max - i-nk:N - i-nk]

    y_aux = y[n_max:N]

    R = np.dot(phi.T, phi)
    S = np.dot(phi.T, y_aux)
    theta = la.solve(R, S)
    erro=y-np.append(np.zeros([n_max]), np.dot(phi,theta))

    M = na + nb + nc
    phi = np.zeros((N - n_max, M))

    for i in range(1, n):
        for i in range(0, na):
            phi[:, i] = -y[n_max - i-1:N - i-1]

        for i in range(0, nb):
            phi[:, i+na] = u[n_max - i-nk:N - i-nk]

        for i in range(0, nc):
            phi[:, i+na+nb] = erro[n_max-i-1:N-i-1]

        y_aux = y[n_max:N]
        R = np.dot(phi.T, phi)
        S = np.dot(phi.T, y_aux)
        theta = la.solve(R, S)
        erro=y-np.append(np.zeros([n_max]), np.dot(phi,theta))
    return [theta]

def armax2(na, nb, nc, u, y, nk=0, n=10):
    N = np.size(y)
    n_max = np.amax([na, nb + nk, nc])

    for k in range(0,nc+1):
        M = na + nb + k
        phi = np.zeros((N - n_max, M))
        for j in range(1, n):
            for i in range(0, na):
                phi[:, i] = -y[n_max - i-1:N - i-1]

            for i in range(0, nb):
                phi[:, i+na] = u[n_max - i-nk:N - i-nk]

            for i in range(0, k):
                phi[:, i+na+nb] = erro[n_max-i-1:N-i-1]

            y_aux = y[n_max:N]
            R = np.dot(phi.T, phi)
            S = np.dot(phi.T, y_aux)
            thetan = la.solve(R, S)

            if (j>1):
                theta=(9*theta+thetan)/10
            else:
                theta=thetan

            print(theta)
            erro=y-np.append(np.zeros([n_max]), np.dot(phi,theta))

    return [theta]

reason: import numpy,from scipy


text:
import time

import numpy as np

from PuzzleLib.Backend import gpuarray

from PuzzleLib.Containers.Sequential import Sequential
from PuzzleLib.Containers.Parallel import Parallel

from PuzzleLib.Modules.Embedder import Embedder
from PuzzleLib.Modules.Reshape import Reshape
from PuzzleLib.Modules.Replicate import Replicate
from PuzzleLib.Modules.Conv2D import Conv2D
from PuzzleLib.Modules.MaxPool2D import MaxPool2D
from PuzzleLib.Modules.Concat import Concat
from PuzzleLib.Modules.Activation import Activation, relu
from PuzzleLib.Modules.Dropout import Dropout
from PuzzleLib.Modules.Linear import Linear

def buildBranch(fHeight, sentlength, branchMaps, embsize):
	seq = Sequential()

	seq.append(Conv2D(1, outmaps=branchMaps, size=(fHeight, embsize)))
	seq.append(MaxPool2D(size=(sentlength - fHeight + 1, 1)))
	seq.append(Reshape((-1, branchMaps)))

	return seq

def buildNet(vocabulary, branches, w2v, sentlength, embsize, wscale, dim=2, branchMaps=100, name="sentinet"):
	def onVocabulary(W):
		W[0] = np.zeros((1, embsize), dtype=np.float32)

		arrayPOS = [
			"", "_S", "_A", "_V", "_UNKN", "_ADJ", "_ADV", "_INTJ", "_NOUN", "_PROPN", "_VERB", "_ADP",
			"_AUX", "_CCONJ", "_DET", "_NUM", "_PART", "_PRON", "_SCONJ", "_SUM", "_X"
		]

		tmpPOS = []
		if not w2v:
			return

		for word in vocabulary:
			for pos in tmpPOS:
				if (word + pos) in w2v.vocab:
					W[vocabulary[word]] = w2v[word + pos]
					break

			for i, pos in enumerate(arrayPOS):
				if (word + pos) in w2v.vocab:
					tmpPOS.append(pos)
					W[vocabulary[word]] = w2v[word + pos]
					del arrayPOS[i]
					break

	net = Sequential(name)
	net.setAttr("timestamp", int(time.time()))

	net.append(Embedder(
		vocabulary, sentlength, embsize, wscale=wscale, onVocabulary=onVocabulary, learnable=True, name="embedder"
	))

	net.append(Reshape((-1, 1, sentlength, embsize)))

	branchNum = len(branches)
	net.append(Replicate(times=branchNum))

	par = Parallel()

	for branchFilterSize in branches:
		par.append(buildBranch(branchFilterSize, sentlength, branchMaps, embsize))

	net.append(par)
	net.append(Concat(axis=1))
	net.append(Activation(relu))
	net.append(Dropout(p=0.5))

	net.append(Linear(branchNum * branchMaps, dim))

	return net

def unittest():
	vocabsize = 1000
	sentlength, embsize = 100, 128

	data = gpuarray.to_gpu(np.random.randint(0, vocabsize, (1, sentlength), dtype=np.int32))

	senti = buildNet(vocabsize, (3, 5, 7), None, sentlength, embsize, 1.0)
	senti(data)

	del senti
	gpuarray.memoryPool.freeHeld()

if __name__ == "__main__":
	unittest()

max_stars_repo_name: EmilPi/PuzzleLib

max_issues_repo_name: EmilPi/PuzzleLib

max_forks_repo_name: EmilPi/PuzzleLib


text:
import numpy as np
from fitter import *
left = 130
right = 270

path =[r'D:\data\20190511\154729_power_sweep_7.002MoRe_Al_50mk',
r'D:\data\20190511\160529_power_sweep_7.002MoRe_Al_100mk',
r'D:\data\20190511\162138_power_sweep_7.002MoRe_Al_200mk',
r'D:\data\20190511\164423_power_sweep_7.002MoRe_Al_500mk',
r'D:\data\20190511\171153_power_sweep_7.002MoRe_Al_800mk']

name = '7.002 GHz  MoRe-Al'

temp = [50,100,200,500,800]

n = len(path)
ki = np.array([9.5284e+05,9.6835e+05,9.7947e+05,1.0056e+06,1.0193e+06])#np.zeros(n)
f0 = np.array([7.0038e+09,7.0038e+09,7.0038e+09,7.0038e+09,7.0038e+09] )#np.zeros(n)
ki_err = np.array([2.29e+03,2.59e+03,2.58e+03,2.84e+03,2.72e+03] )#np.zeros(n)
f0_err = np.array([1.17e+03,1.32e+03,1.31e+03,1.41e+03,1.34e+03] )#np.zeros(n)
Q_err = np.zeros(n)

for i in range(n):
	# data_name = path[i]+path[i][16:]+r'.dat'
	# data = np.loadtxt(data_name, unpack=True)
	# freq = data[1][left:right]
	# real = data[2]
	# imag = data[3]
	# absol = data[4][left:right]
	# f = Fitter(custom)
	# result = f.fit(freq, absol, print_report = True)
	# f.plot()
	# ki[i] = result.params['ki'].value
	# f0[i] = result.params['f0'].value
	# ki_err[i] = result.params['ki'].stderr
	# f0_err[i] = result.params['f0'].stderr
	Q_err[i] = (f0_err[i]/f0[i] + ki_err[i]/ki[i])*(f0[i]/ki[i])

fig, ax = plt.subplots(nrows=1, ncols=1, sharex=True)

ax.errorbar(temp, (f0/ki)/1e3, fmt='o', yerr = Q_err/1e3 ,capsize=2, elinewidth=1, markeredgewidth=2)
ax.set_xlabel(r'$T (mK)$')
ax.set_ylabel(r'$Q_{i} (kU)$')
ax.set_title(name)
plt.show()

# fr.fit(freq, real[-1], print_report = True)
# fr.plot()
# fm.fit(freq, imag[-1], print_report = True)
# fm.plot()

max_stars_repo_name: sourav-majumder/qtlab

max_issues_repo_name: sourav-majumder/qtlab

max_forks_repo_name: sourav-majumder/qtlab


text:
import logging
import numpy as np
from bico.geometry.point import Point
from bico.nearest_neighbor.base import NearestNeighbor
from bico.utils.ClusteringFeature import ClusteringFeature
from datetime import datetime
from typing import Callable, TextIO, List

logger = logging.getLogger(__name__)

class BICONode:
    def __init__(self, level: int, dim: int, proj: int, bico: 'BICO',
                 projection_func: Callable[[int, int, float], NearestNeighbor]):
        self.level = level
        self.dim = dim
        self.proj = proj
        self.point_to_biconode = []
        self.projection_func = projection_func
        self.nn_engine = projection_func(dim, proj, bico.get_radius(self.level))
        self.num_cfs = 0
        self.bico = bico
        self.cf = ClusteringFeature(Point(np.zeros(dim)), Point(np.zeros(dim)), 0, 0)

    def insert_point(self, point_cf: ClusteringFeature) -> int:
        if self.bico.verbose:
            logger.debug("Insert point: {}".format(point_cf))
        # check whether geometry fits into CF
        if self.level > 0:
            if self.cf.size == 0:
                self.cf += point_cf
                self.cf.ref = point_cf.ref
            else:
                test = self.cf + point_cf
                cost = test.kmeans_cost(self.cf.ref)
                if self.bico.verbose:
                    logger.debug("Cost: " + str(cost) + ", Thresh: " + str(self.bico.get_threshold(self.level)))
                if cost < self.bico.get_threshold(self.level):
                    self.cf = test
                    return 0

        # search nearest neighbor and insert geometry there or open new BICONode
        candidates = []
        if self.num_cfs > 0:
            if self.bico.track_time:
                tstart = datetime.now()
            candidates = self.nn_engine.get_candidates(point_cf.ref.p)
            # candidates = self.ann_engine.neighbours(point_cf.ref.p)
            if self.bico.track_time:
                tend = datetime.now()
                if len(self.bico.time) < self.level + 1:
                    self.bico.time.append(tend - tstart)
                else:
                    self.bico.time[self.level] += tend - tstart
        if len(candidates) == 0:
            if self.bico.verbose:
                logger.debug("No nearest neighbor found.")
            self.num_cfs += 1
            self.nn_engine.insert_candidate(point=point_cf.ref.p, metadata=self.num_cfs)
            # self.ann_engine.store_vector(point_cf.ref.p, data=self.num_cfs)
            new_node = BICONode(self.level + 1, self.dim, self.proj, self.bico, self.projection_func)
            # new_node.cf = ClusteringFeature(geometry, geometry, geometry*geometry, 1)
            new_node.cf = point_cf
            # debug
            if len(self.point_to_biconode) != self.num_cfs - 1:
                logger.error("Something is wrong: {} != {}".format(len(self.point_to_biconode), self.num_cfs - 1))
            self.point_to_biconode.append(new_node)
            return 1
        else:
            if self.bico.verbose:
                logger.debug(str(len(candidates)) + " nearest neighbor found!")
                logger.debug(candidates)
            nearest = candidates[0]
            node = nearest.data  # contains the index
            # sanity check
            if len(self.point_to_biconode) < node - 2:
                logger.error("Something is wrong: {} > {}".format(len(self.point_to_biconode), node - 2))
            return self.point_to_biconode[node - 1].insert_point(point_cf)

    def output_cf(self, f: TextIO) -> None:
        if self.level > 0:
            f.write(str(self.cf) + "\n")
        for node in self.point_to_biconode:
            node.output_cf(f)

    def get_cf(self) -> List[np.ndarray]:
        cur = []
        if self.level > 0:
            cur.append(np.insert(self.cf.center().p, 0, self.cf.size))
        for node in self.point_to_biconode:
            cur = cur + node.get_cf()
        return cur

max_stars_repo_name: gallmerci/bico

max_issues_repo_name: gallmerci/bico

max_forks_repo_name: gallmerci/bico


text:
"""a module that houses plotting routines for common uses, defining common parameters for plotting logic
"""
__author__ = "Reed Essick (reed.essick@gmail.com)"

#-------------------------------------------------

import os

import numpy as np

import matplotlib
matplotlib.use("Agg")
from matplotlib import cm
from matplotlib import pyplot as plt
plt.rcParams['font.family'] = 'serif'
if matplotlib.__version__ < '1.3.0':
    plt.rcParams['text.usetex'] = True

### non-standard libraries
from universality.kde import DEFAULT_BANDWIDTH
from universality.stats import (logkde2levels, neff)

#-------------------------------------------------

DEFAULT_LEVELS=[0.5, 0.9]
DEFAULT_NUM_POINTS = 25
DEFAULT_BANDWIDTH = DEFAULT_BANDWIDTH
DEFAULT_FIGWIDTH = DEFAULT_FIGHEIGHT = 6

DEFAULT_COV_FIGWIDTH = 10
DEFAULT_COV_FIGHEIGHT = 4

DEFAULT_COLOR1 = 'k'
DEFAULT_COLOR2 = 'r'
DEFAULT_COLOR3 = 'b'
DEFAULT_COLORMAP = 'RdGy_r'

DEFAULT_TRUTH_COLOR = 'b'

DEFAULT_LINEWIDTH = 1.
DEFAULT_LINESTYLE = 'solid'
DEFAULT_MARKER = None
DEFAULT_ALPHA = 1.0
DEFAULT_LIGHT_ALPHA = 0.25

DEFAULT_FIGTYPES = ['png']
DEFAULT_DPI = 100
DEFAULT_DIRECTORY = '.'

#------------------------

CORNER_LEFT = 0.8
CORNER_RIGHT = 0.2
CORNER_TOP = 0.2
CORNER_BOTTOM = 0.8

HSPACE = 0.05
WSPACE = 0.05

#------------------------

MAIN_AXES_POSITION = [0.18, 0.39, 0.77, 0.56]
RESIDUAL_AXES_POSITION = [0.18, 0.18, 0.77, 0.2]
AXES_POSITION = [
    MAIN_AXES_POSITION[0],
    RESIDUAL_AXES_POSITION[1],
    MAIN_AXES_POSITION[2],
    MAIN_AXES_POSITION[1]+MAIN_AXES_POSITION[3]-RESIDUAL_AXES_POSITION[1]
]

#-------------------------------------------------

def setp(*args, **kwargs):
    return plt.setp(*args, **kwargs)

def figure(*args, **kwargs):
    return plt.figure(*args, **kwargs)

def subplot(*args, **kwargs):
    return plt.subplot(*args, **kwargs)

def subplots_adjust(*args, **kwargs):
    return plt.subplots_adjust(*args, **kwargs)

def save(basename, fig, figtypes=DEFAULT_FIGTYPES, directory=DEFAULT_DIRECTORY, verbose=False, **kwargs):
    template = os.path.join(directory, basename+'.%s')
    for figtype in figtypes:
        figname = template%figtype
        if verbose:
            print('saving: '+figname)
        fig.savefig(figname,  **kwargs)

def weights2color(weights, basecolor, prefact=100., minimum=1e-3):
    Nsamp = len(weights)
    scatter_color = np.empty((Nsamp, 4), dtype=float)
    scatter_color[:,:3] = matplotlib.colors.ColorConverter().to_rgb(basecolor)
    mw, Mw = np.min(weights), np.max(weights)
    if np.all(weights==Mw):
        scatter_color[:,3] = max(min(1., 1.*prefact/Nsamp), minimum) ### equal weights
    else:
        scatter_color[:,3] = weights/np.max(weights)
        scatter_color[:,3] *= prefact/neff(weights)
        scatter_color[scatter_color[:,3]>1,3] = 1 ### give reasonable bounds
        scatter_color[scatter_color[:,3]<minimum,3] = minimum ### give reasonable bounds
    return scatter_color

def close(fig):
    plt.close(fig)

max_stars_repo_name: isaaclegred/universality

max_issues_repo_name: isaaclegred/universality

max_forks_repo_name: isaaclegred/universality


text:
import datetime
import os
import random
import time
import gc
import sys
import numpy as np
from skimage import io
import torch
from torch import optim
from torch.autograd import Variable
from torch.backends import cudnn
from torch.optim.lr_scheduler import ReduceLROnPlateau
from torch.utils.data import DataLoader

from datasets import list_dataset
from models import *
from utils import check_mkdir, evaluate, AverageMeter, CrossEntropyLoss2d

cudnn.benchmark = True

# Predefining directories.
ckpt_path = './ckpt'
outp_path = './outputs'

# Reading system parameters.
conv_name = sys.argv[1] # Semantic segmentation network name (default: segnet)
fold_name = sys.argv[2] # Default: 0 (for k-fold validation)
data_name = sys.argv[3] # Name of the folder of the dataset
task_name = sys.argv[4] # Name of the segmentation task (for when you have diffent segmentations for the same image), necessary for the folder configuration (see readme).

# Setting experiment name.
exp_name  = conv_name + '_' + data_name + '_' + task_name + '_' + fold_name

# Setting predefined arguments.
args = {
    'epoch_num': 500,     # Number of epochs.
    'lr': 1e-4,           # Learning rate.
    'weight_decay': 5e-4, # L2 penalty.
    'momentum': 0.9,      # Momentum.
    'lr_patience': 100,   # Patience for the scheduler to reduce learning rate.
    'num_workers': 0,     # Number of workers on data loader.
    'snapshot': '',       # Network to use as basis for training.
    'batch_size': 4,      # Mini-batch size.
    'print_freq': 10,      # Printing frequency for mini-batch loss.
    'val_freq': 1,         # Validate each val_freq epochs.
    'patch_size': 480     # image patch size during training
}

# Main function.
def main(train_args):

    if (conv_name == 'segnet'):
        net = segnet(num_classes=list_dataset.num_classes, in_channels=3).cuda()
        net.init_vgg16_params()

    # Loading optimizer state in case of resuming training.
    if len(train_args['snapshot']) == 0:

        curr_epoch = 1
        train_args['best_record'] = {'epoch': 0, 'lr': 1e-4, 'val_loss': 1e10, 'acc': 0, 'acc_cls': 0, 'iou': 0}

    else:

        print 'training resumes from ' + train_args['snapshot']
        net.load_state_dict(torch.load(os.path.join(ckpt_path, exp_name, train_args['snapshot'])))
        split_snapshot = train_args['snapshot'].split('_')
        curr_epoch = int(split_snapshot[1]) + 1
        train_args['best_record'] = {'epoch': int(split_snapshot[1]), 'lr': optimizer.param_groups[1]['lr'], 'val_loss': float(split_snapshot[3]),
                                     'acc': float(split_snapshot[5]), 'acc_cls': float(split_snapshot[7]),
                                     'iou': float(split_snapshot[9])}

    # Setting datasets.
    train_set = list_dataset.ListDataset('train', data_name, task_name, fold_name, 'statistical', train_args['patch_size'])
    train_loader = DataLoader(train_set, batch_size=train_args['batch_size'], num_workers=train_args['num_workers'], shuffle=True)

    val_set = list_dataset.ListDataset('validation', data_name, task_name, fold_name, 'statistical')
    val_loader = DataLoader(val_set, batch_size=1, num_workers=train_args['num_workers'], shuffle=False)

    test_set = list_dataset.ListDataset('test', data_name, task_name, fold_name, 'statistical')
    test_loader = DataLoader(test_set, batch_size=1, num_workers=train_args['num_workers'], shuffle=False)

    # Setting criterion.
    if data_name == 'grss_semantic':
        criterion = CrossEntropyLoss2d(ignore_index = -1).cuda()
    else:
        criterion = CrossEntropyLoss2d().cuda()
    # Setting optimizer.
    optimizer = optim.Adam([
        {'params': [param for name, param in net.named_parameters() if name[-4:] == 'bias'],
         'lr': 2 * train_args['lr']},
        {'params': [param for name, param in net.named_parameters() if name[-4:] != 'bias'],
         'lr': train_args['lr'], 'weight_decay': train_args['weight_decay']}
    ], betas=(train_args['momentum'], 0.99))

    # Loading optimizer state in case of resuming training.
    if len(train_args['snapshot']) > 0:

        optimizer.load_state_dict(torch.load(os.path.join(ckpt_path, exp_name, 'opt_' + train_args['snapshot'])))
        optimizer.param_groups[0]['lr'] = 2 * train_args['lr']
        optimizer.param_groups[1]['lr'] = train_args['lr']

    # Making sure checkpoint and output directories are created.
    check_mkdir(ckpt_path)
    check_mkdir(os.path.join(ckpt_path, exp_name))
    check_mkdir(outp_path)
    check_mkdir(os.path.join(outp_path, exp_name))

    # Writing training args to experiment log file.
    open(os.path.join(ckpt_path, exp_name, str(datetime.datetime.now()) + '.txt'), 'w').write(str(train_args) + '\n\n')

    # Setting plateau scheduler for learning rate.
    scheduler = ReduceLROnPlateau(optimizer, 'min', factor=0.5, patience=train_args['lr_patience'], min_lr=2.5e-5, verbose=True)

    # Iterating over epochs.
    for epoch in range(curr_epoch, train_args['epoch_num'] + 1):

        # Training function.
        train_iou = train(train_loader, net, criterion, optimizer, epoch, train_args)

        if epoch % 1 == 0:

            # Computing validation loss and test loss.
            val_iou = validate(val_loader, net, criterion, optimizer, epoch, train_args, task_name)
            #test_iou = test(test_loader, net, criterion, optimizer, epoch, train_args, task_name)

            # Scheduler step.
            scheduler.step(val_iou)

# Training function.
def train(train_loader, net, criterion, optimizer, epoch, train_args):

    # Setting network for training mode.
    net.train()

    # Average Meter for batch loss.
    train_loss = AverageMeter()

    # Lists for whole epoch loss.
    inps_all, labs_all, prds_all = [], [], []

    # Iterating over batches.
    for i, data in enumerate(train_loader):

        # Obtaining images, labels and paths for batch.
        inps, labs, img_name = data

        # Casting tensors to cuda.
        inps, labs = inps.cuda(), labs.cuda()

        # Sanity check on image/label sizes.
        #print(inps.size(), labs.size())
        assert inps.size()[2:] == labs.size()[1:]

        # Casting to cuda variables.
        inps = Variable(inps).cuda()
        labs = Variable(labs).cuda()

        # Clears the gradients of optimizer.
        optimizer.zero_grad()

        # Forwarding.
        outs = net(inps)

        # Computing loss.
        loss = criterion(outs, labs)

        # Obtaining predictions.
        prds = outs.data.max(1)[1].squeeze_(1).squeeze_(0).cpu().numpy()

        # Sanity check on output, label and num_class.
        assert outs.size()[2:] == labs.size()[1:]
        assert outs.size()[1] == list_dataset.num_classes

        # Computing backpropagation.
        loss.backward()
        optimizer.step()

        # Appending images for epoch loss calculation.
        inps_all.append(inps.data.squeeze_(0).cpu())
        labs_all.append(labs.data.squeeze_(0).cpu().numpy())
        prds_all.append(prds)

        # Updating loss meter.
        train_loss.update(loss.data[0], inps.size(0))

        # Printing.
        #if (i + 1) % train_args['print_freq'] == 0:
        #    print '[epoch %d], [iter %d / %d], [train loss %.5f]' % (
        #        epoch, i + 1, len(train_loader), train_loss.avg()
        #    )

    # Computing error metrics for whole epoch.
    acc, acc_cls, mean_iou, iou, fwavacc, kappa = evaluate(prds_all, labs_all, list_dataset.num_classes, task_name)

    # Printing epoch loss.
    print('--------------------------------------------------------------------')
    print('[epoch %d], [lr %.9f], [train loss %.4f], [acc %.4f], [acc_cls %.4f], [iou %.4f], [fwavacc %.4f], [kappa %.4f]' % (
        epoch, optimizer.param_groups[1]['lr'], train_loss.avg(), acc, acc_cls, mean_iou, fwavacc, kappa))
    print('--------------------------------------------------------------------')

    # Returning iou.
    return mean_iou

def validate(val_loader, net, criterion, optimizer, epoch, train_args, task_name):
    outp_path = './outputs'
    # Setting network for evaluation mode.
    net.eval()

    # Average Meter for batch loss.
    val_loss = AverageMeter()

    # Lists for whole epoch loss.
    inps_all, labs_all, prds_all = [], [], []

    save_img_epoch = 20

    if epoch % save_img_epoch == 0:
        check_mkdir(os.path.join(outp_path, exp_name, 'epoch_' + str(epoch)))

    # Iterating over batches.
    for i, data in enumerate(val_loader):

        # Obtaining images, labels and paths for batch.
        inps, labs, img_name = data

        # Casting tensors to cuda.
        inps, labs = inps.cuda(), labs.cuda()

        # Casting to cuda variables.
        inps = Variable(inps, volatile=True).cuda()
        labs = Variable(labs, volatile=True).cuda()

        # Forwarding.
        outs = net(inps)

        # Computing loss.
        loss = criterion(outs, labs)

        # Obtaining predictions.
        prds = outs.data.max(1)[1].squeeze_(1).squeeze_(0).cpu().numpy()
        label = labs.data.squeeze_(0).cpu().numpy()

        # Saving prediction image
        if epoch % save_img_epoch == 0:
            tmp_path = os.path.join(outp_path, exp_name, 'epoch_{}'.format(epoch), img_name[0])
            h, w = prds.shape

            if data_name == 'grss_semantic':
                new = np.zeros((h, w, 3), dtype=np.uint8)
                for i in range(h):
                    for j in range(w):
                        if label[i][j] == -1:
                            new[i][j] = [0,0,0]

                        elif prds[i][j] == 0:
                            new[i][j] = [255,0,255]

                        elif prds[i][j] == 1:
                            new[i][j] = [0,255,0]

                        elif prds[i][j] == 2:
                            new[i][j] = [255,0,0]

                        elif prds[i][j] == 3:
                            new[i][j] = [0,255,255]

                        elif prds[i][j] == 4:
                            new[i][j] = [160,32,240]

                        elif prds[i][j] == 5:
                            new[i][j] = [46,139,87]

                        else:
                            sys.exit('Invalid prediction')

                io.imsave(tmp_path+'.png', new)
            elif data_name == 'coffee_1_semantic' or data_name == 'coffee_2_semantic' or data_name == 'coffee_3_semantic' :
                new = np.zeros((h, w), dtype=np.uint8)

                for i in range(h):
                    for j in range(w):
                        if prds[i][j] == 0:
                            new[i][j] = 0

                        elif prds[i][j] == 1:
                            new[i][j] = 255

                        else:
                            sys.exit('Invalid prediction')

                io.imsave(tmp_path+'.png', new)

            elif data_name == 'vaihingen_semantic':
                new = np.zeros((h, w, 3), dtype=np.uint8)
                for x in range(h):
                    for y in range(w):
                        if prds[x][y] == 0:
                            new[x][y] = [255,255,255]

                        elif prds[x][y] == 1:
                            new[x][y] = [0,0,255]

                        elif prds[x][y] == 2:
                            new[x][y] = [0,255,255]

                        elif prds[x][y] == 3:
                            new[x][y] = [0,255,0]

                        elif prds[x][y] == 4:
                            new[x][y] = [255,255,0]

                        elif prds[x][y] == 5:
                            new[x][y] = [255,0,0]

                        else:
                            sys.exit('Invalid prediction')
                io.imsave(tmp_path+'.png', new)

        # Appending images for epoch loss calculation.
        inps_all.append(inps.data.squeeze_(0).cpu())
        labs_all.append(label)
        prds_all.append(prds)

        # Updating loss meter.
        val_loss.update(loss.data[0], inps.size(0))

    acc, acc_cls, mean_iou, iou, fwavacc, kappa = evaluate(prds_all, labs_all, list_dataset.num_classes, task_name)

    if mean_iou > train_args['best_record']['iou']:

        train_args['best_record']['val_loss'] = val_loss.avg()
        train_args['best_record']['lr'] = optimizer.param_groups[1]['lr']
        train_args['best_record']['epoch'] = epoch
        train_args['best_record']['acc'] = acc
        train_args['best_record']['acc_cls'] = acc_cls
        train_args['best_record']['iou'] = mean_iou
        train_args['best_record']['fwavacc'] = fwavacc
        train_args['best_record']['kappa'] = kappa
        snapshot_name = 'epoch_%d_loss_%.5f_acc_%.5f_acc-cls_%.5f_iou_%.5f_fwavacc_%.5f_lr_%.10f' % (
            epoch, val_loss.avg(), acc, acc_cls, mean_iou, fwavacc, optimizer.param_groups[1]['lr']
        )
        torch.save(net.state_dict(), os.path.join(ckpt_path, exp_name, 'best.pth'))
        torch.save(optimizer.state_dict(), os.path.join(ckpt_path, exp_name, 'opt_best.pth'))

    # Printing epoch loss.
    print('--------------------------------------------------------------------')
    print('[epoch %d], [lr %.9f], [val loss %.9f], [acc %.4f], [acc_cls %.4f] [iou %.4f] [kappa %.4f]' % (epoch, optimizer.param_groups[1]['lr'], val_loss.avg(), acc, acc_cls, mean_iou, kappa))

    # Printing best epoch loss so far.
    print('best record: [lr %.9f], [val loss %.9f], [acc %.4f], [acc_cls %.4f], [iou %.4f], [kappa %.4f], [epoch %d]' % (train_args['best_record']['lr'], train_args['best_record']['val_loss'], train_args['best_record']['acc'], train_args['best_record']['acc_cls'], train_args['best_record']['iou'], train_args['best_record']['kappa'] , train_args['best_record']['epoch']))

    print('--------------------------------------------------------------------')

    # Returning iou.
    return mean_iou

def test(test_loader, net, criterion, optimizer, epoch, train_args, task_name):

    # Setting network for evaluation mode.
    net.eval()

    # Average Meter for batch loss.
    test_loss = AverageMeter()

    # Lists for whole epoch loss.
    inps_all, labs_all, prds_all = [], [], []

    # Iterating over batches.
    for i, data in enumerate(test_loader):

        # Obtaining images, labels and paths for batch.
        inps, labs, img_name = data

        # Casting tensors to cuda.
        inps, labs = inps.cuda(), labs.cuda()

        # Casting to cuda variables.
        inps = Variable(inps, volatile=True).cuda()
        labs = Variable(labs, volatile=True).cuda()

        # Forwarding.
        outs = net(inps)

        # Computing loss.
        loss = criterion(outs, labs)

        # Obtaining predictions.
        prds = outs.data.max(1)[1].squeeze_(1).squeeze_(0).cpu().numpy()

        # Appending images for epoch loss calculation.
        inps_all.append(inps.data.squeeze_(0).cpu())
        labs_all.append(labs.data.squeeze_(0).cpu().numpy())
        prds_all.append(prds)

        # Updating loss meter.
        test_loss.update(loss.data[0], inps.size(0))

    acc, acc_cls, mean_iou, iou, fwavacc, kappa = evaluate(prds_all, labs_all, list_dataset.num_classes, task_name)
    # Printing epoch loss.
    print('--------------------------------------------------------------------')
    print('[epoch %d], [lr %.9f], [test loss %.4f], [acc %.4f], [acc_cls %.4f], [iou %.4f +/- %.4f]' % (
        epoch, optimizer.param_groups[1]['lr'], test_loss.avg(), acc, acc_cls, mean_iou, iou_list.std()))
    print('--------------------------------------------------------------------')

    # Returning iou.
    return mean_iou #test_loss.avg

if __name__ == '__main__':
    main(args)

max_stars_repo_name: matheusbarrosp/sr-semseg-pytorch

max_issues_repo_name: matheusbarrosp/sr-semseg-pytorch

max_forks_repo_name: matheusbarrosp/sr-semseg-pytorch


text:
#
from PIL import Image
import torch
from torch.utils.data import DataLoader, Dataset
import torchvision.transforms as transforms
import glob
import numpy as np

class OmniglotDs(Dataset):
    def __init__(self, data_dir, n_way, k_shot, q_query):
        self.file_list = [f for f in glob.glob(data_dir +
                    '**/character*', recursive=True)]
        self.transform = transforms.Compose([transforms.ToTensor()])
        self.n_way = n_way
        self.n = k_shot + q_query
        self.k_shot = k_shot

    def __getitem__(self, idx):
        sample = np.arange(20)
        np.random.shuffle(sample) # 這裡是為了等一下要 random sample 出我們要的 character
        img_path = self.file_list[idx]
        img_list = [f for f in glob.glob(img_path + "**/*.png",
                    recursive=True)]
        img_list.sort()
        imgs = [self.transform(Image.open(img_file))
                     for img_file in img_list]
        imgs = torch.stack(imgs)[sample[:self.n]] # 每個 character，取出 k_shot + q_query 個
        y = torch.tensor([]).new_full((self.n,), idx % self.n_way, dtype=torch.long)
        return imgs, y

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

    def load_ds(self):
        i = 10
        img = Image.open('./data/Omniglot/images_background/Japanese_(hiragana).0/character13/0500_{0}.png'.format(i))
        img.show()


text:
import math
from collections import defaultdict
from time import time
from typing import List

import numpy as np

from logger import logger
from util import validate_input, sigmoid

class Model(object):
    def __init__(self):
        pass

    def train(self):
        return NotImplemented

class MultinomialNaiveBayesModel(Model):
    """
    parsing input text
    """

    def __init__(self, laplace_smoothing=True):
        super(MultinomialNaiveBayesModel, self).__init__()
        self.class_counter = {}
        self.class_prior = {}
        self.cond_prob_estimates = defaultdict(dict)
        self.laplace_smoothing = laplace_smoothing

    def _validate_input(self, documents: List, labels: List):
        validate_input(documents, labels)

    def train(self, features: List, labels: List):
        """
        :param features:
        :param labels:
        :return:
        """
        self._validate_input(features, labels)
        self.num_class = len(set(labels))
        labels = labels

        for label in labels:
            self.class_counter[label] = self.class_counter.get(label, 0) + 1

        for k, v in self.class_counter.items():
            self.class_prior[k] = math.log(v / len(labels))

        class_feature_counter = defaultdict(dict)
        for row, label in zip(features, labels):

            # iterate through features in a single data point
            for i in range(len(row)):
                class_feature_counter[label][i] = class_feature_counter[
                                                      label].get(i, 0) + row[i]

        for label in self.class_prior:
            d = class_feature_counter[label]
            den = sum(d.values())
            if self.laplace_smoothing:
                den += len(features[0])

            for i in range(len(features[0])):
                if self.laplace_smoothing:
                    num = 1
                if i in d:
                    num += d[i]
                self.cond_prob_estimates[label][i] = math.log(num / den)

    def predict(self, features: List):
        predictions = []
        for row in features:
            class_scores = {}
            for label, log_prior in self.class_prior.items():
                class_scores[label] = log_prior

            for i in range(len(row)):
                for label, _ in self.class_prior.items():
                    class_scores[label] += row[i] * self.cond_prob_estimates[
                        label].get(
                        i, 0)
            predictions.append(
                sorted(class_scores.items(), reverse=True, key=lambda x: x[1])[
                    0][0])
        return predictions

class DiscreteNaiveBayesModel(Model):
    """
    Discrete Naive Bayes Model
    """

    def __init__(self, laplace_smoothing=True):
        super(DiscreteNaiveBayesModel, self).__init__()
        self.class_counter = {}
        self.class_prior = {}
        self.cond_prob_estimates = defaultdict(dict)
        self.laplace_smoothing = laplace_smoothing

    def _validate_input(self, documents: List, labels: List):
        validate_input(documents, labels)

    def train(self, features: List, labels: List):
        """
        :param features:
        :param labels:
        :return:
        """
        self._validate_input(features, labels)
        self.num_class = len(set(labels))

        for label in labels:
            self.class_counter[label] = self.class_counter.get(label, 0) + 1

        for k, v in self.class_counter.items():
            self.class_prior[k] = math.log(v / len(labels))

        class_feature_counter = defaultdict(dict)
        for row, label in zip(features, labels):

            # iterate through features in a single data point
            for i in range(len(row)):
                if row[i] > 0:
                    class_feature_counter[label][i] = class_feature_counter[
                                                          label].get(i, 0) + 1

        for label in self.class_prior:
            d = class_feature_counter[label]
            # den = self.class_counter[label]
            den = sum(d.values())
            if self.laplace_smoothing:
                den += len(features[0])
                # den += 1
            num = 0
            for i in range(len(features[0])):
                if self.laplace_smoothing:
                    num = 1
                if i in d:
                    num += d[i]
                self.cond_prob_estimates[label][i] = math.log(num / den)

    def predict(self, features: List):
        predictions = []
        for row in features:
            class_scores = {}
            for label, log_prior in self.class_prior.items():
                class_scores[label] = log_prior

            for i in range(len(row)):
                for label, _ in self.class_prior.items():
                    if row[i] > 0:
                        class_scores[label] += self.cond_prob_estimates[
                            label].get(
                            i, 0)
            predictions.append(
                sorted(class_scores.items(), reverse=True, key=lambda x: x[1])[
                    0][0])
        return predictions

class LogisticRegression(Model):

    def __init__(self, regularizer: str = "l2", reg_costant=0.1,
                 learning_rate=0.01,
                 max_iterations=100, n_features=None, verbose=True):
        self.regularizer = regularizer
        self.reg_costant = reg_costant
        self.lr = learning_rate
        self.max_iterations = max_iterations
        self.n_features = n_features
        self.verbose = verbose
        self.sigmoid = np.vectorize(sigmoid)

    def _validate_input(self, features: List, labels: List):
        validate_input(documents=features, labels=labels)

    def _append_bias_dimension(self, features):
        # TODO improve this
        new_features = []
        for row in features:
            row = list(row)
            row.append(1)  # appending new dimension for bias handling
            new_features.append(row)
        return new_features

    def train(self, features=None, labels=None):
        if self.n_features is None:
            self.n_features = features[0].__len__()
        else:
            if features[0].__len__() != self.n_features:
                raise ValueError(
                    "Feature vector length is {} \n needed length is {}!!".format(
                        features[0].__len__(), self.n_features))
        self._validate_input(features, labels)

        self.unique_labels = set(labels)
        if len(self.unique_labels) > 2:
            raise ValueError("Implements only supports binary classification!!")

        self.label_map = {}

        self.weights = np.random.normal(0, 0.1, self.n_features + 1)

        features = self._append_bias_dimension(features)

        for index, l in enumerate(self.unique_labels):
            self.label_map[l] = index
        logger.info("Label mapping {}\n".format(self.label_map))

        labels = [self.label_map[label] for label in labels]  # convert to 0/1

        if not isinstance(features, np.ndarray):
            features = np.array(features)
        if not isinstance(labels, np.ndarray):
            labels = np.array(labels)

        # Gradient descent
        for epoch in range(self.max_iterations):
            if self.verbose and epoch % 10 == 0:
                logger.info("At training step {}".format(epoch))

            # yl - P(yl=1|xl, weight)
            a = features @ self.weights  # matrix multiply
            prob_error = labels - self.sigmoid(a)
            if self.verbose and epoch % 10 == 0:
                logger.info("Prob error estimate: {}\n".format(sum(prob_error)))

            for i, wi in enumerate(self.weights):

                # Do not regularize the bias. No point in doing that
                if i == len(self.weights) - 1:
                    wi = wi + self.lr * np.sum(features[..., i] * prob_error)
                else:
                    wi = wi + self.lr * np.sum(features[
                                                   ..., i] * prob_error) - self.lr * self.reg_costant * wi
                self.weights[i] = wi

    def decision(self, x, reverse_label_map):
        if x > 0:
            return reverse_label_map[1]
        else:
            return reverse_label_map[0]

    def predict(self, features):
        """
        Inference
        :param features: feature vectors as array or list
        :return: list of predictions
        """
        features = self._append_bias_dimension(features)

        if not hasattr(self, "weights"):
            raise ValueError("Model hasn't been trained yet!!")

        if not hasattr(self, "reverse_label_map"):
            self.reverse_label_map = {v: k
                                      for k, v in self.label_map.items()}
            self.decide = np.vectorize(
                lambda x: self.decision(x, self.reverse_label_map))

        if not isinstance(features, np.ndarray):
            features = np.array(features)

        s = features @ self.weights
        return self.decide(s)

max_stars_repo_name: mandalbiswadip/MLLearn

max_issues_repo_name: mandalbiswadip/MLLearn

max_forks_repo_name: mandalbiswadip/MLLearn