pytorch转keras
pytorch与keras的区别模型输入:区别pytorchkerasAPItorch.tensorInput形状NCHWNHWC#pytorch #批次, 通道, 高, 宽a = torch.randn(1,4,160,160)#keras#形状和批次分开 a = Input(shape = (160, 160, 4), batch_size = 1)卷积:区别pytorchkerasAPInn.Conv2DConv2D输入通道参数有输入通道没有输入通道padding任意输入一个值'valid'没填充,'same'有填充#pytorchself.conv = Conv2d(in_channels, out_channels, kernel_size, stride, padding=padding, bias=bias)#kerasoutput = Conv2D(input.shape[-1] // reduction, kernel = (1,1), padding = "valid", use_bias = False)(output)反卷积:区别pytorchkerasAPInn.ConvTranspose2dConv2DTranspose输入通道参数有输入通道没有输入通道#pytorchself.dconv = nn.ConvTranspose2d(in_channels, out_channels, kernel_size, stride, bias=bias)#kerasoutput = Conv2DTranspose(out_channels, kernel_size, stride, use_bias=bias)(input)上采样:区别pytorchkerasAPInn.UpsamplingBilinear2d需要自定义#pytorch #缩放因子self.up = nn.UpsamplingBilinear2d(scale_factor=2)#kerasimport tensorflow.compat.v1 as tf#需要tf1版本里的resizedef my_upsampling(x,img_w,img_h,method=0): """0:双线性差值。1:最近邻居法。2:双三次插值法。3:面积插值法""" return tf.image.resize_images(x,(img_w,img_h),0) #输出宽高output = Lambda(my_upsampling,arguments={'img_w':input.shape[2] * 2,'img_h':input.shape[1] * 2})(input)池化:区别pytorchkerasnn.AdaptiveAvgPool2d(1)没有自适应池化,需要利用自定义池化自定义BatchNormalization:区别pytorchkerasAPInn.BatchNorm2dBatchNormalization输入有输入通道没有输入通道#pytorchnn.BatchNorm2d(in_size),#kerasoutput = BatchNormalization()(output)激活:区别pytorchkerasAPInn.ReLUActivation不同激活函数不同api不同激活通过输入字符串来表示#pytorchself.act = nn.Sigmoid()self.act = nn.ReLU#kerasoutput = Activation("sigmoid")(output)output = Activation("relu")(output)不定期更新。。。来源:https://www.icode9.com/content-4-816201.html