text:
import numpy as np

SEED_STR_LIST = ['2021AAAI_COCO']

def generate_random_seed_coco():
    return _generate_random_seed('2021AAAI_COCO', n_init_point_seed=25)

def _generate_random_seed(seed_str, n_init_point_seed=10):
    assert seed_str in SEED_STR_LIST
    rng_state = np.random.RandomState(seed=sum([ord(ch) for ch in seed_str]))
    return rng_state.randint(0, 10000, (n_init_point_seed, ))

def _generate_random_seed_pair(seed_str, n_test_case_seed=5, n_init_point_seed=5):
    assert seed_str in SEED_STR_LIST
    rng_state = np.random.RandomState(seed=sum([ord(ch) for ch in seed_str]))
    result = {}
    for _ in range(n_test_case_seed):
        result[rng_state.randint(0, 10000)] = list(rng_state.randint(0, 10000, (n_init_point_seed, )))
    return result

ext: py

max_stars_repo_name: aryandeshwal/HyBO

max_issues_repo_name: aryandeshwal/HyBO

max_forks_repo_name: aryandeshwal/HyBO

reason: import numpy


text:
import numpy as np
import csv
import sys
import pandas as pd
import torch
import jiwer
import librosa
from pystoi import stoi
import transformers
from transformers import Wav2Vec2ForMaskedLM, Wav2Vec2Tokenizer
import sys, os
import warnings

'''
Functions to compute the metrics for the 2 tasks of the L3DAS21 challenge.
- task1_metric returns the metric for task 1.
- location_sensitive_detection returns the metric for task 1.
Both functions require numpy matrices as input and can compute only 1 batch at time.
Please, have a look at the "evaluation_baseline_taskX.py" scripts for detailed examples
on the use of these functions.
'''

#TASK 1 METRICS
warnings.filterwarnings("ignore", category=FutureWarning)
transformers.logging.set_verbosity_error()
#wer_tokenizer = Wav2Vec2Tokenizer.from_pretrained("facebook/wav2vec2-base-960h");
#wer_model = Wav2Vec2ForMaskedLM.from_pretrained("facebook/wav2vec2-base-960h");

def wer(clean_speech, denoised_speech):
    """
    computes the word error rate(WER) score for 1 single data point
    """
    def _transcription(clean_speech, denoised_speech):

        # transcribe clean audio
        input_values = wer_tokenizer(clean_speech, return_tensors="pt").input_values;
        logits = wer_model(input_values).logits;
        predicted_ids = torch.argmax(logits, dim=-1);
        transcript_clean = wer_tokenizer.batch_decode(predicted_ids)[0];

        # transcribe
        input_values = wer_tokenizer(denoised_speech, return_tensors="pt").input_values;
        logits = wer_model(input_values).logits;
        predicted_ids = torch.argmax(logits, dim=-1);
        transcript_estimate = wer_tokenizer.batch_decode(predicted_ids)[0];

        return [transcript_clean, transcript_estimate]

    transcript = _transcription(clean_speech, denoised_speech);
    try:   #if no words are predicted
        wer_val = jiwer.wer(transcript[0], transcript[1])
    except ValueError:
        wer_val = None

    return wer_val

def task1_metric(clean_speech, denoised_speech, sr=16000):
    '''
    Compute evaluation metric for task 1 as (stoi+(1-word error rate)/2)
    This function computes such measure for 1 single datapoint
    '''
    WER = wer(clean_speech, denoised_speech)
    if WER is not None:  #if there is no speech in the segment
        STOI = stoi(clean_speech, denoised_speech, sr, extended=False)
        WER = np.clip(WER, 0., 1.)
        STOI = np.clip(STOI, 0., 1.)
        metric = (STOI + (1. - WER)) / 2.
    else:
        metric = None
        STOI = None
    return metric, WER, STOI

def compute_se_metrics(predicted_folder, truth_folder, fs=16000):
    '''
    Load all submitted sounds for task 1 and compute the average metric
    '''
    METRIC = []
    WER = []
    STOI = []
    predicted_list = [s for s in os.listdir(predicted_folder) if '.wav' in s]
    truth_list = [s for s in os.listdir(truth_folder) if '.wav' in s]
    n_sounds = len(predicted_list)
    for i in range(n_sounds):
        name = str(i) + '.wav'
        predicted_temp_path = os.path.join(predicted_folder, name)
        truth_temp_path = os.path.join(truth_folder, name)
        predicted = librosa.load(predicted_temp_path, sr=fs)
        truth = librosa.load(truth_temp_path, sr=fs)
        metric, wer, stoi = task1_metric(truth, predicted)
        METRIC.append(metric)
        WER.append(wer)
        STOI.append(stoi)

    average_metric = np.mean(METRIC)
    average_wer = np.mean(WER)
    average_stoi = np.mean(STOI)

    print ('*******************************')
    print ('Task 1 metric: ', average_metric)
    print ('Word error rate: ', average_wer)
    print ('Stoi: ', average_stoi)

    return average_metric

#TASK 2 METRICS
sound_classes_dict_task2 = {'Chink_and_clink':0,
                           'Computer_keyboard':1,
                           'Cupboard_open_or_close':2,
                           'Drawer_open_or_close':3,
                           'Female_speech_and_woman_speaking':4,
                           'Finger_snapping':5,
                           'Keys_jangling':6,
                           'Knock':7,
                           'Laughter':8,
                           'Male_speech_and_man_speaking':9,
                           'Printer':10,
                           'Scissors':11,
                           'Telephone':12,
                           'Writing':13}

def location_sensitive_detection(pred, true, n_frames=100, spatial_threshold=2.,
                                 from_csv=False, verbose=False):
    '''
    Compute TP, FP, FN of a single data point using
    location sensitive detection
    '''
    TP = 0   #true positives
    FP = 0   #false positives
    FN = 0   #false negatives
    #read csv files into numpy matrices if required
    if from_csv:
        pred = pd.read_csv(pred, sep=',',header=None)
        true = pd.read_csv(true, sep=',',header=None)
        pred = pred.values
        true = true.values
    #build empty dict with a key for each time frame
    frames = {}
    for i in range(n_frames):
        frames[i] = {'p':[], 't':[]}
    #fill each time frame key with predicted and true entries for that frame
    for i in pred:
        frames[i[0]]['p'].append(i)
    for i in true:
        frames[i[0]]['t'].append(i)
    #iterate each time frame:
    for frame in range(n_frames):
        t = frames[frame]['t']  #all true events for frame i
        p = frames[frame]['p']  #all predicted events for frame i
        matched = 0           #counts the matching events

        if len(t) == 0:         #if there are PREDICTED but not TRUE events
            FP += len(p)        #all predicted are false positive
        elif len(p) == 0:       #if there are TRUE but not PREDICTED events
            FN += len(t)        #all predicted are false negative

        else:
            for i_t in range(len(t)):           #iterate all true events
                match = False       #flag for matching events
                #count if in each true event there is or not a matching predicted event
                true_class = t[i_t][1]          #true class
                true_coord = t[i_t][-3:]        #true coordinates
                for i_p in range(len(p)):       #compare each true event with all predicted events
                    pred_class = p[i_p][1]      #predicted class
                    pred_coord = p[i_p][-3:]    #predicted coordinates
                    spat_error = np.linalg.norm(true_coord-pred_coord)  #cartesian distance between spatial coords
                    if true_class == pred_class and spat_error < spatial_threshold:  #if predicton is correct (same label + not exceeding spatial error threshold)
                        match = True
                if match:
                    matched += 1    #for each true event, match only once comparing all predicted events

        num_true_items = len(t)
        num_pred_items = len(p)
        fn =  num_true_items - matched
        fp = num_pred_items - matched

        #add to counts
        TP += matched          #number of matches are directly true positives
        FN += fn
        FP += fp

    precision = TP / (TP + FP + sys.float_info.epsilon)
    recall = TP / (TP + FN + sys.float_info.epsilon)
    F_score = 2 * ((precision * recall) / (precision + recall + sys.float_info.epsilon))

    results = {'precision': precision,
               'recall': recall,
               'F score': F_score
               }

    if verbose:
        print ('true positives: ', TP)
        print ('false positives: ', FP)
        print ('false negatives: ', FN)
        print ('---------------------')

        print ('*******************************')
        print ('F score: ', F_score)
        print ('Precision: ', precision)
        print ('Recall: ', recall)
        print  ('TP: ' , TP)
        print  ('FP: ' , FP)
        print  ('FN: ' , FN)

    return TP, FP, FN, F_score

def sed_score_computation(pred, true, n_frames=100, spatial_threshold=2.,
                                 from_csv=False, verbose=False):
    '''
    Compute TP, FP, FN of a single data point using
    location sensitive detection
    '''
    TP = 0   #true positives
    FP = 0   #false positives
    FN = 0   #false negatives
    #read csv files into numpy matrices if required
    if from_csv:
        pred = pd.read_csv(pred, sep=',',header=None)
        true = pd.read_csv(true, sep=',',header=None)
        pred = pred.values
        true = true.values
    #build empty dict with a key for each time frame
    frames = {}
    for i in range(n_frames):
        frames[i] = {'p':[], 't':[]}
    #fill each time frame key with predicted and true entries for that frame
    for i in pred:
        frames[i[0]]['p'].append(i)
    for i in true:
        frames[i[0]]['t'].append(i)
    #iterate each time frame:
    for frame in range(n_frames):
        t = frames[frame]['t']  #all true events for frame i
        p = frames[frame]['p']  #all predicted events for frame i
        matched = 0           #counts the matching events

        if len(t) == 0:         #if there are PREDICTED but not TRUE events
            FP += len(p)        #all predicted are false positive
        elif len(p) == 0:       #if there are TRUE but not PREDICTED events
            FN += len(t)        #all predicted are false negative

        else:
            for i_t in range(len(t)):           #iterate all true events
                match = False       #flag for matching events
                #count if in each true event there is or not a matching predicted event
                true_class = t[i_t][1]          #true class
#                 true_coord = t[i_t][-3:]        #true coordinates
                for i_p in range(len(p)):       #compare each true event with all predicted events
                    pred_class = p[i_p][1]      #predicted class
#                     pred_coord = p[i_p][-3:]    #predicted coordinates
#                     spat_error = np.linalg.norm(true_coord-pred_coord)  #cartesian distance between spatial coords
                    if true_class == pred_class: #and spat_error < spatial_threshold:  #if predicton is correct (same label + not exceeding spatial error threshold)
                        match = True
                if match:
                    matched += 1    #for each true event, match only once comparing all predicted events

        num_true_items = len(t)
        num_pred_items = len(p)
        fn =  num_true_items - matched
        fp = num_pred_items - matched

        #add to counts
        TP += matched          #number of matches are directly true positives
        FN += fn
        FP += fp

    precision = TP / (TP + FP + sys.float_info.epsilon)
    recall = TP / (TP + FN + sys.float_info.epsilon)
    F_score = 2 * ((precision * recall) / (precision + recall + sys.float_info.epsilon))
    Nref=TP+FN
    Nsys=TP+FP
    ER_score = (max(Nref, Nsys) - TP) / (Nref + 0.0)################ from evaluation_metrics.py SELD
    sed_score=np.mean([1-F_score,ER_score])

    results = {'precision': precision,
               'recall': recall,
               'F score': F_score
               }

    if verbose:
        print  ('SED score: ' , sed_score)

    return TP, FP, FN, sed_score
