# -*- coding: utf-8 -*-
"""imageProcessing.ipynb

Automatically generated by Colaboratory.

Original file is located at
    https://colab.research.google.com/drive/18StecKNKHkl2TSzCpC-EtI5hJs5u6Mpf
"""

!wget --no-check-certificate \
https://dicodingacademy.blob.core.windows.net/picodiploma/ml_pengembangan_academy/Chessman-image-dataset.zip \
  -O /tmp/Chessman-image-dataset.zip

#melakukan ektraksi karena file yang diunduh berbentuk zip
import os
import zipfile
local_zip = '/tmp/Chessman-image-dataset.zip'
zip_ref = zipfile.ZipFile(local_zip, 'r')
zip_ref.extractall('/tmp')
zip_ref.close()

#menggunakan fungsi listdir dari kelas os untuk melihat diektiri yang tedapat pada dataset
os.listdir('/tmp/Chessman-image-dataset/Chess')

#melihat berapa banyak gambar pada setiap direktori
print('total pawn images :', len(os.listdir('/tmp/Chessman-image-dataset/Chess/Pawn')))
print('total king images :', len(os.listdir('/tmp/Chessman-image-dataset/Chess/King')))
print('total Knight images :', len(os.listdir('/tmp/Chessman-image-dataset/Chess/Knight')))

# Commented out IPython magic to ensure Python compatibility.
#melihat contoh dari sebuah gambar pada dataset 
from keras.preprocessing import image
import matplotlib.image as mpimg
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
# %matplotlib inline
img = image.load_img('/tmp/Chessman-image-dataset/Chess/King/00000145.png') 
imgplot = plt.imshow(img)

#impor data imageDataGenerator dan aplikasikan augmentsi gambar oada dataset kita

from tensorflow.keras.preprocessing.image import ImageDataGenerator


train_dir = os.path.join('/tmp/Chessman-image-dataset/Chess')
train_datagen = ImageDataGenerator(rescale=1./255,
                                   rotation_range=20,
                                   zoom_range=0.2,
                                   shear_range=0.2,
                                   fill_mode =  'nearest',
                                   validation_split= 0.1) #set validation split

#membagi direktori menjadi training dan testing dengan menggunakan ImageDataGenerator
#dengan menggunakan parameter validation_split kita hanya cukup memberi tahu imagedatagenerator
# mana data untuk training dan mana data untuk testing dnegan menmabhakan parameter 
#subset dengan nilai training atau validation
train_generator = train_datagen.flow_from_directory(
    train_dir,
    target_size=(150, 150),
    batch_size=8,
    class_mode='categorical',
    subset='training') # set as training data
validation_generator = train_datagen.flow_from_directory(
    train_dir, # same directory as training data
    target_size=(150, 150),
    batch_size=16,
    class_mode='categorical',
    subset='validation')

#membuat arsitektur dari model dengan menggunakan 3 lapis convolution dan 2 hidden layer
#dengna 512 dan 256 buah unit presepton
import tensorflow as tf
model = tf.keras.models.Sequential([
    # Note the input shape is the desired size of the image 150x150 with 3 bytes color
    tf.keras.layers.Conv2D(64, (3,3), activation='relu', input_shape=(150, 150, 3)),
    tf.keras.layers.MaxPooling2D(2,2),
    tf.keras.layers.Conv2D(32, (3,3), activation='relu'),
    tf.keras.layers.MaxPooling2D(2,2),
    tf.keras.layers.Dropout(0.4),  
    tf.keras.layers.Conv2D(64, (3,3), activation='relu'), 
    tf.keras.layers.MaxPooling2D(2,2),
    tf.keras.layers.Dropout(0.4),  
    # Flatten the results to feed into a DNN
    tf.keras.layers.Flatten(), 
    # 512 neuron hidden layer
    tf.keras.layers.Dense(512, activation='relu'),
    tf.keras.layers.Dense(256, activation='relu'),
    # Only 1 output neuron. It will contain a value from 0-1 where 0 for 1 class ('cats') and 1 for the other ('dogs')
    tf.keras.layers.Dense(6, activation='softmax')  
])

#menentukan optimizer dan loss yang digunakan model
model.compile(optimizer=tf.optimizers.Adam(),
              loss='categorical_crossentropy',
              metrics = ['accuracy'])

#memanggil fungsi fit untuk melatih midel

history =  model.fit(train_generator,
                     validation_data=validation_generator,
                     epochs=50,
                     verbose=2
                     )

#membuat plot akurasi dari model

import matplotlib.pyplot as plt
plt.plot(history.history['accuracy'])
plt.plot(history.history['val_accuracy'])
plt.title('Akurasi model')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()

#membuat plot loss
import matplotlib.pyplot as plt
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('Los Model')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()


