0% found this document useful (0 votes)
6 views6 pages

Kelompok 2 Deeplearning

The document outlines a project on fish image classification using transfer learning with the MobileNetV2 model. It details the process of setting up the dataset, augmenting images, training the model, and evaluating its performance, achieving a validation accuracy of approximately 92.3%. Additionally, it includes visualizations of training and validation accuracy and loss, as well as predictions on new images.

Uploaded by

Riyan Daifullah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views6 pages

Kelompok 2 Deeplearning

The document outlines a project on fish image classification using transfer learning with the MobileNetV2 model. It details the process of setting up the dataset, augmenting images, training the model, and evaluating its performance, achieving a validation accuracy of approximately 92.3%. Additionally, it includes visualizations of training and validation accuracy and loss, as well as predictions on new images.

Uploaded by

Riyan Daifullah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

FishClassificationKelompok2.ipynb - Colab https://colab.research.google.com/drive/156dUlXMO-TZPD5eDprQ66...

Penerapan Transfer Learning untuk Klasi�kasi Gambar Ikan


Menggunakan MobileNetV2
Nama Anggota :

1. 50421655 Inosensius Himawan Wicaksono


2. 50421675 Izmi Nabilah Isnaini
3. 50421933 Muhammad Dafa Alfaridzy
4. 50421655 Nelta Irianti
5. 51421333 Riyan Daifullah
�. 51421503 Vieri Giofanov

Kelas : 4IA13 Jurusan : Teknik Informatika Dosen : Rosny Gonindjaya, S.Kom, MMSI

 MeMount Google Drive

Menghubungkan Google Colab dengan Google Drive untuk mengakses dataset yang disimpan di
sana.

from google.colab import drive


drive.mount('/content/drive')