def compute_seld_metrics(predicted_folder, truth_folder, n_frames=100, spatial_threshold=0.3):
    '''
    compute F1 score from results folder of submitted results based on the
    location sensitive detection metric
    '''
    TP = 0
    FP = 0
    FN = 0
    predicted_list = [s for s in os.listdir(predicted_folder) if '.csv' in s]
    truth_list = [s for s in os.listdir(truth_folder) if '.csv' in s]
    n_files = len(predicted_list)
    #iterrate each submitted file
    for i in range(n_files):
        name = predicted_list[i]
        predicted_temp_path = os.path.join(predicted_folder, name)
        truth_temp_path = os.path.join(truth_folder, name)
        #compute tp,fp,fn for each file
        tp, fp, fn = location_sensitive_detection(predicted_temp_path,
                                                  truth_temp_path,
                                                  n_frames,
                                                  spatial_threshold)
        TP += tp
        FP += fp
        FN += fn

    #compute total F score
    precision = TP / (TP + FP + sys.float_info.epsilon)
    recall = TP / (TP + FN + sys.float_info.epsilon)

    print ('*******************************')
    F_score = (2 * precision * recall) / (precision + recall + sys.float_info.epsilon)
    print ('F score: ', F_score)
    print ('Precision: ', precision)
    print ('Recall: ', recall)

    return F_score

#gen_dummy_seld_results('./prova')
#compute_seld_metric('./prova/pred', './prova/truth')

max_stars_repo_name: ispamm/DualQSELD-TCN

max_issues_repo_name: ispamm/DualQSELD-TCN

max_forks_repo_name: ispamm/DualQSELD-TCN


text:
import numpy as np

def final_score(y_pred, threshold=0.5):
    y_mae = np.where(y_pred[0] < threshold, 0, np.ceil(y_pred[0])).astype(int)
    y_mse = np.where(y_pred[1] < threshold, 0, np.ceil(y_pred[1])).astype(int)

    y_pred_avg = np.average([y_mae, y_mse], axis=0)
    y_pred_avg = np.around(y_pred_avg).astype(int)

    return y_pred_avg


text:
# -*- coding: utf-8 -*-
"""
Created on Sat Feb 24 11:40:52 2018

@author: Ourabora
"""

import matplotlib.pyplot as plt
plt.plot([1,2,3,4])
plt.ylabel('some numbers')
plt.xlabel('random')
plt.show()

plt.plot([1,2,3,4],[1,10,9,16])

import matplotlib.pyplot as plt
plt.plot([1,2,3,4],[1,18,9,16],'ro') # bo gives blue color dots
plt.axis([0,6,0,20])
plt.show()

import numpy as np
import matplotlib.pyplot as plt

# evenly sampled time at 200ms intervals
t = np.arange(0.,5.,0.2)

# red dashes, blue squares and green triangles
plt.plot(t,t,'r--',t,t**2,'bs',t,t**3,'g^')
plt.show()

# multiplots and axis
import numpy as np
import matplotlib.pyplot as plt

def f(t):
    return np.exp(-t)*np.cos(2*np.pi*t)

t1 = np.arange(0.0,5.0,0.1)
t2 = np.arange(0.0,5.0,0.02)

# pyplot.figure(num=None, figsize=None, dpi=None, facecolor=None, edgecolor=None, frameon=True, FigureClass=<class 'matplotlib.figure.Figure'>, clear=False, **kwargs)
plt.figure(1)
plt.subplot(2,1,1)
plt.plot(t1,f(t1),'bo',t2,f(t2),'k')

plt.subplot(2,1,2)
plt.plot(t2,np.cos(2*np.pi*t2),'r--')
plt.show()

import matplotlib.pyplot as plt
plt.figure(1) # the first figure
plt.subplot(211) # the first subplot in the first figure
plt.plot([1,2,3])
plt.subplot(212)
plt.plot([4,5,6])

plt.figure(2) # a second figure
plt.plot([4,5,6]) #creates a subplot(111) by default

plt.figure(1) # figure 1 current; subplot(212) still current
plt.title('Easy as 1, 2, 3')

# working with text in plot s
import numpy as np
import matplotlib.pyplot as plt

# fixing random state for reproducibility
np.random.seed(19680801)

mu, sigma = 100, 15
x = mu + sigma*np.random.randn(10000)

# the histogram of the data
n, bins, patches = plt.hist(x,50,normed=1,facecolor='g',alpha=0.75)

plt.xlabel('Smarts')
plt.ylabel('Probability')
plt.title('Histogram of IQ')
plt.text(60,.010,r'$\mu=100,\ \sigma=15$')
plt.axis([40,160,0,0.03])
plt.grid(True)
plt.show()

# text properties in pyplots
import matplotlib.pyplot as plt
import matplotlib.patches as patches

# build a rectangle in axes coords
left, width = .25, .5
bottom, height = .25, .5
right = left + width
top = bottom + height

fig = plt.figure()
ax = fig.add_axes([0,0,1,1])

# axes coordinates are 0,0 is bottom left and 1,1 is upper right
p = patches.Rectangle(
        (left,bottom),width,height,
        fill=False,transform=ax.transAxes,clip_on=False
        )

ax.add_patch(p)

ax.text(left,bottom,'left top',
        horizontalalignment='left',
        verticalalignment='top',
        transform = ax.transAxes)

ax.text(left,bottom,'left bottom',
        horizontalalignment='left',
        verticalalignment='bottom',
        transform=ax.transAxes)

ax.text(right, top,'right bottom',
        horizontalalignment='right',
        verticalalignment='bottom',
        transform=ax.transAxes)

ax.text(right,top,'right top',
        horizontalalignment='right',
        verticalalignment='top',
        transform=ax.transAxes)

ax.text(right,bottom,'center top',
        horizontalalignment='center',
        verticalalignment='top',
        transform=ax.transAxes)

ax.text(left,0.5*(bottom+top),'right center',
        horizontalalignment='right',
        verticalalignment='center',
        rotation='vertical',
        transform=ax.transAxes)

ax.text(left,0.5*(bottom+top),'left center',
        horizontalalignment='left',
        verticalalignment='center',
        rotation='vertical',
        transform=ax.transAxes)

ax.text(0.5*(left+right),0.5*(bottom+top),'middle',
        horizontalalignment='center',
        verticalalignment='center',
        fontsize=20,color='red',
        transform=ax.transAxes)

ax.text(right,0.5*(bottom+top),'centered',
        horizontalalignment='center',
        verticalalignment='center',
        rotation='vertical',
        transform=ax.transAxes)

ax.text(left,top,'rotated\nwith newlines',
        horizontalalignment='center',
        verticalalignment='center',
        rotation=45,
        transform=ax.transAxes)

ax.set_axis_off()
plt.show()

# annotating texts
import numpy as np
import matplotlib.pyplot as plt

ax = plt.subplot(111)

t = np.arange(0.0,5.0,0.01)
s = np.cos(2*np.pi*t)
line, = plt.plot(t,s,lw=2)

plt.annotate('local max',xy=(2,1),xytext=(3,1.5),
             arrowprops=dict(facecolor='black',shrink=0.05),
             )

plt.ylim(-2,2)
plt.show()

# log and non linear axis
import numpy as np
import matplotlib.pyplot as plt

from matplotlib.ticker import NullFormatter # useful for 'logit' scale

# Fixing random state for reproducibility

np.random.seed(19680801)

# make up some data in the interval 0,1
y = np.random.nrmal(loc=0.5, scaple=0.4,size=1000)
y = y[(y>0) & (y<1)]
y.sort()
x = np.arange(len(y))

# plot with various axes scales
plt.figure(1)

# linear
plt.subplot(221)
plt.plot(x,y)
plt.yscale('linear')
plt.title('linear')
plt.grid(True)

# log
plt.subplot(222)
plt.plot(x,y)
plt.yscale('log')
plt.title('log')
plt.grid(True)

# Symmetric log
plt.subplot(223)
plt.plot(x,y,-y.mean())
plt.yscale('syslog',linthreshy=0.01)
plt.title('symlog')
plt.grid(True)

# logit
plt.subplot(224)
plt.plot(x,y)
plt.yscale('logit')
plt.grid(True)

# format the minor tick of the y-axis into empty strings with
# NullFormatter , to avoid cumbering the axis with too many labels.
plt.gca().yaxis.set_minor_formatter(NullFormatter())
# Adjust the subplot layout because the logit one may take more space
# than usual, due to y-tick labels like "1 - 10^{-3}"
plt.subplots_adjust(top=0.92,bottom=0.08,left=0.10,right=0.95,hspace = 0.25,
                    wspace=0.35)

max_stars_repo_name: Arunken/PythonScripts

max_issues_repo_name: Arunken/PythonScripts

max_forks_repo_name: Arunken/PythonScripts


text:
from pycocotools.coco import COCO
import pickle
from feature_extractor import FeatureExtractor
import json
import numpy as np
import time
import os
from threading import Thread

dataDir='/data/ouyangzhihao/dataset/MSCOCO'
dataType='train2014'
annFile='{}/annotations/instances_{}.json'.format(dataDir,dataType)

# initialize COCO api for caption annotations
annFile = '{}/annotations/captions_{}.json'.format(dataDir,dataType)
coco = None
coco_caps = None

def if_one_variable_is_none(*args):
    for ele in args:
        if ele is None: return True
    return False

def init_COCO():
    # initialize COCO api for instance annotations
    start_time = time.time()
    global coco, coco_caps
    coco = COCO(annFile)
    coco_caps = COCO(annFile)
    print('Load COCO time:', time.time() - start_time)

def get_file_caption(file_name):
    if if_one_variable_is_none(coco, coco_caps): init_COCO()

    file_id = int(file_name[-4 - 12:-4])
    img = coco.loadImgs(file_id)[0]
    annIds = coco_caps.getAnnIds(imgIds=img['id']);
    anns = coco_caps.loadAnns(annIds)
    captions = coco_caps.showAnns(anns)
    return captions

# Classification
# Read image features
feature_path = '/data/ouyangzhihao/Web/Model/Results/COCO_Norm_Feature_MobileNet/features.pkl'
image_path = '/data/ouyangzhihao/Web/Model/Results/COCO_Norm_Feature_MobileNet/file_names.pkl'
# Files are in static folder
dataset_path = 'dataset/MSCOCO/'

# Read Imagenet classes
fe = features= img_paths = class_idx = None
def init_image_model():
    start_time = time.time()
    global fe, features, img_paths, class_idx
    fe = FeatureExtractor()
    features = pickle.load(open(feature_path, 'rb'))
    img_paths = pickle.load(open(image_path, 'rb'))
    img_paths = [dataset_path + var for var in img_paths]
    with open("imagenet_class_index.json", 'r') as f:
        class_idx = json.load(f)
    print('Load feature time:', time.time() - start_time)

def get_img_html_info(path, dist, probability, categories, captions):
    return {
        'path':path,
        'dist':dist,
        'probability':probability,
        'categories':categories,
        'captions':captions,
    }

def return_model_result(img, top_k):
    start_time = time.time()
    if if_one_variable_is_none(fe, features, img_paths, class_idx): init_image_model()
    query, prediction = fe.extract(img)
    query = np.reshape(query, -1)
    dists = np.linalg.norm(features - query, axis=-1)  # Do search
    ids = np.argsort(dists)[:top_k]  # Top k results

    # ImageNet
    # import ipdb; ipdb.set_trace()
    # print(prediction)

    pre_ids = prediction.argsort()[-3:]
    # descending -> ascending
    pre_ids = pre_ids[::-1]
    topk_class = [class_idx[str(id)][1] for id in pre_ids]
    res_to_html = []
    for id in ids:
        res_to_html += [get_img_html_info(
            path=img_paths[id], dist=dists[id], probability=prediction[pre_ids[0]],
            categories=topk_class, captions=get_file_caption(img_paths[id])
        )]
    print('classification time: ',time.time() - start_time)
    return res_to_html

