Kaggle competition: Prostate cANcer graDe Assessment (PANDA) Challenge
EMBEDDING_FILE = '.. /input/embeddings/wiki-news-300d-1M/wiki-news-300d-1M.vec'
def get_coefs(word,*arr): return word, np.asarray(arr, dtype='float32')
 embeddings_index = dict(get_coefs(*o.split(" ")) for o in open(EMBEDDING_FILE)if len(o)>100)
 all_embs = np.stack(embeddings_index.values())
 emb_mean,emb_std = all_embs.mean() , all_embs.std()
embed_size = all_embs.shape[1]
word_index = tokenizer.word_index
nb_words = min(max_features, len(word_index))
embedding_matrix = np.random.normal(emb_mean, emb_std,(nb_words, embed_size))
for word, i in word_index.items() :
if i >= max_features: continue
embedding_vector = embeddings_index.get(word)
 if embedding_vector is not None: embedding_matrix[i] = embedding_vector<compute_test_metric>
package_path = '/kaggle/input/efficientnet-pytorch/EfficientNet-PyTorch/EfficientNet-PyTorch-master'
sys.path.append(package_path)
def f1(y_true, y_pred):
def recall(y_true, y_pred):

 true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
 possible_positives = K.sum(K.round(K.clip(y_true, 0, 1)))
 recall = true_positives /(possible_positives + K.epsilon())
 return recall
def precision(y_true, y_pred):

 true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
 predicted_positives = K.sum(K.round(K.clip(y_pred, 0, 1)))
 precision = true_positives /(predicted_positives + K.epsilon())
 return precision
precision = precision(y_true, y_pred)
 recall = recall(y_true, y_pred)
 return 2*(( precision*recall)/(precision+recall+K.epsilon()))<choose_model_class>
def net(input_shape):
sentence_indices = Input(input_shape, dtype='int32')
 X = Embedding(max_features, embed_size,weights=[embedding_matrix] )(sentence_indices)
 activations = Bidirectional(CuDNNLSTM(128, return_sequences=True))(X)
 activations = Dropout(0.1 )(activations)
 attention = Dense(1, activation='tanh' )(activations)
 attention = Flatten()(attention)
 attention = Activation('softmax' )(attention)
 attention = RepeatVector(128 )(attention)
 attention = Permute([2, 1] )(attention)
 X = concatenate([activations, attention])
 activations = Bidirectional(CuDNNLSTM(128, return_sequences=True))(X)
 activations = Dropout(0.1 )(activations)
 attention = Dense(1, activation='tanh' )(activations)
 attention = Flatten()(attention)
 attention = Activation('softmax' )(attention)
 attention = RepeatVector(128 )(attention)
 attention = Permute([2, 1] )(attention)
 X = concatenate([activations, attention])
 X = Bidirectional(CuDNNLSTM(64, return_sequences=True))(X)
 X = Dropout(0.1 )(X)
 X = GlobalMaxPool1D()(X)
 X = Dense(16, activation="relu" )(X)
 X = Dropout(0.1 )(X)
 X = Dense(1, activation="sigmoid" )(X)
 model = Model(inputs=sentence_indices, outputs=X)
 return model
model=net(( maxlen,))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy',f1])
 print(model.summary() )<define_search_space>
mean_224 = torch.tensor([1.0-0.82097102, 1.0-0.63302738, 1.0-0.75392824])
 std_224 = torch.tensor([0.37723779, 0.49839178, 0.4015415])
 first_resnext_pth_path_224 = ".. /input/lb91-224tile/LB91_224tile_best_resnext50_X20_30e_0.pth"
ensemble_1 ={"mean":mean_224,"std":std_224,"tileSize":224,"isExpandTile":False,"arch":"resnext50_32x4d","path":first_resnext_pth_path_224,"NN":64}
mean_288 = torch.tensor([1.0-0.82809473, 1.0-0.65345352, 1.0-0.76934528])
 std_288 = torch.tensor([0.40728608, 0.52557509, 0.42674383])
 second_resnext_pth_path_288 = ".. /input/cv8899-expand-288/cv8899_expand_288_tiles_resnext50_X20_30e_0.pth"
expand_sz = 288
ensemble_2 ={"mean":mean_288,"std":std_288,"tileSize":288,"isExpandTile":True,"arch":"resnext50_32x4d","path":second_resnext_pth_path_288,"NN":64}
ensemble_list = []
ensemble_list.append(ensemble_1)
 ensemble_list.append(ensemble_2)
 MODELS = []
for model_path_index in range(len(ensemble_list)) :
MODELS.append(ensemble_list[model_path_index]['path'])
 DATA = '.. /input/prostate-cancer-grade-assessment/test_images'
