Exemple #1
0
    def validate(self):
        print 'Validating'
        self.model.temperature = 0
        # Use temperature 0 while validating
        self.model.evaluator.eval()

        ious = []
        pred_ious = []
        loss = []

        with torch.no_grad():
            for step, data in enumerate(tqdm(self.val_loader)):
                output = self.model(data['img'].to(device))

                # Get full GT masks
                gt_masks = []
                for instance in data['instance']:
                    gt_masks.append(
                        utils.get_full_mask_from_instance(
                            self.opts['dataset']['train_val']['min_area'],
                            instance))

                iou = np.zeros((len(gt_masks)), dtype=np.float32)

                pred_masks = []
                pred_polys = output['pred_polys'].cpu().numpy()
                for i in range(pred_polys.shape[0]):
                    poly = pred_polys[i]
                    mask, poly = utils.get_full_mask_from_xy(
                        poly, self.grid_size, data['patch_w'][i],
                        data['starting_point'][i], instance['img_height'],
                        instance['img_width'])

                    pred_masks.append(mask)

                for i, gt_mask in enumerate(gt_masks):
                    iou[i] = metrics.iou_from_mask(pred_masks[i], gt_mask)

                l = losses.evaluator_loss(output['ious'],
                                          torch.from_numpy(iou).to(device))

                iou = np.mean(iou)
                ious.append(iou)
                pred_ious.append(float(torch.mean(output['ious'])))
                loss.append(l)

                del (output)

            iou = np.mean(ious)
            pred_iou = np.mean(pred_ious)
            loss = np.mean(loss)

            self.val_writer.add_scalar('loss', float(loss), self.global_step)
            print '[VAL] GT IoU: %f, Pred IoU: %f, Loss: %f' % (iou, pred_iou,
                                                                loss)

        # Reset
        self.model.temperature = self.opts['temperature']
        self.model.evaluator.train()
Exemple #2
0
    def validate(self):
        print 'Validating'
        self.model.first_v.eval()
        # Leave LSTM in train mode, encoder always in eval mode for RL
        self.model.temperature = 0

        ious = []

        with torch.no_grad():
            for step, data in enumerate(tqdm(self.val_loader)):
                output = self.model(data['img'].to(device))

                iou = 0
                # Get full GT masks
                gt_masks = []
                for instance in data['instance']:
                    gt_masks.append(
                        utils.get_full_mask_from_instance(
                            self.opts['dataset']['train_val']['min_area'],
                            instance))

                pred_masks = []
                pred_polys = output['pred_polys'].cpu().numpy()
                for i in range(pred_polys.shape[0]):
                    poly = pred_polys[i]
                    mask, poly = utils.get_full_mask_from_xy(
                        poly, self.grid_size, data['patch_w'][i],
                        data['starting_point'][i], instance['img_height'],
                        instance['img_width'])

                    pred_masks.append(mask)

                for i, gt_mask in enumerate(gt_masks):
                    iou += metrics.iou_from_mask(pred_masks[i], gt_mask)

                iou = iou / pred_polys.shape[0]
                ious.append(iou)

                del (output)

            iou = np.mean(ious)
            self.val_writer.add_scalar('iou', float(iou), self.global_step)

            print '[VAL] IoU: %f' % (iou)

        # Reset
        self.model.first_v.train()