# Image Detection
def image_detection(input_img_path, output_img_path):
    def task(name=None):
        os.system("python image_detection.py %s %s" % (input_img_path, output_img_path))
    p = Thread(target=task, args=('线程1',))
    p.start()  # 只是给操作系统发送了一个就绪信号，并不是执行。操作系统接收信号后安排cpu运行

# Image Segmentation
def image_segmentation(input_img_path, output_mask_path, output_img_path):
    def task(name=None):
        os.system("python image_segmentation.py %s %s %s" % (input_img_path, output_mask_path, output_img_path))
    p = Thread(target=task, args=('线程1',))
    p.start()  # 只是给操作系统发送了一个就绪信号，并不是执行。操作系统接收信号后安排cpu运行


text:
import argparse
import time
import csv
import yaml
import os
import logging
from pathlib import Path

import numpy as np
from tqdm import tqdm

from tensorboardX import SummaryWriter
import cv2
import matplotlib.pyplot as plt

class plot_results(object):
    def __init__(self, frame_list=[100], mode='base'):
        #         frame_list = [0, 100, 200, 300]
        # frame_list = [100, 700, 1200]
        # frame_list = [100]
        self.frame_list = frame_list
        print(f"mode = {mode}")
        self.get_image_names(mode=mode)
        pass

    def get_image_names(self, mode='base'):
        frame_list = self.frame_list
        plot_folder = "plots/"
        image_name = None
        if mode == 'base':
            prefix = ["Si-Df-k", "Sp-Df-fp-end-k"]
            plot_name = "mask_conf_"  # 'corr_all_'
            #         image_name = [f"{plot_folder}{plot_name}{prefix}{i:06}_{(i+1):06}.png" for i in frame_list]
        elif mode == 'good' or mode == 'bad':
            prefix = [f"Si-Df-fp-k_{mode}", f"Sp-Df-fp-end-k_{mode}"]
            plot_name = "mask_conf_" # "mask_conf_"  # 'corr_all_'
        elif mode == 'freeze':
            print(f"freeze!")
            iter_list = [0, 400, 1000]
            prefix_base = "Sp-Df-f-end-k-freezeDf"
            plot_name = 'corr_all_random_'  # 'corr_all_', "mask_conf_" "epi_dist_all_" "corr_all_random_"
            print(f"plot_name: {plot_name}")
            # prefix = [f'{prefix_base}_{iter/1000}k_' for iter in iter_list]  # 'Sp-Df-fp-end-k'
            prefix = [f'{prefix_base}_s{frame_list[0]}_{iter/1000}k' for iter in iter_list]  # 'Sp-Df-fp-end-k'
            image_name = [f"{plot_folder}{plot_name}{p}.png" for p in prefix]
            # prefix = f'Sp-Df-f-end-k-freezeDf_s{j}_{iter/1000}k'
        # image_name = [
        #     f"{plot_folder}{plot_name}{pre}{i:06}_{(i+1):06}.png"
        #     for i in frame_list
        #     for pre in prefix
        # ]
        if image_name is None:
            image_name = [
                f"{plot_folder}{plot_name}{pre}_{i}.png"
                for i in frame_list
                for pre in prefix
            ]
        self.prefix = prefix
        self.image_name = image_name
        self.image_data = []
        self.plot_name = plot_name
        print(image_name)

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

    def read_images(self):
        image_data = []
        image_name = self.image_name
        for i, file in enumerate(image_name):
            img = cv2.imread(file)
            img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
            image_data.append(img)
            print(f"read {i}: {file}")
        #     plt.imshow(img)
        #     plt.show()
        self.image_data = image_data
        pass

    def plot_images(
        self, row=2, col=2, col_labels=["Baseline - Si-Df-fp", "Ours - Sp-Df-fp-end"],
        save=True,
        figsize=(48,12),
        ext='pdf'
    ):
        ## create subgraph for combinations
        #         row, col = 2, 2
        img_num = row * col
        assert self.__len__() >= img_num
        image_data = self.image_data

        f, axarr = plt.subplots(row, col, figsize=figsize)
        #         f, axarr = plt.subplots(row, col, figsize=(48, 12))

        axarr = axarr.reshape(-1, col)
        for i in range(img_num):
            print(f"axarr: {axarr.shape}, i= {i}")
            axarr[int(i / col), int(i % col)].imshow(image_data[i])
            axarr[int(i / col), int(i % col)].axis("off")
        #     axarr[i/2,i%2].imshow(imaget(_datas[1])
        #     axarr[1,0].imshow(image_datas[2])
        #     axarr[1,1].imshow(image_datas[3])

        for ax, col_name in zip(axarr[0], col_labels):
            ax.set_title(col_name, fontsize=figsize[0])

        f.tight_layout()
        #         f.suptitle(f'{self.prefix}', fontsize=12)
        savefile = f"{self.plot_name}_{str('_').join(self.prefix)}_{str('_').join([str(f) for f in self.frame_list])}"
        if save:
            if ext == 'pdf':
                file = f"plots/{savefile}.pdf"
                plt.savefig(file, bbox_inches="tight")
            else:
                file = f"plots/{savefile}.png"
                plt.savefig(file, dpi=300, bbox_inches="tight")
            logging.info(f"save image: {savefile}")
            print(f"save image: {file}")
        else:
            print(f"not saved!!")
        #         logging.info(f"save image: {file}")
        plt.show()

if __name__ == "__main__":
    plot_helper = plot_class()
    plot_helper.read_images()
    # plot_helper.plot_images(row=3,col=2)
    plot_helper.plot_images(row=1,col=2)

# class plot_class(object):
#     def __init__(self):
# #         frame_list = [0, 100, 200, 300]
#         frame_list = [100, 700, 1200]
# #         frame_list = [100]
#         prefix = ['Si-Df-k', 'Sp-Df-fp-end-k']
#         plot_folder = 'plots/'
#         plot_name = 'mask_conf_' # 'corr_all_'
# #         image_name = [f"{plot_folder}{plot_name}{prefix}{i:06}_{(i+1):06}.png" for i in frame_list]
#         image_name = [f"{plot_folder}{plot_name}{pre}{i:06}_{(i+1):06}.png" for i in frame_list for pre in prefix ]
#         self.frame_list = frame_list
#         self.prefix = prefix
#         self.image_name = image_name
#         self.image_data = []
#         print(image_name)
#         pass
#     def __len__(self):
#         return len(self.image_name)

#     def read_images(self):
#         image_data = []
#         image_name = self.image_name
#         for i, file in enumerate(image_name):
#             img = cv2.imread(file)
#             img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
#             image_data.append(img)
#             print(f"read {i}: {file}")
#         #     plt.imshow(img)
#         #     plt.show()
#         self.image_data = image_data
#         pass

#     def plot_images(self, row=2, col=2, col_labels=['Baseline - Si-Df-fp', 'Ours - Sp-Df-fp-end']):
#         ## create subgraph for combinations
# #         row, col = 2, 2
#         img_num = row*col
#         assert self.__len__() >= img_num
#         image_data = self.image_data

#         f, axarr = plt.subplots(row, col, figsize=(48, 12))
# #         f, axarr = plt.subplots(row, col, figsize=(48, 12))

#         axarr = axarr.reshape(-1, col)
#         for i in range(img_num):
#             print(f'axarr: {axarr.shape}, i= {i}')
#             axarr[int(i/col),int(i%col)].imshow(image_data[i])
#             axarr[int(i/col),int(i%col)].axis('off')
#         #     axarr[i/2,i%2].imshow(imaget(_datas[1])
#         #     axarr[1,0].imshow(image_datas[2])
#         #     axarr[1,1].imshow(image_datas[3])

#         for ax, col_name in zip(axarr[0], col_labels):
#             ax.set_title(col_name)

#         f.tight_layout()
# #         f.suptitle(f'{self.prefix}', fontsize=12)
#         savefile = f"{str('_').join(self.prefix)}_{str('_').join([str(f) for f in self.frame_list])}"
#         file = f"plots/{savefile}.png"
# #         logging.info(f"save image: {file}")
#         print(f"save image: {file}")
#         plt.show()

# def plot_imgs(imgs, titles=None, cmap='brg', ylabel='', normalize=False, ax=None, dpi=100):
#     n = len(imgs)
#     if not isinstance(cmap, list):
#         cmap = [cmap]*n
#     if ax is None:
#         fig, ax = plt.subplots(1, n, figsize=(6*n, 6), dpi=dpi)
#         if n == 1:
#             ax = [ax]
#     else:
#         if not isinstance(ax, list):
#             ax = [ax]
#         assert len(ax) == len(imgs)
#     for i in range(n):
#         if imgs[i].shape[-1] == 3:
#             imgs[i] = imgs[i][..., ::-1]  # BGR to RGB
#         ax[i].imshow(imgs[i], cmap=plt.get_cmap(cmap[i]),
#                      vmin=None if normalize else 0,
#                      vmax=None if normalize else 1)
#         if titles:
#             ax[i].set_title(titles[i])
#         ax[i].get_yaxis().set_ticks([])
#         ax[i].get_xaxis().set_ticks([])
#         for spine in ax[i].spines.values():  # remove frame
#             spine.set_visible(False)
#     ax[0].set_ylabel(ylabel)
#     plt.tight_layout()

# # from utils.draw import img_overlap
# def img_overlap(img_r, img_g, img_gray):  # img_b repeat
#     img = np.concatenate((img_gray, img_gray, img_gray), axis=0)
#     img[0, :, :] += img_r[0, :, :]
#     img[1, :, :] += img_g[0, :, :]
#     img[img > 1] = 1
#     img[img < 0] = 0
#     return img

# def draw_keypoints(img, corners, color=(0, 255, 0), radius=3, s=3):
#     '''

#     :param img:
#         image:
#         numpy [H, W]
#     :param corners:
#         Points
#         numpy [N, 2]
#     :param color:
#     :param radius:
#     :param s:
#     :return:
#         overlaying image
#         numpy [H, W]
#     '''
#     img = np.repeat(cv2.resize(img, None, fx=s, fy=s)[..., np.newaxis], 3, -1)
#     for c in np.stack(corners).T:
#         # cv2.circle(img, tuple(s * np.flip(c, 0)), radius, color, thickness=-1)
#         cv2.circle(img, tuple((s * c[:2]).astype(int)), radius, color, thickness=-1)
#     return img

# # def draw_keypoints(img, corners, color=(0, 255, 0), radius=3, s=3):
# #     '''

# #     :param img:
# #         np (H, W)
# #     :param corners:
# #         np (3, N)
# #     :param color:
# #     :param radius:
# #     :param s:
# #     :return:
# #     '''
# #     img = np.repeat(cv2.resize(img, None, fx=s, fy=s)[..., np.newaxis], 3, -1)
# #     for c in np.stack(corners).T:
# #         # cv2.circle(img, tuple(s * np.flip(c, 0)), radius, color, thickness=-1)
# #         cv2.circle(img, tuple((s*c[:2]).astype(int)), radius, color, thickness=-1)
# #     return img

# def draw_matches(rgb1, rgb2, match_pairs, filename='matches.png', show=False):
#     '''

#     :param rgb1:
#         image1
#         numpy (H, W)
#     :param rgb2:
#         image2
#         numpy (H, W)
#     :param match_pairs:
#         numpy (keypoiny1 x, keypoint1 y, keypoint2 x, keypoint 2 y)
#     :return:
#         None
#     '''
#     from matplotlib import pyplot as plt

