text:
# coding: utf-8
import numpy as np
import pandas as pd
import yaml

import math
import random
import argparse
import collections
import sys
from pathlib import Path
import os

from lib import *

parser = argparse.ArgumentParser()
config = yaml.safe_load(open('config.yaml'))
config['cross_policy'] = collections.defaultdict(dict,config['cross_policy'])
parameters = config['parameters']

for k, v in parameters.items():
    parser.add_argument('--'+k,default=v['default'],
                        type=str2bool if type(v['default']) == bool else type(v['default']))

args = parser.parse_args()
for k,v in vars(args).items():
    locals()[k] = v
    parameters[k]['value'] = v

num_cross = int((cross_rate * num_pop)/2)
num_no_cross = num_pop-2*num_cross

# information to catch from algorithm
binary_knapsack = BinaryKnapsack()
binary_knapsack.load(DIRS['INPUT']+parameters['instance_name']['value'])
objective = BinaryKnapsackObjective(binary_knapsack)
cross_policy = eval(parameters['cross_policy']['value'])(**config['cross_policy'][parameters['cross_policy']['value']])
mutation_policy = eval(parameters['mutation_policy']['value'])(mutation_rate)

population = []
for i in range(num_pop):
    ind = Individual()
    ind.rand_genome_bool(len(binary_knapsack.weights))
    population.append(ind)
    objective.compute(ind)

columns = ['#Generation','Best fitness','Mean fitness', 'Median fitness', 'Worst fitness']
df = pd.DataFrame([],columns = columns)
df = df.set_index(columns[0])
ofvs = [ind.ofv for ind in population]
argsort_indexes = np.argsort(ofvs)[::-1]
best_ind = population[argsort_indexes[0]]
for j in argsort_indexes:
    if binary_knapsack.is_viable(population[j].genome):
        best_ind = population[j]
        break
# best_ind = population[0]
# best_ind_is_viable = binary_knapsack.is_viable(best_ind.genome)
# for ind in population:
#     is_viable = binary_knapsack.is_viable(ind.genome)
#     if (ind.ofv >= best_ind.ofv and is_viable==best_ind_is_viable) or (not best_ind_is_viable and is_viable):
#         best_ind = ind
#         best_ind_is_viable = is_viable
# best_ind = population[np.argmax()]
# ofvs = [ind.ofv for ind in population]
df.loc[1] = [f'{best_ind.ofv:.4f}',f'{np.mean(ofvs):.4f}',f'{np.median(ofvs):.4f}',f'{np.min(ofvs):.4f}']
for i in range(2,num_generations+1):

    new_population = []
    # Cross
    selection_policy=eval(parameters['selection_policy']['value'])(population)
    for j in range(num_cross):
        ind1=selection_policy.select()
        ind2=selection_policy.select()
        nind1, nind2 = cross_policy.cross(ind1,ind2)
        new_population.append(nind1)
        new_population.append(nind2)
    # Select the rest left of individuals if the cross rate is not 100%
    new_population.extend([population[i].__copy__()
                           for i in random.sample(list(range(len(population))),num_no_cross)])

    # Mutate these new individuals - Mutation
    for j in range(num_pop):
        mutation_policy.mutate(new_population[j])

    # Select best individual from previous population - Elitism
    if elitism:
        new_population[random.randint(0,len(new_population)-1)]=best_ind.__copy__()
    population = new_population

    for ind in population:
        objective.compute(ind)

    ofvs = [ind.ofv for ind in population]
    argsort_indexes = np.argsort(ofvs)[::-1]
    best_ind = population[argsort_indexes[0]]
    for j in argsort_indexes:
        if binary_knapsack.is_viable(population[j].genome):
            best_ind = population[j]
            break

    # best_ind = population[np.argmax(ofvs)]
    # for i in np.argsort(ofvs)[::-1]:
    #     if binary_knapsack.is_viable(population[i].genome):
    #         best_ind = population[i]
    #         break

    # ofvs = [ind.ofv for ind in population]
    # best_ind = population[0]
    # best_ind_is_viable = binary_knapsack.is_viable(best_ind.genome)
    # for ind in population:
    #     is_viable = binary_knapsack.is_viable(ind.genome)
    #     if (ind.ofv >= best_ind.ofv and is_viable==best_ind_is_viable) or (not best_ind_is_viable and is_viable):
    #         best_ind = ind
    #         best_ind_is_viable = is_viable

    df.loc[i] = [f'{best_ind.ofv:.4f}',f'{np.mean(ofvs):.4f}',f'{np.median(ofvs):.4f}',f'{np.min(ofvs):.4f}']
df = df.reset_index()

if config['general']['print_table']:
    # PRINT TABLE BLOCK
    parameters_print = {key: val
                        for key, val
                        in zip([i['pretty_name'] for i in parameters.values()],
                            [i['value'] for i in parameters.values()])}
    column_names_size = []
    max_size = max(map(len,parameters_print.keys()))
    max_size_val = max(map(len,map(str,parameters_print.values())))

    print(Colors.DARK,end='')

    for key, value in parameters_print.items():
        print(Colors.Backgrounds.MAGENTA+key+' '*(max_size-len(key)),
            Colors.Backgrounds.BLUE+str(value)+' '*(max_size_val-len(str(value)))+Colors.RESET_BACKGROUND)
    SPACED_STR = '   '
    for i, col in enumerate(columns):
        col =SPACED_STR + col + SPACED_STR
        print(COLORS[i],end='')
        if i != 0:
            print(col,end='')
        else:
            print(col,end='')
        print(Colors.RESET_BACKGROUND,end='')
        column_names_size.append(len(col))
    print()
    for index, row in df.iterrows():
        print(Colors.Backgrounds.WHITE,end='')
        for j, item in enumerate(row.T.to_list()):
            item = str(item)
            print(item,end='')
            size = len(item)
            print(" "*(len(columns[j]+SPACED_STR*2)-size),end='')

        print(Colors.RESET_BACKGROUND)

    print(Colors.RESET,end='')

string=get_parameters_name({k: v['value'] for k,v in parameters.items()})
Path(os.path.dirname(DIRS['RESULTS']+string)).mkdir(parents=True, exist_ok=True)

fout = open(DIRS['RESULTS']+string+'.json','w')
fout.write(df.to_json(orient='records',lines=False))
fout.close()

if config['general']['print']:
    print(f"Optimal solution OFV is {objective.compute(Individual(genome=binary_knapsack.optimal_solution)):.2f}")
    print(f"Optimal solution utility is {np.sum(binary_knapsack.utilities[binary_knapsack.optimal_solution]):.2f}")
    print(f"Optimal solution is {binary_knapsack.optimal_solution}")
    print(f"Best individual {best_ind.genome}")


text:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Project      : tql-Python.
# @File         : pseudo
# @Time         : 2020/9/6 1:56 下午
# @Author       : yuanjie
# @Email        : yuanjie@xiaomi.com
# @Software     : PyCharm
# @Description  :

import numpy as np
import pandas as pd
from lightgbm import LGBMClassifier
from sklearn.metrics import roc_auc_score

# 目前只支持二分类 TODO: 回归

class Pseudo(object):

    def __init__(self, X, y, X_test, confidences=(0.99, 0.01), **kwargs):
        self.X = X
        self.y = y
        self.X_test = X_test
        self.confidences = confidences  # 顺序(正，负)，加入训练的样本比例： 正、负比例 # todo: 百分位数或者个数，分正负

    def run(self, n_iter=1):
        X, y = self.X.copy(), self.y.copy()

        stat = []
        for i in range(0, n_iter + 1):
            clf, score, self.test_preds = self.fit_predict(X, y, self.X_test)

            if i == 0:
                print(f"Init Score: {score}")
            else:
                print(f"PseudoLabeling{i} Score: {score}")
                X, y = self.pseudo_labeling(self.test_preds)
                stat.append((i, clf, score))

        self.stat_info = pd.DataFrame(stat, columns=['n_iter', 'model', 'score'])
        print(self.stat_info)

    def fit_predict(self, X, y, X_test, **kwargs):
        """重写继承即可
        :return: clf, score, test_preds
        """
        clf = LGBMClassifier()
        clf.fit(X, y)
        score = roc_auc_score(y, clf.predict_proba(X)[:, 1])  # 验证方式？？？线下训练集CV
        test_preds = clf.predict_proba(X_test)
        return clf, score, test_preds

    def pseudo_labeling(self, preds):
        thresholds = np.quantile(preds, self.confidences)  # 计算上下分位数
        print(f"Thresholds: {thresholds}")

        pos_index = np.where(preds > thresholds[0])[0]
        neg_index = np.where(preds < thresholds[1])[0]

        self.X_pseudo = self.X_test[np.r_[pos_index, neg_index], :]
        self.y_pseudo = np.r_[np.ones(len(pos_index)), np.zeros(len(neg_index))]

        # 合并数据
        print(f"Add: PositiveSamples={len(pos_index)} NegativeSamples={len(neg_index)}\n")

        X = np.r_[self.X, self.X_pseudo]
        y = np.r_[self.y, self.y_pseudo]

        return X, y

ext: py

reason: import numpy


text:
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
from statistics import stdev

#labels = ['1', '2', '4', '8', '16', '24','32']

labels =  ['0', '1', '2', '4', '8', '16', '32', '64']

def div_thousand (list):
    return [val /1000.0 for val in list]

throughput = [285210,385989,437217,457021,471494,464344,427846,368955]
throughput = div_thousand(throughput)

figure_name='experiment_4'
treatment_label="throughput"

x = np.arange(len(labels))  # the label locations
width = 0.85  # the width of the bars.0

fig, ax = plt.subplots()
rects1 = ax.bar(x,throughput, width, color='tab:red')

# Add some text for labels, title and custom x-axis tick labels, etc.
ax.set_ylabel('KOP/S')
#ax.set_title('Clover YCSB-A (50% write)')
ax.set_xticks(x)
ax.set_xticklabels(labels)
ax.set_xlabel("Cached Keys")

fig.tight_layout()
plt.savefig(figure_name+'.pdf')
plt.savefig(figure_name+'.png')
plt.show()

max_stars_repo_name: wantonsolutions/warm

max_issues_repo_name: wantonsolutions/warm

max_forks_repo_name: wantonsolutions/warm


text:
import numpy
import threading
import time
import socket
import struct
import numpy as np
import uuid

krangIP = '192.168.30.1'

dronesIP = ['192.168.30.11', '192.168.30.12', '192.168.30.13',
            '192.168.30.14', '192.168.30.15', '192.168.30.16']

broadcastIP = '192.168.30.255'

rightBrainIP = '192.168.40.1'
leftBrainIP = '192.168.40.2'

droneIP = ''

statusPort = 5007
droneCommRightPort = 5108
droneCommLeftPort = 5208

requestStatusCode = 1001
sendStatusCode = 1002

startCode = [2001,2002,2003,2004]
updateVisionCode = [3001,3002]
updateParameters = [4001,4002]
killCode = 99999

running = True

max_int64 = 0xFFFFFFFFFFFFFFFF

class bcolors:
    RED= '\033[31m'
    GREEN = '\033[32m'
    BROWN = '\033[33m'
    BLUE = '\033[34m'
    MAGENTA = '\033[35m'
    CYAN = '\033[36m'
    ENDC = '\033[0m'

    def disable(self):
        self.RED = ''
        self.GREEN = ''
        self.BROWN = ''
        self.BLUE = ''
        self.MAGENTA = ''
        self.CYAN = ''
        self.ENDC = ''

def broadcastSize(fishVR,size):
    global running
    socketSend = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    socketSend.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    socketSend.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)

    print('>> Apoc wants everybody to know the size of the fish : ')

    data = struct.pack('id', fishVR,size)

    #print('Send : ' + str(struct.unpack('dddi', data)))
    try:
        socketSend.sendto(data, (apocWifi , measurementPort + 20))
        time.sleep(1)
    except:
        print('sometthing went with wrong  broadcasting')

    socketSend.close()
    print('Sender Disconnected')

def giveStatus(ip):
    backlog = 1  # how many connections to accept
    maxsize = 28
    server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    binded = False
    while not binded:
        try:
            server.bind((ip,statusPort))
            binded = True
        except:
            print('- Give Status -- binding failed')
            binded = False
            time.sleep(20)
    server.listen(1)
    while running:
        print('--- waiting for a connection')
        try:
            connection, client_address = server.accept()
            print('------ Connection coming from ' + str(client_address))

            code = struct.unpack('i',connection.recv(4))[0]
            print('------ code : '+ str(code))
            if code == requestStatusCode:
                data = struct.pack('i', sendStatusCode)
                try:
                    connection.sendall(data)
                except:
                    print('sending did not work :/ but better not break everything')
        except:
            pass

def requestStatus(ip):
    status = False
    socketClient = socket.socket()
    socketClient.settimeout(1)
    socketClient.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    connect = 0
    fishN=0
    print('--- Connection to ' + str(ip))

    try:
        socketClient.connect((ip, statusPort))
        print('--- Connected to ' + str(ip))
        try:
            data = struct.pack('i', requestStatusCode)
            socketClient.sendall(data)
            code = struct.unpack('ii',socketClient.recv(8))[0]
            if code[0] == sendStatusCode:
                status = code[1]
        except:
            status = 0
        finally:
            socketClient.shutdown(socket.SHUT_RDWR)
            socketClient.close()

    except:
        pass
        #print('--- connection failed')
    return status

def droneComm(ip,dronePort = droneCommLeftPort,code = requestStatusCode,dataSend = []):
    status = 0
    socketClient = socket.socket()
    socketClient.settimeout(1)
    socketClient.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    connect = 0
    fishN=0
    print('--- Connection to ' + str(ip))

    try:
        socketClient.connect((ip, dronePort))
        print('--- Connected to ' + str(ip))
        print('Sending the code : '+str(code))
        try:
            if code == requestStatusCode:
                data = struct.pack('i', code)
                socketClient.sendall(data)
                dataRec = struct.unpack('ii',socketClient.recv(8))
                print(dataRec)
                code =dataRec[0]
                status = dataRec[1]
            if code in startCode:
                data = struct.pack('i', code)
                socketClient.sendall(data)
                dataRec = struct.unpack('i',socketClient.recv(4))
                status =dataRec[0]
            if code in updateVisionCode:
                data = struct.pack('i', code)
                socketClient.sendall(data)
                data = struct.pack('dddd', dataSend[0],dataSend[1],dataSend[2],dataSend[3])
                socketClient.sendall(data)
                dataRec = struct.unpack('i',socketClient.recv(4))
                status =dataRec[0]
            if code in updateParameters:
                data = struct.pack('i', code)
                socketClient.sendall(data)
                data = struct.pack('dddddddd', dataSend[0],dataSend[1],dataSend[2],dataSend[3],dataSend[4],dataSend[5],dataSend[6],dataSend[7])
                socketClient.sendall(data)
                dataRec = struct.unpack('i',socketClient.recv(4))
                status =dataRec[0]

        finally:
            socketClient.shutdown(socket.SHUT_RDWR)
            socketClient.close()

    except:
        print('--- connection failed')
        pass
    return status

max_stars_repo_name: RnoB/droneSwarm

max_issues_repo_name: RnoB/droneSwarm

max_forks_repo_name: RnoB/droneSwarm


text:
import cv2
import numpy as np
from cv2 import aruco

from constants import *

