Restoring Tensorflow model as a class and running a specific layer
My tensorflow model is defined in a class as follows:
class EENetwork:
def __init__(self, state_size, learning_rate, output_units, name='EENetwork'):
self.learning_rate = learning_rate
self.state_size = state_size # State size = height * width * cameras
self.output_units = output_units
with tf.variable_scope(name):
self.inputs_ = tf.placeholder(tf.float32, [None, *state_size], name='inputs')
self.outputs_ = tf.placeholder(tf.float32, [None, self.output_units], name='outputs_') # To hold the training Y
# Placeholders aren't added to the saved graph by default
tf.add_to_collection('inputs', self.inputs_)
tf.add_to_collection('outputs_', self.outputs_)
# Conv1 = samples * h * w * 24
# Assume h, w = 64, so samples * 64 * 64 * 24
self.conv1 = tf.layers.conv2d(inputs=self.inputs_, filters=24, kernel_size=[5, 5],
padding='same', activation=tf.nn.relu, name='conv1')
# Maxpool1 = samples * h/2 * w/2 * 12 = samples * 32 * 32 * 24
self.maxpool1 = tf.layers.max_pooling2d(inputs=self.conv1, pool_size=[2, 2], strides=2, name='mp1')
# Conv2 = samples * 32 * 32* 36
self.conv2 = tf.layers.conv2d(inputs=self.maxpool1, filters=36, kernel_size=[5, 5],
padding='same', activation=tf.nn.relu, name='conv2')
# Maxpool2 = samples * 16 * 16 * 36
self.maxpool2 = tf.layers.max_pooling2d(inputs=self.conv2, pool_size=[2, 2], strides=2, name='mp2')
# Conv3 = samples * 16 * 16 * 48
self.conv3 = tf.layers.conv2d(inputs=self.maxpool2, filters=48, kernel_size=[3, 3],
padding='same', activation=tf.nn.relu, name='conv3')
# Maxpool3 = samples * 8 * 8 * 48
self.maxpool3 = tf.layers.max_pooling2d(inputs=self.conv3, pool_size=[2, 2], strides=2, name='mp3')
# Conv4 = samples * 8 * 8 * 64
self.conv4 = tf.layers.conv2d(inputs=self.maxpool3, filters=64, kernel_size=[3, 3],
padding='same', activation=tf.nn.relu, name='conv4')
# Maxpool4 = samples * 4 * 4 * 64
self.maxpool4 = tf.layers.max_pooling2d(inputs=self.conv4, pool_size=[2, 2], strides=2, name='mp4')
# Flatten = samples * 1024
self.flatten = tf.contrib.layers.flatten(self.maxpool4)
# FC1, Output units = 1164
self.fc1 = tf.layers.dense(inputs=self.flatten, units=1164, activation=tf.nn.elu,
kernel_initializer=tf.contrib.layers.xavier_initializer(), name='fc1')
# FC2, Output units = 100
self.fc2 = tf.layers.dense(inputs=self.fc1, units=100, activation=tf.nn.elu,
kernel_initializer=tf.contrib.layers.xavier_initializer(), name='fc2')
# FC3, Output units = 50
self.fc3 = tf.layers.dense(inputs=self.fc2, units=50, activation=tf.nn.elu,
kernel_initializer=tf.contrib.layers.xavier_initializer(), name='fc3')
# FC4, Output units = 10
self.fc4 = tf.layers.dense(inputs=self.fc3, units=10, activation=tf.nn.elu,
kernel_initializer=tf.contrib.layers.xavier_initializer(), name='fc4')
# Output
self.output = tf.layers.dense(inputs=self.flatten, units=self.output_units, name='DL')
# Cost
#self.loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits=self.output, labels=self.outputs_))
self.loss = tf.losses.mean_squared_error(labels=self.outputs_, predictions=self.output)
# Optimizer
self.optimizer = tf.train.AdamOptimizer(self.learning_rate).minimize(self.loss)
I am creating an instance of this class and then training and saving it. Now, after restoring the saved model in another file, I want to get the output produced by self.output.
This is the code I have written for the second file:
with tf.Session() as sess:
saver = tf.train.import_meta_graph("./models/drivingModel_{LR}_{EP}_{MN}.ckpt.meta".format(LR=learning_rate, EP=epochs, MN=mod_number))
saver.restore(sess, "./models/drivingModel_{LR}_{EP}_{MN}.ckpt".format(LR=learning_rate, EP=epochs, MN=mod_number))
frontImage = cv2.imread('Front_5.png')
leftImage = cv2.imread('Left_5.png')
rightImage = cv2.imread('Right_5.png')
front = cv2.resize(cv2.cvtColor(frontImage, cv2.COLOR_RGB2GRAY), (h, w))
left = cv2.resize(cv2.cvtColor(leftImage, cv2.COLOR_RGB2GRAY), (h, w))
right = cv2.resize(cv2.cvtColor(rightImage, cv2.COLOR_RGB2GRAY), (h, w))
images = np.stack((front, left, right)) # num_cameras * h * w
images = np.moveaxis(images, [0, 1, 2], [2, 0, 1])
X =
X.append(images)
X = np.array(X)
print(tf.get_default_graph().get_all_collection_keys())
all_variables = tf.get_collection('variables')
print("Variables of the tensor:")
for var in all_variables:
print(var)
graph = sess.graph
print("Operations of the tensor: ")
operations = tf.get_collection('train_op')
for op in graph.get_operations():
print(op)
I want to pass the X as a feeddict parameter to self.inputs_ and get the output produced by self.output (named as 'DL').
I couldn't figure out which variables and operations can I use to perform the aforementioned task.
I can get the input placeholder using:
inputs_ = tf.get_collection('inputs')[0]
But how can I pass X to it and use it to get the value produced at self.output layer?
Edit:
I was able to solve it by creating a new instance of the class and then running the following code:
tf.reset_default_graph()
drivingNet = cnn_model.EENetwork(state_size=state_size,
learning_rate=learning_rate, output_units=classes)
with tf.Session() as sess:
saver = tf.train.Saver(tf.global_variables())
saver = tf.train.import_meta_graph("./models/drivingModel_{LR}_{EP}_{MN}.ckpt.meta".format(LR=learning_rate, EP=epochs, MN=mod_number))
saver.restore(sess, "./models/drivingModel_{LR}_{EP}_{MN}.ckpt".format(LR=learning_rate, EP=epochs, MN=mod_number))
frontImage = cv2.imread('Front_5.png')
leftImage = cv2.imread('Left_5.png')
rightImage = cv2.imread('Right_5.png')
front = cv2.resize(cv2.cvtColor(frontImage, cv2.COLOR_RGB2GRAY), (h, w))
left = cv2.resize(cv2.cvtColor(leftImage, cv2.COLOR_RGB2GRAY), (h, w))
right = cv2.resize(cv2.cvtColor(rightImage, cv2.COLOR_RGB2GRAY), (h, w))
images = np.stack((front, left, right)) # num_cameras * h * w
# Move the axis to make it h * w * num_cameras
images = np.moveaxis(images, [0, 1, 2], [2, 0, 1])
# images = np.concatenate((leftImage, frontImage), axis=1)
# images = np.concatenate((images, rightImage), axis=1)
# Input needs to be passed as samples * h * w * num_cameras
X =
X.append(images)
X = np.array(X) # This makes it 1 * h * w * cameras
decision = sess.run(drivingNet.output, feed_dict={drivingNet.inputs_: X})
temp = decision.tolist()
flat_list = [item for sublist in temp for item in sublist]
print(flat_list)
However, I still don't understand what saver = tf.train.Saver(tf.global_variables()) is doing in this case? Why is this line of code required? If I do not include this line, it throws an "attempting to use uninitialized value" error. Shouldn't the meta graph and saver.restore initialize all values to the saved values?
python tensorflow
add a comment |
My tensorflow model is defined in a class as follows:
class EENetwork:
def __init__(self, state_size, learning_rate, output_units, name='EENetwork'):
self.learning_rate = learning_rate
self.state_size = state_size # State size = height * width * cameras
self.output_units = output_units
with tf.variable_scope(name):
self.inputs_ = tf.placeholder(tf.float32, [None, *state_size], name='inputs')
self.outputs_ = tf.placeholder(tf.float32, [None, self.output_units], name='outputs_') # To hold the training Y
# Placeholders aren't added to the saved graph by default
tf.add_to_collection('inputs', self.inputs_)
tf.add_to_collection('outputs_', self.outputs_)
# Conv1 = samples * h * w * 24
# Assume h, w = 64, so samples * 64 * 64 * 24
self.conv1 = tf.layers.conv2d(inputs=self.inputs_, filters=24, kernel_size=[5, 5],
padding='same', activation=tf.nn.relu, name='conv1')
# Maxpool1 = samples * h/2 * w/2 * 12 = samples * 32 * 32 * 24
self.maxpool1 = tf.layers.max_pooling2d(inputs=self.conv1, pool_size=[2, 2], strides=2, name='mp1')
# Conv2 = samples * 32 * 32* 36
self.conv2 = tf.layers.conv2d(inputs=self.maxpool1, filters=36, kernel_size=[5, 5],
padding='same', activation=tf.nn.relu, name='conv2')
# Maxpool2 = samples * 16 * 16 * 36
self.maxpool2 = tf.layers.max_pooling2d(inputs=self.conv2, pool_size=[2, 2], strides=2, name='mp2')
# Conv3 = samples * 16 * 16 * 48
self.conv3 = tf.layers.conv2d(inputs=self.maxpool2, filters=48, kernel_size=[3, 3],
padding='same', activation=tf.nn.relu, name='conv3')
# Maxpool3 = samples * 8 * 8 * 48
self.maxpool3 = tf.layers.max_pooling2d(inputs=self.conv3, pool_size=[2, 2], strides=2, name='mp3')
# Conv4 = samples * 8 * 8 * 64
self.conv4 = tf.layers.conv2d(inputs=self.maxpool3, filters=64, kernel_size=[3, 3],
padding='same', activation=tf.nn.relu, name='conv4')
# Maxpool4 = samples * 4 * 4 * 64
self.maxpool4 = tf.layers.max_pooling2d(inputs=self.conv4, pool_size=[2, 2], strides=2, name='mp4')
# Flatten = samples * 1024
self.flatten = tf.contrib.layers.flatten(self.maxpool4)
# FC1, Output units = 1164
self.fc1 = tf.layers.dense(inputs=self.flatten, units=1164, activation=tf.nn.elu,
kernel_initializer=tf.contrib.layers.xavier_initializer(), name='fc1')
# FC2, Output units = 100
self.fc2 = tf.layers.dense(inputs=self.fc1, units=100, activation=tf.nn.elu,
kernel_initializer=tf.contrib.layers.xavier_initializer(), name='fc2')
# FC3, Output units = 50
self.fc3 = tf.layers.dense(inputs=self.fc2, units=50, activation=tf.nn.elu,
kernel_initializer=tf.contrib.layers.xavier_initializer(), name='fc3')
# FC4, Output units = 10
self.fc4 = tf.layers.dense(inputs=self.fc3, units=10, activation=tf.nn.elu,
kernel_initializer=tf.contrib.layers.xavier_initializer(), name='fc4')
# Output
self.output = tf.layers.dense(inputs=self.flatten, units=self.output_units, name='DL')
# Cost
#self.loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits=self.output, labels=self.outputs_))
self.loss = tf.losses.mean_squared_error(labels=self.outputs_, predictions=self.output)
# Optimizer
self.optimizer = tf.train.AdamOptimizer(self.learning_rate).minimize(self.loss)
I am creating an instance of this class and then training and saving it. Now, after restoring the saved model in another file, I want to get the output produced by self.output.
This is the code I have written for the second file:
with tf.Session() as sess:
saver = tf.train.import_meta_graph("./models/drivingModel_{LR}_{EP}_{MN}.ckpt.meta".format(LR=learning_rate, EP=epochs, MN=mod_number))
saver.restore(sess, "./models/drivingModel_{LR}_{EP}_{MN}.ckpt".format(LR=learning_rate, EP=epochs, MN=mod_number))
frontImage = cv2.imread('Front_5.png')
leftImage = cv2.imread('Left_5.png')
rightImage = cv2.imread('Right_5.png')
front = cv2.resize(cv2.cvtColor(frontImage, cv2.COLOR_RGB2GRAY), (h, w))
left = cv2.resize(cv2.cvtColor(leftImage, cv2.COLOR_RGB2GRAY), (h, w))
right = cv2.resize(cv2.cvtColor(rightImage, cv2.COLOR_RGB2GRAY), (h, w))
images = np.stack((front, left, right)) # num_cameras * h * w
images = np.moveaxis(images, [0, 1, 2], [2, 0, 1])
X =
X.append(images)
X = np.array(X)
print(tf.get_default_graph().get_all_collection_keys())
all_variables = tf.get_collection('variables')
print("Variables of the tensor:")
for var in all_variables:
print(var)
graph = sess.graph
print("Operations of the tensor: ")
operations = tf.get_collection('train_op')
for op in graph.get_operations():
print(op)
I want to pass the X as a feeddict parameter to self.inputs_ and get the output produced by self.output (named as 'DL').
I couldn't figure out which variables and operations can I use to perform the aforementioned task.
I can get the input placeholder using:
inputs_ = tf.get_collection('inputs')[0]
But how can I pass X to it and use it to get the value produced at self.output layer?
Edit:
I was able to solve it by creating a new instance of the class and then running the following code:
tf.reset_default_graph()
drivingNet = cnn_model.EENetwork(state_size=state_size,
learning_rate=learning_rate, output_units=classes)
with tf.Session() as sess:
saver = tf.train.Saver(tf.global_variables())
saver = tf.train.import_meta_graph("./models/drivingModel_{LR}_{EP}_{MN}.ckpt.meta".format(LR=learning_rate, EP=epochs, MN=mod_number))
saver.restore(sess, "./models/drivingModel_{LR}_{EP}_{MN}.ckpt".format(LR=learning_rate, EP=epochs, MN=mod_number))
frontImage = cv2.imread('Front_5.png')
leftImage = cv2.imread('Left_5.png')
rightImage = cv2.imread('Right_5.png')
front = cv2.resize(cv2.cvtColor(frontImage, cv2.COLOR_RGB2GRAY), (h, w))
left = cv2.resize(cv2.cvtColor(leftImage, cv2.COLOR_RGB2GRAY), (h, w))
right = cv2.resize(cv2.cvtColor(rightImage, cv2.COLOR_RGB2GRAY), (h, w))
images = np.stack((front, left, right)) # num_cameras * h * w
# Move the axis to make it h * w * num_cameras
images = np.moveaxis(images, [0, 1, 2], [2, 0, 1])
# images = np.concatenate((leftImage, frontImage), axis=1)
# images = np.concatenate((images, rightImage), axis=1)
# Input needs to be passed as samples * h * w * num_cameras
X =
X.append(images)
X = np.array(X) # This makes it 1 * h * w * cameras
decision = sess.run(drivingNet.output, feed_dict={drivingNet.inputs_: X})
temp = decision.tolist()
flat_list = [item for sublist in temp for item in sublist]
print(flat_list)
However, I still don't understand what saver = tf.train.Saver(tf.global_variables()) is doing in this case? Why is this line of code required? If I do not include this line, it throws an "attempting to use uninitialized value" error. Shouldn't the meta graph and saver.restore initialize all values to the saved values?
python tensorflow
add a comment |
My tensorflow model is defined in a class as follows:
class EENetwork:
def __init__(self, state_size, learning_rate, output_units, name='EENetwork'):
self.learning_rate = learning_rate
self.state_size = state_size # State size = height * width * cameras
self.output_units = output_units
with tf.variable_scope(name):
self.inputs_ = tf.placeholder(tf.float32, [None, *state_size], name='inputs')
self.outputs_ = tf.placeholder(tf.float32, [None, self.output_units], name='outputs_') # To hold the training Y
# Placeholders aren't added to the saved graph by default
tf.add_to_collection('inputs', self.inputs_)
tf.add_to_collection('outputs_', self.outputs_)
# Conv1 = samples * h * w * 24
# Assume h, w = 64, so samples * 64 * 64 * 24
self.conv1 = tf.layers.conv2d(inputs=self.inputs_, filters=24, kernel_size=[5, 5],
padding='same', activation=tf.nn.relu, name='conv1')
# Maxpool1 = samples * h/2 * w/2 * 12 = samples * 32 * 32 * 24
self.maxpool1 = tf.layers.max_pooling2d(inputs=self.conv1, pool_size=[2, 2], strides=2, name='mp1')
# Conv2 = samples * 32 * 32* 36
self.conv2 = tf.layers.conv2d(inputs=self.maxpool1, filters=36, kernel_size=[5, 5],
padding='same', activation=tf.nn.relu, name='conv2')
# Maxpool2 = samples * 16 * 16 * 36
self.maxpool2 = tf.layers.max_pooling2d(inputs=self.conv2, pool_size=[2, 2], strides=2, name='mp2')
# Conv3 = samples * 16 * 16 * 48
self.conv3 = tf.layers.conv2d(inputs=self.maxpool2, filters=48, kernel_size=[3, 3],
padding='same', activation=tf.nn.relu, name='conv3')
# Maxpool3 = samples * 8 * 8 * 48
self.maxpool3 = tf.layers.max_pooling2d(inputs=self.conv3, pool_size=[2, 2], strides=2, name='mp3')
# Conv4 = samples * 8 * 8 * 64
self.conv4 = tf.layers.conv2d(inputs=self.maxpool3, filters=64, kernel_size=[3, 3],
padding='same', activation=tf.nn.relu, name='conv4')
# Maxpool4 = samples * 4 * 4 * 64
self.maxpool4 = tf.layers.max_pooling2d(inputs=self.conv4, pool_size=[2, 2], strides=2, name='mp4')
# Flatten = samples * 1024
self.flatten = tf.contrib.layers.flatten(self.maxpool4)
# FC1, Output units = 1164
self.fc1 = tf.layers.dense(inputs=self.flatten, units=1164, activation=tf.nn.elu,
kernel_initializer=tf.contrib.layers.xavier_initializer(), name='fc1')
# FC2, Output units = 100
self.fc2 = tf.layers.dense(inputs=self.fc1, units=100, activation=tf.nn.elu,
kernel_initializer=tf.contrib.layers.xavier_initializer(), name='fc2')
# FC3, Output units = 50
self.fc3 = tf.layers.dense(inputs=self.fc2, units=50, activation=tf.nn.elu,
kernel_initializer=tf.contrib.layers.xavier_initializer(), name='fc3')
# FC4, Output units = 10
self.fc4 = tf.layers.dense(inputs=self.fc3, units=10, activation=tf.nn.elu,
kernel_initializer=tf.contrib.layers.xavier_initializer(), name='fc4')
# Output
self.output = tf.layers.dense(inputs=self.flatten, units=self.output_units, name='DL')
# Cost
#self.loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits=self.output, labels=self.outputs_))
self.loss = tf.losses.mean_squared_error(labels=self.outputs_, predictions=self.output)
# Optimizer
self.optimizer = tf.train.AdamOptimizer(self.learning_rate).minimize(self.loss)
I am creating an instance of this class and then training and saving it. Now, after restoring the saved model in another file, I want to get the output produced by self.output.
This is the code I have written for the second file:
with tf.Session() as sess:
saver = tf.train.import_meta_graph("./models/drivingModel_{LR}_{EP}_{MN}.ckpt.meta".format(LR=learning_rate, EP=epochs, MN=mod_number))
saver.restore(sess, "./models/drivingModel_{LR}_{EP}_{MN}.ckpt".format(LR=learning_rate, EP=epochs, MN=mod_number))
frontImage = cv2.imread('Front_5.png')
leftImage = cv2.imread('Left_5.png')
rightImage = cv2.imread('Right_5.png')
front = cv2.resize(cv2.cvtColor(frontImage, cv2.COLOR_RGB2GRAY), (h, w))
left = cv2.resize(cv2.cvtColor(leftImage, cv2.COLOR_RGB2GRAY), (h, w))
right = cv2.resize(cv2.cvtColor(rightImage, cv2.COLOR_RGB2GRAY), (h, w))
images = np.stack((front, left, right)) # num_cameras * h * w
images = np.moveaxis(images, [0, 1, 2], [2, 0, 1])
X =
X.append(images)
X = np.array(X)
print(tf.get_default_graph().get_all_collection_keys())
all_variables = tf.get_collection('variables')
print("Variables of the tensor:")
for var in all_variables:
print(var)
graph = sess.graph
print("Operations of the tensor: ")
operations = tf.get_collection('train_op')
for op in graph.get_operations():
print(op)
I want to pass the X as a feeddict parameter to self.inputs_ and get the output produced by self.output (named as 'DL').
I couldn't figure out which variables and operations can I use to perform the aforementioned task.
I can get the input placeholder using:
inputs_ = tf.get_collection('inputs')[0]
But how can I pass X to it and use it to get the value produced at self.output layer?
Edit:
I was able to solve it by creating a new instance of the class and then running the following code:
tf.reset_default_graph()
drivingNet = cnn_model.EENetwork(state_size=state_size,
learning_rate=learning_rate, output_units=classes)
with tf.Session() as sess:
saver = tf.train.Saver(tf.global_variables())
saver = tf.train.import_meta_graph("./models/drivingModel_{LR}_{EP}_{MN}.ckpt.meta".format(LR=learning_rate, EP=epochs, MN=mod_number))
saver.restore(sess, "./models/drivingModel_{LR}_{EP}_{MN}.ckpt".format(LR=learning_rate, EP=epochs, MN=mod_number))
frontImage = cv2.imread('Front_5.png')
leftImage = cv2.imread('Left_5.png')
rightImage = cv2.imread('Right_5.png')
front = cv2.resize(cv2.cvtColor(frontImage, cv2.COLOR_RGB2GRAY), (h, w))
left = cv2.resize(cv2.cvtColor(leftImage, cv2.COLOR_RGB2GRAY), (h, w))
right = cv2.resize(cv2.cvtColor(rightImage, cv2.COLOR_RGB2GRAY), (h, w))
images = np.stack((front, left, right)) # num_cameras * h * w
# Move the axis to make it h * w * num_cameras
images = np.moveaxis(images, [0, 1, 2], [2, 0, 1])
# images = np.concatenate((leftImage, frontImage), axis=1)
# images = np.concatenate((images, rightImage), axis=1)
# Input needs to be passed as samples * h * w * num_cameras
X =
X.append(images)
X = np.array(X) # This makes it 1 * h * w * cameras
decision = sess.run(drivingNet.output, feed_dict={drivingNet.inputs_: X})
temp = decision.tolist()
flat_list = [item for sublist in temp for item in sublist]
print(flat_list)
However, I still don't understand what saver = tf.train.Saver(tf.global_variables()) is doing in this case? Why is this line of code required? If I do not include this line, it throws an "attempting to use uninitialized value" error. Shouldn't the meta graph and saver.restore initialize all values to the saved values?
python tensorflow
My tensorflow model is defined in a class as follows:
class EENetwork:
def __init__(self, state_size, learning_rate, output_units, name='EENetwork'):
self.learning_rate = learning_rate
self.state_size = state_size # State size = height * width * cameras
self.output_units = output_units
with tf.variable_scope(name):
self.inputs_ = tf.placeholder(tf.float32, [None, *state_size], name='inputs')
self.outputs_ = tf.placeholder(tf.float32, [None, self.output_units], name='outputs_') # To hold the training Y
# Placeholders aren't added to the saved graph by default
tf.add_to_collection('inputs', self.inputs_)
tf.add_to_collection('outputs_', self.outputs_)
# Conv1 = samples * h * w * 24
# Assume h, w = 64, so samples * 64 * 64 * 24
self.conv1 = tf.layers.conv2d(inputs=self.inputs_, filters=24, kernel_size=[5, 5],
padding='same', activation=tf.nn.relu, name='conv1')
# Maxpool1 = samples * h/2 * w/2 * 12 = samples * 32 * 32 * 24
self.maxpool1 = tf.layers.max_pooling2d(inputs=self.conv1, pool_size=[2, 2], strides=2, name='mp1')
# Conv2 = samples * 32 * 32* 36
self.conv2 = tf.layers.conv2d(inputs=self.maxpool1, filters=36, kernel_size=[5, 5],
padding='same', activation=tf.nn.relu, name='conv2')
# Maxpool2 = samples * 16 * 16 * 36
self.maxpool2 = tf.layers.max_pooling2d(inputs=self.conv2, pool_size=[2, 2], strides=2, name='mp2')
# Conv3 = samples * 16 * 16 * 48
self.conv3 = tf.layers.conv2d(inputs=self.maxpool2, filters=48, kernel_size=[3, 3],
padding='same', activation=tf.nn.relu, name='conv3')
# Maxpool3 = samples * 8 * 8 * 48
self.maxpool3 = tf.layers.max_pooling2d(inputs=self.conv3, pool_size=[2, 2], strides=2, name='mp3')
# Conv4 = samples * 8 * 8 * 64
self.conv4 = tf.layers.conv2d(inputs=self.maxpool3, filters=64, kernel_size=[3, 3],
padding='same', activation=tf.nn.relu, name='conv4')
# Maxpool4 = samples * 4 * 4 * 64
self.maxpool4 = tf.layers.max_pooling2d(inputs=self.conv4, pool_size=[2, 2], strides=2, name='mp4')
# Flatten = samples * 1024
self.flatten = tf.contrib.layers.flatten(self.maxpool4)
# FC1, Output units = 1164
self.fc1 = tf.layers.dense(inputs=self.flatten, units=1164, activation=tf.nn.elu,
kernel_initializer=tf.contrib.layers.xavier_initializer(), name='fc1')
# FC2, Output units = 100
self.fc2 = tf.layers.dense(inputs=self.fc1, units=100, activation=tf.nn.elu,
kernel_initializer=tf.contrib.layers.xavier_initializer(), name='fc2')
# FC3, Output units = 50
self.fc3 = tf.layers.dense(inputs=self.fc2, units=50, activation=tf.nn.elu,
kernel_initializer=tf.contrib.layers.xavier_initializer(), name='fc3')
# FC4, Output units = 10
self.fc4 = tf.layers.dense(inputs=self.fc3, units=10, activation=tf.nn.elu,
kernel_initializer=tf.contrib.layers.xavier_initializer(), name='fc4')
# Output
self.output = tf.layers.dense(inputs=self.flatten, units=self.output_units, name='DL')
# Cost
#self.loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits=self.output, labels=self.outputs_))
self.loss = tf.losses.mean_squared_error(labels=self.outputs_, predictions=self.output)
# Optimizer
self.optimizer = tf.train.AdamOptimizer(self.learning_rate).minimize(self.loss)
I am creating an instance of this class and then training and saving it. Now, after restoring the saved model in another file, I want to get the output produced by self.output.
This is the code I have written for the second file:
with tf.Session() as sess:
saver = tf.train.import_meta_graph("./models/drivingModel_{LR}_{EP}_{MN}.ckpt.meta".format(LR=learning_rate, EP=epochs, MN=mod_number))
saver.restore(sess, "./models/drivingModel_{LR}_{EP}_{MN}.ckpt".format(LR=learning_rate, EP=epochs, MN=mod_number))
frontImage = cv2.imread('Front_5.png')
leftImage = cv2.imread('Left_5.png')
rightImage = cv2.imread('Right_5.png')
front = cv2.resize(cv2.cvtColor(frontImage, cv2.COLOR_RGB2GRAY), (h, w))
left = cv2.resize(cv2.cvtColor(leftImage, cv2.COLOR_RGB2GRAY), (h, w))
right = cv2.resize(cv2.cvtColor(rightImage, cv2.COLOR_RGB2GRAY), (h, w))
images = np.stack((front, left, right)) # num_cameras * h * w
images = np.moveaxis(images, [0, 1, 2], [2, 0, 1])
X =
X.append(images)
X = np.array(X)
print(tf.get_default_graph().get_all_collection_keys())
all_variables = tf.get_collection('variables')
print("Variables of the tensor:")
for var in all_variables:
print(var)
graph = sess.graph
print("Operations of the tensor: ")
operations = tf.get_collection('train_op')
for op in graph.get_operations():
print(op)
I want to pass the X as a feeddict parameter to self.inputs_ and get the output produced by self.output (named as 'DL').
I couldn't figure out which variables and operations can I use to perform the aforementioned task.
I can get the input placeholder using:
inputs_ = tf.get_collection('inputs')[0]
But how can I pass X to it and use it to get the value produced at self.output layer?
Edit:
I was able to solve it by creating a new instance of the class and then running the following code:
tf.reset_default_graph()
drivingNet = cnn_model.EENetwork(state_size=state_size,
learning_rate=learning_rate, output_units=classes)
with tf.Session() as sess:
saver = tf.train.Saver(tf.global_variables())
saver = tf.train.import_meta_graph("./models/drivingModel_{LR}_{EP}_{MN}.ckpt.meta".format(LR=learning_rate, EP=epochs, MN=mod_number))
saver.restore(sess, "./models/drivingModel_{LR}_{EP}_{MN}.ckpt".format(LR=learning_rate, EP=epochs, MN=mod_number))
frontImage = cv2.imread('Front_5.png')
leftImage = cv2.imread('Left_5.png')
rightImage = cv2.imread('Right_5.png')
front = cv2.resize(cv2.cvtColor(frontImage, cv2.COLOR_RGB2GRAY), (h, w))
left = cv2.resize(cv2.cvtColor(leftImage, cv2.COLOR_RGB2GRAY), (h, w))
right = cv2.resize(cv2.cvtColor(rightImage, cv2.COLOR_RGB2GRAY), (h, w))
images = np.stack((front, left, right)) # num_cameras * h * w
# Move the axis to make it h * w * num_cameras
images = np.moveaxis(images, [0, 1, 2], [2, 0, 1])
# images = np.concatenate((leftImage, frontImage), axis=1)
# images = np.concatenate((images, rightImage), axis=1)
# Input needs to be passed as samples * h * w * num_cameras
X =
X.append(images)
X = np.array(X) # This makes it 1 * h * w * cameras
decision = sess.run(drivingNet.output, feed_dict={drivingNet.inputs_: X})
temp = decision.tolist()
flat_list = [item for sublist in temp for item in sublist]
print(flat_list)
However, I still don't understand what saver = tf.train.Saver(tf.global_variables()) is doing in this case? Why is this line of code required? If I do not include this line, it throws an "attempting to use uninitialized value" error. Shouldn't the meta graph and saver.restore initialize all values to the saved values?
python tensorflow
python tensorflow
edited Nov 25 '18 at 20:27
Mr_Feynman
asked Nov 25 '18 at 16:42
Mr_FeynmanMr_Feynman
86
86
add a comment |
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53469647%2frestoring-tensorflow-model-as-a-class-and-running-a-specific-layer%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53469647%2frestoring-tensorflow-model-as-a-class-and-running-a-specific-layer%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown