Beispiel #1
0
def bbox_overlaps(anchors: mx.nd.NDArray, gt: mx.nd.NDArray):
    """
    Get IoU of the anchors and ground truth bounding boxes.
    The shape of anchors and gt should be (N, 4) and (M, 4)
    So the shape of return value is (N, M)
    """
    ret = []
    for i in range(gt.shape[0]):
        cgt = gt[i].reshape((1, 4)).broadcast_to(anchors.shape)
        # inter
        x0 = nd.max(nd.stack(anchors[:, 0], cgt[:, 0]), axis=0)
        y0 = nd.max(nd.stack(anchors[:, 1], cgt[:, 1]), axis=0)
        x1 = nd.min(nd.stack(anchors[:, 2], cgt[:, 2]), axis=0)
        y1 = nd.min(nd.stack(anchors[:, 3], cgt[:, 3]), axis=0)

        inter = _get_area(
            nd.concatenate([
                x0.reshape((-1, 1)),
                y0.reshape((-1, 1)),
                x1.reshape((-1, 1)),
                y1.reshape((-1, 1))
            ],
                           axis=1))
        outer = _get_area(anchors) + _get_area(cgt) - inter
        iou = inter / outer
        ret.append(iou.reshape((-1, 1)))
    ret = nd.concatenate(ret, axis=1)
    return ret
Beispiel #2
0
    def validate(self):
        """Perform validation"""
        l = []
        input_list, pred_list, wp_list = [], [], []

        for i, (A_rp, wp) in enumerate(self.val_iter):
            # Inputs to GPUs (or CPUs)
            self.set_inputs(A_rp_val=A_rp, wp_val=wp)
            pred = [self.netG(A_rp_val) for A_rp_val in self.A_rp_val]
            # Split segmentation and regression outputs if multitask learning is used
            pred = nd.concatenate(pred)

            # merge data across all used GPUs
            self.A_rp_val, self.wp_val = [
                nd.concatenate(list(x)) for x in [
                    self.A_rp_val,
                    self.wp_val]
            ]

            input_list.append(self.A_rp_val.asnumpy())
            pred_list.append(pred.asnumpy())
            wp_list.append(self.wp_val.asnumpy())

            l.append(self.seg_val(pred, self.wp_val).asnumpy())

        self.running_loss_seg_val = np.concatenate([*l]).mean()

        return input_list, pred_list, wp_list
def evaluate(net,test_data,ctx):
    feature_true_list=[]
    feature_label_list=[]
    p_feature_list=[]
    p_label_list=[]
    for i,(data,label) in enumerate(test_data):
        data=data.as_in_context(ctx)
        label=label.as_in_context(ctx)
        if i<67:
            feature_true=net(data)
            feature_true_list.append(feature_true)
            feature_label=label
            feature_label_list.append(feature_label)
        elif i>=67 and i<134:
            p_data=data
            p_feature = net(p_data)
            p_feature_list.append(p_feature)
            p_label=label
            p_label_list.append(p_label)
        else:
            break
    feature_true=nd.concatenate(feature_true_list,0)
    feature_label=nd.concatenate(feature_label_list,0)
    p_feature=nd.concatenate(p_feature_list,0)
    p_label=nd.concatenate(p_label_list,0).reshape(-1)
    acc=accuracy_metric(feature_true,feature_label,p_feature,p_label)
    return acc
def preEventsDataset(fs, T, C):
    deltat = T / 2  #1#2.5#3#5

    onesecslice = [(65232, 69327), (65178, 69273), (66142, 70237),
                   (66134, 70229), (65902, 69997), (65928, 70023),
                   (65281, 69376), (65294, 69389)]
    llLIGOevents = [
        file for file in os.listdir('Data_LIGO_Totural') if 'strain' in file
    ]
    llLIGOevents.sort()
    aroundEvents = np.concatenate([np.load('./Data_LIGO_Totural/'+file).reshape(1,-1)[:,onesecslice[index][0]-int((deltat-0.5)*fs):onesecslice[index][1]+int((deltat-0.5)*fs)+1] \
                                    for index, file in enumerate(llLIGOevents)])
    logger.debug('Loaded aroundEvents: {}', aroundEvents.shape)  # (8, T*fs)
    logger.debug('Loaded list of Events: \n{}', np.array(llLIGOevents))

    aroundEvents = nd.array(aroundEvents).expand_dims(1)

    if C == 1:
        aroundEvent_psd_block = nd.concatenate(
            [EquapEvent(fs, data) for data in aroundEvents], axis=0)
    elif C == 2:
        aroundEvent_psd_block_H1 = nd.concatenate(
            [EquapEvent(fs, data) for data in aroundEvents[::2]],
            axis=0)  # (4, 2, 1, 102400)
        aroundEvent_psd_block_L1 = nd.concatenate(
            [EquapEvent(fs, data) for data in aroundEvents[1::2]],
            axis=0)  # (4, 2, 1, 102400)
        aroundEvent_psd_block = nd.concat(
            aroundEvent_psd_block_H1.swapaxes(1, 0).expand_dims(2),
            aroundEvent_psd_block_L1.swapaxes(1, 0).expand_dims(2),
            dim=2).swapaxes(1, 0)
    logger.debug('aroundEvent_psd_block: {}', aroundEvent_psd_block.shape)
    return aroundEvent_psd_block
Beispiel #5
0
def predict_all(X, net, ctx, dfeat, batch_size=64, cnn_flag=False):
    '''
    :param X: an ndarray containing the data. The first axis is over examples
    :param net: trained model
    :param dfeat: the dimensionality of the vectorized feature
    :param batchsize: batchsize used in iterators. default is 64.
    :return: Two ndarrays containing the soft and hard predictions of the classifier.
    '''

    data_iterator = mx.io.NDArrayIter(X, None, batch_size, shuffle=False)
    ypred_soft = []
    ypred = []

    for i, batch in enumerate(data_iterator):
        if cnn_flag:
            data = batch.data[0].as_in_context(ctx)
        else:
            data = batch.data[0].as_in_context(ctx).reshape((-1, dfeat))
        output = net(data)
        softpredictions = nd.softmax(output, axis=1)
        predictions = nd.argmax(output, axis=1)
        ypred_soft.append(softpredictions)
        ypred.append(predictions)

    ypred_soft_all = nd.concatenate(ypred_soft, axis=0)
    ypred_all = nd.concatenate(ypred, axis=0)

    # iterator automatically pads the last minibatch, so the length of the vectors might be different.
    ypred_all = ypred_all[:X.shape[0]]
    ypred_soft_all = ypred_soft_all[:X.shape[0], ]

    return ypred_all, ypred_soft_all
