Kaggle competition: SIIM-ISIC Melanoma Classification
<SOS> metric: MCAUC Kaggle data source: siim-isic-melanoma-classification<feature_engineering>
!pip install -q efficientnet_pytorch > /dev/null
new_test_df['Fare'] = new_test_df['Adjusted_Fare']*new_test_df['Members']
new_test_df.info()<prepare_output>
warnings.filterwarnings("ignore")
 warnings.filterwarnings("ignore", category=DeprecationWarning)
 SEED = 42
def seed_everything(seed):
random.seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 np.random.seed(seed)
 torch.manual_seed(seed)
 torch.cuda.manual_seed(seed)
 torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = True
seed_everything(SEED )
data_model = new_train_df[['Survived', 'Pclass', 'Sex', 'Age', 'Embarked', 'Members', 'Adjusted_Fare', 'Title']]
data_model.head()<split>
DATA_PATH = '.. /input/melanoma-merged-external-data-512x512-jpeg'
train_X, test_X, train_y, test_y = train_test_split(data_model.drop(['Survived'], axis=1), data_model['Survived'],\
stratify=data_model['Survived'], random_state=123, test_size=0.25 )<choose_model_class>
TEST_ROOT_PATH = f'{DATA_PATH}/512x512-test/512x512-test'
def get_valid_transforms() :
return A.Compose([
A.Resize(height=512, width=512, p=1.0),
ToTensorV2(p=1.0),
], p=1.0)
 class DatasetRetriever(Dataset):
def __init__(self, image_ids, transforms=None):
super().__init__()
self.image_ids = image_ids
self.transforms = transforms
def __getitem__(self, idx: int):
image_id = self.image_ids[idx]
image = cv2.imread(f'{TEST_ROOT_PATH}/{image_id}.jpg', cv2.IMREAD_COLOR)
 image = image.astype(np.float32)/ 255.0
if self.transforms:
sample = {'image': image}
sample = self.transforms(**sample)
 image = sample['image']
return image, image_id
def __len__(self)-> int:
return self.image_ids.shape[0]
lr = LogisticRegression()
params_lr = {'C':[0.001, 0.01, 0.1, 1, 10, 100]}<choose_model_class>
def get_net() :
net = EfficientNet.from_name('efficientnet-b5')
 net._fc = nn.Linear(in_features=2048, out_features=2, bias=True)
 return net
net = get_net().cuda()
lr_cv = GridSearchCV(lr, params_lr, n_jobs=-1, cv=5 )<train_model>
df_test = pd.read_csv(f'.. /input/siim-isic-melanoma-classification/test.csv', index_col='image_name')
 test_dataset = DatasetRetriever(
 image_ids=df_test.index.values,
transforms=get_valid_transforms() , 
)
 test_loader = torch.utils.data.DataLoader(
 test_dataset,
batch_size=8,
num_workers=2,
shuffle=False,
sampler=SequentialSampler(test_dataset),
pin_memory=False,
drop_last=False,
)
lr_cv.fit(train_X, train_y )<compute_train_metric>
checkpoint_path = '.. /input/melanoma-public-checkpoints/effnet5-best-score-checkpoint-015epoch-version2.bin'
checkpoint = torch.load(checkpoint_path)
 net.load_state_dict(checkpoint);
net.eval() ;
lr_cv_best_model = pd.DataFrame({'Params':lr_cv.best_params_.values() , 'best_score':lr_cv.best_score_,\
'Train Score':accuracy_score(train_y, lr_cv.best_estimator_.predict(train_X)) ,\
'Test Score': accuracy_score(test_y, lr_cv.best_estimator_.predict(test_X)) } )<find_best_params>
result = {'image_name': [], 'target': []}
for images, image_names in tqdm(test_loader, total=len(test_loader)) :
with torch.no_grad() :
images = images.cuda().float()
outputs = net(images)
 y_pred = nn.functional.softmax(outputs, dim=1 ).data.cpu().numpy() [:,1]
result['image_name'].extend(image_names)
 result['target'].extend(y_pred)
 submission = pd.DataFrame(result )
<train_on_grid><EOS> 
submission.to_csv('submission.csv', index=False)
 submission['target'].hist(bins=100);