text:
import os
import numpy as np
import pandas as pd

import datetime
import uuid
from typing import List
from uuid import UUID

from datetime import datetime, timedelta
from typing import Any
import sys
import json
import codecs
import gzip
import numpy as np

from time import time
from scipy.stats import randint as sp_randint

from sklearn.model_selection import GridSearchCV
from sklearn.model_selection import RandomizedSearchCV
from sklearn.datasets import load_digits
from sklearn.ensemble import RandomForestClassifier

from sklearn.datasets import load_iris
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score, make_scorer
from sklearn.metrics import precision_recall_fscore_support, precision_score, recall_score, f1_score
from sklearn.metrics import classification_report
from scikitplot.metrics import plot_confusion_matrix
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import cross_val_predict
import pickle
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import scikitplot as skplt

MINIMUM_TIME_DIFFERENCE_BETWEEN_EPISODES = 10 * 60 * 1000;
# MINIMUM_TIME_DIFFERENCE_FIRST_AND_LAST_PUFFS = 7 * 60 * 1000;
MINIMUM_TIME_DIFFERENCE_FIRST_AND_LAST_PUFFS = 7  # minutes
# MINIMUM_INTER_PUFF_DURATION = 10 * 1000;
MINIMUM_INTER_PUFF_DURATION = 5  # seconds
MINIMUM_PUFFS_IN_EPISODE = 4;

import scikitplot as skplt
# http://scikit-plot.readthedocs.io/en/stable/estimators.html
from sklearn.metrics import confusion_matrix

def load_pickle(filename):
    with open(filename, 'rb') as handle:
        data = pickle.load(handle)
    return data

def my_plot_confution_matrix(X, Y):
    clf = RandomForestClassifier(n_jobs=2, random_state=0, n_estimators=100)

    scores = cross_val_score(clf, X, Y, cv=10, scoring='f1_macro')
    print("Cross-validation Accuracy: %0.2f (+/- %0.2f)" % (scores.mean(), scores.std() * 2))
    y_pred = cross_val_predict(clf, X, Y, cv=10)
    print('Precision: %.3f' % precision_score(y_true=Y, y_pred=y_pred))
    print('Recall: %.3f' % recall_score(y_true=Y, y_pred=y_pred))
    print('F1: %.3f' % f1_score(y_true=Y, y_pred=y_pred))

    plot_confusion_matrix(Y, y_pred, normalize=True)
    plt.xticks(rotation=90)
    plt.show()

def my_plot_confution_matrix_color(X, Y):
    clf = RandomForestClassifier(n_jobs=2, random_state=0, n_estimators=100)

    scores = cross_val_score(clf, X, Y, cv=10, scoring='f1_macro')
    print("Cross-validation Accuracy: %0.2f (+/- %0.2f)" % (scores.mean(), scores.std() * 2))
    y_pred = cross_val_predict(clf, X, Y, cv=10)
    cm = confusion_matrix(Y, y_pred)
    #     print(cm)
    # Show confusion matrix in a separate window
    plt.matshow(cm)
    plt.title('Confusion matrix')
    plt.colorbar()
    plt.ylabel('True label')
    plt.xlabel('Predicted label')
    plt.show()

def my_plot_learning_curve(X, Y):
    clf = RandomForestClassifier(n_jobs=2, random_state=0, n_estimators=100)
    skplt.estimators.plot_learning_curve(clf, X, Y)
    plt.xticks(rotation=90)
    plt.show()

def my_plot_feature_importances(X, Y, features):
    clf = RandomForestClassifier(n_jobs=2, random_state=0, n_estimators=100)
    clf.fit(X, Y)
    skplt.estimators.plot_feature_importances(clf, feature_names=features)
    plt.xticks(rotation=90)
    plt.show()

def process_puffmarker_wrist_level():
    #     F, X, Y, features = load_puffmarker_wrist_feature_data()
    X = load_pickle('Xs')
    Y = load_pickle('Ys')
    print(sum(Y))
    print(str(len(Y) - sum(Y)))
    # X = [v[:-4] for v in X]

    features = ['duration', 'roll_mean', 'roll_median', 'roll_sd',
                'roll_quartile', 'pitch_mean', 'pitch_median', 'pitch_sd',
                'pitch_quartile', 'yaw_mean', 'yaw_median', 'yaw_sd', 'yaw_quartile',
                'gyro_mean', 'gyro_median', 'gyro_sd', 'gyro_quartile',
                'ay_mean', 'ay_median', 'ay_sd', 'ay_quartile',
                'prev_dur', 'nxt_dur', 'prev dif', 'nxt_diff',
                'prev_aymn', 'nxt_aymn', 'prev aysd', 'nxt_aysd']
    # X = [v[:16] for v in X]
    # features = features[:16]
    my_plot_confution_matrix(X, Y)
    #     my_plot_precision_recall_curve(X, Y)
    my_plot_learning_curve(X, Y)
    my_plot_feature_importances(X, Y, features)

# Utility function to report best scores
def report(results, n_top=3):
    for i in range(1, n_top + 1):
        candidates = np.flatnonzero(results['rank_test_score'] == i)
        for candidate in candidates:
            print("Model with rank: {0}".format(i))
            print("Mean validation score: {0:.3f} (std: {1:.3f})".format(
                results['mean_test_score'][candidate],
                results['std_test_score'][candidate]))
            print("Parameters: {0}".format(results['params'][candidate]))
            print("")

def search_parameter(X, y):
    # build a classifier
    clf = RandomForestClassifier(n_estimators=20)

    #
    # # specify parameters and distributions to sample from
    # param_dist = {"max_depth": [3, 5, 7, 11, 13, None],
    #               "max_features": sp_randint(1, 15),
    #               "min_samples_split": sp_randint(2, 11),
    #               "min_samples_leaf": sp_randint(1, 11),
    #               "bootstrap": [True, False],
    #               "criterion": ["gini", "entropy"]}
    #
    # # run randomized search
    # n_iter_search = 20
    # random_search = RandomizedSearchCV(clf, param_distributions=param_dist,
    #                                    n_iter=n_iter_search)
    #
    # start = time()
    # random_search.fit(X, y)
    # print("RandomizedSearchCV took %.2f seconds for %d candidates"
    #       " parameter settings." % ((time() - start), n_iter_search))
    # report(random_search.cv_results_)

    # use a full grid over all parameters
    param_grid = {
        # 'n_estimators': [50, 100, 200, 500, 700],
        'n_estimators': [50, 100],
        'max_features': ['auto', 'sqrt', 'log2'],
        "max_depth": [3, 5, 7, 11, 13, None],
        # "max_features": [1, 3, 5, 10, 13, 15],
        "min_samples_split": [2, 3, 10],
        "min_samples_leaf": [1, 3, 10],
        "bootstrap": [True, False],
        "criterion": ["gini", "entropy"]}

    # run grid search
    scorer = make_scorer(f1_score)
    grid_search = GridSearchCV(estimator=clf, param_grid=param_grid, scoring=scorer, cv=10)
    start = time()
    gs = grid_search.fit(X, y)

    print(gs.best_score_)
    print(gs.best_params_)

    print("GridSearchCV took %.2f seconds for %d candidate parameter settings."
          % (time() - start, len(grid_search.cv_results_['params'])))
    report(grid_search.cv_results_)

if __name__ == '__main__':
    process_puffmarker_wrist_level()
    # X = load_pickle('Xs')
    # Y = load_pickle('Ys')
    # search_parameter(X, Y)
# -------------------------------------------------------------------------------------------------------------

# # get some data
# digits = load_digits()
# X, y = digits.data, digits.target

reason: import numpy,from scipy


text:
import pandas as pd
import numpy as np
import json
import scipy.sparse as sp

country  ='IDN'
industry = "Cultivation of oil seeds"

'''add a check if this file has been parsed'''
__location__ = 'data/exiobase/'

#souce country, target country, source industry target insdustry, weight, year
res=[]

for year in range(2005,2012):

    dir = 'IOT_'+str(year)+'_ixi/'
    print (year)

    params = json.load(open(__location__+dir+'file_parameters.json'))
    Afile = params['files']['A']
    A = pd.read_csv(__location__+dir+Afile['name'],sep='\t',memory_map=True,skiprows=int(Afile['nr_header']),index_col=list(range(int(Afile['nr_index_col']))))
    print 'load'
    Aindex = np.array(list(r for r in A.index))
    A = sp.csr_matrix(A)

    for j,i in enumerate(Aindex):
        if list(i) == [country,industry]:
            print 'index', i,j
            index = j

    nonzero = np.array(A.nonzero()).T

    print 'please wait'

    for to in nonzero[nonzero[:,0]== index][:,1]:
        temp = []
        temp.extend(Aindex[index])
        temp.extend(Aindex[to])
        temp.append(A[index,to])
        temp.append(year)

        res.append(temp)

print 'done'

ext: py

max_stars_repo_name: scpyork/postgresql

max_issues_repo_name: scpyork/postgresql

max_forks_repo_name: scpyork/postgresql

reason: import numpy,import scipy


text:
import unittest
import numpy as np
from unittest.mock import patch
import Transformation as my

atolerance, rtolerance = 0.01, 0.01

class TestPolygon(unittest.TestCase):
    # P1 is the square and P2 a pentagon
    inp = np.array([[1.0, 1.0, 1.0], [1.0, 5.0, 1.0],
                    [5.0, 5.0, 1.0], [5.0, 1.0, 1.0]])
    P1 = my.Polygon(inp)
    inp2 = np.array([[2.0, 1.0, 1.0], [4.0, 1.0, 1.0], [
                    5.0, 2.0, 1.0], [3.0, 3.0, 1.0], [1.0, 2.0, 1.0]])
    P2 = my.Polygon(inp2)
    inp3 = np.array([[0.0, 0.0, 1.0], [4.0, 0.0, 1.0],
                     [4.0, 4.0, 1.0], [0.0, 4.0, 1.0]])
    P3 = my.Polygon(inp3)

    def test_1(self):
        print('test_1 starts')
        user_output = self.P1.rotate(90)
        exp_output = (np.array([1.0, 5.0, 5.0, 1.0]),
                      np.array([-1.0, -1.0, -5.0, -5.0]))
        print(f'returned value {user_output}')
        print(f'expected value {exp_output}')
        np.testing.assert_allclose(exp_output, user_output, rtol=rtolerance, atol=atolerance)
        print('test_1 ends')

    def test_2(self):
        print('test_2 starts')
        user_output = self.P1.translate(2, 2)
        exp_output = (np.array([3.0, 7.0, 7.0, 3.0]),
                      np.array([1.0, 1.0, -3.0, -3.0]))
        print(f'returned value {user_output}')
        print(f'expected value {exp_output}')
        np.testing.assert_allclose(exp_output, user_output, rtol=rtolerance, atol=atolerance)
        print('test_2 ends')

    def test_3(self):
        print('test_3 starts')
        user_output = self.P1.scale(3, 2)
        exp_output = (np.array([-1.0, 11.0, 11.0, -1.0]),
                      np.array([3.0, 3.0, -5.0, -5.0]))
        print(f'returned value {user_output}')
        print(f'expected value {exp_output}')
        np.testing.assert_allclose(exp_output, user_output, rtol=rtolerance, atol=atolerance)
        print('test_3 ends')

    def test_4(self):
        print('test_4 starts')
        user_output = self.P2.scale(-2, -2)
        exp_output = (np.array([5.0, 1.0, -1.0, 3.0, 7.0]),
                      np.array([3.4, 3.4, 1.4, -0.6, 1.4]))
        print(f'returned value {user_output}')
        print(f'expected value {exp_output}')
        np.testing.assert_allclose(exp_output, user_output, rtol=rtolerance, atol=atolerance)
        print('test_4 ends')

    def test_5(self):
        print('test_5 starts')
        user_output = self.P2.rotate(-45)
        exp_output = (np.array([1.13137085, -1.69705627, -1.69705627,  2.54558441,  3.95979797]),
                      np.array([5.93969696, 3.11126984, 0.28284271, 1.69705627, 5.93969696]))
        print(f'returned value {user_output}')
        print(f'expected value {exp_output}')
        np.testing.assert_allclose(exp_output, user_output, rtol=rtolerance, atol=atolerance)
        print('test_5 ends')

    def test_6(self):
        print('test_6 starts')
        user_output = self.P2.scale(0.5, 0.3)
        exp_output = (np.array([0.98994949, -0.42426407, -0.42426407,  1.69705627,  2.40416306]),
                      np.array([4.15778787, 3.30925974, 2.4607316, 2.88499567, 4.15778787]))
        print(f'returned value {user_output}')
        print(f'expected value {exp_output}')
        np.testing.assert_allclose(exp_output, user_output, rtol=rtolerance, atol=atolerance)
        print('test_6 ends')

    def test_7(self):
        print('test_7 starts')
        user_output = self.P3.rotate(45, 2, 2)
        exp_output = (np.array([-0.82842712,  2.0,  4.82842712,  2.0]),
                      np.array([2.0, -0.82842712,  2.0,  4.82842712]))
        print(f'returned value {user_output}')
        print(f'expected value {exp_output}')
        np.testing.assert_allclose(exp_output, user_output, rtol=rtolerance, atol=atolerance)
        print('test_7 ends')