Beispiel #6
0
def validate(nfolds=10):
    metric = FaceVerification(nfolds)
    metric_flip = FaceVerification(nfolds)
    for loader, name in zip(val_datas, targets.split(",")):
        metric.reset()
        for i, batch in enumerate(loader):
            data0s = gluon.utils.split_and_load(batch[0][0][0],
                                                ctx,
                                                even_split=False)
            data1s = gluon.utils.split_and_load(batch[0][1][0],
                                                ctx,
                                                even_split=False)
            data0s_flip = gluon.utils.split_and_load(batch[0][0][1],
                                                     ctx,
                                                     even_split=False)
            data1s_flip = gluon.utils.split_and_load(batch[0][1][1],
                                                     ctx,
                                                     even_split=False)
            issame_list = gluon.utils.split_and_load(batch[1],
                                                     ctx,
                                                     even_split=False)

            embedding0s = [test_net(X) for X in data0s]
            embedding1s = [test_net(X) for X in data1s]
            embedding0s_flip = [test_net(X) for X in data0s_flip]
            embedding1s_flip = [test_net(X) for X in data1s_flip]

            emb0s = [
                nd.L2Normalization(e, mode='instance') for e in embedding0s
            ]
            emb1s = [
                nd.L2Normalization(e, mode='instance') for e in embedding1s
            ]
            for embedding0, embedding1, issame in zip(emb0s, emb1s,
                                                      issame_list):
                metric.update(issame, embedding0, embedding1)

            emb0s_flip = [
                nd.L2Normalization(nd.concatenate([e, ef], 1), mode='instance')
                for e, ef in zip(embedding0s, embedding0s_flip)
            ]
            emb1s_flip = [
                nd.L2Normalization(nd.concatenate([e, ef], 1), mode='instance')
                for e, ef in zip(embedding1s, embedding1s_flip)
            ]
            for embedding0, embedding1, issame in zip(emb0s_flip, emb1s_flip,
                                                      issame_list):
                metric_flip.update(issame, embedding0, embedding1)

        tpr, fpr, accuracy, val, val_std, far, accuracy_std = metric.get()
        print("{}: \t{:.6f}+-{:.6f}".format(name, accuracy, accuracy_std))
        _, _, accuracy, _, _, _, accuracy_std = metric_flip.get()
        print("{}-flip: {:.6f}+-{:.6f}".format(name, accuracy, accuracy_std))
Beispiel #7
0
def test(ctx):
    """Test a model."""
    val_data.reset()
    outputs = []
    labels = []
    for batch in val_data:
        data = gluon.utils.split_and_load(batch.data[0], ctx_list=ctx, batch_axis=0)
        label = gluon.utils.split_and_load(batch.label[0], ctx_list=ctx, batch_axis=0)
        for x in data:
            outputs.append(net(x)[-1])
        labels += label

    outputs = nd.concatenate(outputs, axis=0)[:val_data.n_test]
    labels = nd.concatenate(labels, axis=0)[:val_data.n_test]
    return evaluate_emb(outputs, labels)
Beispiel #8
0
def load_data(data_path, batch_size, reverse=False):
    img_in_list, img_out_list = [], []
    for path, _, files in os.walk(data_path):
        for file in files:
            if not file[-4:] in ['.jpg']:
                continue
            img_arr = mx.image.imread(join(path, file)).astype(np.float32)/127.5 - 1
            img_in, img_out = preprocess_single_img(img_arr)
            if not reverse:
                img_in_list.append(img_in)
                img_out_list.append(img_out)
            else:
                img_in_list.append(img_out)
                img_out_list.append(img_in)
    return mx.io.NDArrayIter(data = [nd.concatenate(img_in_list), nd.concatenate(img_out_list)], batch_size=batch_size)
Beispiel #9
0
def bbox_overlaps(anchors: mx.nd.NDArray, gt: mx.nd.NDArray):
    """
    Get IoU of the anchors and ground truth bounding boxes.
    The shape of anchors and gt should be (N, 4) and (M, 4)
    So the shape of return value is (N, M)
    """
    N, M = anchors.shape[0], gt.shape[0]
    anchors_mat = anchors.reshape((N, 1, 4)).broadcast_to((N, M, 4)).reshape(
        (-1, 4))
    gt_mat = gt.reshape((1, M, 4)).broadcast_to((N, M, 4)).reshape((-1, 4))
    # inter
    x0 = nd.max(nd.stack(anchors_mat[:, 0], gt_mat[:, 0]), axis=0)
    y0 = nd.max(nd.stack(anchors_mat[:, 1], gt_mat[:, 1]), axis=0)
    x1 = nd.min(nd.stack(anchors_mat[:, 2], gt_mat[:, 2]), axis=0)
    y1 = nd.min(nd.stack(anchors_mat[:, 3], gt_mat[:, 3]), axis=0)

    inter = _get_area(
        nd.concatenate([
            x0.reshape((-1, 1)),
            y0.reshape((-1, 1)),
            x1.reshape((-1, 1)),
            y1.reshape((-1, 1))
        ],
                       axis=1))
    outer = _get_area(anchors_mat) + _get_area(gt_mat) - inter
    iou = inter / outer
    iou = iou.reshape((N, M))
    return iou
Beispiel #10
0
def extract_feature():
    """
    extract data feature vector and save
    :param model:
    :param dataloader:
    :return:
    """
    global net
    deepfashion_csv = 'checkpoints/deepfashion.csv' # write vector to this file
    net.initialize()
    net.collect_params().reset_ctx(context)
    net.load_parameters(opt.load_model_path,ctx=context)
    import csv
    f = open(deepfashion_csv,'w')
    writer = csv.writer(f,dialect='excel')

    for i,batch in tqdm(enumerate(val_dataloader)):
        batch_size = batch[0].shape[0]
        data = gluon.utils.split_and_load(batch[0], ctx_list=context, batch_axis=0)
        label = gluon.utils.split_and_load(batch[1], ctx_list=context, batch_axis=0)
        # after split data is list of two data batch
        small_batch_feature = []
        for x in data:
            feature = net.extract(x)
            small_batch_feature.append(feature)
        image_id = np.arange(i*batch_size,(i+1)*batch_size).reshape(-1,1) # prepare the image_id
        vector = nd.concatenate(small_batch_feature,axis=0).asnumpy() # concatenate the feature
        label = np.array([x.asnumpy() for x in label]).reshape(-1,1)
        result = np.hstack((image_id,label,vector))
        writer.writerows(result)
    print("finished extract feature")
    f.close()
    return "True finished"
