Kaggle competition: Plant Seedlings Classification
testing_set[columns_impute_test[3]].fillna(value="VinylSd",inplace=True )<count_values>
import matplotlib.pyplot as plt
from glob import glob
from keras.utils import np_utils
from keras.models import Sequential
from keras.layers import Dense,Dropout,Flatten
from keras.preprocessing.image import ImageDataGenerator
from keras.layers import BatchNormalization
from sklearn import preprocessing
from sklearn.model_selection import train_test_split
import cv2
import numpy as np
import pandas as pd
from keras import layers
from keras import models
from keras import optimizers
from keras.preprocessing.image import img_to_array, load_img
import keras
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D,GlobalMaxPool2D
from keras.layers.normalization import BatchNormalization
from keras.optimizers import Adam
testing_set[columns_impute_test[4]].value_counts()<filter>
TRAIN_DATA_DIR = '/kaggle/input/plant-seedlings-classification/train/*/*.png'
TEST_DATA_DIR = '/kaggle/input/plant-seedlings-classification/test/*.png'
testing_set[testing_set[columns_impute_test[4]].isnull() ][["MasVnrType","ExterCond","ExterQual"]]<count_values>
data = glob(TRAIN_DATA_DIR)
 training_data=[]
training_label=[]
data_count= len(data)
 print("Reading Data")
 for d in data:
training_data.append(cv2.resize(cv2.imread(d),(240,240)))
 training_label.append(d.split('/')[-2])
 print("Data Read Complete")
 training_data = np.asarray(training_data)
 training_label = pd.DataFrame(training_label )
testing_set[testing_set["ExterQual"]=="Gd"]["MasVnrType"].value_counts()<data_type_conversions>
LabelEncode = preprocessing.LabelEncoder()
LabelEncode.fit(training_label[0])
 new_label = LabelEncode.transform(training_label[0])
 clearalllabels = np_utils.to_categorical(new_label)
 print(training_data.shape, clearalllabels.shape )
testing_set["MasVnrType"].fillna(value="None",inplace=True )<count_values>
x_train,x_test,y_train,y_test = train_test_split(training_data,clearalllabels,test_size=0.2,random_state=1,stratify=clearalllabels)
 print(x_train.shape,x_test.shape,y_train.shape,y_test.shape )
testing_set[columns_impute_test[5]].value_counts()<filter>
x_train = x_train.astype('float32')/ 255
x_test = x_test.astype('float32')/ 255
testing_set[testing_set[columns_impute_test[5]].isnull() ]<data_type_conversions>
data_generator = ImageDataGenerator(rotation_range=20,
zoom_range=0.2,
width_shift_range=0.2,
height_shift_range=0.2,
horizontal_flip=True,
vertical_flip=True,
)
testing_set[columns_impute_test[5]].fillna(value=0.0,inplace=True )<count_values>
data_generator.fit(x_train )
testing_set[columns_impute_test[6]].value_counts()<data_type_conversions>
import tensorflow as tf
testing_set.loc[(testing_set[columns_impute_test[6]].isnull())&(testing_set["GarageQual"] == "Ta"),"BsmtQual"] = "Gd"
testing_set[columns_impute_test[6]].fillna(value="TA",inplace=True )<count_values>
tf.keras.backend.clear_session()
model = Sequential()
model.add(Conv2D(filters=64, kernel_size=(3, 3), input_shape=(240, 240, 3), activation='relu'))
model.add(BatchNormalization(axis=3))
model.add(Conv2D(filters=64, kernel_size=(3, 3), activation='relu'))
model.add(MaxPooling2D(( 2, 2)))
 model.add(BatchNormalization(axis=3))
model.add(Dropout(0.1))
model.add(Conv2D(filters=32, kernel_size=(3, 3), activation='relu'))
model.add(BatchNormalization(axis=3))
model.add(Conv2D(filters=32, kernel_size=(3, 3), activation='relu'))
model.add(MaxPooling2D(( 2, 2)))
 model.add(BatchNormalization(axis=3))
model.add(Dropout(0.1))
model.add(GlobalMaxPool2D())
 model.add(Flatten())
 model.add(Dense(100, activation='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.5))
model.add(Dense(50, activation='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.5))
model.add(Dense(12, activation='softmax'))
testing_set[columns_impute_test[7]].value_counts()<data_type_conversions>
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'] )
testing_set[columns_impute_test[7]].fillna(value="TA",inplace=True )<count_values>
model_history=model.fit_generator(data_generator.flow(x_train,y_train,batch_size=16),epochs=200,
validation_data=(x_test,y_test),callbacks=model_checkpoint )
testing_set[columns_impute_test[8]].value_counts()<count_values>
score, acc = model.evaluate(x_test,y_test)
 score1, acc1 = model.evaluate(x_train,y_train)
 print('Test score:', score,' Test accuracy:', acc)
 print('Train score:', score1,' Train accuracy:',acc1)
testing_set[testing_set["GarageQual"]=="TA"]["BsmtExposure"].value_counts()<correct_missing_values>
path_to_test = TEST_DATA_DIR
pics = glob(path_to_test)
 testimages = []
tests = []
num = len(pics)
 print(pics)
 for i in pics:
tests.append(i.split('/')[-1])
 testimages.append(cv2.resize(cv2.imread(i),(240,240)))
 testimages= np.asarray(testimages )
testing_set[columns_impute_test[8]].fillna(value="No",inplace=True )<count_values>
testimages = testimages.astype('float32')/ 255
testing_set[columns_impute_test[9]].value_counts()<count_values>
prediction = model.predict(testimages)
 pred = np.argmax(prediction,axis=1)
 predStr = LabelEncode.classes_[pred]
submission = {'file':tests,'species':predStr}
submission = pd.DataFrame(submission)
 submission.to_csv("Prediction.csv",index=False )
<feature_engineering><EOS> 
model_json = model.to_json()
with open("plant_seedling.json", "w")as json_file:
json_file.write(model_json)
 model.save_weights("plant-seedling.h5")
 print("Saved model to disk" )