# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3.8.0 64-bit
#     language: python
#     name: python3
# ---

# +
import pandas as pd
import seaborn as sns
import numpy as np

pets_df = pd.read_csv('../train.csv')

caminhos = []
names = []
for ii in range(0, len(pets_df)):
    caminho = '../train/'+pets_df.Id[ii]+'.jpg'
    name = pets_df.Id[ii]+'.jpg'
    caminhos.append(caminho)
    names.append(name)

pets_df['full_path'] = caminho
pets_df['file_name'] = names
pets_df = pets_df.rename(columns={"Pawpularity": "label"})
pets_df.label = pets_df.label
# /100
pets_df

# +
bin_lbls = ['10','20','30','40','50','60','70','80','90','100']
lbls = pd.qcut(pets_df['label'], q= 10,labels=bin_lbls)
lbls.head(10)

df2 = pets_df
df2.label = lbls
df2

# +
from imblearn.under_sampling import RandomUnderSampler 

y = df2.label
X = df2.drop('label',axis = 1)
rus = RandomUnderSampler(random_state=42)
X_res, y_res = rus.fit_resample(X, y)
# print(X_res)
sns.histplot(data = y_res)
# -

newdf = X_res
newdf['label'] = y_res
newdf

newdf.label.dtype
# print(pets_df.label.describe())
# pets_df.label.hist()

# +
# https://keras.io/examples/vision/image_classification_efficientnet_fine_tuning/
from tensorflow.keras.applications import EfficientNetB0
import tensorflow as tf

tf.keras.backend.clear_session()

IMG_SIZE = 224
size = (IMG_SIZE, IMG_SIZE)
batch_size =64

import tensorflow.keras.preprocessing.image as image
# datagen = image.ImageDataGenerator(rescale=1/255, validation_split=0.3)
datagen = image.ImageDataGenerator(validation_split=0.3)
train_generator = datagen.flow_from_dataframe(dataframe=newdf, 
                                              directory= '../train',
                                              #again not sure what's up with directories, took awhile to figure out I needed that ending /images
                                             x_col='file_name',
                                             y_col='label',
                                             target_size=(224,224),
                                             class_mode='categorical',
                                             batch_size=batch_size,
                                             subset='training'
                                             )   
                                             
from tensorflow.keras.models import Sequential
from tensorflow.keras import layers

img_augmentation = Sequential(
    [
        layers.RandomRotation(factor=0.15),
        layers.RandomTranslation(height_factor=0.1, width_factor=0.1),
        layers.RandomFlip(),
        layers.RandomContrast(factor=0.1),
    ],
    name="img_augmentation",
)

from tensorflow.keras.applications import EfficientNetB0

NUM_CLASSES = 10
def build_model(num_classes):
    inputs = layers.Input(shape=(IMG_SIZE, IMG_SIZE, 3))
    x = img_augmentation(inputs)
    model = EfficientNetB0(include_top=False, input_tensor=x, weights="imagenet")

    # Freeze the pretrained weights
    model.trainable = False

    # Rebuild top
    x = layers.GlobalAveragePooling2D(name="avg_pool")(model.output)
    x = layers.BatchNormalization()(x)

    top_dropout_rate = 0.2
    x = layers.Dropout(top_dropout_rate, name="top_dropout")(x)
    outputs = layers.Dense(NUM_CLASSES, activation="softmax", name="pred")(x)

    # Compile
    model = tf.keras.Model(inputs, outputs, name="EfficientNet")
    optimizer = tf.keras.optimizers.Adam(learning_rate=1e-2)
    model.compile(
        optimizer=optimizer, loss="categorical_crossentropy", metrics=["accuracy"]
    )
    return model

model = build_model(num_classes=NUM_CLASSES)

# model.summary()

# ds_train = train_generator.map(lambda image, label: (tf.image.resize(image, size), label))

# print(tf.config.experimental.get_memory_usage("GPU:0"))

epochs = 40
hist = model.fit(train_generator, epochs=epochs, verbose=1)#, validation_data=ds_test, verbose=2)

# +
import matplotlib.pyplot as plt


def plot_hist(hist):
    plt.plot(hist.history["accuracy"])
    # plt.plot(hist.history["val_accuracy"])
    plt.title("model accuracy")
    plt.ylabel("accuracy")
    plt.xlabel("epoch")
    plt.legend(["train", "validation"], loc="upper left")
    plt.show()
    
plot_hist(hist)

StarcoderdataJupyterScriptsDedupFiltered