Exemplo n.º 1
0
def eval(model, data_loader, args=args):
    model.eval()

    for _, (data, label, lab_sub1, lab_sub2,
            lab_sub4) in enumerate(data_loader):

        _, _, logit = model(data)
        pred = torch.argmax(logit, dim=1).int()
        miou = mean_iou(pred, lab_sub1, args.num_classes)
Exemplo n.º 2
0
def val_one_step(model, X, Y, total_iou):
    '''
    compute valitation datasets loss and accuracy.

    Parameters
    ----------
    model : psenet + fpn
    X : images
    Y : labels

    Returns loss, accuracy
    -------

    '''
    X = tf.cast(X, tf.float32)
    Y = tf.cast(Y, tf.float32)
    y_pred = model(X)
    loss = build_loss(y_true=Y,y_pred=y_pred)
    loss = tf.reduce_mean(loss)
    accuracy, total_iou = mean_iou(y_true=Y, y_pred=y_pred, total_iou=total_iou)
    return loss, accuracy
Exemplo n.º 3
0
def train(model, optimizer, dataloader_train, device, args=args):
    model.train()
    step = 0
    mIoU_cache = 0.5
    mIou = 0.5
    lr = args.learning_rate

    writer = SummaryWriter(log_dir=args.log_dir)

    for epoch in range(args.num_epochs):
        now = datetime.datetime.now()
        lr = poly_lr_scheduler(optimizer,
                               args.learning_rate,
                               iter=epoch,
                               max_iter=args.num_epochs)
        # epoch = epoch + args.epoch_start_i
        # epoch = epoch + 1
        model.train()
        loss_record = []

        for _, (data, label, lab_sub1, lab_sub2,
                lab_sub4) in enumerate(dataloader_train):

            data, label = data.to(device), label.to(device)
            lab_sub1, lab_sub2, lab_sub4 = lab_sub1.to(device), lab_sub2.to(
                device), lab_sub4.to(device)

            #print("label",label.shape)
            #print("label_sub1",lab_sub1.shape)
            #print(lab_sub1)
            #print("labe2_sub1",lab_sub2.shape)
            #print(lab_sub2)
            #print("labe4_sub1",lab_sub4.shape)
            #print("lab_sub4",lab_sub4)

            sub4_out, sub24_out, sub124_out = model(data)
            # print("sub4_out", sub4_out.shape)
            # print("sub24_out", sub24_out.shape)
            # print("sub124_out", sub124_out)
            loss_sub4 = loss(sub4_out, lab_sub4, args.num_classes)
            loss_sub24 = loss(sub24_out, lab_sub2, args.num_classes)
            loss_sub124 = loss(sub124_out, lab_sub1, args.num_classes)
            #
            reduced_loss = LAMBDA1 * loss_sub4 + LAMBDA2 * loss_sub24 + LAMBDA3 * loss_sub124
            optimizer.zero_grad()  # 梯度清零
            reduced_loss.backward()  # 计算梯度
            optimizer.step()
            step += 1
            loss_record.append(reduced_loss.item())

            # mIou = mean_iou(pred=sub124_out, label=lab_sub1, num_classes=args.num_classes)
            # print("MIOU:", mIou)
            # print("time:",datetime.datetime.now()-now)

        loss_tm = np.mean(loss_record)
        print("epoch:", epoch, "loss:", loss_tm)
        writer.add_scalar("train_loss", loss_tm, epoch)

        if (epoch + 1) % 5 == 0 and epoch != 0:
            torch.save(model.state_dict(), args.checkpoints(epoch + 1))
        if (epoch + 1) % 5 == 0 and epoch != 0:
            # mIou=val(model,dataloader_train,args.csv_dir,epoch,loss_tm,writer,args)
            # print("MIOU:",mIou)
            mIou = mean_iou(pred=sub124_out,
                            label=lab_sub1,
                            num_classes=args.num_classes)
            print("MIOU:", mIou)

        if mIou > mIoU_cache:
            torch.save(model.state_dict(), args.checkpoints(mIou))
            mIoU_cache = mIou
        print("time:", datetime.datetime.now() - now)
    torch.save(model.state_dict(), args.checkpoints("last"))
    writer.close()
