How to use cntk - 10 common examples

To help you get started, we’ve selected a few cntk examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github microsoft / CNTK / Examples / Image / Generative / PixelCNN / pixelcnn / nn.py View on Github external
def masked_conv2d(x, num_filters, filter_shape=(3,3), strides=(1,1), pad=True, nonlinearity=None, mask_type=None, h=None, bias=True, init=ct.glorot_uniform()):
    ''' Convolution layer with mask and conditional input support. '''
    output_channels_shape = _as_tuple(num_filters)
    x_channels_shape      = _as_tuple(x.shape[0])
    paddings              = (False,) + (pad,)*len(filter_shape)

    W = ct.parameter((num_filters, x.shape[0]) + filter_shape, init=init, name='W')

    if mask_type is not None:
        filter_center = (filter_shape[0] // 2, filter_shape[1] // 2)

        mask = np.ones(W.shape, dtype=np.float32)
        mask[:,:,filter_center[0]:,filter_center[1]+1:] = 0
        mask[:,:,filter_center[0]+1:,:] = 0.

        if mask_type == 'a':
            mask[:,:,filter_center[0],filter_center[1]] = 0

        W = ct.element_times(W, ct.constant(mask))

    if bias:
        b = ct.parameter((num_filters, 1, 1), name='b')        
        x = ct.convolution(W, x, strides=x_channels_shape + strides, auto_padding=paddings) + b
github microsoft / CNTK / Examples / Image / Detection / utils / unit_tests.py View on Github external
num_gt_boxes = 50
    gt_boxes_shape_cntk = (num_gt_boxes,5)
    im_info = [1000, 1000, 1]

    # Create input tensors with values
    rpn_cls_score_dummy = np.random.random_sample(rpn_cls_score_shape_cntk).astype(np.float32)

    x1y1 = np.random.random_sample((num_gt_boxes, 2)) * 500
    wh = np.random.random_sample((num_gt_boxes, 2)) * 400
    x2y2 = x1y1 + wh + 50
    label = np.random.random_sample((num_gt_boxes, 1))
    label = (label * 17.0)
    gt_boxes = np.hstack((x1y1, x2y2, label)).astype(np.float32)

    # Create CNTK layer and call forward
    rpn_cls_score_var = input_variable(rpn_cls_score_shape_cntk)
    gt_boxes_var = input_variable(gt_boxes_shape_cntk)

    cntk_layer = user_function(CntkAnchorTargetLayer(rpn_cls_score_var, gt_boxes_var, cntk.constant(im_info, (3,)), deterministic=True))
    state, cntk_output = cntk_layer.forward({rpn_cls_score_var: [rpn_cls_score_dummy], gt_boxes_var: [gt_boxes]})

    obj_key = [k for k in cntk_output if 'objectness_target' in str(k)][0]
    bbt_key = [k for k in cntk_output if 'rpn_bbox_target' in str(k)][0]
    bbw_key = [k for k in cntk_output if 'rpn_bbox_inside_w' in str(k)][0]

    cntk_objectness_target = cntk_output[obj_key][0]
    cntk_bbox_targets = cntk_output[bbt_key][0]
    cntk_bbox_inside_w = cntk_output[bbw_key][0]

    # Create Caffe layer and call forward
    bottom = [np.array(rpn_cls_score_dummy),np.array(gt_boxes), np.array(im_info)]
    top = None # handled through return statement in caffe layer for unit testing
github microsoft / CNTK / Examples / Image / GettingStarted / 07_Deconvolution_Visualizer.py View on Github external
enc_node_name = "pooling_node"
        input_node_name = "input_node"
        output_node_name = "output_node"

    # define location of output, model and data and check existence
    output_path = os.path.join(abs_path, "Output")
    model_file = os.path.join(model_path, model_file_name)
    data_file = os.path.join(data_path, "Test-28x28_cntk_text.txt")
    if not (os.path.exists(model_file) and os.path.exists(data_file)):
        print("Cannot find required data or model. "
              "Please get the MNIST data set and run 'cntk configFile=07_Deconvolution_BS.cntk' or 'python 07_Deconvolution_PY.py' to create the model.")
        exit(0)

    # create minibatch source
    minibatch_source = MinibatchSource(CTFDeserializer(data_file, StreamDefs(
        features  = StreamDef(field='features', shape=(28*28)),
        labels    = StreamDef(field='labels',   shape=10)
    )), randomize=False, max_sweeps = 1)

    # use this to print all node names in the model
    # print_all_node_names(model_file, use_brain_script_model)

    # load model and pick desired nodes as output
    loaded_model = load_model(model_file)
    output_nodes = combine(
        [loaded_model.find_by_name(input_node_name).owner,
         loaded_model.find_by_name(enc_node_name).owner,
         loaded_model.find_by_name(output_node_name).owner])

    # evaluate model save output
    features_si = minibatch_source['features']
    with open(os.path.join(output_path, decoder_output_file_name), 'wb') as decoder_text_file:
github microsoft / CNTK / Examples / Text / BidirectionalAttentionFlow / polymath_crosstalk.py View on Github external
def embed(self):
        # load glove
        npglove = np.zeros((self.wg_dim, self.hidden_dim), dtype=np.float32)
        with open(os.path.join(self.abs_path, 'glove.6B.100d.txt'), encoding='utf-8') as f:
            for line in f:
                parts = line.split()
                word = parts[0].lower()
                if word in self.vocab:
                    npglove[self.vocab[word],:] = np.asarray([float(p) for p in parts[1:]])
        glove = C.constant(npglove)
        nonglove = C.parameter(shape=(len(self.vocab) - self.wg_dim, self.hidden_dim), init=C.glorot_uniform(), name='TrainableE')
        
        def func(wg, wn):
            return C.times(wg, glove) + C.times(wn, nonglove)
        return func
github microsoft / CNTK / Examples / Extensibility / BinaryConvolution / binary_convnet.py View on Github external
pad=False,
                      strides=1,
                      bias=True,
                      init_bias=0,
                      op_name='BinaryConvolution', name=''):
    """ arguments:
            operand: tensor to convolve
            filter_shape: tuple indicating filter size
            num_filters: number of filters to use 
            channels: number of incoming channels
            init: type of initialization to use for weights
    """
    kernel_shape = (num_filters, channels) + filter_shape
    W = C.parameter(shape=kernel_shape, init=init, name="filter")

    binary_convolve_operand_p = C.placeholder(operand.shape, operand.dynamic_axes, name="operand")
    binary_convolve = C.convolution(CustomMultibit(W, 1), CustomMultibit(binary_convolve_operand_p, 1), auto_padding=[False, pad, pad], strides=[strides])
    r = C.as_block(binary_convolve, [(binary_convolve_operand_p, operand)], 'binary_convolve')

    bias_shape = (num_filters, 1, 1)
    b = C.parameter(shape=bias_shape, init=init_bias, name="bias")
    r = r + b

    # apply learnable param relu
    P = C.parameter(shape=r.shape, init=init, name="prelu")
    r = C.param_relu(P, r)
    return r
github microsoft / CNTK / contrib / Python / cntk / examples / MNIST / mnist_one_layer.py View on Github external
labels = C.input(label_dim)
    labels.tag = 'label'
    labels.name = 'labels'

    traning_reader = C.CNTKTextFormatReader(training_filename)
    test_reader = C.CNTKTextFormatReader(test_filename)

    h1 = add_dnn_sigmoid_layer(feat_dim, hidden_dim, feats_scaled, 1)
    out = add_dnn_layer(hidden_dim, label_dim, h1, 1)
    out.tag = 'output'

    ec = C.cross_entropy_with_softmax(labels, out)
    ec.name = criterion_name
    ec.tag = 'criterion'
    
    eval = C.ops.square_error(labels, out)
    eval.name = eval_name
    eval.tag = 'eval'
    
    # Specify the training parameters (settings are scaled down)
    my_sgd = C.SGDParams(epoch_size=600, minibatch_size=32,
                       learning_rates_per_mb=0.1, max_epochs=5, momentum_per_mb=0)

    # Create a context or re-use if already there
    with C.LocalExecutionContext('mnist_one_layer', clean_up=True) as ctx:
        # CNTK actions
         ctx.train(
            root_nodes=[ec, eval],
            training_params=my_sgd,
            input_map=traning_reader.map(labels, alias='labels', dim=label_dim).map(features, alias='features', dim=feat_dim))
            
         result = ctx.test(
github microsoft / CNTK / Examples / Image / Detection / FastRCNN / A2_RunWithPyModel.py View on Github external
(map_file, roi_file, label_file))

    # read images
    transforms = [scale(width=img_width, height=img_height, channels=img_channels,
                        scale_mode="pad", pad_value=114, interpolations='linear')]

    image_source = ImageDeserializer(map_file, StreamDefs(
        features = StreamDef(field='image', transforms=transforms)))

    # read rois and labels
    roi_source = CTFDeserializer(roi_file, StreamDefs(
        rois = StreamDef(field=roi_stream_name, shape=rois_dim, is_sparse=False)))
    label_source = CTFDeserializer(label_file, StreamDefs(
        roiLabels = StreamDef(field=label_stream_name, shape=label_dim, is_sparse=False)))
    gt_source = CTFDeserializer(gt_file, StreamDefs(
        gts = StreamDef(field=gt_stream_name, shape=gt_dim)))

    # define a composite reader
    return MinibatchSource([image_source, roi_source, label_source, gt_source], max_samples=sys.maxsize, randomize=data_set == "train", trace_level=TraceLevel.Error,)
