In [1]:
import sys
sys.path.insert(0, '../')

In [2]:
import time
import numpy as np
np.set_printoptions(precision=3, linewidth=200, suppress=True)
from sklearn.metrics import classification_report, accuracy_score
import tensorflow as tf
from tensorflow.python.framework import graph_util

In [3]:
from library.datasets.cifar10 import CIFAR10
from library.plot_tools import plot
from library.utils import file_utils

In [11]:
def freeze_graph(model_folder, model_name, output_node_name):
    print(model_folder)
    checkpoint = tf.train.get_checkpoint_state(model_folder)
    input_checkpoint = checkpoint.model_checkpoint_path
    absolute_model_folder = '/'.join(input_checkpoint.split('/')[:-1])
    print(absolute_model_folder)
    output_graph = absolute_model_folder + '/' + model_name
    print(output_graph)
    output_node_names = output_node_name
    clear_devices = True
    print(input_checkpoint)
    saver = tf.train.import_meta_graph(input_checkpoint + '.meta', clear_devices=clear_devices)
    graph = tf.get_default_graph()
    input_graph_def = graph.as_graph_def()
    with tf.Session() as sess:
        saver.restore(sess, input_checkpoint)
        output_graph_def = graph_util.convert_variables_to_constants(
            sess,
            input_graph_def,
            output_node_names.split(',')
        )
        with tf.gfile.GFile(output_graph, 'wb') as f:
            f.write(output_graph_def.SerializeToString())
        print('%d ops in the final graph.' % len(output_graph_def.node))
    return True

In [12]:
def load_graph(filename):
    with tf.gfile.GFile(filename, 'rb') as f:
        graph_def = tf.GraphDef()
        graph_def.ParseFromString(f.read())
    with tf.Graph().as_default() as graph:
        tf.import_graph_def(
            graph_def,
            input_map=None,
            return_elements=None,
            name="prefix",
            op_dict=None,
            producer_op_list=None
        )
    return graph

In [13]:
dataset = 'cifar10'
file_no = 202
exp_no = 1
total_time = 0

In [14]:
model_folder = '../logs/' + dataset + '/' + str(file_no).zfill(3) + '_tfl_cnn/exp_no_' + str(exp_no).zfill(3) + '/'
model_name = 'residual_net_classifier.pb'
output_node_name = 'Predictions/predict_classes'

In [15]:
model_folder = './linear_model/'
model_name = 'linear_classifier_01.pb'
output_node_name = 'Predictions/predict_class'

In [16]:
device_name = '/cpu:0'
train_val_split_data = None
one_hot = True
num_images_required = 0.0
transform = True
transform_method = 'StandardScaler'

In [17]:
freeze_graph(model_folder, model_name, output_node_name)


./linear_model/
./linear_model
./linear_model/linear_classifier_01.pb
./linear_model/linear_classifier.ckpt-99
<tensorflow.python.training.saver.Saver object at 0x7f699c556a58>
1
1
1
INFO:tensorflow:Froze 2 variables.
Converted 2 variables to const ops.
11 ops in the final graph.
Out[17]:
True

In [18]:
graph = load_graph(model_folder+'/'+model_name)

In [19]:
for op in graph.get_operations():
    print(op.name)


prefix/Inputs/Data/X_input
prefix/Parameters/Weights/Weight
prefix/Parameters/Weights/Weight/read
prefix/Parameters/Bias/Bias
prefix/Parameters/Bias/Bias/read
prefix/Predictions/mlp_layer/MatMul
prefix/Predictions/mlp_layer
prefix/Predictions/mlp_layer_1
prefix/Predictions/Softmax
prefix/Predictions/predict_class/dimension
prefix/Predictions/predict_class

In [20]:
x = graph.get_tensor_by_name('prefix/Inputs/Data/X_input:0')
y = graph.get_tensor_by_name('prefix/Predictions/predict_class:0')
print(str(x))
print(str(y))


Tensor("prefix/Inputs/Data/X_input:0", dtype=float32)
Tensor("prefix/Predictions/predict_class:0", shape=(?,), dtype=int64)

In [21]:
start = time.time()
one_hot = True
cifar10 = CIFAR10(one_hot_encode=one_hot, num_images=num_images_required, preprocess='StandardScaler',
                  train_validate_split=train_val_split_data, endian='little')
cifar10.load_data(train=False, test=True, data_directory='./datasets/cifar10/')
end = time.time()
print('[ Step 1] Loaded CIFAR 10 Dataset in %.4f ms' %((end-start)*1000))
total_time += (end-start)


Loading CIFAR 10 Dataset
Downloading and extracting CIFAR 10 file
MD5sum of the file: ./datasets/cifar10/cifar-10.tar.gz is verified
Loading 10000 test images
Loading CIFAR 10 Test Dataset
Unpickling test file: ./datasets/cifar10/cifar-10-batches/test_batch
Reading unpicked test file: ./datasets/cifar10/cifar-10-batches/test_batch
/net/voxel03/misc/me/praneethas/Softwares/anaconda3/lib/python3.6/site-packages/sklearn/utils/validation.py:429: DataConversionWarning: Data with input dtype uint8 was converted to float64 by StandardScaler.
  warnings.warn(msg, _DataConversionWarning)
