<jupyter_start><jupyter_text>CNN<jupyter_code>#Creating a function for saving the model at each epoch end
class LossHistory(Callback):
    def __init__(self):
        super().__init__()
        self.epoch_id = 0
        self.losses = ''
 
    def on_epoch_end(self, epoch, logs={}):
        self.losses += "Epoch {}: accuracy -> {:.3f}, val_accuracy -> {:.3f}\n"\
            .format(str(self.epoch_id), logs.get('acc'), logs.get('val_acc'))
        self.epoch_id += 1
 
    def on_train_begin(self, logs={}):
        self.losses += 'Training begins...\n'
#Creating a checkpointer 
checkpointer = ModelCheckpoint(filepath = os.path.join('Detection\\Models', 'male_or_female_pretained.h5'), 
                               verbose=1,save_best_only=True)
# Initialising the CNN
model = Sequential()
model.add(Conv2D(32, (3,3), padding="same", input_shape=(img_dims)))
model.add(Activation("relu"))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(3,3)))
model.add(Dropout(drop_out))

model.add(Conv2D(64, (3,3), padding="same"))
model.add(Activation("relu"))
model.add(BatchNormalization())

model.add(Conv2D(64, (3,3), padding="same"))
model.add(Activation("relu"))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(drop_out))

model.add(Conv2D(128, (3,3), padding="same"))
model.add(Activation("relu"))
model.add(BatchNormalization())
model.add(Conv2D(128, (3,3), padding="same"))
model.add(Activation("relu"))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(drop_out))

model.add(Flatten())
model.add(Dense(1024))
model.add(Activation("relu"))
model.add(BatchNormalization())
model.add(Dropout(0.5))

model.add(Dense(2))
model.add(Activation("sigmoid"))
#compiling
optimizer = Adam(lr=lr_rate, decay= lr_rate/epochs)
metrics=['accuracy']
model.compile(optimizer=optimizer, loss='binary_crossentropy', metrics=metrics)
# Create a loss history
history = LossHistory()
# train the model
x = model.fit_generator(aug.flow(trainX, trainY, batch_size=batch_size),
                        validation_data=(testX,testY),
                        steps_per_epoch=len(trainX) // batch_size,
                        epochs=epochs, 
                        verbose=1, 
                        workers = 6, 
                        callbacks = [history, checkpointer], 
                        max_q_size = 100)
x.history.keys()
plt.rcParams['figure.figsize'] = (15, 20)
plt.style.use("seaborn")
# plot training/validation loss/accuracy
N = epochs
plt.plot(np.arange(0,N), x.history["loss"], label="train_loss")
plt.plot(np.arange(0,N), x.history["val_loss"], label="val_loss")
plt.plot(np.arange(0,N), x.history["acc"], label="train_acc")
plt.plot(np.arange(0,N), x.history["val_acc"], label="val_acc")

plt.title("Training Loss and Accuracy")
plt.xlabel("Epoch #")
plt.ylabel("Loss/Accuracy")
plt.legend(loc="upper right")
plt.show()
# summarize history for accuracy
plt.plot(x.history['acc'])
plt.plot(x.history['val_acc'])
plt.title('Model Accuracy')
plt.ylabel('Accuracy')
plt.xlabel('Epoch #')
plt.legend(['train', 'test'], loc='upper left')
plt.show()
# summarize history for loss
plt.plot(x.history['loss'])
plt.plot(x.history['val_loss'])
plt.title('Model Loss')
plt.ylabel('Loss')
plt.xlabel('Epoch #')
plt.legend(['train', 'test'], loc='upper left')
plt.show()
model_backup_path
score = model.evaluate(testX, testY)
print("Accuracy on test data:", score[1])
# Save loss history to file
loss_history_path = os.path.join('Detection\\Models', 'loss_history_pretained.log')
myFile = open(loss_history_path, 'w+')
myFile.write(history.losses)
myFile.close()
backend.clear_session()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1771058315334773, 'fraction_numerical': 0.01700863930885529, 'mean_word_length': 4.744186046511628, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '333868', 'license': 'Apache-2.0', 'n_tokens_mistral': 1371, 'n_tokens_neox': 1293, 'n_words': 210, 'path': 'Gender_Classification_Experiments/GenderClassification_Model-Exp1.ipynb', 'repo_name': 'Niranjankumar-c/TeamSelectionTool', 'size': '3704'}