Upload 2 files
Browse files- NDM.pth +3 -0
- NoiceDetectionModel.py +107 -0
NDM.pth
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:a5590a6fea4929be15568007ab5515daba4f5883e794d3599c46fe6968ed2685
|
| 3 |
+
size 6855019
|
NoiceDetectionModel.py
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from torch import nn
|
| 2 |
+
import torch
|
| 3 |
+
from torch.nn import functional as F
|
| 4 |
+
from torchvision import transforms as T
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
class PatchEmbedding (nn.Module) :
|
| 8 |
+
def __init__ (self,image_size,patch_size,embedding_size) :
|
| 9 |
+
super().__init__()
|
| 10 |
+
self.projection_layers = nn.Conv2d(in_channels=3,out_channels=embedding_size,kernel_size=patch_size,stride=patch_size)
|
| 11 |
+
self.n_patch = (image_size // patch_size)**2
|
| 12 |
+
|
| 13 |
+
def forward(self,x) :
|
| 14 |
+
x = self.projection_layers(x)
|
| 15 |
+
x = x.flatten(2)
|
| 16 |
+
x = x.transpose(1,2)
|
| 17 |
+
return x
|
| 18 |
+
|
| 19 |
+
class PositionalEmbedding (nn.Module) :
|
| 20 |
+
def __init__ (self,n_patch,embedding_size) :
|
| 21 |
+
super().__init__()
|
| 22 |
+
self.n_patch = n_patch
|
| 23 |
+
self.position = nn.Parameter(torch.normal(0.0,0.02,size=(1,self.n_patch + 1,embedding_size)))
|
| 24 |
+
self.cls_token = nn.Parameter(torch.normal(0.0,0.02,size=(1,1,embedding_size)))
|
| 25 |
+
self.embedding_size = embedding_size
|
| 26 |
+
|
| 27 |
+
def forward(self,x) :
|
| 28 |
+
batch = x.shape[0]
|
| 29 |
+
cls_token = torch.broadcast_to(self.cls_token,(batch,1,self.embedding_size))
|
| 30 |
+
x = torch.cat((cls_token,x),dim=1)
|
| 31 |
+
x = x + self.position
|
| 32 |
+
|
| 33 |
+
return x
|
| 34 |
+
|
| 35 |
+
class BlockTransformers (nn.Module) :
|
| 36 |
+
def __init__ (self,d_model,num_head,ffn_dim,droprate= 0.1) :
|
| 37 |
+
super().__init__()
|
| 38 |
+
self.norm1 = nn.LayerNorm(d_model)
|
| 39 |
+
self.norm2 = nn.LayerNorm(d_model)
|
| 40 |
+
self.MHA = nn.MultiheadAttention(embed_dim=d_model,num_heads=num_head,dropout=droprate)
|
| 41 |
+
self.FeedFordward = nn.Sequential(
|
| 42 |
+
nn.Linear(d_model,ffn_dim),
|
| 43 |
+
nn.GELU(),
|
| 44 |
+
nn.Linear(ffn_dim,d_model)
|
| 45 |
+
)
|
| 46 |
+
self.drop_out = nn.Dropout(droprate)
|
| 47 |
+
|
| 48 |
+
def forward(self,x) :
|
| 49 |
+
attn = self.norm1(x)
|
| 50 |
+
attn,_ = self.MHA(attn,attn,attn)
|
| 51 |
+
x = x+attn
|
| 52 |
+
|
| 53 |
+
ffn = self.norm2(x)
|
| 54 |
+
ffn = self.FeedFordward(x)
|
| 55 |
+
ffn = self.drop_out(x)
|
| 56 |
+
x = x+ffn
|
| 57 |
+
return x
|
| 58 |
+
|
| 59 |
+
class NoiceDetectorModel (nn.Module) :
|
| 60 |
+
def __init__(self,image_size,d_model,num_head,ffn_dim,droprate= 0.1) :
|
| 61 |
+
super().__init__()
|
| 62 |
+
self.patch_embedding = PatchEmbedding(image_size=image_size,patch_size=16,embedding_size=d_model)
|
| 63 |
+
self.positional_embedding = PositionalEmbedding(self.patch_embedding.n_patch,d_model)
|
| 64 |
+
self.blocklayers = nn.Sequential(
|
| 65 |
+
BlockTransformers(d_model,num_head,ffn_dim,droprate),
|
| 66 |
+
BlockTransformers(d_model,num_head,ffn_dim,droprate))
|
| 67 |
+
self.linear1 = nn.Linear(d_model,128)
|
| 68 |
+
self.relu = nn.ReLU()
|
| 69 |
+
self.linear2 = nn.Linear(128,3)
|
| 70 |
+
def forward(self,x) :
|
| 71 |
+
x = self.patch_embedding(x)
|
| 72 |
+
x = self.positional_embedding(x)
|
| 73 |
+
x = self.blocklayers(x)
|
| 74 |
+
x = x[:,-1,:]
|
| 75 |
+
x = self.linear1(x)
|
| 76 |
+
x = self.relu(x)
|
| 77 |
+
x = self.linear2(x)
|
| 78 |
+
return x
|
| 79 |
+
|
| 80 |
+
class ModelRunners :
|
| 81 |
+
def __init__(self,path) :
|
| 82 |
+
self.Model = NoiceDetectorModel(image_size=384,d_model=256,num_head=4,ffn_dim=784)
|
| 83 |
+
self.__checkpoint = torch.load(path)
|
| 84 |
+
self.Model.load_state_dict(self.__checkpoint)
|
| 85 |
+
self.device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
| 86 |
+
self.Model.to(self.device)
|
| 87 |
+
self.Model.eval()
|
| 88 |
+
self.transform =T.Compose([
|
| 89 |
+
T.ToTensor(),
|
| 90 |
+
T.Normalize(mean=[0.485,0.456,0.406],std=[0.229,0.224,0.225])
|
| 91 |
+
])
|
| 92 |
+
|
| 93 |
+
def modelrun (self,x_target) :
|
| 94 |
+
if not isinstance(x_target,torch.Tensor) :
|
| 95 |
+
x_target = self.transform(x_target)
|
| 96 |
+
x_target = torch.unsqueeze(x_target,dim=0)
|
| 97 |
+
|
| 98 |
+
with torch.no_grad() :
|
| 99 |
+
pred = self.Model(x_target)
|
| 100 |
+
pred = F.softmax(pred,dim=-1)
|
| 101 |
+
|
| 102 |
+
if isinstance(pred,torch.Tensor) :
|
| 103 |
+
return pred.detach().numpy()
|
| 104 |
+
|
| 105 |
+
else :
|
| 106 |
+
return pred
|
| 107 |
+
|