TEST = '.. /input/prostate-cancer-grade-assessment/test.csv'
DATA2 = '.. /input/prostate-cancer-grade-assessment/train_images'
TEST2 = '.. /input/prostate-cancer-grade-assessment/train.csv'
SAMPLE = '.. /input/prostate-cancer-grade-assessment/sample_submission.csv'
mixnet_pth=".. /input/segmmixnetv2/checkpoint_segm_mixnet_hardaug_14elr0005_acc88.59.pth"
bs = 2
sz2=224
NUM=20
NN=64
nworkers = 2
debug = False
weight={0:0.4,1:1}<train_model>
if os.path.exists(DATA):
print('test set exist')
 debug = False
chk=False
else:
print('test set not exist')
 chk=True
TEST=TEST2
DATA=DATA2
nchk=10
if debug:
nchk = 10
model.fit(train_X, train_y, batch_size=512, epochs=3 , validation_data=(dev_X, dev_y),class_weight=weight )<train_model>
def _resnext(url, block, layers, pretrained, progress, **kwargs):
model = ResNet(block, layers, **kwargs)
 return model
class Model(nn.Module):
def __init__(self, arch, n=6, pre=True):
super().__init__()
self.arch = arch
if arch == 'resnet18':
m = models.resnet18(pretrained=False)
 elif arch == 'resnet34':
m = models.resnet34(pretrained=False)
 elif arch == 'resnet50':
m = models.resnet50(pretrained=False)
 elif arch.startswith('efficient'):
m = EfficientNet.from_name(arch)
 self.m = m
else:
m = _resnext(semi_supervised_model_urls[arch], Bottleneck, [3, 4, 6, 3], False,
progress=False,groups=32,width_per_group=4)
 print("arch",arch)
 if arch.startswith('efficient'):
nc = self.m._fc.in_features
self.m._fc = nn.Identity()
avgpool = 'avgpool' if hasattr(self.m, 'avgpool')else '_avg_pooling'
setattr(self.m, avgpool, AdaptiveConcatPool2d())
 final = nn.Linear(nc * 2, n)
 self.head = nn.Sequential(*list(self.m.children())[-4:], Flatten() , final)
 else:
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.BatchNorm1d(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])
 if self.arch.startswith('efficient'):
x = self.m.extract_features(x)
 else:
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
class Unnormalize:

 def __init__(self, mean, std):
self.mean = mean
self.std = std
def __call__(self, tensor):
mean = torch.as_tensor(self.mean, dtype=tensor.dtype, device=tensor.device ).view(3, 1, 1)
 std = torch.as_tensor(self.std, dtype=tensor.dtype, device=tensor.device ).view(3, 1, 1)
 return torch.clamp(tensor*std + mean, 0., 1.)
normalize_transform = Normalize(mean_224, std_224)
 unnormalize_transform = Unnormalize(mean_224, std_224)
 def tileSegm(img,N=NN,sz=56):
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, idxs
class PandaSegmDataset(Dataset):
def __init__(self, tiles):
self.tiles = tiles
print("tiles.shape=",self.tiles.shape)
 def __getitem__(self, index):
tile=self.tiles[index]
tile=torch.tensor(tile ).permute(( 2, 0, 1)).float().div(255)
 tile= normalize_transform(tile)
 return tile
def __len__(self):
return len(self.tiles)
 def predict(loader,model):
pred =[]
with tqdm(total=len(loader), leave=False)as pbar:
for batch_idx, data in enumerate(loader):
with torch.no_grad() :
x = data.to(device)
 y_pred = model(x)
 y_pred=torch.sigmoid(y_pred.squeeze())
 pred.append(y_pred.cpu().numpy())
 pbar.update()
pred=np.hstack(pred)
 return pred
def tile(img,idxs,sz=224,N=NN):
if debug:
print('tile_aksell')
 print("tile_sz",sz)
 print("NN",N)
 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)
 img = img[idxs]
return img
def expand_tile(img,idxs,sz=224,N=NN, expand_sz = expand_sz):
if debug:
print('tile_expand_tile')
 print("tile_sz",expand_sz)
 print("NN",N)
 gap =(expand_sz - sz)
 shape = img.shape