Loaded CIFAR 10 Dataset in 1.3864 seconds
[ Step 1] Loaded CIFAR 10 Dataset in 1386.9727 ms

In [22]:
sess = tf.Session(graph=graph)
prediction_numbers = sess.run(y, feed_dict={x: cifar10.test.data})


---------------------------------------------------------------------------
InternalError                             Traceback (most recent call last)
/net/voxel03/misc/me/praneethas/Softwares/anaconda3/lib/python3.6/site-packages/tensorflow/python/client/session.py in _do_call(self, fn, *args)
   1021     try:
-> 1022       return fn(*args)
   1023     except errors.OpError as e:

/net/voxel03/misc/me/praneethas/Softwares/anaconda3/lib/python3.6/site-packages/tensorflow/python/client/session.py in _run_fn(session, feed_dict, fetch_list, target_list, options, run_metadata)
   1003                                  feed_dict, fetch_list, target_list,
-> 1004                                  status, run_metadata)
   1005 

/net/voxel03/misc/me/praneethas/Softwares/anaconda3/lib/python3.6/contextlib.py in __exit__(self, type, value, traceback)
     88             try:
---> 89                 next(self.gen)
     90             except StopIteration:

/net/voxel03/misc/me/praneethas/Softwares/anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/errors_impl.py in raise_exception_on_not_ok_status()
    465           compat.as_text(pywrap_tensorflow.TF_Message(status)),
--> 466           pywrap_tensorflow.TF_GetCode(status))
    467   finally:

InternalError: Dst tensor is not initialized.
	 [[Node: _recv_prefix/Inputs/Data/X_input_0/_1 = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/gpu:0", send_device="/job:localhost/replica:0/task:0/cpu:0", send_device_incarnation=1, tensor_name="edge_18__recv_prefix/Inputs/Data/X_input_0", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/gpu:0"]()]]

During handling of the above exception, another exception occurred:

InternalError                             Traceback (most recent call last)
<ipython-input-22-cb11d26caa40> in <module>()
      1 sess = tf.Session(graph=graph)
----> 2 prediction_numbers = sess.run(y, feed_dict={x: cifar10.test.data})

/net/voxel03/misc/me/praneethas/Softwares/anaconda3/lib/python3.6/site-packages/tensorflow/python/client/session.py in run(self, fetches, feed_dict, options, run_metadata)
    765     try:
    766       result = self._run(None, fetches, feed_dict, options_ptr,
--> 767                          run_metadata_ptr)
    768       if run_metadata:
    769         proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)

/net/voxel03/misc/me/praneethas/Softwares/anaconda3/lib/python3.6/site-packages/tensorflow/python/client/session.py in _run(self, handle, fetches, feed_dict, options, run_metadata)
    963     if final_fetches or final_targets:
    964       results = self._do_run(handle, final_targets, final_fetches,
--> 965                              feed_dict_string, options, run_metadata)
    966     else:
    967       results = []

/net/voxel03/misc/me/praneethas/Softwares/anaconda3/lib/python3.6/site-packages/tensorflow/python/client/session.py in _do_run(self, handle, target_list, fetch_list, feed_dict, options, run_metadata)
   1013     if handle is None:
   1014       return self._do_call(_run_fn, self._session, feed_dict, fetch_list,
-> 1015                            target_list, options, run_metadata)
   1016     else:
   1017       return self._do_call(_prun_fn, self._session, handle, feed_dict,

/net/voxel03/misc/me/praneethas/Softwares/anaconda3/lib/python3.6/site-packages/tensorflow/python/client/session.py in _do_call(self, fn, *args)
   1033         except KeyError:
   1034           pass
-> 1035       raise type(e)(node_def, op, message)
   1036 
   1037   def _extend_graph(self):

InternalError: Dst tensor is not initialized.
	 [[Node: _recv_prefix/Inputs/Data/X_input_0/_1 = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/gpu:0", send_device="/job:localhost/replica:0/task:0/cpu:0", send_device_incarnation=1, tensor_name="edge_18__recv_prefix/Inputs/Data/X_input_0", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/gpu:0"]()]]

In [ ]:
prediction_classes = []
num_test_images = cifar10.test.data.shape[0]
for i in range(num_test_images):
    prediction_classes.append(cifar10.classes[int(prediction_numbers[i])])

In [ ]:
test_accuracy = accuracy_score(cifar10.test.class_labels, prediction_numbers)
print('Accuracy of the classifier on test dataset: %.4f' % test_accuracy)

In [ ]:
cifar10.plot_images(cifar10.test.data[:50], cifar10.test.class_names[:50], cls_pred=prediction_classes[:50], 
                    nrows=5, ncols=10, fig_size=(20,50), fontsize=30, convert=True)

In [ ]:
plot.plot_confusion_matrix(cifar10.test.class_labels, prediction_numbers, classes=cifar10.classes,
                           normalize=True, title='Confusion matrix')

In [ ]:
print('Detailed classification report')
print(classification_report(y_true=cifar10.test.class_labels, y_pred=prediction_numbers,
                            target_names=cifar10.classes))

In [ ]:
sess.close()

In [ ]: