Kaggle competition: Aerial Cactus Identification
<SOS> metric: AUC Kaggle data source: aerial-cactus-identification<load_from_csv>
%matplotlib inline
random.seed(0)
train_dir = pd.read_csv('/kaggle/input/titanic/train.csv')
 test_dir = pd.read_csv('/kaggle/input/titanic/test.csv')
 print("Training data: {}".format(train_dir.shape))
print("Testing data: {}".format(test_dir.shape))<count_missing_values>
print("python", sys.version)
 for module in np, pd, tf, keras:
print(module.__name__, module.__version__ )
train_dir.isnull().any()<count_missing_values>
assert sys.version_info >=(3, 5)
 assert tf.__version__ >= "2.0"
train_dir.isnull().sum()<count_missing_values>
train_dir = ".. /input/cactus-dataset/cactus/train.csv"
train_data = pd.read_csv(train_dir)
 train_data.has_cactus = train_data.has_cactus.astype(str )
train_dir.isnull().sum()<drop_column>
train_data.has_cactus.value_counts()
updated_train_dir = train_dir.drop(['Ticket', 'PassengerId', 'Name','Cabin'], axis = 1)
 updated_train_dir.head(3 )<drop_column>
img = mpimg.imread(".. /input/cactus-dataset/cactus/train/000c8a36845c0208e833c79c1bffedd1.jpg")
 plt.axis("off")
 imgplot = mlp.imshow(img )
updated_test_dir = test_dir.drop(['Ticket', 'PassengerId', 'Name','Cabin'], axis = 1)
 updated_test_dir.head(3 )<count_missing_values>
def generator(train_data, directory, batch_size, target_size, class_mode):
x_col = 'id'
y_col = 'has_cactus'
train_datagen = ImageDataGenerator(
 rescale = 1./255,
horizontal_flip = True,
vertical_flip = True,
validation_split = 0.2)
 train_generator = train_datagen.flow_from_dataframe(
 train_data,
directory = directory,
x_col = x_col,
y_col = y_col,
target_size = target_size,
class_mode = class_mode,
batch_size = batch_size,
shuffle = True,
subset = 'training')
 valid_generator = train_datagen.flow_from_dataframe(
 train_data,
directory = directory,
x_col = x_col,
y_col = y_col,
target_size = target_size,
class_mode = class_mode,
batch_size = batch_size,
shuffle = True,
subset = 'validation')
 return train_generator, valid_generator
Missing_age = 100 * updated_train_dir['Age'].isnull().sum() / updated_train_dir['Age'].shape[0]
print("Age missing value: {}".format(Missing_age))<count_missing_values>
directory = ".. /input/cactus-dataset/cactus/train"
batch_size = 64
target_size =(32,32)
 class_mode = 'binary'
train_generator, valid_generator = generator(train_data, directory, batch_size, target_size, class_mode )
updated_train_dir['Age'].isnull().any()<count_missing_values>
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, Dense, Flatten, Dropout, Activation
from tensorflow.keras.layers import BatchNormalization, MaxPooling2D, GlobalAveragePooling2D
updated_test_dir['Age'].fillna(updated_test_dir['Age'].median() , inplace=True)
 updated_test_dir['Age'].isnull().any()<count_missing_values>
model = Sequential([
Conv2D(32,(3, 3), padding = 'same', activation = 'relu', input_shape =(32,32,3)) ,
BatchNormalization() ,
Conv2D(32,(3, 3), padding = 'same', activation = 'relu', input_shape =(32,32,3)) ,
BatchNormalization() ,
MaxPooling2D() ,
Conv2D(64,(3, 3), padding = 'same', activation = 'relu'),
BatchNormalization() ,
MaxPooling2D() ,
Conv2D(128,(3, 3), padding = 'same', activation = 'relu'),
BatchNormalization() ,
MaxPooling2D() ,
Conv2D(256,(3, 3), padding = 'same', activation = 'relu'),
BatchNormalization() ,
MaxPooling2D() ,
GlobalAveragePooling2D() ,
Dense(256, activation = 'relu'),
Dropout(0.5),
Dense(1, activation = 'sigmoid')
 ])
 model.compile(loss = 'binary_crossentropy',
optimizer = 'adam',
metrics = ['accuracy'])
 model.summary()
updated_train_dir.isnull().any()<count_values>
updated_train_dir['Embarked'].value_counts()<count_missing_values>
class_weights = class_weight.compute_class_weight('balanced', np.unique(train_generator.classes), train_generator.classes)
 callbacks = [EarlyStopping(monitor = 'val_loss', patience = 20),
ReduceLROnPlateau(patience = 10, verbose = 1),
ModelCheckpoint(filepath = 'best_model.h5', monitor = 'val_loss', verbose = 0, save_best_only = True)]
history = model.fit_generator(train_generator,
validation_data = valid_generator,
epochs = 100,
verbose = 1,
shuffle = True,
callbacks = callbacks,
class_weight = class_weights )
Missing_embarked = 100 * updated_train_dir['Embarked'].isnull().sum() / updated_train_dir['Embarked'].shape[0]
print("Missing embakred info: {}".format(Missing_embarked))<count_missing_values>
model.load_weights("best_model.h5" )
updated_train_dir['Embarked'].fillna('S', inplace=True)
 updated_train_dir.isnull().any()<count_values>
def test_gen(test_dir, target_size, batch_size, class_mode):
test_datagen = ImageDataGenerator(
 rescale = 1./255)
 test_generator = test_datagen.flow_from_directory(
 directory = test_dir,
target_size = target_size,
batch_size = batch_size,
class_mode = class_mode,
shuffle = False)
 return test_generator
updated_test_dir['Embarked'].value_counts() 
<count_missing_values>
test_dir = ".. /input//cactus-dataset/cactus/test/"
target_size =(32,32)
 batch_size = 1
class_mode = None
test_generator = test_gen(test_dir, target_size, batch_size, class_mode )
<define_variables><EOS> 
def submission() :
sample_submission = pd.read_csv(".. /input/cactus-dataset/cactus/sample_submission.csv")
 filenames = [path.split('/')[-1] for path in test_generator.filenames]
proba = list(model.predict_generator(test_generator)[:,0])
 sample_submission.id = filenames
sample_submission.has_cactus = proba
sample_submission.to_csv('submission.csv', index=False)
 return sample_submission
sample_submission = submission()
sample_submission.head()