# This Python 3 environment comes with many helpful analytics libraries installed
# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python
# For example, here's several helpful packages to load

import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

# Input data files are available in the read-only "../input/" directory
# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory

import os
for dirname, _, filenames in os.walk('/kaggle/input'):
    for filename in filenames:
        print(os.path.join(dirname, filename))

# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using "Save & Run All" 
# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session
path='/kaggle/input/competitions/dogs-vs-cats'
import zipfile
with zipfile.ZipFile(path + "/train.zip", "r") as zip_ref:
    zip_ref.extractall(".")

import os, shutil, pathlib

original_dir = pathlib.Path("train")
new_base_dir = pathlib.Path("dogs_vs_cats_small")
def make_subset(subset_name, start_index, end_index):
    for category in ("cat", "dog"):
        dir = new_base_dir / subset_name / category
        os.makedirs(dir)
        fnames = [f"{category}.{i}.jpg" for i in range(start_index, end_index)]
        for fname in fnames:
            shutil.copyfile(src=original_dir / fname, dst=dir / fname)


make_subset("train", start_index=0, end_index=1000)
make_subset("validation", start_index=1000, end_index=1500)
make_subset("test", start_index=1500, end_index=2500)

import keras
import os, shutil, pathlib
from keras import layers
from keras import ops
import os
import numpy as np
import PIL
from PIL import Image, ImageChops
from keras.datasets import mnist
import matplotlib.image as mpimg
import matplotlib.pyplot as plt
from optree.typing import F
import tensorflow as tf
from skimage import util, io
from skimage.color import rgb2gray
from skimage import data
import sys
from keras.utils import image_dataset_from_directory



new_base_dir = pathlib.Path("dogs_vs_cats_small")


def cats_and_dogs():
    inputs = keras.Input(shape=(180, 180, 3))
    x = layers.Rescaling(1.0 / 255)(inputs)
    x = layers.Conv2D(filters=32, kernel_size=3, activation="relu")(x)
    x = layers.MaxPooling2D(pool_size=2)(x)
    x = layers.Conv2D(filters=64, kernel_size=3, activation="relu")(x)
    x = layers.MaxPooling2D(pool_size=2)(x)
    x = layers.Conv2D(filters=128, kernel_size=3, activation="relu")(x)
    x = layers.MaxPooling2D(pool_size=2)(x)
    x = layers.Conv2D(filters=256, kernel_size=3, activation="relu")(x)
    x = layers.MaxPooling2D(pool_size=2)(x)
    x = layers.Conv2D(filters=512, kernel_size=3, activation="relu")(x)
    x = layers.GlobalAveragePooling2D()(x)
    x = layers.Dropout(0.25)(x)
    outputs = layers.Dense(1, activation="sigmoid")(x)
    model = keras.Model(inputs=inputs, outputs=outputs)
    return model


#-------------------------------------------------------------------
#----------- Graficas durante training -----------------------------
class LossHistory(keras.callbacks.Callback):
    def on_train_begin(self, logs):
        self.per_batch_losses = []
        self.per_batch_val_losses = []
      
    def on_batch_end(self, batch, logs):
        self.per_batch_losses.append(logs.get("loss"))
        self.per_batch_val_losses.append(logs.get("val_loss"))
     
    

import keras_hub

conv_base = keras_hub.models.Backbone.from_preset(
        "xception_41_imagenet",
)
conv_base.trainable = True
for layer in conv_base.layers[:-4]:
    layer.trainable = False
preprocessor = keras_hub.layers.ImageConverter.from_preset(
        "xception_41_imagenet",
        image_size=(180, 180),
)

def get_features_and_labels(dataset):
    all_features = []
    all_labels = []
    for images, labels in dataset:
        preprocessed_images = preprocessor(images)
        features = conv_base.predict(preprocessed_images, verbose=0)
        all_features.append(features)
        all_labels.append(labels)
    return np.concatenate(all_features), np.concatenate(all_labels)


#-----------Data augmentation------------------
data_augmentation_layers = [
    layers.RandomFlip("horizontal"),
    layers.RandomRotation(0.1),
    layers.RandomZoom(0.2),
    ]

