This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ChannelAttentionGate(nn.Module): | |
def __init__(self, channel, reduction=16): | |
super(ChannelAttentionGate, self).__init__() | |
self.avg_pool = nn.AdaptiveAvgPool2d(1) | |
self.fc = nn.Sequential( | |
nn.Linear(channel, channel // reduction), | |
nn.ReLU(inplace=True), | |
nn.Linear(channel // reduction, channel), | |
nn.Sigmoid() | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#https://www.kaggle.com/c/tgs-salt-identification-challenge/discussion/67693 | |
class ConvBn2d(nn.Module): | |
def __init__(self, in_channels, out_channels, kernel_size=(3,3), stride=(1,1), padding=(1,1)): | |
super(ConvBn2d, self).__init__() | |
self.conv = nn.Conv2d(in_channels, out_channels, kernel_size=kernel_size, stride=stride, padding=padding, bias=False) | |
self.bn = nn.BatchNorm2d(out_channels) | |
def forward(self, z): | |
x = self.conv(z) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#https://www.kaggle.com/c/tgs-salt-identification-challenge/discussion/67693 | |
def iou_metric(outputs, labels,logits=True): | |
outputs,labels= size_correct(outputs,labels) | |
outputs= (outputs>0).detach().cpu().numpy() if logits else (outputs>.5).detach().cpu().numpy() | |
labels= labels.detach().cpu().numpy() | |
batch_size = outputs.shape[0] | |
metric = 0.0 | |
for batch in range(batch_size): | |
t, p = labels[batch], outputs[batch] | |
true = np.sum(t) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
kf = StratifiedKFold(10,shuffle=True,random_state=seed) | |
for fold, (trn_idx,val_idx) in enumerate(kf.split(file_list,coverage)): | |
print('****************************************************') | |
print('******************* fold %d *******************' % fold) | |
print('****************************************************') | |
file_list_train= [x for i,x in enumerate(file_list) if i in trn_idx] | |
file_list_val= [x for i,x in enumerate(file_list) if i in val_idx] | |
train = TGSSaltDataset(train_path, file_list_train,augment=transform_train) | |
val = TGSSaltDataset(train_path, file_list_val,augment= transform_test) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ConvBn2d(nn.Module): | |
def __init__(self, in_channels, out_channels, kernel_size=(3,3), stride=(1,1), padding=(1,1)): | |
super(ConvBn2d, self).__init__() | |
self.conv = nn.Conv2d(in_channels, out_channels, kernel_size=kernel_size, stride=stride, padding=padding, bias=False) | |
self.bn = nn.BatchNorm2d(out_channels) | |
def forward(self, z): | |
x = self.conv(z) | |
x = self.bn(x) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def get_losslv_simple(model,inp): | |
no_mask,final=model(inp[0]) | |
final=final*inp[-1].view(-1,1,1,1) | |
mfl=nn.BCEWithLogitsLoss() | |
lv=LovaszLoss() | |
weights=[.1/2.1,2/2.1] | |
loss=[mfl(no_mask.view(-1,1,1,1),inp[-1].view(-1,1,1,1)),lv(final,inp[1])] | |
accuracy=acc(no_mask.reshape(-1),inp[-1],center=True)/len(inp[-1]) | |
loss=sum(x_i*y_i for x_i, y_i in zip(weights, loss)) | |
return loss,accuracy,final |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def test_loop_fn(loader): | |
loss_,iou_,acc_=0.,0.,0., | |
model.eval() | |
with torch.no_grad(): | |
for image, mask in loader: | |
image = image.to(device) | |
mask=mask.to(device) | |
y_pred= model(image) | |
loss = loss_fn(y_pred, mask) | |
loss_+= loss.item() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def train_loop_fn(loader): | |
model.train() | |
loss_,iou_,acc_=0.,0.,0. | |
for x ,(image, mask) in enumerate(loader): | |
mask=mask.to(device) | |
image= image.to(device) | |
y_pred = model(image) | |
loss = loss_fn(y_pred, mask) | |
loss_+= loss.item() | |
optimizer.zero_grad() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
test_transforms=PadIfNeeded(128,128,cv2.BORDER_REPLICATE), | |
Normalize(mean=(0,0,0),std=(1,1,1,))]) | |
transform_train = Compose([ | |
HorizontalFlip(p=.5), | |
Compose([RandomCrop(90,90), | |
Resize(101,101)],p=.3), | |
OneOf([RandomBrightness(.1), | |
RandomContrast(.1),RandomGamma()],p=.2), | |
PadIfNeeded(256//2,256//2,cv2.BORDER_REPLICATE), | |
Normalize(mean=(0,0,0),std=(1,1,1,))]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class TGSSaltDataset(data.Dataset): | |
def __init__(self, root_path, file_list, is_test = False,augment= def_transforms,dpsv=False): | |
self.is_test = is_test | |
self.root_path = root_path | |
self.file_list = file_list | |
self.augment= augment | |
self.dpsv= dpsv | |
def __len__(self): | |
return len(self.file_list) | |
NewerOlder