
2022年2月24日,俄羅斯宣戰併入侵了和平的烏克蘭。克里米亞吞併和唐巴斯地區的佔領之後,普京政權決定摧毀烏克蘭國籍。烏克蘭人表現出激烈的抵抗,並向全世界展示了為國家獨立而戰的感覺。
烏克蘭政府啟動了一個網站,以幫助俄羅斯母親,妻子和姐妹發現他們心愛的人在烏克蘭被殺害或俘虜-https://200rf.com&https://t.me/rf200_now(Telegram Channel)。我們的目標是告知仍在俄羅斯和白俄羅斯的人們,因此他們拒絕襲擊烏克蘭。
幫助我們最大程度地接觸到烏克蘭,暴力和不人道的恐怖行為,“俄羅斯世界”帶給烏克蘭。這是關於如何幫助結束這場戰爭的全面Wiki:https://how-to-to-help-ukraine-now.super.site/
官方頻道
榮耀烏克蘭!
pytorch-toolbelt是一個Python圖書館,有一套鈴鐺和哨子,用於快速的研發原型和Kaggle養殖:
展示櫃:催化劑,陳詞濫調,pytorch工具帶示例:語義分割 @ camvid
誠實的答案是“我需要一種方便的方法來重新利用我的Kaggle職業的代碼”。在2018年,我獲得了Kaggle Master徽章,這是一條漫長的道路。我經常發現自己一遍又一遍地重複使用大多數舊管道。在某個時候,它結晶到了此存儲庫中。
該液體並不是要取代催化劑 / IGNITE / fast.ai高級框架。相反,它旨在補充它們。
pip install pytorch_toolbelt
在代碼片段下方創建用於二進制分割的Vanilla U-NET模型。根據設計,編碼器和解碼器都產生張量列表,從細(高分辨率,索引0 )到粗(低分辨率)特徵圖。如果您想對其進行深層監督損失或對象檢測任務的編碼器,則訪問所有中間特徵地圖將是有益的,在這些損失中,需要訪問中間特徵地圖。
from torch import nn
from pytorch_toolbelt . modules import encoders as E
from pytorch_toolbelt . modules import decoders as D
class UNet ( nn . Module ):
def __init__ ( self , input_channels , num_classes ):
super (). __init__ ()
self . encoder = E . UnetEncoder ( in_channels = input_channels , out_channels = 32 , growth_factor = 2 )
self . decoder = D . UNetDecoder ( self . encoder . channels , decoder_features = 32 )
self . logits = nn . Conv2d ( self . decoder . channels [ 0 ], num_classes , kernel_size = 1 )
def forward ( self , x ):
x = self . encoder ( x )
x = self . decoder ( x )
return self . logits ( x [ 0 ])與以前的示例類似,您可以使用contatenation將解碼器更改為FPN。
from torch import nn
from pytorch_toolbelt . modules import encoders as E
from pytorch_toolbelt . modules import decoders as D
class SEResNeXt50FPN ( nn . Module ):
def __init__ ( self , num_classes , fpn_channels ):
super (). __init__ ()
self . encoder = E . SEResNeXt50Encoder ()
self . decoder = D . FPNCatDecoder ( self . encoder . channels , fpn_channels )
self . logits = nn . Conv2d ( self . decoder . channels [ 0 ], num_classes , kernel_size = 1 )
def forward ( self , x ):
x = self . encoder ( x )
x = self . decoder ( x )
return self . logits ( x [ 0 ])來自pytorch_toolbelt的所有編碼器都支持更改輸入通道的數量。只需調用encoder.change_input_channels(num_channels)和第一卷積層即可更改。只要有可能,現有的捲積層的權重將被重新使用(如果新數量的通道數大於默認數量,則將使用隨機定位的Weigth填充新的重量張量)。類方法返回self ,因此可以鏈接此呼叫。
from pytorch_toolbelt . modules import encoders as E
encoder = E . SEResnet101Encoder ()
encoder = encoder . change_input_channels ( 6 )在設計模型並優化神經網絡中的功能數量時,我發現在高級塊中打印數量的參數(例如encoder和decoder )非常有用。這是使用pytorch_toolbelt進行操作的方法:
from torch import nn
from pytorch_toolbelt . modules import encoders as E
from pytorch_toolbelt . modules import decoders as D
from pytorch_toolbelt . utils import count_parameters
class SEResNeXt50FPN ( nn . Module ):
def __init__ ( self , num_classes , fpn_channels ):
super (). __init__ ()
self . encoder = E . SEResNeXt50Encoder ()
self . decoder = D . FPNCatDecoder ( self . encoder . channels , fpn_channels )
self . logits = nn . Conv2d ( self . decoder . channels [ 0 ], num_classes , kernel_size = 1 )
def forward ( self , x ):
x = self . encoder ( x )
x = self . decoder ( x )
return self . logits ( x [ 0 ])
net = SEResNeXt50FPN ( 1 , 128 )
print ( count_parameters ( net ))
# Prints {'total': 34232561, 'trainable': 34232561, 'encoder': 25510896, 'decoder': 8721536, 'logits': 129}有多種方法可以結合多種損失,而高級DL框架(例如Catalyst)為實現這一目標提供了更靈活的方法,但是這裡有100%的Pure Pytorch實施:
from pytorch_toolbelt import losses as L
# Creates a loss function that is a weighted sum of focal loss
# and lovasz loss with weigths 1.0 and 0.5 accordingly.
loss = L . JointLoss ( L . FocalLoss (), L . LovaszLoss (), 1.0 , 0.5 )測試時間Augmetnation(TTA)可用於訓練和測試階段。
from pytorch_toolbelt . inference import tta
model = UNet ()
# Truly functional TTA for image classification using horizontal flips:
logits = tta . fliplr_image2label ( model , input )
# Truly functional TTA for image segmentation using D4 augmentation:
logits = tta . d4_image2mask ( model , input )通常,需要對圖像進行分割,以實現極大的圖像(5000px等)。如此大的像素陣列有一些問題:
解決方案之一是將輸入圖像切成圖塊(可選地重疊),並通過模型將每個圖像饋入並將結果串回。通過這種方式,您可以保證GPU RAM使用的上限,同時保持在GPU上處理任意尺寸的圖像的能力。
import numpy as np
from torch . utils . data import DataLoader
import cv2
from pytorch_toolbelt . inference . tiles import ImageSlicer , CudaTileMerger
from pytorch_toolbelt . utils . torch_utils import tensor_from_rgb_image , to_numpy
image = cv2 . imread ( 'really_huge_image.jpg' )
model = get_model (...)
# Cut large image into overlapping tiles
tiler = ImageSlicer ( image . shape , tile_size = ( 512 , 512 ), tile_step = ( 256 , 256 ))
# HCW -> CHW. Optionally, do normalization here
tiles = [ tensor_from_rgb_image ( tile ) for tile in tiler . split ( image )]
# Allocate a CUDA buffer for holding entire mask
merger = CudaTileMerger ( tiler . target_shape , 1 , tiler . weight )
# Run predictions for tiles and accumulate them
for tiles_batch , coords_batch in DataLoader ( list ( zip ( tiles , tiler . crops )), batch_size = 8 , pin_memory = True ):
tiles_batch = tiles_batch . float (). cuda ()
pred_batch = model ( tiles_batch )
merger . integrate_batch ( pred_batch , coords_batch )
# Normalize accumulated mask and convert back to numpy
merged_mask = np . moveaxis ( to_numpy ( merger . merge ()), 0 , - 1 ). astype ( np . uint8 )
merged_mask = tiler . crop_to_orignal_size ( merged_mask ) @misc{Khvedchenya_Eugene_2019_PyTorch_Toolbelt,
author = {Khvedchenya, Eugene},
title = {PyTorch Toolbelt},
year = {2019},
publisher = {GitHub},
journal = {GitHub repository},
howpublished = {url{https://github.com/BloodAxe/pytorch-toolbelt}},
commit = {cc5e9973cdb0dcbf1c6b6e1401bf44b9c69e13f3}
}