Kaggle competition: Leaf Classification
<SOS> metric: MulticlassLoss Kaggle data source: leaf-classification<merge>
%matplotlib inline
train = train.merge(msno_repeat,on="msno",how="left")
 test = test.merge(msno_repeat,on="msno",how="left" )<drop_column>
rcParams['figure.figsize'] = 10,10
<data_type_conversions>
data = pd.read_csv('.. /input/train.csv')
 parent_data = data.copy()
ID = data.pop('id' )
for col in train.columns:
if train[col].dtype == object:
train[col] = train[col].astype('category')
 test[col] = test[col].astype('category' )<data_type_conversions>
y = data.pop('species')
 y = LabelEncoder().fit(y ).transform(y)
 print(y.shape )
train['song_year'] = train['song_year'].astype('category')
 test['song_year'] = test['song_year'].astype('category' )<drop_column>
X = StandardScaler().fit(data ).transform(data)
 print(X.shape )
drop_list = ['repeat_count','repeat_percentage',
'repeat_percentage_artist',
'repeat_count_msno','repeat_percentage_msno'
]
train = train.drop(drop_list,axis=1)
 test = test.drop(drop_list,axis=1 )<data_type_conversions>
y_cat = to_categorical(y)
 print(y_cat.shape )
test['play_count_msno'] = test['play_count_msno'].fillna(0)
 test['play_count_msno'] = test['play_count_msno'].astype('int')
 train['play_count_artist'] = train['play_count_artist'].fillna(0)
 test['play_count_artist'] = test['play_count_artist'].fillna(0)
 train['play_count_artist'] = train['play_count_artist'].astype('int')
 test['play_count_artist'] = test['play_count_artist'].astype('int')
<split>
model = Sequential()
model.add(Dense(1024,input_dim=192))
model.add(Dropout(0.2))
model.add(Activation('sigmoid'))
model.add(Dense(512))
model.add(Dropout(0.3))
model.add(Activation('sigmoid'))
model.add(Dense(99))
model.add(Activation('softmax'))
kf = KFold(n_splits=3)
 predictions = np.zeros(shape=[len(test)])
 for train_indices ,validate_indices in kf.split(train):
train_data = lgb.Dataset(train.drop(['target'],axis=1 ).loc[train_indices,:],label=train.loc[train_indices,'target'])
 val_data = lgb.Dataset(train.drop(['target'],axis=1 ).loc[validate_indices,:],label=train.loc[validate_indices,'target'])
 params = {
'objective': 'binary',
'boosting': 'gbdt',
'learning_rate': 0.2 ,
'verbose': 0,
'num_leaves': 2**8,
'bagging_fraction': 0.95,
'bagging_freq': 1,
'bagging_seed': 1,
'feature_fraction': 0.9,
'feature_fraction_seed': 1,
'max_bin': 256,
'num_rounds': 80,
'metric' : 'auc'
}
lgbm_model = lgb.train(params, train_data, 100, valid_sets=[val_data])
 predictions += lgbm_model.predict(test.drop(['id'],axis=1))
del lgbm_model
predictions = predictions/3
INPUT_DATA_PATH = '.. /input/'
submission = pd.read_csv(INPUT_DATA_PATH + '/sample_submission.csv')
 submission.target=predictions
submission.to_csv('submission.csv',index=False )<load_from_csv>
model.compile(loss='categorical_crossentropy', optimizer='rmsprop' )
data_dir = '/kaggle/input/aerial-cactus-identification'
labels = pd.read_csv(data_dir+"/train.csv")
 labels['has_cactus'] = labels['has_cactus'].astype('str')
 labels.head()<load_pretrained>
history = model.fit(X, y_cat, batch_size=128, nb_epoch=100, verbose=1 )
train_dir = "/kaggle/temp/train"
test_dir = "/kaggle/temp/test"
with zipfile.ZipFile(data_dir+"/train.zip","r")as z:
z.extractall("/kaggle/temp")
 with zipfile.ZipFile(data_dir+"/test.zip","r")as z:
z.extractall("/kaggle/temp/test_dummy" )<set_options>
test = pd.read_csv('.. /input/test.csv' )
im = cv2.imread(train_dir+"/0004be2cfeaba1c0361d39e2b000257b.jpg")
 plt.imshow(im )<install_modules>
index = test.pop('id' )
!pip install git+https://github.com/qubvel/efficientnet<choose_model_class>
test = StandardScaler().fit_transform(test )
efficient_net = EfficientNetB7(
 weights='imagenet',
input_shape=(32,32,3),
include_top=False,
pooling='max' 
)
 model = Sequential()
model.add(efficient_net)
 model.add(Dense(512, activation='relu'))
model.add(Dense(512, activation='relu'))
model.add(Dense(512, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
model.summary()
model.compile(loss='binary_crossentropy',
optimizer='adam',
metrics=['accuracy'] )<define_variables>
yPred = model.predict_proba(test )
train_datagen = ImageDataGenerator(
 rescale=1/255,
validation_split=0.25,
rotation_range=40,
width_shift_range=0.2,
height_shift_range=0.2,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True,
fill_mode='nearest' 
)
 train_generator = train_datagen.flow_from_dataframe(
 dataframe = labels,
directory = train_dir,
x_col="id",
y_col="has_cactus",
target_size=(32,32),
subset="training",
batch_size=256,
shuffle=True,
class_mode="binary" 
)
 val_generator = train_datagen.flow_from_dataframe(
 dataframe = labels,
directory = train_dir,
x_col="id",
y_col="has_cactus",
target_size=(32,32),
subset="validation",
batch_size=256,
shuffle=True,
class_mode="binary" 
)
 test_datagen = ImageDataGenerator(
 rescale=1./255.
)
 test_generator = test_datagen.flow_from_directory(
 '/kaggle/temp/test_dummy/',
target_size=(32,32),
batch_size=256,
shuffle=False,
class_mode=None
)<choose_model_class>
yPred = pd.DataFrame(yPred,index=index,columns=sorted(parent_data.species.unique()))
yPred.to_csv('predictions.csv' )
early_stopping = EarlyStopping(
 monitor='val_accuracy',
patience=20,
mode='auto',
verbose=2 )<train_model>
yPredTest = yPredTest.set_value(1190, 'Acer_Rubrum', 1 )
history = model.fit_generator(
 train_generator,
epochs = 2000,
steps_per_epoch = 15,
validation_data = val_generator,
validation_steps = 10,
callbacks=[early_stopping]
)<predict_on_test>
yPredTest.to_csv('predictions.csv' )
preds = model.predict_generator(
 test_generator,
steps=len(test_generator.filenames)
)
 preds.shape<prepare_output>
( -np.log10(10**-15)/594 )
image_ids = [name.split('/')[-1] for name in test_generator.filenames]
predictions = preds.flatten()
data = {'id': image_ids, 'has_cactus':predictions}
submission = pd.DataFrame(data)
 print(submission.head() )<save_to_csv>
0.407/(-np.log10(10**-15)/594)