<jupyter_start><jupyter_text>Image Recognition- Menggunakan BPNN dengan PCA- Menggunakan CNN dengan Normalisasi Data Anggota Kelompok:- <NAME>- <NAME>- <NAME>- <NAME><jupyter_code>import time

import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
import tensorflow_datasets as tfds

from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.keras.models import Sequential

from sklearn.preprocessing import MinMaxScaler
from sklearn.decomposition import PCA
seed = 1
img_h = 100
img_w = 100
batch_size = 32
data_dir = 'FotoAI2020'

train_set = tf.keras.preprocessing.image_dataset_from_directory(
    directory=data_dir,
    validation_split=0.3,
    subset='training',
    batch_size=batch_size,
    color_mode='grayscale',
    image_size=(img_h, img_w),
    seed=seed
)
test_set = tf.keras.preprocessing.image_dataset_from_directory(
    directory=data_dir,
    validation_split=0.3,
    subset='validation',
    batch_size=batch_size,
    color_mode='grayscale',
    image_size=(img_h, img_w),
    seed=seed
)
classes = train_set.class_names
print(classes)
plt.figure(figsize=(10, 10))
for images, labels in train_set.take(1):
  for i in range(9):
    ax = plt.subplot(3, 3, i + 1)
    plt.imshow(images[i], cmap='gray')
    plt.title(classes[labels[i]])
    plt.axis("off")
for image_batch, labels_batch in train_set:
  print(image_batch.shape)
  print(labels_batch.shape)
  break
train_set = train_set.cache().prefetch(buffer_size=tf.data.AUTOTUNE)
test_set = test_set.cache().prefetch(buffer_size=tf.data.AUTOTUNE)
normalization_layer = layers.experimental.preprocessing.Rescaling(1./255)<jupyter_output><empty_output><jupyter_text>Normalisasi data, mengubah satuan piksel (0-255) menjadi satuan decimal (0.0 - 1.0) agar mudah diolah oleh fungsi Neural Network<jupyter_code>normalized_ds = train_set.map(lambda x, y: (normalization_layer(x), y))
image_batch, labels_batch = next(iter(normalized_ds))
first_image = image_batch[0]
# Notice the pixels values are now in `[0,1]`.
print(np.min(first_image), np.max(first_image))<jupyter_output>0.0 1.0<jupyter_text>CNN- Membangun Layer (Neuron)<jupyter_code>num_classes = 43

model = Sequential([
  layers.experimental.preprocessing.Rescaling(1./255, input_shape=(img_h, img_w, 1)),
  layers.Conv2D(16, 3, padding='same', activation='relu'),
  layers.MaxPooling2D(),
  layers.Conv2D(32, 3, padding='same', activation='relu'),
  layers.MaxPooling2D(),
  layers.Conv2D(64, 3, padding='same', activation='relu'),
  layers.MaxPooling2D(),
  layers.Flatten(),
  layers.Dense(128, activation='relu'),
  layers.Dense(num_classes)
])
model.compile(optimizer='adam',
              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
              metrics=['accuracy'])
model.summary()
class timecallback(tf.keras.callbacks.Callback):
    def __init__(self):
        self.times = []
        # use this value as reference to calculate cummulative time taken
        self.timetaken = time.time()
    def on_epoch_end(self,epoch,logs = {}):
        self.times.append((epoch, time.time() - self.timetaken))
    def on_train_end(self,logs = {}):
        plt.xlabel('Epoch')
        plt.ylabel('Total time taken until an epoch (sec)')
        plt.plot(*zip(*self.times))
        plt.show()
        print('Total Time to Epoch ', self.times[-1][0], ': ', self.times[-1][1], ' Seconds')
epochs = 20
timetaken = timecallback()
history = model.fit(
  train_set,
  validation_data=test_set,
  epochs=epochs,
  callbacks=timetaken
)
acc = history.history['accuracy']
val_acc = history.history['val_accuracy']

