# ---
# 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/Nutellaman/ModernRobotics/blob/master/pipeline3.ipynb" target="_parent"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/></a>

# + id="mGVxZbNpf0wl"
input_shape =300

# + id="W14JZtKDKlqx" colab={"base_uri": "https://localhost:8080/"} outputId="58b67fe1-ad55-4220-c731-606bcfa9df1d"
print("adrian esta en panga")
print("Redo of a Healer best anime of the season")
print("")
print("Space Jam")

# + colab={"base_uri": "https://localhost:8080/"} id="suNDNhCvI4Dh" outputId="e04f1830-a758-4cab-bcde-6981d1c569d7"
import os

from tensorflow.keras import layers
from tensorflow.keras import Model
# !wget --no-check-certificate \
#     https://storage.googleapis.com/mledu-datasets/inception_v3_weights_tf_dim_ordering_tf_kernels_notop.h5 \
#     -O /tmp/inception_v3_weights_tf_dim_ordering_tf_kernels_notop.h5
  
from tensorflow.keras.applications.inception_v3 import InceptionV3

local_weights_file = '/tmp/inception_v3_weights_tf_dim_ordering_tf_kernels_notop.h5'

pre_trained_model = InceptionV3(input_shape = (input_shape, input_shape, 3), 
                                include_top = False, 
                                weights = None)

pre_trained_model.load_weights(local_weights_file)

for layer in pre_trained_model.layers:
  layer.trainable = False
  
# pre_trained_model.summary()

last_layer = pre_trained_model.get_layer('mixed7')
print('last layer output shape: ', last_layer.output_shape)
last_output = last_layer.output

# + id="CgvMiM9AI8rR"
from tensorflow.keras.optimizers import RMSprop

# Flatten the output layer to 1 dimension
x = layers.Flatten()(last_output)
# Add a fully connected layer with 1,024 hidden units and ReLU activation
x = layers.Dense(1024, activation='relu')(x)
# Add a dropout rate of 0.2
x = layers.Dropout(0.2)(x)                  
# Add a final sigmoid layer for classification
x = layers.Dense  (6, activation='softmax')(x)           

model = Model( pre_trained_model.input, x) 

model.compile(optimizer = RMSprop(lr=0.0001), 
              loss = 'categorical_crossentropy', 
              metrics = ['accuracy'])

## Rocket science


# + id="azGOxVOAJCY1"
#model.summary()

# + id="qycU0IzPJoBc"
# ! pip install -q kaggle

# + colab={"resources": {"http://localhost:8080/nbextensions/google.colab/files.js": {"data": "<KEY>", "ok": true, "headers": [["content-type", "application/javascript"]], "status": 200, "status_text": ""}}, "base_uri": "https://localhost:8080/", "height": 90} id="Ss-_KSCzJrSg" outputId="b6c46d8a-e11a-419b-8290-3fcbfc9486c8"
from google.colab import files
files.upload()

# + id="rTfjLSclLNjV"
# ! mkdir ~/.kaggle
# ! cp kaggle.json ~/.kaggle/
# ! chmod 600 ~/.kaggle/kaggle.json

# + colab={"base_uri": "https://localhost:8080/"} id="EM-O0yjpJvNc" outputId="480dd385-5bb6-4d4b-9c49-5893889b8823"
# #!kaggle datasets download -d sriramr/fruits-fresh-and-rotten-for-classification

# + colab={"base_uri": "https://localhost:8080/"} id="NCx1iLrVFJAx" outputId="a3b6cd47-2701-45fd-d609-28e3fa11ae48"
# !kaggle datasets download -d kmader/skin-cancer-mnist-ham10000

# + id="mvPCbq2vM39k"
import os
import zipfile

local_zip = '/content/skin-cancer-mnist-ham10000.zip'
zip_ref = zipfile.ZipFile(local_zip, 'r')
zip_ref.extractall('/tmp')
zip_ref.close()

# + id="SPnLuUEuJE8x" colab={"base_uri": "https://localhost:8080/"} outputId="91215b05-a2d9-46be-ebf3-c38218d9ee09"
from tensorflow.keras.preprocessing.image import ImageDataGenerator
# Define our example directories and files
base_dir = '/tmp/dataset/'

train_dir = os.path.join( base_dir, 'train')
validation_dir = os.path.join( base_dir, 'test')

# Add our data-augmentation parameters to ImageDataGenerator

train_datagen = ImageDataGenerator(rescale = 1./255.,
                                   rotation_range = 40,
                                   width_shift_range = 0.2,
                                   height_shift_range = 0.2,
                                   shear_range = 0.2,
                                   zoom_range = 0.2,
                                   horizontal_flip = True)

# Note that the validation data should not be augmented!
test_datagen = ImageDataGenerator( rescale = 1.0/255. )