Drive already mounted at /content/drive; to attempt to forcibly remount, call drive.mount("/

Menyiapkan dan memuat dataset dengan augmentasi



untuk meningkatkan performa model.
ImageDataGenerator: Digunakan untuk augmentasi data seperti rotasi, pergeseran, dan �ipping.

�ow_from_directory: Memuat gambar dari direktori dan mengelompokkannya berdasarkan kelas.

import tensorflow as tf
from tensorflow.keras.preprocessing.image import ImageDataGenerator

# Path ke dataset di Google Drive


train_dir = '/content/drive/MyDrive/path/to/train'
validation_dir = '/content/drive/MyDrive/path/to/validation'

1 of 6 23/01/2025, 18:08
FishClassificationKelompok2.ipynb - Colab https://colab.research.google.com/drive/156dUlXMO-TZPD5eDprQ66...

validation_dir = '/content/drive/MyDrive/path/to/validation'

# ImageDataGenerator untuk augmentasi data


train_datagen = ImageDataGenerator(
rescale=1./255,
rotation_range=40,
width_shift_range=0.2,
height_shift_range=0.2,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True,
fill_mode='nearest'
)
validation_datagen = ImageDataGenerator(rescale=1./255)

# Load dataset
train_generator = train_datagen.flow_from_directory(
train_dir,
target_size=(150, 150),
batch_size=32,
class_mode='categorical' # Menggunakan 'categorical' untuk multi-class
)

Found 213 images belonging to 3 classes.

validation_generator = validation_datagen.flow_from_directory(
validation_dir,
target_size=(150, 150),
batch_size=32,
class_mode='categorical'
)

Found 26 images belonging to 3 classes.

from tensorflow.keras.applications import MobileNetV2


from tensorflow.keras import layers, models

# Load model pre-trained tanpa layer akhir


base_model = MobileNetV2(input_shape=(150, 150, 3), include_top=False, weights='imagenet'
base_model.trainable = False # Bekukan layer awal

# Tambahkan layer baru di atas model pre-trained


model = models.Sequential([
base_model,
layers.GlobalAveragePooling2D(),
layers.Dense(1024 activation='relu'),

2 of 6 23/01/2025, 18:08
FishClassificationKelompok2.ipynb - Colab https://colab.research.google.com/drive/156dUlXMO-TZPD5eDprQ66...

layers.Dense(1024, activation='relu'),
layers.Dropout(0.5),
layers.Dense(3, activation='softmax') # Sesuaikan dengan jumlah kelas
])

# Kompilasi model
model.compile(optimizer='adam',
loss='categorical_crossentropy',
metrics=['accuracy'])

<ipython-input-11-b4bc9337fac4>:5: UserWarning: `input_shape` is undefined or non-square, or


base_model = MobileNetV2(input_shape=(150, 150, 3), include_top=False, weights='imagenet')

# Latih model
history = model.fit(
train_generator,
epochs=10,
validation_data=validation_generator
)

Epoch 1/10
/usr/local/lib/python3.11/dist-packages/keras/src/trainers/data_adapters/py_dataset_adapter.
self._warn_if_super_not_called()
7/7 ━━━━━━━━━━━━━━━━━━━━ 137s 4s/step - accuracy: 0.4482 - loss: 2.4124 - val_accuracy: 0.73
Epoch 2/10
7/7 ━━━━━━━━━━━━━━━━━━━━ 10s 659ms/step - accuracy: 0.8649 - loss: 0.4975 - val_accuracy: 0.
Epoch 3/10
7/7 ━━━━━━━━━━━━━━━━━━━━ 11s 687ms/step - accuracy: 0.9050 - loss: 0.2467 - val_accuracy: 0.
Epoch 4/10
7/7 ━━━━━━━━━━━━━━━━━━━━ 21s 1s/step - accuracy: 0.9353 - loss: 0.1643 - val_accuracy: 0.961
Epoch 5/10
7/7 ━━━━━━━━━━━━━━━━━━━━ 19s 667ms/step - accuracy: 0.9825 - loss: 0.0655 - val_accuracy: 0.
Epoch 6/10
7/7 ━━━━━━━━━━━━━━━━━━━━ 12s 1s/step - accuracy: 0.9488 - loss: 0.1220 - val_accuracy: 0.923
Epoch 7/10
7/7 ━━━━━━━━━━━━━━━━━━━━ 19s 682ms/step - accuracy: 0.9784 - loss: 0.0506 - val_accuracy: 0.
Epoch 8/10
7/7 ━━━━━━━━━━━━━━━━━━━━ 21s 951ms/step - accuracy: 0.9572 - loss: 0.0821 - val_accuracy: 0.
Epoch 9/10
7/7 ━━━━━━━━━━━━━━━━━━━━ 10s 676ms/step - accuracy: 0.9624 - loss: 0.0986 - val_accuracy: 0.
Epoch 10/10
7/7 ━━━━━━━━━━━━━━━━━━━━ 22s 837ms/step - accuracy: 0.9560 - loss: 0.2121 - val_accuracy: 0.

# Evaluasi model pada data validasi


val_loss, val_accuracy = model.evaluate(validation_generator)
print(f"Validation Loss: {val_loss}")
print(f"Validation Accuracy: {val_accuracy}")

1/1 ━━━━━━━━━━━━━━━━━━━━ 1s 1s/step - accuracy: 0.9231 - loss: 0.2190


Validation Loss: 0.21898426115512848
Validation Accuracy: 0.9230769276618958

3 of 6 23/01/2025, 18:08
FishClassificationKelompok2.ipynb - Colab https://colab.research.google.com/drive/156dUlXMO-TZPD5eDprQ66...

import matplotlib.pyplot as plt

# Plot akurasi dan loss


acc = history.history['accuracy']
val_acc = history.history['val_accuracy']
loss = history.history['loss']
val_loss = history.history['val_loss']

epochs_range = range(len(acc))

plt.figure(figsize=(8, 8))
plt.subplot(1, 2, 1)
plt.plot(epochs_range, acc, label='Training Accuracy')
plt.plot(epochs_range, val_acc, label='Validation Accuracy')
plt.legend(loc='lower right')
plt.title('Training and Validation Accuracy')

plt.subplot(1, 2, 2)
plt.plot(epochs_range, loss, label='Training Loss')
plt.plot(epochs_range, val_loss, label='Validation Loss')
plt.legend(loc='upper right')
plt.title('Training and Validation Loss')
plt.show()

4 of 6 23/01/2025, 18:08
FishClassificationKelompok2.ipynb - Colab https://colab.research.google.com/drive/156dUlXMO-TZPD5eDprQ66...

import matplotlib.pyplot as plt


from tensorflow.keras.preprocessing import image
import numpy as np

# Fungsi untuk memuat dan memproses gambar


def load_and_preprocess_image(img_path):
img = image.load_img(img_path, target_size=(150, 150))
img_array = image.img_to_array(img)
img_array = np.expand_dims(img_array, axis=0) / 255.0
return img_array

# Daftar path ke gambar baru


img_paths = [
'/content/drive/MyDrive/path/to/train/Zebra/00000000.jpg',
'/content/drive/MyDrive/path/to/train/Cupang/00000012.jpg',
'/content/drive/MyDrive/path/to/train/Lohan/Lohan0.jpg'
]

# Label asli untuk setiap gambar


true_labels = ['Zebra', 'Cupang', 'Lohan']

# Muat dan preprocess gambar


images = [load_and_preprocess_image(path) for path in img_paths]

# Prediksi
predictions = [model.predict(img) for img in images]
predicted_classes = [np.argmax(pred) for pred in predictions]

# Mapping indeks ke nama kelas


class_indices = train_generator.class_indices
class_names = list(class_indices.keys())

# Visualisasi
plt.figure(figsize=(12, 4))
for i in range(3):
plt.subplot(1, 3, i + 1)
plt.imshow(image.load_img(img_paths[i], target_size=(150, 150)))
plt.title(f"Prediksi: {class_names[predicted_classes[i]]}\nAsli: {true_labels[i]}")
plt.axis('off')

5 of 6 23/01/2025, 18:08
FishClassificationKelompok2.ipynb - Colab https://colab.research.google.com/drive/156dUlXMO-TZPD5eDprQ66...

plt.axis( )
plt.tight_layout()
plt.show()

1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 64ms/step


1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 61ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 60ms/step

6 of 6 23/01/2025, 18:08

You might also like