<jupyter_start><jupyter_text>Import + Checking Environment<jupyter_code>import keras as k
import tensorflow as tf
print('tensorflow: ', tf.__version__)
print('keras: ', k.__version__)

from keras.preprocessing.image import ImageDataGenerator

from keras.models import Sequential
from keras.layers import Dense, Flatten, Conv2D, AveragePooling2D

# Limit GPU Ram
gpus = tf.config.experimental.list_physical_devices('GPU')
if gpus:
  # Restrict TensorFlow to only allocate 1*X GB of memory on the first GPU
  try:
    tf.config.experimental.set_virtual_device_configuration(
        gpus[0],
        [tf.config.experimental.VirtualDeviceConfiguration(memory_limit=(1024*4))])
    logical_gpus = tf.config.experimental.list_logical_devices('GPU')
    print(len(gpus), "Physical GPUs,", len(logical_gpus), "Logical GPUs")
  except RuntimeError as e:
    # Virtual devices must be set before GPUs have been initialized
    print(e)
tf.test.is_built_with_cuda()
tf.config.list_physical_devices('GPU')
from tensorflow.python.client import device_lib
print(device_lib.list_local_devices())<jupyter_output>[name: "/device:CPU:0"
device_type: "CPU"
memory_limit: 268435456
locality {
}
incarnation: 12866530539060372514
, name: "/device:XLA_CPU:0"
device_type: "XLA_CPU"
memory_limit: 17179869184
locality {
}
incarnation: 16494238091145842728
physical_device_desc: "device: XLA_CPU device"
, name: "/device:XLA_GPU:0"
device_type: "XLA_GPU"
memory_limit: 17179869184
locality {
}
incarnation: 6917013847798718395
physical_device_desc: "device: XLA_GPU device"
, name: "/device:GPU:0"
device_type: "GPU"
memory_limit: 4294967296
locality {
  bus_id: 1
  links {
  }
}
incarnation: 3866630332494563000
physical_device_desc: "device: 0, name: GeForce GTX 1650, pci bus id: 0000:01:00.0, compute capability: 7.5"
]<jupyter_text>Preprocessing<jupyter_code># the FULL dataset

# create generator
datagen = ImageDataGenerator()

# prepare an iterators for each dataset
train_full = datagen.flow_from_directory('/home/maihai/GitHub/Fruit-Images-Dataset/train_full', 
                                          target_size=(32, 32),
                                          class_mode='categorical',
                                          shuffle=True,
                                          batch_size=8)

test_full = datagen.flow_from_directory('/home/maihai/GitHub/Fruit-Images-Dataset/test_full',
                                         target_size=(32, 32),
                                         class_mode='categorical',
                                         shuffle=True,
                                         batch_size=8)
# the SMALLER dataset

# create generator
imgGen = ImageDataGenerator()

# prepare an iterators for each dataset
train_smaller = imgGen.flow_from_directory('train_smaller', 
                                            target_size=(32, 32),
                                            class_mode='categorical',
                                            shuffle=True,
                                            batch_size=8)
imgGen
test_smaller = datagen.flow_from_directory('test_smaller',
                                            target_size=(32, 32),
                                            class_mode='categorical',
                                            shuffle=True,
                                            batch_size=8)<jupyter_output>Found 1409 images belonging to 3 classes.
Found 473 images belonging to 3 classes.<jupyter_text>The LeNet-5<jupyter_code># LeNet-5 - output 131
lenet5 = Sequential()

lenet5.add(Conv2D(filters=6, input_shape=(32, 32, 3), kernel_size=5, strides=1, activation='relu'))
lenet5.add(AveragePooling2D())

lenet5.add(Conv2D(filters=16, kernel_size=(3, 3), activation='relu'))
lenet5.add(AveragePooling2D())

lenet5.add(Flatten())

lenet5.add(Dense(units=120, activation='relu'))
lenet5.add(Dense(units=84, activation='relu'))

# Output
lenet5.add(Dense(units=131, activation = 'softmax'))

lenet5.summary()

lenet5.compile(loss='categorical_crossentropy', optimizer='adam')
%%time
lenet5.fit(train_full, steps_per_epoch=None, epochs=100)
print(lenet5.evaluate(test_full))<jupyter_output><empty_output><jupyter_text>Simple Models<jupyter_code># LeNet-5 - output 131
model = Sequential()

lenet5.add(Conv2D(filters=6, input_shape=(32, 32, 3), kernel_size=5, strides=1, activation='relu'))
lenet5.add(AveragePooling2D())

lenet5.add(Flatten())

lenet5.add(Dense(units=120, activation='relu'))
lenet5.add(Dense(units=84, activation='relu'))

# Output
lenet5.add(Dense(units=131, activation = 'softmax'))

lenet5.summary()<jupyter_output><empty_output><jupyter_text>Tensorboard<jupyter_code>from keras.callbacks import TensorBoard
import datetime # miscellanous: to name folders

log_dir = "logs/fit/" + datetime.datetime.now().strftime("%Y%m%d-%H%M%S")
tensorboard_callback = k.callbacks.TensorBoard(log_dir=log_dir)
%%time
# LeNet5 + Tensorboard + the FULL dataset
lenet5.compile(loss='categorical_crossentropy',
               optimizer='adam',
               metrics=[k.metrics.CategoricalAccuracy(), # Using 2 metrics
                        k.metrics.AUC()])

lenet5.fit(train_full, 
           validation_data=test_full,
           steps_per_epoch=None, epochs=15,
           callbacks=[tensorboard_callback])
%%time
print(lenet5.evaluate(test_full))<jupyter_output>2836/2836 [==============================] - 9s 3ms/step
[0.0, 0.8846967816352844, 0.9428174495697021]
CPU times: user 32.5 s, sys: 1.46 s, total: 33.9 s
Wall time: 9.31 s
{'fraction_non_alphanumeric': 0.11521739130434783, 'fraction_numerical': 0.057427536231884055, 'mean_word_length': 3.1794095382286147, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 32, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '573775', 'license': 'MIT', 'n_tokens_mistral': 1953, 'n_tokens_neox': 1741, 'n_words': 326, 'path': '4_Fruit_classification/ThuyetTrinhTrenLop_LeNet5.ipynb', 'repo_name': 'maisonhai3/Portfolio', 'size': '5520'}