This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 無重複數字 | |
# 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)): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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,) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import shutil | |
fileListingFile = "missclassified_file_path.txt" | |
outputDir = "./" | |
# sample path in txt: /root/notebooks/XXXXX/datasets/XXXXXXXX.jpg | |
with open(fileListingFile, "r") as file: | |
fileNames = [file.strip() for file in file.readlines()] |