{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Original Numpy array:\n",
      "[[1. 2. 3.]\n",
      " [4. 5. 6.]]\n",
      "\n"
     ]
    }
   ],
   "source": [
    "import numpy\n",
    "import onnx\n",
    "import os\n",
    "from onnx import numpy_helper\n",
    "\n",
    "\n",
    "# Preprocessing: create a Numpy array\n",
    "numpy_array = numpy.array([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]], dtype=float)\n",
    "print(\"Original Numpy array:\\n{}\\n\".format(numpy.array2string(numpy_array)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "TensorProto:\n",
      "dims: 2\n",
      "dims: 3\n",
      "data_type: 11\n",
      "raw_data: \"\\000\\000\\000\\000\\000\\000\\360?\\000\\000\\000\\000\\000\\000\\000@\\000\\000\\000\\000\\000\\000\\010@\\000\\000\\000\\000\\000\\000\\020@\\000\\000\\000\\000\\000\\000\\024@\\000\\000\\000\\000\\000\\000\\030@\"\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# Convert the Numpy array to a TensorProto\n",
    "tensor = numpy_helper.from_array(numpy_array)\n",
    "print(\"TensorProto:\\n{}\".format(tensor))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "After round trip, Numpy array:\n",
      "[[1. 2. 3.]\n",
      " [4. 5. 6.]]\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# Convert the TensorProto to a Numpy array\n",
    "new_array = numpy_helper.to_array(tensor)\n",
    "print(\"After round trip, Numpy array:\\n{}\\n\".format(numpy.array2string(numpy_array)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Save the TensorProto\n",
    "with open(os.path.join(\"resources\", \"tensor.pb\"), \"wb\") as f:\n",
    "    f.write(tensor.SerializeToString())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "After saving and loading, new TensorProto:\n",
      "dims: 2\n",
      "dims: 3\n",
      "data_type: 11\n",
      "raw_data: \"\\000\\000\\000\\000\\000\\000\\360?\\000\\000\\000\\000\\000\\000\\000@\\000\\000\\000\\000\\000\\000\\010@\\000\\000\\000\\000\\000\\000\\020@\\000\\000\\000\\000\\000\\000\\024@\\000\\000\\000\\000\\000\\000\\030@\"\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# Load the TensorProto\n",
    "new_tensor = onnx.TensorProto()\n",
    "with open(os.path.join(\"resources\", \"tensor.pb\"), \"rb\") as f:\n",
    "    new_tensor.ParseFromString(f.read())\n",
    "print(\"After saving and loading, new TensorProto:\\n{}\".format(new_tensor))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "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.9.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
