I am trying to solve a image classification problem using tensorflow.contrib.slim implementation of Alexnet. If i try to create the graph without the following line of code, graph is successfully created.
valid_predicitions = tf.nn.softmax(model(tf_validation_dataset))
But when i add this line to the code i get the following error
ValueError: Variable alexnet_v2/conv1/weights already exists, disallowed. Did you mean to set reuse=True or reuse=tf.AUTO_REUSE in VarScope?
I want loss and accuracy for test and validation data after certain number of iterations too. My complete code is as follows
with graph.as_default():
tf_train_dataset = tf.placeholder(tf.float32, shape=(batchsize, height, width,channels))
tf_train_labels = tf.placeholder(tf.float32, shape=(batchsize, num_labels))
tf_validation_dataset = tf.constant(valid_dataset)
tf_test_dataset = tf.constant(test_dataset)
def model(data):
with slim.arg_scope(alexnet.alexnet_v2_arg_scope()):
outputs, end_points = alexnet.alexnet_v2(data,num_classes=num_labels)
return outputs
logits = model(tf_train_dataset)
#calculate loss
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=tf_train_labels, logits=logits))
#optimization Step
optimizer = tf.train.AdamOptimizer(1e-4).minimize(loss)
#predictions for each dataset
train_predicitions = tf.nn.softmax(logits)
valid_predicitions = tf.nn.softmax(model(tf_validation_dataset))
model()is a function that creates the alexnet each time it's called. The second time you call the function, the error message tells you that one of the variable in the alexnet was already created, which is true.You'll have to refactor your code such that you only create the model once. I guess it means that the input to the model should be a placeholder where the batch size is not specified (
None), so that you can pass it the normal training batches, or the full validation data as one big batch.