Skip to content

Instantly share code, notes, and snippets.

Avatar

e96031413

View GitHub Profile
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
'''
@e96031413
e96031413 / pytorch_tensor_to_img.py
Created Dec 10, 2021
從PyTorch的Tensor資料中,將資料保存成實體圖片檔案
View pytorch_tensor_to_img.py
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'
View tsne-tensorboard.py
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)
View calculate_params_and_macs_pytorch.py
# 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)
View custom_pytorch_data_augmentation.py
# 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)