张量流中内置MS-SSIm函数的计算错误

时间:2019-07-20 18:42:43

标签: python tensorflow

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))

在计算ms-ssim时,它显示以下错误,但是似乎一切正确,有2个numpy数组,其中我们正在做MS-SSIM的比较, ans1 [i]和x_test1 [i]是numpy中的两个数组 我已经在tensorflow中使用相同的内置函数计算了psnr和ssim,该函数需要比较相同的numpy输入数组,但是相同的概念给MS-SSIM带来了以下错误

正在跟踪错误

      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) ]]**

2 个答案:

答案 0 :(得分:2)

问题似乎与减少空间维数后的断言有关。在_ssim_per_channel中,图像HW相对于filter_sizeasserted。而在ssim_multiscale中,downsampling 执行了len(power_factors)-1次。

有两种解决方法:

  1. 请确保filter_size足够小,以便在ssim_multiscale中进行下采样后为所有四个空间比例(不包括第一比例)计算ssim值。相反,请确保图像的HW足够大,以致H/(2**4) and W/(2**4) >= filter_size

  2. 由于下采样执行了len(power_factors)-1次,因此您也可以使用少于默认数量的_MSSSIM_WEIGHTS或power_factors,这意味着H/(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)

您可以尝试通过以下方式获得filter_size的合适值:

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)

尝试一下:

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)

答案 1 :(得分:1)

对于使用默认设置的MS-SSIM计算,您的输入数组很可能太小。

来自documentation

  

power_factors:每个秤的权重都是可迭代的。号码   所使用的比例是列表的长度。索引0是未缩放的   分辨率的权重和每个递增比例对应于图像   被2下采样。默认值为(0.0448,0.2856,0.3001,0.2363,   0.1333),这是原始论文中获得的值。

     

filter_size:默认值11(高斯滤波器的大小)

默认设置使用5个2x下采样操作,过滤器大小为11,因此对于较小的图像数组(例如64x64),通过3 2x下采样将其减小为8x8,小于默认内核大小11。

自己尝试一下:

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)