def static_graph_case(self): main = fluid.Program() start = fluid.Program() with fluid.unique_name.guard(): with fluid.program_guard(main, start): self.channel_last = self.data_format == "NDHWC" if self.channel_last: x = x = fluid.data( "input", (-1, -1, -1, -1, self.in_channels), dtype=self.dtype) else: x = fluid.data( "input", (-1, self.in_channels, -1, -1, -1), dtype=self.dtype) weight = fluid.data( "weight", self.weight_shape, dtype=self.dtype) if not self.no_bias: bias = fluid.data("bias", self.bias_shape, dtype=self.dtype) y = F.conv3d( x, weight, None if self.no_bias else bias, padding=self.padding, stride=self.stride, dilation=self.dilation, groups=self.groups, data_format=self.data_format) if self.act == 'sigmoid': y = F.sigmoid(y)
def functional(self, place): main = fluid.Program() start = fluid.Program() with fluid.unique_name.guard(): with fluid.program_guard(main, start): input_shape = (-1, -1, -1, -1, self.num_channels) \ if self.channel_last else (-1, self.num_channels, -1, -1, -1) x_var = fluid.data("input", input_shape, dtype=self.dtype) w_var = fluid.data( "weight", self.weight_shape, dtype=self.dtype) b_var = fluid.data( "bias", (self.num_filters, ), dtype=self.dtype) y_var = F.conv3d( x_var, w_var, None if self.no_bias else b_var, padding=self.padding, stride=self.stride, dilation=self.dilation, groups=self.groups, act=self.act, use_cudnn=self.use_cudnn, data_format=self.data_format) feed_dict = {"input": self.input, "weight": self.weight} if self.bias is not None: feed_dict["bias"] = self.bias exe = fluid.Executor(place) exe.run(start) y_np, = exe.run(main, feed=feed_dict, fetch_list=[y_var]) return y_np
def dygraph_case(self): with dg.guard(): x = dg.to_variable(self.input, dtype=paddle.float32) w = dg.to_variable(self.filter, dtype=paddle.float32) b = None if self.bias is None else dg.to_variable( self.bias, dtype=paddle.float32) y = F.conv3d(x, w, b, padding=self.padding, stride=self.stride, dilation=self.dilation, groups=self.groups, data_format=self.data_format)
def dygraph_case(self): with dg.guard(self.place): x = dg.to_variable(self.input) weight = dg.to_variable(self.weight) bias = None if self.no_bias else dg.to_variable(self.bias) y = F.conv3d(x, weight, bias, padding=self.padding, stride=self.stride, dilation=self.dilation, act=self.act, groups=self.groups, data_format=self.data_format, use_cudnn=self.use_cudnn) out = y.numpy() return out
def static_graph_case_2(self): main = fluid.Program() start = fluid.Program() with fluid.unique_name.guard(): with fluid.program_guard(main, start): if self.channel_last: x = x = fluid.data("input", (-1, -1, -1, -1, self.in_channels), dtype=self.dtype) else: x = fluid.data("input", (-1, self.in_channels, -1, -1, -1), dtype=self.dtype) weight = fluid.data("weight", self.weight.shape, dtype=self.dtype) if not self.no_bias: bias = fluid.data("bias", self.bias.shape, dtype=self.dtype) y = F.conv3d(x, weight, None if self.no_bias else bias, padding=self.padding, stride=self.stride, dilation=self.dilation, groups=self.groups, data_format=self.data_format) if self.act == 'sigmoid': y = F.sigmoid(y) exe = fluid.Executor(self.place) exe.run(start) feed_dict = {"input": self.input, "weight": self.weight} if not self.no_bias: feed_dict["bias"] = self.bias out, = exe.run(main, feed=feed_dict, fetch_list=[y]) return out
print('================= 逐元素乘、1x1x1的3D卷积 ==================') N = 2 in_C = 3 out_C = 16 D = 32 # 增加一个维:深度 H = 64 W = 64 C = out_C # w = paddle.randn((C, in_C, kH, kW)) x = paddle.randn((N, in_C, D, H, W)) w = paddle.randn((C, in_C, 1, 1, 1)) y = F.conv3d(x, w) # [N, C, D, H, W] x_in = L.reshape(x, (N, 1, in_C, D, H, W)) w_r = L.reshape(w, (1, C, in_C, 1, 1, 1)) y2 = x_in * w_r # [N, C, in_C, D, H, W] y2 = L.reduce_sum(y2, dim=[ 2, ]) # [N, C, D, H, W] y = y.numpy() y2 = y2.numpy() d = np.sum((y - y2)**2) print(d) ''' 总结: 1x1x1的3D卷积也有相似的结论。只不过口诀由