widzenie-komputerowe-projekt/notepads/graph.ipynb
2023-02-01 18:42:47 +01:00

357 KiB

import tensorflow as tf
from tensorflow.python.framework.convert_to_constants import convert_variables_to_constants_v2
model = tf.keras.models.load_model('./model')
# Convert Keras model to ConcreteFunction
full_model = tf.function(lambda x: model(x))
full_model = full_model.get_concrete_function(
    tf.TensorSpec(model.inputs[0].shape, model.inputs[0].dtype))

# Get frozen ConcreteFunction
frozen_func = convert_variables_to_constants_v2(full_model)
frozen_func.graph.as_graph_def()


# Save frozen graph from frozen ConcreteFunction to hard drive
tf.io.write_graph(graph_or_graph_def=frozen_func.graph,
                  logdir="./frozen_models",
                  name="frozen_graph.pb",
                  as_text=False)
'./frozen_models\\\\frozen_graph.pb'
def wrap_frozen_graph(graph_def, inputs, outputs, print_graph=False):
    def _imports_graph_def():
        tf.compat.v1.import_graph_def(graph_def, name="")

    wrapped_import = tf.compat.v1.wrap_function(_imports_graph_def, [])
    import_graph = wrapped_import.graph

    if print_graph == True:
        print("-" * 50)
        print("Frozen model layers: ")
        layers = [op.name for op in import_graph.get_operations()]
        for layer in layers:
            print(layer)
        print("-" * 50)

    return wrapped_import.prune(
        tf.nest.map_structure(import_graph.as_graph_element, inputs),
        tf.nest.map_structure(import_graph.as_graph_element, outputs))
   # Load frozen graph using TensorFlow 1.x functions
with tf.io.gfile.GFile("./frozen_models/frozen_graph_best.pb", "rb") as f:
    graph_def = tf.compat.v1.GraphDef()
    loaded = graph_def.ParseFromString(f.read())

# Wrap frozen graph to ConcreteFunctions
frozen_func = wrap_frozen_graph(graph_def=graph_def,
                                inputs=["x:0"],
                                outputs=["Identity:0"],
                                print_graph=True)
