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()] |
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
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) |
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://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 | |
''' |
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
from torchvision.utils import save_image | |
features, imgs, labels, image_path = get_features_trained_weight(model, transform_dataset) | |
img1 = imgs[1] #第1張圖片 | |
img1 = torch.from_numpy(img1) #格式轉換 | |
save_image(img1, 'img1.png') #保存成'img1.png' |
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 get_features_trained_weight(model, transform_dataset): | |
if torch.cuda.is_available(): | |
device = 'cuda' | |
else: | |
device = 'cpu' | |
if isinstance(model,torch.nn.DataParallel): | |
model = model.module | |
model.eval() | |
model.to(device) |
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
# thop(simple) | |
from thop import profile | |
model = mobilenetv3_large(width_mult=args.width_mult, num_classes=2) | |
input = torch.randn(1, 3, 224, 224) | |
macs, params = profile(model, inputs=(input, ), verbose=False) | |
print("%.2f | %.2f" % (params / (1000 ** 2), macs / (1000 ** 3))) | |
# torch info(detailed) | |
from torchinfo import summary | |
model = mobilenetv3_large(width_mult=args.width_mult, num_classes=2) |
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://www.jianshu.com/p/9a9000d226b6 | |
# https://stackoverflow.com/questions/64420379/is-it-possible-to-add-own-function-in-transform-compose-in-pytorch | |
import cv2 | |
import torchvision.transforms as transforms | |
class hisEqulColor(object): | |
def __call__(self, img): | |
img = cv2.cvtColor(np.asarray(img), cv2.COLOR_RGB2BGR) # PyTorch的Transform要求PIL格式的圖片,若要使用OpenCV則需先進行轉換 | |
ycrcb = cv2.cvtColor(img, cv2.COLOR_BGR2YCR_CB) |
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
# 針對每個元件計算其Overkill rate和Leakage rate | |
# 目前只適用於batch size = 1的情形 | |
# 用來產生overkill和leakage數值的dataframe | |
import pandas as pd | |
import torch | |
test_df_mapping2_label = test_df.copy() #複製一份要mapping到2個label的testing資料 | |
test_df_mapping2_label.loc[test_df_mapping2_label['class'] == 0, 'class'] = 0 #將大於1的label轉成1 | |
test_df_mapping2_label.loc[test_df_mapping2_label['class'] == 1, 'class'] = 1 | |
test_df_mapping2_label.loc[test_df_mapping2_label['class'] == 2, 'class'] = 1 |