Exemplo n.º 4
0
    def __getitem__(self, index):
        if index >= self.__len__():
            raise IndexError()
        image, bbox = pickle.load(open(self.file_list.file[index], 'rb'))
        image = image.astype(np.float)
        image = image.transpose((2, 0, 1)) / 255
        image = image.astype(np.float32)
        nchannel, nrow, ncol = image.shape

        arow, acol = nrow // self.ratio, ncol // self.ratio
        archor_reg = np.zeros((self.n_archer, 2, arow, acol), np.float32)
        arc_to_bbox_map = np.zeros((self.n_archer, 1, arow, acol), np.int) - 1

        center_rows = [self.ratio // 2 + i * self.ratio for i in range(arow)]
        center_cols = [self.ratio // 2 + i * self.ratio for i in range(acol)]
        iou_map = np.zeros((len(bbox), self.n_archer, 1, arow, acol))
        # mean_iou_map has shape of n_bbox, n_archer, 1, row, col
        for bbox_idx, label_box in enumerate(bbox):
            if label_box[-1] != 0:
                continue
            # iou_map = np.zeros((self.n_archer, 1, arow, acol), np.float)
            for irow, icol, iarc in product(
                    range(arow),
                    range(acol),
                    range(self.n_archer)):
                abox = (
                    center_rows[irow],
                    center_cols[icol],
                    *self.archers[iarc]
                )
                iou_map[bbox_idx, iarc, 0, irow, icol] = mean_iou(
                    abox, label_box)
        # TODO deal with the f*****g mean_iou thing
        if debug and len(bbox) > 0:
            show_img = np.max(iou_map, axis=1, keepdims=True)
            logger.info(show_img.shape)
            show_img = make_grid(torch.Tensor(iou_map[:, 0, :, :, :]))
            logWriter.add_image('iou_map', show_img, index)
            logger.info(show_img.shape)
            plt.figure()
            plt.imshow(show_img)
            plt.show()
        if len(bbox) == 0:
            positive = np.zeros(iou_map.shape[1:])
            negative = np.zeros(iou_map.shape[1:])
        else:
            # logger.info(iou_map.shape)
            iou_map = np.max(iou_map, axis=0)
            positive = iou_map > 0.5
            positive = positive.astype(np.float32)
            negative = iou_map < 0.2
            negative = negative.astype(np.float32)

        loss_area = ((positive) + (negative)) > 0
        n_postive = np.sum(positive)
        mask = positive > 0
        random_sample = np.random.rand(*mask.shape) * loss_area
        mask += random_sample >= min(self.thresh, np.max(random_sample) * 0.98)
        mask = mask > 0
        mask = mask.astype(np.float32)
        n_psample = np.sum((mask * positive) > 0)
        n_nsample = np.sum((mask * negative) > 0)
        self.thresh -= n_psample / self.smooth_factor
        self.thresh += n_nsample / self.smooth_factor
        # logger.info(f'{n_psample:6d} {n_nsample:6d} {self.thresh:.6f}')

        archor_cls = np.concatenate(
            (negative.astype(np.int), positive.astype(np.int)),
            axis=1)

        # logger.info(f'{arow}, {acol}, {self.n_archer}, {len(bbox)}')
        for irow, icol, iarc in product(
                range(arow),
                range(acol),
                range(self.n_archer)):
            if positive[iarc, 0, irow, icol] == 0:
                continue
            current_bbox = bbox[arc_to_bbox_map[iarc, 0, irow, icol]]
            t_row = (current_bbox[0] - center_rows[irow])\
                / self.archers[iarc][0]
            t_col = (current_bbox[1] - center_cols[icol])\
                / self.archers[iarc][1]
            archor_reg[iarc, :, irow, icol] = (
                t_row, t_col)
        archor_cls = archor_cls.astype(np.float32)
        return image, (archor_cls, archor_reg, mask)
Exemplo n.º 5
0
    def __getitem__(self, index):
        if index >= self.__len__():
            raise IndexError()
        image, bbox = pickle.load(open(self.file_list.file[index], 'rb'))
        image = image.astype(np.float)
        image = image.transpose((2, 0, 1)) / 255
        image = image.astype(np.float32)
        nchannel, nrow, ncol = image.shape

        arow, acol = nrow // self.ratio, ncol // self.ratio
        archor_reg = np.zeros((self.n_archer, 2, arow, acol), np.float32)
        arc_to_bbox_map = np.zeros((self.n_archer, 1, arow, acol), np.int) - 1

        center_rows = [self.ratio // 2 + i * self.ratio for i in range(arow)]
        center_cols = [self.ratio // 2 + i * self.ratio for i in range(acol)]
        iou_map = np.zeros((len(bbox), self.n_archer, 1, arow, acol))
        # mean_iou_map has shape of n_bbox, n_archer, 1, row, col
        for bbox_idx, label_box in enumerate(bbox):
            if label_box[-1] != 0:
                continue
            for irow, icol, iarc in product(
                    range(arow),
                    range(acol),
                    range(self.n_archer)):
                abox = (
                    center_rows[irow],
                    center_cols[icol],
                    *self.archers[iarc]
                )
                iou_map[bbox_idx, iarc, 0, irow, icol] = mean_iou(
                    abox, label_box)
        if len(bbox) == 0:
            positive = np.zeros(iou_map.shape[1:])
            negative = np.zeros(iou_map.shape[1:]) + 1
        else:
            # TODO deal with the f*****g mean_iou thing
            box_map = np.argmax(
                np.concatenate((np.zeros((1, *iou_map.shape[1:])), iou_map)),
                axis=0)
            max_iou = np.max(iou_map, axis=0)
            positive = max_iou > 0.1
            positive = positive.astype(np.float32)
            negative = max_iou == 0
            negative = negative.astype(np.float32)
            box_map *= positive.astype(np.int64)

            if debug:
                plt.figure()
                plt.imshow(max_iou[0, 0, :, :])
                plt.colorbar()
                plt.figure()
                plt.imshow(box_map[0, 0, :, :])
                plt.colorbar()
                plt.figure()
                draw_box = image.transpose(1, 2, 0)
                for sb in bbox:
                    if sb[-1] != 0:
                        continue
                    draw_box = draw_bounding_box(
                        draw_box,
                        *sb[:4],
                    )
                plt.imshow(draw_box)
                plt.colorbar()
                # plt.show()

        n_postive = np.sum(positive)
        random_sample = np.random.rand(*negative.shape) * negative
        neg_sample = random_sample >= min(
            self.thresh, np.max(random_sample) * 0.999)
        mask = (positive + neg_sample) > 0
        mask = mask.astype(np.float32)
        n_psample = np.sum(positive > 0)
        n_nsample = np.sum(neg_sample > 0)
        self.thresh -= n_psample / self.smooth_factor
        self.thresh += n_nsample / self.smooth_factor
        # logger.info(f'{n_psample:6d} {n_nsample:6d} {self.thresh:.6f}')

        archor_cls = np.concatenate(
            (negative.astype(np.int), positive.astype(np.int)),
            axis=1)

        if debug:
            plt.figure()
            plt.imshow(archor_cls[0, 0, :, :])
            plt.colorbar()
            plt.figure()
            plt.imshow(archor_cls[0, 1, :, :])
            plt.colorbar()
            plt.figure()
            plt.imshow(mask[0, 0, :, :])
            plt.colorbar()
            plt.show()

        # logger.info(f'{arow}, {acol}, {self.n_archer}, {len(bbox)}')
        for irow, icol, iarc in product(
                range(arow),
                range(acol),
                range(self.n_archer)):
            if positive[iarc, 0, irow, icol] == 0:
                continue
            current_bbox = bbox[arc_to_bbox_map[iarc, 0, irow, icol]]
            t_row = (current_bbox[0] - center_rows[irow])\
                / self.archers[iarc][0]
            t_col = (current_bbox[1] - center_cols[icol])\
                / self.archers[iarc][1]
            archor_reg[iarc, :, irow, icol] = (
                t_row, t_col)
        archor_cls = archor_cls.astype(np.float32)
        return image, (archor_cls, archor_reg, mask)