def data_augmentation(images, targets):
    for layer in data_augmentation_layers:
        images = layer(images)
    return images, targets
#--------------------------------------------------------------------

#-----------Lo datoooo--------------------------------------------
batch_size = 64
image_size = (180, 180)
#------------Armado de Train,Test,Validation----------------------------
train_dataset = image_dataset_from_directory(
new_base_dir / "train", image_size=image_size, batch_size=batch_size
)
validation_dataset = image_dataset_from_directory(
new_base_dir / "validation", image_size=image_size, batch_size=batch_size
)
test_dataset = image_dataset_from_directory(
new_base_dir / "test", image_size=image_size, batch_size=batch_size
)

#----------Aumentado de datos---------------------------------------
augmented_train_dataset = train_dataset.map(
    data_augmentation, num_parallel_calls=8
)

augmented_train_dataset = augmented_train_dataset.prefetch(tf.data.AUTOTUNE)
#-------------------------------------------------------------------

#train_features, train_labels = get_features_and_labels(train_dataset)
#val_features, val_labels = get_features_and_labels(validation_dataset)
#test_features, test_labels = get_features_and_labels(test_dataset)

inputs = keras.Input(shape=(180, 180, 3))
x = layers.Rescaling(1.0 / 255)(inputs)
x = layers.Conv2D(filters=32, kernel_size=5, use_bias=False)(x)
for size in [32, 64, 128, 256, 512]:
    residual = x
    x = layers.BatchNormalization()(x)
    x = layers.Activation("relu")(x)
    x = layers.SeparableConv2D(size, 3, padding="same", use_bias=False)(x)
    x = layers.BatchNormalization()(x)
    x = layers.Activation("relu")(x)
    x = layers.SeparableConv2D(size, 3, padding="same", use_bias=False)(x)
    x = layers.MaxPooling2D(3, strides=2, padding="same")(x)
    residual = layers.Conv2D(
        size, 1, strides=2, padding="same", use_bias=False
    )(residual)
    x = layers.add([x, residual])

x = layers.GlobalAveragePooling2D()(x)
x = layers.Dropout(0.5)(x)
outputs = layers.Dense(1, activation="sigmoid")(x)
model = keras.Model(inputs=inputs, outputs=outputs)

model.compile(
    loss="binary_crossentropy",
    optimizer="adam",
    metrics=["accuracy"],
)
callbacks = [
    keras.callbacks.ModelCheckpoint(
    filepath="fine_tuning.keras",
    save_best_only=True,
    monitor="val_loss",
    )
]



#.......................................................................
history = model.fit(
    augmented_train_dataset,
    epochs=100,
    validation_data=validation_dataset,
    callbacks=callbacks,
)
#...................................................................
history_dict = history.history

loss_values = history_dict["loss"]
val_loss_values = history_dict["val_loss"]
acc = history_dict["accuracy"]
val_acc = history_dict["val_accuracy"]

epochs=range(1,len(loss_values)+1)

plt.clf()
plt.plot(epochs,loss_values,"r--",label="Training loss")
plt.plot(epochs,val_loss_values,"b",label="Validation loss")

plt.title("Loss evolution during training")
plt.xlabel("Epochs")
plt.xticks(epochs)
plt.ylabel("Loss")
plt.legend()
plt.savefig("train_val_loss.pdf", format="pdf", bbox_inches="tight")
plt.show()

#---------------------------------------------------------------------------
#------------------- Validation and Training Accuracy ----------------------
plt.clf()
plt.plot(epochs,acc,"r--",label="Training acc")
plt.plot(epochs,val_acc,"b",label="Validation acc")
plt.title("Accuracy evolution during training")
plt.xlabel("Epochs")
plt.xticks(epochs)
plt.ylabel("Accuracy")
plt.legend()
plt.savefig("train_val_acc.pdf",format="pdf",bbox_inches="tight")
plt.show()
plt.clf()



floss=open('epochs.dat','a')

for i in range(len(epochs)):
      floss.write(f"{epochs[i]} {loss_values[i]} {val_loss_values[i]} {acc[i]} {val_acc[i]}\n")

floss.close