class TestCircle(unittest.TestCase):

    C1 = my.Circle(2.0, 2.0, 3.0)  # 2,2 is center and 3 is radius
    C2 = my.Circle(2.0, 2.0, 3.0)  # 2,2 is center and 3 is radius

    def test_1(self):
        print('test_1 starts')
        user_output = self.C1.rotate(45)
        exp_output = (2.8284271247461903, 0.0, 3.0)
        print(f'returned value {user_output}')
        print(f'expected value {exp_output}')
        np.testing.assert_allclose(exp_output, user_output, rtol=rtolerance, atol=atolerance)
        print('test_1 ends')

    def test_2(self):
        print('test_2 starts')
        user_output = self.C1.scale(0.5)
        exp_output = (2.8284271247461903, 0.0, 1.5)
        print(f'returned value {user_output}')
        print(f'expected value {exp_output}')
        np.testing.assert_allclose(exp_output, user_output, rtol=rtolerance, atol=atolerance)
        print('test_2 ends')

    def test_3(self):
        print('test_3 starts')
        user_output = self.C1.translate(-3, 3)
        exp_output = (-0.1715728752538097, 3.0, 1.5)
        print(f'returned value {user_output}')
        print(f'expected value {exp_output}')
        np.testing.assert_allclose(exp_output, user_output, rtol=rtolerance, atol=atolerance)
        print('test_3 ends')

    def test_4(self):
        print('test_4 starts')
        user_output = self.C2.rotate(45, 4, 4)
        exp_output = (1.1715728752538097, 4.0, 3.0)
        print(f'returned value {user_output}')
        print(f'expected value {exp_output}')
        np.testing.assert_allclose(exp_output, user_output, rtol=rtolerance, atol=atolerance)
        print('test_4 ends')

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

reason: import numpy


text:
#fetch image
import numpy as np
import matplotlib.pyplot as plt
import keras
import cv2
import requests
from sklearn import preprocessing
from PIL import Image
url = 'https://c8.alamy.com/comp/J2MRAJ/german-road-sign-bicycles-crossing-J2MRAJ.jpg'
r = requests.get(url, stream=True)
img = Image.open(r.raw)
plt.imshow(img, cmap=plt.get_cmap('gray'))

#Preprocess image

img = np.asarray(img)
img = cv2.resize(img, (32, 32))
img = preprocessing(img)
plt.imshow(img, cmap = plt.get_cmap('gray'))
print(img.shape)

#Reshape reshape

img = img.reshape(1, 32, 32, 1)

#Test image
print("predicted sign: "+ str(model.predict_classes(img)))

max_stars_repo_name: kartikeyshaurya/SDV

max_issues_repo_name: kartikeyshaurya/SDV

max_forks_repo_name: kartikeyshaurya/SDV


text:
import argparse

import pandas as pd
import jieba
import numpy as np
import sys

#acc: 0.4904042466312781, f1: 0.4884483462675765
if __name__ == '__main__':
    # if len(sys.argv) != 3:
    #     raise Exception("run this script: "
    #                     "python eval.py $golds_file_path $predict_file_path")
    # input_file = sys.argv[1]
    # output_file = sys.argv[2]

    parser = argparse.ArgumentParser()
    parser.add_argument(
        '-i', '--input_file', default='../data/train.csv',
        help='model config file')
    parser.add_argument('-o',
        '--output_file', default='submission.csv',
        help='used for distributed parallel')
    args = parser.parse_args()

    df1 = pd.read_csv(args.input_file)
    df2 = pd.DataFrame(columns=['id','answer'])

    for i in range(len(df1)):
        sc = df1.loc[i,'sc']
        sc_set = set(jieba.lcut(sc))
        candidate = []
        candidate.append(set(jieba.lcut(df1.loc[i,'A'])))
        candidate.append(set(jieba.lcut(df1.loc[i,'B'])))
        candidate.append(set(jieba.lcut(df1.loc[i,'C'])))
        candidate.append(set(jieba.lcut(df1.loc[i,'D'])))
        candidate.append(set(jieba.lcut(df1.loc[i,'E'])))
        score = []
        for j in range(5):
            score.append(len(candidate[j] & sc_set))
        # print(np.argmax(score))
        df2.loc[i,'id'] = df1.loc[i,'id']
        df2.loc[i,'answer'] = np.argmax(score) + 1

    df2['id'] = df2['id'].astype('int')
    df2['answer'] = df2['answer'].astype('int')
    df2.to_csv(args.output_file,encoding='utf-8',index=False)

max_stars_repo_name: ShenDezhou/CAIL

max_issues_repo_name: ShenDezhou/CAIL

max_forks_repo_name: ShenDezhou/CAIL


text:
import os
import numpy as np
import glob
import sys
sys.path.append('/home/arfeen/ZSDG_main/src/')
from utils.utils import process_txt

BASE_PATH = '/home/arfeen/ZSDG_main/src/data/DomainNet'

def create_trvalte_splits(args):

	semantic_vec = np.load(os.path.join(BASE_PATH, 'w2v_domainnet.npy'), allow_pickle=True, encoding='latin1').item()

	all_domains = []
	for f in os.listdir(os.path.join(args.root_path, 'DomainNet')):
		if os.path.isdir(os.path.join(args.root_path, 'DomainNet', f)):
			all_domains.append(f)

	unseen_domain = args.holdout_domain
	seen_domains = np.setdiff1d(all_domains, [unseen_domain]).tolist()
	import pdb;pdb.set_trace()
	print('\nSeen:{}; Unseen:{}.'.format(seen_domains, unseen_domain))

	fls_train = []
	fls_va = []
	fls_train_cls = []
	fls_va_cls = []
	for dom in seen_domains:

		dom_tr_files = process_txt(os.path.join(BASE_PATH, 'dg_splits', dom + '_train.txt'), os.path.join(args.root_path, 'DomainNet'))
		dom_va_files = process_txt(os.path.join(BASE_PATH, 'dg_splits', dom + '_val.txt'), os.path.join(args.root_path, 'DomainNet'))

		fls_train += dom_tr_files['img_paths'].tolist()
		fls_train_cls += dom_tr_files['labels'].tolist()

		fls_va += dom_va_files['img_paths'].tolist()
		fls_va_cls += dom_va_files['labels'].tolist()

	unseen_domain_te_files = process_txt(os.path.join(BASE_PATH, 'dg_splits', unseen_domain + '_test.txt'), os.path.join(args.root_path, 'DomainNet'))
	fls_te = unseen_domain_te_files['img_paths'].tolist()
	fls_te_cls = unseen_domain_te_files['labels']

	splits = {}
	cls = {}
	splits['tr'] = fls_train
	splits['va'] = fls_va
	splits['te'] = fls_te
	cls['tr'] = fls_train_cls
	cls['te'] = fls_te_cls
	cls['va'] = fls_va_cls

	return semantic_vec, splits, cls


text:
import cirq
import numpy as np
from cirq import Simulator
from cirq.circuits import InsertStrategy
from cirq.ops import CZ, H

print(cirq.google.Foxtail)
length = 3
qubits = [cirq.GridQubit(i, j) for i in range(length) for j in range(length)]
print(qubits)

# define a circuit
circuit = cirq.Circuit()
circuit.append(cirq.H(q) for q in qubits if (q.row + q.col) % 2 == 0)
print(circuit)

circuit.append(cirq.X(q) for q in qubits if (q.row + q.col) % 2 == 1)
print(circuit)

# redefing the circuit with an InsertStrategy
circuit = cirq.Circuit()
circuit.append(
    [cirq.H(q) for q in qubits if (q.row + q.col) % 2 == 0],
    strategy=cirq.InsertStrategy.EARLIEST,
)
circuit.append(
    [cirq.X(q) for q in qubits if (q.row + q.col) % 2 == 1],
    strategy=cirq.InsertStrategy.EARLIEST,
)
print(circuit)

# look at the circuit moments
for i, m in enumerate(circuit):
    print("Moment {}: {}".format(i, m))

# creating a grid of qubits
qubits = [cirq.GridQubit(x, y) for x in range(3) for y in range(3)]
print(qubits)

# applying a gate at Qubit location (0,0)
x_gate = cirq.X

# turn in into an operation
x_op = x_gate(qubits[0])
print(x_op)

# define a Moment
cz = cirq.CZ(qubits[0], qubits[1])
x = cirq.X(qubits[2])
moment = cirq.Moment([x, cz])
print(moment)

# define a Circuit by combining Moments togeteher
cz01 = cirq.CZ(qubits[0], qubits[1])
x2 = cirq.X(qubits[2])
cz12 = cirq.CZ(qubits[1], qubits[2])
moment0 = cirq.Moment([cz01, x2])
moment1 = cirq.Moment([cz12])
circuit = cirq.Circuit((moment0, moment1))
print(circuit)

q0, q1, q2 = [cirq.GridQubit(i, 0) for i in range(3)]

circuit = cirq.Circuit()
circuit.append([CZ(q0, q1)])
circuit.append([H(q0), H(q2)], strategy=InsertStrategy.EARLIEST)
print(circuit)

circuit = cirq.Circuit()
circuit.append([H(q0), H(q1), H(q2)], strategy=InsertStrategy.NEW)
print(circuit)
circuit = cirq.Circuit()
circuit.append([CZ(q1, q2)])
circuit.append([CZ(q1, q2)])
circuit.append([H(q0), H(q1), H(q2)], strategy=InsertStrategy.INLINE)
print(circuit)

circuit = cirq.Circuit()
circuit.append([H(q0)])
circuit.append([CZ(q1, q2), H(q0)], strategy=InsertStrategy.NEW_THEN_INLINE)
print(circuit)

q0 = cirq.GridQubit(0, 0)
q1 = cirq.GridQubit(0, 1)

# create a basic circuit constructor
def basic_circuit(meas=True):
    sqrt_x = cirq.X ** (0.5)
    yield sqrt_x(q0), sqrt_x(q1)
    yield cirq.CZ(q0, q1)
    yield sqrt_x(q0), sqrt_x(q1)
    if meas:
        yield cirq.measure(q0, key="alpha"), cirq.measure(q1, key="beta")

circuit = cirq.Circuit()
circuit.append(basic_circuit())
print(circuit)

simulator = Simulator()
result = simulator.run(circuit)
print(result)

def generate_2x2_grid():
    a, b, c, d = [
        cirq.GridQubit(0, 0),
        cirq.GridQubit(0, 1),
        cirq.GridQubit(1, 1),
        cirq.GridQubit(1, 0),
    ]
    circuit = cirq.Circuit.from_ops(
        cirq.H(a),
        cz_swap(a, b, 0.5),
        cz_swap(b, c, 0.25),
        cz_swap(c, d, 0.125),
        cirq.H(a),
        cz_swap(a, b, 0.5),
        cz_swap(b, c, 0.25),
        cirq.H(a),
        cz_swap(a, b, 0.5),
        cirq.H(a),
        strategy=cirq.InsertStrategy.EARLIEST,
    )
    return circuit

def main():
    qft_circuit = generate_2x2_grid()
    print("Circuit:")
    print(qft_circuit)

    # creating a simulator
    simulator = cirq.Simulator()

    # pass in the circuit and print the result
    result = simulator.simulate(qft_circuit)
    print()
    print("Final State:")
    print(np.around(result.final_state, 3))

def cz_swap(q0, q1, rot):
    yield cirq.CZ(q0, q1) ** rot
    yield cirq.SWAP(q0, q1)


text:
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from fileinput import filename

from numpy.core.fromnumeric import size

import _init_paths

import logging
import os
import os.path as osp
from opts import opts
from tracking_utils.utils import mkdir_if_missing
from tracking_utils.log import logger
import datasets.dataset.jde as datasets
from track import eval_seq

# multiple thread
import threading
from multiprocessing import Process

import copy

logger.setLevel(logging.INFO)

os.environ['KMP_DUPLICATE_LIB_OK'] = 'True'

def demo(opt,deviceid):
    os.environ['CUDA_VISIBLE_DEVICES'] = str(deviceid)
    result_root = opt.output_root if opt.output_root != '' else '.'
    mkdir_if_missing(result_root)

    logger.info('Starting tracking...')
    dataloader = datasets.LoadVideo(opt.input_video, opt.img_size)
    print ('video' ,opt.input_video)
    result_filename = os.path.join(result_root, 'results.txt')
    frame_rate = dataloader.frame_rate

    # Save output frames to folder, for generating video with ffmpeg
    frame_dir = None if opt.output_format == 'text' else osp.join(result_root, 'frame')
    eval_seq(opt, dataloader, 'mot', result_filename,
             save_dir=frame_dir, show_image=True, frame_rate=frame_rate,
             use_cuda=opt.gpus!=[-1])

    # Convert output frames into video file, e.g. mp4
    if opt.output_format == 'video':
        output_video_path = osp.join(result_root, 'result.mp4')
        cmd_str = 'ffmpeg -f image2 -i {}/%05d.jpg -b 5000k -c:v mpeg4 {}'.format(osp.join(result_root, 'frame'), output_video_path)
        os.system(cmd_str)

def file_name(file_dir):
    videofiles=[]
    for root, dirs, files in os.walk(file_dir):
        print('root',root) #当前目录路径
        print('dirs',dirs) #当前路径下所有子目录
        if size(files)<3 and size(files)>0:
            firts=root+'/'+files[0]
            print('files',firts) #当前路径下所有非目录子文件
            videofiles.append(firts)
    return videofiles

if __name__ == '__main__':
    os.environ['CUDA_VISIBLE_DEVICES'] = '1'
    path='../All_Data'
    videoPaths = file_name(path)
    print('files',videoPaths)
    opt = opts().init()
    process=[]
    i=0
    for inputvideopath in videoPaths:
        print('first thread',inputvideopath)
        newopt=copy.deepcopy(opt)
        newopt.input_video=inputvideopath
        #opt.task,'--load_model' '../models/fairmot_lite.pth','--input_video',opt.input_video
        p=Process(target=demo, args=(newopt,(i+1)/2))
        process.append(p)
        i=i+1

    for t in process:
        t.start()
    for t in process:
        t.join()

max_stars_repo_name: YRstronger/FairMOT

max_issues_repo_name: YRstronger/FairMOT

max_forks_repo_name: YRstronger/FairMOT

reason: from numpy


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

# Modified for experiments in https://arxiv.org/abs/2007.00145

"""Class for text data."""
import string
import numpy as np
import torch
import copy
from pytorch_pretrained_bert import BertTokenizer, BertModel
from torch.nn.utils.rnn import pad_sequence
from .TransformerModel import EncoderLayer, Encoder, \
    MultiHeadedAttention, PositionwiseFeedForward, PositionalEncoding

class SimpleVocab(object):

    def __init__(self):
        super(SimpleVocab, self).__init__()
        self.word2id = {}
        self.wordcount = {}
        self.word2id['<UNK>'] = 0
        self.wordcount['<UNK>'] = 9e9

    def tokenize_text(self, text):
        text = text.encode('ascii', 'ignore').decode('ascii')
        text = str(text).lower()
        try:
            tokens = text.translate(
                str.maketrans('', '', string.punctuation)).strip().split()
        except AttributeError:
            tokens = text.translate(None, string.punctuation).strip().split()
        return tokens

    def add_text_to_vocab(self, text):
        tokens = self.tokenize_text(text)
        for token in tokens:
            if token not in self.word2id:
                self.word2id[token] = len(self.word2id)
                self.wordcount[token] = 0
            self.wordcount[token] += 1

    def threshold_rare_words(self, wordcount_threshold=5):
        for w in self.word2id:
            if self.wordcount[w] < wordcount_threshold:
                self.word2id[w] = 0

    def encode_text(self, text):
        tokens = self.tokenize_text(text)
        x = [self.word2id.get(t, 0) for t in tokens]
        return x

    def get_size(self):
        return len(self.word2id)

class EmbeddingModel(torch.nn.Module):

    def __init__(self,
                 texts_to_build_vocab,
                 word_embed_dim=512):

        super(EmbeddingModel, self).__init__()

        self.vocab = SimpleVocab()
        for text in texts_to_build_vocab:
            self.vocab.add_text_to_vocab(text)
        vocab_size = self.vocab.get_size()

        self.word_embed_dim = word_embed_dim
        self.embedding_layer = torch.nn.Embedding(vocab_size,
                                                  word_embed_dim)

    def forward(self, input_list):
        """ input x: list of strings"""
        if type(input_list) is list:
            if type(input_list[0]) is str:
                input_list = [self.vocab.encode_text(text)
                              for text in input_list]

        assert type(input_list) is list
        assert type(input_list[0]) is list
        assert type(input_list[0][0]) is int
        return self.forward_encoded_texts(input_list)

    def forward_encoded_texts(self, texts):
        # to tensor
        lengths = [len(tt) for tt in texts]
        itexts = torch.zeros((np.max(lengths), len(texts))).long()
        for ii in range(len(texts)):
            itexts[:lengths[ii], ii] = torch.tensor(texts[ii])

        # embed words
        itexts = torch.autograd.Variable(itexts).cuda()
        etexts = self.embedding_layer(itexts)

        text_mask = torch.zeros((len(texts), np.max(lengths))).long()
        for ii in range(len(texts)):
            text_mask[ii, :lengths[ii]] = 1

        return etexts.transpose(0, 1), text_mask.cuda()

    def prepend_vocab(self, texts_to_build_vocab):
        """Stores current vocab in self.temp_vocab for future consolidation.
        Current embeddings are lost.
        After calling this method,
        the object is suitable for loading embedding weights,
        after which consolidate_vocab should be called."""
        self.temp_vocab = self.vocab

        self.vocab = SimpleVocab()
        for text in texts_to_build_vocab:
            self.vocab.add_text_to_vocab(text)
        vocab_size = self.vocab.get_size()
        self.embedding_layer = torch.nn.Embedding(
            vocab_size, self.embedding_layer.weight.shape[-1])

    def consolidate_vocab(self):
        old_vocab_size = self.vocab.get_size()
        for text in self.temp_vocab.word2id.keys():
            self.vocab.add_text_to_vocab(text)
        vocab_increase = self.vocab.get_size() - old_vocab_size
        new_embeddings = torch.nn.Embedding(
            vocab_increase, self.embedding_layer.weight.shape[-1])
        all_embeddings_matrix = torch.cat(
            [self.embedding_layer.weight, new_embeddings.weight], dim=0)
        self.embedding_layer = torch.nn.Embedding.from_pretrained(
            all_embeddings_matrix, freeze=False)
        self.temp_vocab = None

class SimpleTransformerEncoderModel(EmbeddingModel):

    def __init__(self,
                 texts_to_build_vocab,
                 word_embed_dim=512,
                 d_model=512,
                 d_ff=512,
                 num_heads=1,
                 num_layers=1,
                 dropout=0.1,
                 max_length=500):

        super().__init__(texts_to_build_vocab, word_embed_dim)

        c = copy.deepcopy
        attn = MultiHeadedAttention(num_heads, d_model)
        ff = PositionwiseFeedForward(d_model, d_ff, dropout)

        self.position_encoder = PositionalEncoding(
            d_model, dropout, max_len=max_length)

        self.model = Encoder(
            EncoderLayer(d_model, c(attn), c(ff), dropout),
            num_layers)

        for p in self.model.parameters():
            if p.dim() > 1:
                torch.nn.init.xavier_uniform_(p)

    def forward_encoded_texts(self, texts):
        # to tensor
        lengths = [len(tt) for tt in texts]
        itexts = torch.zeros((np.max(lengths), len(texts))).long()
        for ii in range(len(texts)):
            itexts[:lengths[ii], ii] = torch.tensor(texts[ii])

        # embed words
        itexts = torch.autograd.Variable(itexts).cuda()
        emb_texts = self.embedding_layer(itexts).transpose(0, 1)

        text_mask = torch.zeros((len(texts), np.max(lengths))).long()
        for ii in range(len(texts)):
            text_mask[ii, :lengths[ii]] = 1
        text_mask = text_mask.cuda()

        emb_texts = self.position_encoder(emb_texts)

        output = self.model(emb_texts, text_mask)

        return output, text_mask

class TextLSTMModel(EmbeddingModel):

    def __init__(self,
                 texts_to_build_vocab,
                 word_embed_dim=512,
                 lstm_hidden_dim=512,
                 num_layers=1,
                 dropout=0.1,
                 text_model_sequence_output=False):

        super(TextLSTMModel, self).__init__(texts_to_build_vocab,
                                            word_embed_dim)

        self.lstm_hidden_dim = lstm_hidden_dim
        self.lstm = torch.nn.LSTM(word_embed_dim, lstm_hidden_dim,
                                  num_layers=num_layers)
        self.num_layers = num_layers
        self.fc_output = torch.nn.Sequential(
            torch.nn.Dropout(p=dropout),
            torch.nn.Linear(lstm_hidden_dim, lstm_hidden_dim),
        )
        self.text_model_sequence_output = text_model_sequence_output

    def forward_encoded_texts(self, texts):
        # to tensor
        lengths = [len(tt) for tt in texts]
        itexts = torch.zeros((np.max(lengths), len(texts))).long()
        for ii in range(len(texts)):
            itexts[:lengths[ii], ii] = torch.tensor(texts[ii])

        # embed words
        itexts = torch.autograd.Variable(itexts).cuda()
        etexts = self.embedding_layer(itexts)

        # lstm
        lstm_output, _ = self.forward_lstm_(etexts)

        if self.text_model_sequence_output:
            # generate mask
            text_mask = torch.zeros((len(texts), np.max(lengths))).long()
            for ii in range(len(texts)):
                text_mask[ii, :lengths[ii]] = 1
            output = self.fc_output(torch.transpose(lstm_output, 0, 1))
            return output, text_mask.long().cuda()
        else:
            # get last output (using length)
            text_features = []
            for ii in range(len(texts)):
                text_features.append(lstm_output[lengths[ii] - 1, ii, :])

            # output
            text_features = torch.stack(text_features)
            text_features = self.fc_output(text_features)

            return text_features

    def forward_lstm_(self, etexts):
        batch_size = etexts.shape[1]
        first_hidden = (torch.zeros(self.num_layers, batch_size,
                                    self.lstm_hidden_dim),
                        torch.zeros(self.num_layers, batch_size,
                                    self.lstm_hidden_dim))
        first_hidden = (first_hidden[0].cuda(), first_hidden[1].cuda())
        lstm_output, last_hidden = self.lstm(etexts, first_hidden)
        return lstm_output, last_hidden

class BERTModel(torch.nn.Module):
    def __init__(self, word_embed_dim=512):

        super(BERTModel, self).__init__()

        self.model = BertModel.from_pretrained('bert-base-uncased')
        self.tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
        self.model.eval()
        # import logging
        # logging.basicConfig(level=logging.INFO)
        # self.fc_output = torch.nn.Sequential(
        #    torch.nn.Dropout(p=0.1),
        #    torch.nn.Linear(lstm_hidden_dim, lstm_hidden_dim),
        # )

    def forward(self, x):
        """ input x: batch sentence strings"""
        sentences = x
        tokenized_texts = [self.tokenizer.tokenize(sent) for sent in sentences]
        indexed_tokens = [torch.tensor(
                            self.tokenizer.convert_tokens_to_ids(tok))
                          for tok in tokenized_texts]
        tokens_tensor = pad_sequence(indexed_tokens, batch_first=True)
        tokens_tensor = tokens_tensor.to('cuda')
        attention_masks = []
        for seq in tokens_tensor:
            seq_mask = [float(ii > 0) for ii in seq]
            attention_masks.append(seq_mask)
        attention_masks = torch.tensor(attention_masks)
        attention_masks = attention_masks.to('cuda')

        with torch.no_grad():
            bert_features, _ = self.model(tokens_tensor, token_type_ids=None,
                                          attention_mask=attention_masks,
                                          output_all_encoded_layers=False)
        # text_features = self.fc_output(bert_features)
        return bert_features, attention_masks


text:
# -*- coding: utf-8 -*-
"""
Created on Tue Oct 20 23:14:25 2020

Descriptor analysis for all equilibrium configurations
"""
import os
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
from matplotlib.pyplot import cm
import matplotlib

font = {'size'   : 18}
matplotlib.rc('font', **font)
matplotlib.rcParams['axes.linewidth'] = 1
matplotlib.rcParams['xtick.major.size'] = 6
matplotlib.rcParams['xtick.major.width'] = 1
matplotlib.rcParams['ytick.major.size'] = 6
dpi = 300.
matplotlib.rcParams['figure.dpi'] = dpi

def set_axis_style(ax, labels):
    ax.get_xaxis().set_tick_params(direction='out')
    ax.xaxis.set_ticks_position('bottom')
    ax.set_xticks(np.arange(1, len(labels) + 1))
    ax.set_xticklabels(labels)
    ax.set_xlim(0.25, len(labels) + 0.75)

def set_color(violin_parts, c):
    for vi in violin_parts['bodies']:
        vi.set_color(c)
    violin_parts['cmeans'].set_color(c)
    violin_parts['cmaxes'].set_color(c)
    violin_parts['cmins'].set_color(c)
    violin_parts['cbars'].set_color(c)

# read the data of stable configurations
filename_stable = os.path.join(os.getcwd(),  'pd_all_config_all_conditions.csv')
df_all = pd.read_csv(filename_stable, index_col=False)

# Preprocess the data
z_support = 14
# fill the nan value in the z position column
df_all = df_all.fillna(value = z_support)
# rescale the z position back 0
df_all['Z_pos'] = df_all['Z_pos'] - z_support