pad0,pad1 =(sz - shape[0]%sz)%sz,(sz - shape[1]%sz)%sz
padgap0,padgap1 = pad0 + gap, pad1 + gap
img_org = np.pad(img,[[pad0//2,pad0-pad0//2],[pad1//2,pad1-pad1//2],[0,0]],
constant_values=255)
 img_org_pad = np.pad(img,[[padgap0//2,padgap0-padgap0//2],[padgap1//2,padgap1-padgap1//2],[0,0]],
constant_values=255)
 img_w_h = img_org.reshape(img_org.shape[0]//sz,sz,img_org.shape[1]//sz,sz,3)
 img_w_h = img_w_h.transpose(0,2,1,3,4)
 img = img_w_h.reshape(-1,sz,sz,3)
 expand_img_list = []
for vertical_row in range(0,img_w_h.shape[0]):
for horizontal_row in range(0,img_w_h.shape[1]):
expand_tile_img = img_org_pad[(sz*vertical_row):(sz*(vertical_row+1)) +gap ,(sz*horizontal_row):(sz*(horizontal_row+1)) +gap]
expand_img_list.append(expand_tile_img)
 expand_img = np.asarray(expand_img_list)
 if debug:
print("gap",gap)
 print('img_w_h',img_w_h.shape)
 print("img.shape",img.shape)
 print('expand_img_shape',expand_img.shape)
 if len(expand_img)< N:
expand_img = np.pad(expand_img,[[0,N-len(expand_img)],[0,0],[0,0],[0,0]],constant_values=255)
 expand_img = expand_img[idxs]
return expand_img
def loadANDtile(image_id,tile_idx, N=NN, phase=0):
level=1
img = skimage.io.MultiImage(os.path.join(DATA,image_id+'.tiff')) [level]
isExpandTile = ensemble_list[phase]['isExpandTile']
if debug:
print("isExpandTile",isExpandTile)
 print("NN",NN)
 if isExpandTile:
tiles=expand_tile(img,tile_idx, N=N, expand_sz = ensemble_list[phase]['tileSize'])
 else:
tiles=tile(img,tile_idx, N=N)
 return tiles
class ResnetDataset(Dataset):
def __init__(self, df, N=NN, phase=0):
self.df = df
self.N = N
self.phase = phase
print("length=",len(self.df))
def __len__(self):
return len(self.df)
 def __getitem__(self, idx):
row = self.df.iloc[idx]
image_id = row["image_id"]
tile_idx = row["tile_idx"]
tile_idx=[int(i)for i in tile_idx]
tiles=loadANDtile(image_id,tile_idx, N=self.N, phase=self.phase)
 tiles = torch.Tensor(1.0 - tiles/255.0)
 mean = ensemble_list[self.phase]['mean']
std = ensemble_list[self.phase]['std']
if debug:
print("mean",mean)
 print("std",std)
 tiles =(tiles - mean)/std
return tiles.permute(0,3,1,2), image_id
model.fit(train_X, train_y, batch_size=512, epochs=2, validation_data=(dev_X, dev_y),class_weight=weight )<predict_on_test>
segmmodel = timm.create_model('mixnet_xl', pretrained=False)
 for param in segmmodel.parameters() :
param.requires_grad = False
segmmodel.classifier=nn.Linear(1536, 1)
 segmmodel.fc = nn.Linear(1536, 1 )
f1=[]
pred_noemb_val_y = model.predict([dev_X], batch_size=1024, verbose=1)
 for thresh in np.arange(0.4, 0.702, 0.01):
thresh = np.round(thresh, 2)
 f1.append(metrics.f1_score(dev_y,(pred_noemb_val_y>thresh ).astype(int)))
 print("F1 score at threshold {0} is {1}".format(thresh, metrics.f1_score(dev_y,(pred_noemb_val_y>thresh ).astype(int))))
f1=np.array(f1 )<set_options>
checkpoint = torch.load(mixnet_pth, map_location=device)
 segmmodel.load_state_dict(checkpoint)
 segmmodel.eval()
segmmodel.cuda()
del checkpoint
thresh=np.argmax(f1)*0.01+0.4
print(thresh )<predict_on_test>
aknell_models = []
for model_index, path in enumerate(MODELS):
print("path",path)
 state_dict = torch.load(path,map_location=torch.device(device))
model = Model(arch=ensemble_list[model_index]['arch'])
 model.load_state_dict(state_dict)
 model.float()
model.eval()
model.cuda()
aknell_models.append(model)
 del state_dict
pred_noemb_val_y1 = model.predict([test_X], verbose=1)
 label1=(pred_noemb_val_y1>thresh ).astype(int)
 model=None<define_variables>
test=pd.read_csv(TEST)
 if chk:
pass
test=test[:][:nchk]
test_labels=label1<save_to_csv>
test_image_dir='/kaggle/input/prostate-cancer-grade-assessment/test_images'
if os.path.exists(test_image_dir):
print('test set exist')
 chk=False
mode='test'
test_image_dir='/kaggle/input/prostate-cancer-grade-assessment/{}_images'.format(mode)
 csv_path='/kaggle/input/prostate-cancer-grade-assessment/{}.csv'.format(mode)
 else:
print('test set not exist')
 chk=True
mode='train'
test_image_dir='/kaggle/input/prostate-cancer-grade-assessment/{}_images'.format(mode)
 csv_path='/kaggle/input/prostate-cancer-grade-assessment/{}.csv'.format(mode)
 submission_path='/kaggle/input/prostate-cancer-grade-assessment/sample_submission.csv'
df=pd.read_csv(csv_path)
 if chk:
pass
df=df[:][:nchk]
mean_256 = [0.85841531, 0.78983734, 0.89534979]
std_256 = [0.16897951, 0.25758676, 0.13730845]
first_effb3_path_256 = '/kaggle/input/pandalayertest4wunertaintyacc/pandalayertest4wunertaintyacc/fold{}top{}.ckpt'
ensemble_shujun_1 ={"mean":mean_256,"std":std_256,"tileImageSize":256,"isCutThreshold":False,"arch":"resnext50_32x4d","path":first_effb3_path_256,"TILE_SIZE":36,"TILE_ALL":False,"CVT_COLOR":True}
mean_224= [0.81702482, 0.6299724, 0.75427603]
std_224=[0.40702703, 0.52099041, 0.42290275]
second_resnext50_path_224 = '/kaggle/input/pandaakselltest3/fold{}top{}.ckpt'
ensemble_shujun_2 ={"mean":mean_224,"std":std_224,"tileImageSize":224,"isCutThreshold":True,"arch":"resnext50_32x4d","path":second_resnext50_path_224,"TILE_SIZE":20,"TILE_ALL":False,"CVT_COLOR":True}
mean_224_30tile= [0.82514849, 0.64166622, 0.76170417]
std_224_30tile=[0.40444484, 0.52074353, 0.42308628]
third_resnext50_path_224 = '/kaggle/input/pandaakselltest4/fold{}top{}.ckpt'
ensemble_shujun_3 ={"mean":mean_224_30tile,"std":std_224_30tile,"tileImageSize":224,"isCutThreshold":True,"arch":"resnext50_32x4d","path":third_resnext50_path_224,"TILE_SIZE":30,"TILE_ALL":False,"CVT_COLOR":False}
mean_288 = mean_224
std_288 = std_224
fourth_resnext50_path_288 = '/kaggle/input/pandaakselltest5/fold{}top{}.ckpt'
ensemble_shujun_4 ={"mean":mean_288,"std":std_288,"tileImageSize":288,"isCutThreshold":True,"arch":"resnext50_32x4d","path":fourth_resnext50_path_288,"TILE_SIZE":20,"TILE_ALL":False,"CVT_COLOR":False}
mean_224_32= [0.82514849, 0.64166622, 0.76170417]
std_224_32=[0.40444484, 0.52074353, 0.42308628]
fifth_resnext50_path_224 = '/kaggle/input/pandaakselltest7/fold{}top{}.ckpt'
ensemble_shujun_5 ={"mean":mean_224_32,"std":std_224_32,"tileImageSize":224,"isCutThreshold":True,"arch":"resnext50_32x4d","path":fifth_resnext50_path_224,"TILE_SIZE":32,"TILE_ALL":True,"CVT_COLOR":True}
mean_256_64= [0.8021112084388733, 0.7040286064147949, 0.8552106022834778]
std_256_64=[0.18155023455619812, 0.27437373995780945, 0.15957728028297424]
sixth_resnext50_path_256 = '/kaggle/input/resnext50-new/fold{}top{}.ckpt'
ensemble_shujun_6 ={"mean":mean_256_64,"std":std_256_64,"tileImageSize":256,"isCutThreshold":False,"arch":"resnext50_32x4d","path":sixth_resnext50_path_256,"TILE_SIZE":64,"TILE_ALL":False,"CVT_COLOR":True}
ensemble_shujun_list = []
ensemble_shujun_list.append(ensemble_shujun_1)
 ensemble_shujun_list.append(ensemble_shujun_2)
 ensemble_shujun_list.append(ensemble_shujun_4)
 ensemble_shujun_list.append(ensemble_shujun_5)
 ensemble_shujun_list.append(ensemble_shujun_6 )
submission=pd.read_csv(".. /input/sample_submission.csv")
 submission["prediction"]=test_labels
submission.head()
submission.to_csv("submission.csv",index=False )<string_transform>
class Mish(nn.Module):
def __init__(self):
super().__init__()
def forward(self, x):
return x *(torch.tanh(F.softplus(x)))
 def gem(x, p=3, eps=1e-6):
return F.avg_pool2d(x.clamp(min=eps ).pow(p),(x.size(-2), x.size(-1)) ).pow(1./p)
 class GeM(nn.Module):
def __init__(self, p=3, eps=1e-6):
super(GeM,self ).__init__()
self.p = Parameter(torch.ones(1)*p)
 self.eps = eps
def forward(self, x):
return gem(x, p=self.p, eps=self.eps)
 def __repr__(self):
return self.__class__.__name__ + '(' + 'p=' + '{:.4f}'.format(self.p.data.tolist() [0])+ ', ' + 'eps=' + str(self.eps)+ ')'
class MultiheadAttentionClassifier(nn.Module):
def __init__(self,num_classes,out_features,ninp,nhead,dropout,attention_dropout=0.1):
super(MultiheadAttentionClassifier, self ).__init__()
self.attention=nn.MultiheadAttention(ninp, nhead, dropout=attention_dropout)
 self.classifier=nn.Linear(ninp*2,num_classes)
 self.dropout=nn.Dropout(dropout)
 self.mish=Mish()
def forward(self,x):
x=x.permute(1,0,2)
 x,_=self.attention(x,x,x)
 x=self.mish(x)
 x=x.permute(1,0,2)
 max_x,_=torch.max(x,dim=1)
 x=torch.cat([torch.mean(x,dim=1),max_x],dim=-1)
 x=self.dropout(x)
 x=self.classifier(x)
 return x
def _resnext(url, block, layers, pretrained, progress, **kwargs):
model = ResNet(block, layers, **kwargs)
 return model
class Network(nn.Module):
def __init__(self,num_classes,arch='efficientnet-b0',dropout_p=0.5,nhead=8,ninp=512, nhid=512,attention_dropout=0.1):
super(Network, self ).__init__()
if debug:
print('arch',arch)
 if arch.startswith('efficient'):
self.backbone=EfficientNet.from_name(arch)
 elif arch == 'resnet34':
self.backbone=models.resnet34(pretrained=False,)
 elif arch == 'resnext50_32x4d':
self.backbone=models.resnext50_32x4d(pretrained=False)
 self.backbone.avgpool=GeM()
self.num_classes=num_classes
self.out_features = self.backbone.fc.in_features
self.ninp=ninp
self.backbone.fc=nn.Linear(self.out_features,ninp)
 self.regression_head=MultiheadAttentionClassifier(1,self.out_features,ninp,nhead,dropout_p)
 self.classifier_head=MultiheadAttentionClassifier(num_classes[0],self.out_features,ninp,nhead,dropout_p)
 self.regression_head2=MultiheadAttentionClassifier(1,self.out_features,ninp,nhead,dropout_p)
 self.regression_head3=MultiheadAttentionClassifier(1,self.out_features,ninp,nhead,dropout_p)
 def forward(self,x):
shape = x[0].shape
bs=x.shape[0]
n = len(x)
 x = x.reshape(( -1,shape[1],shape[2],shape[3]))
features=self.backbone(x)
 features=features.reshape(bs,shape[0],self.ninp)
 outputs=[self.regression_head(features ).squeeze() ,F.log_softmax(self.classifier_head(features),dim=1),
self.regression_head2(features ).squeeze() ,self.regression_head3(features ).squeeze() ]
return outputs
def tile_shujun(img, sz=224,N=20):
if debug:
print('tile_shujun')
 result = []
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
def tile_all(img, N, sz=256):
if debug:
print('tile_all_shujun')
 print('N',N)
 result = []
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))
return img, idxs
def standardize_batch(batch,mean,std,isCutThresholdTile):
if debug:
print("test:",batch.shape[1])
 if batch.shape[1]==64:
if debug:
print("st_01")
 batch=batch/255.0
batch=(batch-mean)/std
elif isCutThresholdTile:
if debug:
print("st_02")
 batch=batch/255.0
batch=(mean-batch)/std
else:
if debug:
print("st_03")
 batch=batch/255.0
return batch
def get_batch_of_image(paths, sz, N, phase = 0, index_df = None):
isCutThreshold = ensemble_shujun_list[phase]['isCutThreshold']
isTileAll = ensemble_shujun_list[phase]['TILE_ALL']
if debug:
print('sz',sz)
 print('N',N)
 print('isCutThreshold',isCutThreshold)
 images=[]
for path in paths:
if isCutThreshold:
tile_idx = index_df.loc[index_df['image_id']== path]['tile_idx'].tolist() [0]
tile_idx=[int(i)for i in tile_idx]
if debug:
print(tile_idx)
 path=os.path.join(test_image_dir,path+'.tiff')
 img = skimage.io.MultiImage(path,conserve_memory=False,dtype='tiff')[1]
if isCutThreshold:
if isTileAll:
tile_idx = tile_idx[:20]
if debug:
print(tile_idx)
 Max_Indices_N = max(tile_idx)+1
if debug:
print("Max_Indices_N",Max_Indices_N)
 img, idxs = tile_all(img, sz=sz, N=Max_Indices_N)
 tiles = []
for sorted_index in idxs:
if sorted_index not in tile_idx:
tiles.append(img[sorted_index])
 if len(tiles)>=N-20:
break
if len(tiles)< N-20:
tiles = np.pad(tiles,[[0,N-len(tiles)],[0,0],[0,0],[0,0]],constant_values=255)
 tiles = np.asarray(tiles)
 if debug:
print("before_concate_tiles_shape",tiles.shape)
 print("aksell_indices.shape",img[tile_idx].shape)
 tiles = np.concatenate([tiles,img[tile_idx]] ).astype("uint8")
 if debug:
print("tiles",tiles.shape)
 img = tiles
else:
if sz == 224:
img = tile(img,tile_idx, N=NN)
 else:
img = expand_tile(img,tile_idx, sz=224, N=NN, expand_sz = sz)
 else:
img = tile_shujun(img, sz=sz, N=N)
 if(ensemble_shujun_list[phase]['CVT_COLOR']==True):
if debug:
print("CVT_COLOR is True")
 for j in range(img.shape[0]):
img[j] = cv2.cvtColor(img[j],cv2.COLOR_RGB2BGR)
 else:
if debug:
print("CVT_COLOR is False")
 img=np.asarray(img,dtype='uint8')
 images.append(img)
 images=np.asarray(images,dtype='uint8' ).transpose(( 0,1,4,2,3))
return images
def classification_threshold(tensor):
for i in range(len(tensor)) :
if tensor[i] > 4.5:
tensor[i]=5
elif tensor[i] > 3.5:
tensor[i]=4
elif tensor[i] > 2.5:
tensor[i]=3
elif tensor[i] > 1.5:
tensor[i]=2
elif tensor[i] > 0.6:
tensor[i]=1
elif tensor[i] < 0.6:
tensor[i]=0
return tensor.astype('int')
 def classification_threshold_item(item):
if item > 4.5:
item=5
elif item > 3.5:
item=4
elif item > 2.5:
item=3
elif item > 1.5:
item=2
elif item > 0.6:
item=1
elif item < 0.6:
item=0
return int(item)
 def hflip(tensor):
for i in range(tensor.shape[0]):
for j in range(tensor.shape[1]):
tensor[i,j]=np.flip(tensor[i,j],axis=0)
 return tensor
def vflip(tensor):
for i in range(tensor.shape[0]):
for j in range(tensor.shape[1]):
tensor[i,j]=np.flip(tensor[i,j],axis=1)
 return tensor
dim = 300
num_words = 75966
max_len = 100
print('Fiting tokenizer')
 tokenizer = Tokenizer(num_words=num_words)
 tokenizer.fit_on_texts(list(df['question_text'])+list(df_final['question_text']))
print('text to sequence')
 x_train = tokenizer.texts_to_sequences(df['question_text'])
 print('pad sequence')
 x_train = pad_sequences(x_train,maxlen=max_len)
 y_train = df['target'].values
print(x_train.shape)
 print(y_train.shape)
 x_test=tokenizer.texts_to_sequences(df_final['question_text'])
 x_test = pad_sequences(x_test,maxlen=max_len)
 print('Test data loaded:',x_test.shape )<compute_train_metric>
class JaneyMultiheadAttentionClassifier(nn.Module):
def __init__(self,num_classes,out_features,ninp,nhead,dropout,nlayers=1,attention_dropout=0.1):
super(JaneyMultiheadAttentionClassifier, self ).__init__()
encoder_layers = nn.TransformerEncoderLayer(ninp, nhead, ninp*4, attention_dropout)
 encoder_layers.activation=Mish()
self.attention = nn.TransformerEncoder(encoder_layers, nlayers)
 self.classifier=nn.Linear(ninp*2,num_classes)
 self.dropout=nn.Dropout(dropout)
 def forward(self,x):
x=self.dropout(x)
 x=x.permute(1,0,2)
 x=self.attention(x)
 x=x.permute(1,0,2)
 max_x,_=torch.max(x,dim=1)
 x=torch.cat([torch.mean(x,dim=1),max_x],dim=-1)
 x=self.dropout(x)
 x=self.classifier(x)
 return x
class JaneyNetwork(nn.Module):
def __init__(self,num_classes,efficientNet_name='efficientnet-b0',dropout_p=0.5,nhead=8,ninp=512, nhid=512,attention_dropout=0.1):
super(JaneyNetwork, self ).__init__()
self.backbone=models.resnext50_32x4d(pretrained=False)
 self.backbone.avgpool=GeM()
self.num_classes=num_classes
self.out_features = self.backbone.fc.in_features
self.ninp=ninp
self.backbone.fc=nn.Identity()
regression_head=JaneyMultiheadAttentionClassifier(1,self.out_features,ninp,nhead,dropout_p)
 classifier_head=JaneyMultiheadAttentionClassifier(num_classes[0],self.out_features,ninp,nhead,dropout_p)
 regression_head2=JaneyMultiheadAttentionClassifier(1,self.out_features,ninp,nhead,dropout_p)
 regression_head3=JaneyMultiheadAttentionClassifier(1,self.out_features,ninp,nhead,dropout_p)
 self.heads=nn.ModuleList([regression_head,classifier_head,regression_head2,regression_head3,nn.Linear(self.out_features,ninp)])
 def forward(self,x):
shape = x[0].shape
bs=x.shape[0]
n = len(x)
 x = x.reshape(( -1,shape[1],shape[2],shape[3]))
features=self.backbone(x)
 features=self.heads[4](features)
 features=features.reshape(bs,shape[0],self.ninp)
 outputs=[self.heads[0](features ).squeeze(-1),F.log_softmax(self.heads[1](features),dim=1),
self.heads[2](features ).squeeze(-1),self.heads[3](features ).squeeze(-1)]
return outputs
print('Glove...')
 def get_coefs(word,*arr): return word, np.asarray(arr, dtype='float32')
 embeddings_index = dict(get_coefs(*o.split(" ")) for o in open('.. /input/embeddings/glove.840B.300d/glove.840B.300d.txt'))
all_embs = np.stack(embeddings_index.values())
 emb_mean,emb_std = all_embs.mean() , all_embs.std()
print(len(all_embs))
word_index = tokenizer.word_index
embedding_matrix_glov = np.random.normal(emb_mean, emb_std,(num_words, dim))
count=0
for word, i in word_index.items() :
if i >= num_words:
break
embedding_vector = embeddings_index.get(word)
 if embedding_vector is not None:
embedding_matrix_glov[i] = embedding_vector
else:
count += 1
print('embedding matrix size:',embedding_matrix_glov.shape)
 print('Number of words not in vocab:',count)
 del embeddings_index,all_embs
gc.collect()<statistical_test>
df=df.set_index('image_id')
 df["tile_idx"]=np.nan
df=df.astype(object)
 iuniq=index_df.image_id.unique()
for i in tqdm(iuniq):
idxs=index_df.loc[index_df.image_id==i].idxs.values
df.at[i,"tile_idx"]=idxs
df = df.reset_index()
del dataset
del loader
del index_df
del y
gc.collect()
print('Para...')
 EMBEDDING_FILE = '.. /input/embeddings/paragram_300_sl999/paragram_300_sl999.txt'
def get_coefs(word,*arr): return word, np.asarray(arr, dtype='float32')
 embeddings_index = dict(get_coefs(*o.split(" ")) for o in open(EMBEDDING_FILE, encoding="utf8", errors='ignore')if len(o)>100)
 all_embs = np.stack(embeddings_index.values())
 emb_mean,emb_std = all_embs.mean() , all_embs.std()
print(len(all_embs))
word_index = tokenizer.word_index
embedding_matrix_para = np.random.normal(emb_mean, emb_std,(num_words, dim))
count=0
for word, i in word_index.items() :
if i >= num_words:
break
embedding_vector = embeddings_index.get(word)
 if embedding_vector is not None:
embedding_matrix_para[i] = embedding_vector
else:
count += 1
print('embedding matrix size:',embedding_matrix_glov.shape)
 print('Number of words not in vocab:',count)
 del embeddings_index,all_embs
gc.collect()<feature_engineering>
Shujun_PRED_LIST=[]
for pred_list_index in range(len(ensemble_shujun_list)) :
Shujun_PRED_LIST.append([])
 for phase_index, ensemble_recipe in enumerate(ensemble_shujun_list):
print("shujun_ensemble_phase",phase_index)
 MODELS=[]
top=3
num_classes=[6,4,4,]
if ensemble_recipe['tileImageSize'] == 288:
print("top_fold is 3")
 top_fold = 3
else:
top_fold = 2
for i in range(top_fold,top_fold+1):
for j in range(top):
if ensemble_recipe['TILE_SIZE'] == 64:
if debug:
print("Janey Network")
 model = JaneyNetwork(num_classes ).to(device)
 else:
model = Network(num_classes,arch = ensemble_recipe['arch'] ).to(device)
 model = model.to(device)
 model = nn.DataParallel(model)
 weights_paths=ensemble_recipe['path'].format(i,j+1)
 if debug:
print(weights_paths)
 model.load_state_dict(torch.load(weights_paths))
model.eval()
MODELS.append(model)
 total_images=0
if ensemble_recipe['TILE_SIZE'] == 64 or ensemble_recipe['tileImageSize'] == 288:
batch_size=8
else:
batch_size=16
if debug:
print("batch_size",batch_size)
 if len(df)%batch_size==0:
batches=int(len(df)/batch_size)
 else:
batches=int(len(df)/batch_size)+1
model.eval()
predictions=[]
mean=torch.Tensor(ensemble_recipe['mean'] ).to(device ).reshape(1,1,3,1,1)
 std=torch.Tensor(ensemble_recipe['std'] ).to(device ).reshape(1,1,3,1,1)
 if debug:
print(mean)
 print(std)
 with torch.no_grad() :
for i in tqdm(range(batches)) :
paths=df.image_id[i*batch_size:(i+1)*batch_size].to_list()
x=get_batch_of_image(paths, sz = ensemble_recipe['tileImageSize'], N = ensemble_recipe['TILE_SIZE'], phase=phase_index, index_df = df)
 x0=torch.Tensor(x ).to(device)
 x_h=torch.Tensor(x ).to(device ).flip(-1)
 x_v=torch.Tensor(x ).to(device ).flip(-2)
 x_vh=torch.Tensor(x ).to(device ).flip(-1,-2)
 x0t=torch.Tensor(x ).to(device ).transpose(-1,-2)
 x_ht=torch.Tensor(x ).to(device ).flip(-1 ).transpose(-1,-2)
 x_vt=torch.Tensor(x ).to(device ).flip(-2 ).transpose(-1,-2)
 x_vht=torch.Tensor(x ).to(device ).flip(-1,-2 ).transpose(-1,-2)
 outputs=[]
isCutThresholdTile = ensemble_recipe['isCutThreshold']
if debug:
print('isCutThresholdTile',isCutThresholdTile)
 for model in MODELS:
output = model(standardize_batch(x0,mean,std,isCutThresholdTile=isCutThresholdTile)) [0]+\
model(standardize_batch(x_h,mean,std,isCutThresholdTile=isCutThresholdTile)) [0]+\
model(standardize_batch(x_v,mean,std,isCutThresholdTile=isCutThresholdTile)) [0]+\
model(standardize_batch(x_vh,mean,std,isCutThresholdTile=isCutThresholdTile)) [0]+\
model(standardize_batch(x0t,mean,std,isCutThresholdTile=isCutThresholdTile)) [0]+\
model(standardize_batch(x_ht,mean,std,isCutThresholdTile=isCutThresholdTile)) [0]+\
model(standardize_batch(x_vt,mean,std,isCutThresholdTile=isCutThresholdTile)) [0]+\
model(standardize_batch(x_vht,mean,std,isCutThresholdTile=isCutThresholdTile)) [0]
output=output.cpu().numpy() /8
outputs.append(output)
 outputs=np.asarray(outputs)
 outputs=np.mean(outputs,axis=0)
 predictions.append(output)
 for pred in predictions:
Shujun_PRED_LIST[phase_index].extend(pred)
 del output, predictions, MODELS
torch.cuda.empty_cache()
if debug:
for check_p_index in range(len(Shujun_PRED_LIST)) :
print(len(Shujun_PRED_LIST[check_p_index]))
print("at last ensemble phase")
 shujun_pred_list = np.asarray(Shujun_PRED_LIST)
 print("shape",shujun_pred_list.shape)
 if debug:
print(Aksell_PRED_LIST)
 print(Shujun_PRED_LIST)
 total_list = np.vstack(( np.asarray(Aksell_PRED_LIST),np.asarray(Shujun_PRED_LIST)) ).transpose()
matrixes = [embedding_matrix_glov,embedding_matrix_para]
matrix = np.mean(matrixes,axis=0)
 del embedding_matrix_glov,embedding_matrix_para
gc.collect()<import_modules>
label_list = []
for total_list_index, list_item in enumerate(total_list.tolist()):
predictions = list(list_item)
 final_value = np.mean(predictions)
 label=classification_threshold_item(final_value)
 if debug:
print("final_value",final_value)
 print("!!label!!",label)
 label_list.append(label)
 predictions_output=np.asarray(label_list,dtype='int')
 sub_df = pd.DataFrame({'image_id':df.image_id.tolist() , 'isup_grade': list(predictions_output)})
 if chk:
ground_truths=np.asarray(df.isup_grade,dtype='int')
 score=metrics.cohen_kappa_score(ground_truths,predictions_output,weights='quadratic')
 print(score)
 sub_df['ig']=df.isup_grade[:nchk]
sub_df.to_csv('submission.csv',index=False )
<save_to_csv><EOS>