问题
t1=tf.image.ssim_multiscale(tf.convert_to_tensor(x_test1[i]),tf.convert_to_tensor(ans1[i]),max_val=1).eval()
file1.write("\tMs-ssim:\t"+str(t1)+"\n")
avgs+=ssim1
avgm+=t1
print(t1)
print(i)
file1.write("MS-SSIM:\t"+str(avgm/100))
When ms-ssim is calculated, its showing the following error, however everything seems to be correct, there are 2 numpy arrays, among which we are doing comparision of MS-SSIM, ans1[i] and x_test1[i] are two arrays in numpy I have calcualted the psnr and ssim using the same inbuilt functions in tensorflow which takes same numpy arrays of inputs to be compared, but the same concept is giving the following error for MS-SSIM
The error is following
0.5862568
---------------------------------------------------------------------------
InvalidArgumentError Traceback (most recent call last)
~/anaconda3/lib/python3.6/site-packages/tensorflow/python/client / session.py in _do_call(self, fn, *args)
1333 try:
-> 1334 return fn(*args)
1335 except errors.OpError as e:
~/anaconda3/lib/python3.6/site-packages/tensorflow/python/client /session.py in _run_fn(feed_dict, fetch_list, target_list, options, run_metadata)
1318 return self._call_tf_sessionrun(
-> 1319 options, feed_dict, fetch_list, target_list, run_metadata)
1320
~/anaconda3/lib/python3.6/site-packages/tensorflow/python/client /session.py in _call_tf_sessionrun(self, options, feed_dict, fetch_list, target_list, run_metadata)
1406 self._session, options, feed_dict, fetch_list, target_list,
-> 1407 run_metadata)
1408
**InvalidArgumentError: Computed output size would be negative: -2 [input_size: 8, effective_filter_size: 11, stride: 1]
[[{{node MS-SSIM_2/Scale4/depthwise_1}}]]**
During handling of the above exception, another exception occurred:
InvalidArgumentError Traceback (most recent call last)
<ipython-input-150-850d3da2c004> in <module>()
12 t=tf.image.psnr(tf.convert_to_tensor(x_test1[i]),tf.convert_to_tensor(ans1[ i]),max_val=1).eval()
13 file1.write("\tPSNR:\t"+str(t))
---> 14 t1=tf.image.ssim_multiscale(tf.convert_to_tensor(x_test1[i]),tf.convert_to_ tensor(ans1[i]),max_val=2).eval()
15 file1.write("\tMs-ssim:\t"+str(t1)+"\n")
16 avgs+=ssim1
~/anaconda3/lib/python3.6/site-packages/tensorflow/python /framework/ops.py in eval(self, feed_dict, session)
693
694 """
--> 695 return _eval_using_default_session(self, feed_dict, self.graph, session)
696
697
~/anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/ops.py in _eval_using_default_session(tensors, feed_dict, graph, session)
5179 "the tensor's graph is different from the session's "
5180 "graph.")
-> 5181 return session.run(tensors, feed_dict)
5182
5183
~/anaconda3/lib/python3.6/site-packages/tensorflow/python/client/session.py in run(self, fetches, feed_dict, options, run_metadata)
927 try:
928 result = self._run(None, fetches, feed_dict, options_ptr,
--> 929 run_metadata_ptr)
930 if run_metadata:
931 proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)
~/anaconda3/lib/python3.6/site-packages/tensorflow/python/client/session.py in _run(self, handle, fetches, feed_dict, options, run_metadata)
1150 if final_fetches or final_targets or (handle and feed_dict_tensor):
1151 results = self._do_run(handle, final_targets, final_fetches,
-> 1152 feed_dict_tensor, options, run_metadata)
1153 else:
1154 results = []
~/anaconda3/lib/python3.6/site-packages/tensorflow/python/client/session.py in _do_run(self, handle, target_list, fetch_list, feed_dict, options, run_metadata)
1326 if handle is None:
1327 return self._do_call(_run_fn, feeds, fetches, targets, options,
-> 1328 run_metadata)
1329 else:
1330 return self._do_call(_prun_fn, handle, feeds, fetches)
~/anaconda3/lib/python3.6/site-packages/tensorflow/python/client/session.py in _do_call(self, fn, *args)
1346 pass
1347 message = error_interpolation.interpolate(message, self._graph)
-> 1348 raise type(e)(node_def, op, message)
1349
1350 def _extend_graph(self):
InvalidArgumentError: Computed output size would be negative: -2 [input_size: 8, effective_filter_size: 11, stride: 1]
[[node MS-SSIM_2/Scale4/depthwise_1 (defined at <ipython-input- 150-850d3da2c004>:14) ]]
Caused by op 'MS-SSIM_2/Scale4/depthwise_1', defined at:
File "/home/dipti/anaconda3/lib/python3.6/runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "/home/dipti/anaconda3/lib/python3.6/runpy.py", line 85, in _run_code
exec(code, run_globals)
File "/home/dipti/anaconda3/lib/python3.6/site-packages /ipykernel_launcher.py", line 16, in <module>
app.launch_new_instance()
File "/home/dipti/anaconda3/lib/python3.6/site-packages/traitlets/config/application.py", line 658, in launch_instance
app.start()
File "/home/dipti/anaconda3/lib/python3.6/site-packages /ipykernel/kernelapp.py", line 477, in start
ioloop.IOLoop.instance().start()
File "/home/dipti/anaconda3/lib/python3.6/site-packages /zmq/eventloop/ioloop.py", line 177, in start
super(ZMQIOLoop, self).start()
File "/home/dipti/anaconda3/lib/python3.6/site-packages /tornado/ioloop.py", line 888, in start
handler_func(fd_obj, events)
File "/home/dipti/anaconda3/lib/python3.6/site-packages/tornado /stack_context.py", line 277, in null_wrapper
return fn(*args, **kwargs)
File "/home/dipti/anaconda3/lib/python3.6/site-packages /zmq/eventloop/zmqstream.py", line 440, in _handle_events
self._handle_recv()
File "/home/dipti/anaconda3/lib/python3.6/site-packages/ zmq/eventloop/zmqstream.py", line 472, in _handle_recv
self._run_callback(callback, msg)
File "/home/dipti/anaconda3/lib/python3.6/site-packages/z mq/eventloop/zmqstream.py", line 414, in _run_callback
callback(*args, **kwargs)
File "/home/dipti/anaconda3/lib/python3.6/site-packages /tornado/stack_context.py", line 277, in null_wrapper
return fn(*args, **kwargs)
File "/home/dipti/anaconda3/lib/python3.6/site-packages/ ipykernel/kernelbase.py", line 283, in dispatcher
return self.dispatch_shell(stream, msg)
File "/home/dipti/anaconda3/lib/python3.6/site-packages /ipykernel/kernelbase.py", line 235, in dispatch_shell
handler(stream, idents, msg)
File "/home/dipti/anaconda3/lib/python3.6/site-packages/ipykernel/k ernelbase.py", line 399, in execute_request
user_expressions, allow_stdin)
File "/home/dipti/anaconda3/lib/python3.6/site-packages/ipykernel /ipkernel.py", line 196, in do_execute
res = shell.run_cell(code, store_history=store_history, silent=silent)
File "/home/dipti/anaconda3/lib/python3.6/site-packages/ipykernel /zmqshell.py", line 533, in run_cell
return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)
File "/home/dipti/anaconda3/lib/python3.6/site-packages/IPython /core/interactiveshell.py", line 2662, in run_cell
raw_cell, store_history, silent, shell_futures)
File "/home/dipti/anaconda3/lib/python3.6/site-packages/IPython /core/interactiveshell.py", line 2785, in _run_cell
interactivity=interactivity, compiler=compiler, result=result)
File "/home/dipti/anaconda3/lib/python3.6/site-packages/IPython/ core/interactiveshell.py", line 2901, in run_ast_nodes
if self.run_code(code, result):
File "/home/dipti/anaconda3/lib/python3.6/site-packages/ IPython/core/interactiveshell.py", line 2961, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-150-850d3da2c004>", line 14, in <module>
t1=tf.image.ssim_multiscale(tf.convert_to_tensor(x_test1[i]),tf.convert_to_ tensor(ans1[i]),max_val=2).eval()
File "/home/dipti/anaconda3/lib/python3.6/site-packages /tensorflow/python/ops/image_ops_impl.py", line 2867, in ssim_multiscale
ssim_per_channel, cs = _ssim_per_channel(*imgs, max_val=max_val)
File "/home/dipti/anaconda3/lib/python3.6/site-packages /tensorflow/python/ops/image_ops_impl.py", line 2700, in _ssim_per_channel
luminance, cs = _ssim_helper(img1, img2, reducer, max_val, compensation)
File "/home/dipti/anaconda3/lib/python3.6/site-packages /tensorflow/python/ops/image_ops_impl.py", line 2610, in _ssim_helper
mean1 = reducer(y)
File "/home/dipti/anaconda3/lib/python3.6/site-packages /tensorflow/python/ops/image_ops_impl.py", line 2696, in reducer
y = nn.depthwise_conv2d(x, kernel, strides=[1, 1, 1, 1], p adding='VALID')
File "/home/dipti/anaconda3/lib/python3.6/site- packages/tensorflow/python/ops/nn_impl.py", line 522, in depthwise_conv2d
op=op)
File "/home/dipti/anaconda3/lib/python3.6/site-packages/tensorflow/python/ops/nn_ops.py", line 435, in with_space_to_batch
return new_op(input, None)
File "/home/dipti/anaconda3/lib/python3.6/site-packages/tensorflow/python/ops/nn_ops.py", line 591, in __call__
return self.call(inp, filter)
File "/home/dipti/anaconda3/lib/python3.6/site-packages/tensorflow/python/ops/nn_ops.py", line 425, in <lambda>
return lambda inp, _: op(inp, num_spatial_dims, padding)
File "/home/dipti/anaconda3/lib/python3.6/site-packages /tensorflow/python/ops/nn_impl.py", line 514, in op
name=name)
File "/home/dipti/anaconda3/lib/python3.6/site-packages /tensorflow/python/ops/gen_nn_ops.py", line 2251, in depthwise_conv2d_native
dilations=dilations, name=name)
File "/home/dipti/anaconda3/lib/python3.6/site-packages /tensorflow/python/framework/op_def_library.py", line 788, in _apply_op_helper
op_def=op_def)
File "/home/dipti/anaconda3/lib/python3.6/site-packages /tensorflow/python/util/deprecation.py", line 507, in new_func
return func(*args, **kwargs)
File "/home/dipti/anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 3300, in create_op
op_def=op_def)
File "/home/dipti/anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 1801, in __init__
self._traceback = tf_stack.extract_stack()
**InvalidArgumentError (see above for traceback): Computed output size would be negative: -2 [input_size: 8, effective_filter_size: 11, stride: 1]
[[node MS-SSIM_2/Scale4/depthwise_1 (defined at <ipython-input-150-850d3da2c004>:14) ]]**
回答1:
The issue appears to be with assertion after spatial-dimension reduction. In _ssim_per_channel
, the H
and W
of images is asserted against filter_size
. Whereas in ssim_multiscale
, downsampling is performed len(power_factors)-1
times.
Here are two workarounds:
Make sure that
filter_size
is small enough to calculate ssim values for all the four spatial-scales(excluding first scale) after downsampling withinssim_multiscale
. Contrarily, ensure bothH
andW
of your image are big enough such thatH/(2**4) and W/(2**4) >= filter_size
.Since downsampling is performed
len(power_factors)-1
times, you can also use lesser number of_MSSSIM_WEIGHTS
or power_factors than default, which meansH/(2**(len(power_factors)-1)) and W/(2**(len(power_factors)-1)) >= filter_size
.
field1 = tf.random.uniform(shape=[8, 64, 64, 1], minval=0, maxval=1)
field2 = tf.random.uniform(shape=[8, 64, 64, 1], minval=0, maxval=1)
#Use smaller filter_size
ms_ssim_score = tf.image.ssim_multiscale(img1=field1, img2=field2, max_val=1.0,
filter_size=4)
#Or use lesser number of power_factors
ms_ssim_score = tf.image.ssim_multiscale(img1=field1, img2=field2, max_val=1.0,
power_factors=(0.0448, 0.2856, 0.3001),
filter_size=11)
You can use try obtaining a suitable value of filter_size
with:
def suggest_filter_size(image1_batch,image2_batch,power_factors,filter_size):
shape1= image1_batch.shape[1:-1]
shape2= image2_batch.shape[1:-1]
if not(shape1[-3:-1][0]/(2**(len(power_factors)-1)) and shape2[-3:-1][0]/(2**(len(power_factors)-1)) >= filter_size):
H = tf.math.reduce_min((shape1,shape2))
suggested_filter_size = int(H/(2**(len(power_factors)-1)))
else:
suggested_filter_size = filter_size
return suggested_filter_size
#Example: suggest_filter_size(field1, field2,power_factors = (0.0448, 0.2856, 0.3001, 0.2363, 0.1333), filter_size = 11)
Try this:
H,W = 128,128
field1 = tf.random.uniform(shape=[8, H, W, 1], minval=0, maxval=1)
field2 = tf.random.uniform(shape=[8, H, W, 1], minval=0, maxval=1)
filter_size = 11 #default from tf.image.ssim_multiscale
power_factors = (0.0448, 0.2856, 0.3001, 0.2363, 0.1333) #default from tf.image.ssim_multiscale
#get a new filter_size from default values
new_filter_size = suggest_filter_size(field1, field2,power_factors,filter_size)
#tf.image.ssim_multiscale would now work with new_filter_size
ms_ssim_score = tf.image.ssim_multiscale(img1=field1, img2=field1, max_val=1.0,filter_size=new_filter_size)
回答2:
Your input arrays are most likely too small for the MS-SSIM calculation using the default setting.
From the documentation:
power_factors: Iterable of weights for each of the scales. The number of scales used is the length of the list. Index 0 is the unscaled resolution's weight and each increasing scale corresponds to the image being downsampled by 2. Defaults to (0.0448, 0.2856, 0.3001, 0.2363, 0.1333), which are the values obtained in the original paper.
filter_size: Default value 11 (size of gaussian filter)
The default setting uses 5 2x downsampling operations a filter size of 11, so for a small image array, say 64x64, 3 2x downsampling reduce it to 8x8, which is smaller than the default kernel size 11.
Try this yourself:
N = 64 # change it to, say 512, then this works
a = np.random.randn(1, N, N, 1)
a = tf.convert_to_tensor(a)
max_val = np.max(np.reshape(a, [-1]))
result = tf.image.ssim_multiscale(a, a, max_val)
来源:https://stackoverflow.com/questions/57127626/error-in-calculation-of-inbuilt-ms-ssim-function-in-tensorflow