# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     name: python3
# ---

# + [markdown] id="view-in-github" colab_type="text"
# <a href="https://colab.research.google.com/github/tharushaRukshanRavindra/SDGP/blob/main/Copy_of_mock_DATASET_JS.ipynb" target="_parent"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/></a>

# + id="y8tkP1OxeVM0" colab={"base_uri": "https://localhost:8080/"} outputId="5eceed00-e6b3-46fc-9327-157b91f4a589"
import matplotlib.pyplot as plt
import numpy as np
import os
import PIL
import tensorflow as tf
print(tf. __version__)
from glob import glob
import pathlib


#Importing keras

from tensorflow import keras
from keras import layers, callbacks, utils, applications, optimizers
from keras.models import Sequential, Model, load_model
import cv2

# + colab={"base_uri": "https://localhost:8080/"} id="M5j3yjoRK9Gv" outputId="eb5571fb-b685-4703-9771-0d869164ded7"
# # !pip install tflite-support
# !pip install tensorflowjs

# + id="QogpYNs0tAth" colab={"base_uri": "https://localhost:8080/"} outputId="17b9b604-a474-465e-edfb-3864aa12cc30"
from google.colab import drive
drive.mount('/content/drive')

# + id="rKI0CP0ojJkI"
#giving the path of the data set main data set ps-not to the sub directories
image_path = "/content/drive/MyDrive/MOCK_DATA/"

# + id="td8F0cjijjFg" colab={"base_uri": "https://localhost:8080/"} outputId="8cdc3930-735c-455b-de86-651748941bb3"
a_path = "/content/drive/MyDrive/MOCK_DATA/A"
a_length = len(list(glob(a_path + "/*.jpg")))
print(a_length)

# + id="-ca5buc67nQW" colab={"base_uri": "https://localhost:8080/", "height": 217} outputId="79461dd9-7eb4-4622-99ea-14214458b76a"
A = list(glob(a_path + "/*.jpg"))
PIL.Image.open(str(A[0]))

# + id="NlPFnc0cwxbK" colab={"base_uri": "https://localhost:8080/"} outputId="6891e219-7fce-4894-d517-f2e569e72b90"
#Image shape 
image = cv2.imread(A[10])
print(image.shape)

# + id="sGLC_aXCj93H" colab={"base_uri": "https://localhost:8080/"} outputId="29998724-d786-4ac7-94ef-16042ab0dc0d"
all_images = len(list(glob(image_path + "*/*.jpg")))
print(all_images)

# + id="DJWLOS_F82AV"
batch_size = 32
img_height = 96
img_width = 96

# + id="Vuv-b3GVNnRq" colab={"base_uri": "https://localhost:8080/"} outputId="b451ffee-5e22-4fd4-dbb0-50e70616274f"
#80% trainnin data
train_ds = tf.keras.utils.image_dataset_from_directory(
  image_path,
  validation_split=0.2,
  subset="training",
  seed=123,
  image_size=(img_height, img_width),
  batch_size=batch_size)

# + id="DcsmGjnXNyZf" colab={"base_uri": "https://localhost:8080/"} outputId="8ff5f28d-44e7-4dc6-e20a-9138177b4585"
#20 training
val_ds = tf.keras.utils.image_dataset_from_directory(
  image_path,
  validation_split=0.2,
  subset="validation",
  seed=123,
  image_size=(img_height, img_width),
  batch_size=batch_size)

# + id="3MWaBHU0N-xG" colab={"base_uri": "https://localhost:8080/"} outputId="bd470d3c-e918-4b60-8eeb-caec97d1037c"
#to confirm the classes name
class_names = train_ds.class_names
size = (len(list(class_names)))
print(size)
print((list(class_names)))

# + id="2aXF4LHw3Eg5"
f= open("Labels.txt","w+")
for i in range(size):
     f.write(class_names[i] + "\n")
f.close()

# + id="1BLwbRKQeQa5" colab={"base_uri": "https://localhost:8080/"} outputId="2f74bedb-a8c9-43a5-ff76-ae1e00ccd28c"
for image_batch, labels_batch in train_ds:
  print(image_batch.shape)
  print(labels_batch.shape)
  break

