> For the complete documentation index, see [llms.txt](https://polinema.gitbook.io/jti-modul-praktikum-pembelajaran-mesin/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://polinema.gitbook.io/jti-modul-praktikum-pembelajaran-mesin/job-sheet-11-convolutional-neural-network-cnn/praktikum-1.md).

# Praktikum 1

## Deskripsi

Pada praktikum ini kita akan membuat model klasifikasi CNN sederhana pada kasus citra kucing dan anjing.

## Dataset

Dataset merupakan data citra anjing dan kucing yang telah dibagi menjadi data training dan data testing. Dikarenakan data cukup besar, pastikan koneksi Anda sebelum mengunduh dataset.

:arrow\_forward:[**DOWNLOAD**](https://drive.google.com/file/d/1vYrqBI1VmiXXJd5sgtKK2nuQvC8T1ryb/view?usp=drive_link):arrow\_left:

## Langkah 1 - Import Library

{% code overflow="wrap" lineNumbers="true" %}

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

{% endcode %}

## Langkah 2 - Pra Pengolahan Data

Pada tahap ini kita akan sedikit melakukan manipulasi pada citra yang digunakan. Manipulasi yang dilakukan diantaranya adalah normalisasi nilai piksel, koreksi kemiringan, pembesaran (zoom), dan flip.

### Langkah 2.1. Pra Pengolahan Data Training

{% code overflow="wrap" lineNumbers="true" %}

```python
train_datagen = ImageDataGenerator(rescale = 1./255,
                                   shear_range = 0.2,
                                   zoom_range = 0.2,
                                   horizontal_flip = True)
training_set = train_datagen.flow_from_directory('dataset/training_set',
                                                 target_size = (64, 64),
                                                 batch_size = 32,
                                                 class_mode = 'binary')
```

{% endcode %}

### Langkah 2.2. Pra Pengolahan Data Testing

{% code overflow="wrap" lineNumbers="true" %}

```python
test_datagen = ImageDataGenerator(rescale = 1./255)
test_set = test_datagen.flow_from_directory('dataset/test_set',
                                            target_size = (64, 64),
                                            batch_size = 32,
                                            class_mode = 'binary')
```

{% endcode %}

## Langkah 3 - Pembuatan Model CNN

### Langkah 3.1.  - Inisiasi Model CNN

{% code overflow="wrap" lineNumbers="true" %}

```python
cnn = tf.keras.models.Sequential()
```

{% endcode %}

### Langkah 3.2. - Pembuatan Layer Konvolusi 1

{% code overflow="wrap" lineNumbers="true" %}

```python
cnn.add(tf.keras.layers.Conv2D(filters=32, kernel_size=3, activation='relu', input_shape=[64, 64, 3]))
```

{% endcode %}

### Langkah 3.3 - Pembuatan Layer Pooling 1

{% code overflow="wrap" lineNumbers="true" %}

```python
cnn.add(tf.keras.layers.MaxPool2D(pool_size=2, strides=2))
```

{% endcode %}

### Langkah 3.4 - Pembuatan Layer Konvolusi 2 dan Pooling 2

{% code overflow="wrap" lineNumbers="true" %}

```python
cnn.add(tf.keras.layers.Conv2D(filters=32, kernel_size=3, activation='relu'))
cnn.add(tf.keras.layers.MaxPool2D(pool_size=2, strides=2))
```

{% endcode %}

### Langkah 3.5 - Flattening

{% code overflow="wrap" lineNumbers="true" %}

```python
cnn.add(tf.keras.layers.Flatten())
```

{% endcode %}

### Langkah 3.6 - Fully Connected Layer 1 (Input)

{% code overflow="wrap" lineNumbers="true" %}

```python
cnn.add(tf.keras.layers.Dense(units=128, activation='relu'))
```

{% endcode %}

### Langkah 3.7 - Fully Connected Layer 2 (Output)

{% code overflow="wrap" lineNumbers="true" %}

```python
cnn.add(tf.keras.layers.Dense(units=1, activation='sigmoid'))
```

{% endcode %}

### Langkah 3.8 - Compile Model CNN

{% code overflow="wrap" lineNumbers="true" %}

```python
cnn.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])
```

{% endcode %}

Penggunaan ***loss function binary crossentropy*** dikarenakan kita hanya melakukan klasifikasi pada dua kelas, yaitu kucing dan anjing.

## Langkah 4 - Fit CNN

{% code overflow="wrap" lineNumbers="true" %}

```python
cnn.fit(x = training_set, validation_data = test_set, epochs = 25)
```

{% endcode %}

## Langkah 5 - Prediksi dengan 1 Citra

Pada langkah ini, kita akan mencoba melakukan prediksi pada 1 citra anjing dan kucing.

```python
import numpy as np
from keras.preprocessing import image
test_image = image.load_img('dataset/single_prediction/cat_or_dog_1.jpg', target_size = (64, 64))
test_image = image.img_to_array(test_image)
test_image = np.expand_dims(test_image, axis = 0)
result = cnn.predict(test_image)
training_set.class_indices
if result[0][0] == 1:
  prediction = 'dog'
else:
  prediction = 'cat'
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://polinema.gitbook.io/jti-modul-praktikum-pembelajaran-mesin/job-sheet-11-convolutional-neural-network-cnn/praktikum-1.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
