Kaggle competition: Plant Seedlings Classification
<SOS> metric: MeanFScore Kaggle data source: plant-seedlings-classification<import_modules>
ScaleTo = 70
seed = 7
path = '.. /input/plant-seedlings-classification/train/*/*.png'
files = glob(path)
 trainImg = []
trainLabel = []
j = 1
num = len(files)
 for img in files:
trainImg.append(cv2.resize(cv2.imread(img),(ScaleTo, ScaleTo)))
 trainLabel.append(img.split('/')[-2])
 j += 1
trainImg = np.asarray(trainImg)
 trainLabel = pd.DataFrame(trainLabel )
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns<load_from_csv>
clearTrainImg = clearTrainImg / 255
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv')
 data = train.append(test)
<count_unique_values>
le = preprocessing.LabelEncoder()
le.fit(trainLabel[0])
 print("Classes: " + str(le.classes_))
encodeTrainLabels = le.transform(trainLabel[0])
 clearTrainLabel = np_utils.to_categorical(encodeTrainLabels)
 num_clases = clearTrainLabel.shape[1]
print("Number of classes: " + str(num_clases))
trainLabel[0].value_counts().plot(kind='bar' )
train.select_dtypes(int ).nunique()<count_unique_values>
trainX, testX, trainY, testY = train_test_split(clearTrainImg, clearTrainLabel,
test_size=0.1, random_state=seed,
stratify = clearTrainLabel )
test.select_dtypes(int ).nunique() 
<count_missing_values>
datagen = ImageDataGenerator(
 rotation_range=180,
zoom_range = 0.1,
width_shift_range=0.1,
height_shift_range=0.1,
horizontal_flip=True,
vertical_flip=True 
)
 datagen.fit(trainX )
print('Empty values by column in Train ',(train.isnull().sum())) 
<count_missing_values>
numpy.random.seed(seed)
 model = Sequential()
model.add(Conv2D(filters=64, kernel_size=(5, 5), input_shape=(ScaleTo, ScaleTo, 3), activation='relu'))
model.add(BatchNormalization(axis=3))
model.add(Conv2D(filters=64, kernel_size=(5, 5), activation='relu'))
model.add(MaxPooling2D(( 2, 2)))
 model.add(BatchNormalization(axis=3))
model.add(Dropout(0.1))
model.add(Conv2D(filters=128, kernel_size=(5, 5), activation='relu'))
model.add(BatchNormalization(axis=3))
model.add(Conv2D(filters=128, kernel_size=(5, 5), activation='relu'))
model.add(MaxPooling2D(( 2, 2)))
 model.add(BatchNormalization(axis=3))
model.add(Dropout(0.1))
model.add(Conv2D(filters=256, kernel_size=(5, 5), activation='relu'))
model.add(BatchNormalization(axis=3))
model.add(Conv2D(filters=256, kernel_size=(5, 5), activation='relu'))
model.add(MaxPooling2D(( 2, 2)))
 model.add(BatchNormalization(axis=3))
model.add(Dropout(0.1))
model.add(Flatten())
 model.add(Dense(256, activation='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.5))
model.add(Dense(256, activation='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.5))
model.add(Dense(num_clases, activation='softmax'))
model.summary()
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'] )
print('Empty values by column in Test ',(test.isnull().sum())) 
<feature_engineering>
learning_rate_reduction = ReduceLROnPlateau(monitor='val_acc',
patience=3,
verbose=1,
factor=0.4,
min_lr=0.00001)
 filepath="drive/DataScience/PlantReco/weights.best_{epoch:02d}-{val_acc:.2f}.hdf5"
checkpoint = ModelCheckpoint(filepath, monitor='val_acc',
verbose=1, save_best_only=True, mode='max')
 filepath="drive/DataScience/PlantReco/weights.last_auto4.hdf5"
checkpoint_all = ModelCheckpoint(filepath, monitor='val_acc',
verbose=1, save_best_only=False, mode='max')
 callbacks_list = [checkpoint, learning_rate_reduction]
data['Title'] = data['Name']
for name_string in data['Name']:
data['Title'] = data['Name'].str.extract('([A-Za-z]+)\.', expand=True)
 mapping = {'Mlle': 'Miss', 'Major': 'Mr', 'Col': 'Mr', 'Sir': 'Mr', 'Don': 'Mr', 'Mme': 'Miss',
'Jonkheer': 'Mr', 'Lady': 'Mrs', 'Capt': 'Mr', 'Countess': 'Mrs', 'Ms': 'Miss', 'Dona': 'Mrs'}
data.replace({'Title': mapping}, inplace=True)
 titles = ['Dr', 'Master', 'Miss', 'Mr', 'Mrs', 'Rev']
for title in titles:
age_to_impute = data.groupby('Title')['Age'].median() [titles.index(title)]
data.loc[(data['Age'].isnull())&(data['Title'] == title), 'Age'] = age_to_impute
train['Age'] = data['Age'][:891]
test['Age'] = data['Age'][891:]<feature_engineering>
model.load_weights(".. /input/plantrecomodels/weights.best_17-0.96.hdf5")
 data = np.load(".. /input/plantrecomodels/Data.npz")
 d = dict(zip(( "trainX","testX","trainY", "testY"),(data[k] for k in data)))
 trainX = d['trainX']
testX = d['testX']
trainY = d['trainY']
testY = d['testY']
print(model.evaluate(trainX, trainY))
print(model.evaluate(testX, testY))
data['Family_Size'] = data['Parch'] + data['SibSp']
train['Family_Size'] = data['Family_Size'][:891]
test['Family_Size'] = data['Family_Size'][891:]<define_variables>
clearTestImg = clearTestImg / 255
<drop_column><EOS> 
pred = model.predict(clearTestImg)
 predNum = np.argmax(pred, axis=1)
 predStr = le.classes_[predNum]
res = {'file': testId, 'species': predStr}
res = pd.DataFrame(res)
 res.to_csv("res.csv", index=False )