# Put PCO into log scale
df_all['PCO'] = np.log10(df_all['PCO'])

# take absolute value for mu
df_all['mu_mean'] = np.abs(df_all['mu_mean'])

#%%
from sklearn.decomposition import PCA
from sklearn.preprocessing import StandardScaler

descriptors= ['n', 'T', 'PCO', 'mu_mean','nCO_mean', 'GCN', 'CN1', 'CN2',  'total_top', 'total_bridge', 'total_hollow', 'total_sites', 'occ_top', 'occ_bridge','occ_hollow', 'Z_pos']
descriptors_rf = ['n', 'GCN', 'CN1', 'CN2',  'total_top', 'total_bridge', 'total_hollow', 'total_sites', 'occ_top', 'occ_bridge','occ_hollow', 'Z_pos']

# descriptors used in plot labels
descriptors_plot = ['n', 'T', r'$\rm P_{CO}$', r'$\rm \overline{|G|}$', r'$\rm \overline{m}_{CO}$',
                    r'$\rm \overline{GCN}$', r'$\rm \overline{CN1}$', r'$\rm \overline{CN2}$', r'$\rm N_{top}$', r'$\rm N_{bridge}$', r'$\rm N_{hollow}$',
                    r'$\rm N_{sites}$', r'$\rm \overline{m}_{CO-top}$',  r'$\rm \overline{m}_{CO-bridge}$', r'$\rm \overline{m}_{CO-hollow}$',
                    r'$\rm \overline{Z}_{CO}$']

descriptors_rf_plot = ['n', r'$\rm \overline{GCN}$', r'$\rm \overline{CN1}$', r'$\rm \overline{CN2}$', r'$\rm N_{top}$', r'$\rm N_{bridge}$', r'$\rm N_{hollow}$',
                    r'$\rm N_{sites}$', r'$\rm \overline{m}_{CO-top}$',  r'$\rm \overline{m}_{CO-bridge}$', r'$\rm \overline{m}_{CO-hollow}$',
                    r'$\rm \overline{Z}_{CO}$']

nDescriptors = len(descriptors)

descriptors_rf_indices = [0] + list(range(5, nDescriptors))
X =  np.array(df_all.loc[:,descriptors], dtype = float)
X_rf =  np.array(df_all.loc[:,descriptors_rf], dtype = float)

y_mu = np.abs(df_all['mu_mean'])
y_mco = np.abs(df_all['nCO_mean'])

pc_reg = 3

#%% PCA
# Normalize the data
#X_std = StandardScaler().fit_transform(X)
scaler = StandardScaler().fit(X)
X_std = scaler.transform(X)

# Covriance matrix of original data
cov_mat = np.cov(X_std.T)

# PCA use sklearn
pca = PCA().fit(X_std)
Xpc = pca.transform(X_std)
# Covriance matrix from PCA
cov_pc = np.cov(Xpc.T)

# Plot Covariance structure
fig, ax1 = plt.subplots(figsize=(12,10))
ax1.set_xticks(range(0,nDescriptors))
ax1.set_xticklabels(descriptors_plot, rotation = 70)
ax1.set_yticks(range(0,nDescriptors))
ax1.set_yticklabels(descriptors_plot)
im1 = ax1.imshow(cov_mat, vmin = -1, vmax = 1)
fig.colorbar(im1, ax = ax1, shrink = 0.5)

#eig_vals, eig_vecs = np.linalg.eig(cov_mat)
eig_vals = pca.explained_variance_ #eigenvalues
eig_vecs = pca.components_  # eigenvector
#tot = sum(eig_vals)
#var_exp = [(i / tot)*100 for i in sorted(eig_vals, reverse=True)]
var_exp = pca.explained_variance_ratio_ #explained variance ratio
cum_var_exp = np.cumsum(var_exp) #cumulative variance ratio
#print('Eigenvectors \n%s' %eig_vecs)
#print('\nEigenvalues \n%s' %eig_vals)

'''
Scree plot for PCs
'''

plt.figure(figsize=(10, 10))

plt.bar(range(nDescriptors), var_exp, alpha=0.5, align='center',
        label='Individual Explained Variance')
plt.step(range(nDescriptors), cum_var_exp, where='mid',
         label='Cumulative Explained Variance')
plt.ylabel('Explained Variance Ratio')
plt.xlabel('Principal Components')
plt.xticks(np.arange(nDescriptors),
            ['PC%i'%(w+1) for w in range(nDescriptors)],  rotation=60)
plt.legend(loc = 'right'	)
plt.tight_layout()

#%% Plot the normalized desciptor loading
'''
Need to be completed
'''
ind = 0
yvals = []
ylabels = []
bar_vals = []
space = 0.2

cm_cur =  cm.jet(np.linspace(0,1,nDescriptors))
fig = plt.figure(figsize=(6,6))

ax = fig.add_subplot(111)
n = len(descriptors)
width = (1 - space) / (len(descriptors))
indeces = np.arange(0, pc_reg) + 0.5

PC_loadings = []
# Create a set of bars at each position
for i, pci in enumerate(eig_vecs[:pc_reg]):

    vals = pci/np.sum(np.absolute(pci))
    PC_loadings.append(vals)
    pos = width*np.arange(n) + i
    ax.bar(pos, vals, width=width, label=str(i+1), color = cm_cur, alpha = 1)

linex = np.arange(np.arange(0, pc_reg).min() -0.1  , np.arange(0, pc_reg).max()+ 1)

ax.set_xticks(indeces)
ax.set_xticklabels(list(np.arange(0, pc_reg)+1))
ax.set_ylabel("Normalized Descriptoor Loading")
ax.set_xlabel("Principal Component (PC) #")

# Add legend using color patches
patches = []
for c in range(n):
    patches.append(mpatches.Patch(color=cm_cur[c]))
plt.legend(patches, descriptors,
           bbox_to_anchor = (1.02, 1),loc= 'upper left', frameon=False)

plt.plot(linex, linex*0, c = 'k', lw = 1.5)
plt.show()

#%% PC1, PC2 2D PC loading plot
plt.figure(figsize=(6, 6))
for i in range(n):
    xl = PC_loadings[0][i]
    yl = PC_loadings[1][i]
    plt.scatter(xl, yl,  c = cm_cur[i], s = 120)
    plt.plot(np.array([0, xl]), np.array([0, yl]), '--', c = cm_cur[i], label = descriptors[i])
plt.xlabel('PC1 Loading')
plt.ylabel('PC2 Loading')

# Add legend using color patches
patches = []
for c in range(n):
    patches.append(mpatches.Patch(color=cm_cur[c]))
plt.legend(patches, descriptors,
           bbox_to_anchor = (1.02, 1),loc= 'upper left', frameon=False)
plt.show()

#%%
# Helper fuction for loading plots
def plot_loadings(loadings, axes=None, pc=(0,1), varlabels=[],
                  scale=False, **kwargs):
    xl = loadings[:,pc[0]]
    yl = loadings[:,pc[1]]
    if scale:
        xl = (xl-xl.mean())/(xl.max() - xl.min())
        yl = (yl-yl.mean())/(yl.max() - yl.min())

    if axes:
        fig = None
        ax = axes
    else:
        fig = plt.figure(**kwargs)
        ax = fig.add_subplot(111)

    for i, (x, y) in enumerate(zip(xl,yl)):
        ax.plot([0, x],[0, y],  '--', c = cm_cur[i])
        ax.scatter(x, y,  c = cm_cur[i], s = 100)

    # if list(varlabels):
    #     for i, txt in enumerate(varlabels):
    #         ax.annotate(txt, xy=(xl[i]*0.95, yl[i]*1.1), fontsize = 16)

    xlim = abs(max(xl.max(),xl.min(),key=abs))*1.1
    ylim = abs(max(yl.max(),yl.min(),key=abs))*1.1
    ax.set_xlim(-0.5,0.5)
    ax.set_ylim(-0.5,0.5)

    ax.set_xlabel("Principle Component {}".format(pc[0]+1))
    ax.set_ylabel("Principle Component {}".format(pc[1]+1))

    return fig, ax

pca = PCA()
scores = pca.fit_transform(X_std)
loadings = np.transpose(pca.components_)
plot_loadings(
    loadings=loadings,
    pc=(0,1),
    figsize=(8,8),
    varlabels=descriptors_plot
)

plot_loadings(
    loadings=loadings,
    pc=(0,2),
    figsize=(8,8),
    varlabels=descriptors_plot
)

#%%
# random forest for feature importance on a regression problem
from sklearn.datasets import make_regression
from sklearn.ensemble import RandomForestRegressor
import time
from sklearn.model_selection import RepeatedKFold, cross_validate, LeaveOneOut, train_test_split
from sklearn.metrics import r2_score, mean_squared_error
import warnings
from sklearn.exceptions import UndefinedMetricWarning

# Turn off warnings for calculating r2 of a single point
warnings.filterwarnings("ignore", category=UndefinedMetricWarning)

def plot_importances(estimator, descriptors):
    '''
    Plot feature importance reported by random forest
    '''
    fig,ax = plt.subplots(figsize=(8,8))
    n_descriptor = len(descriptors)
    importance =  estimator.feature_importances_
    importance_sorted = np.sort(importance)
    descirptors_sorted = [x for _,x in sorted(zip(importance,descriptors))]

    ax.barh(np.arange(n_descriptor),importance_sorted, color= cm_cur[descriptors_rf_indices])
    ax.set_yticks(np.arange(n_descriptor))
    ax.set_yticklabels(descirptors_sorted, rotation=0)
    #ax.set_ylabel('Descriptor')
    ax.set_xlabel('Importance')
    plt.show()

def cross_validation(X, y, estimator):
    '''
    Cross-validation
    '''
    #rkf = RepeatedKFold(n_splits = 10, n_repeats = 3) #change this to leave one out
    loo = LeaveOneOut()
    scores  = cross_validate(estimator, X, y, cv = loo,
                                scoring=('neg_mean_squared_error', 'r2'),
                                return_train_score=True)
    # RMSE for repeated 10 fold test data
    test_RMSE = np.sqrt(np.abs(scores['test_neg_mean_squared_error']))
    test_RMSE_mean = np.mean(test_RMSE)
    test_RMSE_std = np.std(test_RMSE)

    train_r2 = scores['train_r2']
    train_r2_mean =  np.mean(train_r2)
    train_r2_std = np.std(train_r2)

    return [test_RMSE_mean, test_RMSE_std, train_r2_mean, train_r2_std]

X_train_mu, X_test_mu, y_train_mu, y_test_mu = train_test_split(X_rf, y_mu, test_size=0.1, random_state=0)
X_train_mco, X_test_mco, y_train_mco, y_test_mco = train_test_split(X_rf, y_mco, test_size=0.1, random_state=0)

# Train the random forest model using 2D Grid search
train_flag = False

if train_flag:
    # grid search for two hyperparameters
    n_estimators_grid = range(1,1001, 500)
    max_depth_grid = range(1,51, 25)
    test_RMSE_m = np.zeros((len(n_estimators_grid), len(max_depth_grid)))
    progress = 0

    start_time = time.time()
    for i, n_estimators_i in enumerate(n_estimators_grid):
        for j, max_depth_i in enumerate(max_depth_grid):
            progress += 1
            per = progress/len(n_estimators_grid)/len(max_depth_grid) *100
            print('Training {0:.5f} % Done!'.format(per))

            rf = RandomForestRegressor(n_estimators = n_estimators_i,
                                         max_depth = max_depth_i,
                                         random_state=0)

            [test_RMSE_mean, test_RMSE_std, train_r2_mean, train_r2_std] = cross_validation(X_train_mu, y_train_mu, rf)
            test_RMSE_m[i, j] = test_RMSE_mean

    end_time = time.time()

    print("Tuning hyperparameter takes {0:.2f} minutes".format((end_time-start_time)/60.0))

    # Final the optimal model
    test_RMSE_m = np.around(test_RMSE_m, decimals = 3)
    opt_ind = np.unravel_index(np.argmin(test_RMSE_m, axis=None), test_RMSE_m.shape)
    n_estimators_opt = n_estimators_grid[opt_ind[0]]
    max_depth_opt = max_depth_grid[opt_ind[1]]

else:
    n_estimators_opt = 500
    max_depth_opt = 20

print('The optimal number of estimator is: {}'.format(n_estimators_opt))
print('The optimal number of max depth is: {}'.format(max_depth_opt))

#%% The final optimal model for mu
rf_opt_mu = RandomForestRegressor(n_estimators = n_estimators_opt,
                                     max_depth = max_depth_opt,
                                     random_state=0)
rf_opt_mu.fit(X_train_mu, y_train_mu)

#%%
# Error on test data
y_predict_test_mu = rf_opt_mu.predict(X_test_mu)
RMSE_test_mu = np.sqrt(mean_squared_error(y_test_mu, y_predict_test_mu))

# Error on all data
y_predict_mu = rf_opt_mu.predict(X_rf)

