Kaggle competition: APTOS 2019 Blindness Detection
<SOS> metric: QuadraticWeightedKappa Kaggle data source: aptos-2019-blindness-detection<save_to_csv>
%matplotlib inline
warnings.filterwarnings("ignore" )
if False:
train_error_generator = DataGenerator(X_train, shuffle=False, batch_size=256)
 prob_train = keras_model.predict_generator(
 train_error_generator,
use_multiprocessing=True,
workers=2, 
)
 mispred_train = y_train !=(prob_train > best_thresh ).ravel()
print(mispred_train.mean() , mispred_train.sum())
 train_data[mispred_train.astype('bool')].to_csv('mispreditions.csv', index=False )<predict_on_test>
data_dir = '.. /input/aptos2019-blindness-detection/'
test_dir = data_dir + '/test_images/'
label = pd.read_csv(data_dir+"test.csv")
 IMG_SIZE = 224
test_generator = DataGenerator(X_test, shuffle=False, batch_size=256)
 prob_test = keras_model.predict_generator(
 test_generator,
use_multiprocessing=True,
workers=2 , 
)
 pred_test =(prob_test > best_thresh ).astype('int32' )<create_dataframe>
path = ".. /input/efficient-net-deps-1"
sys.path.append(path)
submission = pd.DataFrame({'qid': test_data.qid, 'prediction': pred_test.ravel() } )<save_to_csv>
model = EfficientNet.from_name('efficientnet-b0')
 model.load_state_dict(torch.load('.. /input/efficientnet-pytorch/efficientnet-b0-08094119.pth'))
num_ftrs = model._fc.in_features
model._fc = nn.Linear(num_ftrs, 1)
 for param in model.parameters() :
param.requires_grad = True
model = model.to('cuda')
 optimizer = torch.optim.Adam(model.parameters() , lr=0.00015, weight_decay=1e-5)
 loss_func = nn.MSELoss()
train_on_gpu = torch.cuda.is_available()
model.load_state_dict(torch.load(".. /input/weights/weights.pt"))
model.eval()
submission.to_csv('submission.csv', index=False )<save_to_csv>
data_transf = transforms.Compose([
transforms.ToPILImage() ,
transforms.RandomRotation(( -180, 180)) ,
transforms.RandomHorizontalFlip(p=0.4),
transforms.RandomVerticalFlip(p=0.5),
transforms.ToTensor() ,
transforms.Normalize(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225))
])
 test_data = Imagedata(df = label, data_dir = test_dir, transform = data_transf)
 test_loader = DataLoader(dataset = test_data, batch_size = 64 )
submission.to_csv('submission.csv', index=False )<import_modules>
num = len(pd.read_csv(".. /input/aptos2019-blindness-detection/sample_submission.csv"))
test_preds = np.zeros(( num, 1))
device = torch.device("cuda:0")
 for j in range(5):
i = 0
tk0 = tqdm(test_loader)
 for i, x_batch in enumerate(tk0):
pred = model(x_batch.to(device))
test_preds[i * 64:(i + 1)* 64] += pred.detach().cpu().squeeze().numpy().ravel().reshape(-1, 1)* 0.2
test_preds
tqdm.pandas(desc='Progress')
<define_variables>
coef = [0.5, 1.5, 2.5, 3.5]
for i, pred in enumerate(test_preds):
if pred < coef[0]:
test_preds[i] = 0
elif pred >= coef[0] and pred < coef[1]:
test_preds[i] = 1
elif pred >= coef[1] and pred < coef[2]:
test_preds[i] = 2
elif pred >= coef[2] and pred < coef[3]:
test_preds[i] = 3
else:
test_preds[i] = 4
sample = pd.read_csv(".. /input/aptos2019-blindness-detection/sample_submission.csv")
 sample.diagnosis = test_preds.astype(int)
 sample.to_csv("submission.csv", index=False)
 sample