Skip to content
Snippets Groups Projects
Commit e3b141ee authored by Gabriel Falk's avatar Gabriel Falk
Browse files

Replace LeNet-5-Keras.ipynb

parent cacae355
No related branches found
No related tags found
No related merge requests found
{
"cells": [
{
"cell_type": "code",
"execution_count": 2,
"id": "ab5b8f55",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"2022-11-21 13:55:12.803035: I tensorflow/core/platform/cpu_feature_guard.cc:193] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations: AVX2 FMA\n",
"To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.\n",
"2022-11-21 13:55:13.455822: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libcudart.so.11.0'; dlerror: libcudart.so.11.0: cannot open shared object file: No such file or directory\n",
"2022-11-21 13:55:13.455839: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.\n",
"2022-11-21 13:55:13.531867: E tensorflow/stream_executor/cuda/cuda_blas.cc:2981] Unable to register cuBLAS factory: Attempting to register factory for plugin cuBLAS when one has already been registered\n",
"2022-11-21 13:55:15.035232: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer.so.7'; dlerror: libnvinfer.so.7: cannot open shared object file: No such file or directory\n",
"2022-11-21 13:55:15.035379: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer_plugin.so.7'; dlerror: libnvinfer_plugin.so.7: cannot open shared object file: No such file or directory\n",
"2022-11-21 13:55:15.035388: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Cannot dlopen some TensorRT libraries. If you would like to use Nvidia GPU with TensorRT, please make sure the missing libraries mentioned above are installed properly.\n"
]
}
],
"source": [
"from tensorflow.keras.datasets import mnist\n",
"from tensorflow.keras import layers\n",
"from tensorflow import keras"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "840ed4ea",
"metadata": {},
"outputs": [],
"source": [
"def CNN():\n",
" model = keras.Sequential()\n",
" \n",
" model.add(layers.Conv2D(\n",
" filters=6,\n",
" kernel_size=(5, 5),\n",
" activation='relu',\n",
" padding='valid'\n",
" ))\n",
" \n",
" model.add(layers.MaxPooling2D())\n",
" \n",
" model.add(layers.Conv2D(\n",
" filters=16,\n",
" kernel_size=(5, 5),\n",
" activation='relu',\n",
" padding='valid'\n",
" ))\n",
" \n",
" model.add(layers.MaxPooling2D())\n",
" \n",
" model.add(layers.Flatten())\n",
" \n",
" model.add(layers.Dense(\n",
" units=120,\n",
" activation='relu'\n",
" ))\n",
" \n",
" model.add(layers.Dense(\n",
" units=84,\n",
" activation='relu'\n",
" ))\n",
" \n",
" model.add(layers.Dense(\n",
" units=10,\n",
" activation='softmax'\n",
" ))\n",
" \n",
" model.compile(\n",
" optimizer='adam',\n",
" loss='categorical_crossentropy',\n",
" metrics=['accuracy']\n",
" )\n",
" model.fit(\n",
" train_images, train_labels,\n",
" epochs=10,\n",
" batch_size=128 \n",
" )\n",
" results = model.evaluate(test_images, test_labels)\n",
" model.save('model')\n",
" model.save_weights('weights.h5')\n"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "b25a29c6",
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"2022-11-21 13:55:22.825403: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libcuda.so.1'; dlerror: libcuda.so.1: cannot open shared object file: No such file or directory\n",
"2022-11-21 13:55:22.825842: W tensorflow/stream_executor/cuda/cuda_driver.cc:263] failed call to cuInit: UNKNOWN ERROR (303)\n",
"2022-11-21 13:55:22.825876: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:156] kernel driver does not appear to be running on this host (pop-os): /proc/driver/nvidia/version does not exist\n",
"2022-11-21 13:55:22.826864: I tensorflow/core/platform/cpu_feature_guard.cc:193] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations: AVX2 FMA\n",
"To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Epoch 1/10\n",
"469/469 [==============================] - 7s 14ms/step - loss: 0.3167 - accuracy: 0.9061\n",
"Epoch 2/10\n",
"469/469 [==============================] - 6s 13ms/step - loss: 0.0950 - accuracy: 0.9712\n",
"Epoch 3/10\n",
"469/469 [==============================] - 6s 14ms/step - loss: 0.0697 - accuracy: 0.9786\n",
"Epoch 4/10\n",
"469/469 [==============================] - 6s 14ms/step - loss: 0.0558 - accuracy: 0.9829\n",
"Epoch 5/10\n",
"469/469 [==============================] - 6s 14ms/step - loss: 0.0464 - accuracy: 0.9858\n",
"Epoch 6/10\n",
"469/469 [==============================] - 7s 14ms/step - loss: 0.0395 - accuracy: 0.9876\n",
"Epoch 7/10\n",
"469/469 [==============================] - 7s 14ms/step - loss: 0.0343 - accuracy: 0.9889\n",
"Epoch 8/10\n",
"469/469 [==============================] - 7s 14ms/step - loss: 0.0297 - accuracy: 0.9908\n",
"Epoch 9/10\n",
"469/469 [==============================] - 7s 15ms/step - loss: 0.0257 - accuracy: 0.9918\n",
"Epoch 10/10\n",
"469/469 [==============================] - 6s 13ms/step - loss: 0.0233 - accuracy: 0.9923\n",
"313/313 [==============================] - 1s 2ms/step - loss: 0.0331 - accuracy: 0.9897\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"WARNING:absl:Found untraced functions such as _jit_compiled_convolution_op, _jit_compiled_convolution_op while saving (showing 2 of 2). These functions will not be directly callable after loading.\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"INFO:tensorflow:Assets written to: model/assets\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"INFO:tensorflow:Assets written to: model/assets\n"
]
}
],
"source": [
"(train_images, train_labels), (test_images, test_labels) = mnist.load_data()\n",
"\n",
"train_images = train_images.reshape((60_000, 28, 28, 1)).astype(\"float32\") / 255\n",
"test_images = test_images.reshape((10_000, 28, 28, 1)).astype(\"float32\") / 255\n",
"\n",
"\n",
"train_labels = keras.utils.to_categorical(train_labels)\n",
"test_labels = keras.utils.to_categorical(test_labels)\n",
"\n",
"CNN()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "radl_env",
"language": "python",
"name": "radl_env"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.6"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
import tensorflow as tf
import tf2onnx
import onnx
from keras.datasets import mnist
from keras.utils import to_categorical
from keras import layers
from keras import models
(train_images, train_labels), (test_images, test_labels) = mnist.load_data()
train_images = train_images.reshape((60000, 28, 28, 1))
train_images = train_images.astype('float32')/255
test_images = test_images.reshape((10000, 28, 28, 1))
test_images = test_images.astype('float32')/255
train_labels = to_categorical(train_labels)
test_labels = to_categorical(test_labels)
# Images from 28x28 to 32x32
train_images = tf.pad(train_images, ((0, 0), (2, 2), (2, 2), (0, 0)), 'constant')
test_images = tf.pad(test_images, ((0, 0), (2, 2), (2, 2), (0, 0)), 'constant')
# LeNet5 Graph
model = models.Sequential()
model.add(layers.Conv2D(6, (5, 5), activation='tanh', input_shape=(32, 32, 1))) # Layer 1
model.add(layers.AvgPool2D((2, 2))) # Layer 2
model.add(layers.Conv2D(16, (5, 5), activation='tanh', input_shape=(14, 14, 6))) # Layer 3
model.add(layers.AvgPool2D((2, 2))) # Layer 4
model.add(layers.Conv2D(120, (5, 5), activation='tanh', input_shape=(5, 5, 16))) # Layer 5
model.add(layers.Flatten()) # Layer 6
model.add(layers.Dense(84, activation='tanh')) # Layer 7
model.add(layers.Dense(10, activation='softmax'))
model.summary()
model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy'])
model.fit(train_images, train_labels, epochs=1, batch_size=128)
model.evaluate(test_images, test_labels)
"""input_signature = [tf.TensorSpec((None, 32, 32, 1), tf.float32, name='x')]
onnx_model, _ = tf2onnx.convert.from_keras(model, input_signature, opset=13)
onnx.save(onnx_model, "C:/Users/gabri/Desktop/RADL/model.onnx")"""
first_Conv_weights = model.layers[7].get_weights()[0]
print(first_Conv_weights)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment