Python build mask

51 Python code examples are found related to " build mask". You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.
Example 1
Source File: layers.py    From UMNN with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def build_mask(self):
        n_in, n_out = self.in_features, self.out_features
        assert n_in % n_out == 0 or n_out % n_in == 0

        mask = np.ones((n_in, n_out), dtype=np.float32)
        if n_out >= n_in:
            k = n_out // n_in
            for i in range(n_in):
                mask[i + 1:, i * k:(i + 1) * k] = 0
                if self.diagonal_zeros:
                    mask[i:i + 1, i * k:(i + 1) * k] = 0
        else:
            k = n_in // n_out
            for i in range(n_out):
                mask[(i + 1) * k:, i:i + 1] = 0
                if self.diagonal_zeros:
                    mask[i * k:(i + 1) * k:, i:i + 1] = 0
        return mask 
Example 2
Source File: abstractive_summarization_bertsum.py    From nlp-recipes with MIT License 6 votes vote down vote up
def build_mask(sequence, pad_token_id):
    """ Builds the mask. The attention mechanism will only attend to positions
    with value 1.

    Args:
        sequence (list): sequences for which the mask is built for.
        pad_token_id (long): padding token id for which the mask is 0.

    Returns:
        mask (list): sequences of 1s and 0s.

    """
    mask = torch.ones_like(sequence)
    idx_pad_tokens = sequence == pad_token_id
    mask[idx_pad_tokens] = 0
    return mask 
Example 3
Source File: xmlPAGE.py    From P2PaLA with GNU General Public License v3.0 6 votes vote down vote up
def build_baseline_mask(self, out_size, color, line_width):
        """
        Builds a "image" mask of Baselines on XML-PAGE
        """
        size = self.get_size()[::-1]
        # --- Although NNLLoss requires an Long Tensor (np.int -> torch.LongTensor)
        # --- is better to keep mask as np.uint8 to save disk space, then change it
        # --- to np.int @ dataloader only if NNLLoss is going to be used.
        mask = np.zeros((out_size[0], out_size[1]), np.uint8)
        scale_factor = out_size / size
        for element in self.root.findall("".join([".//", self.base, "Baseline"])):
            # --- get element coords
            str_coords = element.attrib.get("points").split()
            coords = np.array([i.split(",") for i in str_coords]).astype(np.int)
            coords = (coords * np.flip(scale_factor, 0)).astype(np.int)
            cv2.polylines(mask, [coords.reshape(-1, 1, 2)], False, color, line_width)
        if not mask.any():
            self.logger.warning(
                    "File {} do not contains baselines".format(self.name)
                    )
        return mask 
Example 4
Source File: distributed_executer.py    From tpu_models with Apache License 2.0 6 votes vote down vote up
def build_mask_rcnn_estimator(self, params, run_config, mode):
    """Returns Mask Rcnn model running on MultiWorkerMirroredStrategy."""
    assert mode in ('train', 'eval')
    if mode == 'train':
      return tf.estimator.Estimator(
          model_fn=self._model_fn,
          model_dir=self._flags.model_dir,
          config=run_config,
          params=params)

    # Evaluation on multi-worker mirrored strategy is done in CPU for now
    # as only `train_and_evaluate` is supported and eval pipeline for
    # Mask RCNN model is uses `predict` API.
    cpu_run_config = tf.estimator.RunConfig(model_dir=self._flags.model_dir)
    return tf.estimator.Estimator(
        model_fn=self._model_fn,
        model_dir=self._flags.model_dir,
        config=cpu_run_config,
        params=params) 
Example 5
Source File: utils.py    From PyKMIP with Apache License 2.0 6 votes vote down vote up
def build_cryptographic_usage_mask(logger, object_type):
    if object_type == ObjectType.CERTIFICATE:
        flags = [CryptographicUsageMask.ENCRYPT,
                 CryptographicUsageMask.VERIFY]
    elif (object_type == ObjectType.SYMMETRIC_KEY or
          object_type == ObjectType.SECRET_DATA):
        flags = [CryptographicUsageMask.ENCRYPT,
                 CryptographicUsageMask.DECRYPT]
    elif object_type == ObjectType.PUBLIC_KEY:
        flags = [CryptographicUsageMask.VERIFY]
    elif object_type == ObjectType.PRIVATE_KEY:
        flags = [CryptographicUsageMask.SIGN]
    else:
        logger.error("Unrecognized object type, could not build cryptographic "
                     "usage mask")
        sys.exit()

    attribute_type = AttributeType.CRYPTOGRAPHIC_USAGE_MASK
    attribute_factory = AttributeFactory()
    usage_mask = attribute_factory.create_attribute(attribute_type, flags)

    return usage_mask 
Example 6
Source File: pong_catastrophe.py    From human-rl with MIT License 6 votes vote down vote up
def label_and_build_mask(self, episode):
        is_catastrophe_array = np.array(
            [is_catastrophe(frame.image) for frame in episode.frames if frame.action is not None])
        # should_block_array = np.array([should_block(frame.image, frame.action) for frame in episode.frames])

        labels = np.full(len(episode.frames), fill_value=False, dtype=np.bool)
        mask = np.full(len(episode.frames), fill_value=True, dtype=np.bool)

        for i in range(len(episode.frames)):
            if i + self.block_radius + 1 >= len(episode.frames):
                mask[i] = False
                continue
            if is_catastrophe_array[i]:
                mask[i] = False
                continue
            for j in range(self.block_radius + 1):
                if is_catastrophe_array[i + j + 1]:
                    labels[i] = True
                    break
        return labels, mask 
Example 7
Source File: PipelineChipseq.py    From CGATPipelines with MIT License 6 votes vote down vote up
def buildQuicksectMask(bed_file):
    '''return Quicksect object containing the regions specified
       takes a bed file listing the regions to mask 
    '''
    mask = IndexedGenome.Quicksect()

    n_regions = 0
    for bed in Bed.iterator(IOTools.openFile(bed_file)):
        # it is neccessary to extend the region to make an accurate mask
        mask.add(bed.contig, (bed.start - 1), (bed.end + 1), 1)
        n_regions += 1

    E.info("Built Quicksect mask for %i regions" % n_regions)

    return(mask)

############################################################
############################################################
############################################################ 
Example 8
Source File: pipeline_rnaseq.py    From CGATPipelines with MIT License 6 votes vote down vote up
def buildMaskGtf(infile, outfile):
    '''
    This takes ensembl annotations (geneset_all.gtf.gz) and writes out all entries that
    have a 'source' match to "rRNA" or 'contig' match to "chrM". for use with cufflinks
    '''
    geneset = IOTools.openFile(infile)
    outf = open(outfile, "wb")
    for entry in GTF.iterator(geneset):
        if re.findall("rRNA", entry.source) or re.findall("chrM", entry.contig):
            outf.write("\t".join((list(map(str, [entry.contig, entry.source, entry.feature, entry.start, entry.end, ".", entry.strand, ".",
                                                 "transcript_id" + " " + '"' + entry.transcript_id + '"' + ";" + " " + "gene_id" + " " + '"' + entry.gene_id + '"'])))) + "\n")

    outf.close()

#########################################################################
#########################################################################
#########################################################################
######################################################################### 
Example 9
Source File: mask_head.py    From detectron2 with Apache License 2.0 5 votes vote down vote up
def build_mask_head(cfg, input_shape):
    """
    Build a mask head defined by `cfg.MODEL.ROI_MASK_HEAD.NAME`.
    """
    name = cfg.MODEL.ROI_MASK_HEAD.NAME
    return ROI_MASK_HEAD_REGISTRY.get(name)(cfg, input_shape) 
Example 10
Source File: data_abs.py    From fast-bert with Apache License 2.0 5 votes vote down vote up
def build_mask(sequence, pad_token_id):
    """ Builds the mask. The attention mechanism will only attend to positions
    with value 1. """
    mask = torch.ones_like(sequence)
    idx_pad_tokens = sequence == pad_token_id
    mask[idx_pad_tokens] = 0
    return mask 
Example 11
Source File: loss.py    From SRNet with GNU General Public License v3.0 5 votes vote down vote up
def build_l1_loss_with_mask(x_t, x_o, mask, name = 'l1_loss'):
    
    mask_ratio = 1. - tf.reduce_sum(mask) / tf.cast(tf.size(mask), tf.float32)
    l1 = tf.abs(x_t - x_o)
    return mask_ratio * tf.reduce_mean(l1 * mask) + (1. - mask_ratio) * tf.reduce_mean(l1 * (1. - mask)) 
Example 12
Source File: _auth_utils.py    From firebase-admin-python with Apache License 2.0 5 votes vote down vote up
def build_update_mask(params):
    """Creates an update mask list from the given dictionary."""
    mask = []
    for key, value in params.items():
        if isinstance(value, dict):
            child_mask = build_update_mask(value)
            for child in child_mask:
                mask.append('{0}.{1}'.format(key, child))
        else:
            mask.append(key)

    return sorted(mask) 
Example 13
Source File: xmlPAGE.py    From P2PaLA with GNU General Public License v3.0 5 votes vote down vote up
def build_mask(self, out_size, element_name, color_dic):
        """
        Builds a "image" mask of desired elements
        """
        size = self.get_size()[::-1]
        mask = np.zeros(out_size, np.uint8)
        scale_factor = out_size / size
        for element in element_name:
            for node in self.root.findall("".join([".//", self.base, element])):
                # --- get element type
                e_type = self.get_region_type(node)
                if e_type == None or e_type not in color_dic:
                    e_color = 175
                    self.logger.warning(
                        'Element type "{}"undefined on color dic, set to default=175'.format(
                            e_type
                        )
                    )
                    print(
                        'Element type "{}"undefined on color dic, set to default=175'.format(
                            e_type
                        )
                    )
                    continue
                else:
                    e_color = color_dic[e_type]
                # --- get element coords
                coords = self.get_coords(node)
                coords = (coords * np.flip(scale_factor, 0)).astype(np.int)
                cv2.fillConvexPoly(mask, coords, e_color)
        if not mask.any():
            self.logger.warning(
                    "File {} do not contains regions".format(self.name)
                    )
        return mask 
Example 14
Source File: setup.py    From multimodal-vae-public with MIT License 5 votes vote down vote up
def build_mask_dataset(in_dir, out_dir, model_path):
    """Generate a dataset of segmentation masks from images.

    @param in_dir: string
                   input directory of images.
    @param out_dir: string
                    output directory of images.
    @param model_path: string
                       path to HOG model for facial features.
    """
    # initialize dlib's face detector (HOG-based) and then create
    # the facial landmark predictor
    detector = dlib.get_frontal_face_detector()
    predictor = dlib.shape_predictor(model_path)

    image_paths = os.listdir(in_dir)
    n_images = len(image_paths)
    for i, image_path in enumerate(image_paths):
        print('Building face-mask dataset: [%d/%d] images.' % (i + 1, n_images))
        image_full_path = os.path.join(in_dir, image_path)

        image = cv2.imread(image_full_path)
        gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

        # detect faces in the grayscale image
        rects = detector(gray, 1)
        try:
            rect = rects[0]  # we are only going to use the first one

            # determine the facial landmarks for the face region, then
            # convert the landmark (x, y)-coordinates to a NumPy array
            shape = predictor(gray, rect)
            shape = face_utils.shape_to_np(shape)
            output = visualize_facial_landmarks(image, shape)
            cv2.imwrite(os.path.join(out_dir, image_path), output)
        except:
            # if for some reason no bounding box is found, send blank.
            output = np.ones_like(image) * 255
            cv2.imwrite(os.path.join(out_dir, image_path), output) 