Exemple #3
0
    def train(self, epoch):
        print 'Starting training'
        self.model.temperature = self.opts['temperature']
        self.model.evaluator.train()

        accum = defaultdict(float)
        # To accumulate stats for printing

        for step, data in enumerate(self.train_loader):
            if self.global_step % self.opts['val_freq'] == 0:
                self.validate()
                self.save_checkpoint(epoch)

            # Forward pass (Sampling)
            output = self.model(data['img'].to(device))

            # Get full GT masks
            gt_masks = []
            for instance in data['instance']:
                gt_masks.append(
                    utils.get_full_mask_from_instance(
                        self.opts['dataset']['train_val']['min_area'],
                        instance))

            # Get sampling masks
            sampling_masks = []
            pred_polys = output['pred_polys'].cpu().numpy()
            for i in range(pred_polys.shape[0]):
                poly = pred_polys[i]
                mask, poly = utils.get_full_mask_from_xy(
                    poly, self.grid_size, data['patch_w'][i],
                    data['starting_point'][i], instance['img_height'],
                    instance['img_width'])

                sampling_masks.append(mask)

            # Get IoUs
            sampling_ious = np.zeros((len(gt_masks)), dtype=np.float32)

            for i, gt_mask in enumerate(gt_masks):
                sampling_ious[i] = metrics.iou_from_mask(
                    sampling_masks[i], gt_mask)

            # Get Loss
            total_loss = losses.evaluator_loss(
                output['ious'],
                torch.from_numpy(sampling_ious).to(device))

            # Backward pass
            self.optimizer.zero_grad()
            total_loss.backward()

            if 'grad_clip' in self.opts.keys():
                nn.utils.clip_grad_norm_(self.model.parameters(),
                                         self.opts['grad_clip'])

            self.optimizer.step()

            accum['loss'] += float(total_loss)
            accum['sampling_iou'] += np.mean(sampling_ious)
            accum['pred_iou'] += torch.mean(output['ious'])
            accum['length'] += 1

            if step % self.opts['print_freq'] == 0:
                # Mean of accumulated values
                for k in accum.keys():
                    if k == 'length':
                        continue
                    accum[k] /= accum['length']

                # Add summaries
                for k in accum.keys():
                    if k == 'length':
                        continue
                    self.writer.add_scalar(k, accum[k], self.global_step)

                print("[%s] Epoch: %d, Step: %d, Loss: %f, Sampling IOU: %f, Pred IOU: %f"\
                %(str(datetime.now()), epoch, self.global_step, accum['loss'], accum['sampling_iou'], accum['pred_iou']))

                accum = defaultdict(float)

            del (output)

            self.global_step += 1
    def process_outputs(self, data, output, save=True):
        """
        Process outputs to get final outputs for the whole image
        Optionally saves the outputs to a folder for evaluation
        """
        instances = data['instance']

        pred_spline = output['pred_polys']  # (16, 40, 2)
        pred_gcn = output['gcn_layer']  # (16, 40, 2)

        ## overlay pred_spline to 224*224 image

        # origin_img = output['origin_img']
        # img = torch.squeeze(origin_img)
        # img = data['img']
        # img = torch.squeeze(img)
        # img = img.cpu().numpy().transpose(1, 2, 0)  # (224, 224, 3)
        # img = (img-img.min())/(img.max()-img.min()) # normalize to 0~1
        # img_save = np.uint8(255 * img)
        # import os
        # # len_th = len(os.listdir(""))
        # imsave("" + str(instances[0]['img_path'][-17:-4]) + '.png', img_save, 'PNG')
        # len_th = len(os.listdir(""))
        #
        # instance_id = instances[0]['img_path'][-17:-4]
        #
        # for i in range(len(output['pred_polys_all'])):
        #     pred_spline_numpy = torch.squeeze(output['pred_polys_all'][i]).cpu().numpy() # (cp_num,2)
        #     to_axis = (pred_spline_numpy * 224).astype(np.int32)
        #     CAM = np.zeros((224, 224))
        #     for index, item in enumerate(to_axis):
        #         CAM[item[1] - 2, item[0] - 2] = 1
        #         CAM[item[1] - 2, item[0] - 1] = 1
        #         CAM[item[1] - 1, item[0] - 2] = 1
        #         CAM[item[1] - 1, item[0] - 1] = 1  # top-left
        #         CAM[item[1] - 2, item[0]] = 1
        #         CAM[item[1] - 1, item[0]] = 1  # top
        #         CAM[item[1] - 2, item[0] + 1] = 1
        #         CAM[item[1] - 2, item[0] + 2] = 1
        #         CAM[item[1] - 1, item[0] + 2] = 1
        #         CAM[item[1] - 1, item[0] + 1] = 1  # top-right
        #         CAM[item[1], item[0] - 2] = 1
        #         CAM[item[1], item[0] - 1] = 1  # left
        #         CAM[item[1], item[0] + 2] = 1
        #         CAM[item[1], item[0] + 1] = 1  # right
        #         CAM[item[1] + 1, item[0] - 2] = 1
        #         CAM[item[1] + 2, item[0] - 1] = 1
        #         CAM[item[1] + 2, item[0] - 2] = 1
        #         CAM[item[1] + 1, item[0] - 1] = 1  # bottom-left
        #         CAM[item[1] + 2, item[0]] = 1
        #         CAM[item[1] + 1, item[0]] = 1  # bottom
        #         CAM[item[1] + 1, item[0] + 2] = 1
        #         CAM[item[1] + 2, item[0] + 2] = 1
        #         CAM[item[1] + 2, item[0] + 1] = 1
        #         CAM[item[1] + 1, item[0] + 1] = 1
        #         CAM[item[1], item[0]] = 1
        #     import cv2
        #     heatmap = cv2.applyColorMap(np.uint8(255 * CAM), cv2.COLORMAP_JET)
        #     heatmap = np.float32(heatmap) / 255
        #     # cam = heatmap + np.float32(torch.squeeze(img).permute(1,2,0).detach().numpy())
        #     # cam = heatmap + np.float32(torch.squeeze(img).permute(1,2,0).detach().numpy())
        #     cam = 0.7*heatmap + np.float32(img) * 0.4   #0.5 should be smaller
        #     cam = cam / np.max(cam)
        #     import os
        #
        #     cv2.imwrite("" + str(instance_id) + "_" + str(i) + ".jpg", np.uint8(255 * cam))
        #     #cv2.imwrite("" + str(i) + "gray_pre" + str(len_th) + ".jpg", np.uint8(255 * CAM))

        # preds = self.spline.sample_point(pred_spline) # (16, 1280, 2)
        preds = pred_spline
        preds_numpy = preds.cpu().numpy()
        torch.cuda.synchronize()
        preds = preds.cpu().numpy()

        pred_spline = pred_spline.cpu()
        pred_spline = pred_spline.numpy()

        ## overlay pre_polygons on croped img
        # img_save = np.uint8(img)
        # pil_img = Image.fromarray(img_save, mode='RGB')
        # img_draw = ImageDraw.Draw(pil_img, 'RGBA')
        # # img_draw.polygon(polys_pred, outline='red') #pred polygons
        # # img_draw.polygon(polys_gt, outline='blue')  #GT polygons
        # predicted_poly = []
        # poly = preds[0]
        # poly = poly * 224#data['patch_w'][0] #to (224, 224)
        # # poly[:, 0] += data['starting_point'][0][0]
        # # poly[:, 1] += data['starting_point'][0][1]
        # predicted_poly.append(poly.tolist())
        # polys_pre = predicted_poly[0]
        # polys_pre_final = [tuple(item) for item in polys_pre]
        # polys_gt = instances[0]['components'][0]['poly']
        # polys_gt_final = [tuple(((item[0]-data['starting_point'][0][0])*224/data['patch_w'][0],(item[1]-data['starting_point'][0][1])*224/data['patch_w'][0])) for item in polys_gt]
        #
        # # draw circle
        # #img_draw = ImageDraw.Draw(img_ob, 'RGBA')
        # img_draw.polygon(polys_pre_final, outline=(255, 0, 0))  # pred polygons
        # #img_draw.polygon(polys_gt_final, outline=(0, 0, 255))
        #
        # ## width be bigger
        # polys_pre_final = [tuple((item[0]-1, item[1]-1)) for item in polys_pre]
        # polys_gt = instances[0]['components'][0]['poly']
        # polys_gt_final = [tuple(((item[0] - data['starting_point'][0][0]) * 224 / data['patch_w'][0]-1,
        #                          (item[1] - data['starting_point'][0][1]) * 224 / data['patch_w'][0]-1)) for item in
        #                   polys_gt]
        # img_draw.polygon(polys_pre_final, outline=(255, 0, 0))  # pred polygons
        # #img_draw.polygon(polys_gt_final, outline=(0, 0, 255))
        #
        # polys_pre_final = [tuple((item[0] + 1, item[1] + 1)) for item in polys_pre]
        # polys_gt = instances[0]['components'][0]['poly']
        # polys_gt_final = [tuple(((item[0] - data['starting_point'][0][0]) * 224 / data['patch_w'][0] + 1,
        #                          (item[1] - data['starting_point'][0][1]) * 224 / data['patch_w'][0] + 1)) for item in
        #                   polys_gt]
        # img_draw.polygon(polys_pre_final, outline=(255, 0, 0))  # pred polygons
        # #img_draw.polygon(polys_gt_final, outline=(0, 0, 255))
        # ##top
        # polys_pre_final = [tuple((item[0], item[1]- 1)) for item in polys_pre]
        # polys_gt = instances[0]['components'][0]['poly']
        # polys_gt_final = [tuple(((item[0] - data['starting_point'][0][0]) * 224 / data['patch_w'][0],
        #                          (item[1] - data['starting_point'][0][1]) * 224 / data['patch_w'][0] - 1)) for item in
        #                   polys_gt]
        # img_draw.polygon(polys_pre_final, outline=(255, 0, 0))  # pred polygons
        # #img_draw.polygon(polys_gt_final, outline=(0, 0, 255))
        # ##bottom
        # polys_pre_final = [tuple((item[0], item[1]+ 1)) for item in polys_pre]
        # polys_gt = instances[0]['components'][0]['poly']
        # polys_gt_final = [tuple(((item[0] - data['starting_point'][0][0]) * 224 / data['patch_w'][0],
        #                          (item[1] - data['starting_point'][0][1]) * 224 / data['patch_w'][0] + 1)) for item in
        #                   polys_gt]
        # img_draw.polygon(polys_pre_final, outline=(255, 0, 0))  # pred polygons
        # #img_draw.polygon(polys_gt_final, outline=(0, 0, 255))
        # ## top-right
        # polys_pre_final = [tuple((item[0]+1, item[1] - 1)) for item in polys_pre]
        # polys_gt = instances[0]['components'][0]['poly']
        # polys_gt_final = [tuple(((item[0] - data['starting_point'][0][0]) * 224 / data['patch_w'][0]+1,
        #                          (item[1] - data['starting_point'][0][1]) * 224 / data['patch_w'][0] - 1)) for item in
        #                   polys_gt]
        # img_draw.polygon(polys_pre_final, outline=(255, 0, 0))  # pred polygons
        # #img_draw.polygon(polys_gt_final, outline=(0, 0, 255))
        # ## bottom-left
        # polys_pre_final = [tuple((item[0] - 1, item[1] + 1)) for item in polys_pre]
        # polys_gt = instances[0]['components'][0]['poly']
        # polys_gt_final = [tuple(((item[0] - data['starting_point'][0][0]) * 224 / data['patch_w'][0] - 1,
        #                          (item[1] - data['starting_point'][0][1]) * 224 / data['patch_w'][0] + 1)) for item in
        #                   polys_gt]
        # img_draw.polygon(polys_pre_final, outline=(255, 0, 0))  # pred polygons
        # #img_draw.polygon(polys_gt_final, outline=(0, 0, 255))
        # ## left
        # polys_pre_final = [tuple((item[0] - 1, item[1])) for item in polys_pre]
        # polys_gt = instances[0]['components'][0]['poly']
        # polys_gt_final = [tuple(((item[0] - data['starting_point'][0][0]) * 224 / data['patch_w'][0] - 1,
        #                          (item[1] - data['starting_point'][0][1]) * 224 / data['patch_w'][0])) for item in
        #                   polys_gt]
        # img_draw.polygon(polys_pre_final, outline=(255, 0, 0))  # pred polygons
        # #img_draw.polygon(polys_gt_final, outline=(0, 0, 255))
        # ## right
        # polys_pre_final = [tuple((item[0] + 1, item[1])) for item in polys_pre]
        # polys_gt = instances[0]['components'][0]['poly']
        # polys_gt_final = [tuple(((item[0] - data['starting_point'][0][0]) * 224 / data['patch_w'][0] + 1,
        #                          (item[1] - data['starting_point'][0][1]) * 224 / data['patch_w'][0])) for item in
        #                   polys_gt]
        # img_draw.polygon(polys_pre_final, outline=(255, 0, 0))  # pred polygons

        ## bellow code for draw vertexPoly on croped img
        # img_draw.polygon(polys_pre_final, outline=(255, 193, 37))
        # imsave_ob = np.array(pil_img).astype(np.uint8)
        # for index, item in enumerate(polys_pre_final):
        #     if index % 32 == 0:
        #         #cv2.circle(imsave_ob, (int(item[0]),int(item[1])),2,(0,255,255),-1)
        #         rr,cc = draw.circle(item[1],item[0],2)
        #         draw.set_color(imsave_ob, [rr,cc], [0,255,255])
        # pil_img = Image.fromarray(imsave_ob, mode='RGB')
        # pil_img.save(''+str(instances[0]['img_path'][-17:-4])+'.png', 'PNG')

        # img_ob.save(imgpath[:-56] + '' + imgpath[-17:-4] + '_overlay_pre_gt.png','PNG')
        # pil_img.save(imgpath[:-56] + "es" + '/pre_overlay/' + imgpath[-29:-4] + '_overlay_pre_gt.png','PNG')

        polys = []
        for i, instance in enumerate(instances):
            poly = preds[i]
            poly = poly * data['patch_w'][i]  # to (224 224)
            poly[:, 0] += data['starting_point'][i][0]
            poly[:, 1] += data['starting_point'][i][1]

            pred_sp = pred_spline[i]
            pred_sp = pred_sp * data['patch_w'][i]
            pred_sp[:, 0] += data['starting_point'][i][0]
            pred_sp[:, 1] += data['starting_point'][i][1]

            instance['spline_pos'] = pred_sp.tolist()

            polys.append(poly)

            if save:
                img_h, img_w = instance['img_height'], instance['img_width']
                predicted_poly = []

                pred_mask = np.zeros((img_h, img_w), dtype=np.uint8)
                utils.draw_poly(pred_mask, poly.astype(np.int))
                predicted_poly.append(poly.tolist())

                ## overlap pred_poly with original image
                img_path = str(instance['img_path'])
                # get gt_polys
                # polys_gt = data['instance'][0]['components'][0]['poly']
                polys_gt = instance['components'][0]['poly']
                # get polys_pre
                polys_pre = predicted_poly[0]
                polys_gt_final = [tuple(item) for item in polys_gt]
                polys_pre_final = [tuple(item) for item in polys_pre]
                if len(polys_pre_final) == 1:
                    print('error!! ', img_path[10:])
                    print('before optimize is: ', output['pred_polys'][i])
                ## <<overlay gt_pre>>
                else:
                    pass
                    ##for 30 test volumes
                    #utils.overlap_pre_gt('/home/lxj/work_station/' + img_path[10:], polys_pre_final, polys_gt_final)
                    ## for 5-cross-val experiments
                    #utils.overlap_pre_gt('/home/lxj/work_station/' + img_path[25:], polys_pre_final, polys_gt_final)
                    # utils.overlap_pre_gt('/home/lxj/' + img_path[10:], polys_pre_final, polys_gt_final) # only for citiscapse

                gt_mask = utils.get_full_mask_from_instance(
                    self.opts['dataset']['train_val']['min_area'],
                    instance)

                instance['my_predicted_poly'] = predicted_poly
                instance_id = instance['img_path'][-17:-4]
                # image_id = instance['image_id']

                pred_mask_fname = os.path.join(self.output_dir, '{}_pred.png'.format(instance_id))
                instance['pred_mask_fname'] = os.path.relpath(pred_mask_fname, self.output_dir)

                gt_mask_fname = os.path.join(self.output_dir, '{}_gt.png'.format(instance_id))
                instance['gt_mask_fname'] = os.path.relpath(gt_mask_fname, self.output_dir)

                instance['n_corrections'] = 0

                info_fname = os.path.join(self.output_dir, '{}_info.json'.format(instance_id))

                with warnings.catch_warnings():
                    warnings.simplefilter("ignore")
                    sio.imsave(pred_mask_fname, pred_mask)
                    sio.imsave(gt_mask_fname, gt_mask)

                # print '==> dumping json'
                with open(info_fname, 'w') as f:
                    json.dump(instance, f, indent=2)

        return polys
