<jupyter_start><jupyter_text>CNN menggunakan TensorFlow**Studi Kasus : Klasifikasi Anjing Vs Kucing**Data set: 8000 data training, 2000 data testingDataset: https://drive.google.com/drive/folders/1bSdWPKXLf21F0x3qSgq01X2P2HsN394c?usp=sharingTahapan membangun CNN :1. Import Library2. Instansiasi Object Sequential(modelnya)3. Menyusun Arsitektur CNN:    * a. Convulution    * b. Pooling    * c. Flattening    * d. Fully Connection4. Menjalankan(meng-compile) model5. Augmentasi (jika Diperlukan)6. Membangun Model dengan *Training dataset*7. Evaluasi Model8. Prediksi<jupyter_code># Importing the Keras libraries and packages
from keras.models import Sequential
from keras.layers import Conv2D
from keras.layers import MaxPooling2D
from keras.layers import Flatten
from keras.layers import Dense
#create an object sequential
classifier = Sequential()<jupyter_output><empty_output><jupyter_text>1. ConvolutionStep - 2 : PoolingStep - 3 : FlatteningStep - 4 : Full connection<jupyter_code>classifier.add(Conv2D(32, (3, 3), input_shape = (128, 128, 3), activation = 'relu'))<jupyter_output><empty_output><jupyter_text>2 : Pooling<jupyter_code>classifier.add(MaxPooling2D(pool_size = (2, 2)))<jupyter_output><empty_output><jupyter_text>3. Flattening<jupyter_code>classifier.add(Flatten())<jupyter_output><empty_output><jupyter_text>4. Fully Connecting<jupyter_code>classifier.add(Dense(units = 128, activation = 'relu'))
#initialise our output layer
classifier.add(Dense(units = 1, activation = 'sigmoid'))





#compile the model
classifier.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])<jupyter_output><empty_output><jupyter_text><jupyter_code>#augmentation using datagenerator
from keras.preprocessing.image import ImageDataGenerator
from google.colab import drive
drive.mount('/gdrive', force_remount=True)
train_datagen = ImageDataGenerator(rescale = 1./255,
  shear_range = 0.2,
  zoom_range = 0.2,
  horizontal_flip = True)
test_datagen = ImageDataGenerator(rescale = 1./255)

training_set = train_datagen.flow_from_directory('/gdrive/My Drive/Colab Notebooks/CNN_DOG_VS_CAT/dataset/training_set',
  target_size = (128, 128),
  batch_size = 32,
  class_mode = 'binary')
test_set = test_datagen.flow_from_directory('/gdrive/My Drive/Colab Notebooks/CNN_DOG_VS_CAT/dataset/test_set',
  target_size = (128, 128),
  batch_size = 32,
  class_mode = 'binary')
print(training_set.n)
print(training_set.batch_size)
#build the model

STEP_SIZE_TRAIN=training_set.n//training_set.batch_size
STEP_SIZE_VALID=test_set.n//test_set.batch_size
classifier.fit(training_set,
  #steps_per_epoch = 8000,
  steps_per_epoch = STEP_SIZE_TRAIN,
  epochs = 50,
  validation_data = test_set,
  validation_steps = STEP_SIZE_VALID)


classifier.summary()
#simpan model
classifier.save('/gdrive/My Drive/Colab Notebooks/CNN_DOG_VS_CAT/model/model_50_epoch.h5')

# Recreate the exact same model, including its weights and the optimizer
from google.colab import drive
drive.mount('/gdrive', force_remount=True)
from tensorflow.keras.models import load_model
new_model = load_model('/gdrive/My Drive/Colab Notebooks/CNN_DOG_VS_CAT/model/model_50_epoch.h5')

# Show the model architecture
new_model.summary()

# Part 3 - Making new predictions
import numpy as np
from keras.preprocessing import image
from google.colab.patches import cv2,cv2_imshow
import matplotlib.pyplot as plt
import matplotlib.image as mpimg

from skimage import io
url='/gdrive/My Drive/Colab Notebooks/CNN_DOG_VS_CAT/dataset/single_prediction/cat_or_dog_6.jpg'
test_image = image.load_img(url, target_size = (128, 128))
test_image = image.img_to_array(test_image)
test_image = np.expand_dims(test_image, axis = 0)
result = new_model.predict(test_image)
image = mpimg.imread(url)
plt.axis("off")
plt.imshow(image)
plt.show()

print (result[0][0])
#training_set.class_indices
if result[0][0] == 1.0:
  print ( 'dog')
else:
  print('cat')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10697084917617238, 'fraction_numerical': 0.02864385297845374, 'mean_word_length': 6.632495164410058, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 1, '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': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '116296', 'license': 'Apache-2.0', 'n_tokens_mistral': 1517, 'n_tokens_neox': 1420, 'n_words': 283, 'path': 'CNN.ipynb', 'repo_name': 'annaamikom/cnn', 'size': '3945'}