github microsoft / CNTK / Examples / Image / Generative / PixelCNN / train.py View on Github external
def create_reader(map_file, is_training, randomize=False):
    if not os.path.exists(map_file):
        raise RuntimeError("File '%s' does not exist. Please run install_cifar10.py from DataSets/CIFAR-10 to fetch them" %
                           (map_file))

    transforms = []
    if is_training:
        transforms += [
            xforms.crop(crop_type='center', crop_size=32)
        ]
    transforms += [
        xforms.scale(width=image_width, height=image_height, channels=num_channels, interpolations='linear')
    ]
    # deserializer
    return ct.io.MinibatchSource(ct.io.ImageDeserializer(map_file, ct.io.StreamDefs(
        features = ct.io.StreamDef(field='image', transforms=transforms), # first column in map file is referred to as 'image'
        labels   = ct.io.StreamDef(field='label', shape=num_classes))),   # and second as 'label'
        randomize=randomize)
github microsoft / CNTK / bindings / python / doc / simplernn.py View on Github external
def create_reader(path, is_training, input_dim, label_dim):
    return MinibatchSource(CTFDeserializer(path, StreamDefs(
        features=StreamDef(field='x', shape=input_dim, is_sparse=True),
        labels=StreamDef(field='y', shape=label_dim, is_sparse=False)
        )), randomize=is_training,
        max_sweeps=INFINITELY_REPEAT if is_training else 1)
github microsoft / CNTK / bindings / python / examples / Tensorboard / SimpleMNIST_tensorboard.py View on Github external
def create_reader(path, is_training, input_dim, label_dim):
    return MinibatchSource(CTFDeserializer(path, StreamDefs(
        features  = StreamDef(field='features', shape=input_dim, is_sparse=False),
        labels    = StreamDef(field='labels',   shape=label_dim, is_sparse=False)
    )), randomize=is_training, epoch_size = INFINITELY_REPEAT if is_training else FULL_DATA_SWEEP)