Kaggle competition: Plant Seedlings Classification
<SOS> metric: MeanFScore Kaggle data source: plant-seedlings-classification<data_type_conversions>
print(check_output(["ls", ".. /input"] ).decode("utf8"))
for dataset in data_cleaner:
dataset['Age'].fillna(dataset['Age'].median() , inplace = True)
 dataset['Embarked'].fillna(dataset['Embarked'].mode() [0], inplace = True)
 dataset['Fare'].fillna(dataset['Fare'].median() , inplace = True)
 dataset.drop('Cabin', axis=1, inplace=True )<count_missing_values>
BATCH_SIZE = 10
EPOCHS = 1000
RANDOM_STATE = 123
CLASS = {
'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
}
INV_CLASS = {
0: 'Black-grass',
1: 'Charlock',
2: 'Cleavers',
3: 'Common Chickweed',
4: 'Common wheat',
5: 'Fat Hen',
6: 'Loose Silky-bent',
7: 'Maize',
8: 'Scentless Mayweed',
9: 'Shepherds Purse',
10: 'Small-flowered Cranesbill',
11: 'Sugar beet'
}
print(data1.isnull().sum())
 print("-"*10)
 print(data_val.isnull().sum() )<categorify>
def dense_set(inp_layer, n, activation, drop_rate=0.) :
dp = Dropout(drop_rate )(inp_layer)
 dns = Dense(n )(dp)
 bn = BatchNormalization(axis=-1 )(dns)
 act = Activation(activation=activation )(bn)
 return act
varlist = ['Sex']
def binary_map(x):
return x.map({'male': 1, "female": 0})
 for dataset in data_cleaner:
dataset[varlist] = dataset[varlist].apply(binary_map )<categorify>
def conv_layer(feature_batch, feature_map, kernel_size=(3, 3),strides=(1,1), zp_flag=False):
if zp_flag:
zp = ZeroPadding2D(( 1,1))(feature_batch)
 else:
zp = feature_batch
conv = Conv2D(filters=feature_map, kernel_size=kernel_size, strides=strides )(zp)
 bn = BatchNormalization(axis=3 )(conv)
 act = LeakyReLU(1/10 )(bn)
 return act
dummy1 = pd.get_dummies(data1['Embarked'], prefix='Embarked', drop_first=True)
 data1 = pd.concat([data1, dummy1], axis=1 )<categorify>
def get_model() :
inp_img = Input(shape=(51, 51, 3))
conv1 = conv_layer(inp_img, 64, zp_flag=False)
 conv2 = conv_layer(conv1, 64, zp_flag=False)
 mp1 = MaxPooling2D(pool_size=(3, 3), strides=(2, 2))(conv2)
 conv3 = conv_layer(mp1, 128, zp_flag=False)
 conv4 = conv_layer(conv3, 128, zp_flag=False)
 mp2 = MaxPooling2D(pool_size=(3, 3), strides=(2, 2))(conv4)
 conv7 = conv_layer(mp2, 256, zp_flag=False)
 conv8 = conv_layer(conv7, 256, zp_flag=False)
 conv9 = conv_layer(conv8, 256, zp_flag=False)
 mp3 = MaxPooling2D(pool_size=(3, 3), strides=(2, 2))(conv9)
 flt = Flatten()(mp3)
 ds1 = dense_set(flt, 128, activation='tanh')
 out = dense_set(ds1, 12, activation='softmax')
 model = Model(inputs=inp_img, outputs=out)
 mypotim = SGD(lr=1 * 1e-1, momentum=0.9, nesterov=True)
 model.compile(loss='categorical_crossentropy',
optimizer=mypotim,
metrics=['accuracy'])
 model.summary()
return model
dummy1 = pd.get_dummies(data_val['Embarked'], prefix='Embarked', drop_first=True)
 data_val = pd.concat([data_val, dummy1], axis=1 )<categorify>
def train_model(img, target):
callbacks = get_callbacks(filepath='.. /input/model-weight/model_weight_SGD.hdf5', patience=6)
 gmodel = get_model()
gmodel.load_weights(filepath='.. /input/model-weight/model_weight_Adam.hdf5')
 x_train, x_valid, y_train, y_valid = train_test_split(
 img,
target,
shuffle=True,
train_size=0.8,
random_state=RANDOM_STATE 
)
 gen = ImageDataGenerator(
 rotation_range=360.,
width_shift_range=0.3,
height_shift_range=0.3,
zoom_range=0.6,
horizontal_flip=True,
vertical_flip=True 
)
 gmodel.fit_generator(gen.flow(x_train, y_train,batch_size=BATCH_SIZE),
steps_per_epoch=10*len(x_train)/BATCH_SIZE,
epochs=EPOCHS,
verbose=1,
shuffle=True,
validation_data=(x_valid, y_valid),
callbacks=callbacks )
dummy1 = pd.get_dummies(data1['Pclass'], prefix='Pclass', drop_first=True)
 data1 = pd.concat([data1, dummy1], axis=1 )<categorify>
def test_model(img, label):
gmodel = get_model()
gmodel.load_weights(filepath='.. /input/model-weight/model_weight_SGD.hdf5')
 prob = gmodel.predict(img, verbose=1)
 pred = prob.argmax(axis=-1)
 sub = pd.DataFrame({"file": label,
"species": [INV_CLASS[p] for p in pred]})
 sub.to_csv("sub.csv", index=False, header=True )
dummy1 = pd.get_dummies(data_val['Pclass'], prefix='Pclass', drop_first=True)
 data_val = pd.concat([data_val, dummy1], axis=1 )<categorify>
def img_reshape(img):
img = imresize(img,(51, 51, 3))
return img
dummy1 = pd.get_dummies(data1['Sex'], prefix='Male', drop_first=True)
 data1 = pd.concat([data1, dummy1], axis=1 )<categorify>
def img_label(path):
return str(str(path.split('/')[-1]))
def img_class(path):
return str(path.split('/')[-2] )
dummy1 = pd.get_dummies(data_val['Sex'], prefix='Male', drop_first=True)
 data_val = pd.concat([data_val, dummy1], axis=1 )<feature_engineering>
def fill_dict(paths, some_dict):
text = ''
if 'train' in paths[0]:
text = 'Start fill train_dict'
elif 'test' in paths[0]:
text = 'Start fill test_dict'
for p in tqdm(paths, ascii=True, ncols=85, desc=text):
img = imageio.imread(p)
 img = img_reshape(img)
 some_dict['image'].append(img)
 some_dict['label'].append(img_label(p))
if 'train' in paths[0]:
some_dict['class'].append(img_class(p))
return some_dict
<feature_engineering><EOS> 
def main() :
train_dict, test_dict = reader()
X_test = np.array(test_dict['image'])
 label = test_dict['label']
test_model(X_test, label )