import tensorboardX as tbx     # 本範例會使用到tensorboardX套件
writer = tbx.SummaryWriter()
import tensorboardX as tbx
writer = tbx.SummaryWriter()
writer.add_embedding(mat=features, metadata=labels, label_img=label_imgs)
writer.close()
tensorboard --logdir=runs

# 若要使用plot(affine_images),請先定義以下functiondef plot(imgs, with_orig=False, row_title=None, **imshow_kwargs):
if not isinstance(imgs[0], list):
# Make a 2d grid even if there's just 1 row
imgs = [imgs]
num_rows = len(imgs)
num_cols = len(imgs[0]) + with_orig
fig, axs = plt.subplots(nrows=num_rows, ncols=num_cols, squeeze=False)
for row_idx, row in enumerate(imgs):
row = row
for col_idx, img in enumerate(row):
ax = axs[row_idx, col_idx]
ax.imshow(np.asarray(img), **imshow_kwargs)
ax.set(xticklabels=[], yticklabels=[], xticks=[], yticks=[])
plt.tight_layout()

class WhiteBalance(object):
def __call__(self, img):
wbModel = wb_srgb.WBsRGB(gamut_mapping=2, upgraded=0)
img = cv2.cvtColor(np.asarray(img), cv2.COLOR_RGB2BGR)
img = wbModel.correctImage(img)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img = (img * 255).round().astype(np.uint8)
return Image.fromarray(img)
def __repr__(self):
return self.__class__.__name__+'()'
transform_test = transforms.Compose([WhiteBalance(),
transforms.Resize([224, 224]),
transforms.ToTensor(),
normalize
])

# OpenCV和Pillow格式互轉:# opencv -> pil
img_pil = Image.fromarray(cv2.cvtColor(img_cv,cv2.COLOR_BGR2RGB))
# pil -> opencv
img_cv = cv2.cvtColor(np.asarray(img_pil),cv2.COLOR_RGB2BGR)

mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]
這篇討論串的留言提到
Just note that you need to use your own mean and std if your dataset is not similar to ImageNet. In the 3-channel case you have mentioned, you are using mean and std from ImageNet which works for most of the datasets that are similar but if you are using datasets such as medical image processing, then you need to obtain proper mean and std regarding your own dataset.
如果我們自己的資料集與ImageNet差異很大,或許就可以考慮針對自己的Dataset來計算mean和std,作為normalize的參數。
(a)ImageNet的mean和std
(b)自己資料集中的mean和std
mean: [0.385, 0.406, 0.410]
std: [0.222, 0.224, 0.220]

pip install pudb
import pudb; pu.db
# 1.將以下程式碼保存成test.pyimport pudb; pu.dbdef sum(n):
result = 0
for i in range(n):
result = result + i
return result
if __name__ == "__main__":
y1 = sum(10)
y2 = sum(100)
y3 = sum(100)
# 2.執行python test.py指令# 3.此時會進入到GUI的Debug介面# 4.使用n進入程式碼的下一行、當進入到y1 = sum(10)這一行時,可使用s進入函數當中。使用n可在函數當中進入下一行,此時右側Variables的框框中會出現result, i, n的數值。
Ctrl+p: 打開屬性設置
n: next,也就是執行一步 (一定要用)
s: step into,進入函數內部 (常用,可直接進到function當中,搭配n觀看變數變化)
f: finsh current function,結束當前函數?
c: continue,繼續執行
b: break point,在游標所在行添加或消除斷點 (常用)
t: to cursor,運行到游標位置
: 開啟python命令行 (在當下行輸入python程式)
: 開啟幫助視窗 (常用,查詢快捷鍵)
o: output screen,打開輸出視窗/控制台 (觀看程式的output)
m: module,打開模組
q: quit,退出PUDB (離開pudb)
/: 搜尋
,/. 搜索下一個/上一個

Yanwei Liu

Machine Learning | Deep Learning | https://linktr.ee/yanwei

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store