if __name__ == '__main__':

    cap = cv2.VideoCapture(0)
    if not (cap.isOpened()):
        print("Could not open video device")
        exit()
    print("Camera found... Start configuration")
    cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1080)
    cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 810)
    print("Configuration done...")
    cv2.startWindowThread()
    cv2.namedWindow("preview")

    def rot_params_rv(rvecs):
        from math import pi, atan2, asin
        R = cv2.Rodrigues(rvecs)[0]
        roll = 180 * atan2(-R[2][1], R[2][2]) / pi
        pitch = 180 * asin(R[2][0]) / pi
        yaw = 180 * atan2(-R[1][0], R[0][0]) / pi
        rot_params = [roll, pitch, yaw]
        return rot_params

    def estimate_angle(degree):
        myList = [-180, -90, 0, 90, 180]
        x = min(myList, key=lambda x: abs(x - degree))
        if x == -180:
            x = 180
        return x

    while True:
        ret, frame = cap.read()
        # arucoDict = cv2.aruco.Dictionary_get(cv2.aruco.DICT_6X6_50)
        arucoDict = cv2.aruco.Dictionary_get(cv2.aruco.DICT_4X4_50)
        arucoParams = cv2.aruco.DetectorParameters_create()
        (corners, ids, rejected) = cv2.aruco.detectMarkers(frame, arucoDict, parameters=arucoParams)

        cam_matrix = np.array([[14492.753623188406, 0, 1024], [0, 14492.753623188406, 1224],
                               [0, 0, 1]])  # camera matrix for f = 50mm with chipsize = 0.00345 mm and 2048x2448px
        dist_coeffs = np.ndarray([0])  # distortion coefficients
        fRvec, fTvec, _obj_points = cv2.aruco.estimatePoseSingleMarkers(corners, 30.0, cam_matrix, dist_coeffs)

        if len(corners) > 0:
            angle = 0
            center = np.zeros((4, 2))
            for i in range(len(ids)):
                realCorners = corners[i].reshape((4, 2))
                center += realCorners
                (topLeft, topRight, bottomRight, bottomLeft) = realCorners
                topRight = (int(topRight[0]), int(topRight[1]))
                bottomRight = (int(bottomRight[0]), int(bottomRight[1]))
                bottomLeft = (int(bottomLeft[0]), int(bottomLeft[1]))
                topLeft = (int(topLeft[0]), int(topLeft[1]))

                line_thickness = 1
                frame = cv2.line(frame, topLeft, topRight, GREEN, line_thickness)
                frame = cv2.line(frame, topRight, bottomRight, GREEN, line_thickness)
                frame = cv2.line(frame, bottomRight, bottomLeft, GREEN, line_thickness)
                frame = cv2.line(frame, bottomLeft, topLeft, GREEN, line_thickness)

                frame = cv2.putText(frame, str(rot_params_rv(fRvec[i])[2]), (topLeft[0], topLeft[1]),
                                    cv2.FONT_HERSHEY_SIMPLEX, 0.5, RED, 1, cv2.LINE_AA)
                frame = cv2.putText(frame, str(estimate_angle(rot_params_rv(fRvec[i])[2])),
                                    (bottomRight[0], bottomRight[1]), cv2.FONT_HERSHEY_SIMPLEX, 0.5, RED, 1,
                                    cv2.LINE_AA)
                angle += rot_params_rv(fRvec[i])[2]
            if len(ids) == 4:
                center /= 4
                angle /= 4
                x = (np.sum(center, 0) / 4).astype(int)
                frame = cv2.putText(frame, str(estimate_angle(angle)), (x[0], x[1]), cv2.FONT_HERSHEY_SIMPLEX, 0.5, RED,
                                    1, cv2.LINE_AA)

        cv2.imshow('preview', frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

max_stars_repo_path: webcam rotate.py

max_stars_repo_name: neokazemi/bubble-sheet

max_issues_repo_path: webcam rotate.py

max_issues_repo_name: neokazemi/bubble-sheet

max_forks_repo_path: webcam rotate.py

max_forks_repo_name: neokazemi/bubble-sheet


text:
"""
Test for CSVCaseRecorder and CSVCaseIterator.
"""
import glob
import os
import time
import StringIO
import tempfile
import shutil
import unittest

from numpy import array

from openmdao.lib.casehandlers.api import CSVCaseIterator, CSVCaseRecorder, \
                                          DumpCaseRecorder
from openmdao.main.datatypes.api import Array, Str, Bool, VarTree, Float
from openmdao.lib.drivers.api import SimpleCaseIterDriver
from openmdao.main.api import Assembly, Case, set_as_top, VariableTree, Component
from openmdao.test.execcomp import ExecComp
from openmdao.util.testutil import assert_raises
from openmdao.main.test.test_vartree import DumbVT
from openmdao.lib.drivers.conmindriver import CONMINdriver

class TestContainer(VariableTree):

    dummy1 = Float(desc='default value of 0.0') #this value is being grabbed by the optimizer
    dummy2 = Float(11.0)

class TestComponent(Component):

    dummy_data = VarTree(TestContainer(), iotype='in')
    x = Float(iotype='out')

    def execute(self):
        self.x = (self.dummy_data.dummy1-3)**2 - self.dummy_data.dummy2

class TestAssembly(Assembly):

    def configure(self):
        self.add('dummy_top', TestContainer())
        self.add('comp', TestComponent())
        self.add('driver', CONMINdriver())

        self.driver.workflow.add(['comp'])
        #self.driver.iprint = 4 #debug verbosity
        self.driver.add_objective('comp.x')
        self.driver.add_parameter('comp.dummy_data.dummy1',
                                  low=-10.0, high=10.0)

class TestCase(unittest.TestCase):
    def setUp(self):
        self.filename = "openmdao_test_csv_case_iterator.csv"
        self.startdir = os.getcwd()
        self.tempdir = tempfile.mkdtemp(prefix='test_csv-')
        os.chdir(self.tempdir)

    def tearDown(self):
        for recorder in self.top.recorders:
            recorder.close()
        os.chdir(self.startdir)
        if not os.environ.get('OPENMDAO_KEEPDIRS', False):
            try:
                shutil.rmtree(self.tempdir)
            except OSError:
                pass

    def test_flatten(self):
        self.top = set_as_top(TestAssembly())
        self.top.recorders = [CSVCaseRecorder(filename=self.filename)]
        self.top.run()
        cases = [case for case in self.top.recorders[0].get_iterator()]
        end_case = cases[-1]
        #end_case.get_input('comp.dummy_data.dummy1')
        self.assertAlmostEqual(end_case.get_input('comp.dummy_data.dummy1'), 3.0, 1) #3.0 should be minimum

    def test_inoutCSV(self):
        self.top = set_as_top(TestAssembly())
        self.top.recorders = [CSVCaseRecorder(filename=self.filename)]
        self.top.run()

        # now use the CSV recorder as source of Cases
        cases = [case for case in self.top.recorders[0].get_iterator()]
        driver = self.top.add('driver', SimpleCaseIterDriver())
        Case.set_vartree_inputs(self.top.driver, cases)

        sout = StringIO.StringIO()
        self.top.recorders = [DumpCaseRecorder(sout)]
        driver.add_responses(['comp.x',])
        self.top.run()

        # Check the results
        expected = [
         "Case:",
        "   uuid: 2983e819-9c1b-11e4-804e-20c9d0478eff",
        "   timestamp: 1421260120.252554",
        "   inputs:",
        "      comp.dummy_data.dummy1: 0.0333333393814",
        "   outputs:",
        "      _pseudo_1.out0: -2.19888892477",
        "      comp.derivative_exec_count: 0",
        "      comp.exec_count: 46",
        "      comp.itername: 4-comp",
        "      comp.x: -2.19888892477",
        "      driver.workflow.itername: 4",
        ]
        lines = sout.getvalue().split('\n')
        count = 0
        for index, line in enumerate(lines):
            if line.startswith('Case:'):
                count += 1
                if count != 4:
                    continue
                for i in range(len(expected)):
                    prefix = expected[i].split(':')[0]
                    if prefix.lstrip() in ['uuid', 'timestamp', 'comp.exec_count']:
                        # these values vary, just check proper prefix & indentation
                        self.assertTrue(lines[index+i].startswith(prefix+':'))
                    else:
                        self.assertEqual(lines[index+i], expected[i])
                break
        else:
            self.fail("couldn't find the expected Case")

    def test_inoutCSV_delimiter(self):

        # Repeat test above using semicolon delimiter and ' as quote char.

        self.top = set_as_top(TestAssembly())
        self.top.recorders = [CSVCaseRecorder(filename=self.filename,
                                              delimiter=';', quotechar="'")]
        self.top.run()

        # now use the CSV recorder as source of Cases
        cases = [case for case in self.top.recorders[0].get_iterator()]
        driver = self.top.add('driver', SimpleCaseIterDriver())
        Case.set_vartree_inputs(self.top.driver, cases)

        sout = StringIO.StringIO()
        self.top.recorders = [DumpCaseRecorder(sout)]
        driver.add_responses(['comp.x',])
        self.top.run()

        # Check the results
        expected = [
         "Case:",
        "   uuid: 2983e819-9c1b-11e4-804e-20c9d0478eff",
        "   timestamp: 1421260120.252554",
        "   inputs:",
        "      comp.dummy_data.dummy1: 0.0333333393814",
        "   outputs:",
        "      _pseudo_1.out0: -2.19888892477",
        "      comp.derivative_exec_count: 0",
        "      comp.exec_count: 46",
        "      comp.itername: 4-comp",
        "      comp.x: -2.19888892477",
        "      driver.workflow.itername: 4",
        ]

        lines = sout.getvalue().split('\n')
        count = 0
        for index, line in enumerate(lines):
            if line.startswith('Case:'):
                count += 1
                if count != 4:
                    continue
                for i in range(len(expected)):
                    prefix = expected[i].split(':')[0]
                    if prefix.lstrip() in ['uuid', 'timestamp', 'comp.exec_count']:
                        # these values vary, just check proper prefix & indentation
                        self.assertTrue(lines[index+i].startswith(prefix+':'))
                    else:
                        self.assertEqual(lines[index+i], expected[i])
                break
        else:
            self.fail("couldn't find the expected Case")

class CSVCaseRecorderTestCase(unittest.TestCase):

    def setUp(self):
        self.startdir = os.getcwd()
        self.tempdir = tempfile.mkdtemp(prefix='test_csv-')
        os.chdir(self.tempdir)

        self.top = top = set_as_top(Assembly())
        driver = top.add('driver', SimpleCaseIterDriver())
        top.add('comp1', ExecComp(exprs=['z=x+y']))
        top.add('comp2', ExecComp(exprs=['z=x+1']))
        top.connect('comp1.z', 'comp2.x')
        top.comp1.add('a_string', Str("Hello',;','", iotype='out'))
        top.comp1.add('a_array', Array(array([1.0, 3.0, 5.5]), iotype='out'))
        top.comp1.add('x_array', Array(array([1.0, 1.0, 1.0]), iotype='in'))
        top.comp1.add('b_bool', Bool(False, iotype='in'))
        top.comp1.add('vt', VarTree(DumbVT(), iotype='out'))
        top.driver.workflow.add(['comp1', 'comp2'])

        # now create some Cases
        outputs = ['comp1.z', 'comp2.z', 'comp1.a_string', 'comp1.a_array[2]']
        cases = []
        for i in range(10):
            i = float(i)
            inputs = [('comp1.x', i+0.1), ('comp1.y', i*2 + .1),
                      ('comp1.x_array[1]', 99.88), ('comp1.b_bool', True)]
            cases.append(Case(inputs=inputs, outputs=outputs))

        Case.set_vartree_inputs(driver, cases)
        driver.add_responses(sorted(outputs))

        self.filename = "openmdao_test_csv_case_iterator.csv"

    def tearDown(self):
        for recorder in self.top.recorders:
            recorder.close()
        os.chdir(self.startdir)
        if not os.environ.get('OPENMDAO_KEEPDIRS', False):
            try:
                shutil.rmtree(self.tempdir)
            except OSError:
                pass

    def test_CSVCaseIterator_read_external_file_with_header(self):

        csv_data = [
            '"comp1.x", "comp1.y", "comp2.b_string"\n',
            '33.5, 76.2, "Hello There"\n'
            '3.14159, 0, "Goodbye z"\n'
        ]

        outfile = open(self.filename, 'w')
        outfile.writelines(csv_data)
        outfile.close()

        self.top.comp2.add('b_string', Str("Hello',;','", iotype='in'))

        sout = StringIO.StringIO()
        cases = [case for case in CSVCaseIterator(filename=self.filename)]
        self.top.driver.clear_parameters()
        Case.set_vartree_inputs(self.top.driver, cases)
        self.top.recorders = [DumpCaseRecorder(sout)]
        self.top.run()

        self.assertEqual(self.top.comp1.x, 3.14159)
        self.assertEqual(self.top.comp1.y, 0.0)
        self.assertEqual(self.top.comp2.b_string, "Goodbye z")

    def test_CSVCaseIterator_read_external_file_without_header(self):

        csv_data = [
            '33.5, 76.2, "Hello There"\n'
            '3.14159, 0, "Goodbye z"\n'
        ]

        outfile = open(self.filename, 'w')
        outfile.writelines(csv_data)
        outfile.close()

        header_dict = {
            0: "comp1.x",
            1: "comp1.y",
            2: "comp2.b_string"
        }

        self.top.comp2.add('b_string', Str("Hello',;','", iotype='in'))

        sout = StringIO.StringIO()
        cases = [case for case in CSVCaseIterator(filename=self.filename,
                                                  headers=header_dict)]
        self.top.driver.clear_parameters()
        Case.set_vartree_inputs(self.top.driver, cases)
        self.top.recorders = [DumpCaseRecorder(sout)]

        self.top.run()

        self.assertEqual(self.top.comp1.x, 3.14159)
        self.assertEqual(self.top.comp1.y, 0.0)
        self.assertEqual(self.top.comp2.b_string, "Goodbye z")

    def test_sorting(self):
        # Make sure outputs are sorted

        rec = CSVCaseRecorder(filename=self.filename)
        rec.num_backups = 0
        rec.startup()
        rec.register(self, ['comp1.x', 'comp1.y', 'comp2.x', ], [])
        rec.record(self, [2.0, 4.3, 1.9], [], None, '', '')
        rec.close()

        outfile = open(self.filename, 'r')
        csv_data = outfile.readlines()
        outfile.close()

        line = '"timestamp","/INPUTS","comp1.x","comp1.y","comp2.x",' \
               '"/OUTPUTS","/METADATA","uuid","parent_uuid","msg"\r\n'
        self.assertEqual(csv_data[0], line)
        line = '"",2.0,4.3,1.9,"","","","",""\r\n'
        self.assertTrue(csv_data[1].endswith(line))

    def test_CSVCaseRecorder_messages(self):
        rec = CSVCaseRecorder(filename=self.filename)
        rec.startup()
        rec.register(self, ['comp1.x', 'comp1.y', 'comp2.x'], [])
        rec.record(self, [2.0, 4.3, 1.9], [], None, '', '')
        try:
            rec.record(self, [2.0, 1.9], [], None, '', '')
        except Exception as err:
            self.assertEqual(str(err),
                             "number of data points (9) doesn't match"
                             " header size (10) in CSV recorder")
        else:
            self.fail("Exception expected")
        finally:
            rec.close()

        ## BAN - took this test out because only types with a flattener function
        ##       will be returned by the Case, so incompatible types just won't
        ##       be seen by the CSVCaseRecorder at all.  Need to discuss with
        ##       users (and Ken) to see if this is reasonable.
        #self.top.comp2.add('a_slot', Slot(object, iotype='in'))
        #self.top.recorders = [CSVCaseRecorder(filename=self.filename)]

        #case = Case(inputs=[('comp2.a_slot', None)])
        #try:
            #self.top.recorders[0].record(case)
        #except ValueError, err:
            #msg = "CSV format does not support variables of type <type 'NoneType'>"
            #self.top.recorders[0].close()
            #self.assertEqual(msg, str(err))
        #else:
            #self.top.recorders[0].close()
            #self.fail('ValueError Expected')

    def test_close(self):
        self.top.recorders = [CSVCaseRecorder(filename=self.filename)]
        self.top.recorders[0].num_backups = 0
        self.top.run()
        outputs = ['comp1.z', 'comp2.z', 'comp1.a_string',
                   'comp1.a_array[2]', 'driver.workflow.itername']
        inputs = ['comp1.x', 'comp1.y', 'comp1.x_array[1]', 'comp1.b_bool']
        self.top.recorders[0].register(self, inputs, outputs)
        outputs = [0, 0, 'world', 0, '1']
        inputs = [0.1, 2 + .1, 99.88, True]
        code = "self.top.recorders[0].record(self, inputs, outputs, None, '', '')"
        assert_raises(self, code, globals(), locals(), RuntimeError,
                      'Attempt to record on closed recorder')

    def test_csvbackup(self):

        # Cleanup from any past failures
        parts = self.filename.split('.')
        backups = glob.glob(''.join(parts[:-1]) + '_*')
        for item in backups:
            os.remove(item)

        self.top.recorders = [CSVCaseRecorder(filename=self.filename)]

        # Run twice, two backups.
        self.top.recorders[0].num_backups = 2
        self.top.run()
        # Granularity on timestamp is 1 second.
        time.sleep(1)
        self.top.run()
        backups = glob.glob(''.join(parts[:-1]) + '_*')
        self.assertEqual(len(backups), 2)

        # Set backups to 1 and rerun. Should delete down to 1 backup.
        self.top.recorders[0].num_backups = 1
        self.top.run()
        backups = glob.glob(''.join(parts[:-1]) + '_*')
        self.assertEqual(len(backups), 1)

        for item in backups:
            os.remove(item)
        backups = glob.glob(''.join(parts[:-1]) + '_*')

        self.top.recorders[0].num_backups = 0

    def test_iterate_twice(self):

        self.top.recorders = [CSVCaseRecorder(filename=self.filename)]
        self.top.recorders[0].num_backups = 0
        self.top.run()

        data = self.top.recorders[0].get_iterator()

        for case in data:
            keys1 = case.keys()

        for case in data:
            keys2 = case.keys()

        self.assertEqual(keys1, keys2)

    def test_save(self):
        # Check that a recorder can be saved to an egg.
        self.top.recorders = [CSVCaseRecorder(filename=self.filename)]
        self.top.recorders[0].num_backups = 0
        self.top.recorders[0].startup()
        try:
            self.top.save_to_egg('top', '1')
        finally:
            for name in glob.glob('top*.egg'):
                os.remove(name)

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

max_stars_repo_name: mjfwest/OpenMDAO-Framework

max_issues_repo_name: jcchin/OpenMDAO-Framework

max_forks_repo_name: jcchin/OpenMDAO-Framework

reason: from numpy


text:
import argparse
import ntpath
import os

import matplotlib.pyplot as plt
import numpy as np
from imutils import paths
from sklearn.metrics import classification_report
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelBinarizer
from tensorflow.keras.applications import MobileNetV2
from tensorflow.keras.applications.mobilenet_v2 import preprocess_input
from tensorflow.keras.layers import AveragePooling2D
from tensorflow.keras.layers import Dense
from tensorflow.keras.layers import Dropout
from tensorflow.keras.layers import Flatten
from tensorflow.keras.layers import Input
from tensorflow.keras.models import Model
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.preprocessing.image import img_to_array
from tensorflow.keras.preprocessing.image import load_img
from tensorflow.keras.utils import to_categorical

def main():
    # 初始化Arguments
    ap = argparse.ArgumentParser()
    ap.add_argument("-d", "--dataset", required=True, help="path to input dataset")
    ap.add_argument("-p", "--plot", type=str, default="plot.png", help="path to output loss/accuracy plot")
    ap.add_argument("-m", "--model", type=str, default="mask_detector.model",
                    help="path to output face mask detector model")
    args = vars(ap.parse_args())

    # 初始化訓練用參數與Batch Size
    INIT_LR = 1e-4
    EPOCHS = 20
    BS = 32

    # 載入圖片
    print("[INFO] loading images...")
    imagePaths = list(paths.list_images(args["dataset"]))
    data = []
    labels = []

    # 將訓練圖片進行前處理與建立訓練data
    for imagePath in imagePaths:
        label = ntpath.normpath(imagePath).split(os.path.sep)[-2]

        # 注意這裡將圖片轉成224 x 224，與MobileNetV2模型需要的Input一樣大小
        image = load_img(imagePath, target_size=(224, 224))
        image = img_to_array(image)
        image = preprocess_input(image)

        data.append(image)
        labels.append(label)

    data = np.array(data, dtype="float32")
    labels = np.array(labels)

    # 將類別encoding成數值方便訓練
    lb = LabelBinarizer()
    labels = lb.fit_transform(labels)
    labels = to_categorical(labels)

    # 切分訓練資料與測試資料
    (trainX, testX, trainY, testY) = train_test_split(data, labels, test_size=0.20, stratify=labels, random_state=9527)

    # 做Data Argumentation，強化模型的辨識能力
    aug = ImageDataGenerator(
        rotation_range=20,
        zoom_range=0.15,
        width_shift_range=0.2,
        height_shift_range=0.2,
        shear_range=0.15,
        horizontal_flip=True,
        fill_mode="nearest")

    # 載入模型，去除模型最後一層 (等等要改為我們要辨識的"兩種類別")
    baseModel = MobileNetV2(weights="imagenet", include_top=False, input_tensor=Input(shape=(224, 224, 3)))

    # 組合自定義的最後層
    headModel = baseModel.output
    headModel = AveragePooling2D(pool_size=(7, 7))(headModel)
    headModel = Flatten(name="flatten")(headModel)
    headModel = Dense(128, activation="relu")(headModel)
    headModel = Dropout(0.5)(headModel)
    headModel = Dense(2, activation="softmax")(headModel)

    # 建立模型
    model = Model(inputs=baseModel.input, outputs=headModel)

    # 確認模型只有我們新增的最後層可以訓練 (transfer learning)
    for layer in baseModel.layers:
        layer.trainable = False

    # 編譯模型
    print("[INFO] compiling model...")
    opt = Adam(lr=INIT_LR, decay=INIT_LR / EPOCHS)
    model.compile(loss="binary_crossentropy", optimizer=opt, metrics=["accuracy"])

    # 開始訓練
    print("[INFO] training head...")
    H = model.fit(
        aug.flow(trainX, trainY, batch_size=BS),
        steps_per_epoch=len(trainX) // BS,
        validation_data=(testX, testY),
        validation_steps=len(testX) // BS,
        epochs=EPOCHS)

    # 使用測試資料驗證模型準確率
    print("[INFO] evaluating network...")
    predIdxs = model.predict(testX, batch_size=BS)
    predIdxs = np.argmax(predIdxs, axis=1)

    # 印出測試結果
    print(classification_report(testY.argmax(axis=1), predIdxs, target_names=lb.classes_))

    # 儲存模型
    print("[INFO] saving mask detector model...")
    model.save(args["model"], save_format="h5")

    # 劃出訓練結果
    N = EPOCHS
    plt.style.use("ggplot")
    plt.figure()
    plt.plot(np.arange(0, N), H.history["loss"], label="train_loss")
    plt.plot(np.arange(0, N), H.history["val_loss"], label="val_loss")
    plt.plot(np.arange(0, N), H.history["accuracy"], label="train_acc")
    plt.plot(np.arange(0, N), H.history["val_accuracy"], label="val_acc")
    plt.title("Training Loss and Accuracy")
    plt.xlabel("Epoch #")
    plt.ylabel("Loss/Accuracy")
    plt.legend(loc="lower left")
    plt.savefig(args["plot"])

if __name__ == '__main__':
    main()

max_stars_repo_name: saivirtue/easy-eye-app

max_issues_repo_name: saivirtue/easy-eye-app

max_forks_repo_name: saivirtue/easy-eye-app


text:
from argparse import ArgumentParser
import time
from datetime import datetime
import os
import shutil
import logging
import random
import json
import mkl
import multiprocessing
import copy

import ssl
ssl._create_default_https_context = ssl._create_unverified_context

import torch
from torch import nn
from torch import optim

import numpy as np
import matplotlib.pyplot as plt

from model import DataLoader, SharedConvNet, NoShareConvNet

def get_config():
    parser = ArgumentParser()
    parser.add_argument("--hidden_channels", type=int, default=101)
    parser.add_argument("--out_channels", type=int, default=225)
    parser.add_argument("--kernel_size1", type=int, default=3)
    parser.add_argument("--kernel_size2", type=int, default=3)
    parser.add_argument("--kernel_size3", type=int, default=3)
    parser.add_argument("--intermediate_dim", type=int, default=512)
    parser.add_argument("--batch_norm", type=int, default=1)
    parser.add_argument("--pooling", type=int, default=1)
    parser.add_argument("--resnet", type=int, default=1)
    parser.add_argument("--dropout", type=float, default=0.5)
    parser.add_argument("--convdropout", type=float, default=0.0)
    parser.add_argument("--model_name", type=str, default='SharedConvNet')

    parser.add_argument("--batch_size", type=int, default=200)
    parser.add_argument("--nb_epochs", type=int, default=100)
    parser.add_argument("--lr", type=float, default=1e-3)
    parser.add_argument("--gamma", type=float, default=0.5)
    parser.add_argument("--lr_milestones", type=str, default="800,1200,1800,2500,4000,6000,8000")
    parser.add_argument("--save_dir", type=str, default='none')
    parser.add_argument("--seed", type=int, default=1)
    parser.add_argument("--float64", type=int, default=0)
    parser.add_argument("--device", type=str, default="cuda")

    args = parser.parse_args()
    if args.save_dir.lower() == "none":
        args.save_dir='./results/'+args.model_name+'/'+now.strftime("%m-%d-%Y-%H-%M-%S")+\
            f'{args.hidden_channels}_{args.out_channels}_{args.kernel_size1}_{args.kernel_size2}_{args.kernel_size3}_{args.intermediate_dim}_{args.batch_norm}_{args.pooling}_{args.resnet}_{args.dropout}_{args.convdropout}'
    else:
        args.save_dir = "./results/" + args.save_dir

    if args.float64:
        torch.set_default_dtype(torch.float64)
    else:
        torch.set_default_dtype(torch.float32)

    os.makedirs(args.save_dir, exist_ok=True)
    with open(args.save_dir + '/config.json', 'w') as f:
        json.dump(args.__dict__, f, indent=2)
    file_handler = logging.FileHandler(os.path.join(args.save_dir, "log.txt"), mode="w")
    if args.save_dir:
        for filename in os.listdir('./'):
            if '.sh' in filename or \
               '.swb' in filename or \
               '.py' in filename:
                   if filename == '.pylint.d':
                       continue
                   if '__pycache__' in filename:
                       continue
                   shutil.copy(filename, args.save_dir)
        shutil.copytree('./model', args.save_dir+'/model', dirs_exist_ok=True, ignore=shutil.ignore_patterns('*__pycache__*'))
        logger.addHandler(file_handler)

    return args

def set_seed(seed):
    """set random seed
    """
    logging.info(f'random {seed=}')
    torch.backends.cudnn.deterministic = False
    torch.backends.cudnn.benchmark = True
    torch.manual_seed(seed)
    torch.cuda.manual_seed_all(seed)
    np.random.seed(seed)
    random.seed(seed)

if __name__ == '__main__':

    logger = logging.getLogger()
    logging.basicConfig(level=logging.INFO, format='%(message)s')
    now = datetime.now()

    config = get_config()
    set_seed(config.seed)

    device = torch.device(config.device) if torch.cuda.is_available() else torch.device('cpu')

    loss_writer = open(config.save_dir+"/loss.txt", 'a+')
    accu_writer = open(config.save_dir+"/accuracy.txt", 'a+')

    dataloader = DataLoader(config.batch_size)
    trainloader, testloader, classes = dataloader()

    model = globals()[config.model_name](3, config.hidden_channels, config.out_channels,
                                         config.kernel_size1, config.kernel_size2, config.kernel_size3,
                                         config.intermediate_dim, 10, config.batch_norm,
                                         config.pooling, config.resnet, config.dropout, config.convdropout)
    logging.info(f'number of parameters: {model.count_params()}')
    model = model.to(device)
    model.train()
    criterion = nn.CrossEntropyLoss()
    optimizer = optim.AdamW(model.parameters(), config.lr)
    scheduler = optim.lr_scheduler.MultiStepLR(optimizer, milestones=list(map(int, config.lr_milestones.split(','))), gamma=config.gamma)

    for epoch in range(config.nb_epochs):
        model.train()
        running_loss = 0.
        for i, data in enumerate(trainloader):
            inputs, labels = data
            optimizer.zero_grad()
            outputs = model(inputs.to(device))
            loss = criterion(outputs, labels.to(device))
            loss.backward()
            optimizer.step()
            scheduler.step()
            running_loss += loss.item()
            if (i+1) % 25 == 0:
                logging.info(f'epoch={epoch+1}, i={i+1}, loss={running_loss/25}')
                for item in [epoch+1, i+1, running_loss]:
                    loss_writer.write("%s " % item)
                loss_writer.write('\n')
                running_loss = 0.0

        model.eval()

        train_correct = 0
        train_total = 0.

        with torch.no_grad():
            for i, data in enumerate(trainloader):
                if i == 10000:
                    break
                images, labels = data
                outputs = model(images.to(device))
                _, predicted = torch.max(outputs.data, 1)
                train_total += labels.shape[0]
                train_correct += (predicted == labels.to(device)).sum().item()
        train_accu = train_correct / train_total
        logging.info(f'epoch={epoch+1}, train accuracy={train_accu * 100}%')

        test_correct = 0
        test_total = 0.

        with torch.no_grad():
            for data in testloader:
                images, labels = data
                outputs = model(images.to(device))
                _, predicted = torch.max(outputs.data, 1)
                test_total += labels.shape[0]
                test_correct += (predicted == labels.to(device)).sum().item()
        test_accu = test_correct / test_total
        logging.info(f'epoch={epoch+1}, test accuracy={test_accu * 100}%')

        for item in [epoch+1, train_accu, test_accu]:
            accu_writer.write("%s " % item)
        accu_writer.write('\n')

        loss_writer.flush()
        accu_writer.flush()

    torch.save(model.state_dict(), config.save_dir+"/model.pt")


text:
import cv2
import numpy as np
"""
Shi-Tomasi角点检测：
    Harris角点检测算的稳定性和K有关，而K是个经验值，不好设定最佳值。
    而Shi-Tomasi是Harris角点检测的改进。
    goodFeaturesToTrack(img, maxCorners, ..)
    maxCorners:  角点的最大数，值为0表示无限制
    qualityLevel: <1.0的正数，一般在0.01~0.1之间
    minDistance：角之间最小欧式距离，忽略小于此距离的点
    mask: 感兴趣的区域
    blockSize: 检测窗口
    useHarrisDetector: 是否使用Harris算法
    k: 默认是0.04
"""
img = cv2.imread(r'E:\PycharmProjects\funnyPython\opencv_py\data\imgs\chess.png')
#灰度化
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

corners = cv2.goodFeaturesToTrack(gray_img, 0, 0.01, 10)
corners = np.int0(corners)
for i in corners:
    x, y = i.ravel() # 一维
    cv2.circle(img, (x, y), 3, (255, 0, 0), -1)

cv2.imshow('shi-tomasi', img)
cv2.waitKey(0)

max_stars_repo_name: yinleiCoder/interesting-python

max_issues_repo_name: yinleiCoder/interesting-python

max_forks_repo_name: yinleiCoder/interesting-python


text:
import os
from time import strftime
import copy
import numpy as np
if "DISPLAY" not in os.environ:
    import matplotlib
    print("No DISPLAY found. Switching to noninteractive matplotlib backend...")
    print("Old backend is: {}".format(matplotlib.get_backend()))
    matplotlib.use('Agg')
    print("New backend is: {}".format(matplotlib.get_backend()))
import matplotlib.pyplot as plt
import matplotlibex as plx
import ml.gptheano.vecgpdm.multiprimitivemodel as mdl
import ml.gptheano.vecgpdm.kernels as krn
import numerical.numpytheano.theanopool as tp
import ml.gptheano.vecgpdm.equations as vceq
import numerical.numpytheano as nt
import matplotlibex.mlplot as plx
import bvhrwroutines.bvhrwroutines as br
from ml.gptheano.vecgpdm.enums import *
from validation.common import *
import argparse

validation_skip = 5

def load_bvh_motion_2(bvhfilename):
    motion = br.BVH_Bridge()
    motion.read_BVH(bvhfilename)
    motion.add_part("upper", ["pelvis_spine1",
                              "spine1",
                              "neck",
                              "spine2",
                              "left_manubrium",
                              "left_clavicle",
                              "left_humerus",
                              "left_radius",
                              "right_manubrium",
                              "right_clavicle",
                              "right_humerus",
                              "right_radius",
                              ])
    motion.add_part("lower", ["pelvis",
                              "pelvis_right_femur",
                              "right_femur_tibia",
                              "right_tibia_foot",
                              "pelvis_left_femur",
                              "left_femur_tibia",
                              "left_tibia_foot",
                              ])
    return motion

def load_bvh_motion_4(bvhfilename):
    motion = br.BVH_Bridge()
    motion.read_BVH(bvhfilename)
    motion.add_part("head", ["neck",
                             "spine2",
                             ])
    motion.add_part("left",  ["left_manubrium",
                              "left_clavicle",
                              "left_humerus",
                              "left_radius",
                              ])
    motion.add_part("right", ["right_manubrium",
                              "right_clavicle",
                              "right_humerus",
                              "right_radius",
                              ])
    motion.add_part("lower", ["pelvis",
                              "pelvis_right_femur",
                              "right_femur_tibia",
                              "right_tibia_foot",
                              "pelvis_left_femur",
                              "left_femur_tibia",
                              "left_tibia_foot",
                              "pelvis_spine1",
                              "spine1",
                              ])
    return motion

def load_bvh_motion_3(bvhfilename):
    motion = br.BVH_Bridge()
    motion.read_BVH(bvhfilename)
    motion.add_part("left",  ["left_manubrium",
                              "left_clavicle",
                              "left_humerus",
                              "left_radius",
                              ])
    motion.add_part("right", ["right_manubrium",
                              "right_clavicle",
                              "right_humerus",
                              "right_radius",
                              ])
    motion.add_part("lower", ["pelvis",
                              "pelvis_right_femur",
                              "right_femur_tibia",
                              "right_tibia_foot",
                              "pelvis_left_femur",
                              "left_femur_tibia",
                              "left_tibia_foot",
                              "pelvis_spine1",
                              "spine1",
                              "spine2",
                              "neck",
                              ])
    return motion

def run_multiprimitive_CGPDM_crossvalidation(training, validation, settings, bvhpartitioner=None):

    def add_segments(trial, mps, starts_ends, hold_id=None):
        ses = copy.deepcopy(starts_ends)
        if hold_id is not None:
            # Put the held-out primer at the beginning
            ses[hold_id][1] = ses[hold_id][0] + settings["validation_seed_size"]
        print(ses)
        for start, end in ses:
            for mp in mps:
                trial.add_segment(mdl.MPSegment(mp_type=mp, start=start, end=end))

    hold_id = settings["hold"]
    dir_data = "../../../data/phasespace"
    dir_out = settings["directory"]
    completed_filename = dir_out + "/completed.pkl"
    if os.path.exists(completed_filename):
        return

    if not os.path.exists(dir_out):
        os.makedirs(dir_out)

    #ns = tp.NumpyVarPool()
    ns = tp.TheanoVarPool()
    lvm_kern_type = krn.RBF_ARD_Kernel
    dyn_kern_type = krn.RBF_ARD_Kernel_noscale
    load_motion = load_bvh_motion_2
    dataset = mdl.Dataset(lvm_kern_type=lvm_kern_type,
                          dyn_kern_type=dyn_kern_type,
                          observed_mean_mode=mdl.ObservedMeanMode.per_part,
                          estimation_mode=EstimationMode.ELBO, ns=ns)

    Q = [3, 3]  # latent space dims
    lvm_M = settings["lvm_Ms"][0]
    dyn_M = settings["dyn_Ms"][0]
    motion = load_motion(dir_data + "/2018.01.30_olaf/walking-01_skeleton.bvh")  # just to load the skeleton
    #motion = load_motion(dir_data + "/2016.05.03_bjoern/009_skeleton.bvh")  # just to load the skeleton
    y, parts_IDs = motion.get_all_parts_data_and_IDs()
    part_indexes = br.IDs_to_indexes(parts_IDs)
    pt_upper = dataset.create_part_type("upper", y_indexes=part_indexes[0], Q=Q[0], M=lvm_M)
    pt_lower = dataset.create_part_type("lower", y_indexes=part_indexes[1], Q=Q[1], M=lvm_M)
    mpt_upper_walk = dataset.create_mp_type("walk", pt_upper)
    mpt_upper_wave = dataset.create_mp_type("wave", pt_upper, remove_trend=True)
    mpt_lower_walk = dataset.create_mp_type("walk", pt_lower)

    motion = load_motion(dir_data + "/2018.01.30_olaf/walking-01_skeleton.bvh")  # walking wall-to-wall
    #motion = load_motion(dir_data + "/2016.05.03_bjoern/003_skeleton.bvh")  # walking wall-to-wall
    y, parts_IDs = motion.get_all_parts_data_and_IDs()
    trial = dataset.create_trial(Y=y)
    mps1 = [mpt_upper_walk, mpt_lower_walk]
    starts_ends=[   [609, 713],
                    [1144, 1244],
                    [1672, 1774],
                    [2245, 2342],
                    [2783, 2882],
                    [3339, 3437],
                    [3859, 3959],
                    [5622, 5722],
                    [7126, 7226],
                    [7637, 7737],
                ]
    add_segments(trial, mps=mps1, starts_ends=starts_ends, hold_id=hold_id)
    held_walk_walk = y[starts_ends[hold_id][0]:starts_ends[hold_id][1], :]

    motion = load_motion(dir_data + "/2018.01.30_olaf/protesting_walking_2arms-01_skeleton.bvh")  # walking and both-arms-waving, side-to-wall
    #motion = load_motion(dir_data + "/2016.05.03_bjoern/011_skeleton.bvh")  # walking and both-arms-waving, side-to-wall
    y, parts_IDs = motion.get_all_parts_data_and_IDs()
    trial = dataset.create_trial(Y=y)
    mps2 = [mpt_upper_wave, mpt_lower_walk]
    starts_ends=[   [664, 743],
                    [920, 1001],
                    [1209, 1287],
                    [1477, 1556],
                    [1752, 1830],
                    [2028, 2108],
                    [2294, 2372],
                    [2532, 2610],
                    [2787, 2867],
                    [3024, 3103],
                ]
    add_segments(trial, mps=mps2, starts_ends=starts_ends, hold_id=hold_id)
    held_wave_walk = y[starts_ends[hold_id][0]:starts_ends[hold_id][1], :]

    directory = dir_out
    dataset.init_pieces()
    dataset.init_x()
    dataset.init_aug_z(dyn_M=dyn_M)
    dataset.init_psi_stats()

    elbo_mode = mdl.ELBOMode.full
    dataset.init_elbo(elbo_mode)
    #print(dataset.get_elbo_value())
    dataset.precalc_posterior_predictive()
    mdl.save_plot_latent_space(dataset, dir_out, "initial")
    t0 = time.time()
    if not settings["dry_run"]:
        mdl.optimize_blocked(dataset, niterations=settings["maxrun"], maxiter=settings["maxiter"], print_vars=True, save_directory=directory)
    t1 = time.time()
    dataset.save_state_to(directory + "/step_1.pkl")
    dataset.save_state_to(directory + "/step_2.pkl")

    # Generate motion, make plots
    elbo_mode = mdl.ELBOMode.full
    dataset.init_elbo(elbo_mode)
    dataset.load_state_from(directory + "/step_2.pkl")
    dataset.precalc_posterior_predictive()
    mpss = [mps1, mps2]
    held_data = [held_walk_walk, held_wave_walk]
    for i, mps in enumerate(mpss):
        name = "final_({})".format(".".join([mp.name for mp in mps]))
        mdl.save_plot_latent_space(dataset, directory, name)
        mdl.save_plot_latent_vs_generated(dataset, mps, directory, name)
        mdl.save_plot_training_vs_generated(dataset, mps, directory, name)
        x_path = dataset.run_generative_dynamics(mps, nsteps=300, start_pice_id=hold_id)
        y_path = dataset.lvm_map_to_observed(mps, x_path)
        motion.set_all_parts_data(np.hstack(y_path))
        motion.write_BVH("{}/{}.bvh".format(directory, name))

        # Compute the crossvalidation errors
        held_y = held_data[i]
        nsteps = held_y.shape[0]
        x_path = dataset.run_generative_dynamics(mps, nsteps=nsteps, start_pice_id=hold_id)
        y_path = dataset.lvm_map_to_observed(mps, x_path)
        errors = compute_errors(observed=held_y,
                        predicted=np.hstack(y_path))
        errors["ELBO"] = dataset.get_elbo_value()
        errors["timing"] = t1-t0
        errors["settings"] = settings
        # Write the errors
        with open(dir_out + "/errors_mps({}).pkl".format(i), "wb") as filehandle:
            pickle.dump(errors, filehandle)

    with open(completed_filename, "wb") as filehandle:
        pickle.dump("completed", filehandle)

def create_model_iterator(id=1):
    miter = ModelIterator()
    # Dummy recording
    miter.load_recording(
        ds.Recordings.exp3_walk,
        bodypart_motiontypes=[(ds.BodyParts.Upper, ds.MotionType.WALK_PHASE_ALIGNED),
                              (ds.BodyParts.Lower, ds.MotionType.WALK_PHASE_ALIGNED)],
        max_chunks=10)
    miter.settings = {"validation_seed_size": 4,  # number of frames to be included into the training set
                      "dry_run": False,  # skip optimization completely
                      "Qs": [3] * miter.nparts,
                      "dyn_Ms": None,
                      "lvm_Ms": None,
                      "optimize_joint": False,
                      "maxrun": 5,
                      "maxiter": 300,
                      "hold": None,}
    miter.params_range = [("hold", range(10)),
                          ("dyn_Ms", [(i,)*miter.nparts for i in [8]]),
                          ("lvm_Ms", [(i,)*miter.nparts for i in [10]]),]
    miter.directory = "./multiprimitive_{}".format(id)
    return miter

def analyze_stats():
    def firts_values(llst):
        return [lst[0] for lst in llst]

    for err_id in [0, 1]:
        stats = ErrorStatsReader()
        stats.errorsfilename = "/errors_mps({}).pkl".format(err_id)
        miter = create_model_iterator()
        miter.iterate_all_settings(stats.read_learned_errors)
        stats.params_range = miter.params_range
        print(stats.errs)

        # Analysis
        save_dir = "{}/statistics-mps_({})".format(miter.directory, err_id)
        if not os.path.exists(save_dir):
            os.makedirs(save_dir)

        statsfile = open(save_dir + "/stats-info.txt", "w")
        # vCGPDM ELBO and MSE plots
        for key in ["ELBO", "MSE", "WRAP_DYN", "WRAP_PATH", "timing"]:
            #print(stats.errs)
            errs_axes = stats.params_range
            errs_value = stats.to_tensor(key=key, filter=None)
            #errs_axes, errs_value = select_by(errs_axes, errs_value, [("estimation_mode", EstimationMode.ELBO)])

            axes, data = errs_axes, np.squeeze(errs_value)
            means, std = mean_std(axes, data, alongs=["hold"])
            fig = plt.figure(figsize=(6, 5))
            plt.plot(0*data, data, "x", markersize=10)
            plt.errorbar(0*means, means, std, fmt='--o', capsize=2)
            plt.xlabel("dyn_Ms")
            plt.ylabel(key)
            plt.title(key)
            plot_dir = save_dir
            plt.savefig("{}/vCGPDM-{}.pdf".format(plot_dir, key))
            plt.close(fig)
            statsfile.write("Key: {}, means: {}, std: {}\n".format(key, means, std))
        statsfile.close()

if __name__ == "__main__":
    parser = argparse.ArgumentParser(
    prog=__file__,
    description="""\
        Train multiprimitive vCGPDM crossvalidation models""",
    formatter_class=argparse.RawTextHelpFormatter)
    parser.add_argument("--v", action='version', version='%(prog)s 0.1')
    parser.add_argument("--i", type=int, default=None, help="""\
        run only i-th model. i in 1..IMAX. None to run all models. 0 to collect statistics""")
    args = parser.parse_args()
    i_model = args.i

    if i_model == None:
        miter = create_model_iterator()
        miter.iterate_all_settings(run_multiprimitive_CGPDM_crossvalidation)
    elif i_model == 0:
        analyze_stats()
    elif i_model <= 10:
        miter = create_model_iterator()
        miter.iterate_all_settings(run_multiprimitive_CGPDM_crossvalidation, i_model=i_model-1)

max_stars_repo_name: dmytrov/gaussianprocess

max_issues_repo_name: dmytrov/gaussianprocess

max_forks_repo_name: dmytrov/gaussianprocess


text:
#!/usr/bin/env python

""""""

# ============================================================================== #
#                                                                                #
#   (c) Copyright, 2014 University Corporation for Atmospheric Research (UCAR).  #
#       All rights reserved.                                                     #
#                                                                                #
#       File: $RCSfile: simulation_reader.py,v $                                           #
#       Version: $Revision: 1.1 $  Dated: $Date: 2014/06/18 20:19:50 $           #
#                                                                                #
# ============================================================================== #

import numpy
import os
import sys
import time
from optparse import OptionParser

class SimulationReader(object):

    Token_list = ["Size:", "File Name:", "VIN:", "Date:", "Air Temp:", "Barometer:", "Coolant Temp:", "Odometer:"]
    In_missing = 10001
    In_missing_string = '10001'
    Out_missing = -9999
    Out_missing_string = '-9999'

    def __init__(self, size):

        self.size = size
        self.heading = numpy.zeros(size, dtype=numpy.float32) + 180
        self.abs = numpy.zeros(size, dtype=numpy.int8) + 2
        self.air_temperature2 = numpy.zeros(size, dtype=numpy.float32) + 20
        self.brake = numpy.zeros(size, dtype=numpy.int8) + 15
        self.latitude = numpy.zeros(size, dtype=numpy.float32) + 35
        self.longitude = numpy.zeros(size, dtype=numpy.float32) + 190
        self.dewpoint = numpy.zeros(size, dtype=numpy.float32) + 20
        self.elevation = numpy.zeros(size, dtype=numpy.float32) + 1000
        self.humidity = numpy.zeros(size, dtype=numpy.float32) + 60
        self.lights = numpy.zeros(size, dtype=numpy.int8) + 64
        self.speed_mps = numpy.zeros(size, dtype=numpy.float32) + 20
        self.accel_lat = numpy.zeros(size, dtype=numpy.float32) + 5
        self.accel_long = numpy.zeros(size, dtype=numpy.float32) + 5
        self.surface_temperature = numpy.zeros(size, dtype=numpy.float32) + 20
        self.stab = numpy.zeros(size, dtype=numpy.int16) + 3
        self.trac = numpy.zeros(size, dtype=numpy.int16) + 3
        self.unix_time = numpy.zeros(size, dtype=numpy.float64) + time.time()
        self.steering_angle = numpy.zeros(size, dtype=numpy.float32) + 180
        self.yaw_rate = numpy.zeros(size, dtype=numpy.float32) + 30
        self.rec_time = numpy.zeros(size, dtype=numpy.float64) + time.time()
        self.vehicle_id = None

    def get_field(self, field):
        """Return appropriate values for given field"""
        if field in SimulationReader.Field_dict.keys():
            return self.Field_dict[field](self)

    def get_heading(self):
        """Return heading"""
        return self.heading

    def get_abs(self):
        """Return automatic brake system status"""
        return self.abs

    def get_air_temperature2(self):
        """Return air temperature in Celsius converting from Fahrenheit"""
        return self.air_temperature2

    def get_brake(self):
        """Return brake status"""
        return self.brake

    def get_latitude(self):
        return self.latitude

    def get_longitude(self):
        return self.longitude

    def get_elevation(self):
        """Return elevation in meters"""
        return self.elevation

    def get_dewpoint(self):
        """Return dewpoint in Celsius"""
        return self.dewpoint

    def get_humidity(self):
        """Return humidity"""
        return self.humidity

    def get_lights(self):
        """Return lights"""
        return self.lights

    def get_speed_meters_per_second(self):
        """Return speed in meters per second"""
        return self.speed_mps

    def get_accel_lat(self):
        """Return latitudinal acceleration"""
        return self.accel_lat

    def get_accel_long(self):
        """Return longitudinal acceleration"""
        return self.accel_long

    def get_stab(self):
        """Return stability"""
        return self.stab

    def get_surface_temperature(self):
        """Return surface temperature in Celsius converting from Fahrenheit"""
        return self.surface_temperature

    def get_time(self):
        """Convert time to Unix seconds"""
        return self.unix_time

    def get_vehicle_id(self):
        """Return vehicle id"""
        return self.vehicle_id

    def get_trac(self):
        """Return traction control"""
        return self.trac

    def get_steering_angle(self):
        """Return steering angle"""
        return self.steering_angle

    def get_yaw_rate(self):
        """Return yaw rate"""
        return self.yaw_rate

    def get_rec_time(self):
        """Return received time"""
        return self.rec_time

    # Maps the netcdf output variable name to the function to be called
    # to get the appropriate value
    Field_dict = {'rec_time': get_rec_time, 'obs_time': get_time, 'latitude': get_latitude, 'longitude': get_longitude, 'elevation': get_elevation,
                  'speed': get_speed_meters_per_second, 'hoz_accel_lat': get_accel_lat, 'hoz_accel_long': get_accel_long, 'humidity':get_humidity,
                  'surface_temp': get_surface_temperature, 'air_temp2': get_air_temperature2, 'dew_temp': get_dewpoint,
                  'heading': get_heading, 'abs': get_abs, 'brake_status': get_brake, 'lights': get_lights, 'stab': get_stab,
                  'trac': get_trac, 'steering_angle': get_steering_angle, 'yaw_rate': get_yaw_rate}

def main():

    usage_str = "%prog"
    parser = OptionParser(usage = usage_str)
    parser.add_option("-n", "--nothing", dest="nothing", action="store_true", help="nothing at all")
    parser.add_option("-c", "--config_file", dest="config", help="read specified configuration file")
    parser.add_option("-l", "--log", dest="log", help="write log messages to specified file")

    (options, args) = parser.parse_args()

    SimulationReader(10)

if __name__ == "__main__":

   main()

max_stars_repo_name: OSADP/Pikalert-Vehicle-Data-Translator-

max_issues_repo_name: OSADP/Pikalert-Vehicle-Data-Translator-

max_forks_repo_name: OSADP/Pikalert-Vehicle-Data-Translator-


text:
# google colab imports
%%capture
from os.path import exists
from wheel.pep425tags import get_abbr_impl, get_impl_ver, get_abi_tag
platform = '{}{}-{}'.format(get_abbr_impl(), get_impl_ver(), get_abi_tag())
cuda_output = !ldconfig -p|grep cudart.so|sed -e 's/.*\.\([0-9]*\)\.\([0-9]*\)$/cu\1\2/'
accelerator = cuda_output[0] if exists('/dev/nvidia0') else 'cpu'
!pip install -q http://download.pytorch.org/whl/{accelerator}/torch-0.4.1-{platform}-linux_x86_64.whl torchvision
!pip install livelossplot

# main imports
import math
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
import torchvision
import matplotlib.pyplot as plt
from livelossplot import PlotLosses

device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')

# helper function to make getting another batch of data easier
def cycle(iterable):
    while True:
        for x in iterable:
            yield x

class_names = ['apple','aquarium_fish','baby','bear','beaver','bed','bee','beetle','bicycle','bottle','bowl','boy','bridge','bus','butterfly','camel','can','castle','caterpillar','cattle','chair','chimpanzee','clock','cloud','cockroach','couch','crab','crocodile','cup','dinosaur','dolphin','elephant','flatfish','forest','fox','girl','hamster','house','kangaroo','computer_keyboard','lamp','lawn_mower','leopard','lion','lizard','lobster','man','maple_tree','motorcycle','mountain','mouse','mushroom','oak_tree','orange','orchid','otter','palm_tree','pear','pickup_truck','pine_tree','plain','plate','poppy','porcupine','possum','rabbit','raccoon','ray','road','rocket','rose','sea','seal','shark','shrew','skunk','skyscraper','snail','snake','spider','squirrel','streetcar','sunflower','sweet_pepper','table','tank','telephone','television','tiger','tractor','train','trout','tulip','turtle','wardrobe','whale','willow_tree','wolf','woman','worm',]

train_loader = torch.utils.data.DataLoader(
    torchvision.datasets.CIFAR100('data', train=True, download=True, transform=torchvision.transforms.Compose([
        torchvision.transforms.ToTensor()
    ])),
batch_size=16, drop_last=True)

test_loader = torch.utils.data.DataLoader(
    torchvision.datasets.CIFAR100('data', train=False, download=True, transform=torchvision.transforms.Compose([
        torchvision.transforms.ToTensor()
    ])),
batch_size=16, drop_last=True)

train_iterator = iter(cycle(train_loader))
test_iterator = iter(cycle(test_loader))

print(f'> Size of training dataset {len(train_loader.dataset)}')
print(f'> Size of test dataset {len(test_loader.dataset)}')

# view some of the test dataset
plt.figure(figsize=(10,10))
for i in range(25):
    plt.subplot(5,5,i+1)
    plt.xticks([])
    plt.yticks([])
    plt.grid(False)
    plt.imshow(test_loader.dataset[i][0].permute(0,2,1).contiguous().permute(2,1,0), cmap=plt.cm.binary)
    plt.xlabel(class_names[test_loader.dataset[i][1]])

# define the model
class ConvolutionalNetwork(nn.Module):
    def __init__(self):
        super(ConvolutionalNetwork, self).__init__()
        layers = nn.ModuleList()
        layers.append(nn.Conv2d(3, 64, kernel_size=4, stride=2, padding=1, bias=False))
        layers.append(nn.BatchNorm2d(64))
        layers.append(nn.ReLU())
        layers.append(nn.Conv2d(64, 128, kernel_size=4, stride=2, padding=1, bias=False))
        layers.append(nn.BatchNorm2d(128))
        layers.append(nn.ReLU())
        layers.append(nn.Conv2d(128, 100, kernel_size=4, stride=2, padding=1, bias=False))
        layers.append(nn.AvgPool2d((4,4)))
        self.layers = layers

    def forward(self, x):
        for m in self.layers:
            x = m(x)
        return x

N = ConvolutionalNetwork().to(device)

print(f'> Number of network parameters {len(torch.nn.utils.parameters_to_vector(N.parameters()))}')

# initialise the optimiser
optimiser = torch.optim.Adam(N.parameters(), lr=0.001)
epoch = 0
liveplot = PlotLosses()

# train and test
while (epoch<5):

    # arrays for metrics
    logs = {}
    train_loss_arr = np.zeros(0)
    train_acc_arr = np.zeros(0)
    test_loss_arr = np.zeros(0)
    test_acc_arr = np.zeros(0)

    # iterate over some of the train dateset
    for i in range(1000):
        x,t = next(train_iterator)
        x,t = x.to(device), t.to(device)

        optimiser.zero_grad()
        p = N(x).view(x.size(0), len(class_names))
        pred = p.argmax(dim=1, keepdim=True)
        loss = torch.nn.functional.cross_entropy(p, t)
        loss.backward()
        optimiser.step()

        train_loss_arr = np.append(train_loss_arr, loss.data)
        train_acc_arr = np.append(train_acc_arr, pred.data.eq(t.view_as(pred)).float().mean().item())

    # iterate entire test dataset
    for x,t in test_loader:
        x,t = x.to(device), t.to(device)

        p = N(x).view(x.size(0), len(class_names))
        loss = torch.nn.functional.cross_entropy(p, t)
        pred = p.argmax(dim=1, keepdim=True)

        test_loss_arr = np.append(test_loss_arr, loss.data)
        test_acc_arr = np.append(test_acc_arr, pred.data.eq(t.view_as(pred)).float().mean().item())

    # NOTE: live plot library has dumb naming forcing our 'test' to be called 'validation'
    liveplot.update({
        'accuracy': train_acc_arr.mean(),
        'val_accuracy': test_acc_arr.mean(),
        'loss': train_loss_arr.mean(),
        'val_loss': test_loss_arr.mean()
    })
    liveplot.draw()

    epoch = epoch+1

# plot predictions
def plot_image(i, predictions_array, true_label, img):
    predictions_array, true_label, img = predictions_array[i], true_label[i], img[i]
    plt.grid(False)
    plt.xticks([])
    plt.yticks([])

    plt.imshow(img, cmap=plt.cm.binary)

    predicted_label = np.argmax(predictions_array)
    color = '#335599' if predicted_label == true_label else '#ee4433'

    plt.xlabel("{} {:2.0f}% ({})".format(class_names[predicted_label],
                                  100*np.max(predictions_array),
                                  class_names[true_label]),
                                  color=color)

def plot_value_array(i, predictions_array, true_label):
    predictions_array, true_label = predictions_array[i], true_label[i]
    plt.grid(False)
    plt.xticks([])
    plt.yticks([])
    thisplot = plt.bar(range(len(class_names)), predictions_array, color="#777777")
    plt.ylim([0, 1])
    predicted_label = np.argmax(predictions_array)

    thisplot[predicted_label].set_color('#ee4433')
    thisplot[true_label].set_color('#335599')

test_images, test_labels = next(test_iterator)
test_images, test_labels = test_images.to(device), test_labels.to(device)
test_preds = torch.softmax(N(test_images).view(test_images.size(0), len(class_names)), dim=1).data.squeeze().cpu().numpy()

num_rows = 4
num_cols = 4
num_images = num_rows*num_cols
plt.figure(figsize=(2*2*num_cols, 2*num_rows))
for i in range(num_images):
    plt.subplot(num_rows, 2*num_cols, 2*i+1)
    plot_image(i, test_preds, test_labels.cpu(), test_images.cpu().squeeze().permute(1,3,2,0).contiguous().permute(3,2,1,0))
    plt.subplot(num_rows, 2*num_cols, 2*i+2)
    plot_value_array(i, test_preds, test_labels)

max_stars_repo_name: cwkx/ml-materials

max_issues_repo_name: cwkx/ml-materials

max_forks_repo_name: cwkx/ml-materials