本文介绍了3D卷积神经网络输入形状的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 Keras 和Python对3D形状进行分类时输入3D CNN时遇到问题.我有一个文件夹,其中包含一些JSON格式的模型.我将这些模型读入Numpy Array.模型为25 * 25 * 25,代表体素化模型的占用网格(每个位置代表位置(i,j,k)中的体素中是否有点),所以我只有1个输入通道,例如2D图像中的灰度图像.我的代码如下:

I'm having a problem feeding a 3D CNN using Keras and Python to classify 3D shapes. I have a folder with some models in JSON format. I read those models into a Numpy Array. The models are 25*25*25 and represent the occupancy grid of the voxelized model (each position represents if the voxel in position (i,j,k) has points in it or no), so I only have 1 channel of input, like grayscale images in 2D images. The code that I have is the following:

import numpy as np
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten
from keras.layers import Convolution3D, MaxPooling3D
from keras.optimizers import SGD
from keras.utils import np_utils
from keras import backend as K

# Number of Classes and Epochs of Training
nb_classes = 3 # cube, cone or sphere
nb_epoch = 100
batch_size = 2

# Input Image Dimensions
img_rows, img_cols, img_depth = 25, 25, 25

# Number of Convolutional Filters to use
nb_filters = 32

# Convolution Kernel Size
kernel_size = [5,5,5]

X_train, Y_train = [], []

# Read from File
import os
import json

i=0
for filename in os.listdir(os.path.join(os.getcwd(), 'models')):
    with open(os.path.join(os.getcwd(), 'models', filename)) as f:
        file = f.readlines()
        json_file = '\n'.join(file)
        content = json.loads(json_file)
        occupancy = content['model']['occupancy']
        form = []
        for value in occupancy:
            form.append(int(value))
        final_model = [ [ [ 0 for i in range(img_rows) ]
                              for j in range(img_cols) ]
                              for k in range(img_depth) ]
        a = 0
        for i in range(img_rows):
            for j in range(img_cols):
                for k in range(img_depth):
                    final_model[i][j][k] = form[a]
                    a = a + 1
        X_train.append(final_model)
        Y_train.append(content['model']['label'])

X_train = np.array(X_train)
Y_train = np.array(Y_train)

# (1 channel, 25 rows, 25 cols, 25 of depth)
input_shape = (1, img_rows, img_cols, img_depth)

# Init
model = Sequential()

# 3D Convolution layer
model.add(Convolution3D(nb_filters, kernel_size[0], kernel_size[1], kernel_size[2],
                        input_shape=input_shape,
                        activation='relu'))

# Fully Connected layer
model.add(Flatten())
model.add(Dense(128,
          init='normal',
          activation='relu'))
model.add(Dropout(0.5))

# Softmax Layer
model.add(Dense(nb_classes,
                init='normal'))
model.add(Activation('softmax'))

# Compile
model.compile(loss='categorical_crossentropy',
              optimizer=SGD())

# Fit network
model.fit(X_train, Y_train, nb_epoch=nb_epoch,
         verbose=1)

此后,出现以下错误

在处理上述异常期间,发生了另一个异常:

During handling of the above exception, another exception occurred:

回溯(最近一次通话最后一次):文件"CNN_3D.py",第76行,在 activation ='relu'))文件"/usr/local/lib/python3.6/site-packages/keras/models.py",第299行,在 添加 layer.create_input_layer(batch_input_shape,input_dtype)文件"/usr/local/lib/python3.6/site-packages/keras/engine/topology.py", 第401行,位于create_input_layer中 self(x)文件"/usr/local/lib/python3.6/site-packages/keras/engine/topology.py", 第572行,在致电中 self.add_inbound_node(inbound_layers,node_indices,tensor_indices)文件 "/usr/local/lib/python3.6/site-packages/keras/engine/topology.py", 第635行,位于add_inbound_node中 Node.create_node(self,inbound_layers,node_indices,tensor_indices)文件 "/usr/local/lib/python3.6/site-packages/keras/engine/topology.py", 第166行,位于create_node中 output_tensors = to_list(outbound_layer.call(input_tensors [0],mask = input_masks [0]))文件 "/usr/local/lib/python3.6/site-packages/keras/layers/convolutional.py", 通话中的第1234行 filter_shape = self.W_shape)文件"/usr/local/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py", conv3d中的2831行 x = tf.nn.conv3d(x,内核,步幅,填充)文件"/usr/local/lib/python3.6/site-packages/tensorflow/python/ops/gen_nn_ops.py", conv3d中的522行 步幅=步幅,填充=填充,名称=名称)文件"/usr/local/lib/python3.6/site-packages/tensorflow/python/framework/op_def_library.py", 第763行,位于apply_op中 op_def = op_def)文件"/usr/local/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", 第2397行,位于create_op中 set_shapes_for_outputs(ret)文件"/usr/local/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", 第1757行,在set_shapes_for_outputs中 Shapes = shape_func(op)文件"/usr/local/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", 第1707行,在call_with_requiring中 返回call_cpp_shape_fn(op,require_shape_fn = True)文件"/usr/local/lib/python3.6/site-packages/tensorflow/python/framework/common_shapes.py", 第610行,在call_cpp_shape_fn中 debug_python_shape_fn,require_shape_fn)文件"/usr/local/lib/python3.6/site-packages/tensorflow/python/framework/common_shapes.py", _call_cpp_shape_fn_impl中的第675行 引发ValueError(err.message)ValueError:负尺寸大小是由于输入的'Conv3D'(op:'Conv3D')从1中减去5引起的 形状:[?, 1,25,25,25],[5,5,5,25,32].

