def masks_to_boxes(masks): """ Compute the bounding boxes around the provided masks The masks should be in format [N, H, W] where N is the number of masks, (H, W) are the spatial dimensions. Returns a [N, 4] tensors, with the boxes in xyxy format """ if np.sum(masks.shape) == 0: return dg.to_variable(np.zeros((0, 4))) h, w = masks.shape[-2:] y = dg.to_variable(np.arange(0, h, 1, dtype="float32")) x = dg.to_variable(np.arange(0, w, 1, dtype="float32")) y, x = T.meshgrid([y, x]) # [h, w] x_mask = (masks * L.unsqueeze(x, [0])) # [N, H, W] x_max = L.reduce_max(L.flatten(x_mask, axis=1), dim=-1) non_mask = dg.to_variable(~masks.numpy()) x_mask[non_mask] = 1e8 x_min = L.reduce_min(L.flatten(x_mask, axis=1), dim=-1) y_mask = (masks * L.unsqueeze(y, [0])) # [N, H, W] y_max = L.reduce_max(L.flatten(y_mask, axis=1), dim=-1) y_mask[non_mask] = 1e8 y_min = L.reduce_min(L.flatten(y_mask, axis=1), dim=-1) return L.stack([x_min, y_min, x_max, y_max], 1)
def loss(self, predictions, labels): logits, input_seqlen = predictions logits = L.flatten(logits, axis=2) labels = L.flatten(labels, axis=2) ce_loss, probs = L.softmax_with_cross_entropy(logits=logits, label=labels, return_softmax=True) loss = L.mean(x=ce_loss) return loss
def forward(self, x): # print(x[0, :, :, :, :].shape) x = self.conv1(x) # print("conv1 shape", x.shape) x = self.bn1(x) x = relu(x) if not self.no_max_pool: x = pool3d(x, pool_size=3, pool_type='max', pool_stride=2, pool_padding=1, data_format="NCDHW") # print("conv1 shape", x.shape) x = self.layer1(x) # print("layer1 shape", x.shape) x = self.layer2(x) # print("layer2 shape", x.shape) x = self.layer3(x) # print("layer3 shape", x.shape) x = self.layer4(x) # print("layer4 shape", x.shape) x = adaptive_pool3d(x, pool_size=[1, 1, 1], pool_type='avg') x = flatten(x) x = self.fc(x) return x
def test_flatten(self): program = Program() with program_guard(program): x = layers.data(name='x', append_batch_size=False, shape=[4, 4, 3], dtype="float32") out = layers.flatten(x, axis=1, name="flatten") self.assertIsNotNone(out)
def network_d(self, input, name="discriminator"): h_conv1 = conv2d(input, num_filters=64, filter_size=5, stride=2, padding=2, activation_fn='leaky_relu', relufactor=0.2, name=name + '_conv1') h_dropout1 = layers.dropout(h_conv1, dropout_prob=0.3) h_conv2 = conv2d(h_dropout1, num_filters=128, filter_size=5, stride=2, padding=2, activation_fn='leaky_relu', relufactor=0.2, name=name + '_conv2') h_dropout2 = layers.dropout(h_conv2, dropout_prob=0.3) h_conv3 = conv2d(h_dropout2, num_filters=128, filter_size=3, stride=2, padding_type="SAME", activation_fn='leaky_relu', relufactor=0.2, name=name + '_conv3') h_dropout3 = layers.dropout(h_conv3, dropout_prob=0.3) h_conv4 = conv2d(h_dropout3, num_filters=256, filter_size=3, stride=1, padding_type="SAME", activation_fn='leaky_relu', relufactor=0.2, name=name + '_conv4') h_dropout4 = layers.dropout(h_conv4, dropout_prob=0.3) features = layers.flatten(h_conv2) fake = linear(features, output_size=1, name=name + '_fc1') aux = linear(features, output_size=self.num_classes, name=name + '_fc2') return fake, aux
def _forward_impl(self, x): x = self.conv1(x) x = self.bn1(x) x = self.relu(x) x = self.maxpool(x) x = self.layer1(x) x = self.layer2(x) x = self.layer3(x) x = self.layer4(x) x = self.avgpool(x) x = L.flatten(x, 1) x = self.fc(x) return x
def net(self, inputs): print(inputs.shape) x = conv2d(inputs, 64, 3, padding=1, act='relu') x = conv2d(x, 64, 3, padding=1, act='relu') x = pool2d(x, 2, pool_stride=2) print(x.shape) x = conv2d(x, 128, 3, padding=1, act='relu') x = conv2d(x, 128, 3, padding=1, act='relu') x = pool2d(x, 2, pool_stride=2) print(x.shape) x = conv2d(x, 256, 3, padding=1, act='relu') x = conv2d(x, 256, 3, padding=1, act='relu') x = conv2d(x, 256, 3, padding=1, act='relu') x = pool2d(x, 2, pool_stride=2) print(x.shape) x = conv2d(x, 512, 3, padding=1, act='relu') x = conv2d(x, 512, 3, padding=1, act='relu') x = conv2d(x, 512, 3, padding=1, act='relu') x = pool2d(x, 2, pool_stride=2) print(x.shape) x = conv2d(x, 512, 3, padding=1, act='relu') x = conv2d(x, 512, 3, padding=1, act='relu') x = conv2d(x, 512, 3, padding=1, act='relu') x = pool2d(x, 2, pool_stride=2) print(x.shape) x = flatten(x) x = fc(x, 4096, act='relu') x = fc(x, 4096, act='relu') out = fc(x, self.class_num) print(out.shape) return out
def net(self, inputs=None): if self.inputs is None: self.inputs = inputs or fluid.layers.data( name=self.name + "_image", shape=self.image_size, dtype='float32') x = conv2d(self.inputs, 32, 3, stride=2, padding=1, act='leaky_relu', name=self.name + "_conv2d_1") x = dropout(x, 0.25) x = down_sampling_2(x, 64, name=self.name + "_64×32*32") x = down_sampling_2(x, 128, name=self.name + "_128×16*16") x = down_sampling_2(x, 256, name=self.name + "_256×8*8") x = flatten(x, name=self.name + "_fc") x = fc(x, 1, act="sigmoid") return x
num_classes = 10 epochs = 12 img_rows = 28 img_cols = 28 # define the model X = layers.data(name="img", shape=[-1, 1, 28, 28], dtype="float32") Y = layers.data(name="label", shape=[-1, 1], dtype="int64") h_conv = layers.conv2d(X, num_filters=32, filter_size=(3, 3), act="relu") h_conv = layers.conv2d(h_conv, num_filters=64, filter_size=(3, 3), act="relu") h_pool = layers.pool2d(h_conv, pool_size=(2, 2)) h_dropout = layers.dropout(h_pool, dropout_prob=0.25) h_flatten = layers.flatten(h_dropout) h_fc = layers.fc(h_flatten, size=128, act="relu", bias_attr=fluid.param_attr.ParamAttr(name="b_0")) h_dropout2 = layers.dropout(h_fc, dropout_prob=0.25) pred = layers.fc(h_dropout2, size=num_classes, act="softmax", bias_attr=fluid.param_attr.ParamAttr(name="b_1")) loss = layers.reduce_mean(layers.cross_entropy(input=pred, label=Y)) acc = layers.accuracy(input=pred, label=Y) test_program = fluid.default_main_program().clone(for_test=True)
def _reshape_to_2d(var): return layers.flatten(x=var, axis=2)