Kaggle competition: Cassava Leaf Disease Classification
<SOS> metric: CategorizationAccuracy Kaggle data source: cassava-leaf-disease-classification<feature_engineering>
print("Tensorflow version " + tf.__version__)
def prepare(df):
df['_budget_runtime_ratio'] = df['budget']/df['runtime']
df['_budget_popularity_ratio'] = df['budget']/df['popularity']
df['_budget_year_ratio'] = df['budget']/(df['release_year']*df['release_year'])
 df['_releaseYear_popularity_ratio'] = df['release_year']/df['popularity']
df['_releaseYear_popularity_ratio2'] = df['popularity']/df['release_year']
df['_year_to_log_budget'] = df['release_year'] / df['log_budget']
df['_year_to_log_popularity'] = df['release_year'] / df['log_popularity']
df['has_homepage'] = 0
df.loc[pd.isnull(df['homepage']),"has_homepage"] = 1
df['isTaglineNA'] = 0
df.loc[df['tagline'] == 0 ,"isTaglineNA"] = 1
df['isTitleDifferent'] = 1
df.loc[ df['original_title'] == df['title'] ,"isTitleDifferent"] = 0
df['isMovieReleased'] = 1
df.loc[ df['status'] != "Released" ,"isMovieReleased"] = 0
df['original_title_letter_count'] = df['original_title'].str.len()
df['original_title_word_count'] = df['original_title'].str.split().str.len()
df['title_word_count'] = df['title'].str.split().str.len()
df['overview_word_count'] = df['overview'].str.split().str.len()
df['tagline_word_count'] = df['tagline'].str.split().str.len()
df['meanruntimeByYear'] = df.groupby("release_year")["runtime"].aggregate('mean')
 df['meanPopularityByYear'] = df.groupby("release_year")["popularity"].aggregate('mean')
 df['meanBudgetByYear'] = df.groupby("release_year")["budget"].aggregate('mean')
 return df
train_new=prepare(train)
 test_new=prepare(test)
<save_to_csv>
dense201 = tf.keras.models.load_model('.. /input/trained-models/densenet201_40.h5')
 inception = tf.keras.models.load_model('.. /input/trained-models/inceptionv3_40.h5')
 efficient_net = tf.keras.models.load_model(
 '.. /input/trained-models/efficient_netb3_40.h5',
compile=False,
custom_objects={'FixedDropout':FixedDropout}
)
train_new.to_csv("train_new.csv", index=False)
 test_new.to_csv("test_new.csv", index=False )<drop_column>
JPEG_PATH = ".. /input/cassava-leaf-disease-classification/test_images"
def load_image(jpeg_path, image_id):
img = cv2.imread(os.path.join(jpeg_path, image_id)) /255.0
img = cv2.resize(img,(512, 512)) [:, :, ::-1]
return img
def generator(filepath, paths, batch_size=32):
i=0
print(len(paths))
while i <= len(paths):
batch = []
for cpt in range(batch_size):
if i + cpt >= len(paths):
i += batch_size
break
batch.append(load_image(filepath, paths[i+cpt]))
i += batch_size
yield np.stack(batch )
drop_columns=['homepage','imdb_id','poster_path','status','title', 'release_date','tagline', 'overview', 'original_title','all_genres','all_cast',
'original_language','collection_name','all_crew']
train_new=train_new.drop(drop_columns,axis=1)
 test_new=test_new.drop(drop_columns,axis=1 )<split>
submission = pd.read_csv('.. /input/cassava-leaf-disease-classification/sample_submission.csv' )
X = train_new.drop(['id', 'revenue'], axis=1)
 y = np.log1p(train_new['revenue'])
 X_test = test_new.drop(['id'], axis=1)
 X_train, X_valid, y_train, y_valid = train_test_split(X, y, test_size=0.2 )<choose_model_class>
def vote_in_ensemble(v1, v2, v3):
if v1 == v2:
return v1
if v2 == v3:
return v2
if v1 == v3:
return v3
return v1
params = {'num_leaves': 30,
'min_data_in_leaf': 20,
'objective': 'regression',
'max_depth': 5,
'learning_rate': 0.01,
"boosting": "gbdt",
"feature_fraction": 0.9,
"bagging_freq": 1,
"bagging_fraction": 0.9,
"bagging_seed": 11,
"metric": 'rmse',
"lambda_l1": 0.2,
"verbosity": -1}
lgb_model = lgb.LGBMRegressor(**params, n_estimators = 10000, nthread = 4, n_jobs = -1)
 lgb_model.fit(X_train, y_train,
eval_set=[(X_train, y_train),(X_valid, y_valid)], eval_metric='rmse',
verbose=1000, early_stopping_rounds=200)
 eli5.show_weights(lgb_model, feature_filter=lambda x: x != '<BIAS>' )<train_on_grid>
def predict_for_pretrained(model):
ds_test = generator(JPEG_PATH,np.sort(submission.image_id.values))
preds = np.argmax(model.predict(ds_test, verbose=True), axis=-1)
 return preds
dense_preds = predict_for_pretrained(dense201)
 inception_preds = predict_for_pretrained(inception)
 efficient_net_preds = predict_for_pretrained(efficient_net)
 result = []
for idx, val in enumerate(dense_preds):
result.append(
 vote_in_ensemble(
 dense_preds[idx],
inception_preds[idx],
efficient_net_preds[idx] 
)
 )
<init_hyperparams><EOS> 
submission["label"] = result
submission.to_csv("submission.csv", index=False )