Beispiel #11
0
def test(ctx):
    """Test a model."""
    val_data.reset()
    outputs = []
    labels = []
    for batch in val_data:
        data = gluon.utils.split_and_load(batch.data[0],
                                          ctx_list=ctx,
                                          batch_axis=0)
        label = gluon.utils.split_and_load(batch.label[0],
                                           ctx_list=ctx,
                                           batch_axis=0)
        for x in data:
            outputs.append(net(x)[-1])
        labels += label

    outputs = nd.concatenate(outputs, axis=0)[:val_data.n_test]
    labels = nd.concatenate(labels, axis=0)[:val_data.n_test]
    return evaluate_emb(outputs, labels)
Beispiel #12
0
    def get_test_batch(self):
        """Sample a testing batch (data and label)."""

        batch_size = self.batch_size
        batch = [self.get_image(self.test_image_files[(self.test_count*batch_size + i)
                                                      % len(self.test_image_files)],
                                is_train=False) for i in range(batch_size)]
        labels = [self.test_labels[(self.test_count*batch_size + i)
                                   % len(self.test_image_files)] for i in range(batch_size)]
        return nd.concatenate(batch, axis=0), labels
Beispiel #13
0
def test(ctx):
    """Test a model."""
    if opt.use_viz:
        viz.log("begin to valid")

    outputs = []
    labels = []
    for i,batch in enumerate(val_dataloader):
        data = gluon.utils.split_and_load(batch[0], ctx_list=ctx, batch_axis=0)
        label = gluon.utils.split_and_load(batch[1], ctx_list=ctx, batch_axis=0)
        # after split data is list of two data batch
        for x in data:
            outputs.append(net(x)[-1])
        labels +=label
        if (i+1)%(opt.log_interval*2) ==0:
            viz.log("valid iter {0}".format(i))
    outputs = nd.concatenate(outputs, axis=0)
    labels = nd.concatenate(labels, axis=0)
    viz.log("begin to eval embedding search")
    return evaluate_emb(outputs, labels)
Beispiel #14
0
    def evaluate_net(self, base_net, val_data):
        triplet_loss = gluon.loss.TripletLoss(margin=0)
        rate = 0.0
        sum_correct, sum_all = 0, 0

        for i, batch in enumerate(val_data):
            data, labels = batch[0], batch[1].astype('float32')

            data = split_and_load(data,
                                  ctx_list=self.ctx,
                                  batch_axis=0,
                                  even_split=False)  # 多GPU
            labels = split_and_load(labels,
                                    ctx_list=self.ctx,
                                    batch_axis=0,
                                    even_split=False)
            for X in data:
                anchor_ins, pos_ins, neg_ins = [], [], []
                for b_X in X:
                    anchor_ins.append(nd.expand_dims(b_X[0], axis=0))
                    pos_ins.append(nd.expand_dims(b_X[1], axis=0))
                    neg_ins.append(nd.expand_dims(b_X[2], axis=0))

                anchor_ins = nd.concatenate(anchor_ins, axis=0)
                pos_ins = nd.concatenate(pos_ins, axis=0)
                neg_ins = nd.concatenate(neg_ins, axis=0)

                inter1 = base_net(anchor_ins)
                inter2 = base_net(pos_ins)
                inter3 = base_net(neg_ins)
                loss = triplet_loss(inter1, inter2, inter3)  # TripletLoss
                n_correct = np.sum(np.where(loss == 0, 1, 0))
                sum_all += loss.shape[0]
                sum_correct += n_correct
            rate = safe_div(sum_correct, sum_all)
            self.print_info('验证Batch: {}, 准确率: {:.4f} ({} / {})'.format(
                i, rate, sum_correct, sum_all))
        rate = safe_div(sum_correct, sum_all)
        self.print_info('验证准确率: %.4f (%s / %s)' % (rate, sum_correct, sum_all))
        return rate
    def __call__(self, src):
        """Augmenter body"""
        assert src.shape[-2:] == (self.C, self.N)  # (nsample, C, N)
        if self.ori_peak is None:
            self.ori_peak = int(
                src.argmax(axis=2)[0, 0].asscalar())  # first+H1 as bench
            logger.debug('self.ori_peak: {}', self.ori_peak)

        # myrelu = lambda x: x if (x>0) and (x<=self.ori_peak*2) else None
        # (nsample, C, 2*(N-margin))
        # full = nd.concatenate([src, nd.zeros(shape=src.shape[:2]+(self.ori_peak*2-self.N,))], axis=2)[:,:,myrelu(self.ori_peak-(self.N-self.margin)):myrelu(self.ori_peak+(self.N-self.margin))]
        full = nd.concat(src,
                         nd.zeros(shape=src.shape[:2] +
                                  (self.ori_peak - self.margin, )),
                         dim=2)[:, :, self.ori_peak - (self.N - self.margin):]
        assert (nd.sum(full[:, :1].argmax(-1) / full[:, :1].shape[-1]) /
                full[:, :1].shape[0]).asscalar() == 0.5

        if self.margin == (self.T * self.fs) // 2:
            return full

        if self.rand_jitter:  # for every sample
            """
            RP = RandomPeakAug(margin=0.1, fs = fs, C = 2, ori_peak=0.9, rand_jitter=0)
            %timeit _ = RP(dataset_GW[pre])
            # 505 ms ± 30.9 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
            """
            randlist = [(i, i + fs) for i in np.random.randint(
                low=1, high=(fs - 2 * self.margin), size=full.shape[0])
                        if i + fs <= full.shape[-1]]
            assert len(randlist) == full.shape[0]
            return nd.concatenate([
                sample.expand_dims(axis=0)[:, :, i:j]
                for sample, (i, j) in zip(full, randlist)
            ],
                                  axis=0)  # (nsample, C, N)

            # full = nd.concatenate([self.shape_aug(sample.swapaxes(0,1).expand_dims(axis=0)) for sample in full ], axis=0) # (nsample, N, C)
            # return full.swapaxes(1,2) # (nsample, C, N)
        else:
            """
            RP = RandomPeakAug(margin=0.1, fs = fs, C = 2, ori_peak=0.9, rand_jitter=1)
            %timeit _ = RP(dataset_GW[pre])
            # 808 µs ± 37.7 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
            """
            full = full.swapaxes(0, 2).expand_dims(
                axis=0)  # (1, 2*(N-margin), C, nsample)
            return self.shape_aug(full.reshape(1, 0, -3)).reshape(
                1, 0, self.C, -1
            ).swapaxes(
                1, 3
            )[0]  # where swapaxes from (1, 2*(N-margin), C, nsample) to (nsample, C, N)