# simpan hasil akurasi untuk di plot
CNN_acc = (acc[-1], val_acc[-1])

loss = history.history['loss']
val_loss = history.history['val_loss']

epochs_range = range(epochs)

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()<jupyter_output><empty_output><jupyter_text>BPNN  PCA - Reduksi Fitur- Ubah ke Format Numpy Array dari Tensorflow data.Dataset agar dapat diolah oleh Sklearn PCA<jupyter_code>def tfds_to_numpy(ds):
    ret = None
    lab = None
    for image, label in tfds.as_numpy(ds):
        if (ret is None):
            ret = image
            lab = label
        else:
            ret = np.append(ret, image, axis=0)
            lab = np.append(lab, label)
    return ret, lab.flatten()

reshape2 = (lambda arr: arr.reshape(arr.shape[0], arr.shape[1] * arr.shape[2]))

x_train_np, y_train_np = tfds_to_numpy(train_set)
x_test_np, y_test_np = tfds_to_numpy(test_set)


x_train_np = reshape2(x_train_np)
x_test_np = reshape2(x_test_np)

print(x_train_np.shape)
print(y_train_np.shape)
scaler = MinMaxScaler()
X_train_scaled = scaler.fit_transform(x_train_np)
X_test_scaled = scaler.transform(x_test_np)
pca = PCA(n_components=200, whiten=True)
pca.fit(X_train_scaled)
X_pca_train = pca.fit_transform(X_train_scaled)
X_pca_test = pca.transform(X_test_scaled)
pca_std = np.std(X_pca_train)

print(X_pca_train.shape)<jupyter_output>(294, 200)<jupyter_text>Membangun BPNN Layer- Relu - Relu - Relu - Sigmoid<jupyter_code>model = Sequential([
    keras.layers.Flatten(),
    keras.layers.GaussianNoise(pca_std),
    keras.layers.Dense(units=512, activation='relu'),
    keras.layers.Dense(units=256, activation='relu'),
    keras.layers.Dense(units=128, activation='relu'),
    keras.layers.Dense(units=num_classes, activation='sigmoid')
])
model.compile(optimizer='adam',
              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False),
              metrics=['accuracy'])
history = model.fit(
    x=X_pca_train,
    y=y_train_np,
    validation_data=(X_pca_test, y_test_np),
    epochs=epochs,
    callbacks=timetaken
)
acc = history.history['accuracy']
val_acc = history.history['val_accuracy']

# simpan hasil akurasi untuk di plot
BPNN_acc = (acc[-1], val_acc[-1])

loss = history.history['loss']
val_loss = history.history['val_loss']

epochs_range = range(epochs)

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()<jupyter_output><empty_output><jupyter_text>Mantap!<jupyter_code>fig = plt.figure(figsize = (10, 5))

xloc = ['CNN Training', 'CNN Validation',
        'BPNN Training', 'BPNN Validation']
yloc = [CNN_acc[0], CNN_acc[1],
        BPNN_acc[0], BPNN_acc[1]]
 
# creating the bar plot
mybar = plt.bar(xloc, yloc, color ='maroon', width = 0.4)

# Add counts above the two bar graphs
for rect in mybar:
    height = rect.get_height()
    plt.text(rect.get_x() + rect.get_width() / 2.0, height, f'{height:.3f}', ha='center', va='bottom')
 
plt.ylim(0.0, 1.1)
plt.xlabel("Algorithm")
plt.ylabel("Accuracy (Score)")
plt.title("CNN vs BPNN")
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13695125235658498, 'fraction_numerical': 0.018448693778615674, 'mean_word_length': 5.062857142857143, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, 'https://': 0, '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': '467905', 'license': 'MIT', 'n_tokens_mistral': 2900, 'n_tokens_neox': 2751, 'n_words': 522, 'path': 'Face Recognition (Project UAS)/Face Recognition.ipynb', 'repo_name': 'ariefsaferman/image-classification-using-CNN-BPNN', 'size': '7461'}