#     h1, w1 = rgb1.shape[:2]
#     h2, w2 = rgb2.shape[:2]
#     canvas = np.zeros((max(h1, h2), w1 + w2, 3), dtype=rgb1.dtype)
#     canvas[:h1, :w1] = rgb1[:,:,np.newaxis]
#     canvas[:h2, w1:] = rgb2[:,:,np.newaxis]
#     # fig = plt.figure(frameon=False)
#     fig = plt.imshow(canvas)

#     xs = match_pairs[:, [0, 2]]
#     xs[:, 1] += w1
#     ys = match_pairs[:, [1, 3]]

#     alpha = 1
#     sf = 5
#     lw = 0.5
#     # markersize = 1
#     markersize = 2

#     plt.plot(
#         xs.T, ys.T,
#         alpha=alpha,
#         linestyle="-",
#         linewidth=lw,
#         aa=False,
#         marker='o',
#         markersize=markersize,
#         fillstyle='none',
#         color=[0.0, 0.8, 0.0],
#     );
#     plt.tight_layout()
#     plt.savefig(filename, dpi=300, bbox_inches='tight')
#     print('#Matches = {}'.format(len(match_pairs)))
#     if show:
#         plt.show()

# # from utils.draw import draw_matches_cv
# def draw_matches_cv(data):
#     keypoints1 = [cv2.KeyPoint(p[1], p[0], 1) for p in data['keypoints1']]
#     keypoints2 = [cv2.KeyPoint(p[1], p[0], 1) for p in data['keypoints2']]
#     inliers = data['inliers'].astype(bool)
#     matches = np.array(data['matches'])[inliers].tolist()
#     def to3dim(img):
#         if img.ndim == 2:
#             img = img[:, :, np.newaxis]
#         return img
#     img1 = to3dim(data['image1'])
#     img2 = to3dim(data['image2'])
#     img1 = np.concatenate([img1, img1, img1], axis=2)
#     img2 = np.concatenate([img2, img2, img2], axis=2)
#     return cv2.drawMatches(img1, keypoints1, img2, keypoints2, matches,
#                            None, matchColor=(0,255,0), singlePointColor=(0, 0, 255))

# def drawBox(points, img, offset=np.array([0,0]), color=(0,255,0)):
# #     print("origin", points)
#     offset = offset[::-1]
#     points = points + offset
#     points = points.astype(int)
#     for i in range(len(points)):
#         img = img + cv2.line(np.zeros_like(img),tuple(points[-1+i]), tuple(points[i]), color,5)
#     return img

max_stars_repo_name: KuangHaofei/pytorch-deepFEPE


text:
import numpy as np
import pygame
import random
import sys
from ga_2048.colours import Colors
from pygame.locals import QUIT,KEYDOWN
from ga_2048.utils import get_logger,reverse_mapping

class Game:

    def __init__(self, loaded_game = False, show = True, bot=True, disable_logs = True):
        self.show = show
        self.bot = bot
        self.total_points = 0
        self.board_size = 4
        self.logger = get_logger("GAME")
        self.logger.disabled = disable_logs
        self.previous_tile = None

        pygame.init()
        if not bot and not show:
            raise ValueError("A human cannot operate without the visual screen")
        if self.show:
            self.surface = pygame.display.set_mode((500, 500), 1, 32)
            pygame.display.set_caption("2048")
            self.myfont = pygame.font.SysFont("monospace", 25)
            self.scorefont = pygame.font.SysFont("monospace", 50)

        if not loaded_game:
            self.tiles = np.zeros((4, 4),dtype=int)
        else:
            if not isinstance(loaded_game,(list,np.ndarray)):
                raise TypeError("Please input a list of numpy array")
            self.tiles = np.array(loaded_game)

        self.place_random_tile()
        self.place_random_tile()

        if self.show:
            self.show_matrix()
        self.undo = list()

    def show_matrix(self):
        self.surface.fill(Colors.BLACK)

        for i in range(0,self.board_size):
            for j in range(0,self.board_size):
                pygame.draw.rect(self.surface, Colors().getcolour(self.tiles[i][j]),
                                 (i * (400 / self.board_size), j * (400 / self.board_size) + 100, 400 / self.board_size,
                                  400 / self.board_size))

                label = self.myfont.render(str(self.tiles[i][j]), 1, (255, 255, 255))
                label2 = self.scorefont.render("Score:" + str(self.total_points), 1, (255, 255, 255))

                self.surface.blit(label, (i * (400 / self.board_size) + 30, j * (400 / self.board_size) + 130))
                self.surface.blit(label2, (10, 20))

    def show_game_over(self):

        self.surface.fill(Colors.BLACK)

        label = self.scorefont.render("Game Over!", 1, (255, 255, 255))
        label2 = self.scorefont.render("Score:" + str(self.total_points), 1, (255, 255, 255))
        label3 = self.myfont.render("Press r to restart!", 1, (255, 255, 255))

        self.surface.blit(label, (50, 100))
        self.surface.blit(label2, (50, 200))
        self.surface.blit(label3, (50, 300))

    @staticmethod
    def get_rotations(k):
        if k == pygame.K_UP:
            return 0
        elif k == pygame.K_DOWN:
            return 2
        elif k == pygame.K_LEFT:
            return 1
        elif k == pygame.K_RIGHT:
            return 3

    def rotate_matrix(self):
        for i in range(0, int(self.board_size / 2)):
            for k in range(i, self.board_size - i - 1):
                temp1 = self.tiles[i][k]
                temp2 = self.tiles[self.board_size - 1 - k][i]
                temp3 = self.tiles[self.board_size - 1 - i][self.board_size - 1 - k]
                temp4 = self.tiles[k][self.board_size - 1 - i]

                self.tiles[self.board_size - 1 - k][i] = temp1
                self.tiles[self.board_size - 1 - i][self.board_size - 1 - k] = temp2
                self.tiles[k][self.board_size - 1 - i] = temp3
                self.tiles[i][k] = temp4

    @staticmethod
    def is_arrow(k):
        return k in [pygame.K_UP,pygame.K_DOWN,pygame.K_LEFT,pygame.K_RIGHT]

    def move_tiles(self):
        for i in range(self.board_size):
            for j in range(self.board_size-1):
                while self.tiles[i][j] == 0 and sum(self.tiles[i][j:]) > 0:
                    for k in range(j,self.board_size-1):
                        self.tiles[i][k] = self.tiles[i][k+1]
                    self.tiles[i][self.board_size-1] = 0

    def merge_tiles(self):
        for i in range(0, self.board_size):
            for k in range(0, self.board_size - 1):
                if self.tiles[i][k] == self.tiles[i][k + 1] and self.tiles[i][k] != 0:
                    self.tiles[i][k] = self.tiles[i][k] * 2
                    self.tiles[i][k + 1] = 0
                    self.total_points += self.tiles[i][k]
                    self.move_tiles()

    def place_random_tile(self):
        # TODO: Always 2 is being added. Probabilistically 4 should also be added
        positions = list()
        for i in range(self.board_size):
            for j in range(self.board_size):
                if self.tiles[i][j] == 0:
                    positions.append((i,j))
        tile_x,tile_y = random.choice(positions)
        self.tiles[tile_x][tile_y] = 2

    def check_game_status(self):
        for i in range(self.board_size):
            for j in range(self.board_size):
                if self.tiles[i][j] == 0:
                    return True

                try:
                    if self.tiles[i][j] == self.tiles[i][j + 1]:
                        return True
                except IndexError:
                    pass

                try:
                    if self.tiles[i][j] == self.tiles[i+1][j]:
                        return True
                except IndexError:
                    pass
        return False

    def human_player(self):
        while True:
            for event in pygame.event.get():
                if event.type == QUIT:
                    pygame.quit()
                    sys.exit()
                if self.check_game_status():
                    if event.type == KEYDOWN:
                        if self.is_arrow(event.key):
                            rotations = self.get_rotations(event.key)

                            for i in range(rotations):
                                self.rotate_matrix()

                            self.move_tiles()
                            self.merge_tiles()

                            for j in range(4-rotations):
                                self.rotate_matrix()

                            if self.previous_tile is None or (self.previous_tile is not None and not np.array_equal(self.previous_tile,self.tiles)):
                                try:
                                    self.place_random_tile()
                                    self.previous_tile = self.tiles.copy()
                                except IndexError:
                                    pass

                            if self.show:
                                self.show_matrix()

                            self.logger.info("Total score is {}".format(self.total_points))

                else:
                    return self.total_points
            pygame.display.update()

    def bot_simulation(self,key):
        self.logger.info("Running the key {}".format(reverse_mapping[key[0]]))
        rotations = self.get_rotations(key[0])

        for i in range(rotations):
            self.rotate_matrix()

        self.move_tiles()
        self.merge_tiles()

        for j in range(4-rotations):
            self.rotate_matrix()

        if self.show:
            self.show_matrix()
            pygame.display.update()

        if self.previous_tile is None or (
                self.previous_tile is not None and not np.array_equal(self.previous_tile, self.tiles)):
            try:
                self.logger.info("Placing a random tile")
                self.place_random_tile()
                self.previous_tile = self.tiles.copy()

            except IndexError:
                self.logger.info("Invalid position")
                return 300, np.amax(self.tiles)
        else:
            self.logger.info("Invalid position")
            return 300, np.amax(self.tiles)

        if self.show:
            self.show_matrix()
            pygame.display.update()

        if not self.check_game_status():
            self.logger.info(self.tiles)
            self.logger.info("Game over")
            return 500, np.amax(self.tiles)

        if self.show:
            self.show_matrix()
            pygame.display.update()

    def run(self,*args):
        if self.bot:
            return self.bot_simulation(key = args)
        else:
            self.human_player()

class HumanBot:

    def __init__(self):
        self.mapping = {0: pygame.K_UP, 1: pygame.K_LEFT, 2: pygame.K_DOWN, 3: pygame.K_RIGHT}

    def run(self):
        game = Game(show=True, bot=True)
        game.show_matrix()
        pygame.display.update()
        for i in range(1000):
            try:
                direction = int(input())
            except ValueError:
                raise ValueError("Invalid input")

            try:
                direction = self.mapping[direction]
            except KeyError:
                raise ValueError("Please enter 0 for up, 1 for left, 2 for down, 3 for right")
            output = game.run(direction)
            if output is not None:
                break

if __name__== "__main__":
    HumanBot().run()


text:
#External libraries
import sys
import json
import networkx as nx
from networkx.readwrite import json_graph
import httplib2
from xml.dom import minidom
from lxml import etree
#Own libraries
import restconf
import frontend

#Base URLs for Config and operational
baseUrl = 'http://192.168.231.246:8080'
confUrl = baseUrl + '/restconf/config/' #Contains data inserted via controller
operUrl = baseUrl + '/restconf/operational/' # Contains other data
findTopology = operUrl + '/network-topology:network-topology/topology/flow:1/'
#Specific REST URLs

h = httplib2.Http(".cache")
h.add_credentials('admin', 'admin')

flowIdCounter = int(100)
hosts = restconf.get_active_hosts()
srcIP = '10.0.0.1'
destIP = '10.0.0.2'
nodes = restconf.get_topology(restconf.get(findTopology))#['topology'][0]['node']
#print nodes
nodes = restconf.get_topology(restconf.get(findTopology))['topology'][0]['node']