--------------------------------------------------
Frozen model layers: 
x
sequential/conv2d/Conv2D/ReadVariableOp/resource
sequential/conv2d/Conv2D/ReadVariableOp
sequential/conv2d/Conv2D
sequential/conv2d/BiasAdd/ReadVariableOp/resource
sequential/conv2d/BiasAdd/ReadVariableOp
sequential/conv2d/BiasAdd
sequential/conv2d/Relu
sequential/batch_normalization/ReadVariableOp/resource
sequential/batch_normalization/ReadVariableOp
sequential/batch_normalization/ReadVariableOp_1/resource
sequential/batch_normalization/ReadVariableOp_1
sequential/batch_normalization/FusedBatchNormV3/ReadVariableOp/resource
sequential/batch_normalization/FusedBatchNormV3/ReadVariableOp
sequential/batch_normalization/FusedBatchNormV3/ReadVariableOp_1/resource
sequential/batch_normalization/FusedBatchNormV3/ReadVariableOp_1
sequential/batch_normalization/FusedBatchNormV3
sequential/max_pooling2d/MaxPool
sequential/conv2d_1/Conv2D/ReadVariableOp/resource
sequential/conv2d_1/Conv2D/ReadVariableOp
sequential/conv2d_1/Conv2D
sequential/conv2d_1/BiasAdd/ReadVariableOp/resource
sequential/conv2d_1/BiasAdd/ReadVariableOp
sequential/conv2d_1/BiasAdd
sequential/conv2d_1/Relu
sequential/batch_normalization_1/ReadVariableOp/resource
sequential/batch_normalization_1/ReadVariableOp
sequential/batch_normalization_1/ReadVariableOp_1/resource
sequential/batch_normalization_1/ReadVariableOp_1
sequential/batch_normalization_1/FusedBatchNormV3/ReadVariableOp/resource
sequential/batch_normalization_1/FusedBatchNormV3/ReadVariableOp
sequential/batch_normalization_1/FusedBatchNormV3/ReadVariableOp_1/resource
sequential/batch_normalization_1/FusedBatchNormV3/ReadVariableOp_1
sequential/batch_normalization_1/FusedBatchNormV3
sequential/max_pooling2d_1/MaxPool
sequential/conv2d_2/Conv2D/ReadVariableOp/resource
sequential/conv2d_2/Conv2D/ReadVariableOp
sequential/conv2d_2/Conv2D
sequential/conv2d_2/BiasAdd/ReadVariableOp/resource
sequential/conv2d_2/BiasAdd/ReadVariableOp
sequential/conv2d_2/BiasAdd
sequential/conv2d_2/Relu
sequential/batch_normalization_2/ReadVariableOp/resource
sequential/batch_normalization_2/ReadVariableOp
sequential/batch_normalization_2/ReadVariableOp_1/resource
sequential/batch_normalization_2/ReadVariableOp_1
sequential/batch_normalization_2/FusedBatchNormV3/ReadVariableOp/resource
sequential/batch_normalization_2/FusedBatchNormV3/ReadVariableOp
sequential/batch_normalization_2/FusedBatchNormV3/ReadVariableOp_1/resource
sequential/batch_normalization_2/FusedBatchNormV3/ReadVariableOp_1
sequential/batch_normalization_2/FusedBatchNormV3
sequential/conv2d_3/Conv2D/ReadVariableOp/resource
sequential/conv2d_3/Conv2D/ReadVariableOp
sequential/conv2d_3/Conv2D
sequential/conv2d_3/BiasAdd/ReadVariableOp/resource
sequential/conv2d_3/BiasAdd/ReadVariableOp
sequential/conv2d_3/BiasAdd
sequential/conv2d_3/Relu
sequential/batch_normalization_3/ReadVariableOp/resource
sequential/batch_normalization_3/ReadVariableOp
sequential/batch_normalization_3/ReadVariableOp_1/resource
sequential/batch_normalization_3/ReadVariableOp_1
sequential/batch_normalization_3/FusedBatchNormV3/ReadVariableOp/resource
sequential/batch_normalization_3/FusedBatchNormV3/ReadVariableOp
sequential/batch_normalization_3/FusedBatchNormV3/ReadVariableOp_1/resource
sequential/batch_normalization_3/FusedBatchNormV3/ReadVariableOp_1
sequential/batch_normalization_3/FusedBatchNormV3
sequential/conv2d_4/Conv2D/ReadVariableOp/resource
sequential/conv2d_4/Conv2D/ReadVariableOp
sequential/conv2d_4/Conv2D
sequential/conv2d_4/BiasAdd/ReadVariableOp/resource
sequential/conv2d_4/BiasAdd/ReadVariableOp
sequential/conv2d_4/BiasAdd
sequential/conv2d_4/Relu
sequential/batch_normalization_4/ReadVariableOp/resource
sequential/batch_normalization_4/ReadVariableOp
sequential/batch_normalization_4/ReadVariableOp_1/resource
sequential/batch_normalization_4/ReadVariableOp_1
sequential/batch_normalization_4/FusedBatchNormV3/ReadVariableOp/resource
sequential/batch_normalization_4/FusedBatchNormV3/ReadVariableOp
sequential/batch_normalization_4/FusedBatchNormV3/ReadVariableOp_1/resource
sequential/batch_normalization_4/FusedBatchNormV3/ReadVariableOp_1
sequential/batch_normalization_4/FusedBatchNormV3
sequential/max_pooling2d_2/MaxPool
sequential/flatten/Const
sequential/flatten/Reshape
sequential/dense/MatMul/ReadVariableOp/resource
sequential/dense/MatMul/ReadVariableOp
sequential/dense/MatMul
sequential/dense/BiasAdd/ReadVariableOp/resource
sequential/dense/BiasAdd/ReadVariableOp
sequential/dense/BiasAdd
sequential/dense/Relu
sequential/dense_1/MatMul/ReadVariableOp/resource
sequential/dense_1/MatMul/ReadVariableOp
sequential/dense_1/MatMul
sequential/dense_1/BiasAdd/ReadVariableOp/resource
sequential/dense_1/BiasAdd/ReadVariableOp
sequential/dense_1/BiasAdd
sequential/dense_1/Relu
sequential/dense_2/MatMul/ReadVariableOp/resource
sequential/dense_2/MatMul/ReadVariableOp
sequential/dense_2/MatMul
sequential/dense_2/BiasAdd/ReadVariableOp/resource
sequential/dense_2/BiasAdd/ReadVariableOp
sequential/dense_2/BiasAdd
sequential/dense_2/Softmax
NoOp
Identity
--------------------------------------------------
import cv2 as cv
fish = cv.imread('./new_data/train/Jellyfish/03Q7BTGDW34G.jpg')
fish = cv.resize(fish, (227, 227), interpolation=cv.INTER_AREA)
class_names=sorted(['Fish', "Jellyfish", 'Penguin', 'Puffin', 'Shark', 'Starfish', 'Stingray'])
frozen_func(x=tf.convert_to_tensor(fish[None, :], dtype="float32"))[0].numpy()
array([[0., 0., 0., 0., 0., 0., 1., 0., 0., 0.]], dtype=float32)
import numpy as np
from PIL import Image
fishes = [
    tf.convert_to_tensor(cv.resize(cv.imread('./new_data/train/Shark/D3U6ZGZZCQTF.jpg'), (227,227),interpolation=cv.INTER_AREA)[None, :], dtype='float32'),
    tf.convert_to_tensor(cv.resize(cv.imread('./new_data/train/Shark/08XY6WGTVFYN.jpg'), (227,227), interpolation=cv.INTER_AREA)[None, :], dtype='float32')
    ]
frozen_func(x=fishes[0])
[<tf.Tensor: shape=(1, 10), dtype=float32, numpy=array([[1., 0., 0., 0., 0., 0., 0., 0., 0., 0.]], dtype=float32)>]
fish_path = './new_data/test/4R4JNOPP4WYE.jpg'
fish = cv.imread(fish_path)
fish = cv.resize(fish, (227, 227), interpolation=cv.INTER_AREA)
print(frozen_func(x=tf.convert_to_tensor(fish[None, :], dtype='float32')))
print(f"Category: {class_names[np.argmax(frozen_func(x=tf.convert_to_tensor(fish[None, :], dtype='float32'))[0].numpy())]}")
Image.open(fish_path)
[<tf.Tensor: shape=(1, 6), dtype=float32, numpy=array([[1., 0., 0., 0., 0., 0.]], dtype=float32)>]
Category: Fish