Closed Arkadeep-sophoIITG closed 6 years ago
Could you please give a minimal example (*) that shows how you evaluate your model directly and how you evaluate it using foolbox and the predicted logits / probabilities in both cases.
I think I figured out the reason. What should be the input to the tensorflow model, the whole test dataset right ? I think I am unable to provide the 4-d placeholder to the model as input. Can you please help me with that ?
import cv2
import numpy as np
import tensorflow as tf
import tensorflow.contrib.slim as slim
from nets import nets_factory
import foolbox
from PIL import Image
import tensorflow.contrib.slim as slim
import os
graph = tf.Graph()
image = np.asarray(
Image.open('/local-scratch/ada77/cedar-rm/scratch/afresh/organised_test_nasnet/No_finding/00000820_021.jpeg'))
with graph.as_default():
data_tf = tf.convert_to_tensor(image, np.float32)
global_step = slim.get_or_create_global_step()
arg_scope = nets_factory.arg_scopes_map['nasnet_large']()
with slim.arg_scope(arg_scope):
logits, end_points = nets_factory.networks_map['nasnet_large'](
inputs=data_tf,
num_classes=2,
is_training=False
)
predictions = end_points['Predictions']
labels = 1
variables_to_restore = slim.get_variables_to_restore()
saver = tf.train.Saver(variables_to_restore)
metric_map = {
'Accuracy': tf.metrics.accuracy(labels=labels, predictions=tf.argmax(predictions, 1))
}
names_to_values, names_to_updates = slim.metrics.aggregate_metric_map(metric_map)
checkpoint_dir1 = '/local-scratch/ada77/07_JUne/final_model_scratch/final_model'
checkpoint_dir = '/local-scratch/ada77/07_JUne/Generalized_tfclassifer/tf_classification_nasnet/experiment/logdir'
checkpoint_path = tf.train.latest_checkpoint(checkpoint_dir)
num_batches = 1
slim.evaluation.evaluate_once(
master='',
checkpoint_path=checkpoint_path,
logdir='/',
num_evals=num_batches,
eval_op=tf.Print(list(names_to_updates.values()), [labels, predictions, tf.argmax(predictions, 1)],
message="original label predictions predicted label ", summarize=100),
variables_to_restore=variables_to_restore
)
with tf.Session() as sess:
saver.restore(sess, checkpoint_path)
model = foolbox.models.TensorFlowModel(data_tf, logits, (0, 255))
path = '/local-scratch/ada77/cedar-rm/scratch/afresh/organised_test_nasnet/chest_disease/'
dirs = os.listdir(path)
counter = 0
header = ['image', 'clean predicted label', 'clean predicted label confidence', 'other label conf',
'adversarial label', 'adversarial predicted label confidence', 'other adv label confidence ']
data = []
for item in dirs:
counter = counter + 1
image = cv2.imread(path + item)
print(item)
example_label = np.argmax(model.predictions(image))
print(example_label)
pred = foolbox.utils.softmax(model.predictions(image))[0]
print(pred , 1-pred)
# Label is always 0 no matter what the image is with high confidence score
Here's another example I reproduced rectifying the previous error . But now there is a problem with the logits and it is throwing the same error everytime. Here's the code
import numpy as np
import os
import cv2
import tensorflow as tf
import tensorflow.contrib.slim as slim
from nets import nets_factory
import foolbox
from PIL import Image
import tensorflow.contrib.slim as slim
path = '/local-scratch/ada77/cedar-rm/scratch/afresh/organised_test_nasnet/chest_disease/'
dirs = os.listdir(path)
images = []
counter = 0
for item in dirs:
counter = counter + 1
image = cv2.imread(path + item)
img = np.asarray(image, np.float32)
images.append(img)
graph = tf.Graph()
with graph.as_default():
data_tf = tf.convert_to_tensor(np.array(images))
global_step = slim.get_or_create_global_step()
arg_scope = nets_factory.arg_scopes_map['nasnet_large']()
with slim.arg_scope(arg_scope):
logits, end_points = nets_factory.networks_map['nasnet_large'](
inputs=data_tf,
num_classes=2,
is_training=False
)
predictions = end_points['Predictions']
variable_averages = tf.train.ExponentialMovingAverage(
0.9999, global_step)
variables_to_restore = variable_averages.variables_to_restore(
slim.get_model_variables())
saver = tf.train.Saver(variables_to_restore)
checkpoint_dir1 = '/local-scratch/ada77/07_JUne/final_model_scratch/final_model'
checkpoint_dir = '/local-scratch/ada77/07_JUne/Generalized_tfclassifer/tf_classification_nasnet/experiment/logdir'
checkpoint_path = tf.train.latest_checkpoint(checkpoint_dir)
with tf.Session() as sess:
saver.restore(sess, checkpoint_path)
model = foolbox.models.TensorFlowModel(data_tf, logits, (0, 255))
path = '/local-scratch/ada77/cedar-rm/scratch/afresh/organised_test_nasnet/chest_disease/'
dirs = os.listdir(path)
counter = 0
for item in dirs:
counter = counter + 1
image = cv2.imread(path + item)
print(item)
example_label = np.argmax(model.predictions(image))
print(example_label)
pred = foolbox.utils.softmax(model.predictions(image))[0]
print(pred, 1 - pred)
Here's the traceback: Traceback (most recent call last): File "fool_nasnet.py", line 45, in
model = foolbox.models.TensorFlowModel(data_tf, logits, (0, 255)) File "/home/ada77/.local/lib/python2.7/site-packages/foolbox/models/tensorflow.py", line 55, in init self._logits = tf.squeeze(logits, axis=0) File "/rcg/software/Linux/Ubuntu/16.04/amd64/TOOLS/TENSORFLOW/1.8.0-GPU-PY2713/lib/python2.7/site-packages/tensorflow/python/ops/array_ops.py", line 2630, in squeeze return gen_array_ops.squeeze(input, axis, name) File "/rcg/software/Linux/Ubuntu/16.04/amd64/TOOLS/TENSORFLOW/1.8.0-GPU-PY2713/lib/python2.7/site-packages/tensorflow/python/ops/gen_array_ops.py", line 7862, in squeeze "Squeeze", input=input, squeeze_dims=axis, name=name) File "/rcg/software/Linux/Ubuntu/16.04/amd64/TOOLS/TENSORFLOW/1.8.0-GPU-PY2713/lib/python2.7/site-packages/tensorflow/python/framework/op_def_library.py", line 787, in _apply_op_helper op_def=op_def) File "/rcg/software/Linux/Ubuntu/16.04/amd64/TOOLS/TENSORFLOW/1.8.0-GPU-PY2713/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 3392, in create_op op_def=op_def) File "/rcg/software/Linux/Ubuntu/16.04/amd64/TOOLS/TENSORFLOW/1.8.0-GPU-PY2713/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1734, in init control_input_ops) File "/rcg/software/Linux/Ubuntu/16.04/amd64/TOOLS/TENSORFLOW/1.8.0-GPU-PY2713/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1570, in _create_c_op raise ValueError(str(e)) ValueError: Can not squeeze dim[0], expected a dimension of 1, got 93 for 'Squeeze' (op: 'Squeeze') with input shapes: [93,2].
It's very difficult to help you with this issue since we can't run your example (e.g. you are referring to checkpoints on your local harddrive). Please provide a self-contained example without references to files that we cannot access. Please reopen this issue if it's still relevant.
Foolbox tensorflow model predictions donot match tensorflow slim model predictions on clean images . Is there some issue while restoring the model ?