for node in nodes:
    print node['node-id']
    tables = restconf.get('http://192.168.231.246:8080/restconf/operational/opendaylight-inventory:nodes/node/'+node['node-id'])
    flowTables = json.loads(tables)
    #print tables
    try:
        for table in flowTables['node'][0]['flow-node-inventory:table']:
            if table['opendaylight-flow-table-statistics:flow-table-statistics']['opendaylight-flow-table-statistics:active-flows'] != 0:
                print table['flow-node-inventory:id']
                #print confUrl+'opendaylight-inventory:nodes/node/'+node['node-id']+'/table/'+str(table['flow-node-inventory:id'])
                try:
                    flowRules = restconf.get(confUrl+'opendaylight-inventory:nodes/node/'+node['node-id']+'/table/'+str(table['flow-node-inventory:id']))
                    #print confUrl+'opendaylight-inventory:nodes/node/'+node['node-id']+'/table/'+str(table['flow-node-inventory:id'])
                    #flowRules = restconf.get(confUrl+'opendaylight-inventory:nodes/node/openflow:3/table/0')
                    rules = json.loads(flowRules)
                    print rules
                except ValueError:
                    pass
                #print rules['flow-node-inventory:table'][0]['flow-node-inventory:flow']
                #flowRules['flow-node-inventory:table'][0]['flow-node-inventory:flow']
                """try:
                    for rule in flowRules:
                        if rule['flow-node-inventory:match']['flow-node-inventory:ipv4-destination'] == destIP:
                            print "found"
                except KeyError:
                    pass"""
    except KeyError:
        pass
"""
tables = restconf.get('http://192.168.231.246:8080/restconf/operational/opendaylight-inventory:nodes/node/openflow:1')
flowtables = json.loads(tables)
print flowtables['node'][0]['flow-node-inventory:table']
flowRules = restconf.get(confUrl+'opendaylight-inventory:nodes/node/openflow:1/table/0')
rules = json.loads(flowRules)
print rules['flow-node-inventory:table'][0]['flow-node-inventory:flow']
#print flowRules
"""
"""

    #print json.dumps(flows, indent=2)

#content = restconf.get('http://192.168.231.246:8080/restconf/config/opendaylight-inventory:nodes/node/openflow:1/table/0')#node/openflow:1/table/0/')
content = restconf.get('http://192.168.231.246:8080/restconf/operational/opendaylight-inventory:nodes/node/openflow:1')
flows = json.loads(content)
#print flows['flow-node-inventory:table'][0]['flow-node-inventory:flow']

#print json.dumps(flows['flow-node-inventory:table'][0]['flow-node-inventory:flow'], indent=2)
#print json.dumps(flows, indent=2)
"""

max_stars_repo_name: eriksore/sdn

max_issues_repo_name: eriksore/sdn

max_forks_repo_name: eriksore/sdn

reason: import networkx,from networkx


text:
import os
import json
import time
import random

import click
import cv2
import torch
import numpy as np
from PIL import Image

from deepliif.data import create_dataset, AlignedDataset, transform
from deepliif.models import inference, postprocess, compute_overlap, init_nets, DeepLIIFModel
from deepliif.util import allowed_file, Visualizer

def ensure_exists(d):
    if not os.path.exists(d):
        os.makedirs(d)

@click.group()
def cli():
    """Commonly used DeepLIIF batch operations"""
    pass

@cli.command()
@click.option('--dataroot', required=True, type=str,
              help='path to images (should have subfolders trainA, trainB, valA, valB, etc)')
@click.option('--name', default='experiment_name',
              help='name of the experiment. It decides where to store samples and models')
@click.option('--gpu-ids', multiple=True, help='gpu-ids 0 gpu-ids 1 or gpu-ids -1 for CPU')
@click.option('--checkpoints-dir', default='./checkpoints', help='models are saved here')
@click.option('--targets-no', default=5, help='number of targets')
# model parameters
@click.option('--input-nc', default=3, help='# of input image channels: 3 for RGB and 1 for grayscale')
@click.option('--output-nc', default=3, help='# of output image channels: 3 for RGB and 1 for grayscale')
@click.option('--ngf', default=64, help='# of gen filters in the last conv layer')
@click.option('--ndf', default=64, help='# of discrim filters in the first conv layer')
@click.option('--net-d', default='n_layers',
              help='specify discriminator architecture [basic | n_layers | pixel]. The basic model is a 70x70 '
                   'PatchGAN. n_layers allows you to specify the layers in the discriminator')
@click.option('--net-g', default='resnet_9blocks',
              help='specify generator architecture [resnet_9blocks | resnet_6blocks | unet_512 | unet_256 | unet_128]')
@click.option('--n-layers-d', default=4, help='only used if netD==n_layers')
@click.option('--norm', default='batch',
              help='instance normalization or batch normalization [instance | batch | none]')
@click.option('--init-type', default='normal',
              help='network initialization [normal | xavier | kaiming | orthogonal]')
@click.option('--init-gain', default=0.02, help='scaling factor for normal, xavier and orthogonal.')
@click.option('--no-dropout', is_flag=True, help='no dropout for the generator')
# dataset parameters
@click.option('--direction', default='AtoB', help='AtoB or BtoA')
@click.option('--serial-batches', is_flag=True,
              help='if true, takes images in order to make batches, otherwise takes them randomly')
@click.option('--num-threads', default=4, help='# threads for loading data')
@click.option('--batch-size', default=1, help='input batch size')
@click.option('--load-size', default=512, help='scale images to this size')
@click.option('--crop-size', default=512, help='then crop to this size')
@click.option('--max-dataset-size', type=int,
              help='Maximum number of samples allowed per dataset. If the dataset directory contains more than '
                   'max_dataset_size, only a subset is loaded.')
@click.option('--preprocess', type=str,
              help='scaling and cropping of images at load time [resize_and_crop | crop | scale_width | '
                   'scale_width_and_crop | none]')
@click.option('--no-flip', is_flag=True,
              help='if specified, do not flip the images for data augmentation')
@click.option('--display-winsize', default=512, help='display window size for both visdom and HTML')
# additional parameters
@click.option('--epoch', default='latest',
              help='which epoch to load? set to latest to use latest cached model')
@click.option('--load-iter', default=0,
              help='which iteration to load? if load_iter > 0, the code will load models by iter_[load_iter]; '
                   'otherwise, the code will load models by [epoch]')
@click.option('--verbose', is_flag=True, help='if specified, print more debugging information')
@click.option('--lambda-L1', default=100.0, help='weight for L1 loss')
@click.option('--is-train', is_flag=True, default=True)
@click.option('--continue-train', is_flag=True, help='continue training: load the latest model')
@click.option('--epoch-count', type=int, default=0,
              help='the starting  epoch count, we save the model by <epoch_count>, <epoch_count>+<save_latest_freq>')
@click.option('--phase', default='train', help='train, val, test, etc')
# training parameters
@click.option('--n-epochs', type=int, default=100,
              help='number of epochs with the initial learning rate')
@click.option('--n-epochs-decay', type=int, default=100,
              help='number of epochs to linearly decay learning rate to zero')
@click.option('--beta1', default=0.5, help='momentum term of adam')
@click.option('--lr', default=0.0002, help='initial learning rate for adam')
@click.option('--lr-policy', default='linear',
              help='learning rate policy. [linear | step | plateau | cosine]')
@click.option('--lr-decay-iters', type=int, default=50,
              help='multiply by a gamma every lr_decay_iters iterations')
# visdom and HTML visualization parameters
@click.option('--display-freq', default=400, help='frequency of showing training results on screen')
@click.option('--display-ncols', default=4,
              help='if positive, display all images in a single visdom web panel with certain number of images per row.')
@click.option('--display-id', default=1, help='window id of the web display')
@click.option('--display-server', default="http://localhost", help='visdom server of the web display')
@click.option('--display-env', default='main',
              help='visdom display environment name (default is "main")')
@click.option('--display-port', default=8097, help='visdom port of the web display')
@click.option('--update-html-freq', default=1000, help='frequency of saving training results to html')
@click.option('--print-freq', default=100, help='frequency of showing training results on console')
@click.option('--no-html', is_flag=True,
              help='do not save intermediate training results to [opt.checkpoints_dir]/[opt.name]/web/')
# network saving and loading parameters
@click.option('--save-latest-freq', default=500, help='frequency of saving the latest results')
@click.option('--save-epoch-freq', default=100,
              help='frequency of saving checkpoints at the end of epochs')
@click.option('--save-by-iter', is_flag=True, help='whether saves model by iteration')
def train(dataroot, name, gpu_ids, checkpoints_dir, targets_no, input_nc, output_nc, ngf, ndf, net_d, net_g,
          n_layers_d, norm, init_type, init_gain, no_dropout, direction, serial_batches, num_threads,
          batch_size, load_size, crop_size, max_dataset_size, preprocess, no_flip, display_winsize, epoch, load_iter,
          verbose, lambda_l1, is_train, display_freq, display_ncols, display_id, display_server, display_env,
          display_port, update_html_freq, print_freq, no_html, save_latest_freq, save_epoch_freq, save_by_iter,
          continue_train, epoch_count, phase, lr_policy, n_epochs, n_epochs_decay, beta1, lr, lr_decay_iters):
    """General-purpose training script for multi-task image-to-image translation.

    This script works for various models (with option '--model': e.g., DeepLIIF) and
    different datasets (with option '--dataset_mode': e.g., aligned, unaligned, single, colorization).
    You need to specify the dataset ('--dataroot'), experiment name ('--name'), and model ('--model').

    It first creates model, dataset, and visualizer given the option.
    It then does standard network training. During the training, it also visualize/save the images, print/save the loss
    plot, and save models.The script supports continue/resume training.
    Use '--continue_train' to resume your previous training.
    """

    # create a dataset given dataset_mode and other options
    dataset = AlignedDataset(dataroot, load_size, crop_size, input_nc, output_nc, direction, targets_no, preprocess,
                             no_flip, phase, max_dataset_size)

    dataset = create_dataset(dataset, batch_size, serial_batches, num_threads, max_dataset_size)
    # get the number of images in the dataset.
    click.echo('The number of training images = %d' % len(dataset))

    # create a model given model and other options
    model = DeepLIIFModel(gpu_ids, is_train, checkpoints_dir, name, preprocess, targets_no, input_nc, output_nc, ngf,
                          net_g, norm, no_dropout, init_type, init_gain, ndf, net_d, n_layers_d, lr, beta1, lambda_l1,
                          lr_policy)
    # regular setup: load and print networks; create schedulers
    model.setup(lr_policy, epoch_count, n_epochs, n_epochs_decay, lr_decay_iters, continue_train, load_iter, epoch,
                verbose)

    # create a visualizer that display/save images and plots
    visualizer = Visualizer(display_id, is_train, no_html, display_winsize, name, display_port, display_ncols,
                            display_server, display_env, checkpoints_dir)
    # the total number of training iterations
    total_iters = 0

    # outer loop for different epochs; we save the model by <epoch_count>, <epoch_count>+<save_latest_freq>
    for epoch in range(epoch_count, n_epochs + n_epochs_decay + 1):
        # timer for entire epoch
        epoch_start_time = time.time()
        # timer for data loading per iteration
        iter_data_time = time.time()
        # the number of training iterations in current epoch, reset to 0 every epoch
        epoch_iter = 0
        # reset the visualizer: make sure it saves the results to HTML at least once every epoch
        visualizer.reset()

        # inner loop within one epoch
        for i, data in enumerate(dataset):
            # timer for computation per iteration
            iter_start_time = time.time()
            if total_iters % print_freq == 0:
                t_data = iter_start_time - iter_data_time

            total_iters += batch_size
            epoch_iter += batch_size
            # unpack data from dataset and apply preprocessing
            model.set_input(data)
            # calculate loss functions, get gradients, update network weights
            model.optimize_parameters()

            # display images on visdom and save images to a HTML file
            if total_iters % display_freq == 0:
                save_result = total_iters % update_html_freq == 0
                model.compute_visuals()
                visualizer.display_current_results(model.get_current_visuals(), epoch, save_result)

            # print training losses and save logging information to the disk
            if total_iters % print_freq == 0:
                losses = model.get_current_losses()
                t_comp = (time.time() - iter_start_time) / batch_size
                visualizer.print_current_losses(epoch, epoch_iter, losses, t_comp, t_data)
                if display_id > 0:
                    visualizer.plot_current_losses(epoch, float(epoch_iter) / len(dataset), losses)

            # cache our latest model every <save_latest_freq> iterations
            if total_iters % save_latest_freq == 0:
                print('saving the latest model (epoch %d, total_iters %d)' % (epoch, total_iters))
                save_suffix = 'iter_%d' % total_iters if save_by_iter else 'latest'
                model.save_networks(save_suffix)

            iter_data_time = time.time()

        # cache our model every <save_epoch_freq> epochs
        if epoch % save_epoch_freq == 0:
            print('saving the model at the end of epoch %d, iters %d' % (epoch, total_iters))
            model.save_networks('latest')
            model.save_networks(epoch)

        print('End of epoch %d / %d \t Time Taken: %d sec' % (
            epoch, n_epochs + n_epochs_decay, time.time() - epoch_start_time))
        # update learning rates at the end of every epoch.
        model.update_learning_rate()

