Kaggle competition: Plant Seedlings Classification
<SOS> metric: MeanFScore Kaggle data source: plant-seedlings-classification<create_dataframe>
x_train = []
x_test = []
y_train = []
df_test = pd.read_csv('.. /input/plant-seedlings-classification/sample_submission.csv')
 label_map = { "Black-grass" :0,
"Charlock" :1,
"Cleavers" :2,
"Common Chickweed" :3,
"Common wheat" :4,
"Fat Hen" :5,
"Loose Silky-bent" :6,
"Maize" :7,
"Scentless Mayweed" :8,
"Shepherds Purse" :9,
"Small-flowered Cranesbill" :10,
"Sugar beet" :11}
num_pipeline = Pipeline([ 
('select_numeric', DataFrameSelector(['Age','SibSp','Parch','Fare'])) , 
('imputer', SimpleImputer(strategy='median'))
] )<feature_engineering>
dim = 256
dirs = os.listdir(".. /input/plant-seedlings-classification/train/")
 for k in tqdm(range(len(dirs))):
files = os.listdir(".. /input/plant-seedlings-classification/train/{}".format(dirs[k]))
for f in range(len(files)) :
img = cv2.imread('.. /input/plant-seedlings-classification/train/{}/{}'.format(dirs[k], files[f]))
targets = np.zeros(12)
 targets[label_map[dirs[k]]] = 1
x_train.append(cv2.resize(img,(dim, dim)))
 y_train.append(targets)
 y_train = np.array(y_train, np.uint8)
 x_train = np.array(x_train, np.float32)
 print(x_train.shape)
 print(y_train.shape)
num_pipeline.fit_transform(train_data )<train_model>
sss = StratifiedShuffleSplit(n_splits=1, test_size=0.16, random_state=42)
 for train_index, test_index in sss.split(x_train, y_train):
print("Using {} for training and {} for validation".format(len(train_index), len(test_index)))
 x_train, x_valid = x_train[train_index], x_train[test_index]
y_train, y_valid = y_train[train_index], y_train[test_index]
class MostFrequentImputer(BaseEstimator, TransformerMixin):
def fit(self, X, y=None):
self.most_frequent_ = pd.Series([X[c].value_counts().index[0] for c in X], index=X.columns)
 return self
def transform(self, X, y=None):
return X.fillna(self.most_frequent_ )<import_modules>
datagen = ImageDataGenerator(horizontal_flip=True,
vertical_flip=True)
 weights = os.path.join('', 'weights.h5')
 epochs = 100
learning_rate = 0.0001
batch_size = 32
callbacks = [ EarlyStopping(monitor='val_loss', patience=5, verbose=0),
ModelCheckpoint(weights, monitor='val_loss', save_best_only=True, verbose=0),
ReduceLROnPlateau(monitor='val_loss', factor=0.1, patience=2, verbose=0, mode='auto', epsilon=0.0001, cooldown=0, min_lr=0)]
from sklearn.preprocessing import OneHotEncoder<categorify>
base_model = Xception(input_shape=(dim, dim, 3), include_top=False, weights='imagenet', pooling='avg')
 x = base_model.output
x = Dense(256, activation='relu' )(x)
 x = Dropout(0.5 )(x)
 predictions = Dense(12, activation='softmax' )(x)
 model = Model(inputs=base_model.input, outputs=predictions)
 print(model.summary())
cat_pipeline = Pipeline([ 
('select_cat', DataFrameSelector(['Pclass','Sex','Embarked'])) , 
('imputer', MostFrequentImputer()), 
('cat_encoder', OneHotEncoder(sparse=False))
] )<feature_engineering>
model.compile(loss='categorical_crossentropy', optimizer=optimizers.Adam(lr=learning_rate), metrics=['accuracy'])
cat_pipeline.fit_transform(train_data )<categorify>
model.fit_generator(datagen.flow(x_train, y_train, batch_size=batch_size),
steps_per_epoch=len(x_train)/batch_size,
validation_data=datagen.flow(x_valid, y_valid, batch_size=batch_size),
validation_steps=len(x_valid)/batch_size,
callbacks=callbacks,
epochs=10,
verbose=1 )
preprocess_pipeline = FeatureUnion(transformer_list=[ 
('num_pipeline', num_pipeline), 
('cat_pipeline', cat_pipeline),
] )<prepare_x_and_y>
for f, species in tqdm(df_test.values, miniters=100):
img = cv2.imread('.. /input/plant-seedlings-classification/test/{}'.format(f))
x_test.append(cv2.resize(img,(dim, dim)))
 x_test = np.array(x_test, np.float32)
 print(x_test.shape )
y_train = train_data['Survived']<train_model>
if os.path.isfile(weights):
model.load_weights(weights )
svm_clf = SVC(gamma='auto')
 svm_clf.fit(X_train, y_train )<compute_train_metric>
p_test = model.predict(x_test, verbose=1)
 preds = []
for i in range(len(p_test)) :
pos = np.argmax(p_test[i])
 preds.append(list(label_map.keys())[list(label_map.values() ).index(pos)])
<compute_train_metric><EOS> 
df_test['species'] = preds
df_test.to_csv('submission.csv', index=False )