Kaggle competition: Prostate cANcer graDe Assessment (PANDA) Challenge
<SOS> metric: QuadraticWeightedKappa Kaggle data source: prostate-cancer-grade-assessment-(panda)-challenge<define_variables>
warnings.filterwarnings("ignore")
data_dir = '.. /input/vsb-power-line-fault-detection'<import_modules>
sz = 128
bs = 1
N = 128
nworkers = 2
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_128k_0_{i}.pth' for i in range(4)] + \
[f'.. /input/panda-init-class-model1/RNXT50_128k_3_{i}.pth' for i in range(4)] + \
[f'.. /input/panda-init-class-model1/RNXT50_128kr_1_{i}.pth' for i in range(4)] + \
[f'.. /input/panda-init-class-model1/RNXT50_128kr_2_{i}.pth' for i in range(4)] + \
[f'.. /input/panda-init-class-model1/RNXT50_128kr_2feature_{i}.pth' for i in range(4)] + \
[f'.. /input/panda-init-class-model1/RNXT50_128kr_9_{i}.pth' for i in range(4)] + \
[f'.. /input/panda-init-class-model1/RNXT50_128kr_7c_{i}.pth' for i in range(4)] + \
[f'.. /input/panda-init-class-model1/RNXT50_128kr_3_{i}.pth' for i in range(4)]
ws = [1,1,1,6,1,1,1,1]
ws = [w for w in ws for k in range(4)]
for p in [scipy, np, numba, sklearn, lgb, pyarrow, pd]:
print('{}=={}'.format(p.__name__, p.__version__))
print()
print(sys.version)
<load_from_csv>
class Model(nn.Module):
def __init__(self, arch='resnext50_32x4d', n=11, pre=True):
super().__init__()
m = ResNet(Bottleneck, [3, 4, 6, 3], 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]
meta_train_df = pd.read_csv(data_dir + '/metadata_train.csv')
 meta_test_df = pd.read_csv(data_dir + '/metadata_test.csv' )<feature_engineering>
models = []
for path in MODELS:
state_dict = torch.load(path,map_location=torch.device('cpu'))
model = Model()
model.load_state_dict(state_dict)
 model.float()
model.eval()
model.cuda()
models.append(model)
 del state_dict
@numba.jit(nopython=True)
 def flatiron(x, alpha=100., beta=1):

 new_x = np.zeros_like(x)
 zero = x[0]
for i in range(1, len(x)) :
zero = zero*(alpha-beta)/alpha + beta*x[i]/alpha
new_x[i] = x[i] - zero
return new_x<categorify>
def tile(img):
shape = img.shape
pad0,pad1 =(sz - shape[0]%sz)%sz,(sz - shape[1]%sz)%sz
img = np.pad(img,[[pad0//2,pad0-pad0//2],[pad1//2,pad1-pad1//2],[0,0]],constant_values=255)
 img = img.reshape(img.shape[0]//sz,sz,img.shape[1]//sz,sz,3)
 img = img.transpose(0,2,1,3,4 ).reshape(-1,sz,sz,3)
 if len(img)< N:
img = np.pad(img,[[0,N-len(img)],[0,0],[0,0],[0,0]],constant_values=255)
 idxs = np.argsort(img.reshape(img.shape[0],-1 ).sum(-1)) [:N]
img = img[idxs]
return img
mean = torch.Tensor([1.0-0.85506157, 1.0-0.7035249, 1.0-0.80203127])
 std = torch.Tensor([0.40011922, 0.52504386, 0.42675745])
 class PandaDataset(Dataset):
def __init__(self, path, test):
self.path = path
self.names = list(pd.read_csv(test ).image_id)
 def __len__(self):
return len(self.names)
 def __getitem__(self, idx):
name = self.names[idx]
img = skimage.io.MultiImage(os.path.join(DATA,name+'.tiff')) [1]
tiles = torch.Tensor(( 255 - tile(img)) /255.0)
 tiles =(tiles - mean)/std
return tiles.permute(0,3,1,2), name
<define_variables><EOS> 
sub_df.to_csv("submission.csv", index=False)
 sub_df.head()