widzenie-komputerowe-projekt/graph.ipynb

378 lines
627 KiB
Plaintext
Raw Normal View History

2023-01-31 19:30:04 +01:00
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import tensorflow as tf\n",
"from tensorflow.python.framework.convert_to_constants import convert_variables_to_constants_v2"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"model = tf.keras.models.load_model('./model')"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'./frozen_models\\\\frozen_graph.pb'"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Convert Keras model to ConcreteFunction\n",
"full_model = tf.function(lambda x: model(x))\n",
"full_model = full_model.get_concrete_function(\n",
" tf.TensorSpec(model.inputs[0].shape, model.inputs[0].dtype))\n",
"\n",
"# Get frozen ConcreteFunction\n",
"frozen_func = convert_variables_to_constants_v2(full_model)\n",
"frozen_func.graph.as_graph_def()\n",
"\n",
"\n",
"# Save frozen graph from frozen ConcreteFunction to hard drive\n",
"tf.io.write_graph(graph_or_graph_def=frozen_func.graph,\n",
" logdir=\"./frozen_models\",\n",
" name=\"frozen_graph.pb\",\n",
" as_text=False)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"def wrap_frozen_graph(graph_def, inputs, outputs, print_graph=False):\n",
" def _imports_graph_def():\n",
" tf.compat.v1.import_graph_def(graph_def, name=\"\")\n",
"\n",
" wrapped_import = tf.compat.v1.wrap_function(_imports_graph_def, [])\n",
" import_graph = wrapped_import.graph\n",
"\n",
" if print_graph == True:\n",
" print(\"-\" * 50)\n",
" print(\"Frozen model layers: \")\n",
" layers = [op.name for op in import_graph.get_operations()]\n",
" for layer in layers:\n",
" print(layer)\n",
" print(\"-\" * 50)\n",
"\n",
" return wrapped_import.prune(\n",
" tf.nest.map_structure(import_graph.as_graph_element, inputs),\n",
" tf.nest.map_structure(import_graph.as_graph_element, outputs))"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"--------------------------------------------------\n",
"Frozen model layers: \n",
"x\n",
"sequential_1/conv2d_5/Conv2D/ReadVariableOp/resource\n",
"sequential_1/conv2d_5/BiasAdd/ReadVariableOp/resource\n",
"sequential_1/batch_normalization/ReadVariableOp/resource\n",
"sequential_1/batch_normalization/ReadVariableOp_1/resource\n",
"sequential_1/batch_normalization/FusedBatchNormV3/ReadVariableOp/resource\n",
"sequential_1/batch_normalization/FusedBatchNormV3/ReadVariableOp_1/resource\n",
"sequential_1/conv2d_6/Conv2D/ReadVariableOp/resource\n",
"sequential_1/conv2d_6/BiasAdd/ReadVariableOp/resource\n",
"sequential_1/batch_normalization_1/ReadVariableOp/resource\n",
"sequential_1/batch_normalization_1/ReadVariableOp_1/resource\n",
"sequential_1/batch_normalization_1/FusedBatchNormV3/ReadVariableOp/resource\n",
"sequential_1/batch_normalization_1/FusedBatchNormV3/ReadVariableOp_1/resource\n",
"sequential_1/conv2d_7/Conv2D/ReadVariableOp/resource\n",
"sequential_1/conv2d_7/BiasAdd/ReadVariableOp/resource\n",
"sequential_1/batch_normalization_2/ReadVariableOp/resource\n",
"sequential_1/batch_normalization_2/ReadVariableOp_1/resource\n",
"sequential_1/batch_normalization_2/FusedBatchNormV3/ReadVariableOp/resource\n",
"sequential_1/batch_normalization_2/FusedBatchNormV3/ReadVariableOp_1/resource\n",
"sequential_1/conv2d_8/Conv2D/ReadVariableOp/resource\n",
"sequential_1/conv2d_8/BiasAdd/ReadVariableOp/resource\n",
"sequential_1/batch_normalization_3/ReadVariableOp/resource\n",
"sequential_1/batch_normalization_3/ReadVariableOp_1/resource\n",
"sequential_1/batch_normalization_3/FusedBatchNormV3/ReadVariableOp/resource\n",
"sequential_1/batch_normalization_3/FusedBatchNormV3/ReadVariableOp_1/resource\n",
"sequential_1/conv2d_9/Conv2D/ReadVariableOp/resource\n",
"sequential_1/conv2d_9/BiasAdd/ReadVariableOp/resource\n",
"sequential_1/batch_normalization_4/ReadVariableOp/resource\n",
"sequential_1/batch_normalization_4/ReadVariableOp_1/resource\n",
"sequential_1/batch_normalization_4/FusedBatchNormV3/ReadVariableOp/resource\n",
"sequential_1/batch_normalization_4/FusedBatchNormV3/ReadVariableOp_1/resource\n",
"sequential_1/flatten_1/Const\n",
"sequential_1/dense_3/MatMul/ReadVariableOp/resource\n",
"sequential_1/dense_3/BiasAdd/ReadVariableOp/resource\n",
"sequential_1/dense_4/MatMul/ReadVariableOp/resource\n",
"sequential_1/dense_4/BiasAdd/ReadVariableOp/resource\n",
"sequential_1/dense_5/MatMul/ReadVariableOp/resource\n",
"sequential_1/dense_5/BiasAdd/ReadVariableOp/resource\n",
"sequential_1/conv2d_5/Conv2D/ReadVariableOp\n",
"sequential_1/conv2d_5/Conv2D\n",
"sequential_1/conv2d_5/BiasAdd/ReadVariableOp\n",
"sequential_1/conv2d_5/BiasAdd\n",
"sequential_1/conv2d_5/Relu\n",
"sequential_1/batch_normalization/ReadVariableOp\n",
"sequential_1/batch_normalization/ReadVariableOp_1\n",
"sequential_1/batch_normalization/FusedBatchNormV3/ReadVariableOp\n",
"sequential_1/batch_normalization/FusedBatchNormV3/ReadVariableOp_1\n",
"sequential_1/batch_normalization/FusedBatchNormV3\n",
"sequential_1/max_pooling2d_3/MaxPool\n",
"sequential_1/conv2d_6/Conv2D/ReadVariableOp\n",
"sequential_1/conv2d_6/Conv2D\n",
"sequential_1/conv2d_6/BiasAdd/ReadVariableOp\n",
"sequential_1/conv2d_6/BiasAdd\n",
"sequential_1/conv2d_6/Relu\n",
"sequential_1/batch_normalization_1/ReadVariableOp\n",
"sequential_1/batch_normalization_1/ReadVariableOp_1\n",
"sequential_1/batch_normalization_1/FusedBatchNormV3/ReadVariableOp\n",
"sequential_1/batch_normalization_1/FusedBatchNormV3/ReadVariableOp_1\n",
"sequential_1/batch_normalization_1/FusedBatchNormV3\n",
"sequential_1/max_pooling2d_4/MaxPool\n",
"sequential_1/conv2d_7/Conv2D/ReadVariableOp\n",
"sequential_1/conv2d_7/Conv2D\n",
"sequential_1/conv2d_7/BiasAdd/ReadVariableOp\n",
"sequential_1/conv2d_7/BiasAdd\n",
"sequential_1/conv2d_7/Relu\n",
"sequential_1/batch_normalization_2/ReadVariableOp\n",
"sequential_1/batch_normalization_2/ReadVariableOp_1\n",
"sequential_1/batch_normalization_2/FusedBatchNormV3/ReadVariableOp\n",
"sequential_1/batch_normalization_2/FusedBatchNormV3/ReadVariableOp_1\n",
"sequential_1/batch_normalization_2/FusedBatchNormV3\n",
"sequential_1/conv2d_8/Conv2D/ReadVariableOp\n",
"sequential_1/conv2d_8/Conv2D\n",
"sequential_1/conv2d_8/BiasAdd/ReadVariableOp\n",
"sequential_1/conv2d_8/BiasAdd\n",
"sequential_1/conv2d_8/Relu\n",
"sequential_1/batch_normalization_3/ReadVariableOp\n",
"sequential_1/batch_normalization_3/ReadVariableOp_1\n",
"sequential_1/batch_normalization_3/FusedBatchNormV3/ReadVariableOp\n",
"sequential_1/batch_normalization_3/FusedBatchNormV3/ReadVariableOp_1\n",
"sequential_1/batch_normalization_3/FusedBatchNormV3\n",
"sequential_1/conv2d_9/Conv2D/ReadVariableOp\n",
"sequential_1/conv2d_9/Conv2D\n",
"sequential_1/conv2d_9/BiasAdd/ReadVariableOp\n",
"sequential_1/conv2d_9/BiasAdd\n",
"sequential_1/conv2d_9/Relu\n",
"sequential_1/batch_normalization_4/ReadVariableOp\n",
"sequential_1/batch_normalization_4/ReadVariableOp_1\n",
"sequential_1/batch_normalization_4/FusedBatchNormV3/ReadVariableOp\n",
"sequential_1/batch_normalization_4/FusedBatchNormV3/ReadVariableOp_1\n",
"sequential_1/batch_normalization_4/FusedBatchNormV3\n",
"sequential_1/max_pooling2d_5/MaxPool\n",
"sequential_1/flatten_1/Reshape\n",
"sequential_1/dense_3/MatMul/ReadVariableOp\n",
"sequential_1/dense_3/MatMul\n",
"sequential_1/dense_3/BiasAdd/ReadVariableOp\n",
"sequential_1/dense_3/BiasAdd\n",
"sequential_1/dense_3/Relu\n",
"sequential_1/dense_4/MatMul/ReadVariableOp\n",
"sequential_1/dense_4/MatMul\n",
"sequential_1/dense_4/BiasAdd/ReadVariableOp\n",
"sequential_1/dense_4/BiasAdd\n",
"sequential_1/dense_4/Relu\n",
"sequential_1/dense_5/MatMul/ReadVariableOp\n",
"sequential_1/dense_5/MatMul\n",
"sequential_1/dense_5/BiasAdd/ReadVariableOp\n",
"sequential_1/dense_5/BiasAdd\n",
"sequential_1/dense_5/Softmax\n",
"NoOp\n",
"Identity\n",
"--------------------------------------------------\n"
]
}
],
"source": [
" # Load frozen graph using TensorFlow 1.x functions\n",
"with tf.io.gfile.GFile(\"./frozen_models/frozen_graph.pb\", \"rb\") as f:\n",
" graph_def = tf.compat.v1.GraphDef()\n",
" loaded = graph_def.ParseFromString(f.read())\n",
"\n",
"# Wrap frozen graph to ConcreteFunctions\n",
"frozen_func = wrap_frozen_graph(graph_def=graph_def,\n",
" inputs=[\"x:0\"],\n",
" outputs=[\"Identity:0\"],\n",
" print_graph=True)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"import cv2 as cv"
]
},
{
"cell_type": "code",
"execution_count": 64,
"metadata": {},
"outputs": [],
"source": [
"fish = cv.imread('./new_data/train/Fish/acanthopagrus_latus_2.png')"
]
},
{
"cell_type": "code",
"execution_count": 62,
"metadata": {},
"outputs": [],
"source": [
"fish = cv.resize(fish, (227, 227), interpolation=cv.INTER_AREA)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"class_names=sorted(['Fish', \"Jellyfish\", 'Penguin', 'Puffin', 'Shark', 'Starfish', 'Stingray'])"
]
},
{
"cell_type": "code",
"execution_count": 57,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[0., 0., 0., 0., 0., 0., 1., 0., 0., 0.]], dtype=float32)"
]
},
"execution_count": 57,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"frozen_func(x=tf.convert_to_tensor(fish[None, :], dtype=\"float32\"))[0].numpy()"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"from PIL import Image"
]
},
2023-01-31 20:05:31 +01:00
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [],
"source": [
"fishes = [\n",
" tf.convert_to_tensor(cv.resize(cv.imread('./new_data/train/Shark/D3U6ZGZZCQTF.jpg'), (227,227),interpolation=cv.INTER_AREA)[None, :], dtype='float32'),\n",
" tf.convert_to_tensor(cv.resize(cv.imread('./new_data/train/Shark/08XY6WGTVFYN.jpg'), (227,227), interpolation=cv.INTER_AREA)[None, :], dtype='float32')\n",
" ]"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[<tf.Tensor: shape=(1, 10), dtype=float32, numpy=array([[1., 0., 0., 0., 0., 0., 0., 0., 0., 0.]], dtype=float32)>]"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"frozen_func(x=fishes[0])"
]
},
2023-01-31 19:30:04 +01:00
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Category: Fish\n"
]
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAABAAAAAKrCAIAAADgdGjDAAEAAElEQVR4nOz93a8sSZIfiP3MPCLz3Hurqns+ej5EzuwMKe0u9UFyIUjgQlhgAT3pSW/6S7XQ00IPBARCgFYLLQXuasnZGc5wONPsqa6PW/eckxFupgeLsLDwCI/MyJPn41aX9e1TkZGRHubm5vbl5ub0f/k//28BEBERxQuD2v0lMPP6F5RX2xGR1cebptl4ywo+mlafPBwOxZP296E7MTNzAzDT4dC+e//+q7vjh5999buH9v3d8csm3RE1UGZuUmpx6G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/RXh2B4CUyP6SFXtQFSIelIKQakeUNJRuq+FJa5Hgz90BKPb+smX3q5AKAB4zRZVIqmk/Mv60iqdju2J0UnF//R0vo1VX37s6338EDgDWxmWv6IjxIIR5UbMT9o5jbd55vTWaxyujM3D1S28I2/q9EPJn23m63T6F/Yq2bg7P3X4BxnArDgALkABRJSCr5tEm7kVyoLvUlcsWRJOo2OD4AhBNBwpBRC8H2Y/1DlOaKhTRWAio6Ei82NuR2Lgbcxts7WOEcRoYnpYacTlsvyJK7V3N3hx8wsc7G0RexfnyXiwFy4gAjRJzfQVgew+GB7DdWmIVrK8AjC/1CwBAHzaNPdNkMTW2ws+TzIWMZTYLB8Ch7/vozJtKyzkfj8dVvnKL3Mv7eCEgXdsYZ+U+l3sMzJHw6eBvjy+KPR1XAMTdjEvUyduBvQ5ADWrPJ1ICMYhUiBJIFaQqJARkgKFEDFIlGZm1jmq0Qnah94ZB3Pq
"text/plain": [
"<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=1024x683>"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"fish_path = './new_data/train/Shark/D3U6ZGZZCQTF.jpg'\n",
"fish = cv.imread(fish_path)\n",
"fish = cv.resize(fish, (227, 227), interpolation=cv.INTER_AREA)\n",
"print(f\"Category: {class_names[np.argmax(frozen_func(x=tf.convert_to_tensor(fish[None, :], dtype='float32'))[0].numpy())]}\")\n",
"Image.open(fish_path)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "um",
"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.15"
},
"orig_nbformat": 4,
"vscode": {
"interpreter": {
"hash": "876e189cbbe99a9a838ece62aae1013186c4bb7e0254a10cfa2f9b2381853efb"
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}