#%%
plot_importances(rf_opt_mu, descriptors_rf_plot)

# #%% The final optimal model for mco
# rf_opt_mco = RandomForestRegressor(n_estimators = n_estimators_opt,
#                                      max_depth = max_depth_opt,
#                                      random_state=0)
# rf_opt_mco.fit(X_train_mco, y_train_mco)

# y_predict_test_mco = rf_opt_mco.predict(X_test_mco)
# RMSE_test_mco = np.sqrt(mean_squared_error(y_test_mco, y_predict_test_mco))

# # Error on all data
# y_predict_mco = rf_opt_mco.predict(X_rf)
# plot_importances(rf_opt_mco, descriptors_rf)

# plot the actual data and prediction

fig, ax = plt.subplots(figsize=(6, 6))
X0_min, X0_max = 0, 4
X_plot = np.linspace(X0_min, X0_max, 50)
ax.scatter(y_mu, y_predict_mu, s = 20, alpha = 0.5)
ax.plot(X_plot, X_plot, color = 'r')
ax.set_xlabel(r'$\rm \overline{|G|}$')
ax.set_ylabel(r'$\rm Model\ Predicted\ \overline{|G|}$')

max_stars_repo_name: VlachosGroup/Pdn-CO-Stability

max_issues_repo_name: VlachosGroup/Pdn-CO-Stability

max_forks_repo_name: VlachosGroup/Pdn-CO-Stability


text:
import numpy as np
import matplotlib.pyplot as plt
import scipy.optimize

t, V = np.genfromtxt("Battimenti1.txt", unpack=True)

t *= 10**-6     # Converte il tempo in secondi
fft_V = np.abs(np.fft.rfft(V))  # Trasformata di fourier del segnale V

dt_eff = ((t[len(t)-1] - t[0]) / len(t))    # Intervallo medio di tempo tra 2 campionamenti
f_max = 1 / (2*dt_eff)  # Frequenza massima dello spettro
f = np.linspace(0, f_max, len(fft_V))

f = np.delete(f, 0)
fft_V = np.delete(fft_V, 0)     # Elimina il primo dato che corrisponde a f=0 (media di tutti i valori di V)

plt.figure()
plt.subplot(211)
plt.xlabel("Tempo [s]")
plt.ylabel("V(t) [digit]")
plt.plot(t, V, '.-')

plt.subplot(212)
plt.xlabel("Frequenza [Hz]")
plt.ylabel("V(f) [arb. un.]")
plt.yscale("log")   # Inserisce la scala logaritmica sull'asse y
plt.plot(f, fft_V)
plt.show()

max_stars_repo_path: Lab 2/FFT/FFT.py

max_issues_repo_path: Lab 2/FFT/FFT.py

max_forks_repo_path: Lab 2/FFT/FFT.py


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

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

#高斯金字塔
def pydown(img):
    #图像向下取样
    r1 = cv2.pyrDown(img)
    r2 = cv2.pyrDown(r1)
    r3 = cv2.pyrDown(r2)

    cv2.imshow('original', img)
    cv2.imshow('PyrDown1', r1)
    cv2.imshow('PyrDown2', r2)
    cv2.imshow('PyrDown3', r3)
    cv2.waitKey()
    cv2.destroyAllWindows()

#高斯金字塔
def pyup(img):
    #图像向上取样
    r1 = cv2.pyrUp(img)
    r2 = cv2.pyrUp(r1)
    r3 = cv2.pyrUp(r2)

    cv2.imshow('original', img)
    cv2.imshow('PyrUp1', r1)
    cv2.imshow('PyrUp2', r2)
    cv2.imshow('PyrUp3', r3)
    cv2.waitKey()
    cv2.destroyAllWindows()

#拉普拉斯金字塔
def laplacian(img):
    #图像向下取样
    r1 = cv2.pyrDown(img)

    #图像向上取样
    r2 = cv2.pyrUp(r1)

    # 拉普拉斯第0层
    LapPyr0 =img-r2

    #图像向下取样
    r3 = cv2.pyrDown(r1)

    #图像向上取样
    r4 = cv2.pyrUp(r3)

    # 拉普拉斯第1层
    LapPyr1 =r1-r4

    #显示图像
    cv2.imshow('original', img)
    cv2.imshow('LapPyr0', LapPyr0)
    cv2.imshow('LapPyr1', LapPyr1)
    cv2.waitKey()
    cv2.destroyAllWindows()

if __name__ == '__main__':
    img = cv2.imread("images/Forest_500X280.jpg")
    pydown(img)
    pyup(img)
    laplacian(img)

max_stars_repo_name: neohope/NeoDemosImageProcess

max_issues_repo_name: neohope/NeoDemosImageProcess

max_forks_repo_name: neohope/NeoDemosImageProcess


text:
import numpy as np

from .base import Animation

class Rainbow(Animation):

    def __init__(self, direction=(1, 0), speed=1, **kw):
        super().__init__(**kw)
        self.d = np.array(direction)
        self.speed = speed

    def shade(self, out, mask, x):
        from .utils import hsv_to_rgb
        h = np.tensordot(x, self.d / mask.shape, (0, 0))
        out[..., 0] = (h - self.speed * self.passed) % 1
        out[..., 1] = 1
        out[..., 2] = 1
        return hsv_to_rgb(out), mask

class Gradient(Animation):

    def __init__(self, direction=(1, 0), **kw):
        super().__init__(**kw)
        self.d = np.array(direction)

    def shade(self, out, mask, x):
        v = np.tensordot(x, self.d, (0, 0))
        div = np.amax(v)
        if div:
            v /= div
        out[:] = v[..., np.newaxis] * self.color
        return out, mask

max_stars_repo_name: randName/ecran

max_issues_repo_name: randName/ecran

max_forks_repo_name: randName/ecran


text:
import editdistance
import numpy as np

def word_error_rate(predicted_outputs, ground_truths):
	sum_wer=0.0
	for output,ground_truth in zip(predicted_outputs,ground_truths):
		output=output.split(" ")
		ground_truth=ground_truth.split(" ")
		distance = editdistance.eval(output, ground_truth)
		length = max(len(output),len(ground_truth))
		sum_wer+=(distance/length)
	return sum_wer/len(predicted_outputs)

def sentence_acc(predicted_outputs, ground_truths):
	correct_sentences=0
	for output,ground_truth in zip(predicted_outputs,ground_truths):
		if np.array_equal(output,ground_truth):
			correct_sentences+=1
	return correct_sentences/len(predicted_outputs)


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

import torchvision

from brancher.variables import RootVariable, ProbabilisticModel
from brancher.standard_variables import NormalVariable, CategoricalVariable, EmpiricalVariable, RandomIndices
from brancher import inference
import brancher.functions as BF

# Data
number_pixels = 28*28
number_output_classes = 10
train = torchvision.datasets.MNIST(root='./data', train=True, download=True, transform=None)
test = torchvision.datasets.MNIST(root='./data', train=False, download=True, transform=None)
dataset_size = len(train)
input_variable = np.reshape(train.train_data.numpy(), newshape=(dataset_size, number_pixels, 1))
output_labels = train.train_labels.numpy()

# Data sampling model
minibatch_size = 5
minibatch_indices = RandomIndices(dataset_size=dataset_size, batch_size=minibatch_size, name="indices", is_observed=True)
x = EmpiricalVariable(input_variable, indices=minibatch_indices, name="x", is_observed=True)
labels = EmpiricalVariable(output_labels, indices=minibatch_indices, name="labels", is_observed=True)

# Architecture parameters
number_hidden_units = 20
b1 = NormalVariable(np.zeros((number_hidden_units, 1)),
                    10*np.ones((number_hidden_units, 1)), "b1")
b2 = NormalVariable(np.zeros((number_output_classes, 1)),
                    10*np.ones((number_output_classes, 1)), "b2")
weights1 = NormalVariable(np.zeros((number_hidden_units, number_pixels)),
                          10*np.ones((number_hidden_units, number_pixels)), "weights1")
weights2 = NormalVariable(np.zeros((number_output_classes, number_hidden_units)),
                          10*np.ones((number_output_classes, number_hidden_units)), "weights2")

# Forward pass
hidden_units = BF.tanh(BF.matmul(weights1, x) + b1)
final_activations = BF.matmul(weights2, hidden_units) + b2
k = CategoricalVariable(logits=final_activations, name="k")

# Probabilistic model
model = ProbabilisticModel([k])

# Observations
k.observe(labels)

# Variational Model
Qb1 = NormalVariable(np.zeros((number_hidden_units, 1)),
                     0.01*np.ones((number_hidden_units, 1)), "b1", learnable=True)
Qb2 = NormalVariable(np.zeros((number_output_classes, 1)),
                     0.01*np.ones((number_output_classes, 1)), "b2", learnable=True)
Qweights1 = NormalVariable(np.zeros((number_hidden_units, number_pixels)),
                           0.01*np.ones((number_hidden_units, number_pixels)), "weights1", learnable=True)
Qweights2 = NormalVariable(np.zeros((number_output_classes, number_hidden_units)),
                           0.01*np.ones((number_output_classes, number_hidden_units)), "weights2", learnable=True)
variational_model = ProbabilisticModel([Qb1, Qb2, Qweights1, Qweights2])
model.set_posterior_model(variational_model)

# Inference
inference.perform_inference(model,
                            number_iterations=200,
                            number_samples=1,
                            optimizer='Adam', lr=0.001)

# Test accuracy
num_images = 500
test_size = len(test)
test_indices = RandomIndices(dataset_size=test_size, batch_size=1, name="test_indices", is_observed=True)
test_images = EmpiricalVariable(np.reshape(test.test_data.numpy(), newshape=(test.test_data.shape[0], number_pixels, 1)),
                           indices=test_indices, name="x_test", is_observed=True)
test_labels = EmpiricalVariable(test.test_labels.numpy(), indices=test_indices, name="labels", is_observed=True)
test_model = ProbabilisticModel([test_images, test_labels])

s = 0
for _ in range(num_images):
    test_sample = test_model._get_sample(1)
    test_image, test_label = test_sample[test_images], test_sample[test_labels]
    model_output = np.reshape(np.mean(model._get_posterior_sample(10, input_values={x: test_image})[k].cpu().detach().numpy(), axis=0), newshape=(10,))
    s += 1 if int(np.argmax(model_output)) == int(test_label.cpu().detach().numpy()) else 0
print("Accuracy: {} %".format(100*s/float(num_images)))

#weight_map = variational_model._get_sample(1)[Qweights1].data[0, 0, 0, :]
#plt.imshow(np.reshape(weight_map, (28, 28)))
#plt.show()

plt.plot(model.diagnostics["loss curve"])
plt.show()

max_stars_repo_name: kateiyas/Brancher

max_issues_repo_name: kateiyas/Brancher

max_forks_repo_name: kateiyas/Brancher


text:
"""manual:
https://gist.github.com/taruma/6a0b0f9dd26359f6832fe12bab30fdc7"""

import numpy as np
import pandas as pd

def _var(x):
    n = x.size
    return ((x-x.mean())**2).sum()/(n-1)

def _std(x):
    return np.sqrt(_var(x))

def _momen(x, r):
    n = x.size
    return 1/n * ((x-x.mean())**r).sum()

def _skew(x):
    n = x.size
    return n**2 / ((n-1)*(n-2)) * _momen(x, 3) / _std(x)**3

def _kurt(x):
    n = x.size
    return n**3 / ((n-1)*(n-2)*(n-3)) * _momen(x, 4) / _std(x)**4

def _Cv(x):
    return _std(x) / x.mean()

def calc_coef(x):
    """Return (Cv, Cs, Ck)"""
    return (_Cv(x), _skew(x), _kurt(x))

def check_distribution(x, show_stat=False, show_detail=False):

    Cv, Cs, Ck = calc_coef(x)

    if show_stat:
        print(
            f'Cv = {Cv:.5f}',
            f'Cs = {Cs:.5f}',
            f'Ck = {Ck:.5f}',
            sep='\n', end='\n'
        )

    b_normal = True if np.isclose(Cs, 0, atol=0.1) and np.isclose(Ck, 3, atol=0.1) else False
    b_lognormal = True if np.isclose(Cs, 3, atol=0.1) and np.greater(Cs, 0) and np.isclose(Cs, 3*Cv, atol=0.1) else False
    b_gumbel = True if np.isclose(Cs, 1.1396, atol=0.0001) and np.isclose(Ck, 5.4002, atol=0.0001) else False
    b_logpearson = True #selalu benar terlepas nilai Cv, Cs, Ck

    _kriteria = lambda x: "Memenuhi" if x else "Tidak Memenuhi"

    print(
        f'{"Normal":<20}: {_kriteria(b_normal)}',
        f'{"Log Normal":<20}: {_kriteria(b_lognormal)}',
        f'{"Gumbel Tipe I":<20}: {_kriteria(b_gumbel)}',
        f'{"Log Pearson Tipe III":<20}: {_kriteria(b_logpearson)}',
        sep='\n', end='\n'
    )

    if show_detail:
        print(
            '-----------------------------------------------',
            '> Distribusi Normal [syarat](nilai)',
            f'[Cs ~ 0](Cs = {Cs:.5f})',
            f'[Ck ~ 3](Ck = {Ck:.5f})',
            '> Log Normal',
            f'[Cs ~ 3](Cs = {Cs:.5f})',
            f'[Cs > 0](Cs = {Cs:.5f})',
            f'[Cs ~ 3Cv](Cs = {Cs:.5f} ~ 3Cv = {3*Cv:.5f})',
            '> Gumbel Tipe I',
            f'[Cs ~ 1.1396](Cs = {Cs:.5f})',
            f'[Ck ~ 5.4002](Ck = {Ck:.5f})',
            '> Log Pearson Tipe III',
            'Tidak memiliki ciri khas (Cs/Ck/Cv = Bebas)',
            '-----------------------------------------------',
            sep='\n', end='\n'
        )

