# SimpleITK ExhaustiveOptimizerv4 Error

**URL:** https://discourse.itk.org/t/simpleitk-exhaustiveoptimizerv4-error/4516
**Category:** Beginner Questions
**Tags:** simpleitk
**Created:** [October 21, 2021, 9:31am UTC](https://discourse.itk.org/t/simpleitk-exhaustiveoptimizerv4-error/4516 "2021-10-21T09:31:53Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![yarzh](https://discourse.itk.org/letter_avatar_proxy/v4/letter/y/7ab992/32.png) [@yarzh](https://discourse.itk.org/u/yarzh)
#### Post date: [October 21, 2021, 9:31am UTC](https://discourse.itk.org/t/simpleitk-exhaustiveoptimizerv4-error/4516/1 "2021-10-21T09:31:53Z")

</div>

Hello, I am trying to use ExhaustiveOptimizer to find a good-ish initial displacement field transform between multimodal 2D images. But it looks like it is impossible to assign Optimizer Scales in any way. I’ve tried  
R.SetOptimizerScales(scales) with a suitably sized array, as well as R.SetOptimizerScalesFromJacobian() and other similar methods, always getting the same error.

Below is a snippet of code and the error message:

```auto

R = sitk.ImageRegistrationMethod()
R.SetMetricAsMattesMutualInformation(50)
R.SetMetricSamplingStrategy(R.REGULAR)
R.SetMetricSamplingPercentage(0.25)
R.MetricUseFixedImageGradientFilterOff()
displacementField = sitk.Image(fixed.GetSize(), sitk.sitkVectorFloat64)
displacementField.CopyInformation(fixed)
displacementTx = sitk.DisplacementFieldTransform(displacementField)
del displacementField
displacementTx.SetSmoothingGaussianOnUpdate(varianceForUpdateField=0.0,
                                             varianceForTotalField=1.5)
R.SetInitialTransform(displacementTx, inPlace=True)
numParams = displacementTx.GetNumberOfParameters()
print(numParams) # output: 46080
numberOfSteps = [10]*numParams
stepLength = 2
scales = [1]*numParams
R.SetOptimizerAsExhaustive(numberOfSteps=numberOfSteps, stepLength=stepLength)
print(len(scales)) # output: 46080
R.SetOptimizerScales(scales)
#R.SetOptimizerScalesFromJacobian() # gives the same error
R.SetInterpolator(sitk.sitkLinear)
displacementTx = R.Execute(fixed,moving)
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_20096/376383059.py in <module>
      8 R.SetOptimizerScalesFromPhysicalShift()
      9 R.SetInterpolator(sitk.sitkLinear)
---> 10 displacementTx = R.Execute(fixed,moving)

~\Miniconda3\envs\dev\lib\site-packages\SimpleITK\SimpleITK.py in Execute(self, fixed, moving)
   6693 def Execute(self, fixed, moving):
   6694 r"""Execute(ImageRegistrationMethod self, Image fixed, Image moving) -> Transform"""
-> 6695 return _SimpleITK.ImageRegistrationMethod_Execute(self, fixed, moving)
   6696 
   6697 def MetricEvaluate(self, fixed, moving):

RuntimeError: Exception thrown in SimpleITK ImageRegistrationMethod_Execute: d:\a\1\sitk-build\itk-prefix\include\itk-5.1\itkExhaustiveOptimizerv4.hxx:82:
itk::ERROR: itk::ERROR: ExhaustiveOptimizerv4(0000013A34F97400): The size of Scales is 2, but the NumberOfParameters is 46080.

```

---

<div class="post-metadata">

### Author: ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)
#### Post date: [October 26, 2021, 3:20pm UTC](https://discourse.itk.org/t/simpleitk-exhaustiveoptimizerv4-error/4516/2 "2021-10-26T15:20:11Z")

</div>

The error comes form here:

> <https://github.com/InsightSoftwareConsortium/ITK/blob/7777908fe897d7c8c4b90d9f3ca1ff33b8cff970/Modules/Numerics/Optimizersv4/include/itkExhaustiveOptimizerv4.hxx#L76-L81>

Have you tried making the scales with length of 2, e.g. `scales = [1, 1]`?