8dee5025ab4850d4a7f491827a2ce46940103e95
# -*- coding: utf-8 -*-
"""imageProcessing.ipynb

Automatically generated by Colaboratory.

Original file is located at
    https://colab.research.google.com/drive/18StecKNKHkl2TSzCpC-EtI5hJs5u6Mpf
"""

!wget --no-check-certificate \
https://dicodingacademy.blob.core.windows.net/picodiploma/ml_pengembangan_academy/Chessman-image-dataset.zip \
  -O /tmp/Chessman-image-dataset.zip

#melakukan ektraksi karena file yang diunduh berbentuk zip
import os
import zipfile
local_zip = '/tmp/Chessman-image-dataset.zip'
zip_ref = zipfile.ZipFile(local_zip, 'r')
zip_ref.extractall('/tmp')
zip_ref.close()

#menggunakan fungsi listdir dari kelas os untuk melihat diektiri yang tedapat pada dataset
os.listdir('/tmp/Chessman-image-dataset/Chess')

#melihat berapa banyak gambar pada setiap direktori
print('total pawn images :', len(os.listdir('/tmp/Chessman-image-dataset/Chess/Pawn')))
print('total king images :', len(os.listdir('/tmp/Chessman-image-dataset/Chess/King')))
print('total Knight images :', len(os.listdir('/tmp/Chessman-image-dataset/Chess/Knight')))

# Commented out IPython magic to ensure Python compatibility.
#melihat contoh dari sebuah gambar pada dataset 
from keras.preprocessing import image
import matplotlib.image as mpimg
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
# %matplotlib inline
img = image.load_img('/tmp/Chessman-image-dataset/Chess/King/00000145.png') 
imgplot = plt.imshow(img)

#impor data imageDataGenerator dan aplikasikan augmentsi gambar oada dataset kita

from tensorflow.keras.preprocessing.image import ImageDataGenerator


train_dir = os.path.join('/tmp/Chessman-image-dataset/Chess')
train_datagen = ImageDataGenerator(rescale=1./255,
                                   rotation_range=20,
                                   zoom_range=0.2,
                                   shear_range=0.2,
                                   fill_mode =  'nearest',
                                   validation_split= 0.1) #set validation split

#membagi direktori menjadi training dan testing dengan menggunakan ImageDataGenerator
#dengan menggunakan parameter validation_split kita hanya cukup memberi tahu imagedatagenerator
# mana data untuk training dan mana data untuk testing dnegan menmabhakan parameter 
#subset dengan nilai training atau validation
train_generator = train_datagen.flow_from_directory(
    train_dir,
    target_size=(150, 150),
    batch_size=8,
    class_mode='categorical',
    subset='training') # set as training data
validation_generator = train_datagen.flow_from_directory(
    train_dir, # same directory as training data
    target_size=(150, 150),
    batch_size=16,
    class_mode='categorical',
    subset='validation')

#membuat arsitektur dari model dengan menggunakan 3 lapis convolution dan 2 hidden layer
#dengna 512 dan 256 buah unit presepton
import tensorflow as tf
model = tf.keras.models.Sequential([
    # Note the input shape is the desired size of the image 150x150 with 3 bytes color
    tf.keras.layers.Conv2D(64, (3,3), activation='relu', input_shape=(150, 150, 3)),
    tf.keras.layers.MaxPooling2D(2,2),
    tf.keras.layers.Conv2D(32, (3,3), activation='relu'),
    tf.keras.layers.MaxPooling2D(2,2),
    tf.keras.layers.Dropout(0.4),  
    tf.keras.layers.Conv2D(64, (3,3), activation='relu'), 
    tf.keras.layers.MaxPooling2D(2,2),
    tf.keras.layers.Dropout(0.4),  
    # Flatten the results to feed into a DNN
    tf.keras.layers.Flatten(), 
    # 512 neuron hidden layer
    tf.keras.layers.Dense(512, activation='relu'),
    tf.keras.layers.Dense(256, activation='relu'),
    # Only 1 output neuron. It will contain a value from 0-1 where 0 for 1 class ('cats') and 1 for the other ('dogs')
    tf.keras.layers.Dense(6, activation='softmax')  
])

#menentukan optimizer dan loss yang digunakan model
model.compile(optimizer=tf.optimizers.Adam(),
              loss='categorical_crossentropy',
              metrics = ['accuracy'])

#memanggil fungsi fit untuk melatih midel

history =  model.fit(train_generator,
                     validation_data=validation_generator,
                     epochs=50,
                     verbose=2
                     )

#membuat plot akurasi dari model

import matplotlib.pyplot as plt
plt.plot(history.history['accuracy'])
plt.plot(history.history['val_accuracy'])
plt.title('Akurasi model')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()

#membuat plot loss
import matplotlib.pyplot as plt
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('Los Model')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()

