markus93 / NN_calibration

Calibration of Convolutional Neural Networks
MIT License
156 stars 41 forks source link

Error when running eval_cifar10.py #16

Open NainaSaid opened 1 year ago

NainaSaid commented 1 year ago

Hello. I have trained the network but when running the eval_cifar10.py file, i get the following error. Why this might be happening?

Traceback (most recent call last): File "/data22/nas/NN_calibration/scripts/resnet_cifar/eval_model_c10.py", line 130, in evaluate_model(model, weights_file_10, x_test, y_test, bins = 15, verbose = True, File "/data22/nas/NN_calibration/scripts/utility/evaluation.py", line 33, in evaluate_model o = last_layer(model.layers[-1].output) File "/data22/nas/myvm/lib/python3.10/site-packages/keras/utils/traceback_utils.py", line 70, in error_handler raise e.with_traceback(filtered_tb) from None File "/data22/nas/myvm/lib/python3.10/site-packages/tensorflow/python/framework/ops.py", line 1973, in _create_c_op raise ValueError(e.message) ValueError: Exception encountered when calling layer "dense" (type Dense).

Dimensions must be equal, but are 10 and 64 for '{{node dense/MatMul}} = MatMul[T=DT_FLOAT, transpose_a=false, transpose_b=false](Placeholder, dense/MatMul/ReadVariableOp)' with input shapes: [?,10], [64,10].

Call arguments received by layer "dense" (type Dense): • inputs=tf.Tensor(shape=(None, 10), dtype=float32)

markus93 commented 1 year ago

Hey!

This is quite old code. Thus, it uses an older version of Keras (likely Keras 2.3.1), and other packages could influence the behaviour.

The problem is that lines 30-34 in "utility/evaluate.py", where the last layers activation is changed to linear to get logits, do not work properly for newer Keras versions.

Let me know if you have more questions.

All the best, Markus

NainaSaid commented 1 year ago

Hey!

This is quite old code. Thus, it uses an older version of Keras (likely Keras 2.3.1), and other packages could influence the behaviour.

The problem is that lines 30-34 in "utility/evaluate.py", where the last layers activation is changed to linear to get logits, do not work properly for newer Keras versions.

Let me know if you have more questions.

All the best, Markus

Thanks Markus. So what could be the way forward? How do we do this in the newer version of keras?

markus93 commented 1 year ago

You could try model._layers.pop() instead of model.layers.pop(), maybe that does the trick. Or some other workaround from here https://stackoverflow.com/questions/57316557/tf-keras-layers-pop-doesnt-work-but-tf-keras-layers-pop-does.