@cli.command()
@click.option('--input-dir', default='./Sample_Large_Tissues/', help='reads images from here')
@click.option('--output-dir', help='saves results here.')
@click.option('--tile-size', default=512, help='tile size')
def test(input_dir, output_dir, tile_size):
    """Test trained models
    """
    output_dir = output_dir or input_dir
    ensure_exists(output_dir)

    image_files = [fn for fn in os.listdir(input_dir) if allowed_file(fn)]

    with click.progressbar(
            image_files,
            label=f'Processing {len(image_files)} images',
            item_show_func=lambda fn: fn
    ) as bar:
        for filename in bar:
            img = Image.open(os.path.join(input_dir, filename))

            images = inference(
                img,
                tile_size=tile_size,
                overlap_size=compute_overlap(img.size, tile_size)
            )

            post_images, scoring = postprocess(img, images['Seg'])
            images = {**images, **post_images}

            for name, i in images.items():
                i.save(os.path.join(
                    output_dir,
                    filename.replace('.' + filename.split('.')[-1], f'_{name}.png')
                ))

            with open(os.path.join(
                    output_dir,
                    filename.replace('.' + filename.split('.')[-1], f'.json')
            ), 'w') as f:
                json.dump(scoring, f, indent=2)

@cli.command()
@click.option('--models-dir', default='./model-server/DeepLIIF_Latest_Model', help='reads models from here')
@click.option('--output-dir', help='saves results here.')
def serialize(models_dir, output_dir):
    """Serialize DeepLIIF models using Torchscript
    """
    output_dir = output_dir or models_dir

    sample = transform(Image.new('RGB', (512, 512)))

    with click.progressbar(
            init_nets(models_dir, eager_mode=True).items(),
            label='Tracing nets',
            item_show_func=lambda n: n[0] if n else n
    ) as bar:
        for name, net in bar:
            traced_net = torch.jit.trace(net, sample)
            traced_net.save(f'{output_dir}/{name}.pt')

@cli.command()
@click.option('--input-dir', type=str, required=True, help='Path to input images')
@click.option('--output-dir', type=str, required=True, help='Path to output images')
@click.option('--validation-ratio', default=0.2,
              help='The ratio of the number of the images in the validation set to the total number of images')
def prepare_training_data(input_dir, output_dir, validation_ratio):
    """Preparing data for training

    This function, first, creates the train and validation directories inside the given dataset directory.
    Then it reads all images in the folder and saves the pairs in the train or validation directory, based on the given
    validation_ratio.
    *** for training, you need to have paired data including IHC, Hematoxylin Channel, mpIF DAPI, mpIF Lap2, mpIF
    marker, and segmentation mask in the input directory ***

    :param input_dir: Path to the input images.
    :param outputt_dir: Path to the dataset directory. The function automatically creates the train and validation
        directories inside of this directory.
    :param validation_ratio: The ratio of the number of the images in the validation set to the total number of images.
    :return:
    """
    train_dir = os.path.join(output_dir, 'train')
    val_dir = os.path.join(output_dir, 'val')
    if not os.path.exists(train_dir):
        os.makedirs(train_dir, exist_ok=True)
    if not os.path.exists(val_dir):
        os.makedirs(val_dir, exist_ok=True)
    images = os.listdir(input_dir)
    for img in images:
        if 'IHC' in img:
            IHC_image = cv2.resize(cv2.imread(os.path.join(input_dir, img)), (512, 512))
            Hema_image = cv2.resize(cv2.imread(os.path.join(input_dir, img.replace('IHC', 'Hematoxylin'))), (512, 512))
            DAPI_image = cv2.resize(cv2.imread(os.path.join(input_dir, img.replace('IHC', 'DAPI'))), (512, 512))
            Lap2_image = cv2.resize(cv2.imread(os.path.join(input_dir, img.replace('IHC', 'Lap2'))), (512, 512))
            Marker_image = cv2.resize(cv2.imread(os.path.join(input_dir, img.replace('IHC', 'Marker'))), (512, 512))
            Seg_image = cv2.resize(cv2.imread(os.path.join(input_dir, img.replace('IHC', 'Seg'))), (512, 512))

            save_dir = train_dir
            if random.random() < validation_ratio:
                save_dir = val_dir
            cv2.imwrite(os.path.join(save_dir, img),
                        np.concatenate([IHC_image, Hema_image, DAPI_image, Lap2_image, Marker_image, Seg_image], 1))

@cli.command()
@click.option('--input_dir', required=True, help='path to input images')
@click.option('--output_dir', type=str, help='path to output images')
def prepare_testing_data(input_dir, dataset_dir):
    """Preparing data for testing

    This function, first, creates the test directory inside the given dataset directory.
    Then it reads all images in the folder and saves pairs in the test directory.
    *** for testing, you only need to have IHC images in the input directory ***

    :param input_dir: Path to the input images.
    :param dataset_dir: Path to the dataset directory. The function automatically creates the train and validation
        directories inside of this directory.
    :return:
    """
    test_dir = os.path.join(dataset_dir, 'test')
    if not os.path.exists(test_dir):
        os.mkdir(test_dir)
    images = os.listdir(input_dir)
    for img in images:
        if 'IHC' in img:
            image = cv2.resize(cv2.imread(os.path.join(input_dir, img)), (512, 512))
            cv2.imwrite(os.path.join(test_dir, img), np.concatenate([image, image, image, image, image, image], 1))

if __name__ == '__main__':
    cli()


text:
import logging
import os
import shutil
import time
import paddle
from datetime import datetime, timedelta
from distutils.dir_util import mkpath
import numpy as np
import paddle.fluid as fluid
from visualdl import LogWriter
from utils.error_rate import char_errors, word_errors
from decoders.ctc_greedy_decoder import greedy_decoder_batch
from model_utils.network import deep_speech_v2_network

logging.basicConfig(format='[%(levelname)s %(asctime)s %(filename)s:%(lineno)d] %(message)s')
paddle.enable_static()

