<jupyter_start><jupyter_text>Custom TransferLearning using EfficientNetB7<jupyter_code>import tensorflow as tf

from tensorflow.keras.applications import EfficientNetB7

from tensorflow_examples.lite.model_maker.core.data_util.image_dataloader import ImageClassifierDataLoader

import matplotlib.pyplot as plt
import numpy as np
gpus = tf.config.experimental.list_physical_devices('GPU')
if gpus:
    try:
        # Currently, memory growth needs to be the same across GPUs
        for gpu in gpus:
            tf.config.experimental.set_memory_growth(gpu, True)
        logical_gpus = tf.config.experimental.list_logical_devices('GPU')
        print(len(gpus), "Physical GPUs,", len(logical_gpus), "Logical GPUs")
    except RuntimeError as e:
        # Memory growth must be set before GPUs have been initialized
        print(e)<jupyter_output>1 Physical GPUs, 1 Logical GPUs<jupyter_text>Carga de los datos para el entrenamiento<jupyter_code>img_size = 300
batch_size = 4
path_train = 'data_300/train'
path_test_validation = 'data_300/validation'
datagen_train = tf.keras.preprocessing.image.ImageDataGenerator(rescale=1./255)
datagen_val = tf.keras.preprocessing.image.ImageDataGenerator(rescale=1./255)

train_generator = datagen_train.flow_from_directory(
    path_train,
    target_size=(img_size, img_size),
    batch_size=batch_size)

val_generator = datagen_val.flow_from_directory(
    path_test_validation,
    target_size=(img_size, img_size),
    batch_size=batch_size)<jupyter_output>Found 12000 images belonging to 5 classes.
Found 1515 images belonging to 5 classes.<jupyter_text>Mostrando los labels que van a ser entrenados<jupyter_code>for image_batch, label_batch in train_generator:
  break
image_batch.shape, label_batch.shape

labels = '\n'.join(sorted(train_generator.class_indices.keys()))
print(labels)
# numero de clases a entrenar
num_classes = len(train_generator.class_indices.keys())
base_model = EfficientNetB7(
    weights='imagenet',
    input_shape=(img_size,img_size,3),
    include_top=False,
    classes=num_classes,
    classifier_activation='softmax'
)
base_model.trainable = False
base_model.summary()<jupyter_output>Model: "efficientnetb7"
__________________________________________________________________________________________________
Layer (type)                    Output Shape         Param #     Connected to                     
==================================================================================================
input_1 (InputLayer)            [(None, 300, 300, 3) 0                                            
__________________________________________________________________________________________________
rescaling (Rescaling)           (None, 300, 300, 3)  0           input_1[0][0]                    
__________________________________________________________________________________________________
normalization (Normalization)   (None, 300, 300, 3)  7           rescaling[0][0]                  
__________________________________________________________________________________________________
stem_conv_pad (ZeroPadding2D)   (None, 301, 301, 3)  0           normalization[0][0] [...]<jupyter_text>Empezando el entrenamiento<jupyter_code>from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Dropout, Flatten, Dense, BatchNormalization, GlobalAveragePooling2D
model = tf.keras.Sequential([
  base_model,
  #GlobalAveragePooling2D(),
  Flatten(),
  Dropout(0.5),
  Dense(num_classes, activation='softmax')
])<jupyter_output><empty_output><jupyter_text>Compilamos el modelo<jupyter_code>optimizer = tf.keras.optimizers.Adam(learning_rate=0.0001) #default learning_rate = 0.001
loss = 'categorical_crossentropy'
metrics = ['accuracy']

model.compile(optimizer=optimizer, 
              loss=loss, 
              metrics=metrics)
model.summary()
epochs = 3
history = model.fit(train_generator, 
                    epochs=epochs,
                    verbose=1,
                    validation_data=val_generator)<jupyter_output>Epoch 1/3
3000/3000 [==============================] - 230s 77ms/step - loss: 5.1702 - accuracy: 0.2043 - val_loss: 7.1201 - val_accuracy: 0.1993
Epoch 2/3
 767/3000 [======>.......................] - ETA: 2:30 - loss: 5.9143 - accuracy: 0.1894<jupyter_text>Creando un gráfico para la revisión<jupyter_code>acc = history.history['accuracy']
val_acc = history.history['val_accuracy']

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

plt.figure(figsize=(8, 8))
plt.subplot(2, 1, 1)
plt.plot(acc, label='Training Accuracy')
plt.plot(val_acc, label='Validation Accuracy')
plt.legend(loc='lower right')
plt.ylabel('Accuracy')
#plt.ylim([min(plt.ylim()),1])
plt.ylim([0,1.1])
plt.title('Training and Validation Accuracy')

plt.subplot(2, 1, 2)
plt.plot(loss, label='Training Loss')
plt.plot(val_loss, label='Validation Loss')
plt.legend(loc='upper right')
plt.ylabel('Cross Entropy')
#plt.ylim([0,1.0])
plt.ylim([0,12])
plt.title('Training and Validation Loss')
plt.xlabel('epoch')
plt.show()<jupyter_output><empty_output><jupyter_text>Guardando el modelo<jupyter_code>saved_model_dir = 'model'
tf.saved_model.save(model, saved_model_dir)<jupyter_output><empty_output><jupyter_text>Convirtiendo el modelo en tensorflow lite para el uso en mobile<jupyter_code># Convert the model.
converter = tf.lite.TFLiteConverter.from_saved_model(saved_model_dir)
tflite_model = converter.convert()

path_tensorflow_lite = 'model_tflite/EfficientNetB7_50_all_parameters_trained_model.tflite'
with open(path_tensorflow_lite, 'wb') as f:
  f.write(tflite_model)<jupyter_output><empty_output><jupyter_text>Tomando 100 imágenes de validación y observando el resultado<jupyter_code>test_data = ImageClassifierDataLoader.from_folder(path_test_validation)

def get_label_color(val1, val2):
  if val1 == val2:
    return 'black'
  else:
    return 'red'

plt.figure(figsize=(20, 20))

for i, (image, label) in enumerate(test_data.dataset.take(100)):
    ax = plt.subplot(10, 10, i+1)
    plt.xticks([])
    plt.yticks([])
    plt.grid(False)
    plt.imshow(image.numpy(), cmap=plt.cm.gray)

    swapped = np.moveaxis(image, 0, 1)
    arr4d = np.expand_dims(swapped, 0)
    id_predict = np.argmax(model.predict(arr4d))
    
    predict_label = "100"
    if id_predict == 1:
        predict_label = "20"
    color = get_label_color(predict_label, test_data.index_to_label[label.numpy()])
    #color = 'black'
    ax.xaxis.label.set_color(color)
    plt.xlabel('Predicted: %s' % predict_label)
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.127792128855797, 'fraction_numerical': 0.030694423339917946, 'mean_word_length': 4.819628647214854, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 34, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '131621', 'license': 'MIT', 'n_tokens_mistral': 2299, 'n_tokens_neox': 2101, 'n_words': 400, 'path': '5. Custom_Transfer_Learning_EfficientNetB7.ipynb', 'repo_name': 'christianquicano/tflite_classification_banknotes', 'size': '6581'}