# + id="8GEOTUdA7TYM"
data_augmentation = keras.Sequential(
  [
    layers.RandomFlip("horizontal",
                      input_shape=(img_height,
                                  img_width,
                                  3)),
    layers.RandomRotation(0.1),
    layers.RandomZoom(0.1),
  ]
)


# + id="0uKiph5p8BxM" colab={"base_uri": "https://localhost:8080/", "height": 632} outputId="72fb4727-d241-4650-9951-c311944f0da0"
plt.figure(figsize=(10, 10))
for images, _ in train_ds.take(2):
  for i in range(9):
    augmented_images = data_augmentation(images)
    ax = plt.subplot(3, 3, i + 1)
    plt.imshow(augmented_images[0].numpy().astype("uint8"))
    plt.axis("off")

# + id="gnviydXxmNk1"
num_classes = len(class_names)

model = Sequential([
  data_augmentation,
  layers.Rescaling(1./255),
  layers.Conv2D(16, (3, 3),  strides=(1, 1),  padding="same", activation="relu"),
  layers.MaxPooling2D(),
  layers.Conv2D(32, (3, 3),  strides=(1, 1),  padding="same", activation="relu"),
  layers.MaxPooling2D(),
  layers.Conv2D(64, (3, 3),  strides=(1, 1),  padding="same", activation="relu"),
  layers.MaxPooling2D(),
  layers.Dropout(0.2),
  layers.Flatten(),
  layers.Dense(128, activation='relu'),
  layers.Dense(num_classes)
])

#https://www.tensorflow.org/tutorials/images/classification#compile_the_model
#https://colab.research.google.com/github/tensorflow/docs/blob/master/site/en/tutorials/images/classification.ipynb#scrollTo=SbtTDYhOHZb6
# https://machinelearningmastery.com/pooling-layers-for-convolutional-neural-networks/

# + colab={"base_uri": "https://localhost:8080/"} id="fI3qztmp46pL" outputId="a1cc714f-3d12-446f-eb6b-f649c4f4fb61"
model.summary()

# + id="jb1uYefoSnqW"
model.compile(optimizer='adam',
              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
              metrics=['accuracy'])

# + id="Ml4jxPP3S7WZ" colab={"base_uri": "https://localhost:8080/"} outputId="e09986d1-8e18-4951-bb7d-b1125424f51d"
epochs=5
history = model.fit(
  train_ds,
  validation_data=val_ds,
  epochs=epochs
)

# + id="aYxail-qTwGX" colab={"base_uri": "https://localhost:8080/", "height": 499} outputId="1129037d-e811-4256-be7d-cb349c115606"
acc = history.history['accuracy']
val_acc = history.history['val_accuracy']

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()

# + id="PXc8E3CfUZV-"
import tensorflowjs as tfjs
tfjs.converters.save_keras_model(model, "mockModel" )

# + id="tpJBYHeYBkhA"


# + id="9UrXivFKWJeb" colab={"base_uri": "https://localhost:8080/"} outputId="2cec3e65-9336-4771-a81b-2f45b6c13b53"
# TF_LITE_MODEL_FILE_NAME = "tf_lite_model.tflite"
# tf_lite_converter = tf.lite.TFLiteConverter.from_keras_model(model)
# tflite_model = tf_lite_converter.convert()
# tflite_model_name = TF_LITE_MODEL_FILE_NAME
# open(tflite_model_name, "wb").write(tflite_model)


# + [markdown] id="youzk6H8Uvo2"
# Adding meta data into the tflite model

# + id="l1MKEjBXKAcr"
# from tflite_support import flatbuffers
# from tflite_support import metadata as _metadata
# from tflite_support import metadata_schema_py_generated as _metadata_fb

# + id="XlDGzsYBKYOX"
# # Creates model info.
# model_meta = _metadata_fb.ModelMetadataT()
# model_meta.name = "sign classification for 5 letters"
# model_meta.description = ("Identify the most prominent object in the "
#                           "image from a set of 5 categories such as "
#                           "A, B, C, D,E ")
# model_meta.version = "v1"
# model_meta.author = "TensorFlow"
# model_meta.license = ("Apache License. Version 2.0 "
#                       "http://www.apache.org/licenses/LICENSE-2.0.")

