Kaggle competition: Plant Seedlings Classification
<SOS> metric: MeanFScore Kaggle data source: plant-seedlings-classification<count_values>
dim = 160
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),(dim, dim)))
 trainLabel.append(img.split('/')[-2])
 j += 1
trainImg = np.asarray(trainImg)
 trainLabel = pd.DataFrame(trainLabel )
testing_set[columns_impute_test[10]].value_counts()<filter>
clearTrainImg = preProcessImage(trainImg )
testing_set[testing_set[columns_impute_test[10]].isnull() ]<count_values>
x_train = clearTrainImg / 255.0
testing_set[testing_set["SaleCondition"]=="Abnorml"][columns_impute_test[10]].value_counts().head()<data_type_conversions>
enc = OneHotEncoder(categories='auto')
 y_train = enc.fit_transform(trainLabel ).toarray()
testing_set[columns_impute_test[10]].fillna(value=0.0,inplace=True )<count_values>
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]
testing_set[columns_impute_test[11]].value_counts()<count_values>
datagen = ImageDataGenerator(rotation_range=20,
zoom_range=0.15,
width_shift_range=0.2,
height_shift_range=0.2,
shear_range=0.15,
horizontal_flip=True,
vertical_flip=True,
brightness_range=[0.4,1],
rescale=1.0/255.0)
 datagen.fit(x_train )
testing_set[testing_set["SaleCondition"]=="AdjLand"][columns_impute_test[11]].value_counts()<data_type_conversions>
num_classes = 12
learning_rate = 0.001
batch_size = 32
base_model = Xception(input_shape=(dim, dim, 3), include_top=False,weights='imagenet')
 base_model.trainable = False
model = Sequential([
base_model,
GlobalAveragePooling2D() ,
Dense(100, activation="relu"),
BatchNormalization(trainable = True,axis=1),
Dropout(0.5),
Dense(50, activation="relu"),
BatchNormalization(trainable = True,axis=1),
Dense(num_classes,activation='softmax')
 ])
 model.compile(optimizer = optimizers.Nadam(learning_rate=learning_rate),
loss = 'categorical_crossentropy',
metrics=['accuracy'])
 result = model.fit(datagen.flow(x_train, y_train, batch_size=batch_size), verbose = 1,
batch_size=batch_size, epochs=25, validation_data=(x_valid, y_valid)) 
(loss, accuracy)= model.evaluate(x_valid, y_valid)
 print("[INFO] loss={:.4f}, accuracy: {:.4f}%".format(loss,accuracy * 100))
testing_set[columns_impute_test[11]].fillna(value="Unf",inplace=True)
<data_type_conversions>
base_model.trainable = True
model.get_layer('xception' ).trainable
testing_set[columns_impute_test[12]].fillna(value=0.0,inplace=True )<count_values>
model.compile(optimizer=optimizers.Nadam(learning_rate=0.0006), loss='categorical_crossentropy', metrics=['accuracy'] )
testing_set[columns_impute_test[13]].value_counts()<filter>
result = model.fit(datagen.flow(x_train, y_train, batch_size=batch_size), epochs=50,
initial_epoch=25, validation_data=(x_valid, y_valid),verbose=1 )
testing_set[testing_set[columns_impute_test[13]].isnull() ][["BsmtFinType1","GarageCond","GarageQual","BldgType"]]<data_type_conversions>
gc.collect()
testing_set[columns_impute_test[13]].fillna(value=0.0,inplace=True )<count_values>
model.compile(optimizer=optimizers.Nadam(learning_rate=0.00006), loss='categorical_crossentropy', metrics=['accuracy'] )
testing_set[columns_impute_test[14]].value_counts()<filter>
result = model.fit(datagen.flow(x_train, y_train, batch_size=batch_size), epochs=75,
initial_epoch=50, validation_data=(x_valid, y_valid),verbose=1 )
testing_set[testing_set[columns_impute_test[14]].isnull() ][["BsmtFinType1","GarageCond","GarageQual","BldgType"]]<data_type_conversions>
gc.collect()
testing_set[columns_impute_test[14]].fillna(value=0.0,inplace=True )<count_values>
path = '.. /input/plant-seedlings-classification/test/*.png'
files = glob(path)
 testImg = []
testId = []
j = 1
num = len(files)
 for img in files:
print("Obtain images: " + str(j)+ "/" + str(num), end='\r')
 testId.append(img.split('/')[-1])
 testImg.append(cv2.resize(cv2.imread(img),(dim, dim)))
 j += 1
testImg = np.asarray(testImg )
testing_set[columns_impute_test[15]].value_counts()<filter>
clearTestImg = preProcessImage(testImg,getEx=True )
testing_set[testing_set[columns_impute_test[15]].isnull() ][["BsmtFinType1","GarageCond","GarageQual","BldgType"]]<data_type_conversions>
clearTestImg = clearTestImg / 255
testing_set[columns_impute_test[15]].fillna(value=0.0,inplace=True )<count_values>
pred = model.predict(clearTestImg )
testing_set[columns_impute_test[16]].value_counts()<data_type_conversions>
predNum = np.argmax(pred, axis=1)
 predStr = []
for i in range(len(predNum)) :
predStr.append(enc.categories_[0][predNum[i]])
 res = {'file': testId, 'species': predStr}
res = pd.DataFrame(res)
 res.to_csv("submission.csv", index=False )
testing_set[columns_impute_test[16]].fillna(value=0.0,inplace=True )<count_values>
model.save("saved_model" )