max_stars_repo_name: taruma/hidrokit

max_issues_repo_name: taruma/hidrokit

max_forks_repo_name: taruma/hidrokit


text:
import pickle, os, datetime
import numpy as np
import pymysql
pymysql.install_as_MySQLdb()
import MySQLdb

from flask import *
from app.config import Config # config 사용
from flask_mongoalchemy import MongoAlchemy
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)
app.config.from_object(Config)
app.config.update(TEMPLATES_AUTO_RELOAD=True, DEBUG=True,)

#app을 생성하고 import, items에서 app객체를 import 받아 db연결 session을 생성하기 때문에
#from app.items.mongo_items import ArticleMongo, mongo_db
#from app.items.mysql_items import ArticleMysql, mysql_db

#setting mongodb
#client_1 = {"username": "dss", "pw": "dsspw", "ip": "3.36.218.2"}
#app.config["MONGOALCHEMY_DATABASE"] = 'mongo' # mongo 라는 db를 사용
#app.config["MONGOALCHEMY_CONNECTION_STRING"] = f'mongodb://{client_1["username"]}:{client_1["pw"]}@{client_1["ip"]}:27017'
#mongo_db = MongoAlchemy(app)

#setting mysql
#client_2 = {"username": "root", "pw": "dss", "ip": "3.36.218.2"}
#app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
#app.config["SQLALCHEMY_DATABASE_URI"] = f'mysql://{client_2["username"]}:{client_2["pw"]}@{client_2["ip"]}/dss'
#mysql_db = SQLAlchemy(app)

#class ArticleMysql(mysql_db.Model):

#    __tablename__ = 'article'

#     article_id = mysql_db.Column(mysql_db.Integer, primary_key=True)
#     sentence = mysql_db.Column(mysql_db.String(500), nullable=False)
#     category = mysql_db.Column(mysql_db.String(20), nullable=False)

#     def __init__(self, sentence, category):
#         self.sentence = sentence
#         self.category = category

# mysql_db.create_all()

# class ArticleMongo(mongo_db.Document):
#     sentence = mongo_db.StringField()
#     category = mongo_db.StringField()
#     rdate = mongo_db.DateTimeField()

# with open('app/models/model.pkl', 'rb') as f:
#     model = pickle.load(f)

# @app.route("/")
# def index():
#     return render_template("index.html")

from app.views import AAPL
from app.views import SP500

# @app.route("/predict")
# def predict():
#     result = {"code": 200}
#     sentence = request.values.get("sentence") # html에서 sentence의 value를 가져옴
#     print(sentence)
#     result["sentence"] = sentence

#     global model # 전역영역의 변수를 지역영역에서 사용하기 위해
#     result['datas'] = list(np.round(model.predict_proba([sentence])[0] * 100, 2))
#     result['categories'] = ['정치', '경제', '사회', '생활', '세계', 'IT']

#     category = result["categories"][model.predict([sentence])[0] - 100] # cate가 100부터이므로 0부터로 만들기 위해

#     # insert data into mongo_db
#     article = ArticleMongo(sentence=sentence, category=category, rdate=datetime.datetime.now())
#     article.save()

#     #insert data into mysql_db
#     global mysql_db
#     article = ArticleMysql(sentence=sentence, category=category)
#     mysql_db.session.add(article)
#     mysql_db.session.commit()

#     return jsonify(result)

app.run() # run.sh에서 flask run을 해서

max_stars_repo_path: Flask 3/app/__init__.py

max_issues_repo_path: Flask 3/app/__init__.py

max_forks_repo_path: Flask 3/app/__init__.py


text:
import numpy as np
from ibslib.io import read,write
from ogre.utils import UniquePlanes

struct_dict = read("Examples/")

unique_planes_kw = \
    {
        "index": 2,
        "symprec": 0.1,
        "verbose": True,
        "force_hall_number": 0
    }

for struct_id,struct in struct_dict.items():
    atoms = struct.get_ase_atoms()
    up = UniquePlanes(atoms, **unique_planes_kw)

    print("{}: {} \n{}".format(struct_id,
                               len(up.unique_idx),
                               np.vstack(up.unique_idx)))


text:
import numpy as np
import points

n = 200

# get a grid
v = np.linspace(-1, 1, n)
x, y = np.meshgrid(v, v, indexing='ij')

# get a boundary
theta = np.linspace(0, 2*np.pi, n, endpoint=False)
bx = 0.5*np.cos(theta)
by = 0.5*np.sin(theta)

pg = points.points(x, y)
pb = points.points(bx, by)

close_pg1 = pb.near_points_slow(pg, 0.02)
close_pg2 = pb.near_points(pg, 0.02)

# test whether the fast and slow give the same result
def test_fast_and_slow_same():
    assert np.allclose(close_pg1, close_pg2)


text:
import numpy as np

from . import utils

class StopWatch(utils.StopWatch):
  def __del__(self):
    print('='*80)
    print('gtimer:')
    total = ', '.join(['%s: %f[s]' % (k,v) for k,v in self.get(reduce=np.sum).items()])
    print(f'  [total]  {total}')
    mean = ', '.join(['%s: %f[s]' % (k,v) for k,v in self.get(reduce=np.mean).items()])
    print(f'  [mean]   {mean}')
    median = ', '.join(['%s: %f[s]' % (k,v) for k,v in self.get(reduce=np.median).items()])
    print(f'  [median] {median}')
    print('='*80)

GTIMER = StopWatch()

def start(name):
  GTIMER.start(name)
def stop(name):
  GTIMER.stop(name)

class Ctx(object):
  def __init__(self, name):
    self.name = name

  def __enter__(self):
    start(self.name)

  def __exit__(self, *args):
    stop(self.name)


text:
import tensorflow as tf
import numpy as np
from tensorflow.examples.tutorials.mnist import input_data
import random, pickle

glob_cost = []
glob_acc = []

file_one = open('cost_fib.pkl', 'wb')
file_two = open('accur_fib.pkl', 'wb')

def fib_init(X_val,Y_val):

    mul_fact = 0.001

    fib_series = [0, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597]

    W = np.zeros((X_val, Y_val))

    for i in range(X_val):
        for j in range(Y_val):
            W[i][j] = random.choice(fib_series) * mul_fact
            if(random.uniform(0, 1)<0.5):
                W[i][j] = -W[i][j]

    return np.float32(W)

def fib_bias(X_val):
    mul_fact = 0.001
    fib_series = [0, 1, 2, 3, 5, 8, 13, 21, 34, 55]
    b = np.zeros(X_val)
    for i in range(X_val):
        b[i] = random.choice(fib_series) * mul_fact
        if(random.uniform(0, 1)<0.5):
            b[i] = -b[i]
    return np.float32(b)

def nn_example():

    mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
    # Python optimisation variables
    learning_rate = 0.0161803399
    epochs = 300
    batch_size = 100
    # declare the training data placeholders
    # input x - for 28 x 28 pixels = 784
    x = tf.placeholder(tf.float32, [None, 784])
    # now declare the output data placeholder - 10 digits
    y = tf.placeholder(tf.float32, [None, 10])
    # now declare the weights connecting the input to the hidden layer
    # W1 = tf.Variable(tf.random_normal([784, 300], stddev=0.03), name='W1')
    # b1 = tf.Variable(tf.random_normal([300]), name='b1')

    W1 = tf.Variable(fib_init(784,300), name='W1')
    b1 = tf.Variable(fib_bias(300), name='b1')

    # and the weights connecting the hidden layer to the output layer
    # W2 = tf.Variable(tf.random_normal([300, 10], stddev=0.03), name='W2')
    # b2 = tf.Variable(tf.random_normal([10]), name='b2')

    W2 = tf.Variable(fib_init(300, 10), name='W2')
    b2 = tf.Variable(fib_bias(10), name='b2')

    # calculate the output of the hidden layer
    hidden_out = tf.add(tf.matmul(x, W1), b1)
    hidden_out = tf.nn.relu(hidden_out)
    # now calculate the hidden layer output - in this case, let's use a softmax activated
    # output layer
    y_ = tf.nn.softmax(tf.add(tf.matmul(hidden_out, W2), b2))
    # now let's define the cost function which we are going to train the model on
    y_clipped = tf.clip_by_value(y_, 1e-10, 0.9999999)
    cross_entropy = -tf.reduce_mean(tf.reduce_sum(y * tf.log(y_clipped)
                                                  + (1 - y) * tf.log(1 - y_clipped), axis=1))
    # add an optimiser
    optimiser = tf.train.GradientDescentOptimizer(learning_rate=learning_rate).minimize(cross_entropy)
    # finally setup the initialisation operator
    init_op = tf.global_variables_initializer()
    # define an accuracy assessment operation
    correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
    # add a summary to store the accuracy
    tf.summary.scalar('accuracy', accuracy)
    merged = tf.summary.merge_all()
    # writer = tf.summary.FileWriter('GoldenRatioProjects')
    # start the session
    with tf.Session() as sess:
        # initialise the variables
        sess.run(init_op)
        total_batch = int(len(mnist.train.labels) / batch_size)
        for epoch in range(epochs):
            avg_cost = 0
            for i in range(total_batch):
                batch_x, batch_y = mnist.train.next_batch(batch_size=batch_size)
                _, c = sess.run([optimiser, cross_entropy], feed_dict={x: batch_x, y: batch_y})
                avg_cost += c / total_batch
            print("Epoch:", (epoch + 1), "cost =", "{:.3f}".format(avg_cost))
            summary = sess.run(merged, feed_dict={x: mnist.test.images, y: mnist.test.labels})
            # writer.add_summary(summary, epoch)
            glob_cost.append(avg_cost)
            print(sess.run(accuracy, feed_dict={x: mnist.test.images, y: mnist.test.labels}))
            glob_acc.append(sess.run(accuracy, feed_dict={x: mnist.test.images, y: mnist.test.labels}))
        print("\nTraining complete!")
        # writer.add_graph(sess.graph)
        print(sess.run(accuracy, feed_dict={x: mnist.test.images, y: mnist.test.labels}))
        pickle.dump(glob_cost, file_one)
        pickle.dump(glob_acc, file_two)

if __name__ == "__main__":
    nn_example()


text:
import torch
from efficientnet_pytorch import EfficientNet
# import onnx # 环境问题
import onnxruntime
import onnx
from torchvision import datasets, models, transforms
import json
from PIL import Image
import cv2
import numpy as np
from timeit import default_timer as timer
from ghost_net import ghost_net
from mobilenetv3 import MobileNetV3
from ghostnet import ghostnet
from mnext import mnext

# RuntimeError: ONNX export failed: Couldn't export Python operator SwishImplementation
# 解决办法 model.set_swish(memory_efficient=False)

# 转化onnx模型 为opencvDNN 中间模型
# 再全局环境中执行
# cd D:\Program Files (x86)\Intel\openvino_2020.3.341\bin
# setupvars.bat
# cd D:\Program Files (x86)\Intel\openvino_2020.3.341\deployment_tools\model_optimizer
# --data_type {FP16,FP32,half,float}
# python mo_onnx.py --input_model D:\code\EfficientNet-PyTorch-master\checkpoint\data_12_23\mobilenetv2\888\mobilenetv2_1_12_23_acc=91.6275.onnx --output_dir D:\code\EfficientNet-PyTorch-master\checkpoint\data_12_23\mobilenetv2\888\ --input_shape [1,3,224,224]
# python mo_onnx.py --input_model D:\code\EfficientNet-PyTorch-master\checkpoint\data_12_23\mobilenetv2\888\mobilenetv2_1_12_23_acc=91.6275.onnx --output_dir D:\code\EfficientNet-PyTorch-master\checkpoint\data_12_23\mobilenetv2\8888\ --input_shape [1,3,224,224] --data_type FP16
# python mo_onnx.py --input_model D:\code\EfficientNet-PyTorch-master\checkpoint\B0\000\B0_acc=99.8528.onnx --output_dir D:\code\EfficientNet-PyTorch-master\checkpoint\B0\000\ --input_shape [1,3,224,224]
# python mo_onnx.py --input_model D:\code\EfficientNet-PyTorch-master\checkpoint\resnet18\111\resnet18_kg.onnx --output_dir D:\code\EfficientNet-PyTorch-master\checkpoint\resnet18\111\ --input_shape [1,3,224,224]
def convert():

    model = EfficientNet.from_name('efficientnet-b0',num_classes=10)
    model.set_swish(memory_efficient=False)
    # 加载模型参数
    path = r"checkpoint/B0/000/B0_acc=99.8528.pth"
    to_path = r"checkpoint/B0/000/B0_acc=99.8528.onnx"

    checkpoint = torch.load(path)
    model.load_state_dict(checkpoint["net"])
    print("loaded model with acc:{}".format(checkpoint["acc"]))
    model.cuda()
    # dummy_input = torch.randn(10, 3, 224, 224)
    dummy_input = torch.randn(1, 3, 224, 224, device='cuda')
    torch.onnx.export(model, dummy_input, to_path, verbose=True)