Beispiel #16
0
 def update_running_loss(self, first_iter=False, num_batch=None):
     """Compute running loss"""
     if num_batch is None:
         if first_iter:
             loss_fields = [field for field in self.__dict__.keys() if ('loss' in field) or ('err' in field)]
             self.running_loss_fields = ['running_' + field for field in loss_fields]
             [self.__setattr__(field, 0.) for field in self.running_loss_fields]
         for loss_field in self.running_loss_fields:
             _loss = nd.concatenate(list(self.__getattribute__(loss_field.replace('running_', ''))))
             self.__setattr__(loss_field, (self.__getattribute__(loss_field) + _loss.mean().asscalar()))
     else:
         for loss_field in self.running_loss_fields:
             self.__setattr__(loss_field, (self.__getattribute__(loss_field) / num_batch))
    def update(self, labels, preds):
        """
        :param labels: [(batch_per_device, ), (), ...]
        :param preds: [(batch_per_device, 1), (), ...]
        :return:
        """
        # binary_label = labels
        # binary_cls_logits = preds[0]
        num_acc = 0
        for lb, pd in zip(labels, preds):
            tp_RET = nd.concatenate([nd.expand_dims(lb, axis=1), pd], axis=1)
            if self.RET is None:
                self.RET = tp_RET
            else:
                self.RET = nd.concatenate([self.RET, tp_RET], axis=0)
            pred_label = nd.squeeze(pd) >= 0.5

            tp = nd.sum(pred_label == lb)
            num_acc = num_acc + tp.asscalar()

        self.sum_metric += num_acc
        self.num_inst += self.config.batch_size
Beispiel #18
0
def extract_feat(ids, data, model):
    ctx = try_gpu()
    if len(ctx) > 1:
        _data_list = gluon.utils.split_and_load(data=data, ctx_list=ctx)
        _id_list = gluon.utils.split_and_load(data=ids, ctx_list=ctx)
    else:
        _data_list = [data.as_in_context(ctx[0])]
        _id_list = [ids.as_in_context(ctx[0])]
    # print(_id_list)
    data_list = []
    id_list = []

    for _ids, _data in zip(_id_list, _data_list):
        feats = model(_data)
        feats = map(lambda x: x / nd.norm(x), feats[:, :, 0, 0])
        feats = [v.expand_dims(axis=0) for v in feats]
        feats = nd.concatenate(feats)
        id_list.append(_ids)
        data_list.append(feats)
    id_list = nd.concatenate(id_list)
    data_list = nd.concatenate(data_list)
    return id_list, data_list
Beispiel #19
0
    def sample_train_batch(self):
        """Sample a training batch (data and label)."""
        batch = []
        labels = []
        num_groups = self.batch_size // self.batch_k

        # For CUB200, we use the first 100 classes for training.
        sampled_classes = np.random.choice(100, num_groups, replace=False)
        for i in range(num_groups):
            img_fnames = np.random.choice(self.train_image_files[sampled_classes[i]],
                                          self.batch_k, replace=False)
            batch += [self.get_image(img_fname, is_train=True) for img_fname in img_fnames]
            labels += [sampled_classes[i] for _ in range(self.batch_k)]

        return nd.concatenate(batch, axis=0), labels
Beispiel #20
0
    def get_test_batch(self):
        """Sample a testing batch (data and label)."""

        batch_size = self.batch_size
        batch = [
            self.get_image(
                self.test_image_files[(self.test_count * batch_size + i) %
                                      len(self.test_image_files)],
                is_train=False) for i in range(batch_size)
        ]
        labels = [
            self.test_labels[(self.test_count * batch_size + i) %
                             len(self.test_image_files)]
            for i in range(batch_size)
        ]
        return nd.concatenate(batch, axis=0), labels
Beispiel #21
0
def extract_feature(model, dataloaders, ctx):
    count = 0
    features = []
    for img, _ in dataloaders:
        n = img.shape[0]
        count += n
        print(count)
        ff = np.zeros((n, 256*num_parts))
        for i in range(2):
            if(i==1):
                img = fliplr(img)
            f = nd.concatenate(model(img.as_in_context(ctx)), axis=1).as_in_context(mx.cpu()).asnumpy()
            ff = ff+f
        features.append(ff)
    features = np.concatenate(features)
    return features/np.linalg.norm(features, axis=1, keepdims=True)
def load_data():
    lfw_url = 'http://vis-www.cs.umass.edu/lfw/lfw-deepfunneled.tgz'
    data_path = data_dir + '/lfw_dataset'
    if not os.path.exists(data_path):
        os.makedirs(data_path)
        data_file = utils.download(lfw_url)
        with tarfile.open(data_file) as tar:
            tar.extractall(path=data_path)
    img_list = []
    for path, _, fnames in os.walk(data_path):
        for fname in fnames:
            if not fname.endswith('.jpg'):
                continue
            img = os.path.join(path, fname)
            img_arr = mx.image.imread(img)
            img_arr = transform(img_arr)
            img_list.append(img_arr)
    train_data = mx.io.NDArrayIter(data=nd.concatenate(img_list), batch_size=batch_size)
    return train_data
Beispiel #23
0
def predict(yolo: Yolo, x, threshold=0.5):
    """
    return label ,C,location
    :param yolo:
    :return:
    """
    assert len(x) == 1, "Only One image for now"
    ypre = yolo(x)
    label, preds, location = deal_output(ypre,
                                         yolo.s,
                                         b=yolo.b,
                                         c=yolo.class_num)
    indexs = []
    for i, c in enumerate(preds[0]):
        if c > threshold:
            indexs.append(i)
    class_names = []
    C_list = []
    bos_list = []
    for index in indexs:
        label_index = int(index / 2)
        location_offect = int(index % 2)
        class_index = nd.argmax(label[0][label_index], axis=0)
        C = preds[0][index]
        locat = location[0][label_index][location_offect]
        C_list.append(C.asscalar())
        #######traslate the name
        label_name = yolo.class_names
        text = label_name[int(class_index.asscalar())]
        class_names.append(text)
        ###traslate the locat
        x, y, w, h = locat
        w, h = nd.power(w, 2), nd.power(h, 2)
        ceil = 1 / 4
        row = int(label_index / 4)
        columns = label_index % 4
        x_center = columns * ceil + x
        y_center = row * ceil + y
        x_min, y_min, x_max, y_max = x_center - 0.5 * w, y_center - 0.5 * h, x_center + 0.5 * w, y_center + 0.5 * h
        box = nd.concatenate([x_min, y_min, x_max, y_max], axis=0) * 256
        bos_list.append(box.asnumpy())
        return class_names, C_list, bos_list