Traceback (most recent call last): File "CNN_3D.py", line 76, in activation='relu')) File "/usr/local/lib/python3.6/site-packages/keras/models.py", line 299, in add layer.create_input_layer(batch_input_shape, input_dtype) File "/usr/local/lib/python3.6/site-packages/keras/engine/topology.py", line 401, in create_input_layer self(x) File "/usr/local/lib/python3.6/site-packages/keras/engine/topology.py", line 572, in call self.add_inbound_node(inbound_layers, node_indices, tensor_indices) File "/usr/local/lib/python3.6/site-packages/keras/engine/topology.py", line 635, in add_inbound_node Node.create_node(self, inbound_layers, node_indices, tensor_indices) File "/usr/local/lib/python3.6/site-packages/keras/engine/topology.py", line 166, in create_node output_tensors = to_list(outbound_layer.call(input_tensors[0], mask=input_masks[0])) File "/usr/local/lib/python3.6/site-packages/keras/layers/convolutional.py", line 1234, in call filter_shape=self.W_shape) File "/usr/local/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py", line 2831, in conv3d x = tf.nn.conv3d(x, kernel, strides, padding) File "/usr/local/lib/python3.6/site-packages/tensorflow/python/ops/gen_nn_ops.py", line 522, in conv3d strides=strides, padding=padding, name=name) File "/usr/local/lib/python3.6/site-packages/tensorflow/python/framework/op_def_library.py", line 763, in apply_op op_def=op_def) File "/usr/local/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 2397, in create_op set_shapes_for_outputs(ret) File "/usr/local/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 1757, in set_shapes_for_outputs shapes = shape_func(op) File "/usr/local/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 1707, in call_with_requiring return call_cpp_shape_fn(op, require_shape_fn=True) File "/usr/local/lib/python3.6/site-packages/tensorflow/python/framework/common_shapes.py", line 610, in call_cpp_shape_fn debug_python_shape_fn, require_shape_fn) File "/usr/local/lib/python3.6/site-packages/tensorflow/python/framework/common_shapes.py", line 675, in _call_cpp_shape_fn_impl raise ValueError(err.message) ValueError: Negative dimension size caused by subtracting 5 from 1 for 'Conv3D' (op: 'Conv3D') with input shapes: [?,1,25,25,25], [5,5,5,25,32].

我要怎么做才能得到这个错误?

What am I doing wrong to get this error?

推荐答案

我认为问题是您在Theano排序中设置了输入形状,但在Tensorflow后端和Tensorflow img排序中使用了Keras.另外,y_train数组必须转换为分类标签.

I think that the problem is that you are setting the input shape in Theano ordering but you are using Keras with Tensorflow backend and Tensorflow img ordering. In addition the y_train array has to be converted to categorical labels.

更新的代码:

from keras.utils import np_utils
from keras import backend as K

if K.image_dim_ordering() == 'th':
    X_train = X_train.reshape(X_train.shape[0], 1, img_rows, img_cols, img_depth)
    input_shape = (1, img_rows, img_cols, img_depth)
else:
    X_train = X_train.reshape(X_train.shape[0], img_rows, img_cols, img_depth, 1)
    input_shape = (img_rows, img_cols, img_depth, 1)

Y_train = np_utils.to_categorical(Y_train, nb_classes)

添加此行应该可以解决此问题.

Adding this lines should fix it.

这篇关于3D卷积神经网络输入形状的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-12 02:43