Kaggle competition: APTOS 2019 Blindness Detection
test_ids_for_sub = test_df['id'].values
test_ids_for_sub<concatenate>
package_path = '.. /input/efficientnet/efficientnet-pytorch/EfficientNet-PyTorch/'
sys.path.append(package_path )
print("Combine Train and Submission")
 df = pd.concat([train_df, test_df],axis=0,ignore_index=True)
 del train_df, test_df
gc.collect()
print(df.shape)
 df.head()<string_transform>
from efficientnet_pytorch import EfficientNet
lemmatizer = WordNetLemmatizer()
def preprocess(ingredients):
ingredients_text = ' '.join(ingredients)
 ingredients_text = ingredients_text.lower()
ingredients_text = ingredients_text.replace('-', ' ')
 words = []
for word in ingredients_text.split() :
if re.findall('[0-9]', word): continue
if len(word)<= 2: continue
if '’' in word: continue
word = lemmatizer.lemmatize(word)
 if len(word)> 0: words.append(word)
 return ' '.join(words)
 for ingredient, expected in [ 
('Eggs', 'egg'), 
('all-purpose flour', 'all purpose flour'), 
('purée', 'purée'), 
('1% low-fat milk', 'low fat milk'), 
('half & half', 'half half'), 
('safetida(powder)', 'safetida(powder)')
 ]:
actual = preprocess([ingredient])
 assert actual == expected, f'"{expected}" is excpected but got "{actual}"'<feature_engineering>
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
df['ingredients'] = df['ingredients'].apply(lambda ingredients: preprocess(ingredients))<categorify>
seed_everything(1234)
 TTA = 5
num_classes = 1
IMG_SIZE = 256
print("TF-IDF on text data...")
 tfidf = TfidfVectorizer(binary=True)
 print("combine other features with text vectors")
 text_features = tfidf.fit_transform(df['ingredients'])
 print(text_features.shape)
 df_1 = pd.DataFrame(text_features.toarray())
 del text_features
gc.collect()
print(df_1.shape)
 print(df.shape)
 df_1.head()<concatenate>
test = '.. /input/aptos2019-blindness-detection/test_images/'
df = pd.concat([df, df_1, df_2],axis=1)
 del df_1
del df_2
gc.collect()
print(df.shape )<rename_columns>
test_transform = transforms.Compose([
transforms.RandomHorizontalFlip() ,
transforms.RandomRotation(( -120, 120)) ,
transforms.ToTensor() ,
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])])
 testset = MyDataset(pd.read_csv('.. /input/aptos2019-blindness-detection/sample_submission.csv'),
transform=test_transform)
 test_loader = torch.utils.data.DataLoader(testset, batch_size=16, shuffle=False )
df.columns =['target', 'id', 'ingredients', 'num_ingredients']+['TfidfV_'+str(n)for n in range(2867)]+['Ngram_'+str(n)for n in range(2000)]<drop_column>
model = EfficientNet.from_name('efficientnet-b0')
 in_features = model._fc.in_features
model._fc = nn.Linear(in_features, num_classes)
 model.load_state_dict(torch.load('.. /input/enet-test/weight_best(3 ).pt'))
model.cuda()
df.drop('ingredients',inplace=True,axis=True)
 df.head()<categorify>
for param in model.parameters() :
param.requires_grad = False
train_df = df[df['target'].notnull() ]
target = train_df['target']
print("Label Encode the Target Variable...")
 lb = LabelEncoder()
y = lb.fit_transform(target)
 len_y = len(set(y))

 bayes_trials = Trials()
tpe_algorithm = tpe.suggest
final_selection = [n for n in train_df.columns if n not in ['target', 'id']]
train_set = lgb.Dataset(train_df[final_selection], y)
 print(train_set)
 min_value = 100000
iteration = 0
def objective(params, n_folds = 5):
global iteration
iteration+=1

 params['num_leaves']=int(params['num_leaves'])
 params['min_data_in_leaf']=int(params['min_data_in_leaf'])
 params['max_depth']=int(params['max_depth'])
 params['min_child_weight']=int(params['min_child_weight'])
 params['colsample_bytree']=round(params['colsample_bytree'],5)
 params['reg_alpha']=round(params['reg_alpha'],5)
 params['reg_lambda']=round(params['reg_lambda'],5)
 params['subsample']=round(params['subsample'],5)
 params['learning_rate']=round(params['learning_rate'],5)
 params['learning_rate']=round(params['learning_rate'],5)
 cv_results = lgb.cv(params,
train_set,
nfold = n_folds,
num_boost_round = 10000,
early_stopping_rounds = 200,
metrics='multi_logloss',
seed = 50,
stratified=False)
 best_score = min(cv_results['multi_logloss-mean'])
 optimal_rounds =len(cv_results['multi_logloss-mean'])
 global min_value
