Keras中維度報錯


在運行keras代碼的時候,出現了以下的錯誤:

 

Traceback (most recent call last):
  File "segnet_train.py", line 254, in <module>
    train(args)  
  File "segnet_train.py", line 210, in train
    model = SegNet()  
  File "segnet_train.py", line 134, in SegNet
    model.add(MaxPooling2D(pool_size=(2,2)))  
  File "/usr/local/lib/python2.7/dist-packages/keras/engine/sequential.py", line 181, in add
    output_tensor = layer(self.outputs[0])
  File "/usr/local/lib/python2.7/dist-packages/keras/engine/base_layer.py", line 457, in __call__
    output = self.call(inputs, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/keras/layers/pooling.py", line 205, in call
    data_format=self.data_format)
  File "/usr/local/lib/python2.7/dist-packages/keras/layers/pooling.py", line 268, in _pooling_function
    pool_mode='max')
  File "/usr/local/lib/python2.7/dist-packages/keras/backend/tensorflow_backend.py", line 3978, in pool2d
    data_format=tf_data_format)
  File "/home/ys/.local/lib/python2.7/site-packages/tensorflow/python/ops/nn_ops.py", line 2154, in max_pool
    name=name)
  File "/home/ys/.local/lib/python2.7/site-packages/tensorflow/python/ops/gen_nn_ops.py", line 4640, in max_pool
    data_format=data_format, name=name)
  File "/home/ys/.local/lib/python2.7/site-packages/tensorflow/python/framework/op_def_library.py", line 787, in _apply_op_helper
    op_def=op_def)
  File "/home/ys/.local/lib/python2.7/site-packages/tensorflow/python/util/deprecation.py", line 454, in new_func
    return func(*args, **kwargs)
  File "/home/ys/.local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 3155, in create_op
    op_def=op_def)
  File "/home/ys/.local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1731, in __init__
    control_input_ops)
  File "/home/ys/.local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1579, in _create_c_op
    raise ValueError(str(e))
ValueError: Negative dimension size caused by subtracting 2 from 1 for 'max_pooling2d_2/MaxPool' (op: 'MaxPool') with input shapes: [?,1,128,128].

 

Keras的圖片處理文檔中給出:
dim_ordering: One of {“th”, “tf”}. “tf” mode means that the images should have shape (samples, height, width, channels), “th” mode means that the images should have shape (samples, channels, height, width). It defaults to the image_dim_ordering value found in your Keras config file at ~/.keras/keras.json. If you never set it, then it will be “tf”. 

即:

關於圖片的維度順序有兩種類型,分別是“th”和”tf“,它們的差別如下:

圖片維序類型為 th 時(dim_ordering='th'): 輸入數據格式為[samples][channels][rows][cols];

圖片維序類型為 tf 時(dim_ordering='tf'):輸入數據格式為[samples][rows][cols][channels];

在Keras里默認的是“tf”順序,如果想要改為“th”順序,需要手動在前面加上如下代碼:

  from keras import backend as K
  K.set_image_dim_ordering('th')


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM