R1chrdson / vesuvius_challenge

The Vesuvius Challenge - Ink Detection kaggle competition from the "UCU dropouts" team
0 stars 0 forks source link

Time Test Augmentations #24

Open R1chrdson opened 1 year ago

R1chrdson commented 1 year ago

Try to use TTA in final submissions to improve score:

Image

Simple example and pseudocode behind tta prediction:

def TTA(x:tc.Tensor,model:nn.Module):
    #x.shape=(batch,c,h,w)
    if CFG.TTA:
        shape=x.shape
        # Perform rotations
        x=[x,*[tc.rot90(x,k=i,dims=(-2,-1)) for i in range(1,4)]]
        x=tc.cat(x,dim=0)
        x=model(x)
        x=torch.sigmoid(x)
        x=x.reshape(4,shape[0],*shape[2:])
        # Undo rotations on predicted masks
        x=[tc.rot90(x[i],k=-i,dims=(-2,-1)) for i in range(4)]
        x=tc.stack(x,dim=0)
        # Average results
        return x.mean(0)
    else :
        x=model(x)
        x=torch.sigmoid(x)
        return x

model = load_model()
for batch in inference_set:
     prediction = TTA(batch, model)

Also, it's already implemented in https://www.kaggle.com/code/r1chardson/vesuvius-challenge-inference-smp:

Image

JUST A REMINDER TO NOT TO FORGOT IT! ALSO TRY HORIZONTAL/VERTICAL FLIPS and both of them