Beispiel #24
0
    def sample_train_batch(self):
        """Sample a training batch (data and label)."""
        batch = []
        labels = []
        num_groups = self.batch_size // self.batch_k

        # For CUB200, we use the first 100 classes for training.
        sampled_classes = np.random.choice(100, num_groups, replace=False)
        for i in range(num_groups):
            img_fnames = np.random.choice(
                self.train_image_files[sampled_classes[i]],
                self.batch_k,
                replace=False)
            batch += [
                self.get_image(img_fname, is_train=True)
                for img_fname in img_fnames
            ]
            labels += [sampled_classes[i] for _ in range(self.batch_k)]

        return nd.concatenate(batch, axis=0), labels
Beispiel #25
0
def concat_and_load(data, use_cpu=True, gpu_id=0):
    """Splits an NDArray into `len(ctx_list)` slices along `batch_axis` and loads
    each slice to one context in `ctx_list`.
    Parameters
    ----------
    data : NDArray
        A batch of data in different gpu.
    use_cpu : bool, default True
        Use cpu or gpu.
    gpu_id : int, default 0
        Which gpu to use.
    Returns
    -------
    NDArray
        All data in one device.
    """
    if isinstance(data, list):
        if use_cpu:
            ctx = mx.cpu()
        else:
            ctx = mx.gpu(gpu_id)
        data = [x.as_in_context(ctx) for x in data]
        return nd.concatenate(data)
Beispiel #26
0
def read_image(dir_path):

    path_list = [path for path in os.listdir(dir_path)]
    path_list = [os.path.join(dir_path, path) for path in path_list]

    p = multiprocessing.Pool(NUM_THREADS)
    print('Check if the path is valid...')
    check_result = []
    for path in tqdm(path_list):
        check_result.append(p.apply_async(_check_img, args=(path, )))
    p.close()
    p.join()

    valid_path_list = []
    ids = []
    for path, check in tqdm(zip(path_list, check_result)):
        if check.get():
            valid_path_list.append(path)
            ids.append(path.split('/')[-1].split('.')[0])
        else:
            continue
    print('Reading images...')
    n = len(valid_path_list)
    num_batches = n // BATCH_SIZE
    ids = nd.array(ids)
    for i in range(num_batches + 1):
        _path_list = valid_path_list[i * BATCH_SIZE:min((i + 1) *
                                                        BATCH_SIZE, n)]
        _ids = ids[i * BATCH_SIZE:min((i + 1) * BATCH_SIZE, n)]
        _data = []
        for path in _path_list:
            img = mx.image.imread(path)
            _data.append(img)
        _data = [img for img in map(transform, _data)]
        _data = nd.concatenate(_data)
        yield _data, _ids.astype('int')
def Gen_noise(fs, T, C, fixed=None):
    tNoiseKEY = 'Event'
    noise_address = os.path.join('./', 'data', 'LIGO_O1_noise_ndarray')
    root = os.path.expanduser(noise_address)
    ll = [
        file for file in os.listdir(root) if '_bug' not in file
        if tNoiseKEY in file
    ]

    if fixed:
        r = 2
        noise = nd.concatenate([
            readnpy(noise_address, file)[1][:, :C, ::4] for file in ll[r:r + T]
        ],
                               axis=0).astype('float32')  # (4096*T, C, 4096)
        noise_gps = nd.concatenate([
            readnpy(noise_address, file)[0, :, :1, ::4] for file in ll[r:r + T]
        ],
                                   axis=0).astype(
                                       'float32')  # (4096*T, C, 4096)
        noise = noise.swapaxes(1, 0).reshape(0, -1, fs * T).swapaxes(1, 0)
        noise_gps = noise_gps.swapaxes(1, 0).reshape(0, -1,
                                                     fs * T).swapaxes(1, 0)
        noise = nd.concatenate([noise, noise_gps], axis=1)
        return noise[:, :2], (ll[r:r + T], noise_gps.asnumpy())

    r = np.random.randint(len(ll) - T)
    noise = nd.concatenate(
        [readnpy(noise_address, file)[1][:, :C, ::4] for file in ll[r:r + T]],
        axis=0).astype('float32')  # (4096*T, C, 4096)
    noise_gps = nd.concatenate(
        [readnpy(noise_address, file)[0, :, :1, ::4] for file in ll[r:r + T]],
        axis=0).astype('float32')  # (4096*T, C, 4096)
    noise = noise.swapaxes(1, 0).reshape(0, -1, fs * T).swapaxes(1, 0)
    noise_gps = noise_gps.swapaxes(1, 0).reshape(0, -1, fs * T).swapaxes(1, 0)
    noise = nd.concatenate([noise, noise_gps], axis=1)
    noise = nd.shuffle(noise)
    if T != 1:
        return noise[:, :2], (ll[r:r + T], noise[:, 2].asnumpy())
    return noise, (ll[r:r + T], noise[:, 2, 0].asnumpy())  #  (nsample, C, N)
    utils.visualize(utils.img_list_clock[i][0])
plt.suptitle('clock')
plt.show()
fig = plt.figure(figsize=(3.5,3.5))
for i in range(9):
    plt.subplot(3,3,i+1)
    utils.visualize(utils.img_list_crocodile[i][0])
plt.suptitle('crocodile')
plt.show()


#======================shuffle data and set some params======================
batch_size = 50
ctx = utils.try_gpu()
label = nd.stack(nd.zeros([int(len(utils.img_list)/2),1],ctx=ctx),nd.ones([int(len(utils.img_list)/2),1],ctx=ctx)).reshape([len(utils.img_list)])
data = nd.concatenate(utils.img_list)
mx.random.seed(2)
data = random.shuffle(data)
mx.random.seed(2)
label = random.shuffle(label)



epochs = 200
loss = gluon.loss.SoftmaxCrossEntropyLoss()
k_cross = 10
path_net = 'net'
if not os.path.exists(path_net):
    os.makedirs(path_net)