if str(best_score)== '-inf':
best_score = min_value+1
if best_score < 0:
best_score = min_value+1
if best_score < min_value:
min_value = best_score
print(f'Round: {iteration}
the score is: {best_score:.3f}
the best score so far {min_value:.3f}')
 loss= best_score
of_connection = open(out_file, 'a')
 writer = csv.writer(of_connection)
 writer.writerow([loss, params, best_score, optimal_rounds])
 of_connection.close()
return {'loss': loss, 'params': params, 'status': STATUS_OK}
space = {
'subsample': hp.uniform('subsample', 0.2, 1),
'num_leaves': hp.quniform('num_leaves', 5, 150, 1),
'learning_rate': hp.loguniform('learning_rate', np.log(0.001), np.log(0.9)) ,
'min_child_samples': hp.quniform('min_child_samples', 1, 100, 1),
'min_data_in_leaf': hp.quniform('min_data_in_leaf', 1, 100, 1),
'reg_alpha': hp.uniform('reg_alpha', 0.0, 1.0),
'reg_lambda': hp.uniform('reg_lambda', 0.0, 1.0),
'colsample_bytree': hp.uniform('colsample_bytree', 0.2, 1.0),
'max_depth':hp.uniform('max_depth', 2, 15),
'min_child_weight':hp.quniform('min_child_weight', 1, 100, 1),
}
out_file = 'gbm_trials_essential.csv'
of_connection = open(out_file, 'w')
 writer = csv.writer(of_connection)
 writer.writerow(['loss', 'params', 'best_score', 'optimal_rounds'])
 of_connection.close()
MAX_EVALS = 40
best = fmin(fn = objective,
space = space,
algo = tpe.suggest,
max_evals = MAX_EVALS,
trials = bayes_trials)
<init_hyperparams>
sample = pd.read_csv('.. /input/aptos2019-blindness-detection/sample_submission.csv' )
params = {}
params['num_leaves']=int(best['num_leaves'])
 params['min_data_in_leaf']=int(best['min_data_in_leaf'])
 params['max_depth']=int(best['max_depth'])
 params['min_child_weight']=int(best['min_child_weight'])
 params['colsample_bytree']=round(best['colsample_bytree'],5)
 params['reg_alpha']=round(best['reg_alpha'],5)
 params['reg_lambda']=round(best['reg_lambda'],5)
 params['subsample']=round(best['subsample'],5)
 params['learning_rate']=round(best['learning_rate'],5)
<count_unique_values>
%%time
test_pred = np.zeros(( len(sample), 1))
model.eval()
for _ in range(TTA):
with torch.no_grad() :
for i, data in tqdm(enumerate(test_loader)) :
images, _ = data
images = images.cuda()
pred = model(images)
 test_pred[i * 16:(i + 1)* 16] += pred.detach().cpu().squeeze().numpy().reshape(-1, 1)
 output = test_pred / TTA
len(target.unique() )<choose_model_class>
coef = [0.57, 1.37, 2.57, 3.57]
def kfold_lightgbm(train_df, test_df,
num_folds, lr = 0.02,
stratified = True, params={},
n_estimators=5000000, early_stopping_rounds= 200):
print("Starting LightGBM.Train shape: {}, test shape: {}".format(train_df.shape, test_df.shape))
gc.collect()
if stratified:
folds = StratifiedKFold(n_splits= num_folds, shuffle=True, random_state=1001)
 else:
folds = KFold(n_splits= num_folds, shuffle=True, random_state=1001)
 nclasses = len(train_df['target'].unique())
 oof_preds = np.zeros(( train_df.shape[0],nclasses))
sub_preds = np.zeros(( test_df.shape[0],nclasses))
feature_importance_df = pd.DataFrame()
feats = [f for f in train_df.columns if f not in ['target', 'id']]
print('len_feat', len(feats))
feature_importance_df['f']=feats
y = train_df['target']
train_df = train_df[feats]
for n_fold,(train_idx, valid_idx)in enumerate(folds.split(train_df, y)) :
train_x, train_y = train_df.iloc[train_idx], y.iloc[train_idx]
valid_x, valid_y = train_df.iloc[valid_idx], y.iloc[valid_idx]
clf = LGBMClassifier(
 nthread=10,
n_estimators=n_estimators,
silent=-1,
objective='multiclass',
num_leaves=params['num_leaves'],
min_data_in_leaf=params['min_data_in_leaf'],
max_depth=params['max_depth'],
min_child_weight=params['min_child_weight'],
colsample_bytree=params['colsample_bytree'],
reg_alpha=params['reg_alpha'],
reg_lambda=params['reg_lambda'],
subsample=params['subsample'],
learning_rate=params['learning_rate'] 
)
 clf.fit(train_x, train_y, eval_set=[(train_x, train_y),(valid_x, valid_y)],
verbose= 100, early_stopping_rounds= early_stopping_rounds)
 oof_preds[valid_idx] = clf.predict_proba(valid_x, num_iteration=clf.best_iteration_)
 preds = clf.predict_proba(test_df[feats], num_iteration=clf.best_iteration_)
 sub_preds += preds
feature_importance_df["fold"+str(n_fold)] = clf.feature_importances_
del train_x, train_y, valid_x, valid_y
gc.collect()
sub_preds = sub_preds/float(folds.n_splits)
 return feature_importance_df, clf, sub_preds, oof_preds<filter>
for i, pred in enumerate(output):
if pred < coef[0]:
output[i] = 0
elif pred >= coef[0] and pred < coef[1]:
output[i] = 1
elif pred >= coef[1] and pred < coef[2]:
output[i] = 2
elif pred >= coef[2] and pred < coef[3]:
output[i] = 3
else:
output[i] = 4
test_df = df[df['target'].isnull() ]
del df
gc.collect()
test_df.head()<train_model>
submission = pd.DataFrame({'id_code':pd.read_csv('.. /input/aptos2019-blindness-detection/sample_submission.csv' ).id_code.values,
'diagnosis':np.squeeze(output ).astype(int)})
 print(submission.head())
 submission.to_csv('submission.csv', index=False)
 print(os.listdir('./'))