Kaggle competition: Plant Seedlings Classification
<SOS> metric: MeanFScore Kaggle data source: plant-seedlings-classification<rename_columns>
print(check_output(["ls", ".. /input"] ).decode("utf8"))
BATCH_SIZE = 16
EPOCHS = 100
RANDOM_STATE = 11
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'
}
data1= data1.rename(columns={ 'Male_1' : 'Male'})
 data_val= data_val.rename(columns={ 'Male_1' : 'Male'} )<drop_column>
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
drop_column = ['PassengerId', 'Pclass', 'Name', 'Sex', 'Ticket', 'Fare', 'Embarked']
data1.drop(drop_column, axis=1, inplace = True )<import_modules>
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
from sklearn.model_selection import train_test_split<import_modules>
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 = Adam(lr=0.5 * 1e-3, beta_1=0.9, beta_2=0.999, epsilon=1e-08)
 model.compile(loss='categorical_crossentropy',
optimizer=mypotim,
metrics=['accuracy'])
 model.summary()
return model
from sklearn.model_selection import train_test_split<drop_column>
def train_model(img, target):
callbacks = get_callbacks(filepath='.. /input/plant-weight/model_weight_SGD.hdf5', patience=5)
 gmodel = get_model()
gmodel.load_weights(filepath='.. /input/plant-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)
X = data1.drop(['Survived'], axis=1)
 X.head()<prepare_x_and_y>
def test_model(img, label):
gmodel = get_model()
gmodel.load_weights(filepath='.. /input/plant-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 )
y = data1['Survived']
y.head()<split>
def img_reshape(img):
img = imresize(img,(51, 51, 3))
return img
X_train, X_test, y_train, y_test = train_test_split(X, y, train_size=0.7, test_size=0.3, random_state=100 )<feature_engineering>
def img_label(path):
return str(str(path.split('/')[-1]))
def img_class(path):
return str(path.split('/')[-2] )
survived =(sum(data1['Survived'])/len(data1['Survived'].index)) *100
survived<choose_model_class>
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
<train_model><EOS> 
def main() :
train_dict, test_dict = reader()
X_train = np.array(train_dict['image'])
 y_train = to_categorical(np.array([CLASS[l] for l in train_dict['class']]))
X_test = np.array(test_dict['image'])
 label = test_dict['label']
train_model(X_train, y_train)
 test_model(X_test, label)
 if __name__=='__main__':
main()