Beispiel #29
0
def MultiBoxTarget(anchors,
                   class_predictions,
                   labels,
                   hard_neg_ratio=3,
                   ctx=gpu(),
                   verbose=False):
    '''將真實方框(ground truth boxes)和預設錨框(default anchor boxes)做配對。
       labels:         真實方框 (ground truth boxes)。
       anchors:        預設錨框 (default anchor boxes)。
       hard_neg_ratio: 負樣本(背景)和正樣本(有物體的錨框數)的比例。預設是3:1。
    '''

    if verbose:
        print("anchors shape=", anchors.shape)
    assert anchors.shape[0] == 1

    batch_size = len(labels)
    num_priors = anchors.shape[1]

    if verbose:
        print("batch size=\t", batch_size)
        print("num priors=\t", num_priors)

    anchor_shifts = nd.zeros((batch_size, anchors.shape[1] * 4), ctx=ctx)
    box_mask = nd.zeros((batch_size, anchors.shape[1] * 4), ctx=ctx)

    anchor_classes = nd.zeros((batch_size, num_priors), ctx=ctx) - 1
    anchor_indices = nd.zeros((batch_size, num_priors), ctx=ctx) - 1

    classes_mask = nd.zeros((batch_size, anchors.shape[1]), ctx=ctx)

    shifts_tmp = nd.zeros_like(anchors[0])
    mask_tmp = nd.zeros_like(anchors[0])

    for i in range(batch_size):

        label_locs = labels[i][:, 1:]
        label_classes = labels[i][:, 0]
        class_preds = class_predictions[i]
        # obtain IoU
        ious = iou(label_locs, anchors[0])
        # identify how many ground truth objects are there in this batch
        if -1 in label_classes:
            num_obj = label_classes.argmin(axis=0).astype("int32").asscalar()
        else:
            num_obj = label_classes.size
        if num_obj == 0:
            continue
        # matching anchor boxes with ground truth boxes
        ious_flag = ious > 0.5
        # find locations of the best priors
        best_prior_idx = ious[0:num_obj, :].argmax(axis=1)

        idx_row = [*range(best_prior_idx.size)]
        ious_flag[idx_row, best_prior_idx] += 1

        if_negative = label_classes != -1
        label_classes = label_classes + if_negative * 1
        # add the -1 class to the end
        label_classes = nd.concat(label_classes,
                                  nd.array([-1], ctx=label_classes.context),
                                  dim=0)
        if verbose:
            print("label classes=\t", label_classes)
        if_matched = ious_flag.sum(axis=0) != 0
        label_classes_last_idx = len(label_classes) - 1
        anchor_indices[i] = (ious_flag.argmax(axis=0) - label_classes_last_idx
                             ) * if_matched + label_classes_last_idx
        anchor_classes[i] = label_classes[anchor_indices[i]]

        # find indices of the negative anchors
        neg_indices, = np.where(anchor_classes[i].asnumpy() == -1)
        # count number of positive/negative/hard negative anchors
        num_neg_anchors = len(neg_indices)
        num_pos_anchors = num_priors - num_neg_anchors
        num_hard_neg_anchors = num_pos_anchors * hard_neg_ratio

        # hard negative mining
        #conf_loss_indices = nd.argsort( (-nd.softmax(  class_preds[neg_indices] ) )[:,0], is_ascend=False ).astype("int32")
        conf_loss_indices = nd.argsort((-class_preds[neg_indices])[:, 0],
                                       is_ascend=False).astype("int32")
        neg_indices_sorted = nd.array(neg_indices,
                                      ctx=class_preds.context,
                                      dtype="int32")[conf_loss_indices]
        hard_neg_indices = neg_indices_sorted[:num_hard_neg_anchors]

        anchor_classes[i][hard_neg_indices] = 0

        # find indices of the positive anchors
        pos_indices, = np.where(
            anchor_indices[i].asnumpy() < label_classes_last_idx)

        pos_indices = nd.array(pos_indices,
                               ctx=hard_neg_indices.context).astype("int32")

        cls_indices = nd.concatenate([hard_neg_indices, pos_indices
                                      ])  # store indices for classification.
        classes_mask[i][cls_indices] = 1

        if verbose:
            print("========================================")
            display(
                pd.DataFrame(anchor_classes[i].asnumpy()).groupby(0).indices)
            df = pd.DataFrame(
                anchor_classes[i].asnumpy()).reset_index().groupby(0).count()
            df.index.name = "class"
            df.index = df.index.astype("int32")
            df.columns = ["number of default anchor boxes"]
            display(df)
            print("========================================")

        # obtain locations of the positve anchors
        pos_anchors_loc = anchors[0][pos_indices]
        # obtain indices of the ground truth labels
        idx_gt = anchor_indices[i][pos_indices]
        # obtain locations of the ground truth labels
        labels_loc = label_locs[idx_gt]
        assert len(pos_anchors_loc) == len(labels_loc)
        # calculate location differences between ground truth labels and positive anchors
        shifts_tmp[pos_indices] = loc_difference_calculator(
            pos_anchors_loc, labels_loc)
        mask_tmp[pos_indices] = 1.

        anchor_shifts[i] = shifts_tmp.reshape(-1)
        box_mask[i] = mask_tmp.reshape(-1)

    return anchor_shifts, box_mask, anchor_classes, classes_mask
Beispiel #30
0
def pose_nms(pose_preds, pose_scores, bbox_preds, bbox_scores, ori_bbox):
    '''
    Parametric Pose NMS algorithm
    pose_preds:     pose locations nd array (n, 17, 2)
    pose_scores:    pose scores nd array    (n, 17, 1)

    bboxes:         bbox locations list (n, 4)
    bbox_scores:    bbox scores list (n,)

    return:
        pred_coords:    pose locations array    (n_new, 17, 2)
        confidence:     pose scores array       (n_new, 17, 1)
    '''
    np_bbox_preds = np.array(bbox_preds)
    np_ori_bbox = np.array(ori_bbox)
    np_bbox_scores = np.array(bbox_scores)
    pose_preds = pose_preds.asnumpy()
    pose_scores = pose_scores.asnumpy()

    pose_scores[pose_scores < scoreThreds] == 1e-5

    ori_pose_preds = pose_preds.copy()
    ori_pose_scores = pose_scores.copy()
    ori_bbox = np_ori_bbox.copy()

    final_result = []
    pred_coords, confidence, pred_bbox = [], [], []

    xmax = np_bbox_preds[:, 2]
    xmin = np_bbox_preds[:, 0]
    ymax = np_bbox_preds[:, 3]
    ymin = np_bbox_preds[:, 1]

    widths = xmax - xmin
    heights = ymax - ymin
    ref_dists = alpha * np.maximum(widths, heights)

    nsamples = np_bbox_preds.shape[0]

    human_scores = np.mean(pose_scores, axis=1) + np.max(
        pose_scores, axis=1) + np_bbox_scores

    human_ids = np.arange(nsamples)

    # Do pPose-NMS
    pick = []
    merge_ids = []
    while (human_scores.shape[0] != 0):
        pick_id = np.argmax(human_scores)
        pick.append(human_ids[pick_id])

        # Get numbers of match keypoints by calling PCK_match
        ref_dist = ref_dists[human_ids[pick_id]]
        simi = get_parametric_distance(pick_id, pose_preds, pose_scores,
                                       ref_dist)
        num_match_keypoints = PCK_match(pose_preds[pick_id], pose_preds,
                                        ref_dist)

        # Delete humans who have more than matchThreds keypoints overlap and high similarity
        delete_ids = np.arange(
            human_scores.shape[0])[(simi > gamma) |
                                   (num_match_keypoints >= matchThreds)]

        if delete_ids.shape[0] == 0:
            delete_ids = pick_id

        merge_ids.append(human_ids[delete_ids])
        pose_preds = np.delete(pose_preds, delete_ids, axis=0)
        pose_scores = np.delete(pose_scores, delete_ids, axis=0)
        human_ids = np.delete(human_ids, delete_ids)
        human_scores = np.delete(human_scores, delete_ids, axis=0)

    assert len(merge_ids) == len(pick)
    preds_pick = ori_pose_preds[pick]
    scores_pick = ori_pose_scores[pick]
    ori_bbox_pick = ori_bbox[pick]

    for j in range(len(pick)):
        ids = np.arange(17)
        max_score = np.max(scores_pick[j, ids, 0])

        if max_score < scoreThreds:
            continue

        # Merge poses
        merge_id = merge_ids[j]
        merge_pose, merge_score = p_merge_fast(preds_pick[j],
                                               ori_pose_preds[merge_id],
                                               ori_pose_scores[merge_id],
                                               ref_dists[pick[j]])

        max_score = np.max(merge_score[ids])

        if max_score < scoreThreds:
            continue

        xmax = max(merge_pose[:, 0])
        xmin = min(merge_pose[:, 0])
        ymax = max(merge_pose[:, 1])
        ymin = min(merge_pose[:, 1])

        if (1.5**2 * (xmax - xmin) * (ymax - ymin) < 40 * 40):
            continue

        pred_coords.append(nd.array(merge_pose[None, :, :]))
        confidence.append(nd.array(merge_score[None, :, :]))
        pred_bbox.append(nd.array(ori_bbox_pick[j][None, :]))

        final_result.append({
            'keypoints':
            merge_pose,
            'kp_score':
            merge_score,
            'proposal_score':
            np.mean(merge_score) + 1.25 * np.max(merge_score)
        })
    if len(pred_coords) == 0:
        return None, None, None
    pred_coords = nd.concatenate(pred_coords)
    confidence = nd.concatenate(confidence)
    pred_bbox = nd.concatenate(pred_bbox)
    return pred_coords, confidence, pred_bbox
