
2022 년 2 월 24 일, 러시아는 전쟁을 선포하고 평화로운 우크라이나를 침공했습니다. 크리미아의 합병과 Donbas 지역의 점령 후 푸틴 정권은 우크라이나 국적을 파괴하기로 결정했습니다. 우크라이나 인들은 치열한 저항을 보여주고 전 세계에 국가의 독립을 위해 싸우는 것이 어떤지 보여줍니다.
우크라이나 정부는 러시아 어머니, 아내와 자매들이 우크라이나에서 사랑하는 사람들이 살해되거나 체포 된 것을 발견 할 수 있도록 웹 사이트를 시작했습니다. 우리의 목표는 여전히 러시아와 벨로루시에있는 사람들에게 알리기 때문에 우크라이나 폭행을 거부하는 것입니다.
우크라이나에서 일어나고있는 일, 폭력 및 비인간적 인 공포 행위에 "러시아 세계"가 우크라이나로 가져온 것에 대한 최대한의 노출을 얻도록 도와줍니다. 이것은이 전쟁을 끝내는 방법에 대한 포괄적 인 위키입니다 : https://how-to-help-ukraine-now.super.site/
공식 채널
우크라이나에게 영광을!
pytorch-toolbelt 빠른 R & D 프로토 타이핑 및 Kaggle 농업을위한 Pytorch를위한 종과 휘파람이있는 Python 라이브러리입니다.
쇼케이스 : Catalyst, Albumentations, Pytorch Toolbelt 예제 : Semantic Segmentation @ Camvid
정직한 대답은 "Kaggle 경력을 위해 코드를 재사용하는 편리한 방법이 필요했습니다"입니다. 2018 년 동안 나는 Kaggle 마스터 배지를 달성했으며 이것은 긴 길이었습니다. 매우 종종 나는 오래된 파이프 라인의 대부분을 반복해서 재사용하는 것을 발견했습니다. 어느 시점에서 그것은이 저장소로 결정화되었습니다.
이 lib는 catalyst / Ignite / Fast.ai 고급 프레임 워크를 대체하기위한 것이 아닙니다. 대신 보완하도록 설계되었습니다.
pip install pytorch_toolbelt
이진 세분화를위한 바닐라 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 ])이전 예제와 마찬가지로 콘트라 텐화와 함께 디코더를 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}여러 손실을 결합하는 여러 가지 방법이 있으며 Catalyst와 같은 고급 DL 프레임 워크는이를 달성 할 수있는보다 유연한 방법을 제공하지만 여기에는 100%파이토치 구현이 있습니다.
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}
}