View LabelSmoothing_public.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
# 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
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") |
View tsne_mobilenetv2.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
for i, (img, target,_) in enumerate(tqdm(dataloader)): | |
feat_list = [] | |
def hook(module, input, output): | |
# 由於MobileNetv2不像ResNet18有宣告self.avgpool(),因此我的作法是將模型卷積層的最後一層的輸出手動進行adaptive_avg_pool2d | |
# 接著將它加到feature_list中 | |
feat_list.append(nn.functional.adaptive_avg_pool2d(output.clone(), (1, 1)).reshape(output.clone().shape[0], -1).detach()) | |
View extractDatasetFeatureToPickle.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
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(): |
View CopyFileFromPathInTXT.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
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()] |
View PyTorchDataloader_wo_StopIteration.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
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
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 | |
''' |
View pytorch_tensor_to_img.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' |
View Heroku操作指令.sh
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
#初始化 | |
$ heroku login #登入heroku | |
$ git init #設定git | |
$ heroku git:remote -a your-app-name #填入在Heroku中設定好的名字 | |
$ heroku config:set DISABLE_COLLECTSTATIC=1 | |
#Push到Heroku | |
$ git add . | |
$ git commit -am "make it better" | |
$ git push heroku master |
View tsne-tensorboard.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
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) |