Kaggle competition: Prostate cANcer graDe Assessment (PANDA) Challenge
<SOS> metric: QuadraticWeightedKappa Kaggle data source: prostate-cancer-grade-assessment-(panda)-challenge<split>
warnings.filterwarnings("ignore")
 sys.path.insert(0, '.. /input/semisupervised-imagenet-models/semi-supervised-ImageNet1K-models-master/')
DATA_SPLIT_SEED = 2018
clr = CyclicLR(base_lr=0.001, max_lr=0.002,
step_size=300., mode='exp_range',
gamma=0.99994)
 train_meta = np.zeros(train_y.shape)
 test_meta = np.zeros(test_X.shape[0])
 splits = list(StratifiedKFold(n_splits=4, shuffle=True, random_state=DATA_SPLIT_SEED ).split(train_X, train_y))
for idx,(train_idx, valid_idx)in enumerate(splits):
X_train = train_X[train_idx]
y_train = train_y[train_idx]
X_val = train_X[valid_idx]
y_val = train_y[valid_idx]
model = model_lstm_atten(embedding_matrix)
 pred_val_y, pred_test_y, best_score = train_pred(model, X_train, y_train, X_val, y_val, epochs = 8, callback = [clr,])
 train_meta[valid_idx] = pred_val_y.reshape(-1)
 test_meta += pred_test_y.reshape(-1)/ len(splits )<save_to_csv>
DATA = '.. /input/prostate-cancer-grade-assessment/test_images'
TEST = '.. /input/prostate-cancer-grade-assessment/test.csv'
SAMPLE = '.. /input/prostate-cancer-grade-assessment/sample_submission.csv'
MODELS = [f'.. /input/panda-init-class-model1/RNXT50_128krnew1_3featureB_{i}.pth' for i in range(4)] + \
[f'.. /input/panda-init-class-model1/RNXT50_128krnew1_2featureB_{i}.pth' for i in range(4)] + \
[f'.. /input/panda-init-class-model1/RNXT50_128krnew1_1featureB_{i}.pth' for i in range(4)]
MODELS1 = [f'.. /input/panda-init-class-model1/RNXT50_128krnew_2_{i}.pth' for i in range(4)]
sz = 128
bs = 1
N_max = 3*128
N_max1 = 256
nworkers = 2
sub = pd.read_csv('.. /input/sample_submission.csv')
 sub.prediction = test_meta > 0.33
sub.to_csv("submission.csv", index=False )<import_modules>
class Modelm1(nn.Module):
def __init__(self, arch='resnext50_32x4d', n=6, pre=True):
super().__init__()
m = _resnext(semi_supervised_model_urls[arch], Bottleneck, [3, 4, 6, 3], False, progress=False,\
groups=32,width_per_group=4)
 self.enc = nn.Sequential(*list(m.children())[:-2])
 nc = list(m.children())[-1].in_features
self.head = nn.Sequential(AdaptiveConcatPool2d() ,Flatten() ,
nn.Linear(2*nc,512),Mish() ,nn.GroupNorm(32,512),
nn.Dropout(0.5),nn.Linear(512,n))
def forward(self, x):
shape = x.shape
n = shape[1]
x = x.view(-1,shape[2],shape[3],shape[4])
 x = self.enc(x)
 shape = x.shape
x = x.view(-1,n,shape[1],shape[2],shape[3] ).permute(0,2,1,3,4 ).contiguous() \ 
.view(-1,shape[1],shape[2]*n,shape[3])
 x = self.head(x)
 return x[:,:1]
import os
import time
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn import metrics
from keras.preprocessing.text import Tokenizer
from keras.preprocessing.sequence import pad_sequences
from keras.layers import Dense, Input, LSTM, Embedding, Dropout, Activation, CuDNNLSTM, Conv1D,Dropout
from keras.layers import Bidirectional, GlobalMaxPool1D,MaxPooling1D,BatchNormalization,Flatten,RepeatVector,Permute,concatenate
from keras.models import Model<load_from_csv>
class AdaptiveConcatPool2dm1(Module):
"Layer that concats `AdaptiveAvgPool2d` and `AdaptiveMaxPool2d`."
def __init__(self, sz:Optional[int]=None):
"Output will be 2*sz or 2 if sz is None"
self.output_size = sz or 1
self.ap = nn.AdaptiveAvgPool2d(self.output_size)
 self.mp = nn.AdaptiveMaxPool2d(self.output_size)
 def forward(self, x): return torch.cat([self.mp(x), self.ap(x)], 1)
 def _resnext(url, block, layers, pretrained, progress, **kwargs):
model = ResNet(block, layers, **kwargs)
 return model
class Model(nn.Module):
def __init__(self, arch='resnext50_32x4d', n=6, pre=True):
super().__init__()
m = _resnext(semi_supervised_model_urls[arch], Bottleneck, [3, 4, 6, 3], False, progress=False,\
groups=32,width_per_group=4)
 self.enc = nn.Sequential(*list(m.children())[:-2])
 nc = list(m.children())[-1].in_features
self.head = nn.Sequential(AdaptiveConcatPool2d() ,Flatten() ,
nn.Linear(2*nc,512),Mish() ,nn.GroupNorm(32,512),
nn.Dropout(0.5),nn.Linear(512,n))
def forward(self, x):
shape = x.shape
n = shape[1]
x = x.view(-1,shape[2],shape[3],shape[4])
 x = self.enc(x)
 shape = x.shape
x = x.view(-1,n,shape[1],shape[2],shape[3] ).permute(0,2,1,3,4 ).contiguous() \ 
.view(-1,shape[1],shape[2]*n,shape[3])
 x = self.head(x)
 return x[:,:1]
