text_plain_output_4.png application_vnd.jupyter.stderr_output_3.png text_plain_output_2.png application_vnd.jupyter.stderr_output_1.png
from imblearn.under_sampling import RandomUnderSampler

from keras.layers import Input, Conv2D, MaxPooling2D, UpSampling2D, concatenate, Conv2DTranspose, BatchNormalization, Dropout, Lambda

from keras.models import Sequential, Model

from tensorflow.keras import Sequential

from tensorflow.keras import datasets, layers, models

from tensorflow.keras.callbacks import EarlyStopping,ReduceLROnPlateau

from tensorflow.keras.layers import Flatten, Dense,BatchNormalization,Dropout,Input

from tensorflow.keras.preprocessing.image import ImageDataGenerator

import numpy as np

seed = 42
np.random.seed = seed

x_train = np.asarray(x_train, np.float32) / 255
x_test = np.asarray(x_test, np.float32) / 255
y_train = np.asarray(y_train)
y_test = np.asarray(y_test)

shape = 50 * 50 * 3
x_train = x_train.reshape(x_train.shape[0], shape)
x_test = x_test.reshape(x_test.shape[0], shape)

from imblearn.under_sampling import RandomUnderSampler
underampler = RandomUnderSampler(sampling_strategy='auto')
x_train_fit, y_train_fit = underampler.fit_resample(x_train, y_train)
X_test_fit, Y_test_fit = underampler.fit_resample(x_test, y_test)
x_train_ = x_train_fit.reshape(x_train_fit.shape[0], 50, 50, 3)
x_test_ = X_test_fit.reshape(X_test_fit.shape[0], 50, 50, 3)

batch_size = 32
image_gen = ImageDataGenerator(shear_range=0.2, zoom_range=0.3, height_shift_range=0.2, width_shift_range=0.2, fill_mode='nearest', horizontal_flip=True, rotation_range=20)
test_data_gen = ImageDataGenerator(shear_range=0.2, zoom_range=0.3, height_shift_range=0.2, width_shift_range=0.2, fill_mode='nearest', horizontal_flip=True, rotation_range=20)
train = image_gen.flow(x_train_, y_train_fit, shuffle=True, batch_size=batch_size)
test = test_data_gen.flow(x_test_, Y_test_fit, shuffle=True, batch_size=batch_size)

cnn_model = Sequential()
cnn_model.add(layers.Conv2D(64, (3, 3), padding='Same', activation='relu', input_shape=(50, 50, 3)))
cnn_model.add(layers.MaxPooling2D(2, 2))
cnn_model.add(BatchNormalization())
cnn_model.add(layers.Conv2D(128, (3, 3), padding='same', activation='relu'))
cnn_model.add(layers.MaxPooling2D(2, 2))
cnn_model.add(BatchNormalization())
cnn_model.add(layers.Conv2D(128, (3, 3), padding='same', activation='relu'))
cnn_model.add(layers.MaxPooling2D(2, 2))
cnn_model.add(BatchNormalization())
cnn_model.add(layers.Conv2D(256, (3, 3), padding='same', activation='relu'))
cnn_model.add(layers.MaxPooling2D(2, 2))
cnn_model.add(layers.Conv2D(256, (3, 3), padding='same', activation='relu'))
cnn_model.add(layers.MaxPooling2D(2, 2))
cnn_model.add(BatchNormalization())
cnn_model.summary()

cnn_model.add(layers.Flatten())
cnn_model.add(layers.Dense(1024, activation='relu'))
cnn_model.add(BatchNormalization())
cnn_model.add(Dropout(0.3))
cnn_model.add(layers.Dense(1024, activation='relu'))
cnn_model.add(BatchNormalization())
cnn_model.add(Dropout(0.3))
cnn_model.add(layers.Dense(1, activation='sigmoid'))
cnn_model.summary()

cnn_model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])

early = EarlyStopping(monitor='loss', mode='min', min_delta=0, patience=10, verbose=1, restore_best_weights=True)
learning_rate_reduction = ReduceLROnPlateau(monitor='loss', patience=2, verbose=1, factor=0.3, min_lr=1e-06)
callbacks_list = [early, learning_rate_reduction]

n_training_samples = len(train)
n_validation_samples = len(test)
history = cnn_model.fit(train, epochs=150, validation_data=test, validation_steps=n_validation_samples // batch_size, shuffle=True, callbacks=callbacks_list)