class DeepSpeech2Model(object):
    """DeepSpeech2Model class.

    :param vocab_size: 词汇表大小
    :type vocab_size: int
    :param num_conv_layers: 叠加卷积层数
    :type num_conv_layers: int
    :param num_rnn_layers: 叠加RNN层数
    :type num_rnn_layers: int
    :param rnn_layer_size: RNN层大小
    :type rnn_layer_size: int
    :param place: Program running place.
    :type place: CPUPlace or CUDAPlace
    :param resume_model: 恢复模型路径
    :type resume_model: string|None
    :param pretrained_model: 预训练模型路径
    :type pretrained_model: string|None
    :param output_model_dir: 保存模型的路径
    :type output_model_dir: string|None
    :param error_rate_type: 测试计算错误率的方式
    :type error_rate_type: string|None
    :param vocab_list: 词汇表列表
    :type vocab_list: list|None
    :param blank: 损失函数的空白索引
    :type blank: int
    """

    def __init__(self,
                 vocab_size,
                 num_conv_layers,
                 num_rnn_layers,
                 rnn_layer_size,
                 place=paddle.CPUPlace(),
                 resume_model=None,
                 pretrained_model=None,
                 output_model_dir=None,
                 error_rate_type='cer',
                 vocab_list=None,
                 blank=0):
        self._vocab_size = vocab_size
        self._num_conv_layers = num_conv_layers
        self._num_rnn_layers = num_rnn_layers
        self._rnn_layer_size = rnn_layer_size
        self._place = place
        self._blank = blank
        self._pretrained_model = pretrained_model
        self._resume_model = resume_model
        self._output_model_dir = output_model_dir
        self._ext_scorer = None
        self.logger = logging.getLogger("")
        self.logger.setLevel(level=logging.INFO)
        self.error_rate_type = error_rate_type
        self.vocab_list = vocab_list
        self.save_model_path = ''
        # 预测相关的参数
        self.infer_program = None
        self.infer_compiled_prog = None
        self.infer_feeder = None
        self.infer_log_probs = None
        self.infer_exe = None

    def create_network(self, is_infer=False):
        """Create data layers and model network.
        :param is_infer: Whether to create a network for Inference.
        :type is_infer: bool
        :return reader: Reader for input.
        :rtype reader: read generater
        :return log_probs: An output unnormalized log probability layer.
        :rtype lig_probs: Varable
        :return loss: A ctc loss layer.
        :rtype loss: Variable
        """

        if not is_infer:
            input_fields = {
                'names': ['audio_data', 'text_data', 'seq_len_data', 'masks'],
                'shapes': [[None, 161, None], [None, 1], [None, 1], [None, 32, 81, None]],
                'dtypes': ['float32', 'int32', 'int64', 'float32'],
                'lod_levels': [0, 1, 0, 0]
            }

            inputs = [
                paddle.static.data(name=input_fields['names'][i],
                                   shape=input_fields['shapes'][i],
                                   dtype=input_fields['dtypes'][i],
                                   lod_level=input_fields['lod_levels'][i])
                for i in range(len(input_fields['names']))
            ]

            reader = fluid.io.DataLoader.from_generator(feed_list=inputs,
                                                        capacity=64,
                                                        iterable=False,
                                                        use_double_buffer=True)

            (audio_data, text_data, seq_len_data, masks) = inputs
        else:
            audio_data = paddle.static.data(name='audio_data',
                                            shape=[None, 161, None],
                                            dtype='float32',
                                            lod_level=0)
            seq_len_data = paddle.static.data(name='seq_len_data',
                                              shape=[None, 1],
                                              dtype='int64',
                                              lod_level=0)
            masks = paddle.static.data(name='masks',
                                       shape=[None, 32, 81, None],
                                       dtype='float32',
                                       lod_level=0)
            text_data = None
            reader = fluid.DataFeeder([audio_data, seq_len_data, masks], self._place)

        log_probs, loss = deep_speech_v2_network(audio_data=audio_data,
                                                 text_data=text_data,
                                                 seq_len_data=seq_len_data,
                                                 masks=masks,
                                                 dict_size=self._vocab_size,
                                                 num_conv_layers=self._num_conv_layers,
                                                 num_rnn_layers=self._num_rnn_layers,
                                                 rnn_size=self._rnn_layer_size,
                                                 blank=self._blank)
        return reader, log_probs, loss

    # 加载模型
    def load_param(self, program, model_path, ignore_opt=False):
        if not os.path.exists(model_path):
            raise Warning("The pretrained params [%s] do not exist." % model_path)

        load_state_dict = paddle.load(model_path)
        if ignore_opt:
            for key in program.state_dict(mode='opt').keys():
                load_state_dict.pop(key)
        program.set_state_dict(load_state_dict)
        print('[{}] 成功加载模型：{}'.format(datetime.now(), model_path))

    # 保存模型
    def save_param(self, program, epoch):
        if not os.path.exists(self._output_model_dir):
            os.mkdir(self._output_model_dir)
        model_path = '{}/{}.pdparams'.format(self._output_model_dir, epoch)
        paddle.save(program.state_dict(), model_path)
        old_model_path = '{}/{}.pdparams'.format(self._output_model_dir, epoch - 3)
        if os.path.exists(old_model_path):
            os.remove(old_model_path)
        print("模型已保存在：%s" % model_path)
        return model_path

    def train(self,
              train_batch_reader,
              dev_batch_reader,
              learning_rate,
              gradient_clipping,
              num_epoch,
              batch_size,
              num_samples,
              test_off=False):
        """Train the model.

        :param train_batch_reader: Train data reader.
        :type train_batch_reader: callable
        :param dev_batch_reader: Validation data reader.
        :type dev_batch_reader: callable
        :param learning_rate: Learning rate for ADAM optimizer.
        :type learning_rate: float
        :param gradient_clipping: Gradient clipping threshold.
        :type gradient_clipping: float
        :param num_epoch: Number of training epochs.
        :type num_epoch: int
        :param batch_size: Number of batch size.
        :type batch_size: int
        :param num_samples: The num of train samples.
        :type num_samples: int
        :param test_off: Turn off testing.
        :type test_off: bool
        """
        shutil.rmtree('log', ignore_errors=True)
        writer = LogWriter(logdir='log')
        # prepare model output directory
        if not os.path.exists(self._output_model_dir):
            mkpath(self._output_model_dir)

        if isinstance(self._place, paddle.CUDAPlace):
            dev_count = len(paddle.static.cuda_places())
        else:
            dev_count = int(os.environ.get('CPU_NUM', 1))

        pre_epoch = 0
        if self._resume_model:
            try:
                pre_epoch = os.path.basename(self._resume_model).split('.')[0]
                pre_epoch = int(pre_epoch)
            except:
                print("恢复模型命名不正确，epoch从0开始训练！")

        # prepare the network
        train_program = paddle.static.Program()
        startup_prog = paddle.static.Program()
        with paddle.static.program_guard(train_program, startup_prog):
            with paddle.utils.unique_name.guard():
                train_reader, _, ctc_loss = self.create_network()
                # 学习率
                scheduler = paddle.optimizer.lr.ExponentialDecay(learning_rate=learning_rate, gamma=0.83,
                                                                 last_epoch=pre_epoch - 1)
                # 准备优化器
                optimizer = paddle.optimizer.Adam(
                    learning_rate=scheduler,
                    weight_decay=paddle.regularizer.L2Decay(5e-4),
                    grad_clip=paddle.nn.ClipGradByGlobalNorm(clip_norm=gradient_clipping))
                optimizer.minimize(loss=ctc_loss)

        exe = paddle.static.Executor(self._place)
        exe.run(startup_prog)

        # 加载预训练模型
        if self._resume_model is not None or self._pretrained_model is not None:
            if self._resume_model is not None:
                self.load_param(train_program, self._resume_model)
            else:
                self.load_param(train_program, self._pretrained_model, ignore_opt=True)

        build_strategy = paddle.static.BuildStrategy()
        exec_strategy = paddle.static.ExecutionStrategy()

        # pass the build_strategy to with_data_parallel API
        train_compiled_prog = paddle.static.CompiledProgram(train_program) \
            .with_data_parallel(loss_name=ctc_loss.name,
                                build_strategy=build_strategy,
                                exec_strategy=exec_strategy)

        train_reader.set_batch_generator(train_batch_reader)

        train_step = 0
        test_step = 0
        num_batch = num_samples // batch_size // dev_count
        sum_batch = num_batch * (num_epoch - pre_epoch)
        # run train
        for epoch_id in range(pre_epoch, num_epoch):
            epoch_id += 1
            train_reader.start()
            epoch_loss = []
            time_begin = time.time()
            batch_id = 0
            while True:
                try:
                    if batch_id % 100 == 0:
                        start = time.time()
                        # 执行训练
                        fetch = exe.run(program=train_compiled_prog, fetch_list=[ctc_loss.name], return_numpy=False)
                        each_loss = fetch[0]
                        epoch_loss.extend(np.array(each_loss[0]) / batch_size)

                        eta_sec = ((time.time() - start) * 1000) * (
                                sum_batch - (epoch_id - pre_epoch - 1) * num_batch - batch_id)
                        eta_str = str(timedelta(seconds=int(eta_sec / 1000)))
                        print(
                            "Train [%s] epoch: [%d/%d], batch: [%d/%d], learning rate: %.8f, train loss: %f, eta: %s" %
                            (datetime.now(), epoch_id, num_epoch, batch_id, num_batch, scheduler.get_lr(),
                             np.mean(each_loss[0]) / batch_size, eta_str))
                        # 记录训练损失值
                        writer.add_scalar('Train loss', np.mean(each_loss[0]) / batch_size, train_step)
                        writer.add_scalar('Learning rate', scheduler.get_lr(), train_step)
                        train_step += 1
                    else:
                        # 执行训练
                        _ = exe.run(program=train_compiled_prog, fetch_list=[], return_numpy=False)
                    # 每10000个batch保存一次模型
                    if batch_id % 10000 == 0 and batch_id != 0:
                        self.save_param(train_program, epoch_id)
                    batch_id = batch_id + 1
                except fluid.core.EOFException:
                    train_reader.reset()
                    break
            scheduler.step()
            # 每一个epoch保存一次模型
            self._resume_model = self.save_param(train_program, epoch_id)
            used_time = time.time() - time_begin
            if test_off:
                print('======================last Train=====================')
                print("Train time: %f sec, epoch: %d, train loss: %f\n" %
                      (used_time, epoch_id, float(np.mean(np.array(epoch_loss)))))
                print('======================last Train=====================')
            else:
                print('\n======================Begin test=====================')
                # 执行测试
                test_result = self.test(test_reader=dev_batch_reader)
                print("Train time: %s, epoch: %d, train loss: %f, test %s: %f"
                      % (str(timedelta(seconds=int(used_time))), epoch_id, float(np.mean(np.array(epoch_loss))),
                         self.error_rate_type, test_result))
                print('======================Stop Test=====================\n')
                # 记录测试结果
                writer.add_scalar('Test %s' % self.error_rate_type, test_result, test_step)
                test_step += 1

        self.save_param(train_program, num_epoch)
        print("\n------------Training finished!!!-------------")

    def test(self, test_reader):
        '''Test the model.

        :param test_reader: Reader of test.
        :type test_reader: Reader
        :return: Wer/Cer rate.
        :rtype: float
        '''
        # 初始化预测程序
        self.create_infer_program()
        # 加载预训练模型
        self.load_param(self.infer_program, self._resume_model)
        errors_sum, len_refs = 0.0, 0
        errors_func = char_errors if self.error_rate_type == 'cer' else word_errors
        for infer_data in test_reader():
            # 执行预测
            probs_split = self.infer_batch_data(infer_data=infer_data)
            # 使用最优路径解码
            result_transcripts = greedy_decoder_batch(probs_split=probs_split, vocabulary=self.vocab_list)
            target_transcripts = infer_data[1]
            # 计算字错率
            for target, result in zip(target_transcripts, result_transcripts):
                errors, len_ref = errors_func(target, result)
                errors_sum += errors
                len_refs += len_ref
        return errors_sum / len_refs

    # 预测一个batch的音频
    def infer_batch_data(self, infer_data):
        """Infer the prob matrices for a batch of speech utterances.
        :param infer_data: List of utterances to infer, with each utterance
                           consisting of a tuple of audio features and
                           transcription text (empty string).
        :type infer_data: list
        :return: List of 2-D probability matrix, and each consists of prob
                 vectors for one speech utterancce.
        :rtype: List of matrix
        """
        # define inferer
        if self.infer_exe is None:
            # 初始化预测程序
            self.create_infer_program()
            # 加载预训练模型
            self.load_param(self.infer_program, self._resume_model)
        infer_results = []
        data = []
        if isinstance(self._place, paddle.CUDAPlace):
            num_places = len(paddle.static.cuda_places())
        else:
            num_places = int(os.environ.get('CPU_NUM', 1))
        # 开始预测
        for i in range(infer_data[0].shape[0]):
            # 使用多卡推理
            data.append([[infer_data[0][i], infer_data[2][i], infer_data[3][i]]])
            if len(data) == num_places:
                each_log_probs = self.infer_exe.run(program=self.infer_compiled_prog,
                                                    feed=list(self.infer_feeder.feed_parallel(
                                                        iterable=data, num_places=num_places)),
                                                    fetch_list=[self.infer_log_probs],
                                                    return_numpy=False)
                data = []
                infer_results.extend(np.array(each_log_probs[0]))
        # 如果数据是单数，就获取最后一个计算
        last_data_num = infer_data[0].shape[0] % num_places
        if last_data_num != 0:
            for i in range(infer_data[0].shape[0] - last_data_num, infer_data[0].shape[0]):
                each_log_probs = self.infer_exe.run(program=self.infer_program,
                                                    feed=self.infer_feeder.feed(
                                                        [[infer_data[0][i], infer_data[2][i], infer_data[3][i]]]),
                                                    fetch_list=[self.infer_log_probs],
                                                    return_numpy=False)
                infer_results.extend(np.array(each_log_probs[0]))

        # slice result
        infer_results = np.array(infer_results)
        seq_len = (infer_data[2] - 1) // 3 + 1

        start_pos = [0] * (infer_data[0].shape[0] + 1)
        for i in range(infer_data[0].shape[0]):
            start_pos[i + 1] = start_pos[i] + seq_len[i][0]
        probs_split = [
            infer_results[start_pos[i]:start_pos[i + 1]]
            for i in range(0, infer_data[0].shape[0])
        ]

        return probs_split

    # 初始化预测程序，加预训练模型
    def create_infer_program(self):
        # define inferer
        self.infer_program = paddle.static.Program()
        startup_prog = paddle.static.Program()

        # prepare the network
        with paddle.static.program_guard(self.infer_program, startup_prog):
            with paddle.utils.unique_name.guard():
                self.infer_feeder, self.infer_log_probs, _ = self.create_network(is_infer=True)

        self.infer_program = self.infer_program.clone(for_test=True)
        self.infer_exe = paddle.static.Executor(self._place)
        self.infer_exe.run(startup_prog)

        # 支持多卡推理
        build_strategy = paddle.static.BuildStrategy()
        exec_strategy = paddle.static.ExecutionStrategy()
        self.infer_compiled_prog = paddle.static.CompiledProgram(self.infer_program) \
            .with_data_parallel(build_strategy=build_strategy,
                                exec_strategy=exec_strategy)

    # 导出预测模型
    def export_model(self, model_path):
        self.create_infer_program()
        # 加载预训练模型
        self.load_param(self.infer_program, self._resume_model)
        audio_data = paddle.static.data(name='audio_data',
                                        shape=[None, 161, None],
                                        dtype='float32',
                                        lod_level=0)
        seq_len_data = paddle.static.data(name='seq_len_data',
                                          shape=[None, 1],
                                          dtype='int64',
                                          lod_level=0)
        masks = paddle.static.data(name='masks',
                                   shape=[None, 32, 81, None],
                                   dtype='float32',
                                   lod_level=0)
        if not os.path.exists(os.path.dirname(model_path)):
            os.makedirs(os.path.dirname(model_path))
        paddle.static.save_inference_model(path_prefix=model_path + '/inference',
                                           feed_vars=[audio_data, seq_len_data, masks],
                                           fetch_vars=[self.infer_log_probs],
                                           executor=self.infer_exe,
                                           program=self.infer_program)