train = pd.read_csv(".. /input/train.csv")
 test = pd.read_csv(".. /input/test.csv" )<load_from_csv>
class AdaptiveConcatPool2dm(Module):
"Layer that concats `AdaptiveAvgPool2d` and `AdaptiveMaxPool2d`."
def __init__(self, sz:Optional[int]=None):
"Output will be 2*sz or 2 if sz is None"
self.output_size = sz or 1
self.ap = nn.AdaptiveAvgPool2d(self.output_size)
 self.mp = nn.AdaptiveMaxPool2d(self.output_size)
 def forward(self, x): return torch.cat([self.ap(x),self.mp(x)], 1)
 def _resnext(url, block, layers, pretrained, progress, **kwargs):
model = ResNet(block, layers, **kwargs)
 return model
class Modelm(nn.Module):
def __init__(self, arch='resnext50_32x4d', n=6, pre=True):
super().__init__()
m = _resnext(semi_supervised_model_urls[arch], Bottleneck, [3, 4, 6, 3], False, progress=False,\
groups=32,width_per_group=4)
 self.enc = nn.Sequential(*list(m.children())[:-2])
 nc = list(m.children())[-1].in_features
self.head = nn.Sequential(AdaptiveConcatPool2dm() ,Flatten() ,
nn.Linear(2*nc,512),Mish() ,nn.GroupNorm(32,512),
nn.Dropout(0.5),nn.Linear(512,n))
def forward(self, x):
shape = x.shape
n = shape[1]
x = x.view(-1,shape[2],shape[3],shape[4])
 x = self.enc(x)
 shape = x.shape
x = x.view(-1,n,shape[1],shape[2],shape[3] ).permute(0,2,1,3,4 ).contiguous() \ 
.view(-1,shape[1],shape[2]*n,shape[3])
 x = self.head(x)
 return x[:,:1]
train = pd.read_csv(".. /input/train.csv")
 test = pd.read_csv(".. /input/test.csv" )<string_transform>
models = []
for path in MODELS[:-4]:
state_dict = torch.load(path,map_location=torch.device('cpu'))
model = Model(n=1+10)
 model.load_state_dict(state_dict)
 model.float()
model.eval()
model.cuda()
models.append(model)
 for path in MODELS[-4:]:
state_dict = torch.load(path,map_location=torch.device('cpu'))
model = Modelm(n=1+10)
 model.load_state_dict(state_dict)
 model.float()
model.eval()
model.cuda()
models.append(model)
 models2 = []
for path in MODELS1:
state_dict = torch.load(path,map_location=torch.device('cpu'))
model = Modelm1(n=1+10)
 model.load_state_dict(state_dict)
 model.float()
model.eval()
model.cuda()
models2.append(model)
 del state_dict
train, dev = train_test_split(train, test_size=0.1, random_state=2018)
 embed_size = 300
max_features = 50000
maxlen = 40
train_X = train["question_text"].fillna("_na_" ).values
dev_X = dev["question_text"].fillna("_na_" ).values
test_X = test["question_text"].fillna("_na_" ).values
tokenizer = Tokenizer(num_words=max_features)
 tokenizer.fit_on_texts(list(train_X))
train_X = tokenizer.texts_to_sequences(train_X)
 dev_X = tokenizer.texts_to_sequences(dev_X)
 test_X = tokenizer.texts_to_sequences(test_X)
 train_X = pad_sequences(train_X, maxlen=maxlen)
 dev_X = pad_sequences(dev_X, maxlen=maxlen)
 test_X = pad_sequences(test_X, maxlen=maxlen)
 train_y = train['target'].values
dev_y = dev['target'].values<choose_model_class>
sub_df = pd.read_csv(SAMPLE)
 if os.path.exists(DATA):
ds = PandaDataset(DATA,TEST)
 dl = DataLoader(ds, batch_size=bs, num_workers=nworkers, shuffle=False)
 names,preds = [],[]
with torch.no_grad() :
for x,x2,y in tqdm(dl):
x = x.cuda()
x = torch.stack([x,x.flip(-1),x.flip(-2),x.flip(-1,-2),x.transpose(-1,-2),\
x.transpose(-1,-2 ).flip(-1), x.transpose(-1,-2 ).flip(-2),\
x.transpose(-1,-2 ).flip(-1,-2)],1)
 x = x.view(8,-1,3,sz,sz)
 p = [model(x[a].unsqueeze(0)) for model in models for a in range(x.shape[0])]
x2 = x2.cuda()
x2 = torch.stack([x2,x2.flip(-1),x2.flip(-2),x2.flip(-1,-2),x2.transpose(-1,-2),\
x2.transpose(-1,-2 ).flip(-1), x2.transpose(-1,-2 ).flip(-2),\
x2.transpose(-1,-2 ).flip(-1,-2)],1)
 x2 = x2.view(8,-1,3,sz,sz)
 p += [model(x2[a].unsqueeze(0)) for model in models2 for a in range(x2.shape[0])]
p = torch.stack(p,1)
 p = p.view(bs,8*(len(models)+len(models2)) ).mean(1 ).cpu()
p = torch.clamp(( 6.0*torch.sigmoid(p)).long().view(-1),0,5)
 names.append(y)
 preds.append(p)
 names = np.concatenate(names)
 preds = torch.cat(preds ).numpy()
sub_df = pd.DataFrame({'image_id': names, 'isup_grade': preds})
 sub_df.to_csv('submission.csv', index=False)
 sub_df.head()
<statistical_test>
sub_df.to_csv("submission.csv", index=False)
 sub_df.head()