TTXian/RemoteSensingChangeDetection-RSCD.HA2F
0
1# Copyright (c) Meta Platforms, Inc. and affiliates.2#3# This source code is licensed under the Apache License, Version 2.04# found in the LICENSE file in the root directory of this source tree.5 6# References:7# https://github.com/facebookresearch/dino/blob/master/vision_transformer.py8# https://github.com/rwightman/pytorch-image-models/tree/master/timm/layers/drop.py9 10 11from torch import nn12 13 14def drop_path(x, drop_prob: float = 0.0, training: bool = False):15 if drop_prob == 0.0 or not training:16 return x17 keep_prob = 1 - drop_prob18 shape = (x.shape[0],) + (1,) * (x.ndim - 1) # work with diff dim tensors, not just 2D ConvNets19 random_tensor = x.new_empty(shape).bernoulli_(keep_prob)20 if keep_prob > 0.0:21 random_tensor.div_(keep_prob)22 output = x * random_tensor23 return output24 25 26class DropPath(nn.Module):27 """Drop paths (Stochastic Depth) per sample (when applied in main path of residual blocks)."""28 29 def __init__(self, drop_prob=None):30 super(DropPath, self).__init__()31 self.drop_prob = drop_prob32 33 def forward(self, x):34 return drop_path(x, self.drop_prob, self.training)35 