Alya Shafwa Wafiya
Laporan ini membahas langkah-langkah yang dilakukan dalam membangun model klasifikasi gambar kucing dan anjing menggunakan dataset "Cat and Dog". Model ini dapat digunakan untuk mengenali dan membedakan gambar kucing dan anjing.
Project Overview
By understanding this project overview, we can have a general understanding of the project's objectives, business context, problem to be solved, proposed solution, and the methods and tools used in this project.
Data Understanding
By understanding the characteristics and condition of the data in the dataset, we can determine the next steps in the data preparation, modeling, and evaluation processes of the machine learning model.
Data Preparation
Here is an explanation of the code:
• The first step is to import the necessary libraries:
• Next, you define the training directory with the variable TRAINING_DIR, which indicates the location of the training dataset.
• You use the ImageDataGenerator to perform data augmentation on the training images. Some of the augmentations applied include rescaling, rotation, horizontal and vertical shifts, shearing, zooming, and horizontal flipping. Additionally, you split the validation data by 20% from the training data using the validation_split parameter.
• After that, you use flow_from_directory to create the training and validation generators. You specify class_mode='categorical' to generate one-hot encoded categorical labels. The images are also resized to 150x150 pixels using the target_size parameter.
• Then, you define an ImageDataGenerator for the validation data, which only performs rescaling.
• Once all the generator settings are in place, you define a Callback class, which is a subclass of tf.keras.callbacks.Callback. This will be used to stop the training when the training and validation accuracies reach a specified threshold (greater than 0.92 in this example).
By understanding this code, you have set up the necessary libraries, prepared the data generators with augmentation, and defined a callback for stopping the training based on accuracy.
Modelling
Here is an explanation of the code:
• A pre-trained MobileNetV2 model is loaded using the MobileNetV2 function from tensorflow.keras.applications. The weights parameter is set to "imagenet" to load the weights pre-trained on the ImageNet dataset. include_top is set to False to exclude the fully connected layers of the model, and input_tensor is defined to specify the input shape as (150, 150, 3).
• The layers of the pre-trained model are set as non-trainable to freeze their weights and prevent them from being updated during training.
• The output of the pre-trained model is stored in the last_output variable.
• Additional layers are added to the model. A Flatten layer is used to flatten the output, followed by a Dropout layer to reduce overfitting. Then, two fully connected (Dense) layers with ReLU activation functions are added, and the final output layer with softmax activation is added with 2 units representing the two classes (cat and dog).
• The model is created using tf.keras.models.Model by specifying the input and output layers.
• The learning rate (int_lr) and the number of epochs (num_epochs) are defined.
• The model is compiled with the Adam optimizer, categorical cross-entropy loss function, and accuracy metric.
• The model is trained using the fit function. The training data is provided by the train_generator, and validation data is provided by the validation_generator. The training is stopped if the accuracy of both training and validation exceeds 0.92, as defined in the callbacks parameter.
• The trained model is saved in the SavedModel format using tf.saved_model.save.
• The SavedModel is converted to a TensorFlow Lite model (tflite_model) using tf.lite.TFLiteConverter.
• The TensorFlow Lite model is saved as a .tflite file using the write_bytes method of pathlib.Path.
This code loads the pre-trained MobileNetV2 model, adds additional layers, compiles and trains the model, and finally saves it as a TensorFlow Lite model for deployment on resource-constrained devices.
Evaluasi Model
The purpose of this visualization is to aid in understanding how the model progresses during training. We can observe whether the model tends to overfit or underfit, as well as examine the trends of accuracy and loss for each epoch. This visualization can also assist in parameter selection and decision-making related to the model.
The code above is used to create plots showing the changes in model accuracy and loss during training.
By using this code, we can visualize the changes in model accuracy and loss during training with the presented plots. These plots help us analyze and understand the model's performance visually.