我在pytorch中有一个3D CNN网络,我曾尝试将其转换为keras,但我对转换不太确定。另外,当我运行keras代码时,出现此错误:
ValueError:输入尺寸为[?,10,2,2,512],[3,3,3,512,512]的'conv3d_13 / convolution'(op:'Conv3D)的2减去3导致的负尺寸大小
火炬代码:
class semi_C3D(nn.Module):
def __init__(self, num_classes = 101):
super(semi_C3D, self).__init__()
self.features = nn.Sequential(
nn.Conv3d(3, 64, kernel_size=(1,3,3), padding=(0,1,1)),
nn.ReLU(inplace=True),
nn.Conv3d(64, 64, kernel_size=(1,3,3), padding=(0,1,1)),
nn.ReLU(inplace=True),
nn.MaxPool3d(kernel_size=(1,2,2), stride=(1,2,2)),
nn.Conv3d(64, 128, kernel_size=(1,3,3), padding=(0,1,1)),
nn.ReLU(inplace=True),
nn.Conv3d(128, 128, kernel_size=(1,3,3), padding=(0,1,1)),
nn.ReLU(inplace=True),
nn.MaxPool3d(kernel_size=(1,2,2), stride=(1,2,2)),
nn.Conv3d(128, 256, kernel_size=(1,3,3), padding=(0,1,1)),
nn.ReLU(inplace=True),
nn.Conv3d(256, 256, kernel_size=(1,3,3), padding=(0,1,1)),
nn.ReLU(inplace=True),
nn.Conv3d(256, 256, kernel_size=(1,3,3), padding=(0,1,1)),
nn.ReLU(inplace=True),
nn.MaxPool3d(kernel_size=(1,2,2), stride=(1,2,2)),
nn.Conv3d(256, 512, kernel_size=(1,3,3), padding=(0,1,1)),
nn.ReLU(inplace=True),
nn.Conv3d(512, 512, kernel_size=(1,3,3), padding=(0,1,1)),
nn.ReLU(inplace=True),
nn.MaxPool3d(kernel_size=(1,2,2), stride=(1,2,2)),
)
self.conv3d = nn.Sequential(
nn.Conv3d(512, 512, kernel_size=(3,1,1), padding=(1,0,0)),
nn.ReLU(inplace=True),
nn.MaxPool3d(kernel_size=(3,1,1), stride=(2,1,1), padding=(1,0,0)),
nn.Conv3d(512, 512, kernel_size=(3,3,3), padding=(1,1,1)),
nn.ReLU(),
nn.Conv3d(512, 512, kernel_size=(3,3,3), padding=(1,1,1)),
nn.ReLU(),
nn.MaxPool3d(kernel_size=(2,2,2), stride=(2,2,2)),
nn.Conv3d(512, 512, kernel_size=(3,3,3), padding=(1,1,1)),
nn.ReLU(),
nn.MaxPool3d(kernel_size=(2, 2, 2), stride=(2, 2, 2)),
)
self.st_classifier = nn.Sequential(
nn.Linear(9216, 4096),
nn.ReLU(),
nn.Dropout(p=0.5),
nn.Linear(4096, 4096),
nn.ReLU(),
nn.Dropout(p=0.5),
nn.Linear(4096, num_classes),
)
def forward(self, input):
x = self.features(input)
x = self.conv3d(x)
x = x.view(-1, 9216)
x = self.st_classifier(x)
return x
Keras代码:
def semi_3d(self):
model = Sequential()
#First bloc of layers (self.features = nn.Sequential part for the pytorch code)
model.add(Conv3D(64,kernel_size=(1, 3, 3),activation='relu',
input_shape=self.input_shape))
model.add(ZeroPadding3D(padding=(0, 1, 1)))
model.add(Conv3D(64,kernel_size=(1, 3, 3),activation='relu',))
model.add(ZeroPadding3D(padding=(0, 1, 1)))
model.add(MaxPooling3D(pool_size=(1, 2, 2), strides=(1,2,2)))
model.add(Conv3D(128,kernel_size=(1, 3, 3),activation='relu'))
model.add(ZeroPadding3D(padding=(0, 1, 1)))
model.add(Conv3D(128,kernel_size=(1, 3, 3),activation='relu'))
model.add(ZeroPadding3D(padding=(0, 1, 1)))
model.add(MaxPooling3D(pool_size=(1, 2, 2), strides=(1,2,2)))
model.add(Conv3D(256,kernel_size=(1, 3, 3),activation='relu'))
model.add(ZeroPadding3D(padding=(0, 1, 1)))
model.add(Conv3D(256,kernel_size=(1, 3, 3),activation='relu'))
model.add(ZeroPadding3D(padding=(0, 1, 1)))
model.add(Conv3D(256,kernel_size=(1, 3, 3),activation='relu'))
model.add(ZeroPadding3D(padding=(0, 1, 1)))
model.add(MaxPooling3D(pool_size=(1, 2, 2), strides=(1,2,2)))
model.add(Conv3D(512,kernel_size=(1, 3, 3),activation='relu'))
model.add(ZeroPadding3D(padding=(0, 1, 1)))
model.add(Conv3D(512,kernel_size=(1, 3, 3),activation='relu'))
model.add(ZeroPadding3D(padding=(0, 1, 1)))
model.add(MaxPooling3D(pool_size=(1, 2, 2), strides=(1,2,2)))
#Second bloc of layers (self.conv3d = nn.Sequential part for the pytorch code)
model.add(Conv3D(512,kernel_size=(3, 1, 1),activation='relu'))
model.add(ZeroPadding3D(padding=(1, 0, 0)))
model.add(MaxPooling3D(pool_size=(3, 1, 1), strides=(2, 1, 1)))
model.add(ZeroPadding3D(padding=(1, 0, 0)))
model.add(Conv3D(512,kernel_size=(3, 3, 3),activation='relu'))
model.add(ZeroPadding3D(padding=(1, 1, 1)))
model.add(Conv3D(512,kernel_size=(3, 3, 3),activation='relu'))
model.add(ZeroPadding3D(padding=(1, 1, 1)))
model.add(MaxPooling3D(pool_size=(2, 2, 2), strides=(2, 2, 2)))
model.add(Conv3D(512,kernel_size=(3, 3, 3),activation='relu'))
model.add(ZeroPadding3D(padding=(1, 1, 1)))
model.add(MaxPooling3D(pool_size=(2, 2, 2), strides=(2, 2, 2)))
#FC Layers
model.add(Dense(4096, activation='softmax', name='fc1'))
model.add(Dropout(0.5))
model.add(Dense(4096, activation='softmax', name='fc2'))
model.add(Dropout(0.5))
model.add(Dense(self.nb_classes, activation='softmax', name='fc3'))
return model
答案 0 :(得分:0)
您可以按照文档的“ onNX是一种用于表示机器学习模型的开放格式来使用onnx进行尝试。ONNX定义了一组通用运算符-机器学习和深度学习模型的构建块-以及一种通用文件格式使AI开发人员能够使用具有各种框架,工具,运行时和编译器的模型。”
源代码在这里Open standard for machine learning interoperability 您将像下面这样;
import onnx
from keras.models import load_model
pytorch_model = '/path/to/pytorch/model'
keras_output = '/path/to/converted/keras/model.hdf5'
onnx.convert(pytorch_model, keras_output)
model = load_model(keras_output)
preds = model.predict(x)