Skip to content

Instantly share code, notes, and snippets.

@kiransair
Created March 5, 2024 06:53
Show Gist options
  • Save kiransair/b98dadd179bac57ac54790e8c3a49360 to your computer and use it in GitHub Desktop.
Save kiransair/b98dadd179bac57ac54790e8c3a49360 to your computer and use it in GitHub Desktop.
TF_Forum_22662.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": [],
"authorship_tag": "ABX9TyO/x91DT4wW0PJttERZzyCZ",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/kiransair/b98dadd179bac57ac54790e8c3a49360/tf_forum_22662.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"id": "l81mRrEbV4K-"
},
"outputs": [],
"source": [
"import tensorflow as tf\n",
"from tensorflow import keras\n",
"import numpy as np"
]
},
{
"cell_type": "code",
"source": [
"mnist=keras.datasets.mnist.load_data()"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "mhW11bqSV-Jt",
"outputId": "b9206e6a-a686-4faf-e6b8-a65b6f44c756"
},
"execution_count": 2,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/mnist.npz\n",
"11490434/11490434 [==============================] - 0s 0us/step\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"(x_train,y_train),(x_test,y_test)=mnist"
],
"metadata": {
"id": "kBPL_fWYWAcv"
},
"execution_count": 3,
"outputs": []
},
{
"cell_type": "code",
"source": [
"x_train=x_train.astype('float32')/255\n",
"x_test=x_test.astype('float32')/255"
],
"metadata": {
"id": "D_bAdC9CWCTu"
},
"execution_count": 4,
"outputs": []
},
{
"cell_type": "code",
"source": [
"y_train=keras.utils.to_categorical(y_train,10)\n",
"y_test=keras.utils.to_categorical(y_test,10)"
],
"metadata": {
"id": "_ofWINugWEJd"
},
"execution_count": 5,
"outputs": []
},
{
"cell_type": "code",
"source": [
"model=keras.Sequential([\n",
" keras.Input(shape=(28,28,1)),\n",
" keras.layers.Conv2D(32,kernel_size=(3,3),activation='relu',),\n",
" keras.layers.MaxPooling2D(pool_size=(2,2)),\n",
" keras.layers.Conv2D(64,kernel_size=(3,3),activation='relu'),\n",
" keras.layers.MaxPooling2D(pool_size=(2,2)),\n",
" keras.layers.GlobalAveragePooling2D(),\n",
" keras.layers.Dropout(0.5),\n",
" keras.layers.Dense(10,activation='softmax')\n",
"])"
],
"metadata": {
"id": "cfekXdZsWFme"
},
"execution_count": 6,
"outputs": []
},
{
"cell_type": "code",
"source": [
"optimizer=tf.keras.optimizers.Adam()"
],
"metadata": {
"id": "jprJx9SCWHQn"
},
"execution_count": 7,
"outputs": []
},
{
"cell_type": "code",
"source": [
"model.compile(loss=\"categorical_crossentropy\", optimizer=optimizer,metrics=['accuracy'])"
],
"metadata": {
"id": "NFUzgnf7WJAk"
},
"execution_count": 8,
"outputs": []
},
{
"cell_type": "code",
"source": [
"model.fit(x_train,y_train,epochs=2)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "2oy2szfAWL21",
"outputId": "2b098663-4887-44f9-f666-ceef1974590a"
},
"execution_count": 9,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Epoch 1/2\n",
"1875/1875 [==============================] - 49s 26ms/step - loss: 1.3896 - accuracy: 0.5195\n",
"Epoch 2/2\n",
"1875/1875 [==============================] - 46s 25ms/step - loss: 0.7924 - accuracy: 0.7463\n"
]
},
{
"output_type": "execute_result",
"data": {
"text/plain": [
"<keras.src.callbacks.History at 0x7f5949151930>"
]
},
"metadata": {},
"execution_count": 9
}
]
},
{
"cell_type": "code",
"source": [
"tf.keras.saving.save_model(model,'./model')"
],
"metadata": {
"id": "XTarUed7WQXb"
},
"execution_count": 10,
"outputs": []
},
{
"cell_type": "code",
"source": [
"!tflite_convert --saved_model_dir='/content/model' --graph_def_file='/content/model/saved_model.pb' --output_file='model1_clpb.tflite' --input_shapes=1,28,28,1 --input_arrays='input' --output_arrays='Softmax'"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "vMJYAzzyWTGr",
"outputId": "5dd99272-e1a8-4bb9-8fe1-3ee5d56d805b"
},
"execution_count": 11,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"2024-03-05 06:44:28.136040: E external/local_xla/xla/stream_executor/cuda/cuda_dnn.cc:9261] Unable to register cuDNN factory: Attempting to register factory for plugin cuDNN when one has already been registered\n",
"2024-03-05 06:44:28.136114: E external/local_xla/xla/stream_executor/cuda/cuda_fft.cc:607] Unable to register cuFFT factory: Attempting to register factory for plugin cuFFT when one has already been registered\n",
"2024-03-05 06:44:28.138076: E external/local_xla/xla/stream_executor/cuda/cuda_blas.cc:1515] Unable to register cuBLAS factory: Attempting to register factory for plugin cuBLAS when one has already been registered\n",
"2024-03-05 06:44:29.398578: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Could not find TensorRT\n",
"2024-03-05 06:44:31.594119: W tensorflow/compiler/mlir/lite/python/tf_tfl_flatbuffer_helpers.cc:378] Ignored output_format.\n",
"2024-03-05 06:44:31.594191: W tensorflow/compiler/mlir/lite/python/tf_tfl_flatbuffer_helpers.cc:381] Ignored drop_control_dependency.\n",
"Summary on the non-converted ops:\n",
"---------------------------------\n",
" * Accepted dialects: tfl, builtin, func\n",
" * Non-Converted Ops: 7, Total Ops 17, % non-converted = 41.18 %\n",
" * 7 ARITH ops\n",
"\n",
"- arith.constant: 7 occurrences (f32: 6, i32: 1)\n",
"\n",
"\n",
"\n",
" (f32: 2)\n",
" (f32: 1)\n",
" (f32: 2)\n",
" (f32: 1)\n",
" (f32: 1)\n"
]
}
]
},
{
"cell_type": "code",
"source": [],
"metadata": {
"id": "ht7IE0z1WcXr"
},
"execution_count": null,
"outputs": []
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment