Skip to content

Instantly share code, notes, and snippets.

Avatar

e96031413

View GitHub Profile
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():
@e96031413
e96031413 / CopyFileFromPathInTXT.py
Created Dec 27, 2021
Copy file to a specific folder with paths in a txt file
View CopyFileFromPathInTXT.py
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()]
@e96031413
e96031413 / PyTorchDataloader_wo_StopIteration.py
Last active Dec 20, 2021
Iterating over PyTorch dataloader without StopIteration using try/except
View PyTorchDataloader_wo_StopIteration.py
batch_iterator = iter(dataloader)
images, targets = next(batch_iterator)
try:
images, targets = next(batch_iterator)
except StopIteration:
batch_iterator = iter(data_loader)
images, targets = next(batch_iterator)
View resnet.py
# https://github.com/rajatkoner08/oodformer/blob/master/networks/resnet.py
'''ResNet in PyTorch.
BasicBlock and Bottleneck module is from the original ResNet paper:
[1] Kaiming He, Xiangyu Zhang, Shaoqing Ren, Jian Sun
Deep Residual Learning for Image Recognition. arXiv:1512.03385
PreActBlock and PreActBottleneck module is from the later paper:
[2] Kaiming He, Xiangyu Zhang, Shaoqing Ren, Jian Sun
Identity Mappings in Deep Residual Networks. arXiv:1603.05027
Original code is from https://github.com/kuangliu/pytorch-cifar/blob/master/models/resnet.py
'''