Example 15
Source File: encoder_decoder_segmentation.py    From taskonomy with MIT License 5 votes vote down vote up
def build_ones_mask(self):
        '''Build a mask of ones which has the same size as the input.
        '''
        cfg = self.cfg
        C = cfg['target_num_channels']
        batch_size = cfg['batch_size']
        mask = tf.constant(1.0, tf.float32, shape=[batch_size, 256, 256, C],
                           name='identity_mask')
        return mask 
Example 16
Source File: refinenet_mask_v3.py    From FashionAI_KeyPoint_Detection_Challenge_Keras with MIT License 5 votes vote down vote up
def build_resnet101_stack_mask_v3(inputHeight, inputWidth, n_classes, nStack):

    input_mask = Input(shape=(inputHeight//2, inputHeight//2, n_classes), name='mask')
    input_ohem_mask = Input(shape=(inputHeight//2, inputHeight//2, n_classes), name='ohem_mask')

    # backbone network
    input_image, lf2x,lf4x, lf8x, lf16x = load_backbone_res101net(inputHeight, inputWidth)

    # global net
    g8x, g4x, g2x = create_global_net_dilated((lf2x, lf4x, lf8x, lf16x), n_classes)

    s8x, s4x, s2x = g8x, g4x, g2x

    g2x_mask = apply_mask_to_output(g2x, input_mask)

    outputs =  [g2x_mask]
    for i in range(nStack):
        s8x, s4x, s2x =  create_stack_refinenet((s8x, s4x, s2x), n_classes, 'stack_'+str(i))
        if i == (nStack-1): # last stack with ohem_mask
            s2x_mask = apply_mask_to_output(s2x, input_ohem_mask)
        else:
            s2x_mask = apply_mask_to_output(s2x, input_mask)
        outputs.append(s2x_mask)

    model = Model(inputs=[input_image, input_mask, input_ohem_mask], outputs=outputs)

    adam = Adam(lr=1e-4)
    model.compile(optimizer=adam, loss=euclidean_loss, metrics=["accuracy"])
    return model 
Example 17
Source File: cell_utils.py    From drizzlepac with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def build_mask(self):
        naxis1, naxis2 = self.wcs.pixel_shape
        edges_x = [0]*naxis2 + [naxis1-1]*naxis2 + list(range(naxis1)) * 2
        edges_y = list(range(naxis2)) * 2 + [0]*naxis1 + [naxis2-1]*naxis1

        polygon = list(zip(edges_x, edges_y))
        img = Image.new("L", (naxis1, naxis2), 0)
        ImageDraw.Draw(img).polygon(polygon, outline=1, fill=1)
        mask = np.array(img)

        self.mask = mask 
Example 18
Source File: imageObject.py    From drizzlepac with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def buildMask(self,chip,bits=0,write=False):
        """
        Build masks as specified in the user parameters found in the
        configObj object.

        We should overload this function in the instrument specific
        implementations so that we can add other stuff to the badpixel
        mask? Like vignetting areas and chip boundries in nicmos which
        are camera dependent? these are not defined in the DQ masks, but
        should be masked out to get the best results in multidrizzle.
        """
        dqarr = self.getData(exten=self.maskExt+','+str(chip))
        dqmask = buildmask.buildMask(dqarr,bits)

        if write:
            phdu = fits.PrimaryHDU(data=dqmask,header=self._image[self.maskExt,chip].header)
            dqmask_name = self._image[self.scienceExt,chip].dqrootname+'_dqmask.fits'
            log.info('Writing out DQ/weight mask: %s' % dqmask_name)
            if os.path.exists(dqmask_name): os.remove(dqmask_name)
            phdu.writeto(dqmask_name)
            del phdu
            self._image[self.scienceExt,chip].dqmaskname = dqmask_name
            # record the name of this mask file that was created for later
            # removal by the 'clean()' method
            self._image[self.scienceExt,chip].outputNames['dqmask'] = dqmask_name
        del dqarr
        return dqmask 
Example 19
Source File: request.py    From Xpedite with Apache License 2.0 5 votes vote down vote up
def buildMask(self):
    """Builds bit mask for general purpose pmu request"""
    return struct.pack('=BBBBBBBB',
      self.event.eventSelect,
      self.event.unitMask,
      1 if self.user else 0,
      1 if self.kernel else 0,
      self.event.invert,
      self.event.counterMask,
      self.event.edgeDetect,
      self.event.anyThread) 
Example 20
Source File: encoder.py    From OpenNMT-tf with MIT License 5 votes vote down vote up
def build_mask(self, inputs, sequence_length=None, dtype=tf.bool):
    """Builds a boolean mask for :obj:`inputs`."""
    if sequence_length is None:
      return None
    return tf.sequence_mask(sequence_length, maxlen=tf.shape(inputs)[1], dtype=dtype) 
Example 21
Source File: masked_convolution.py    From attn2d with MIT License 5 votes vote down vote up
def build_mask(self, unidirectional=False):
        mask = torch.ones_like(self.weight)
        if self.kernel_size > 1:
            mask[:, :, self.kernel_size // 2 + 1:, :] = 0
            if unidirectional:
                mask[:, :, :, self.kernel_size // 2 + 1:] = 0
        assert(mask.shape == self.weight.shape), \
            "Mask of shape {} must match weights of shape {}" \
            .format(mask.shape, self.weight.shape)
        return mask 
Example 22
Source File: neural_stack.py    From BERT with Apache License 2.0 5 votes vote down vote up
def build_read_mask(self):
    """Creates a mask which allows us to attenuate subsequent read strengths.

    This is exposed as it's own function so that it can be overridden to provide
    alternate read adressing schemes.

    Returns:
      A tf.float32 tensor of shape [1, memory_size, memory_size]
    """
    return common_layers.mask_pos_gt(self._memory_size, self._memory_size) 
Example 23
Source File: attn2d_sim_hmm.py    From attn2d with MIT License 5 votes vote down vote up
def build_grid_mask(self, src_tokens, tgt_tokens, pad):
        src_mask = src_tokens.eq(pad)  # N, Ts
        tgt_mask = tgt_tokens.eq(pad)  # N, Tt
        grid_mask = (src_mask.unsqueeze(1) + tgt_mask.unsqueeze(-1)).gt(0)  # N, Tt, Ts: either source or target is padded
        return grid_mask 
Example 24
Source File: MaskRCNN.py    From PyTorch-Luna16 with Apache License 2.0 4 votes vote down vote up
def build_fpn_mask_graph(rois, feature_maps,
                         image_shape, pool_size, num_classes):
    """Builds the computation graph of the mask head of Feature Pyramid Network.

    rois: [batch, num_rois, (y1, x1, y2, x2)] Proposal boxes in normalized
          coordinates.
    feature_maps: List of feature maps from different layers of the pyramid,
                  [P2, P3, P4, P5]. Each has a different resolution.
    image_shape: [height, width, depth]
    pool_size: The width of the square feature map generated from ROI Pooling.
    num_classes: number of classes, which determines the depth of the results

    Returns: Masks [batch, roi_count, height, width, num_classes]
    """
    # ROI Pooling
    # Shape: [batch, boxes, pool_height, pool_width, channels]
    x = PyramidROIAlign([pool_size, pool_size], image_shape,
                        name="roi_align_mask")([rois] + feature_maps)

    # Conv layers
    x = TimeDistributed(Conv2D(256, (3, 3), padding="same"),
                           name="mrcnn_mask_conv1")(x)
    x = TimeDistributed(BatchNorm(axis=3),
                           name='mrcnn_mask_bn1')(x)
    x = Activation('relu')(x)

    x = TimeDistributed(Conv2D(256, (3, 3), padding="same"),
                           name="mrcnn_mask_conv2")(x)
    x = TimeDistributed(BatchNorm(axis=3),
                           name='mrcnn_mask_bn2')(x)
    x = Activation('relu')(x)

    x = TimeDistributed(Conv2D(256, (3, 3), padding="same"),
                           name="mrcnn_mask_conv3")(x)
    x = TimeDistributed(BatchNorm(axis=3),
                           name='mrcnn_mask_bn3')(x)
    x = Activation('relu')(x)

    x = TimeDistributed(Conv2D(256, (3, 3), padding="same"),
                           name="mrcnn_mask_conv4")(x)
    x = TimeDistributed(BatchNorm(axis=3),
                           name='mrcnn_mask_bn4')(x)
    x = Activation('relu')(x)

    x = TimeDistributed(Conv2DTranspose(256, (2, 2), strides=2, activation="relu"),
                           name="mrcnn_mask_deconv")(x)
    x = TimeDistributed(Conv2D(num_classes, (1, 1), strides=1, activation="sigmoid"),
                           name="mrcnn_mask")(x)
    return x


############################################################
#  Loss Functions
############################################################ 
Example 25
Source File: model.py    From latte with Apache License 2.0 4 votes vote down vote up
def build_fpn_mask_graph(rois, feature_maps,
                         image_shape, pool_size, num_classes):
    """Builds the computation graph of the mask head of Feature Pyramid Network.

    rois: [batch, num_rois, (y1, x1, y2, x2)] Proposal boxes in normalized
          coordinates.
    feature_maps: List of feature maps from diffent layers of the pyramid,
                  [P2, P3, P4, P5]. Each has a different resolution.
    image_shape: [height, width, depth]
    pool_size: The width of the square feature map generated from ROI Pooling.
    num_classes: number of classes, which determines the depth of the results

    Returns: Masks [batch, roi_count, height, width, num_classes]
    """
    # ROI Pooling
    # Shape: [batch, boxes, pool_height, pool_width, channels]
    x = PyramidROIAlign([pool_size, pool_size], image_shape,
                        name="roi_align_mask")([rois] + feature_maps)

    # Conv layers
    x = KL.TimeDistributed(KL.Conv2D(256, (3, 3), padding="same"),
                           name="mrcnn_mask_conv1")(x)
    x = KL.TimeDistributed(BatchNorm(axis=3),
                           name='mrcnn_mask_bn1')(x)
    x = KL.Activation('relu')(x)

    x = KL.TimeDistributed(KL.Conv2D(256, (3, 3), padding="same"),
                           name="mrcnn_mask_conv2")(x)
    x = KL.TimeDistributed(BatchNorm(axis=3),
                           name='mrcnn_mask_bn2')(x)
    x = KL.Activation('relu')(x)

    x = KL.TimeDistributed(KL.Conv2D(256, (3, 3), padding="same"),
                           name="mrcnn_mask_conv3")(x)
    x = KL.TimeDistributed(BatchNorm(axis=3),
                           name='mrcnn_mask_bn3')(x)
    x = KL.Activation('relu')(x)

    x = KL.TimeDistributed(KL.Conv2D(256, (3, 3), padding="same"),
                           name="mrcnn_mask_conv4")(x)
    x = KL.TimeDistributed(BatchNorm(axis=3),
                           name='mrcnn_mask_bn4')(x)
    x = KL.Activation('relu')(x)

    x = KL.TimeDistributed(KL.Conv2DTranspose(256, (2, 2), strides=2, activation="relu"),
                           name="mrcnn_mask_deconv")(x)
    x = KL.TimeDistributed(KL.Conv2D(num_classes, (1, 1), strides=1, activation="sigmoid"),
                           name="mrcnn_mask")(x)
    return x


############################################################
#  Loss Functions
############################################################ 
Example 26
Source File: model.py    From ocrd_anybaseocr with Apache License 2.0 4 votes vote down vote up
def build_fpn_mask_graph(rois, feature_maps, image_meta,
                         pool_size, num_classes, train_bn=True):
    """Builds the computation graph of the mask head of Feature Pyramid Network.

    rois: [batch, num_rois, (y1, x1, y2, x2)] Proposal boxes in normalized
          coordinates.
    feature_maps: List of feature maps from different layers of the pyramid,
                  [P2, P3, P4, P5]. Each has a different resolution.
    image_meta: [batch, (meta data)] Image details. See compose_image_meta()
    pool_size: The width of the square feature map generated from ROI Pooling.
    num_classes: number of classes, which determines the depth of the results
    train_bn: Boolean. Train or freeze Batch Norm layers

    Returns: Masks [batch, num_rois, MASK_POOL_SIZE, MASK_POOL_SIZE, NUM_CLASSES]
    """
    # ROI Pooling
    # Shape: [batch, num_rois, MASK_POOL_SIZE, MASK_POOL_SIZE, channels]
    x = PyramidROIAlign([pool_size, pool_size],
                        name="roi_align_mask")([rois, image_meta] + feature_maps)

    # Conv layers
    x = KL.TimeDistributed(KL.Conv2D(256, (3, 3), padding="same"),
                           name="mrcnn_mask_conv1")(x)
    x = KL.TimeDistributed(BatchNorm(),
                           name='mrcnn_mask_bn1')(x, training=train_bn)
    x = KL.Activation('relu')(x)

    x = KL.TimeDistributed(KL.Conv2D(256, (3, 3), padding="same"),
                           name="mrcnn_mask_conv2")(x)
    x = KL.TimeDistributed(BatchNorm(),
                           name='mrcnn_mask_bn2')(x, training=train_bn)
    x = KL.Activation('relu')(x)

    x = KL.TimeDistributed(KL.Conv2D(256, (3, 3), padding="same"),
                           name="mrcnn_mask_conv3")(x)
    x = KL.TimeDistributed(BatchNorm(),
                           name='mrcnn_mask_bn3')(x, training=train_bn)
    x = KL.Activation('relu')(x)

    x = KL.TimeDistributed(KL.Conv2D(256, (3, 3), padding="same"),
                           name="mrcnn_mask_conv4")(x)
    x = KL.TimeDistributed(BatchNorm(),
                           name='mrcnn_mask_bn4')(x, training=train_bn)
    x = KL.Activation('relu')(x)

    x = KL.TimeDistributed(KL.Conv2DTranspose(256, (2, 2), strides=2, activation="relu"),
                           name="mrcnn_mask_deconv")(x)
    x = KL.TimeDistributed(KL.Conv2D(num_classes, (1, 1), strides=1, activation="sigmoid"),
                           name="mrcnn_mask")(x)
    return x


############################################################
#  Loss Functions
############################################################ 
Example 27
Source File: coupling.py    From pixyz with MIT License 4 votes vote down vote up
def build_mask(self, x):
        """
        Parameters
        ----------
        x : torch.Tensor

        Returns
        -------
        mask : torch.tensor

        Examples
        --------
        >>> scale_translate_net = lambda x: (x, x)
        >>> f1 = AffineCoupling(4, mask_type="channel_wise", scale_translate_net=scale_translate_net,
        ...                     inverse_mask=False)
        >>> x1 = torch.randn([1,4,3,3])
        >>> f1.build_mask(x1)
        tensor([[[[1.]],
        <BLANKLINE>
                 [[1.]],
        <BLANKLINE>
                 [[0.]],
        <BLANKLINE>
                 [[0.]]]])
        >>> f2 = AffineCoupling(2, mask_type="checkerboard", scale_translate_net=scale_translate_net,
        ...                     inverse_mask=True)
        >>> x2 = torch.randn([1,2,5,5])
        >>> f2.build_mask(x2)
        tensor([[[[0., 1., 0., 1., 0.],
                  [1., 0., 1., 0., 1.],
                  [0., 1., 0., 1., 0.],
                  [1., 0., 1., 0., 1.],
                  [0., 1., 0., 1., 0.]]]])

        """
        if x.dim() == 4:
            [_, channels, height, width] = x.shape
            if self.mask_type == "checkerboard":
                mask = checkerboard_mask(height, width, self.inverse_mask)
                return torch.from_numpy(mask).view(1, 1, height, width).to(x.device)
            else:
                mask = channel_wise_mask(channels, self.inverse_mask)
                return torch.from_numpy(mask).view(1, channels, 1, 1).to(x.device)

        elif x.dim() == 2:
            [_, n_features] = x.shape
            if self.mask_type != "checkerboard":
                mask = channel_wise_mask(n_features, self.inverse_mask)
                return torch.from_numpy(mask).view(1, n_features).to(x.device)

        raise ValueError 
Example 28
Source File: buildmask.py    From drizzlepac with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
def buildMaskImage(rootname, bitvalue, output, extname='DQ', extver=1):
    """ Builds mask image from rootname's DQ array
        If there is no valid 'DQ' array in image, then return
        an empty string.
    """

    # If no bitvalue is set or rootname given, assume no mask is desired
    # However, this name would be useful as the output mask from
    # other processing, such as MultiDrizzle, so return it anyway.
    #if bitvalue == None or rootname == None:
    #    return None

    # build output name
    maskname = output

    # If an old version of the maskfile was present, remove it and rebuild it.
    if fileutil.findFile(maskname):
        fileutil.removeFile(maskname)

    # Open input file with DQ array
    fdq = fileutil.openImage(rootname, mode='readonly', memmap=False)
    try:
        _extn = fileutil.findExtname(fdq, extname, extver=extver)
        if _extn is not None:
            # Read in DQ array
            dqarr = fdq[_extn].data
        else:
            dqarr = None

        # For the case where there is no DQ array,
        # create a mask image of all ones.
        if dqarr is None:
            # We need to get the dimensions of the output DQ array
            # Since the DQ array is non-existent, look for the SCI extension
            _sci_extn = fileutil.findExtname(fdq,'SCI',extver=extver)
            if _sci_extn is not None:
                _shape = fdq[_sci_extn].data.shape
                dqarr = np.zeros(_shape,dtype=np.uint16)
            else:
                raise Exception
        # Build mask array from DQ array
        maskarr = buildMask(dqarr,bitvalue)
        #Write out the mask file as simple FITS file
        fmask = fits.open(maskname, mode='append', memmap=False)
        maskhdu = fits.PrimaryHDU(data = maskarr)
        fmask.append(maskhdu)

        #Close files
        fmask.close()
        del fmask
        fdq.close()
        del fdq

    except:
        fdq.close()
        del fdq
        # Safeguard against leaving behind an incomplete file
        if fileutil.findFile(maskname):
            os.remove(maskname)
        _errstr = "\nWarning: Problem creating MASK file for "+rootname+".\n"
        #raise IOError, _errstr
        print(_errstr)
        return None

    # Return the name of the mask image written out
    return maskname

# Utility functions for generating X and Y arrays for
# creating the WFPC2 shadow masks.
# Coefficients are from WMOSAIC function 'get_edge2.x' 
Example 29
Source File: model_inceptionresnet.py    From cvpr-2018-autonomous-driving-autopilot-solution with MIT License 4 votes vote down vote up
def build_fpn_mask_graph(rois, feature_maps, image_meta,
                         pool_size, num_classes, train_bn=True):
    """Builds the computation graph of the mask head of Feature Pyramid Network.

    rois: [batch, num_rois, (y1, x1, y2, x2)] Proposal boxes in normalized
          coordinates.
    feature_maps: List of feature maps from diffent layers of the pyramid,
                  [P2, P3, P4, P5]. Each has a different resolution.
    image_meta: [batch, (meta data)] Image details. See compose_image_meta()
    pool_size: The width of the square feature map generated from ROI Pooling.
    num_classes: number of classes, which determines the depth of the results
    train_bn: Boolean. Train or freeze Batch Norm layres

    Returns: Masks [batch, roi_count, height, width, num_classes]
    """
    # ROI Pooling
    # Shape: [batch, boxes, pool_height, pool_width, channels]
    x = PyramidROIAlign([pool_size, pool_size],
                        name="roi_align_mask")([rois, image_meta] + feature_maps)

    # Conv layers
    x = KL.TimeDistributed(KL.Conv2D(256, (3, 3), padding="same"),
                           name="mrcnn_mask_conv1")(x)
    x = KL.TimeDistributed(BatchNorm(),
                           name='mrcnn_mask_bn1')(x, training=train_bn)
    x = KL.Activation('relu')(x)

    x = KL.TimeDistributed(KL.Conv2D(256, (3, 3), padding="same"),
                           name="mrcnn_mask_conv2")(x)
    x = KL.TimeDistributed(BatchNorm(),
                           name='mrcnn_mask_bn2')(x, training=train_bn)
    x = KL.Activation('relu')(x)

    x = KL.TimeDistributed(KL.Conv2D(256, (3, 3), padding="same"),
                           name="mrcnn_mask_conv3")(x)
    x = KL.TimeDistributed(BatchNorm(),
                           name='mrcnn_mask_bn3')(x, training=train_bn)
    x = KL.Activation('relu')(x)

    x = KL.TimeDistributed(KL.Conv2D(256, (3, 3), padding="same"),
                           name="mrcnn_mask_conv4")(x)
    x = KL.TimeDistributed(BatchNorm(),
                           name='mrcnn_mask_bn4')(x, training=train_bn)
    x = KL.Activation('relu')(x)

    x = KL.TimeDistributed(KL.Conv2DTranspose(256, (2, 2), strides=2, activation="relu"),
                           name="mrcnn_mask_deconv")(x)
    x = KL.TimeDistributed(KL.Conv2D(num_classes, (1, 1), strides=1, activation="sigmoid"),
                           name="mrcnn_mask")(x)
    return x


############################################################
#  Loss Functions
############################################################ 
Example 30
Source File: toolkit.py    From PICwriter with MIT License 4 votes vote down vote up
def build_mask(cell, wgt, final_layer=None, final_datatype=None):
    """ Builds the appropriate mask according to the resist specifications and fabrication type.  Does this by applying a boolean 'XOR' or 'AND' operation on the waveguide and clad masks.

        Args:
           * **cell** (gdspy.Cell):  Cell with components.  Final mask is placed in this cell.
           * **wgt** (WaveguideTemplate):  Waveguide template containing the resist information, and layers/datatypes for the waveguides and cladding.

        Keyword Args:
           * **final_layer** (int): layer to place the mask on (defaults to `wgt.clad_layer + 1`)
           * **final_datatype** (int): datatype to place the mask on (defaults to `0`)

        Returns:
           None

    """
    fl = wgt.clad_layer + 1 if final_layer == None else final_layer
    fd = 0 if final_datatype == None else final_datatype

    polygons = cell.get_polygons(by_spec=True)
    try:
        pWG = polygons[(wgt.wg_layer, wgt.wg_datatype)]
        pCLAD = polygons[(wgt.clad_layer, wgt.clad_datatype)]
    except KeyError:
        print(
            "Warning! No objects written to layer/datatype specified by WaveguideTemplate"
        )
    if wgt.resist == "+":
        cell.add(
            gdspy.fast_boolean(
                pWG,
                pCLAD,
                "xor",
                precision=0.001,
                max_points=199,
                layer=fl,
                datatype=fd,
            )
        )
    elif wgt.resist == "-":
        cell.add(
            gdspy.fast_boolean(
                pWG,
                pCLAD,
                "and",
                precision=0.001,
                max_points=199,
                layer=fl,
                datatype=fd,
            )
        ) 
Example 31
Source File: product_module.py    From pytorch_GAN_zoo with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
def buildMaskSplit(noiseGShape,
                   noiseGTexture,
                   categoryVectorDim,
                   attribKeysOrder,
                   attribShift,
                   keySplits=None,
                   mixedNoise=False):
    r"""
    Build a 8bits mask that split a full input latent vector into two
    intermediate latent vectors: one for the shape network and one for the
    texture network.
    """

    # latent vector split
    # Reminder, a latent vector is organized as follow
    # [z1,......, z_N, c_1, ....., c_C]
    # N : size of the noise part
    # C : size of the conditional part (ACGAN)

    # Here we will split the vector in
    # [y1, ..., y_N1, z1,......, z_N2, c_1, ....., c_C]

    N1 = noiseGShape
    N2 = noiseGTexture

    if not mixedNoise:
        maskShape = [1 for x in range(N1)] + [0 for x in range(N2)]
        maskTexture = [0 for x in range(N1)] + [1 for x in range(N2)]
    else:
        maskShape = [1 for x in range(N1 + N2)]
        maskTexture = [1 for x in range(N1 + N2)]

    # Now the conditional part
    # Some conditions apply to the shape, other to the texture, and sometimes
    # to both
    if attribKeysOrder is not None:

        C = categoryVectorDim

        if keySplits is not None:
            maskShape = maskShape + [0 for x in range(C)]
            maskTexture = maskTexture + [0 for x in range(C)]

            for key in keySplits["GShape"]:

                index = attribKeysOrder[key]["order"]
                shift = N1 + N2 + attribShift[index]

                for i in range(shift, shift + len(attribKeysOrder[key]["values"])):
                    maskShape[i] = 1

            for key in keySplits["GTexture"]:

                index = attribKeysOrder[key]["order"]
                shift = N1 + N2 + attribShift[index]
                for i in range(shift, shift + len(attribKeysOrder[key]["values"])):
                    maskTexture[i] = 1
        else:

            maskShape = maskShape + [1 for x in range(C)]
            maskTexture = maskTexture + [1 for x in range(C)]

    return maskShape, maskTexture 
Example 32
Source File: model.py    From dataiku-contrib with Apache License 2.0 4 votes vote down vote up
def build_fpn_mask_graph(rois, feature_maps, image_meta,
                         pool_size, num_classes, train_bn=True):
    """Builds the computation graph of the mask head of Feature Pyramid Network.
    rois: [batch, num_rois, (y1, x1, y2, x2)] Proposal boxes in normalized
          coordinates.
    feature_maps: List of feature maps from different layers of the pyramid,
                  [P2, P3, P4, P5]. Each has a different resolution.
    image_meta: [batch, (meta data)] Image details. See compose_image_meta()
    pool_size: The width of the square feature map generated from ROI Pooling.
    num_classes: number of classes, which determines the depth of the results
    train_bn: Boolean. Train or freeze Batch Norm layers
    Returns: Masks [batch, num_rois, MASK_POOL_SIZE, MASK_POOL_SIZE, NUM_CLASSES]
    """
    # ROI Pooling
    # Shape: [batch, num_rois, MASK_POOL_SIZE, MASK_POOL_SIZE, channels]
    x = PyramidROIAlign([pool_size, pool_size],
                        name="roi_align_mask")([rois, image_meta] + feature_maps)

    # Conv layers
    x = KL.TimeDistributed(KL.Conv2D(256, (3, 3), padding="same"),
                           name="mrcnn_mask_conv1")(x)
    x = KL.TimeDistributed(BatchNorm(),
                           name='mrcnn_mask_bn1')(x, training=train_bn)
    x = KL.Activation('relu')(x)

    x = KL.TimeDistributed(KL.Conv2D(256, (3, 3), padding="same"),
                           name="mrcnn_mask_conv2")(x)
    x = KL.TimeDistributed(BatchNorm(),
                           name='mrcnn_mask_bn2')(x, training=train_bn)
    x = KL.Activation('relu')(x)

    x = KL.TimeDistributed(KL.Conv2D(256, (3, 3), padding="same"),
                           name="mrcnn_mask_conv3")(x)
    x = KL.TimeDistributed(BatchNorm(),
                           name='mrcnn_mask_bn3')(x, training=train_bn)
    x = KL.Activation('relu')(x)

    x = KL.TimeDistributed(KL.Conv2D(256, (3, 3), padding="same"),
                           name="mrcnn_mask_conv4")(x)
    x = KL.TimeDistributed(BatchNorm(),
                           name='mrcnn_mask_bn4')(x, training=train_bn)
    x = KL.Activation('relu')(x)

    x = KL.TimeDistributed(KL.Conv2DTranspose(256, (2, 2), strides=2, activation="relu"),
                           name="mrcnn_mask_deconv")(x)
    x = KL.TimeDistributed(KL.Conv2D(num_classes, (1, 1), strides=1, activation="sigmoid"),
                           name="mrcnn_mask")(x)
    return x


############################################################
#  Loss Functions
############################################################ 
Example 33
Source File: model.py    From Skin-Cancer-Segmentation with MIT License 4 votes vote down vote up
def build_fpn_mask_graph(rois, feature_maps, image_meta,
                         pool_size, num_classes, train_bn=True):
    """Builds the computation graph of the mask head of Feature Pyramid Network.

    rois: [batch, num_rois, (y1, x1, y2, x2)] Proposal boxes in normalized
          coordinates.
    feature_maps: List of feature maps from different layers of the pyramid,
                  [P2, P3, P4, P5]. Each has a different resolution.
    image_meta: [batch, (meta data)] Image details. See compose_image_meta()
    pool_size: The width of the square feature map generated from ROI Pooling.
    num_classes: number of classes, which determines the depth of the results
    train_bn: Boolean. Train or freeze Batch Norm layers

    Returns: Masks [batch, roi_count, height, width, num_classes]
    """
    # ROI Pooling
    # Shape: [batch, boxes, pool_height, pool_width, channels]
    x = PyramidROIAlign([pool_size, pool_size],
                        name="roi_align_mask")([rois, image_meta] + feature_maps)

    # Conv layers
    x = KL.TimeDistributed(KL.Conv2D(256, (3, 3), padding="same"),
                           name="mrcnn_mask_conv1")(x)
    x = KL.TimeDistributed(BatchNorm(),
                           name='mrcnn_mask_bn1')(x, training=train_bn)
    x = KL.Activation('relu')(x)

    x = KL.TimeDistributed(KL.Conv2D(256, (3, 3), padding="same"),
                           name="mrcnn_mask_conv2")(x)
    x = KL.TimeDistributed(BatchNorm(),
                           name='mrcnn_mask_bn2')(x, training=train_bn)
    x = KL.Activation('relu')(x)

    x = KL.TimeDistributed(KL.Conv2D(256, (3, 3), padding="same"),
                           name="mrcnn_mask_conv3")(x)
    x = KL.TimeDistributed(BatchNorm(),
                           name='mrcnn_mask_bn3')(x, training=train_bn)
    x = KL.Activation('relu')(x)

    x = KL.TimeDistributed(KL.Conv2D(256, (3, 3), padding="same"),
                           name="mrcnn_mask_conv4")(x)
    x = KL.TimeDistributed(BatchNorm(),
                           name='mrcnn_mask_bn4')(x, training=train_bn)
    x = KL.Activation('relu')(x)

    x = KL.TimeDistributed(KL.Conv2DTranspose(256, (2, 2), strides=2, activation="relu"),
                           name="mrcnn_mask_deconv")(x)
    x = KL.TimeDistributed(KL.Conv2D(num_classes, (1, 1), strides=1, activation="sigmoid"),
                           name="mrcnn_mask")(x)
    return x


############################################################
#  Loss Functions
############################################################ 
Example 34
Source File: citypersons2.py    From Detectron-PYTORCH with Apache License 2.0 4 votes vote down vote up
def build_occlusion_mask(self, gt_boxes, gt_classes, iw, ih):
        n = gt_boxes.shape[0]
        mask = np.zeros([ih, iw], dtype=np.int32)

        for i in range(n):
            box = gt_boxes[i].astype(np.int32)
            cls = gt_classes[i]
            if cls <= 0: continue
            x1, y1 = max(box[0], 0), max(box[1], 0)
            x2, y2 = min(box[2], iw), min(box[3], ih)
            mask[y1:y2, x1:x2] = 1

        for i in range(n):
            box = gt_boxes[i].astype(np.int32)
            cls = gt_classes[i]
            if cls <= 0: continue
            x1, y1 = max(box[0], 0), max(box[1], 0)
            x2, y2 = min(box[2], iw), min(box[3], ih)
            # mask[y1:y2, x1:x2] = 1

            for j in range(i+1, n):
                box2 = gt_boxes[j].astype(np.int32)
                cls2 = gt_classes[j]
                if cls2 <= 0: continue
                x1, y1 = max(box2[0], 0), max(box2[1], 0)
                x2, y2 = min(box2[2], iw), min(box2[3], ih)
                # mask[y1:y2, x1:x2] = 1

                x1, y1 = max(box[0], box2[0]), max(box[1], box2[1])
                x2, y2 = min(box[2], box2[2]), min(box[3], box2[3])
                x1, y1 = max(x1, 0), max(y1, 0)
                x2, y2 = min(x2, iw), min(y2, ih)
                if x2 > x1 and y2 > y1:
                    mask[y1:y2, x1:x2] = 2

        for i in range(n):
            box = gt_boxes[i].astype(np.int32)
            cls = gt_classes[i]
            if cls > 0: continue
            x1, y1 = max(box[0], 0), max(box[1], 0)
            x2, y2 = min(box[2], iw), min(box[3], ih)
            mask[y1:y2, x1:x2] = -1

        return mask 
Example 35
Source File: model.py    From Mask-RCNN-Pedestrian-Detection with MIT License 4 votes vote down vote up
def build_fpn_mask_graph(rois, feature_maps, image_shape,
                         pool_size, num_classes, train_bn=True):
    """Builds the computation graph of the mask head of Feature Pyramid Network.

    rois: [batch, num_rois, (y1, x1, y2, x2)] Proposal boxes in normalized
          coordinates.
    feature_maps: List of feature maps from diffent layers of the pyramid,
                  [P2, P3, P4, P5]. Each has a different resolution.
    image_shape: [height, width, depth]
    pool_size: The width of the square feature map generated from ROI Pooling.
    num_classes: number of classes, which determines the depth of the results
    train_bn: Boolean. Train or freeze Batch Norm layres

    Returns: Masks [batch, roi_count, height, width, num_classes]
    """
    # ROI Pooling
    # Shape: [batch, boxes, pool_height, pool_width, channels]
    x = PyramidROIAlign([pool_size, pool_size], image_shape,
                        name="roi_align_mask")([rois] + feature_maps)

    # Conv layers
    x = KL.TimeDistributed(KL.Conv2D(256, (3, 3), padding="same"),
                           name="mrcnn_mask_conv1")(x)
    x = KL.TimeDistributed(BatchNorm(),
                           name='mrcnn_mask_bn1')(x, training=train_bn)
    x = KL.Activation('relu')(x)

    x = KL.TimeDistributed(KL.Conv2D(256, (3, 3), padding="same"),
                           name="mrcnn_mask_conv2")(x)
    x = KL.TimeDistributed(BatchNorm(),
                           name='mrcnn_mask_bn2')(x, training=train_bn)
    x = KL.Activation('relu')(x)

    x = KL.TimeDistributed(KL.Conv2D(256, (3, 3), padding="same"),
                           name="mrcnn_mask_conv3")(x)
    x = KL.TimeDistributed(BatchNorm(),
                           name='mrcnn_mask_bn3')(x, training=train_bn)
    x = KL.Activation('relu')(x)

    x = KL.TimeDistributed(KL.Conv2D(256, (3, 3), padding="same"),
                           name="mrcnn_mask_conv4")(x)
    x = KL.TimeDistributed(BatchNorm(),
                           name='mrcnn_mask_bn4')(x, training=train_bn)
    x = KL.Activation('relu')(x)

    x = KL.TimeDistributed(KL.Conv2DTranspose(256, (2, 2), strides=2, activation="relu"),
                           name="mrcnn_mask_deconv")(x)
    x = KL.TimeDistributed(KL.Conv2D(num_classes, (1, 1), strides=1, activation="sigmoid"),
                           name="mrcnn_mask")(x)
    return x


############################################################
#  Loss Functions
############################################################