Exemple #5
0
    def train(self, epoch):
        print 'Starting training'

        accum = defaultdict(float)
        # To accumulate stats for printing

        for step, data in enumerate(self.train_loader):
            if self.global_step % self.opts['val_freq'] == 0:
                self.validate()
                self.save_checkpoint(epoch)

            # Forward pass (Sampling)
            self.model.temperature = self.opts['temperature']
            output = self.model(data['img'].to(device))

            # Forward pass (Greedy)
            self.model.temperature = 0.0
            with torch.no_grad():
                output_greedy = self.model(data['img'].to(device))

            # Get full GT masks
            gt_masks = []
            for instance in data['instance']:
                gt_masks.append(
                    utils.get_full_mask_from_instance(
                        self.opts['dataset']['train_val']['min_area'],
                        instance))

            # Get sampling masks
            sampling_masks = []
            pred_polys = output['pred_polys'].cpu().numpy()
            for i in range(pred_polys.shape[0]):
                poly = pred_polys[i]
                mask, poly = utils.get_full_mask_from_xy(
                    poly, self.grid_size, data['patch_w'][i],
                    data['starting_point'][i], instance['img_height'],
                    instance['img_width'])

                sampling_masks.append(mask)

            # Get greedy masks
            greedy_masks = []
            pred_polys = output_greedy['pred_polys'].cpu().numpy()
            for i in range(pred_polys.shape[0]):
                poly = pred_polys[i]
                mask, poly = utils.get_full_mask_from_xy(
                    poly, self.grid_size, data['patch_w'][i],
                    data['starting_point'][i], instance['img_height'],
                    instance['img_width'])

                greedy_masks.append(mask)

            # Get IoUs
            sampling_ious = np.zeros((len(gt_masks)), dtype=np.float32)
            greedy_ious = np.zeros((len(gt_masks)), dtype=np.float32)

            for i, gt_mask in enumerate(gt_masks):
                sampling_ious[i] = metrics.iou_from_mask(
                    sampling_masks[i], gt_mask)
                greedy_ious[i] = metrics.iou_from_mask(greedy_masks[i],
                                                       gt_mask)

            # Get Loss
            total_loss = losses.self_critical_loss(
                output['log_probs'], output['lengths'],
                torch.from_numpy(sampling_ious).to(device),
                torch.from_numpy(greedy_ious).to(device))

            # Backward pass
            self.optimizer.zero_grad()
            total_loss.backward()

            if 'grad_clip' in self.opts.keys():
                nn.utils.clip_grad_norm_(self.model.parameters(),
                                         self.opts['grad_clip'])

            self.optimizer.step()

            accum['loss'] += float(total_loss)
            accum['sampling_iou'] += np.mean(sampling_ious)
            accum['greedy_iou'] += np.mean(greedy_ious)
            accum['length'] += 1

            if step % self.opts['print_freq'] == 0:
                # Mean of accumulated values
                for k in accum.keys():
                    if k == 'length':
                        continue
                    accum[k] /= accum['length']

                # Add summaries
                img = (data['img'].cpu().numpy()[-1, ...] * 255).astype(
                    np.uint8)
                img = np.transpose(img, [1, 2, 0])  # Make [H, W, 3]
                vert_logits = np.reshape(
                    output['vertex_logits'][-1, ...].detach().cpu().numpy(),
                    (self.grid_size, self.grid_size, 1))
                edge_logits = np.reshape(
                    output['edge_logits'][-1, ...].detach().cpu().numpy(),
                    (self.grid_size, self.grid_size, 1))
                vert_logits = (1 / (1 + np.exp(-vert_logits)) * 255).astype(
                    np.uint8)
                edge_logits = (1 / (1 + np.exp(-edge_logits)) * 255).astype(
                    np.uint8)
                vert_logits = np.tile(vert_logits, [1, 1, 3])  # Make [H, W, 3]
                edge_logits = np.tile(edge_logits, [1, 1, 3])  # Make [H, W, 3]
                vertex_mask = np.tile(
                    np.expand_dims(
                        data['vertex_mask'][-1, ...].cpu().numpy().astype(
                            np.uint8) * 255, -1), (1, 1, 3))
                edge_mask = np.tile(
                    np.expand_dims(
                        data['edge_mask'][-1, ...].cpu().numpy().astype(
                            np.uint8) * 255, -1), (1, 1, 3))

                self.writer.add_image('image', img, self.global_step)
                self.writer.add_image('vertex_logits', vert_logits,
                                      self.global_step)
                self.writer.add_image('edge_logits', edge_logits,
                                      self.global_step)
                self.writer.add_image('edge_mask', edge_mask, self.global_step)
                self.writer.add_image('vertex_mask', vertex_mask,
                                      self.global_step)

                if self.opts['return_attention'] is True:
                    att = output['attention'][-1, 1:4,
                                              ...].detach().cpu().numpy()
                    att = np.transpose(att, [0, 2, 3, 1])  # Make [T, H, W, 1]
                    att = np.tile(att, [1, 1, 1, 3])  # Make [T, H, W, 3]

                    def _scale(att):
                        att = att / np.max(att)
                        return (att * 255).astype(np.int32)

                    self.writer.add_image(
                        'attention_1',
                        pyramid_expand(_scale(att[0]), upscale=8, sigma=10),
                        self.global_step)
                    self.writer.add_image(
                        'attention_2',
                        pyramid_expand(_scale(att[1]), upscale=8, sigma=10),
                        self.global_step)
                    self.writer.add_image(
                        'attention_3',
                        pyramid_expand(_scale(att[2]), upscale=8, sigma=10),
                        self.global_step)

                for k in accum.keys():
                    if k == 'length':
                        continue
                    self.writer.add_scalar(k, accum[k], self.global_step)

                print("[%s] Epoch: %d, Step: %d, Polygon Loss: %f, Sampling IOU: %f, Greedy IOU: %f"\
                %(str(datetime.now()), epoch, self.global_step, accum['loss'], accum['sampling_iou'], accum['greedy_iou']))

                accum = defaultdict(float)

            del (output)
            del (output_greedy)

            self.global_step += 1
    def process_outputs(self, data, output, save=True):
        """
        Process outputs to get final outputs for the whole image
        Optionally saves the outputs to a folder for evaluation
        """

        #pred_spline = output['pred_polys']
        pred_spline = output['pred_polys']
        preds = self.spline.sample_point(pred_spline)
        torch.cuda.synchronize()
        preds = preds.cpu().numpy()

        pred_spline = pred_spline.cpu()
        pred_spline = pred_spline.numpy()

        instances = data['instance']
        polys = []
        for i, instance in enumerate(instances):
            poly = preds[i]
            poly = poly * data['patch_w'][i]
            poly[:, 0] += data['starting_point'][i][0]
            poly[:, 1] += data['starting_point'][i][1]


            pred_sp = pred_spline[i]
            pred_sp = pred_sp * data['patch_w'][i]
            pred_sp[:, 0] += data['starting_point'][i][0]
            pred_sp[:, 1] += data['starting_point'][i][1]

            instance['spline_pos'] = pred_sp.tolist()

            polys.append(poly)

            if save:
                img_h, img_w = instance['img_height'], instance['img_width']
                predicted_poly = []

                pred_mask = np.zeros((img_h, img_w), dtype=np.uint8)
                utils.draw_poly(pred_mask, poly.astype(np.int))
                #utils.get_edge_mask(poly.astype(np.int),pred_mask)
                #utils.get_vertices_mask(poly.astype(np.int),pred_mask)
                predicted_poly.append(poly.tolist())

                gt_mask = utils.get_full_mask_from_instance(
                    self.opts['dataset']['train_val']['min_area'],
                    instance)

                instance['my_predicted_poly'] = predicted_poly
                instance_id = instance['instance_id']
                image_id = instance['image_id']

                pred_mask_fname = os.path.join(self.output_dir, '{}_pred.png'.format(instance_id))
                instance['pred_mask_fname'] = os.path.relpath(pred_mask_fname, self.output_dir)

                gt_mask_fname = os.path.join(self.output_dir, '{}_gt.png'.format(instance_id))
                instance['gt_mask_fname'] = os.path.relpath(gt_mask_fname, self.output_dir)

                instance['n_corrections'] = 0

                info_fname = os.path.join(self.output_dir, '{}_info.json'.format(instance_id))

                with warnings.catch_warnings():
                    warnings.simplefilter("ignore")
                    sio.imsave(pred_mask_fname, pred_mask)
                    sio.imsave(gt_mask_fname, gt_mask)

                # print '==> dumping json'
                with open(info_fname, 'w') as f:
                    json.dump(instance, f, indent=2)

        return polys