# Flow training images in batches of 20 using train_datagen generator
train_generator = train_datagen.flow_from_directory(train_dir,
                                                    batch_size = 20,
                                                    class_mode = 'categorical', 
                                                    target_size = (input_shape, input_shape))     

# Flow validation images in batches of 20 using test_datagen generator
validation_generator =  test_datagen.flow_from_directory( validation_dir,
                                                          batch_size  = 20,
                                                          class_mode  = 'categorical', 
                                                          target_size = (input_shape, input_shape))

# + id="fbp23IcjtDN3"
import tensorflow as tf
class myCallback(tf.keras.callbacks.Callback):
        def on_epoch_end(self, epoch, logs={}):
            if(logs.get('accuracy') >= 0.98):
                print("\nReached 98% accuracy so cancelling training!")
                self.model.stop_training = True
callbacks = myCallback()

# + id="OMzReZgpJR1o" colab={"base_uri": "https://localhost:8080/"} outputId="ba570752-d7e2-460a-d3ae-2f981ce57eb8"
history = model.fit(
            train_generator,
            validation_data = validation_generator,
            steps_per_epoch = 32,
            epochs = 100,
            callbacks=[callbacks],
            validation_steps = 32,
            verbose = 1)

# + id="HhYYUrdsJUty" colab={"base_uri": "https://localhost:8080/", "height": 298} outputId="42bbd8a9-18e7-4e2c-cab8-e42935b44bb7"
import matplotlib.pyplot as plt
acc = history.history['accuracy']
val_acc = history.history['val_accuracy']
loss = history.history['loss']
val_loss = history.history['val_loss']

epochs = range(len(acc))

plt.plot(epochs, acc, 'r', label='Training accuracy')
plt.plot(epochs, val_acc, 'b', label='Validation accuracy')
plt.title('Training and validation accuracy')
plt.legend(loc=0)
plt.figure()


plt.show()

# + colab={"base_uri": "https://localhost:8080/", "height": 562} id="spFdwlS0g6gZ" outputId="030b8d0d-4c71-415b-bb38-dbe2ca8b4ca0"
#-----------------------------------------------------------
# Retrieve a list of list results on training and test data
# sets for each training epoch
#-----------------------------------------------------------
import matplotlib.pyplot as plt
acc      = history.history[     'accuracy' ]
val_acc  = history.history[ 'val_accuracy' ]
loss     = history.history[    'loss' ]
val_loss = history.history['val_loss' ]

epochs   = range(len(acc)) # Get number of epochs

#------------------------------------------------
# Plot training and validation accuracy per epoch
#------------------------------------------------
plt.plot  ( epochs,     acc )
plt.plot  ( epochs, val_acc )
plt.title ('Training and validation accuracy')
plt.figure()

#------------------------------------------------
# Plot training and validation loss per epoch
#------------------------------------------------
plt.plot  ( epochs,     loss )
plt.plot  ( epochs, val_loss )
plt.title ('Training and validation loss'   )

# + colab={"resources": {"http://localhost:8080/nbextensions/google.colab/files.js": {"data": "<KEY>", "ok": true, "headers": [["content-type", "application/javascript"]], "status": 200, "status_text": ""}}, "base_uri": "https://localhost:8080/", "height": 126} id="_UuvIjVGflDN" outputId="330f0626-328d-424d-c8c1-bdf98d1c0187"
import numpy as np
from google.colab import files
from keras.preprocessing import image

uploaded = files.upload()

for fn in uploaded.keys():
 
  # predicting images
  path = '/content/' + fn
  img = image.load_img(path, target_size=(input_shape, input_shape))
  x = image.img_to_array(img)
  x = np.expand_dims(x, axis=0)

  images = np.vstack([x])
  classes = model.predict(images, batch_size=32)
  print(classes[0])
  # if classes[0]>0.5:
  #   print(fn + " is a dog")
  # else:
  #   print(fn + " is a cat")

# + id="n4egQ5OTwx9F"
import time
path = '/tmp/simple_keras_model'
model.save(saved_model_path)

# + colab={"base_uri": "https://localhost:8080/"} id="IX0IKXTitiin" outputId="ab3b0fb7-6ab9-4421-81de-b19c1edb0520"
new_model = tf.keras.models.load_model('/tmp/saved_models/1612553978/')

# Check its architecture
#new_model.summary()


# + id="nyqUerVSr3pq"
# Save the entire model to a HDF5 file.
# The '.h5' extension indicates that the model should be saved to HDF5.
model.save('/tmp/saved_models/versions/my_model1.h5')

# + colab={"base_uri": "https://localhost:8080/"} id="fG6Z2uxRwq-J" outputId="b1e0f09d-4745-4cd2-a27a-8b1c76123a8a"
# Recreate the exact same model, including its weights and the optimizer
new_model = tf.keras.models.load_model('/tmp/saved_models/versions/my_model1.h5')

# Show the model architecture
#new_model.summary()

StarcoderdataJupyterScriptsDedupFiltered