def convert_resnet18():

    model = models.resnet18(pretrained=False,num_classes=9)
    # 加载模型参数
    path = r"checkpoint/resnet18/000/B0_acc=84.8921.pth"
    to_path = r"checkpoint/resnet18/000/B0_acc=84.8921_2.onnx"

    checkpoint = torch.load(path)
    model.load_state_dict(checkpoint["net"])
    print("loaded model with acc:{}".format(checkpoint["acc"]))
    model.cuda()
    # dummy_input = torch.randn(10, 3, 224, 224)
    dummy_input = torch.randn(1, 3, 224, 224, device='cuda')
    torch.onnx.export(model, dummy_input, to_path, verbose=True)

def convert_resnet18_kg():

    model = models.resnet18(pretrained=False,num_classes=10)
    # 加载模型参数
    path = r"checkpoint/resnet18/111/resnet18_kg_acc=99.3310.pth"
    to_path = r"checkpoint/resnet18/111/resnet18_kg_acc=99.3310.onnx"

    checkpoint = torch.load(path)
    model.load_state_dict(checkpoint["net"])
    print("loaded model with acc:{}".format(checkpoint["acc"]))
    model.cuda()
    # dummy_input = torch.randn(10, 3, 224, 224)
    dummy_input = torch.randn(1, 3, 224, 224, device='cuda')
    torch.onnx.export(model, dummy_input, to_path, verbose=True)

def convert_ghostnet_1_kg():

    model =  ghost_net(num_classes=10, width_mult=1.0)
    # 加载模型参数
    path = r"checkpoint/ghost_net/000/ghostnet_kg_acc=97.8591.pth"
    to_path = r"checkpoint/ghost_net/000/ghostnet_kg_acc=97.8591.onnx"

    checkpoint = torch.load(path)
    model.load_state_dict(checkpoint["net"])
    print("loaded model with acc:{}".format(checkpoint["acc"]))
    model.cuda()
    # dummy_input = torch.randn(10, 3, 224, 224)
    dummy_input = torch.randn(1, 3, 224, 224, device='cuda')
    torch.onnx.export(model, dummy_input, to_path, verbose=True)
# 转化onnx模型
# cd D:\Program Files (x86)\Intel\openvino_2021.1.110\deployment_tools\model_optimizer
# python mo_onnx.py --input_model D:\code\EfficientNet-PyTorch-master\checkpoint\data_11_16\ghostnet\222\ghostnet_1_drive.onnx --output_dir D:\code\EfficientNet-PyTorch-master\checkpoint\data_11_16\ghostnet\222\ --input_shape [1,3,224,224]
def convert_ghostnet_1_my():

    # model =  ghost_net(num_classes=9, width_mult=1.0)
    model = ghostnet(num_classes=9, width=1.)
    # 加载模型参数
    path = r"checkpoint/data_11_16/ghostnet/nopre/222/ghostnet_1_drive=96.5665.pth"
    to_path = r"checkpoint/data_11_16/ghostnet/nopre/222/ghostnet_1_drive.onnx"

    checkpoint = torch.load(path)
    model.load_state_dict(checkpoint["net"])
    print("loaded model with acc:{}".format(checkpoint["acc"]))
    model.cuda()
    # dummy_input = torch.randn(10, 3, 224, 224)
    dummy_input = torch.randn(1, 3, 224, 224, device='cuda')
    torch.onnx.export(model, dummy_input, to_path, verbose=True)

def convert_ghostnet_0_5_kg():

    model =  ghost_net(num_classes=9, width_mult=0.5)
    # 加载模型参数
    path = r"checkpoint/ghost_net/333/ghostnet_05_kg_acc=68.3453.pth"
    to_path = r"checkpoint/ghost_net/333/ghostnet_05_my_acc=68.3453.onnx"

    checkpoint = torch.load(path)
    model.load_state_dict(checkpoint["net"])
    print("loaded model with acc:{}".format(checkpoint["acc"]))
    model.cuda()
    model.eval()
    # dummy_input = torch.randn(10, 3, 224, 224)
    dummy_input = torch.randn(1, 3, 224, 224, device='cuda')
    torch.onnx.export(model, dummy_input, to_path, verbose=True)

def convert_ghostnet_0_3_kg():

    model =  ghost_net(num_classes=10, width_mult=0.3)
    # 加载模型参数
    path = r"checkpoint/ghost_net/222/ghostnet_03_kg_acc=89.4291.pth"
    to_path = r"checkpoint/ghost_net/222/ghostnet_03_kg_acc=89.4291.onnx"

    checkpoint = torch.load(path)
    model.load_state_dict(checkpoint["net"])
    print("loaded model with acc:{}".format(checkpoint["acc"]))
    model.cuda()
    # dummy_input = torch.randn(10, 3, 224, 224)
    dummy_input = torch.randn(1, 3, 224, 224, device='cuda')
    torch.onnx.export(model, dummy_input, to_path, verbose=True)
# 转化onnx模型
# python mo_onnx.py --input_model D:\code\EfficientNet-PyTorch-master\checkpoint\data_11_16\mobilenetv2\pre\0\111\mobilenetv2_1_my_224.onnx --output_dir D:\code\EfficientNet-PyTorch-master\checkpoint\data_11_16\mobilenetv2\pre\0\111\ --input_shape [1,3,224,224]
def convert_mobilenetv2():

    # model = models.mobilenet_v2(pretrained=False, num_classes=9)
    # model = models.mobilenet_v2(pretrained=False, num_classes=6)
    # model = models.mobilenet_v2(pretrained=False, num_classes=7)
    model = models.mobilenet_v2(pretrained=False, num_classes=8)
    # 加载模型参数
    # path = r"checkpoint/data_3_25_all/mobilenetv2/111/mobilenetv2_224_acc=85.6154.pth"
    # to_path = r"checkpoint/data_3_25_all/mobilenetv2/111/mobilenetv2_224_acc=85.6154.onnx"

    path = r"checkpoint/data_3_25_all/mobilenetv2/class8/666/mobilenetv2_224_8_acc=90.5516.pth"
    to_path = r"checkpoint/data_3_25_all/mobilenetv2/class8/666/mobilenetv2_224_8_acc=90.5516.onnx"
    checkpoint = torch.load(path)
    model.load_state_dict(checkpoint["net"])
    print("loaded model with acc:{}".format(checkpoint["acc"]))
    model.cuda()
    dummy_input = torch.randn(1, 3, 224, 224, device='cuda')
    # dummy_input = torch.randn(1, 3, 160, 160, device='cuda')
    torch.onnx.export(model, dummy_input, to_path, verbose=True)

# 转化onnx模型 为opencvDNN 中间模型
# 再全局环境中执行
# cd D:\Program Files (x86)\Intel\openvino_2020.3.341\bin
# setupvars.bat
# cd D:\Program Files (x86)\Intel\openvino_2020.3.341\deployment_tools\model_optimizer
# python mo_onnx.py --input_model D:\code\EfficientNet-PyTorch-master\checkpoint\data_12_23\mnext\000\mnext_1_12_23_acc=92.1753.onnx --output_dir D:\code\EfficientNet-PyTorch-master\checkpoint\data_12_23\mnext\000\ --input_shape [1,3,224,224]
def convert_mnext():

    model = mnext(num_classes=9, width_mult=1.)
    # 加载模型参数
    path = r"checkpoint/data_12_23/mnext/000/mnext_1_12_23_acc=92.1753.pth"
    to_path = r"checkpoint/data_12_23/mnext/000/mnext_1_12_23_acc=92.1753.onnx"
    checkpoint = torch.load(path)
    model.load_state_dict(checkpoint["net"])
    print("loaded model with acc:{}".format(checkpoint["acc"]))
    model.cuda()
    dummy_input = torch.randn(1, 3, 224, 224, device='cuda')
    # dummy_input = torch.randn(1, 3, 160, 160, device='cuda')
    torch.onnx.export(model, dummy_input, to_path, verbose=True)

def convert_shufflenetv2():
    # model= models.shufflenet_v2_x1_0(pretrained=False, num_classes=9)
    # path = r"checkpoint/data_11_16/shufflenetv2/pre/111/shufflenetv2_1_my_acc=97.8541.pth"
    # to_path = r"checkpoint/data_11_16/shufflenetv2/pre/111/shufflenetv2_1.onnx"

    model = models.shufflenet_v2_x0_5(pretrained=False, num_classes=9)
    path = r"checkpoint/data_11_16/shufflenetv2/pre/333/shufflenetv2_05_my_acc=99.5708.pth"
    to_path = r"checkpoint/data_11_16/shufflenetv2/pre/333/shufflenetv2_05_my.onnx"

    checkpoint = torch.load(path)
    model.load_state_dict(checkpoint["net"])
    print("loaded model with acc:{}".format(checkpoint["acc"]))
    model.cuda()
    # dummy_input = torch.randn(10, 3, 224, 224)
    dummy_input = torch.randn(1, 3, 224, 224, device='cuda')
    torch.onnx.export(model, dummy_input, to_path, verbose=True)

def convert_mobilenetv2_kaggle():

    model = models.mobilenet_v2(pretrained=False, num_classes=10)
    # 加载模型参数
    path = r"checkpoint/mobilenetv2/111/mobilenetv2_kg_acc=99.5986.pth"
    to_path = r"checkpoint/mobilenetv2/111/mobilenetv2_kg_acc=99.5986.onnx"

    checkpoint = torch.load(path)
    model.load_state_dict(checkpoint["net"])
    print("loaded model with acc:{}".format(checkpoint["acc"]))
    model.cuda()
    # dummy_input = torch.randn(10, 3, 224, 224)
    dummy_input = torch.randn(1, 3, 224, 224, device='cuda')
    torch.onnx.export(model, dummy_input, to_path, verbose=True)

def convert_mobilenetv2_05_kaggle():

    model = models.mobilenet_v2(pretrained=False,num_classes=10,width_mult=0.5)
    # 加载模型参数
    path = r"checkpoint/mobilenetv2/222/moblienetv2_05_kg_acc=83.0508.pth"
    to_path = r"checkpoint/mobilenetv2/222/moblienetv2_05_kg_acc=83.0508.onnx"

    checkpoint = torch.load(path)
    model.load_state_dict(checkpoint["net"])
    print("loaded model with acc:{}".format(checkpoint["acc"]))
    model.cuda()
    # dummy_input = torch.randn(10, 3, 224, 224)
    dummy_input = torch.randn(1, 3, 224, 224, device='cuda')
    torch.onnx.export(model, dummy_input, to_path, verbose=True)

def convert_mobilenetv3():
    num_classes = 9
    model = MobileNetV3(n_class=num_classes, mode="small", dropout=0.2, width_mult=1.0)
    # model = MobileNetV3(n_class=num_classes, mode="large", dropout=0.2, width_mult=1.0)
    # 加载模型参数
    path = r"checkpoint/mobilenetv3/000/moblienetv3_s_my_acc=65.4676.pth"
    to_path = r"checkpoint/mobilenetv3/000/moblienetv3_s_my_acc=65.4676.onnx"

    checkpoint = torch.load(path)
    model.load_state_dict(checkpoint["net"])
    print("loaded model with acc:{}".format(checkpoint["acc"]))
    model.cuda()
    # dummy_input = torch.randn(10, 3, 224, 224)
    dummy_input = torch.randn(1, 3, 224, 224, device='cuda')
    torch.onnx.export(model, dummy_input, to_path, verbose=True)

def convert_resnet50():

    # model = models.resnet50(pretrained=True)
    # model = models.resnet18(pretrained=True)
    model = models.resnext50_32x4d(pretrained=True)
    # model = models.mobilenet_v2(pretrained=True)
    # 加载模型参数
    # path = r"checkpoint/B0/000/B0_acc=99.8528.pth"
    # to_path = r"weights/resnet50.onnx"
    # to_path = r"weights/resnet18.onnx"
    # to_path = r"weights/resnet18_2.onnx"
    to_path = r"weights/resnext50.onnx"
    # to_path = r"weights/mobilenetv2_2.onnx"
    # checkpoint = torch.load(path)
    # model.load_state_dict(checkpoint["net"])
    # print("loaded model with acc:{}".format(checkpoint["acc"]))
    model.cuda()
    # dummy_input = torch.randn(10, 3, 224, 224)
    dummy_input = torch.randn(1, 3, 224, 224, device='cuda')
    torch.onnx.export(model, dummy_input, to_path, verbose=True)