Exemple #7
0
    def process_outputs(self, data, output, save=True):
        """
        Process outputs to get final outputs for the whole image
        Optionally saves the outputs to a folder for evaluation
        """
        instances = data['instance']
        polys = []
        for i, instance in enumerate(instances):
            # Postprocess polygon
            poly = output['pred_polys'][i]

            _, poly = utils.get_full_mask_from_xy(poly, self.grid_size,
                                                  data['patch_w'][i],
                                                  data['starting_point'][i],
                                                  instance['img_height'],
                                                  instance['img_width'])

            polys.append(poly)

            if save:
                img_h, img_w = instance['img_height'], instance['img_width']
                predicted_poly = []

                # Paint pred mask
                pred_mask = np.zeros((img_h, img_w), dtype=np.uint8)
                poly = poly.astype(np.int)
                utils.draw_poly(pred_mask, poly)
                predicted_poly.append(poly.tolist())

                # Paint GT mask
                gt_mask = utils.get_full_mask_from_instance(
                    self.opts['dataset']['train_val']['min_area'], instance)

                instance['my_predicted_poly'] = predicted_poly
                instance_id = instance['instance_id']
                image_id = instance['image_id']

                pred_mask_fname = os.path.join(
                    self.output_dir, '{}_pred.png'.format(instance_id))
                instance['pred_mask_fname'] = os.path.relpath(
                    pred_mask_fname, self.output_dir)

                gt_mask_fname = os.path.join(self.output_dir,
                                             '{}_gt.png'.format(instance_id))
                instance['gt_mask_fname'] = os.path.relpath(
                    gt_mask_fname, self.output_dir)

                instance['n_corrections'] = 0

                info_fname = os.path.join(self.output_dir,
                                          '{}_info.json'.format(instance_id))

                with warnings.catch_warnings():
                    warnings.simplefilter("ignore")
                    sio.imsave(pred_mask_fname, pred_mask)
                    sio.imsave(gt_mask_fname, gt_mask)

                with open(info_fname, 'w') as f:
                    json.dump(instance, f, indent=2)

        return polys