# + id="2wfpl35IKnbM"
# # Creates input info.
# input_meta = _metadata_fb.TensorMetadataT()

# # Creates output info.
# output_meta = _metadata_fb.TensorMetadataT()

# + id="b-eBTElZ-tzf"
# input_meta.name = "image"
# input_meta.description = (
#     "Input image to be classified. The expected image is {0} x {1}, with "
#     "three channels (red, blue, and green) per pixel. Each value in the "
#     "tensor is a single byte between 0 and 1".format(96, 96))
# input_meta.content = _metadata_fb.ContentT()
# input_meta.content.contentProperties = _metadata_fb.ImagePropertiesT()
# input_meta.content.contentProperties.colorSpace = (
#     _metadata_fb.ColorSpaceType.RGB)
# input_meta.content.contentPropertiesType = (
#     _metadata_fb.ContentProperties.ImageProperties)
# input_normalization = _metadata_fb.ProcessUnitT()
# input_normalization.optionsType = (
#     _metadata_fb.ProcessUnitOptions.NormalizationOptions)
# input_normalization.options = _metadata_fb.NormalizationOptionsT()
# input_normalization.options.mean = [96]
# input_normalization.options.std = [96]
# input_meta.processUnits = [input_normalization]
# input_stats = _metadata_fb.StatsT()
# input_stats.max = [1]
# input_stats.min = [0]
# input_meta.stats = input_stats

# + id="n0wXxYlo-81V"
# # Creates output info.
# output_meta = _metadata_fb.TensorMetadataT()
# output_meta.name = "probability"
# output_meta.description = "Probabilities of the 5 labels respectively."
# output_meta.content = _metadata_fb.ContentT()
# output_meta.content.content_properties = _metadata_fb.FeaturePropertiesT()
# output_meta.content.contentPropertiesType = (
#     _metadata_fb.ContentProperties.FeatureProperties)
# output_stats = _metadata_fb.StatsT()
# output_stats.max = [1.0]
# output_stats.min = [0.0]
# output_meta.stats = output_stats
# label_file = _metadata_fb.AssociatedFileT()
# label_file.name = os.path.basename("/content/Labels.txt")
# label_file.description = "Labels for objects that the model can recognize."
# label_file.type = _metadata_fb.AssociatedFileType.TENSOR_AXIS_LABELS
# output_meta.associatedFiles = [label_file]

# + id="3ZX-jJee_O3i"
# # Creates subgraph info.
# subgraph = _metadata_fb.SubGraphMetadataT()
# subgraph.inputTensorMetadata = [input_meta]
# subgraph.outputTensorMetadata = [output_meta]
# model_meta.subgraphMetadata = [subgraph]

# b = flatbuffers.Builder(0)
# b.Finish(
#     model_meta.Pack(b),
#     _metadata.MetadataPopulator.METADATA_FILE_IDENTIFIER)
# metadata_buf = b.Output()

# + id="Iedu0vM6_QsL"
# populator = _metadata.MetadataPopulator.with_model_file("/content/tf_lite_model.tflite")
# populator.load_metadata_buffer(metadata_buf)
# populator.load_associated_files(["/content/Labels.txt"])
# populator.populate()

# + colab={"base_uri": "https://localhost:8080/"} id="9qYtc2dS5j7n" outputId="85d62cb7-90ba-485e-adbb-7f18167c56c9"
# interpreter = tf.lite.Interpreter(model_path = TF_LITE_MODEL_FILE_NAME)
# input_details = interpreter.get_input_details()
# output_details = interpreter.get_output_details()
# print("Input Shape:", input_details[0]['shape'])
# print("Input Type:", input_details[0]['dtype'])
# print("Output Shape:", output_details[0]['shape'])
# print("Output Type:", output_details[0]['dtype'])

# + [markdown] id="ubM-TYwZVRXj"
# Input is expected in 96 * 96 RGB

# + id="3-s2mwqZk3fQ"



# + [markdown] id="qa7wnVWYVQes"
#

# + id="P7lWby49UiK_"


StarcoderdataJupyterScriptsDedupFiltered