Skip to content

Instantly share code, notes, and snippets.

Avatar

e96031413

View GitHub Profile
View plot_decision_boundary.py
# ref: https://blog.csdn.net/MachineLearner/article/details/104587288
def plot_decision_boundary(pred_func, X, y, figure=None):
"""Plot a decision boundary"""
if figure is None: # If no figure is given, create a new one
plt.figure()
# Set min and max values and give it some padding
x_min, x_max = X[:, 0].min() - .5, X[:, 0].max() + .5
y_min, y_max = X[:, 1].min() - .5, X[:, 1].max() + .5
h = 0.01
View ListAndDictReorder.py
# 無重複數字
# input: name_list = [0, 1, 2, 3, 8]
for idx, num in enumerate(set(name_list)):
if idx != num:
name_list[idx] = idx
# output: name_list = [0, 1, 2, 3, 4]
# 有重複數字
# input: name_list = [0, 2, 2, 1, 6, 6]
for idx, num in enumerate(set(name_list)):
View metric_learning_NMI.py
# evaluate the clustering performance
from sklearn.cluster import KMeans
from sklearn.metrics.cluster import normalized_mutual_info_score
import numpy as np
def evaluation(X, Y):
classN = np.max(Y)+1
kmeans = KMeans(n_clusters=classN).fit(X)
nmi = normalized_mutual_info_score(Y, kmeans.labels_, average_method='arithmetic')
return nmi
View TensorFlow_Load_Image_from_Dataframe.py
# https://stackoverflow.com/questions/63761717/load-image-dataset
# https://stackoverflow.com/questions/60655280/how-to-split-an-image-dataset-in-x-train-y-train-x-test-y-test-by-tensorflow
import tensorflow as tf
import pandas as pd
train_df = pd.read_csv('train.csv')
train_df['class'] = train_df['class'].apply(str)
train_datagen = tf.keras.preprocessing.image.ImageDataGenerator(horizontal_flip=True, vertical_flip=True,)
View RocAucScore.py
import numpy as np
from sklearn import metrics
from sklearn.metrics import roc_auc_score
y = np.array([1, 1, 2, 2])
#scores可以是模型預測結果(Label)
#scores也可以是模型預測的confidence(softmax probability)
scores = np.array([1, 1, 2, 2])
scores = np.array([0.1, 0.4, 0.35, 0.8])
area_under_curve = roc_auc_score(y, scores)
View decisionBoundary.py
def eval(loader, gt_labels_t, output_file="output.txt"):
G.eval() # 特徵提取器
F1.eval() # 分類器
size = 0
correct = 0
y_pred=[]
y_true=[]
pred_prob = None
pred_result = None
View FocalLoss.py
import torch.nn.functional as F
import torch
import numpy as np
from torch.autograd import Function
import torch.nn as nn
from pdb import set_trace as breakpoint
import sys
import math
from torch.nn.parameter import Parameter
from torch.nn import init
View LabelSmoothing_public.py
# LabelSmoothing.py
# https://www.aiuai.cn/aifarm1333.html 示例 3
# From: Github - NVIDIA/DeepLearningExamples/PyTorch/Classification
# smoothing.py
import torch
import torch.nn as nn
# 一般版本LabelSmoothing
class LabelSmoothing(nn.Module):
View multiple-y-axis-value-in-same-plot.py
# https://stackoverflow.com/a/45925049/13369757
import matplotlib.pyplot as plt
import numpy as np
fig, host = plt.subplots(figsize=(12,10)) # (width, height) in inches
par1 = host.twinx()
par2 = host.twinx()
host.set_xlabel("Threshold")
View extractDatasetFeatureToPickle.py
import numpy as np
import torch
import pickle
from torch.utils.data import Dataset, DataLoader
# 提取特徵
def extract(loader):
featureExtractor.eval()
features = None
with torch.no_grad():