Beispiel #31
0
    def train_model_for_tl(self):
        """
        训练Triplet Loss模型
        :return: 当前模型
        """
        net_path = os.path.join(
            DATA_DIR, 'model',
            'epoch-24-0.54-20180920182658.params-symbol.json')
        params_path = os.path.join(
            DATA_DIR, 'model',
            'epoch-24-0.54-20180920182658.params-0024.params')
        hash_num = 128

        # base_net = gluon.nn.SymbolBlock.imports(net_path, ['data'], params_path)
        base_net = self.get_base_net()
        with base_net.name_scope():
            base_net.output = Dense(units=hash_num)  # 全连接层
        base_net.output.initialize(Xavier(), ctx=self.ctx)  # 初始化
        base_net.collect_params().reset_ctx(self.ctx)
        base_net.hybridize()

        train_data, train_len = self.get_tl_train_data(self.batch_size)
        val_data, val_len = self.get_tl_val_data(self.batch_size)
        self.print_info("Triplet Loss 训练样本数: {}".format(train_len))
        self.print_info("Triplet Loss 验证样本数: {}".format(val_len))

        triplet_loss = gluon.loss.TripletLoss(margin=10.0)
        trainer = Trainer(base_net.collect_params(), 'rmsprop',
                          {'learning_rate': 1e-4})

        for epoch in range(self.epochs):
            e_loss, final_i = 0, 0
            for i, batch in enumerate(train_data):
                data, labels = batch[0], batch[1].astype('float32')
                data = split_and_load(data,
                                      ctx_list=self.ctx,
                                      batch_axis=0,
                                      even_split=False)
                labels = split_and_load(labels,
                                        ctx_list=self.ctx,
                                        batch_axis=0,
                                        even_split=False)
                data_loss = []

                with autograd.record():  # 梯度求导
                    for X in data:
                        anchor_ins, pos_ins, neg_ins = [], [], []
                        for b_X in X:
                            anchor_ins.append(nd.expand_dims(b_X[0], axis=0))
                            pos_ins.append(nd.expand_dims(b_X[1], axis=0))
                            neg_ins.append(nd.expand_dims(b_X[2], axis=0))
                        anchor_ins = nd.concatenate(anchor_ins, axis=0)
                        pos_ins = nd.concatenate(pos_ins, axis=0)
                        neg_ins = nd.concatenate(neg_ins, axis=0)

                        inter1 = base_net(anchor_ins)
                        inter2 = base_net(pos_ins)
                        inter3 = base_net(neg_ins)

                        loss = triplet_loss(inter1, inter2,
                                            inter3)  # TripletLoss
                        data_loss.append(loss)

                for l in data_loss:
                    l.backward()

                curr_loss = np.mean(
                    [mx.nd.mean(loss).asscalar() for loss in data_loss])
                self.print_info("batch: {}, loss: {}".format(i, curr_loss))
                e_loss += curr_loss
                final_i = i + 1
                trainer.step(self.batch_size)

            self.print_info("epoch: {}, loss: {}".format(
                epoch, safe_div(e_loss, final_i)))
            dist_acc = self.evaluate_net(base_net, val_data)  # 评估epoch的性能
            self.save_net_and_params(base_net,
                                     epoch,
                                     dist_acc,
                                     name='tripletloss')  # 存储网络