# def test():
#
#     # Load the ONNX model
#     to_path = r"checkpoint\B0\B0_acc=99.8528.onnx"
#     model = onnx.load(to_path)
#
#     # Check that the IR is well formed
#     onnx.checker.check_model(model)
#
#     # Print a human readable representation of the graph
#     onnx.helper.printable_graph(model.graph)

def test_resnet50():

    model = models.resnet50(pretrained=True)

    # Preprocess image
    tfms = transforms.Compose([transforms.Resize((224,224)), transforms.ToTensor(),
        transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),])
    img = tfms(Image.open('./data/imgs/elephant.jpg')).unsqueeze(0)
    print(img.shape) # torch.Size([1, 3, 224, 224])

    # Load ImageNet class names
    labels_map = json.load(open('.\data\labels_map.txt'))
    labels_map = [labels_map[str(i)] for i in range(1000)]

    # Classify
    model.eval()
    with torch.no_grad():
        outputs = model(img)

    # Print predictions
    print('-----')
    for idx in torch.topk(outputs, k=5).indices.squeeze(0).tolist():
        prob = torch.softmax(outputs, dim=1)[0, idx].item()
        print('{label:<75} ({p:.2f}%)'.format(label=labels_map[idx], p=prob*100))
        # print('{label:<75} ({p:.2f}%)'.format(label=idx, p=prob * 100))

def test_resnet50_cv2():

    # model = models.resnet50(pretrained=True).cuda()
    model = models.resnet50(pretrained=True)
    src = cv2.imread("./data/imgs/elephant.jpg") # aeroplane.jpg
    image = cv2.resize(src, (224, 224))
    image = np.float32(image) / 255.0
    image[:,:,] -= (np.float32(0.485), np.float32(0.456), np.float32(0.406))
    image[:,:,] /= (np.float32(0.229), np.float32(0.224), np.float32(0.225))
    image = image.transpose((2, 0, 1))
    input_x = torch.from_numpy(image).unsqueeze(0)
    print(input_x.size())

    # pred = model(input_x.cuda())
    pred = model(input_x)
    pred_index = torch.argmax(pred, 1).cpu().detach().numpy()
    print(pred_index)

    # Load ImageNet class names
    labels_map = json.load(open('.\data\labels_map.txt'))
    labels_map = [labels_map[str(i)] for i in range(1000)]
    print("current predict class name : %s"%labels_map[pred_index[0]])
    cv2.putText(src, labels_map[pred_index[0]], (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1.0, (0, 0, 255), 2)
    cv2.imshow("input", src)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

def testONNX():

    # Load ImageNet class names
    labels_map = json.load(open('.\data\labels_map.txt'))
    labels_map = [labels_map[str(i)] for i in range(1000)]

    # path = r"weights/resnet18_2.onnx" # inferrence time:0.026459999999999928
    # path = r"checkpoint/mobilenetv2/000/mv2_acc=82.7338.onnx" #inferrence time:0.008720400000000073
    path = r"checkpoint/mobilenetv3/000/moblienetv3_s_my_acc=65.4676.onnx"  # inferrence time:0.013804399999999939

    # path = r"weights/resnet50.onnx" #inferrence time:0.07290449999999993
    # path = r"weights/resnext50.onnx"  # inferrence time:0.11427840000000011

    onnx_session = onnxruntime.InferenceSession(path,None)

    src = cv2.imread("./data/imgs/elephant.jpg")  # aeroplane.jpg
    src2 = cv2.cvtColor(src, cv2.COLOR_BGR2RGB)
    image = cv2.resize(src2, (224, 224))
    image = np.float32(image) / 255.0
    image[:, :, ] -= (np.float32(0.485), np.float32(0.456), np.float32(0.406))
    image[:, :, ] /= (np.float32(0.229), np.float32(0.224), np.float32(0.225))

    image = image.transpose(2, 0, 1)  # 转换轴，pytorch为channel first
    image = image.reshape(1, 3, 224, 224)  # barch,channel,height,weight
    pre_t = timer()
    inputs = {onnx_session.get_inputs()[0].name:image}
    probs =  onnx_session.run(None, inputs)
    index = np.argmax(probs)
    curr_t = timer()
    infer_t = curr_t - pre_t
    print("inferrence time:{}".format(infer_t))
    cv2.putText(src, labels_map[index], (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1.0, (0, 0, 255), 2)
    cv2.imshow("input", src)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

def testONNX_dataset():

    # Load dataset
    dataset_path = r"data/drive_data.txt"
    with open(dataset_path) as f:
        datasets= [c.strip() for c in f.readlines()]
    path = r"ghostnet_my_nv_05_acc=97.4856_eval.onnx"
    # path = r"checkpoint/ghost_net/555/ghostnet_my_nv_05_acc=97.4856.onnx" # acc:0.2557471264367816,  356/1392
    # path = r"checkpoint/ghost_net/555/ghostnet_my_nv_05_acc=97.4856_eval.onnx" # acc:0.2557471264367816,  356/1392
    onnx_session = onnxruntime.InferenceSession(path, None)
    dnn_net = cv2.dnn.readNetFromONNX(path)

    model = ghost_net(num_classes=9, width_mult=0.5)
    # 加载模型参数
    path = r"checkpoint/ghost_net/555/ghostnet_my_nv_05_acc=97.4856.pth"
    checkpoint = torch.load(path)
    model.load_state_dict(checkpoint["net"])
    model.cpu()
    model.eval()

    total = 0
    right = 0
    dnn_right = 0
    model_right = 0
    for data in datasets:

        img_path = data.split(" ")[0]
        label = int(data.split(" ")[1])
        src = cv2.imread(img_path)
        # print(src.shape) # height,weight,channel
        src2 = cv2.cvtColor(src, cv2.COLOR_BGR2RGB)
        image = cv2.resize(src2, (224, 224))
        # print(image.shape)
        image = np.float32(image) / 255.0
        image[:, :, ] -= (np.float32(0.485), np.float32(0.456), np.float32(0.406))
        image[:, :, ] /= (np.float32(0.229), np.float32(0.224), np.float32(0.225))
        blob = cv2.dnn.blobFromImage(image, 1.0, (224, 224), (0, 0, 0), False)
        dnn_net.setInput(blob)
        dnn_probs = dnn_net.forward()
        dnn_index = np.argmax(dnn_probs)

        # print(image.shape)
        image = image.transpose(2, 0, 1) # 转换轴，pytorch为channel first
        image = image.reshape(1, 3, 224, 224) # barch,channel,height,weight

        # image = []
        # image.append(image)
        # image = np.asarray(image)

        inputs = {onnx_session.get_inputs()[0].name: image}
        probs =  onnx_session.run(None, inputs)
        index = np.argmax(probs)

        model_image =torch.from_numpy(image)
        output = model(model_image)
        model_index = np.argmax(output.detach().numpy())
        print("dnn_probs:{},probs:{},output:{}".format(dnn_probs, probs,output))
        print("dnn_index:{},index:{},model_index:{},label:{}".format(dnn_index,index,model_index,label))
        total +=1
        if index == label:
            right += 1

        if dnn_index == label:
            dnn_right += 1

        if model_index == label:
            model_right += 1
    print("acc:{},dnn_acc:{},model_acc :{}".format(right/total,dnn_right/total,model_right/total))

def testDNN():

    # Load ImageNet class names
    labels_map = json.load(open('.\data\labels_map.txt'))
    labels_map = [labels_map[str(i)] for i in range(1000)]

    # path = r"weights/resnet50.onnx" # inferrence time:0.25801849999999993
    # path = r"weights/resnet18.onnx" # inferrence time:0.09869299999999992
    path = r"weights/resnet18_2.onnx"  # inferrence time:0.09869299999999992
    # path = r"weights/resnext50.onnx" # inferrence time:0.30201659999999997
    # path = r"weights/mobilenetv2.onnx"
    net = cv2.dnn.readNetFromONNX(path)
    src = cv2.imread("./data/imgs/elephant.jpg")  # aeroplane.jpg
    src2 = cv2.cvtColor(src, cv2.COLOR_BGR2RGB)
    image = cv2.resize(src2, (224, 224))
    image = np.float32(image) / 255.0
    image[:, :, ] -= (np.float32(0.485), np.float32(0.456), np.float32(0.406))
    image[:, :, ] /= (np.float32(0.229), np.float32(0.224), np.float32(0.225))
    pre_t = timer()
    blob = cv2.dnn.blobFromImage(image, 1.0, (224, 224), (0, 0, 0), False)
    net.setInput(blob)
    probs = net.forward()
    index = np.argmax(probs)
    curr_t = timer()
    infer_t = curr_t - pre_t
    print("inferrence time:{}".format(infer_t))
    cv2.putText(src, labels_map[index], (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1.0, (0, 0, 255), 2)
    cv2.imshow("input", src)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

# 初始化openvino，必须在指定目录运行

# cd D:\Program Files (x86)\Intel\openvino_2020.3.341\bin
# setupvars.bat
# d:
def testDNN_openvino():
    print("testDNN_openvino")
    # Load ImageNet class names
    labels_map = json.load(open('.\data\labels_map.txt'))
    labels_map = [labels_map[str(i)] for i in range(1000)]

    xml_path = r"weights\resnet18.xml"
    bin_path = r"weights\resnet18.bin"

    net = cv2.dnn.readNetFromModelOptimizer(xml_path, bin_path)
    # 下面设置不需要
    # net.setPreferableBackend(cv2.dnn.DNN_BACKEND_INFERENCE_ENGINE)
    # net.setPreferableTarget(cv2.dnn.DNN_TARGET_CPU)

    src = cv2.imread("./data/imgs/elephant.jpg")  # aeroplane.jpg
    image = cv2.resize(src, (224, 224))
    image = np.float32(image) / 255.0
    image[:, :, ] -= (np.float32(0.485), np.float32(0.456), np.float32(0.406))
    image[:, :, ] /= (np.float32(0.229), np.float32(0.224), np.float32(0.225))
    blob = cv2.dnn.blobFromImage(image, 1.0, (224, 224), (0, 0, 0), False)
    net.setInput(blob)
    probs = net.forward()
    index = np.argmax(probs)
    cv2.putText(src, labels_map[index], (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1.0, (0, 0, 255), 2)
    cv2.imshow("input", src)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

if __name__ == '__main__':
    # convert()
    # convert_resnet18()
    # convert_resnet18_kg()
    # convert_ghostnet_1_kg()
    # convert_ghostnet_1_my()
    # convert_ghostnet_0_5_kg()
    # convert_ghostnet_0_3_kg()
    convert_mobilenetv2()
    # convert_mnext()
    # convert_shufflenetv2()
    # convert_mobilenetv2_kaggle()
    # convert_mobilenetv2_05_kaggle()
    # convert_mobilenetv3()
    # convert_resnet50()
    # test()
    # test_resnet50()
    # test_resnet50_cv2()
    # testONNX()
    # testDNN()
    # testDNN_openvino()
    # testONNX_dataset()

max_stars_repo_name: QFaceblue/Driving-Behavior-Recognition

max_issues_repo_name: QFaceblue/Driving-Behavior-Recognition

max_forks_repo_name: QFaceblue/Driving-Behavior-Recognition


text:
import random
import numpy as np
from numpy.linalg import svd
import matplotlib.pyplot as plt

from utils.utility import mean_normalize

class PCA:
    def __init__(self, k_dim=2):
        self.k_dim = k_dim

        return

    def fit(self, X):
        m, n = X.shape
        X, mu = mean_normalize(X)
        # cov_X = 1/m * np.cov(X)
        cov_X = 1/m * (X.T @ X)
        U, S, vh = svd(cov_X)
        self.components_ = U

        return self

    def transform(self, X):
        # if not self.components_:
        #     raise ValueError('Model not fit. Fit the data first.')

        X, mu = mean_normalize(X)
        _new_basis = self.components_[:, :self.k_dim]
        Xt = []
        for i in range(X.shape[0]):
            xt = X[i, :] @ _new_basis
            Xt.append(xt)

        Xt = np.array(Xt)

        return Xt

    def inverse_transform(self, Xt):
        # if not self.components_:
        #     raise ValueError('Model not fit. Fit the data first.')

        _new_basis = self.components_[:, :self.k_dim]
        X_approx = []
        for i in range(Xt.shape[0]):
            xt = _new_basis @ Xt[i, :].reshape(Xt.shape[1], 1)
            X_approx.append(xt)

        X_approx = np.array(X_approx)

        return X_approx

reason: import numpy,from numpy