max_stars_repo_name: yeyupiaoling/PaddlePaddle-DeepSpeech

max_issues_repo_name: yeyupiaoling/PaddlePaddle-DeepSpeech

max_forks_repo_name: yeyupiaoling/PaddlePaddle-DeepSpeech


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

"""The data layer used during training to train a Fast R-CNN network.

RoIDataLayer implements a Caffe Python layer.
"""

import caffe
from fast_rcnn.config import cfg
from vrd.wsd.mini_batch import get_minibatch
import numpy as np
import yaml
#from multiprocessing import Process, Queue
import pdb
import zl_config as C
import glog
class RoIDataLayerEbBinaryLogLoss(caffe.Layer):
    """Fast R-CNN data layer used for training."""

    def _shuffle_roidb_inds(self):
        """Randomly permute the training roidb."""
        if cfg.TRAIN.ASPECT_GROUPING:
            widths = np.array([r['width'] for r in self._roidb])
            heights = np.array([r['height'] for r in self._roidb])
            horz = (widths >= heights)
            vert = np.logical_not(horz)
            horz_inds = np.where(horz)[0]
            vert_inds = np.where(vert)[0]
            inds = np.hstack((
                np.random.permutation(horz_inds),
                np.random.permutation(vert_inds)))
            inds = np.reshape(inds, (-1, 2))
            row_perm = np.random.permutation(np.arange(inds.shape[0]))
            inds = np.reshape(inds[row_perm, :], (-1,))
            self._perm = inds
        else:
            self._perm = np.random.permutation(np.arange(len(self._roidb)))
        self._cur = 0

    def _get_next_minibatch_inds(self):
        """Return the roidb indices for the next minibatch."""
        if self._cur + cfg.TRAIN.IMS_PER_BATCH >= len(self._roidb):
            self._shuffle_roidb_inds()

        db_inds = self._perm[self._cur:self._cur + cfg.TRAIN.IMS_PER_BATCH]
        self._cur += cfg.TRAIN.IMS_PER_BATCH
        return db_inds

    def _get_next_minibatch(self):
        """Return the blobs to be used for the next minibatch.
        If cfg.TRAIN.USE_PREFETCH is True, then blobs will be computed in a
        separate process and made available through self._blob_queue.
        """
        if cfg.TRAIN.USE_PREFETCH:
            return self._blob_queue.get()
        else:
            db_inds = self._get_next_minibatch_inds()
            minibatch_db = [self._roidb[i] for i in db_inds]
            return get_minibatch(minibatch_db, self._num_classes)

    def set_roidb(self, roidb):
        """Set the roidb to be used by this layer during training."""
        self._roidb = roidb
        self._shuffle_roidb_inds()

    def setup(self, bottom, top):
        """Setup the RoIDataLayer."""

        # parse the layer parameter string, which must be valid YAML
        layer_params = yaml.load(self.param_str)

        self._num_classes = layer_params['num_classes']
        self._name_to_top_map = {}

        # data blob: holds a batch of N images, each with 3 channels
        idx = 0
        top[idx].reshape(cfg.TRAIN.IMS_PER_BATCH, 3,
                         max(cfg.TRAIN.SCALES), cfg.TRAIN.MAX_SIZE)
        self._name_to_top_map['data'] = idx
        idx += 1

        ##todo necessary?
        # top[idx].reshape(1, 3)
        # self._name_to_top_map['im_info'] = idx
        # idx += 1
        # rois blob: holds R regions of interest, each is a 5-tuple
        # (n, x1, y1, x2, y2) specifying an image batch index n and a
        # rectangle (x1, y1, x2, y2)
        top[idx].reshape(1, 5, 1, 1)
        self._name_to_top_map['gt_boxes'] = idx
        idx += 1

        # labels blob: R categorical labels in [0, ..., K] for K foreground
        # classes plus background
        top[idx].reshape(1, 1, 1, 1)
        self._name_to_top_map['labels'] = idx
        idx += 1

        top[idx].reshape(1, 20, 1, 1)
        self._name_to_top_map['labels_v'] = idx
        idx += 1
        #top[idx].reshape(1, 1000, 1, 1)
        #self._name_to_top_map['word_embeddings'] = idx
        #idx += 1

        #top[idx].reshape(1, 20, 1, 1)
        #self._name_to_top_map['regression_targets'] = idx
        #idx += 1

        #print 'RoiDataLayer: name_to_top:', self._name_to_top_map
        #assert len(top) == len(self._name_to_top_map)

    def forward(self, bottom, top):
        """Get blobs and copy them into this layer's top blob vector."""
        # todo: modify mini_batch
        blobs = self._get_next_minibatch()
        labels = blobs['labels']
        gt_boxes = blobs['gt_boxes']
        im_info = blobs['im_info']
        #gt_boxes = gt_boxes
        #blobs['labels']=blobs['gt_boxes']
        #blobs['word_embeddings'] = np.zeros((1,1,1,1),np.float32)
        #blobs['regression_targets'] = np.zeros((1,20,1,1),np.float32)

        #for blob_name, blob in blobs.iteritems():
        #    top_ind = self._name_to_top_map[blob_name]
        #    shape = blob.shape
        #    top[top_ind].reshape(*(blob.shape))
        #    # Copy data into net's input blobs
        #    top[top_ind].data[...] = blob.astype(np.float32, copy=False)

        top[0].reshape(*blobs['data'].shape)
        top[0].data[...] = blobs['data']

        gt_boxes = gt_boxes.reshape((gt_boxes.shape[0],gt_boxes.shape[1],1,1))
        zeros = np.zeros((gt_boxes.shape[0],1,1, 1), dtype=gt_boxes.dtype)
        all_rois = np.concatenate((zeros, gt_boxes),axis=1)

        top[1].reshape(*all_rois.shape)
        top[1].data[...] = all_rois
        #labels = np.concatenate((labels,np.array([0,0,0])))
        labels = np.unique(labels)
        labels_v = np.zeros((20))

        labels_v[...]=-1
        labels_v[labels.astype(np.int32)-1]=1
        #labels_v = np.tile(labels_v,labels.shape[0])

        labels_v = labels_v.reshape((1,20))
        #for i in xrange(labels.shape[0]):
        #    labels_v[i,labels[i]]=1
        labels = labels.reshape((labels.shape[0],1,1,1))
        #labels[-1,0,0,0]=0
        top[2].reshape(*labels.shape)
        top[2].data[...] = labels

        top[3].reshape(*labels_v.shape)
        top[3].data[...] =labels_v# labels

        #embeddings = np.zeros((gt_boxes.shape[0],1,1, 1), dtype=gt_boxes.dtype)
        #top[3].reshape(*embeddings.shape)
        #top[3].data[...] = embeddings

        #regression_targets = np.zeros((gt_boxes.shape[0],20,1, 1), dtype=gt_boxes.dtype)
        #top[4].reshape(*regression_targets.shape)
        #top[4].data[...] = regression_targets
        #glog.info('here')
        #pdb.set_trace()

    def backward(self, top, propagate_down, bottom):
        """This layer does not propagate gradients."""
        pass

    def reshape(self, bottom, top):
        """Reshaping happens during the call to forward."""
        pass


text:
import logging
import multiprocessing
import time

from random import randrange

import numpy as np

from .. import expr, ir_pass

def get_const_int(exp):
    """Verifies expr is integer and get the constant value.

    Parameters
    ----------
    exp : tvm.Expr or int
        The input expression.

    Returns
    -------
    out_value : int
        The output.
    """
    if isinstance(exp, int):
        return exp
    if not isinstance(exp, (expr.IntImm, expr.UIntImm)):
        exp = ir_pass.Simplify(exp)
    if not isinstance(exp, (expr.IntImm, expr.UIntImm)):
        raise ValueError("Expect value to be constant int")
    return exp.value

def get_const_tuple(in_tuple):
    """Verifies input tuple is IntImm or Var, returns tuple of int or Var.

    Parameters
    ----------
    in_tuple : tuple of Expr
        The input.

    Returns
    -------
    out_tuple : tuple of int
        The output.
    """
    ret = []
    for elem in in_tuple:
        if isinstance(elem, expr.Var):
            ret.append(elem)
        elif not isinstance(elem, (expr.IntImm, expr.UIntImm, int)):
            elem = ir_pass.Simplify(elem)
            if not isinstance(elem, (expr.IntImm, expr.UIntImm)):
                ret.append(elem)
        else:
            ret.append(get_const_int(elem))
    return tuple(ret)

max_stars_repo_name: csullivan/ffi-navigator

max_issues_repo_name: csullivan/ffi-navigator

max_forks_repo_name: csullivan/ffi-navigator


text:
import pandas as pd
import numpy as np
import pyBigWig
import pdb
from itertools import islice

def open_bigwig_for_parsing(fname):
    return fname

def open_csv_for_parsing(fname):
    return fname

def parse_bigwig_chrom_vals(bigwig_name,chrom,start,end,cur_attribute_info):
    store_summits=None
    summit_indicator=None
    if 'store_summits' in cur_attribute_info:
        store_summits=cur_attribute_info['store_summits']
    if 'summit_indicator' in cur_attribute_info:
        summit_indicator=cur_attribute_info['summit_indicator']
    bigwig_object=pyBigWig.open(bigwig_name)
    #note: pybigwig uses NA in place of 0 where there are no reads, replace with 0.
    #check to see if chromosome in bigwig, if not, return all NA's & warning that chromosome is not present in the dataset
    bw_chroms=bigwig_object.chroms().keys()
    if chrom not in bw_chroms:
        print("WARNING: chromosome:"+str(chrom) +" was not found in the bigwig file:"+str(bigwig_name))
        size=(end-start+1)
        signal_data=np.full(size,np.nan)
    else:
        signal_data=np.nan_to_num(bigwig_object.values(chrom,start,end))
    return signal_data

def parse_narrowPeak_chrom_vals(narrowPeak_fname,chrom,start,end,cur_attribute_info):
    store_summits=None
    summit_indicator=None
    if 'store_summits' in cur_attribute_info:
        store_summits=cur_attribute_info['store_summits']
    if 'summit_indicator' in cur_attribute_info:
        summit_indicator=cur_attribute_info['summit_indicator']

    narrowPeak_df=pd.read_csv(narrowPeak_fname,header=None,sep='\t')
    signal_data = np.zeros(end, dtype=np.int)
    chrom_subset_df=narrowPeak_df[narrowPeak_df[0]==chrom]
    del narrowPeak_df
    nitems_in_row=chrom_subset_df.shape[1]
    if (store_summits==True) and (chrom_subset_df.shape[1]<10):
        print("warning! dataset does not have 10 columns of the standard narrowPeak format, falling back to peak centers")
    summits=[]
    for index,row in chrom_subset_df.iterrows():
        signal_data[row[1]:row[2]]=1
        #add in summits in a separate step to avoid overwriting them with "1's" for overlaping peak coordinates;
        #The overwriting issue is particularly relevant for pseudobulk data.
        if store_summits is True:
            if len(row)<10:
                summit_pos=int(round(row[1]+0.5*(row[2]-row[1])))
            else:
                summit_pos=row[1]+row[nitems_in_row-1]
            summits.append(summit_pos)
    if store_summits is True:
        signal_data[summits]=summit_indicator
    return signal_data