def preDataset2(SNR, data, batch_size, shuffle=True, fixed=None, debug=True):

    dataset, _, RP, keys, fs, T, C, margin, _ = data
    # Window function
    dwindow = tukey(fs * T, alpha=1. / 8)

    data_block, label_block, chiMkeys_block, Mratiokeys_block, datasets, iterator = {}, {}, {}, {}, {}, {}

    for pre in ['train', 'test']:
        data_block[pre] = RP(dataset[pre])  # (nsample, C, T*fs)
        # data_block[pre] = nd.concat(RP(dataset[pre]), RP(dataset[pre]), dim=0)  # 3150x1x4096  cpu nd.array
        if margin != 0.5:  # global
            assert nd.sum(
                nd.abs(data_block[pre].argmax(-1) - fs // 2) > fs /
                10).asscalar() == 0  # Check the peaks
        nsample = data_block[pre].shape[0]

        noise, noise_m_gps = Gen_noise(
            fs, T, C, fixed=fixed)  # (4096, C, fs*T)  cpu ndarray

        sigma = data_block[pre].max(axis=-1) / SNR / nd_std(noise[:nsample],
                                                            axis=-1)
        signal = nd.divide(data_block[pre], sigma[:, 0].reshape(
            (nsample, 1, 1)))  # taking H1 as leading
        data_block[pre] = signal + noise[:nsample]  # (nsample, C, T*fs)
        if fixed:
            noise_m, noise_p_gps = noise, noise_m_gps
        else:
            noise_m, noise_p_gps = Gen_noise(
                fs, T, C, fixed=fixed)  # (4096, C, fs*T)  cpu ndarray
        data_block[pre] = nd.concat(data_block[pre], noise_m[:nsample],
                                    dim=0)  # (nsample, 1, T*fs) cpu nd.array
        # (nsample, C, T*fs)

        # Note: use mixed data to gen PSD
        spsd_block_channel = []
        for c in range(C):
            spsd_block = np.concatenate([
                np.real(np.fft.ifft(
                    1 / np.sqrt(power_vec(i[c].asnumpy(), fs)))).reshape(
                        1, -1) for i in data_block[pre]
            ])
            # (nsample, T*fs)  np.array
            spsd_block_channel.append(
                nd.array(spsd_block).expand_dims(1).expand_dims(
                    1))  # (nsample, 1, 1, T*fs) nd.array cpu
        spsd_block = nd.concatenate(spsd_block_channel,
                                    axis=1)  # (nsample, C, 1, T*fs)
        if debug:
            logger.debug('spsd_block for {}: {}', pre, spsd_block.shape)

        # data * dwindow
        data_block[pre] = (data_block[pre] * nd.array(dwindow)).expand_dims(
            2)  # (nsample, C, 1, T*fs) nd.array cpu
        if debug:
            logger.debug('data_block for {}: {}', pre, data_block[pre].shape)

        data_block[pre] = nd.concat(data_block[pre].expand_dims(1),
                                    spsd_block.expand_dims(1),
                                    dim=1)
        if debug:
            logger.debug(
                'data_block(psd,nd) for {}: {}', pre,
                data_block[pre].shape)  # (nsmaple, 2, C, 1, T*fs) cpu nd.array

        label_block[pre] = nd.array([1] * nsample + [0] * nsample)

        chiMkeys_block[pre] = nd.array(
            getchiM(keys[pre]).tolist() + [0] * nsample)
        Mratiokeys_block[pre] = nd.array(
            getMratio(keys[pre]).tolist() + [0] * nsample)

        datasets[pre] = gluon.data.ArrayDataset(data_block[pre],
                                                label_block[pre],
                                                chiMkeys_block[pre],
                                                Mratiokeys_block[pre])
        iterator[pre] = gdata.DataLoader(datasets[pre],
                                         batch_size,
                                         shuffle=shuffle,
                                         last_batch='keep',
                                         num_workers=0)
        if debug:
            logger.debug('\nNoise from: {} | {}', noise_m_gps[0],
                         noise_p_gps[0])

    return dataset, iterator, (noise_m_gps[0] + noise_p_gps[0],
                               np.concatenate((noise_m_gps[1][:nsample],
                                               noise_p_gps[1][:nsample]),
                                              axis=0))
Beispiel #33
0
    def fit(self,
            data_dir_path,
            model_dir_path,
            epochs=2,
            batch_size=64,
            learning_rate=0.0002,
            beta1=0.5,
            extension='.jpg'):

        config = dict()
        config['random_input_size'] = self.random_input_size
        np.save(self.get_config_file_path(model_dir_path), config)

        img_list = load_images(data_dir_path, extension=extension)
        img_list = [
            img_arr.reshape((1, ) + img_arr.shape) for img_arr in img_list
        ]
        train_data = mx.io.NDArrayIter(data=nd.concatenate(img_list),
                                       batch_size=batch_size)

        loss = gluon.loss.SigmoidBinaryCrossEntropyLoss()

        self.netG, self.netD = self.create_model()

        self.netG.initialize(mx.init.Normal(0.02), ctx=self.model_ctx)
        self.netD.initialize(mx.init.Normal(0.02), ctx=self.model_ctx)

        trainerG = gluon.Trainer(self.netG.collect_params(), 'adam', {
            'learning_rate': learning_rate,
            'beta1': beta1
        })
        trainerD = gluon.Trainer(self.netD.collect_params(), 'adam', {
            'learning_rate': learning_rate,
            'beta1': beta1
        })

        real_label = nd.ones((batch_size, ), ctx=self.model_ctx)
        fake_label = nd.zeros((batch_size, ), ctx=self.model_ctx)

        metric = mx.metric.CustomMetric(facc)

        logging.basicConfig(level=logging.DEBUG)

        fake = []
        for epoch in range(epochs):
            tic = time.time()
            btic = time.time()
            train_data.reset()
            iter = 0
            for batch in train_data:

                # Step 1: Update netD
                data = batch.data[0].as_in_context(self.model_ctx)
                random_input = nd.random_normal(0,
                                                1,
                                                shape=(data.shape[0],
                                                       self.random_input_size,
                                                       1, 1),
                                                ctx=self.model_ctx)

                with autograd.record():
                    # train with real image
                    output = self.netD(data).reshape((-1, 1))
                    errD_real = loss(output, real_label)
                    metric.update([
                        real_label,
                    ], [
                        output,
                    ])

                    # train with fake image
                    fake = self.netG(random_input)
                    output = self.netD(fake).reshape((-1, 1))
                    errD_fake = loss(output, fake_label)
                    errD = errD_real + errD_fake
                    errD.backward()
                    metric.update([
                        fake_label,
                    ], [
                        output,
                    ])

                trainerD.step(batch.data[0].shape[0])

                # Step 2: Update netG
                with autograd.record():
                    fake = self.netG(random_input)
                    output = self.netD(fake).reshape((-1, 1))
                    errG = loss(output, real_label)
                    errG.backward()

                trainerG.step(batch.data[0].shape[0])

                # Print log infomation every ten batches
                if iter % 10 == 0:
                    name, acc = metric.get()
                    logging.info('speed: {} samples/s'.format(
                        batch_size / (time.time() - btic)))
                    logging.info(
                        'discriminator loss = %f, generator loss = %f, binary training acc = %f at iter %d epoch %d'
                        % (nd.mean(errD).asscalar(), nd.mean(errG).asscalar(),
                           acc, iter, epoch))
                iter = iter + 1
                btic = time.time()

            name, acc = metric.get()
            metric.reset()
            logging.info('\nbinary training acc at epoch %d: %s=%f' %
                         (epoch, name, acc))
            logging.info('time: %f' % (time.time() - tic))

            self.checkpoint(model_dir_path)

            # Visualize one generated image for each epoch
            fake_img = fake[0]
            fake_img = ((fake_img.asnumpy().transpose(1, 2, 0) + 1.0) *
                        127.5).astype(np.uint8)
            save_image(
                fake_img,
                os.path.join(model_dir_path, DCGan.model_name + '-training-') +
                str(epoch) + '.png')