<jupyter_start><jupyter_text><jupyter_code>from google.colab import drive
drive.mount('/content/drive/')
# import the necessary packages
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.applications import MobileNetV2
from tensorflow.keras.layers import AveragePooling2D,Dropout,Flatten,Dense,Input
from tensorflow.keras.models import Model
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.applications.mobilenet_v2 import preprocess_input
from tensorflow.keras.preprocessing.image import img_to_array
from tensorflow.keras.preprocessing.image import load_img
from tensorflow.keras.utils import to_categorical
from sklearn.preprocessing import LabelBinarizer
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report
from imutils import paths
import matplotlib.pyplot as plt
import numpy as np

# initialize the initial learning rate, number of epochs to train for,
# and batch size
INIT_LR = 1e-4
EPOCHS = 60
BS = 20
# load the MobileNetV2 network, ensuring the head FC layer sets are
# left off
baseModel = MobileNetV2(weights="imagenet", include_top=False,
	input_tensor=Input(shape=(224, 224, 3)))
# construct the head of the model that will be placed on top of the
# the base model
headModel = baseModel.output
headModel = AveragePooling2D(pool_size=(7, 7))(headModel)
headModel = Flatten(name="flatten")(headModel)
headModel = Dense(128, activation="relu")(headModel)
headModel = Dropout(0.5)(headModel)
headModel = Dense(2, activation="softmax")(headModel)
# place the head FC model on top of the base model (this will become
# the actual model we will train)
model = Model(inputs=baseModel.input, outputs=headModel)
# loop over all layers in the base model and freeze them so they will
# *not* be updated during the first training process
for layer in baseModel.layers:
	layer.trainable = False
# compile our model
print("[INFO] compiling model...")
opt = Adam(lr=INIT_LR, decay=INIT_LR / EPOCHS)
model.compile(loss="binary_crossentropy", optimizer=opt,metrics=["accuracy"])

# construct the training image generator for data augmentation
train_datagen = ImageDataGenerator(
	rescale = 1./255,
	rotation_range=20,
	zoom_range=0.15,
	width_shift_range=0.2,
	height_shift_range=0.2,
	shear_range=0.15,
	horizontal_flip=True,
	fill_mode="nearest")
test_datagen = ImageDataGenerator(rescale = 1./255)
train_path = '/content/drive/MyDrive/Colab Notebooks/cell_images/train'
test_path = '/content/drive/MyDrive/Colab Notebooks/cell_images/test'
training_set = train_datagen.flow_from_directory(
	train_path,
	target_size=(224, 224),
	batch_size = BS,
	class_mode = 'categorical'
	)

test_set = train_datagen.flow_from_directory(
	test_path,
	target_size=(224, 224),
	batch_size = BS,
	class_mode = 'categorical'
	)

from keras.callbacks import ModelCheckpoint
filepath = '/content/drive/MyDrive/Colab Notebooks/malaria/my_best_malaria_model.epoch{epoch:02d}-loss{val_loss:.2f}.hdf5'
checkpoint = ModelCheckpoint(filepath=filepath, 
                             monitor='val_loss',
                             verbose=1, 
                             save_best_only=True,
                             mode='min')
callbacks = [checkpoint]
# train the head of the network
print("[INFO] training head...")
H = model.fit_generator(training_set,
	steps_per_epoch=len(training_set) // BS,
	validation_data=test_set,
	validation_steps=len(test_set) // BS ,
	epochs=EPOCHS,
  callbacks=callbacks)

from keras.models import load_model
model = load_model('/content/drive/MyDrive/Colab Notebooks/malaria/my_best_malaria_model.epoch28-loss0.07.hdf5')
pred = model.predict(test_set)
# make predictions on the testing set
print("[INFO] evaluating network...")
model.evaluate(test_set,batch_size= BS)
# plot the training loss and accuracy
N = EPOCHS
plt.style.use("ggplot")
plt.figure()
plt.plot(np.arange(0, N), H.history["loss"], label="train_loss")
plt.plot(np.arange(0, N), H.history["val_loss"], label="val_loss")
plt.plot(np.arange(0, N), H.history["accuracy"], label="train_acc")
plt.plot(np.arange(0, N), H.history["val_accuracy"], label="val_acc")
plt.title("Training Loss and Accuracy")
plt.xlabel("Epoch #")
plt.ylabel("Loss/Accuracy")
plt.legend(loc="lower left")
plt.savefig("plot.png")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10833333333333334, 'fraction_numerical': 0.01712962962962963, 'mean_word_length': 5.527190332326284, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '294016', 'license': 'MIT', 'n_tokens_mistral': 1474, 'n_tokens_neox': 1408, 'n_words': 337, 'path': 'detect_malaria.ipynb', 'repo_name': 'Amanuel4/Realtime-Drowsiness-Detection-and--Alarm', 'size': '4320'}