def __init__(self, hidden_size=128, intermediate_size=4 * 128, initializer_range=0.02): super(MLPLayer, self).__init__() d_model = hidden_size dim_feedforward = intermediate_size np.random.seed(2021) arr0 = np.random.normal(0, 0.02, size=(d_model, dim_feedforward)) arr1 = np.random.normal(0, 0.02, size=(dim_feedforward, d_model)) weight_attr0 = paddle.ParamAttr(initializer=NumpyArrayInitializer(arr0)) weight_attr1 = paddle.ParamAttr(initializer=NumpyArrayInitializer(arr1)) bias_attr = None self.linear0 = nn.Linear( d_model, dim_feedforward, weight_attr0, bias_attr=bias_attr) self.linear1 = nn.Linear( dim_feedforward, d_model, weight_attr1, bias_attr=bias_attr) self.linear2 = nn.Linear( d_model, dim_feedforward, weight_attr0, bias_attr=bias_attr) self.linear3 = nn.Linear( dim_feedforward, d_model, weight_attr1, bias_attr=bias_attr) self.linear4 = nn.Linear( d_model, dim_feedforward, weight_attr0, bias_attr=bias_attr) self.linear5 = nn.Linear( dim_feedforward, d_model, weight_attr1, bias_attr=bias_attr) self.norm0 = nn.LayerNorm(d_model, epsilon=1e-5) self.norm1 = nn.LayerNorm(d_model, epsilon=1e-5) self.norm2 = nn.LayerNorm(d_model, epsilon=1e-5)
def _create_tensor_from_numpy(self, numpy_array): paddle_array = fluid.layers.create_parameter( attr=ParamAttr(), shape=numpy_array.shape, dtype=numpy_array.dtype, default_initializer=NumpyArrayInitializer(numpy_array)) paddle_array.stop_gradient = True return paddle_array
def conv2d(x, weight, bias=None, stride=1, padding=1, dilation=1, groups=1): if bias is None: bias_attr = False else: bias_attr = NumpyArrayInitializer(bias.numpy()) return fluid.layers.conv2d( x, num_filters=weight.shape[0], filter_size=weight.shape[-2:], stride=stride, padding=padding, dilation=dilation, groups=groups, param_attr=fluid.ParamAttr( initializer=NumpyArrayInitializer(weight.numpy())), bias_attr=bias_attr) # from torch.nn.functional import l1_loss,mse_loss,binary_cross_entropy_with_logits # # def l1_loss(input, target, size_average=None, reduce=None, reduction='mean'): # return fluid.dygraph.L1Loss()
import paddle.fluid as fluid from paddle.fluid.dygraph.nn import Conv2D from paddle.fluid.initializer import NumpyArrayInitializer img = Image.open('./demo.png') with fluid.dygraph.guard(): # 设置卷积核参数 w = np.array([[-1,-1,-1], [-1,8,-1], [-1,-1,-1]], dtype='float32')/8 w = w.reshape([1, 1, 3, 3]) # 由于输入通道数是3,将卷积核的形状从[1,1,3,3]调整为[1,3,3,3] w = np.repeat(w, 3, axis=1) # 创建卷积算子,输出通道数为1,卷积核大小为3x3, # 并使用上面的设置好的数值作为卷积核权重的初始化参数 conv = Conv2D(num_channels=3, num_filters=1, filter_size=[3, 3], param_attr=fluid.ParamAttr( initializer=NumpyArrayInitializer(value=w))) # 将读入的图片转化为float32类型的numpy.ndarray x = np.array(img).astype('float32') # 图片读入成ndarry时,形状是[H, W, 3], # 将通道这一维度调整到最前面 x = np.transpose(x, (2,0,1)) # 将数据形状调整为[N, C, H, W]格式 x = x.reshape(1, 3, img.height, img.width) x = fluid.dygraph.to_variable(x) y = conv(x) out = y.numpy() plt.figure(figsize=(20, 10)) f = plt.subplot(121) f.set_title('input image', fontsize=15)
def create_fake_model(program_config): ''' Create a Paddle model(in memory) according to the given config. ''' paddle.enable_static() main_program_desc = core.ProgramDesc() util_program = fluid.Program() main_block_desc = main_program_desc.block(0) var_desc = main_block_desc.var(cpt.to_bytes("feed")) var_desc.set_type(core.VarDesc.VarType.FEED_MINIBATCH) var_desc.set_persistable(True) index = 0 for name, tensor_config in program_config.inputs.items(): var_desc = main_block_desc.var(cpt.to_bytes(name)) var_desc.set_type(core.VarDesc.VarType.LOD_TENSOR) var_desc.set_dtype(convert_np_dtype_to_dtype_(tensor_config.dtype)) var_desc.set_shape(tensor_config.shape) var_desc.set_need_check_feed(True) if tensor_config.lod is not None: var_desc.set_lod_level(len(tensor_config.lod)) op_desc = main_block_desc._prepend_op() op_desc.set_type("feed") op_desc.set_input('X', ["feed"]) op_desc.set_output('Out', [name]) op_desc._set_attr("col", index) index = index + 1 save_var_map = {} for name, tensor_config in program_config.weights.items(): var_desc = main_block_desc.var(cpt.to_bytes(name)) var_desc.set_type(core.VarDesc.VarType.LOD_TENSOR) var_desc.set_dtype(convert_np_dtype_to_dtype_(tensor_config.dtype)) var_desc.set_shape(tensor_config.shape) var_desc.set_persistable(True) save_var_map[name] = util_program.global_block().create_parameter( dtype=tensor_config.dtype, shape=tensor_config.shape, type=core.VarDesc.VarType.LOD_TENSOR, name=name, initializer=NumpyArrayInitializer(tensor_config.data)) in_vars = [] for name in sorted(save_var_map.keys()): in_vars.append(save_var_map[name]) out_var = util_program.global_block().create_var( type=core.VarDesc.VarType.RAW, name="out_var_0") out_var.desc.set_persistable(True) util_program.global_block().append_op(type='save_combine', inputs={'X': in_vars}, outputs={'Y': out_var}, attrs={ 'file_path': '', 'save_to_memory': True }) for op_config in program_config.ops: op_desc = main_block_desc.append_op() op_desc.set_type(op_config.type) for name, values in op_config.inputs.items(): op_desc.set_input(name, values) for name, values in op_config.attrs.items(): op_desc._set_attr(name, values) for name, values in op_config.outputs.items(): op_desc.set_output(name, values) for v in values: var_desc = main_block_desc.var(cpt.to_bytes(v)) var_desc.set_type(core.VarDesc.VarType.LOD_TENSOR) var_desc.set_dtype(convert_np_dtype_to_dtype_(np.float32)) if op_config.outputs_dtype is not None and v in op_config.outputs_dtype.keys( ): var_desc.set_dtype( convert_np_dtype_to_dtype_(op_config.outputs_dtype[v])) op_desc.infer_var_type(main_block_desc) op_desc.infer_shape(main_block_desc) op_desc.check_attrs() for index, name in enumerate(program_config.outputs): var_desc = main_block_desc.var(cpt.to_bytes("fetch")) var_desc.set_type(core.VarDesc.VarType.FETCH_LIST) var_desc.set_need_check_feed(True) op_desc = main_block_desc.append_op() op_desc.set_type("fetch") op_desc.set_input('X', [name]) op_desc.set_output('Out', ["fetch"]) op_desc._set_attr("col", index) main_program_desc._set_version() paddle.fluid.core.save_op_version_info(main_program_desc) model = main_program_desc.serialize_to_string() util_program._sync_with_cpp() place = fluid.CPUPlace() executor = fluid.Executor(place) scope = fluid.Scope() with fluid.scope_guard(scope): executor.run(util_program) params = scope.find_var("out_var_0").get_bytes() return model, params
def __init__(self, channels=1, params=[0, 1, 1, 1, 1], n_iter=10): super(FlowLayer, self).__init__() self.n_iter = n_iter sobel = np.kron(np.resize(np.eye(channels), [channels, channels, 1, 1]), np.array([[[[-0.5, 0, 0.5], [-0.5, 0, 0.5], [-0.5, 0, 0.5]]]])) # Sobel矩阵 wx = np.array([[[[-1, 1]]]]).repeat(channels, axis=0) wy = np.array([[[[-1], [1]]]]).repeat(channels, axis=0) if params[0]: self.conv_img_grad = Conv2D( num_channels=channels, num_filters=channels, filter_size=3, padding=1, stride=1, bias_attr=False, param_attr=fluid.ParamAttr(initializer=NumpyArrayInitializer( value=sobel))) self.conv_img_grad2 = Conv2D( num_channels=channels, num_filters=channels, filter_size=3, padding=1, stride=1, bias_attr=False, param_attr=fluid.ParamAttr(initializer=NumpyArrayInitializer( value=sobel.transpose([0, 1, 3, 2])))) else: self.conv_img_grad = Conv2D( num_channels=channels, num_filters=channels, filter_size=3, padding=1, stride=1, bias_attr=False, param_attr=fluid.ParamAttr( initializer=NumpyArrayInitializer(value=sobel), trainable=False)) self.conv_img_grad2 = Conv2D( num_channels=channels, num_filters=channels, filter_size=3, padding=1, stride=1, bias_attr=False, param_attr=fluid.ParamAttr(initializer=NumpyArrayInitializer( value=sobel.transpose([0, 1, 3, 2])), trainable=False)) if params[1]: self.conv_f_grad = Conv2D( num_channels=channels, num_filters=channels, filter_size=3, padding=0, stride=1, bias_attr=False, groups=channels, param_attr=fluid.ParamAttr(initializer=NumpyArrayInitializer( value=wx))) self.conv_f_grad2 = Conv2D( num_channels=channels, num_filters=channels, filter_size=3, padding=0, stride=1, bias_attr=False, groups=channels, param_attr=fluid.ParamAttr(initializer=NumpyArrayInitializer( value=wy))) self.conv_div = Conv2D( num_channels=channels, num_filters=channels, filter_size=(1, 2), padding=0, stride=1, bias_attr=False, groups=channels, param_attr=fluid.ParamAttr(initializer=NumpyArrayInitializer( value=wx))) self.conv_div2 = Conv2D( num_channels=channels, num_filters=channels, filter_size=(2, 1), padding=0, stride=1, bias_attr=False, groups=channels, param_attr=fluid.ParamAttr(initializer=NumpyArrayInitializer( value=wy))) else: self.conv_f_grad = Conv2D( num_channels=channels, num_filters=channels, filter_size=3, padding=0, stride=1, bias_attr=False, groups=channels, param_attr=fluid.ParamAttr( initializer=NumpyArrayInitializer(value=wx), trainable=False)) self.conv_f_grad2 = Conv2D( num_channels=channels, num_filters=channels, filter_size=3, padding=0, stride=1, bias_attr=False, groups=channels, param_attr=fluid.ParamAttr( initializer=NumpyArrayInitializer(value=wy), trainable=False)) self.conv_div = Conv2D( num_channels=channels, num_filters=channels, filter_size=(1, 2), padding=0, stride=1, bias_attr=False, groups=channels, param_attr=fluid.ParamAttr( initializer=NumpyArrayInitializer(value=wx), trainable=False)) self.conv_div2 = Conv2D( num_channels=channels, num_filters=channels, filter_size=(2, 1), padding=0, stride=1, bias_attr=False, groups=channels, param_attr=fluid.ParamAttr( initializer=NumpyArrayInitializer(value=wy), trainable=False)) self.channels = channels self.t = 0.3 # theta self.l = 0.15 # lambda self.a = 0.25 # tau if params[2]: self.t = fluid.layers.create_parameter( shape=[1], dtype='float32', attr=fluid.ParamAttr(initializer=ConstantInitializer( value=self.t))) if params[3]: self.l = fluid.layers.create_parameter( shape=[1], dtype='float32', attr=fluid.ParamAttr(initializer=ConstantInitializer( value=self.l))) if params[4]: self.a = fluid.layers.create_parameter( shape=[1], dtype='float32', attr=fluid.ParamAttr(initializer=ConstantInitializer( value=self.a)))
from paddle.fluid.initializer import NumpyArrayInitializer # 读入图片并转成numpy.ndarray img = Image.open(r'D:\softwaresavfile\Github\machine_learning\计算视觉基础\test2.jpg' ).convert('L') img = np.array(img) # 换成灰度图 with fluid.dygraph.guard(): # 创建初始化参数 w = np.ones([1, 1, 5, 5], dtype='float32') / 25 conv = Conv2D( num_channels=1, num_filters=1, filter_size=[5, 5], param_attr=fluid.ParamAttr(initializer=NumpyArrayInitializer(value=w))) x = img.astype('float32') x = x.reshape(1, 1, img.shape[0], img.shape[1]) x = fluid.dygraph.to_variable(x) y = conv(x) out = y.numpy() plt.figure(figsize=(20, 12)) f = plt.subplot(121) f.set_title('input image') plt.imshow(img, cmap='gray') f = plt.subplot(122) f.set_title('output feature map') out = out.squeeze()
def embedding(x, weight): return fluid.layers.embedding(fluid.layers.reshape(x, [-1, 1]), size=weight.shape, param_attr=NumpyArrayInitializer( weight.numpy()))
def _build_once(self, input): input_channel = input.shape[1] if (input_channel == self._groups and self._num_filters == input_channel and not self._use_cudnn): self._op_type = "depthwise_conv2d_transpose" if not isinstance(input, Variable): raise TypeError("Input of conv2d_transpose must be Variable") self._padding = utils.convert_to_list(self._padding, 2, "padding") self._stride = utils.convert_to_list(self._stride, 2, "stride") self._dilation = utils.convert_to_list(self._dilation, 2, "dilation") if not isinstance(self._use_cudnn, bool): raise ValueError("use_cudnn should be True or False") if self._filter_size is None: if self._output_size is None: raise ValueError( "output_size must be set when filter_size is None") if isinstance(self._output_size, int): self._output_size = [self._output_size, self._output_size] h_in = input.shape[2] w_in = input.shape[3] filter_size_h = (self._output_size[0] - (h_in - 1) * self._stride[0] + 2 * self._padding[0] - 1) // self._dilation[0] + 1 filter_size_w = (self._output_size[1] - (w_in - 1) * self._stride[1] + 2 * self._padding[1] - 1) // self._dilation[1] + 1 self._filter_size = [filter_size_h, filter_size_w] else: self._filter_size = utils.convert_to_list( self._filter_size, 2, "conv2d_transpose.filter_size") if self._output_size is None: self._output_size = [] elif isinstance(self._output_size, list) or isinstance( self._output_size, int): self._output_size = utils.convert_to_list(self._output_size, 2, "output_size") else: raise ValueError("output_size should be list or int") self._padding = utils.convert_to_list(self._padding, 2, "padding") self._groups = 1 if self._groups is None else self._groups filter_shape = [ input_channel, self._num_filters // self._groups, ] + self._filter_size # img filter v (direction) self._img_filter_v = self.create_parameter( dtype=input.dtype, shape=filter_shape, attr=self._param_attr) # img filter g (magnitude) img_filter_magnitude = _norm( self._img_filter_v.numpy(), dim=0) # CAUTION: hard-code self._img_filter_g = self.create_parameter( dtype=input.dtype, shape=img_filter_magnitude.shape, attr=fluid.ParamAttr( initializer=NumpyArrayInitializer(img_filter_magnitude))) self._img_bias = self.create_parameter( attr=self._bias_attr, shape=[self._num_filters], dtype=self._dtype, is_bias=True)