Kaggle competition: Plant Seedlings Classification
<SOS> metric: MeanFScore Kaggle data source: plant-seedlings-classification<filter>
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:
print(str(j)+ "/" + str(num), end="\r")
 trainImg.append(cv2.resize(cv2.imread(img),(ScaleTo, ScaleTo)))
 trainLabel.append(img.split('/')[-2])
 j += 1
trainImg = np.asarray(trainImg)
 trainLabel = pd.DataFrame(trainLabel )
testing_set[testing_set[columns_impute_test[0]].isnull() ][["MSSubClass","MSZoning"]]<feature_engineering>
clearTrainImg = clearTrainImg / 255
testing_set.loc[(testing_set[columns_impute_test[0]].isnull())&(testing_set["MSSubClass"] == 20),"MSZoning"] = "RL"
testing_set.loc[(testing_set[columns_impute_test[0]].isnull())&(testing_set["MSSubClass"] == 30),"MSZoning"] = "RM"
testing_set.loc[(testing_set[columns_impute_test[0]].isnull())&(testing_set["MSSubClass"] == 70),"MSZoning"] = "RM" 
<count_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' )
testing_set[columns_impute_test[1]].value_counts()<filter>
trainX, testX, trainY, testY = train_test_split(clearTrainImg, clearTrainLabel,
test_size=0.1, random_state=seed,
stratify = clearTrainLabel )
testing_set[testing_set[columns_impute_test[1]].isnull() ]<data_type_conversions>
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 )
testing_set[columns_impute_test[1]].fillna(value="AllPub",inplace=True )<count_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'] )
testing_set[columns_impute_test[2]].value_counts()<filter>
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, checkpoint_all]
testing_set[testing_set[columns_impute_test[2]].isnull() ]<data_type_conversions>
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))
testing_set[columns_impute_test[2]].fillna(value="VinylSd",inplace=True )<count_values>
clearTestImg = clearTestImg / 255
testing_set[columns_impute_test[3]].value_counts()<filter>
pred = model.predict(clearTestImg )
testing_set[testing_set[columns_impute_test[3]].isnull() ]<data_type_conversions>
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 )