I am trying to build this LSTM network and I ran into this error constantly. I looked it up on Google and am still not sure what is going on. I have tried adding this:with tf.variable_scope("cell1"). Still not working. Any help is greatly appreciated.
Here is the code of the main LSTM structure
def lstm_structure(self):
train_dataset, train_labels, valid_dataset, valid_labels, test_dataset, test_labels = self.data_preprocessing(
'SSE_Composite_Index.csv')
graph=tf.Graph()
with graph.as_default():
'''Placeholders'''
X=tf.placeholder(tf.float32,shape=[None,self.num_steps,self.input_size])
y=tf.placeholder(tf.float32,shape=[None,self.num_classes])
valid=tf.constant(valid_dataset)
test=tf.constant(test_dataset)
'''Weights'''
weights={'in':tf.Variable(tf.random_normal([self.input_size,self.num_neurons])),'out':tf.Variable(tf.random_normal([self.num_neurons,self.num_classes]))}
'''Biases'''
biases={'in':tf.Variable(tf.zeros(shape=[self.num_neurons,])),'out':tf.Variable(tf.zeros(shape=[self.num_classes,]))}
def lstm(X, weights, biases, reuse=True
):
'''from input to cell'''
with tf.variable_scope("foo") as f:
if reuse:
f.reuse_variables()
X = tf.reshape(X, shape=[-1, self.input_size])
X_in = tf.matmul(X, weights['in']) + biases['in']
X_in = tf.reshape(X_in, shape=[-1, self.num_steps, self.num_neurons])
'''cell'''
cell_ = tf.contrib.rnn.BasicLSTMCell(self.num_neurons, forget_bias=1, state_is_tuple=True)
_init_state = cell_.zero_state(self.batch_size, dtype=tf.float32)
outputs, states = tf.nn.dynamic_rnn(cell_, X_in, initial_state=_init_state, time_major=False, dtype=tf.float32)
'''from cell to output'''
results = tf.matmul(states[1], weights['out']) + biases['out']
return results
logits=lstm(X,weights,biases,False)
loss=tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits,labels=y))
optimizer = tf.train.AdamOptimizer(learning_rate=self.learning_rate).minimize(loss)
train_prediction=tf.nn.softmax(logits=logits)
valid_prediction=tf.nn.softmax(lstm(valid,weights,biases,True))
test_prediction=tf.nn.softmax(lstm(test,weights,biases,True))
with tf.Session(graph=graph) as session:
tf.initialize_all_variables().run()
print('Initialised')
loss_list = []
for step in range(self.num_epochs):
if step==0:
i = 0
while i < len(train_dataset):
start = i
end = i + self.batch_size
batch_data = train_dataset[start:end, :]
batch_label = train_labels[start:end, :]
i += self.batch_size
a, b, prediction = session.run([optimizer, loss, train_prediction],
feed_dict={X: batch_data, y: batch_label})
loss_list.append(b)
else:
i = 0
while i < len(train_dataset):
start = i
end = i + self.batch_size
batch_data = train_dataset[start:end, :]
batch_label = train_labels[start:end, :]
i += self.batch_size
a, b, prediction = session.run([optimizer, loss, train_prediction],
feed_dict={X: batch_data, y: batch_label})
loss_list.append(b)
if step % 10 == 0:
print('Step', step, 'Loss', b)
print('Training Accuracy', self.accuracy(prediction, batch_label), '%')
print('Validation Accuracy',
self.accuracy(valid_prediction.eval(), valid_labels), '%')
print('test Accuracy', self.accuracy(test_prediction.eval(), test_labels), '%')
print('Finished')
The error message that I am getting is:
Traceback (most recent call last):
File "C:/Users/LiXin/PycharmProjects/PythonProjects/LSTM_CLASSIFIER.py", line 114, in <module>
lstm.LSTM_structure()
File "C:/Users/LiXin/PycharmProjects/PythonProjects/LSTM_CLASSIFIER.py", line 87, in LSTM_structure
valid_prediction=tf.nn.softmax(LSTM(valid,weights,biases))
File "C:/Users/LiXin/PycharmProjects/PythonProjects/LSTM_CLASSIFIER.py", line 75, in LSTM
outputs,states=tf.nn.dynamic_rnn(cell_,X_in,initial_state=_init_state,time_major=False,dtype=tf.float32)
File "C:\Users\LiXin\PycharmProjects\PythonProjects\venv\lib\site-packages\tensorflow\python\ops\rnn.py", line 614, in dynamic_rnn
dtype=dtype)
File "C:\Users\LiXin\PycharmProjects\PythonProjects\venv\lib\site-packages\tensorflow\python\ops\rnn.py", line 777, in _dynamic_rnn_loop
swap_memory=swap_memory)
File "C:\Users\LiXin\PycharmProjects\PythonProjects\venv\lib\site-packages\tensorflow\python\ops\control_flow_ops.py", line 2816, in while_loop
result = loop_context.BuildLoop(cond, body, loop_vars, shape_invariants)
File "C:\Users\LiXin\PycharmProjects\PythonProjects\venv\lib\site-packages\tensorflow\python\ops\control_flow_ops.py", line 2640, in BuildLoop
pred, body, original_loop_vars, loop_vars, shape_invariants)
File "C:\Users\LiXin\PycharmProjects\PythonProjects\venv\lib\site-packages\tensorflow\python\ops\control_flow_ops.py", line 2590, in _BuildLoop
body_result = body(*packed_vars_for_body)
File "C:\Users\LiXin\PycharmProjects\PythonProjects\venv\lib\site-packages\tensorflow\python\ops\rnn.py", line 762, in _time_step
(output, new_state) = call_cell()
File "C:\Users\LiXin\PycharmProjects\PythonProjects\venv\lib\site-packages\tensorflow\python\ops\rnn.py", line 748, in <lambda>
call_cell = lambda: cell(input_t, state)
File "C:\Users\LiXin\PycharmProjects\PythonProjects\venv\lib\site-packages\tensorflow\python\ops\rnn_cell_impl.py", line 183, in __call__
return super(RNNCell, self).__call__(inputs, state)
File "C:\Users\LiXin\PycharmProjects\PythonProjects\venv\lib\site-packages\tensorflow\python\layers\base.py", line 575, in __call__
outputs = self.call(inputs, *args, **kwargs)
File "C:\Users\LiXin\PycharmProjects\PythonProjects\venv\lib\site-packages\tensorflow\python\ops\rnn_cell_impl.py", line 438, in call
self._linear = _Linear([inputs, h], 4 * self._num_units, True)
File "C:\Users\LiXin\PycharmProjects\PythonProjects\venv\lib\site-packages\tensorflow\python\ops\rnn_cell_impl.py", line 1171, in __init__
initializer=kernel_initializer)
File "C:\Users\LiXin\PycharmProjects\PythonProjects\venv\lib\site-packages\tensorflow\python\ops\variable_scope.py", line 1203, in get_variable
constraint=constraint)
File "C:\Users\LiXin\PycharmProjects\PythonProjects\venv\lib\site-packages\tensorflow\python\ops\variable_scope.py", line 1092, in get_variable
constraint=constraint)
File "C:\Users\LiXin\PycharmProjects\PythonProjects\venv\lib\site-packages\tensorflow\python\ops\variable_scope.py", line 417, in get_variable
return custom_getter(**custom_getter_kwargs)
File "C:\Users\LiXin\PycharmProjects\PythonProjects\venv\lib\site-packages\tensorflow\python\ops\rnn_cell_impl.py", line 186, in _rnn_get_variable
variable = getter(*args, **kwargs)
File "C:\Users\LiXin\PycharmProjects\PythonProjects\venv\lib\site-packages\tensorflow\python\ops\variable_scope.py", line 394, in _true_getter
use_resource=use_resource, constraint=constraint)
File "C:\Users\LiXin\PycharmProjects\PythonProjects\venv\lib\site-packages\tensorflow\python\ops\variable_scope.py", line 742, in _get_single_variable
name, "".join(traceback.format_list(tb))))
ValueError: Variable rnn/basic_lstm_cell/kernel already exists, disallowed. Did you mean to set reuse=True or reuse=tf.AUTO_REUSE in VarScope? Originally defined at:
File "C:\Users\LiXin\PycharmProjects\PythonProjects\venv\lib\site-packages\tensorflow\python\framework\ops.py", line 1470, in __init__
self._traceback = self._graph._extract_stack() # pylint: disable=protected-access
File "C:\Users\LiXin\PycharmProjects\PythonProjects\venv\lib\site-packages\tensorflow\python\framework\ops.py", line 2956, in create_op
op_def=op_def)
File "C:\Users\LiXin\PycharmProjects\PythonProjects\venv\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 787, in _apply_op_helper
op_def=op_def)
The following code works, however.
def RNN_neural_network_model(x):
layer={'weights':tf.Variable(tf.random_normal([rnn_size,n_classes])),
'biases':tf.Variable(tf.random_normal([n_classes]))}
x=tf.transpose(x,[1,0,2])
x=tf.reshape(x,[-1,chunk_size])
x=tf.split(x,n_chunks,0)
lstm_cell=tf.contrib.rnn.BasicLSTMCell(rnn_size,state_is_tuple=True)
# stacked_lstm = tf.contrib.rnn.MultiRNNCell(
# [lstmcell(rnn_size) for _ in range(num_layers)])
outputs, states=rnn.static_rnn(lstm_cell,x,dtype=tf.float32)
output = tf.matmul(outputs[-1], layer['weights'])+ layer['biases']
return output
You should wrap your LSTM definition inside a variable scope and then reuse it for validation and testing. Try the following
def LSTM(X,weights,biases, reuse=reuse):
'''from input to cell'''
with tf.variable_scope("foo") as f:
if reuse:
f.reuse_variables()
X=tf.reshape(X,shape=[-1,self.input_size])
X_in=tf.matmul(X,weights['in'])+biases['in']
X_in=tf.reshape(X_in,shape=[-1,self.num_steps,self.num_neurons])
'''cell'''
cell_=tf.contrib.rnn.BasicLSTMCell(self.num_neurons,forget_bias=1,state_is_tuple=True)
_init_state=cell_.zero_state(self.batch_size,dtype=tf.float32)
outputs,states=tf.nn.dynamic_rnn(cell_,X_in,initial_state=_init_state,time_major=False,dtype=tf.float32)
'''from cell to output'''
results=tf.matmul(states[1],weights['out'])+biases['out']
return results
Change your training, testing code as below
logits=LSTM(X,weights,biases,False)
loss=tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits,labels=y))
optimizer = tf.train.AdamOptimizer(learning_rate=self.learning_rate).minimize(loss)
train_prediction=tf.nn.softmax(logits=logits)
valid_prediction=tf.nn.softmax(LSTM(valid,weights,biases, True))
test_prediction=tf.nn.softmax(LSTM(test,weights,biases, True))
Related
I wrote this code which gives me this error "Graph execution error". On my friend's pc the code works without errors. what could be the problem?
I've checked the versions of the various libraries and everything seems to be the same.
I'm working in spyder environment and I don't understand what the problem is.
This is the error
File "C:\Users\hp\anaconda3\envs\unet\lib\site-packages\spyder_kernels\py3compat.py", line 356, in compat_exec
exec(code, globals, locals)
File "c:\users\hp\desktop\universita_laurea_magistrale\tirocinio\dataset\codiceprof.py", line 147, in <module>
storia=model.fit(train_gen, epochs=epochs, validation_data=train_gen)
File "C:\Users\hp\anaconda3\envs\unet\lib\site-packages\keras\utils\traceback_utils.py", line 70, in error_handler
raise e.with_traceback(filtered_tb) from None
File "C:\Users\hp\anaconda3\envs\unet\lib\site-packages\tensorflow\python\eager\execute.py", line 54, in quick_execute
tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
InvalidArgumentError: Graph execution error:
Detected at node 'sparse_categorical_crossentropy/SparseSoftmaxCrossEntropyWithLogits/SparseSoftmaxCrossEntropyWithLogits' defined at (most recent call last):
File "C:\Users\hp\anaconda3\envs\unet\lib\runpy.py", line 197, in _run_module_as_main
return _run_code(code, main_globals, None,
File "C:\Users\hp\anaconda3\envs\unet\lib\runpy.py", line 87, in _run_code
exec(code, run_globals)
File "C:\Users\hp\anaconda3\envs\unet\lib\site-packages\spyder_kernels\console\__main__.py", line 24, in <module>
start.main()
File "C:\Users\hp\anaconda3\envs\unet\lib\site-packages\spyder_kernels\console\start.py", line 340, in main
kernel.start()
File "C:\Users\hp\anaconda3\envs\unet\lib\site-packages\ipykernel\kernelapp.py", line 712, in start
self.io_loop.start()
File "C:\Users\hp\anaconda3\envs\unet\lib\site-packages\tornado\platform\asyncio.py", line 215, in start
self.asyncio_loop.run_forever()
File "C:\Users\hp\anaconda3\envs\unet\lib\asyncio\base_events.py", line 596, in run_forever
self._run_once()
File "C:\Users\hp\anaconda3\envs\unet\lib\asyncio\base_events.py", line 1890, in _run_once
handle._run()
File "C:\Users\hp\anaconda3\envs\unet\lib\asyncio\events.py", line 80, in _run
self._context.run(self._callback, *self._args)
File "C:\Users\hp\anaconda3\envs\unet\lib\site-packages\ipykernel\kernelbase.py", line 510, in dispatch_queue
await self.process_one()
File "C:\Users\hp\anaconda3\envs\unet\lib\site-packages\ipykernel\kernelbase.py", line 499, in process_one
await dispatch(*args)
File "C:\Users\hp\anaconda3\envs\unet\lib\site-packages\ipykernel\kernelbase.py", line 406, in dispatch_shell
await result
File "C:\Users\hp\anaconda3\envs\unet\lib\site-packages\ipykernel\kernelbase.py", line 730, in execute_request
reply_content = await reply_content
File "C:\Users\hp\anaconda3\envs\unet\lib\site-packages\ipykernel\ipkernel.py", line 390, in do_execute
res = shell.run_cell(code, store_history=store_history, silent=silent)
File "C:\Users\hp\anaconda3\envs\unet\lib\site-packages\ipykernel\zmqshell.py", line 528, in run_cell
return super().run_cell(*args, **kwargs)
File "C:\Users\hp\anaconda3\envs\unet\lib\site-packages\IPython\core\interactiveshell.py", line 2914, in run_cell
result = self._run_cell(
File "C:\Users\hp\anaconda3\envs\unet\lib\site-packages\IPython\core\interactiveshell.py", line 2960, in _run_cell
return runner(coro)
File "C:\Users\hp\anaconda3\envs\unet\lib\site-packages\IPython\core\async_helpers.py", line 78, in _pseudo_sync_runner
coro.send(None)
File "C:\Users\hp\anaconda3\envs\unet\lib\site-packages\IPython\core\interactiveshell.py", line 3185, in run_cell_async
has_raised = await self.run_ast_nodes(code_ast.body, cell_name,
File "C:\Users\hp\anaconda3\envs\unet\lib\site-packages\IPython\core\interactiveshell.py", line 3377, in run_ast_nodes
if (await self.run_code(code, result, async_=asy)):
File "C:\Users\hp\anaconda3\envs\unet\lib\site-packages\IPython\core\interactiveshell.py", line 3457, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "C:\Users\hp\AppData\Local\Temp\ipykernel_9760\815882102.py", line 1, in <module>
runfile('C:/Users/hp/Desktop/Universita_laurea_Magistrale/TIROCINIO/Dataset/codiceprof.py', wdir='C:/Users/hp/Desktop/Universita_laurea_Magistrale/TIROCINIO/Dataset')
File "C:\Users\hp\anaconda3\envs\unet\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 524, in runfile
return _exec_file(
File "C:\Users\hp\anaconda3\envs\unet\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 611, in _exec_file
exec_code(file_code, filename, ns_globals, ns_locals,
File "C:\Users\hp\anaconda3\envs\unet\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 469, in exec_code
exec_fun(compile(ast_code, filename, 'exec'), ns_globals, ns_locals)
File "C:\Users\hp\anaconda3\envs\unet\lib\site-packages\spyder_kernels\py3compat.py", line 356, in compat_exec
exec(code, globals, locals)
File "c:\users\hp\desktop\universita_laurea_magistrale\tirocinio\dataset\codiceprof.py", line 147, in <module>
storia=model.fit(train_gen, epochs=epochs, validation_data=train_gen)
File "C:\Users\hp\anaconda3\envs\unet\lib\site-packages\keras\utils\traceback_utils.py", line 65, in error_handler
return fn(*args, **kwargs)
File "C:\Users\hp\anaconda3\envs\unet\lib\site-packages\keras\engine\training.py", line 1564, in fit
tmp_logs = self.train_function(iterator)
File "C:\Users\hp\anaconda3\envs\unet\lib\site-packages\keras\engine\training.py", line 1160, in train_function
return step_function(self, iterator)
File "C:\Users\hp\anaconda3\envs\unet\lib\site-packages\keras\engine\training.py", line 1146, in step_function
outputs = model.distribute_strategy.run(run_step, args=(data,))
File "C:\Users\hp\anaconda3\envs\unet\lib\site-packages\keras\engine\training.py", line 1135, in run_step
outputs = model.train_step(data)
File "C:\Users\hp\anaconda3\envs\unet\lib\site-packages\keras\engine\training.py", line 994, in train_step
loss = self.compute_loss(x, y, y_pred, sample_weight)
File "C:\Users\hp\anaconda3\envs\unet\lib\site-packages\keras\engine\training.py", line 1052, in compute_loss
return self.compiled_loss(
File "C:\Users\hp\anaconda3\envs\unet\lib\site-packages\keras\engine\compile_utils.py", line 265, in __call__
loss_value = loss_obj(y_t, y_p, sample_weight=sw)
File "C:\Users\hp\anaconda3\envs\unet\lib\site-packages\keras\losses.py", line 152, in __call__
losses = call_fn(y_true, y_pred)
File "C:\Users\hp\anaconda3\envs\unet\lib\site-packages\keras\losses.py", line 272, in call
return ag_fn(y_true, y_pred, **self._fn_kwargs)
File "C:\Users\hp\anaconda3\envs\unet\lib\site-packages\keras\losses.py", line 2084, in sparse_categorical_crossentropy
return backend.sparse_categorical_crossentropy(
File "C:\Users\hp\anaconda3\envs\unet\lib\site-packages\keras\backend.py", line 5630, in sparse_categorical_crossentropy
res = tf.nn.sparse_softmax_cross_entropy_with_logits(
Node: 'sparse_categorical_crossentropy/SparseSoftmaxCrossEntropyWithLogits/SparseSoftmaxCrossEntropyWithLogits'
logits and labels must have the same first dimension, got logits shape [2048,2] and labels shape [131072]
[[{{node sparse_categorical_crossentropy/SparseSoftmaxCrossEntropyWithLogits/SparseSoftmaxCrossEntropyWithLogits}}]] [Op:__inference_train_function_14691]
from tensorflow import keras
import numpy as np
from tensorflow.keras.preprocessing.image import load_img
from tensorflow.keras.preprocessing.image import img_to_array
import PIL
from keras.models import Model
from keras.layers import Input, Conv2D, MaxPooling2D, UpSampling2D, concatenate, Conv2DTranspose, BatchNormalization, Dropout, Lambda
from keras.optimizers import Adam
from keras.layers import Activation, MaxPool2D, Concatenate
import glob
import matplotlib
import matplotlib.pyplot as plt
from matplotlib import pyplot as plt
from tensorflow.keras import layers
class leggi_immagini(keras.utils.Sequence):
def __init__(self, batch_size, img_size, input_img_paths, target_img_paths):
self.batch_size = batch_size
self.img_size = img_size
self.input_img_paths = input_img_paths
self.target_img_paths = target_img_paths
def __len__(self):
return len(self.target_img_paths) // self.batch_size
def __getitem__(self, idx):
"""Returns tuple (input, target) correspond to batch #idx."""
i = idx * self.batch_size
batch_input_img_paths = self.input_img_paths[i : i + self.batch_size]
batch_target_img_paths = self.target_img_paths[i : i + self.batch_size]
x = np.zeros((self.batch_size,) + self.img_size +(1,), dtype="float32")
for j, path in enumerate(batch_input_img_paths):
#print("eccomi")
img = load_img(path, color_mode='grayscale')
x[j] = img_to_array(img) #/ 8191 # normalizza al massimo
y = np.zeros((self.batch_size,) + self.img_size + (1,), dtype="uint8")
for j, path2 in enumerate(batch_target_img_paths):
img2 = load_img(path2, color_mode="grayscale")
y[j] = img_to_array(img2) / 255
return x, y
def get_model(img_size, num_classes):
inputs = keras.Input(shape=img_size + (1,))
### [First half of the network: downsampling inputs] ###
# Entry block
x = layers.Conv2D(32, (3,3),strides=2,padding="same",input_shape=(256,256,1))(inputs)# strides=2, padding="same")(inputs)
x = layers.BatchNormalization()(x)
x = layers.Activation("relu")(x)
previous_block_activation = x # Set aside residual
# Blocks 1, 2, 3 are identical apart from the feature depth.
for filters in [64, 128, 256]:
x = layers.Activation("relu")(x)
x = layers.SeparableConv2D(filters, 3, padding="same")(x)
x = layers.BatchNormalization()(x)
x = layers.Activation("relu")(x)
x = layers.SeparableConv2D(filters, 3, padding="same")(x)
x = layers.BatchNormalization()(x)
x = layers.MaxPooling2D(3, strides=2, padding="same")(x)
# Project residual
residual = layers.Conv2D(filters, 1, strides=2, padding="same")(
previous_block_activation
)
x = layers.add([x, residual]) # Add back residual
previous_block_activation = x # Set aside next residual
### [Second half of the network: upsampling inputs] ###
for filters in [256, 128, 64, 32]:
x = layers.Activation("relu")(x)
x = layers.Conv2DTranspose(filters, 3, padding="same")(x)
x = layers.BatchNormalization()(x)
x = layers.Activation("relu")(x)
x = layers.Conv2DTranspose(filters, 3, padding="same")(x)
x = layers.BatchNormalization()(x)
x = layers.UpSampling2D(2)(x)
# Project residual
residual = layers.UpSampling2D(2)(previous_block_activation)
residual = layers.Conv2D(filters, 1, padding="same")(residual)
x = layers.add([x, residual]) # Add back residual
previous_block_activation = x # Set aside next residual
# Add a per-pixel classification layer
outputs = layers.Conv2D(num_classes, 3, activation="softmax", padding="same")(x)
# Define the model
model = keras.Model(inputs, outputs)
return model
epochs=5;
batch_size=2;
img_size=(256,256)
inputdir= 'C://Users//hp//Desktop//Universita_laurea_Magistrale//TIROCINIO//Imm//immagine_uscita//'
input_path= glob.glob(inputdir+ '*.tif')
maskdir= 'C://Users//hp//Desktop//Universita_laurea_Magistrale//TIROCINIO//Imm//maschera_uscita//'
mask_path= glob.glob(maskdir+ '*.tif')
train_gen= leggi_immagini(batch_size,img_size,input_path,mask_path)
model= get_model(img_size,num_classes=2) #configuro il modello per il training
model.summary()
model.compile(optimizer="rmsprop", loss="sparse_categorical_crossentropy")
storia=model.fit(train_gen, epochs=epochs, validation_data=train_gen)
valori=model.predict(train_gen)
plt.plot(storia.history['loss'])
plt.plot(storia.history['val_loss'])
I am trying to implement an autoencoder using a datagenerator, the autoencoder works fine if I implement it directly with X_train, but when I try to use the data generator always returns that error.
#!/usr/bin/env python
# coding: utf-8
import zipfile
import pandas as pd
import tensorflow as tf
from tensorflow.keras.models import Sequential, Model
from tensorflow.keras.layers import Dense, Conv2D, Flatten, Activation, MaxPooling2D, Reshape, Input, Dropout
from tensorflow.keras.layers import BatchNormalization, Lambda, Conv2DTranspose, Add
from tensorflow.keras import regularizers
from tensorflow.keras import initializers
from tensorflow.keras import constraints
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.utils import Sequence
import random
import numpy as np
from sklearn.preprocessing import MinMaxScaler
from matplotlib import pyplot as plt
# descargamos el conjunto de datos
train_zip_path = tf.keras.utils.get_file(
fname='train_femto.zip',
origin='https://hdvirtual.us.es/discovirt/index.php/s/z75ekAqY3tt3aKy/download',
cache_subdir='datasets', extract=False,
)
WINDOW_SIZE = 500
HEADERS = ['Hour', 'Minute', 'Second', 'Microsecond', 'H-acc', 'V-acc']
FEATURES = ['H-acc', 'V-acc']
TEST_RULS = {
'Bearing1_3': 5730,
'Bearing1_4': 339,
'Bearing1_5': 1610,
'Bearing1_6': 1460,
'Bearing1_7': 7570,
'Bearing2_3': 7530,
'Bearing2_4': 1390,
'Bearing2_5': 3090,
'Bearing2_6': 1290,
'Bearing2_7': 580,
'Bearing3_3': 820
}
def read_femto_file(z, file_path, bearing):
with z.open(file_path) as f:
X_aux = pd.read_csv(f, names=HEADERS, delimiter=",")
X_aux['Bearing'] = bearing.split('/')[-1][-3:]
del X_aux['Hour']
del X_aux['Minute']
del X_aux['Second']
del X_aux['Microsecond']
return X_aux
def read_femto_dataset(file_path, RULS=None):
datasets = []
with zipfile.ZipFile(file_path) as z:
files = z.namelist()
dirs = sorted(set(['/'.join(f.split('/')[:-1]) for f in files if len(f.split('/')) > 2]))
ds = []
for bearing in dirs:
bearing_name = bearing.split('/')[-1]
print("Reading", bearing_name)
bearing_files = sorted([f for f in files if bearing in f and 'acc' in f])
for i, bearing_file in enumerate(bearing_files):
ds.append(read_femto_file(z, bearing_file, bearing))
X = pd.concat(ds, axis=0)
X = X.reset_index(drop=True)
# compute RUL
X['RUL'] = (X.index / 256).astype('int')[::-1].values
if RULS is not None:
X['RUL'] += RULS[bearing.split('/')[-1]]
X.RUL = X.RUL.astype('int32')
datasets.append(X)
X = pd.concat(datasets, axis=0)
X['H-acc'] = X['H-acc'].astype('float32')
X['V-acc'] = X['V-acc'].astype('float32') #errata, H-acc en vez de V-acc
X['RUL'] = X['RUL'].astype('int32')
return X
# leemos el conjunto de entrenamiento
X_train = read_femto_dataset(train_zip_path)
# truncamos el RUL y lo normalzamos entre 100 y 0
X_train['RUL'] = X_train.RUL.clip(0, 10000) / 100
# dividimos entre entrenamiento y validación
X_val = X_train[X_train.Bearing.isin(['1_1', '2_1', '3_1'])]
X_train = X_train[X_train.Bearing.isin(['1_2', '2_2', '3_2'])]
# normalizamos los sensores
min_max = {}
for feature in FEATURES:
# calculamos los parámetros de la normalización con el train
min_max[feature] = {}
min_max[feature]['min'] = X_train[feature].min()
min_max[feature]['max'] = X_train[feature].max()
# normalizamos ambos datasets
X_train[feature] = ((X_train[feature] - min_max[feature]['min']) /
(min_max[feature]['max'] - min_max[feature]['min']))
X_val[feature] = ((X_val[feature] - min_max[feature]['min']) /
(min_max[feature]['max'] - min_max[feature]['min']))
print("Feature %s: train(%f, %f), val(%f, %f)" % (feature, X_train[feature].min(),
X_train[feature].max(), X_val[feature].min(),
X_val[feature].max()))
# generador de datos
class DataGenerator(Sequence):
def __init__(self, X, attributes, window_size=10, batch_size=32,
epoch_len_reducer=100, add_extra_channel=False,
return_label=True, y_key='Y', unit_key='id'):
self.batch_size = batch_size
self.return_label = return_label
self.window_size = window_size
self.attributes = attributes
self.epoch_len_reducer = epoch_len_reducer
self._X = {}
self._Y = {}
self._ids = X[unit_key].unique()
self.add_extra_channel = add_extra_channel
for _id in self._ids:
self._X[_id] = X.loc[(X[unit_key]==_id), self.attributes].values
self._Y[_id] = X.loc[(X[unit_key]==_id), y_key].values
self.__len = int((X.groupby(unit_key).size() - self.window_size).sum() /
self.batch_size)
del X
def __len__(self):
return int(self.__len / self.epoch_len_reducer)
def __getitem__(self, index):
X = self._X
_X = []
_y = []
for _ in range(self.batch_size):
sid = random.choice(self._ids)
unit = self._X[sid]
nrows = unit.shape[0]
cut = random.randint(0, nrows - self.window_size)
s = unit[cut: cut + self.window_size].T
y =self._Y[sid][cut + self.window_size-1]
_X.append(s)
_y.append(y)
_X = np.array(_X)
if self.add_extra_channel:
_X = _X.reshape(_X.shape + (1,))
if self.return_label:
return _X, np.array(_y).reshape((self.batch_size, 1))
else:
return _X, _X
def on_epoch_end(self):
pass
# generators
train_gen = DataGenerator(X_train, attributes=FEATURES, window_size=WINDOW_SIZE, batch_size=256,
add_extra_channel=True, return_label=True, y_key='RUL', unit_key='Bearing')
val_gen = DataGenerator(X_val, attributes=FEATURES, window_size=WINDOW_SIZE, batch_size=256,
add_extra_channel=True, return_label=True, y_key='RUL',
unit_key='Bearing', epoch_len_reducer=1000)
#Autoencoder
class AutoEncoder(Model):
def __init__(self):
super(AutoEncoder, self).__init__()
self.encoder = tf.keras.Sequential([
tf.keras.layers.Dense(64, activation='relu'),
tf.keras.layers.Dense(32, activation='relu'),
tf.keras.layers.Dense(16, activation='relu'),
tf.keras.layers.Dense(8, activation='relu')])
self.decoder = tf.keras.Sequential([
tf.keras.layers.Dense(16, activation='relu'),
tf.keras.layers.Dense(32, activation='relu'),
tf.keras.layers.Dense(64, activation='relu'),
tf.keras.layers.Dense(2, activation='sigmoid')
])
def call(self, x):
encoded = self.encoder(x)
decoded = self.decoder(encoded)
return decoded
definition of the autoencoder
model = AutoEncoder()
early_stopping = tf.keras.callbacks.EarlyStopping(monitor = 'val_loss', patience = 2, mode='min')
model.compile(optimizer = 'adam', loss='mae')
history = model.fit_generator(train_gen,
validation_data = val_gen,
epochs = 10)
After this is where I get the error,I have tried using different parameters but the result is always the same:
Epoch 1/10
/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py:3: UserWarning: `Model.fit_generator` is deprecated and will be removed in a future version. Please use `Model.fit`, which supports generators.
This is separate from the ipykernel package so we can avoid doing imports until
---------------------------------------------------------------------------
InvalidArgumentError Traceback (most recent call last)
<ipython-input-12-ee4502a47627> in <module>()
1 history = model.fit_generator(train_gen,
2 validation_data = val_gen,
----> 3 epochs = 10)
2 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/eager/execute.py in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name)
53 ctx.ensure_initialized()
54 tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
---> 55 inputs, attrs, num_outputs)
56 except core._NotOkStatusException as e:
57 if name is not None:
InvalidArgumentError: Graph execution error:
Detected at node 'mean_absolute_error/sub' defined at (most recent call last):
File "/usr/lib/python3.7/runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "/usr/lib/python3.7/runpy.py", line 85, in _run_code
exec(code, run_globals)
File "/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py", line 16, in <module>
app.launch_new_instance()
File "/usr/local/lib/python3.7/dist-packages/traitlets/config/application.py", line 846, in launch_instance
app.start()
File "/usr/local/lib/python3.7/dist-packages/ipykernel/kernelapp.py", line 499, in start
self.io_loop.start()
File "/usr/local/lib/python3.7/dist-packages/tornado/platform/asyncio.py", line 132, in start
self.asyncio_loop.run_forever()
File "/usr/lib/python3.7/asyncio/base_events.py", line 541, in run_forever
self._run_once()
File "/usr/lib/python3.7/asyncio/base_events.py", line 1786, in _run_once
handle._run()
File "/usr/lib/python3.7/asyncio/events.py", line 88, in _run
self._context.run(self._callback, *self._args)
File "/usr/local/lib/python3.7/dist-packages/tornado/platform/asyncio.py", line 122, in _handle_events
handler_func(fileobj, events)
File "/usr/local/lib/python3.7/dist-packages/tornado/stack_context.py", line 300, in null_wrapper
return fn(*args, **kwargs)
File "/usr/local/lib/python3.7/dist-packages/zmq/eventloop/zmqstream.py", line 452, in _handle_events
self._handle_recv()
File "/usr/local/lib/python3.7/dist-packages/zmq/eventloop/zmqstream.py", line 481, in _handle_recv
self._run_callback(callback, msg)
File "/usr/local/lib/python3.7/dist-packages/zmq/eventloop/zmqstream.py", line 431, in _run_callback
callback(*args, **kwargs)
File "/usr/local/lib/python3.7/dist-packages/tornado/stack_context.py", line 300, in null_wrapper
return fn(*args, **kwargs)
File "/usr/local/lib/python3.7/dist-packages/ipykernel/kernelbase.py", line 283, in dispatcher
return self.dispatch_shell(stream, msg)
File "/usr/local/lib/python3.7/dist-packages/ipykernel/kernelbase.py", line 233, in dispatch_shell
handler(stream, idents, msg)
File "/usr/local/lib/python3.7/dist-packages/ipykernel/kernelbase.py", line 399, in execute_request
user_expressions, allow_stdin)
File "/usr/local/lib/python3.7/dist-packages/ipykernel/ipkernel.py", line 208, in do_execute
res = shell.run_cell(code, store_history=store_history, silent=silent)
File "/usr/local/lib/python3.7/dist-packages/ipykernel/zmqshell.py", line 537, in run_cell
return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)
File "/usr/local/lib/python3.7/dist-packages/IPython/core/interactiveshell.py", line 2718, in run_cell
interactivity=interactivity, compiler=compiler, result=result)
File "/usr/local/lib/python3.7/dist-packages/IPython/core/interactiveshell.py", line 2822, in run_ast_nodes
if self.run_code(code, result):
File "/usr/local/lib/python3.7/dist-packages/IPython/core/interactiveshell.py", line 2882, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-7-342600d44df8>", line 3, in <module>
epochs = 10)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 2223, in fit_generator
initial_epoch=initial_epoch)
File "/usr/local/lib/python3.7/dist-packages/keras/utils/traceback_utils.py", line 64, in error_handler
return fn(*args, **kwargs)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1384, in fit
tmp_logs = self.train_function(iterator)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1021, in train_function
return step_function(self, iterator)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1010, in step_function
outputs = model.distribute_strategy.run(run_step, args=(data,))
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1000, in run_step
outputs = model.train_step(data)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 860, in train_step
loss = self.compute_loss(x, y, y_pred, sample_weight)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 919, in compute_loss
y, y_pred, sample_weight, regularization_losses=self.losses)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/compile_utils.py", line 201, in __call__
loss_value = loss_obj(y_t, y_p, sample_weight=sw)
File "/usr/local/lib/python3.7/dist-packages/keras/losses.py", line 141, in __call__
losses = call_fn(y_true, y_pred)
File "/usr/local/lib/python3.7/dist-packages/keras/losses.py", line 245, in call
return ag_fn(y_true, y_pred, **self._fn_kwargs)
File "/usr/local/lib/python3.7/dist-packages/keras/losses.py", line 1457, in mean_absolute_error
return backend.mean(tf.abs(y_pred - y_true), axis=-1)
Node: 'mean_absolute_error/sub'
required broadcastable shapes
[[{{node mean_absolute_error/sub}}]] [Op:__inference_train_function_1827]
I am trying to set up a simple convolutional network in Tensorflow. I'll try to keep the amount of code at a minimum level. Here's the class:
class ConvNet(object):
def __init__(self, input, labels, dataset):
self.input = input
self.true_labels = labels
#'dataset' is an instance of a class that
#I am using to read the training images
self.data = dataset
self.build()
self._optimize = None
self.sess = tf.Session()
self.sess.run(tf.global_variables_initializer())
def build(self):
self.conv_layers = []
# create 3 conv layers using tf.nn.conv2d
# and append them to self.conv_layers
# create flattening layer using tf.nn.reshape
self.fc_layers = []
# create 2 fully connected layers using tf.matmul
# and append them to self.fc_layers
#property
def optimize(self):
"""Return the optimize operation."""
if not self._optimize:
cross_entropy = tf.nn.softmax_cross_entropy_with_logits(logits = self.fc_layers[-1],
labels = self.true_labels)
cost = tf.reduce_mean(cross_entropy)
self._optimize = tf.train.AdamOptimizer(learning_rate=1e-4).minimize(cost)
return self._optimize
def train(self, num_epochs=1, batch_size=16):
epochs_done = 0
while not epochs_done == num_epochs:
# get next batch from training data
x_batch, y_true_batch, _, _ = self.data.train.next_batch(batch_size)
feed_dict_train = {self.input: x_batch, self.true_labels: y_true_batch}
#even if I initialize variables here, same error occurs
#self.sess.run(tf.global_variables_initializer())
self.sess.run(self.optimize, feed_dict=feed_dict_train)
if self.data.train.epochs_done > epochs_done:
#print stuff...
epochs_done += 1
self.sess.close()
I was trying to run the optimize operation to train the network on a simple dataset (e.g. images of cats vs dogs). Here is the main:
if __name__ == "__main__":
classes = ['dogs', 'cats']
num_classes = len(classes)
batch_size = 16
img_size = 128
num_channels = 3
#read training data and resize images to 128 x 128 pixels
data = dataset.read_train_sets(img_size, classes)
x = tf.placeholder(tf.float32, shape = [None, img_size, img_size, num_channels], name='x')
y_true = tf.placeholder(tf.float32, shape = [None, num_classes], name = 'y_true')
net = ConvNet(x, y_true, data)
net.train()
However I keep getting the following error (it's actually much longer than this, I can post it if necessary):
Caused by op 'beta1_power/read', defined at:
File "<string>", line 1, in <module>
File "/usr/lib/python3.5/idlelib/run.py", line 124, in main
ret = method(*args, **kwargs)
File "/usr/lib/python3.5/idlelib/run.py", line 351, in runcode
exec(code, self.locals)
File "/home/gian/face_recognition/model.py", line 366, in <module>
net.train()
File "/home/gian/face_recognition/model.py", line 309, in train
self.sess.run(self.optimize, feed_dict=feed_dict_train)
File "/home/gian/face_recognition/model.py", line 269, in optimize
self._optimize = tf.train.AdamOptimizer(learning_rate=1e-4).minimize(cost)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/training/optimizer.py", line 409, in minimize
name=name)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/training/optimizer.py", line 552, in apply_gradients
self._create_slots([_get_variable_for(v) for v in var_list])
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/training/adam.py", line 124, in _create_slots
colocate_with=first_var)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/training/optimizer.py", line 665, in _create_non_slot_variable
v = variable_scope.variable(initial_value, name=name, trainable=False)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/ops/variable_scope.py", line 2157, in variable
use_resource=use_resource)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/ops/variable_scope.py", line 2147, in <lambda>
previous_getter = lambda **kwargs: default_variable_creator(None, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/ops/variable_scope.py", line 2130, in default_variable_creator
constraint=constraint)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/ops/variables.py", line 235, in __init__
constraint=constraint)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/ops/variables.py", line 391, in _init_from_args
self._snapshot = array_ops.identity(self._variable, name="read")
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/ops/array_ops.py", line 142, in identity
return gen_array_ops.identity(input, name=name)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/ops/gen_array_ops.py", line 3053, in identity
"Identity", input=input, name=name)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/op_def_library.py", line 787, in _apply_op_helper
op_def=op_def)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/ops.py", line 3290, in create_op
op_def=op_def)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/ops.py", line 1654, in __init__
self._traceback = self._graph._extract_stack() # pylint: disable=protected-access
FailedPreconditionError (see above for traceback): Attempting to use uninitialized value beta1_power
[[Node: beta1_power/read = Identity[T=DT_FLOAT, _class=["loc:#Variable"], _device="/job:localhost/replica:0/task:0/device:GPU:0"](beta1_power)]]
Can somebody help me figure out where the problem is? Thanks a lot
This error generally occurs when you haven't initialized the optimizer.
So just add self.optimize before you initialize all the global variables.
Your code should look like this.
def __init__(self, input, labels, dataset):
self.input = input
self.true_labels = labels
#'dataset' is an instance of a class that
#I am using to read the training images
self.data = dataset
self.build()
self._optimize = None
self.sess = tf.Session()
self.optimize()
self.sess.run(tf.global_variables_initializer())
This is the part of the code
def train(x):
prediction = cnn(x)
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits=prediction, labels=y))
optimizer = tf.train.AdadeltaOptimizer().minimize(cost)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for epoch in xrange(num_epochs):
epoch_loss = 0
for _ in xrange(batch_size):
_, c = sess.run([optimizer, cost])
epoch_loss += c
print('Epoch {} completed out of {} - loss {}'.format(epoch + 1, num_epochs, epoch_loss))
n_classes = 17
batch_size = 32
dropout_rate = 0.4
num_epochs = 10
train_set = read_image_dataset_tfrecordfile('train.tfrecord', resize=True)
train_set = train_set.batch(batch_size)
train_set.repeat(num_epochs)
train_iterator = train_set.make_one_shot_iterator()
x, y = train_iterator.get_next()
train(x)
When I run this it does only the first epoch and then throws OutOfRangeError, here the stack
Epoch 1 completed out of 10 - loss 5.82853866496e+11
Traceback (most recent call last):
File "/Users/user/PycharmProjects/ProveTF/main.py", line 113, in <module>
train(x)
File "/Users/user/PycharmProjects/ProveTF/main.py", line 83, in train
_, c = sess.run([optimizer, cost])
File "/Users/user/venv/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 905, in run
run_metadata_ptr)
File "/Users/user/venv/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 1137, in _run
feed_dict_tensor, options, run_metadata)
File "/Users/user/venv/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 1355, in _do_run
options, run_metadata)
File "/Users/user/venv/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 1374, in _do_call
raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.OutOfRangeError: End of sequence
[[Node: IteratorGetNext = IteratorGetNext[output_shapes=[[?,100,100,1], [?,17]], output_types=[DT_FLOAT, DT_FLOAT], _device="/job:localhost/replica:0/task:0/device:CPU:0"](OneShotIterator)]]
Caused by op u'IteratorGetNext', defined at:
File "/Users/user/PycharmProjects/ProveTF/main.py", line 110, in <module>
x, y = train_iterator.get_next()
File "/Users/user/venv/lib/python2.7/site-packages/tensorflow/python/data/ops/iterator_ops.py", line 330, in get_next
name=name)), self._output_types,
File "/Users/user/venv/lib/python2.7/site-packages/tensorflow/python/ops/gen_dataset_ops.py", line 866, in iterator_get_next
output_shapes=output_shapes, name=name)
File "/Users/user/venv/lib/python2.7/site-packages/tensorflow/python/framework/op_def_library.py", line 787, in _apply_op_helper
op_def=op_def)
File "/Users/user/venv/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 3271, in create_op
op_def=op_def)
File "/Users/user/venv/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1650, in __init__
self._traceback = self._graph._extract_stack() # pylint: disable=protected-access
OutOfRangeError (see above for traceback): End of sequence
[[Node: IteratorGetNext = IteratorGetNext[output_shapes=[[?,100,100,1], [?,17]], output_types=[DT_FLOAT, DT_FLOAT], _device="/job:localhost/replica:0/task:0/device:CPU:0"](OneShotIterator)]]
I tried to move the repeat() method in other places and I tried to write simply repeat() without the parameter, but it doesn't work anyway.
Any solutions or suggestions?
You need to assign train_set = train_set.repeat() just as you do with the batch method. It doesn't modify the dataset in place.
I have issues using an exported tensorflow model. It doesn't allow me to evaluate the dataset I provided it with. If I run the evaluation in the same session as the training, there are no issues, however, that defeats the purpose of saving the model, if I have to retrain my model just to test with another dataset. The python file for generating the model is as such:
x = tf.placeholder(tf.float32, shape=[None, 1024], name = "x")
y_ = tf.placeholder(tf.float32, shape=[None, 10], name = "y_")
#===Model===
#Train
cross_entropy = tf.reduce_mean(
tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y_conv))
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
correct_prediction = tf.equal(tf.argmax(y_conv,1), tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32), name= "accuracy")
#Create Saver
saver = tf.train.Saver()
sess.run(tf.global_variables_initializer())
for i in range(40000):
batch = shvn_data.nextbatch(100)
if i%100 == 0:
train_accuracy = accuracy.eval(feed_dict={x:batch[0], y_: batch[1], keep_prob: 1.0})
print("step %d, training accuracy %f"%(i, train_accuracy))
train_step.run(feed_dict={x: batch[0], y_: batch[1], keep_prob: 0.5})
#Save
saver.save(sess,'svhn_model1')
I saved input variables x and y_, to be fed through function 'accuracy', so that I can run accuracy.eval() to obtain the accuracy of prediction. I evaluated the dataset in batches of 100 images, then summed the final prediction. The python file to evaluate the model in another session is as such:
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
sess = tf.Session(config = config)
shvn_data = DataLoader()
saver = tf.train.import_meta_graph('svhn_model1.meta')
saver.restore(sess,tf.train.latest_checkpoint('./'))
#sess.run(tf.global_variables_initializer())
#Variables to use with model
graph = tf.get_default_graph()
x = graph.get_tensor_by_name("x:0")
y_ = graph.get_tensor_by_name("y_:0")
accuracy = graph.get_tensor_by_name("accuracy:0")
keep_prob = tf.placeholder(tf.float32)
img_whole = np.reshape(shvn_data.test_images,(-1,1024))
batch_whole = np.asarray(shvn_data.test_label.eval(), dtype = np.float32)
total_accuracy = 0
test_count = shvn_data.TEST_COUNT
batch_size = 100
steps = int(math.ceil(test_count/float(batch_size)))
for j in range(steps):
start = j*batch_size
if (j+1)*batch_size > shvn_data.TEST_COUNT:
end = test_count
else:
end = (j+1)*batch_size
img_batch = img_whole[start:end]
label_batch = batch_whole[start:end]
batch_accuracy = accuracy.eval(session = sess, feed_dict={ x: img_batch, y_: label_batch, keep_prob: 1.0}) #ISSUE LIES HERE
print("Test batch %d:%d accuracy %g"%(start,end,batch_accuracy))
total_accuracy += batch_accuracy
print ("Total Accuracy: %f" %(total_accuracy/steps))
The error is as follows.
File "/home/lwenyao/anaconda2/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 1052, in _do_call
raise type(e)(node_def, op, message)
InvalidArgumentError: You must feed a value for placeholder tensor 'Placeholder' with dtype float
[[Node: Placeholder = Placeholder[dtype=DT_FLOAT, shape=[], _device="/job:localhost/replica:0/task:0/gpu:0"]()]]
Caused by op u'Placeholder', defined at:
File "/home/lwenyao/anaconda2/lib/python2.7/site-packages/spyder/utils/ipython/start_kernel.py", line 227, in <module>
main()
File "/home/lwenyao/anaconda2/lib/python2.7/site-packages/spyder/utils/ipython/start_kernel.py", line 223, in main
kernel.start()
File "/home/lwenyao/anaconda2/lib/python2.7/site-packages/ipykernel/kernelapp.py", line 474, in start
ioloop.IOLoop.instance().start()
File "/home/lwenyao/anaconda2/lib/python2.7/site-packages/zmq/eventloop/ioloop.py", line 177, in start
super(ZMQIOLoop, self).start()
File "/home/lwenyao/anaconda2/lib/python2.7/site-packages/tornado/ioloop.py", line 887, in start
handler_func(fd_obj, events)
File "/home/lwenyao/anaconda2/lib/python2.7/site-packages/tornado/stack_context.py", line 275, in null_wrapper
return fn(*args, **kwargs)
File "/home/lwenyao/anaconda2/lib/python2.7/site-packages/zmq/eventloop/zmqstream.py", line 440, in _handle_events
self._handle_recv()
File "/home/lwenyao/anaconda2/lib/python2.7/site-packages/zmq/eventloop/zmqstream.py", line 472, in _handle_recv
self._run_callback(callback, msg)
File "/home/lwenyao/anaconda2/lib/python2.7/site-packages/zmq/eventloop/zmqstream.py", line 414, in _run_callback
callback(*args, **kwargs)
File "/home/lwenyao/anaconda2/lib/python2.7/site-packages/tornado/stack_context.py", line 275, in null_wrapper
return fn(*args, **kwargs)
File "/home/lwenyao/anaconda2/lib/python2.7/site-packages/ipykernel/kernelbase.py", line 276, in dispatcher
return self.dispatch_shell(stream, msg)
File "/home/lwenyao/anaconda2/lib/python2.7/site-packages/ipykernel/kernelbase.py", line 228, in dispatch_shell
handler(stream, idents, msg)
File "/home/lwenyao/anaconda2/lib/python2.7/site-packages/ipykernel/kernelbase.py", line 390, in execute_request
user_expressions, allow_stdin)
File "/home/lwenyao/anaconda2/lib/python2.7/site-packages/ipykernel/ipkernel.py", line 196, in do_execute
res = shell.run_cell(code, store_history=store_history, silent=silent)
File "/home/lwenyao/anaconda2/lib/python2.7/site-packages/ipykernel/zmqshell.py", line 501, in run_cell
return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)
File "/home/lwenyao/anaconda2/lib/python2.7/site-packages/IPython/core/interactiveshell.py", line 2717, in run_cell
interactivity=interactivity, compiler=compiler, result=result)
File "/home/lwenyao/anaconda2/lib/python2.7/site-packages/IPython/core/interactiveshell.py", line 2827, in run_ast_nodes
if self.run_code(code, result):
File "/home/lwenyao/anaconda2/lib/python2.7/site-packages/IPython/core/interactiveshell.py", line 2881, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-1-33e4fce19d34>", line 1, in <module>
runfile('/home/lwenyao/Desktop/Python/Import_Model.py', wdir='/home/lwenyao/Desktop/Python')
File "/home/lwenyao/anaconda2/lib/python2.7/site-packages/spyder/utils/site/sitecustomize.py", line 866, in runfile
execfile(filename, namespace)
File "/home/lwenyao/anaconda2/lib/python2.7/site-packages/spyder/utils/site/sitecustomize.py", line 94, in execfile
builtins.execfile(filename, *where)
File "/home/lwenyao/Desktop/Python/Import_Model.py", line 63, in <module>
saver = tf.train.import_meta_graph('svhn_model1.meta')
File "/home/lwenyao/anaconda2/lib/python2.7/site-packages/tensorflow/python/training/saver.py", line 1595, in import_meta_graph
**kwargs)
File "/home/lwenyao/anaconda2/lib/python2.7/site-packages/tensorflow/python/framework/meta_graph.py", line 499, in import_scoped_meta_graph
producer_op_list=producer_op_list)
File "/home/lwenyao/anaconda2/lib/python2.7/site-packages/tensorflow/python/framework/importer.py", line 308, in import_graph_def
op_def=op_def)
File "/home/lwenyao/anaconda2/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 2336, in create_op
original_op=self._default_original_op, op_def=op_def)
File "/home/lwenyao/anaconda2/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1228, in __init__
self._traceback = _extract_stack()
InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'Placeholder' with dtype float
[[Node: Placeholder = Placeholder[dtype=DT_FLOAT, shape=[], _device="/job:localhost/replica:0/task:0/gpu:0"]()]]
As previously mentioned, evaluation has no issues if I run it in the same session when training the model. The only changes that I made were i added the argument session = sess each time I called .eval() when using the imported model. Sorry for the long post!
Alright, it appears that the error was caused from attempting to create and use another keep_prob variable in the test script, after importing the model. I.e. I created keep_prob = tf.placeholder(tf.float32,) in the training file. However,accuracy.eval() in the testing file was trying to look for keep_prob specifically from the model. I created another keep_prob = tf.placeholder(tf.float32,) in testing file, thinking it would be the same, but it was not.
I modified my code in the training file by adding the label:
keep_prob = tf.placeholder(tf.float32, name="keep_prob")
and in my testing file, called for the model's variable:
#Variables to use with model
graph = tf.get_default_graph()
x = graph.get_tensor_by_name("x:0")
y_ = graph.get_tensor_by_name("y_:0")
keep_prob = graph.get_tensor_by_name("keep_prob:0")#Changed this
accuracy = graph.get_tensor_by_name("accuracy:0")
And now it works fine. My code is modified from Deep MNIST for Experts from tensorflow.