Exemple #1
0
def compute_area_list(x_fake, given_y, nd_to_sample, im_size=256):
    maps_batch = x_fake.detach().cpu().numpy()
    nodes_batch = given_y.detach().cpu().numpy()
    batch_size = torch.max(nd_to_sample) + 1
    rooms_areas = {}
    for b in range(batch_size):
        inds_nd = np.where(nd_to_sample == b)  #b ~ b_index #根据坐标获取位置
        mks = maps_batch[inds_nd]
        nds = nodes_batch[inds_nd]
        i = 0
        for mk, nd in zip(mks, nds):
            room_type = str(np.where(nd > 0)[0][0])
            x0, y0, x1, y1 = np.array(mask_to_bb(mk))
            area = compute_area(x_fake[inds_nd][i], [x0, y0, x1 - 1, y1 - 1])
            # if area > 0 :
            #     print([x0, y0, x1-1, y1-1])
            #     np.save("./tracking/debug_fake.npy",x_fake[inds_nd][i].detach().numpy())
            #     exit()
            if area < 0:
                area = abs(area * 0)
            area_rate = area / (mk.shape[-1] * mk.shape[-1])

            i += 1
            if room_type not in rooms_areas.keys():
                rooms_areas[room_type] = [area_rate]
                continue
            rooms_areas[room_type].append(area_rate)
    return rooms_areas
Exemple #2
0
def compute_iou_list_v1(x_fake,
                        given_w,
                        nd_to_sample,
                        ed_to_sample,
                        tag='fake',
                        im_size=256):
    maps_batch = x_fake.detach().cpu().numpy()
    edges_batch = given_w.detach().cpu().numpy()
    batch_size = torch.max(nd_to_sample) + 1
    np.seterr(divide='ignore', invalid='ignore')
    pos = 0
    iou_pos = {}
    iou_neg = {}
    iou_invalid = {}
    iou_dict = {}
    for b in range(batch_size):
        inds_nd = np.where(nd_to_sample == b)  #b ~ b_index #根据坐标获取位置
        inds_ed = np.where(ed_to_sample == b)

        mks = maps_batch[inds_nd]
        eds = edges_batch[inds_ed]

        rooms_axes = []
        for mk in mks:
            r = im_size / mk.shape[-1]
            x0, y0, x1, y1 = np.array(mask_to_bb(mk)) * r
            rooms_axes.append([x0, y0, x1, y1])

        rooms_cnt = len(rooms_axes)

        for i in range(rooms_cnt):
            axes = rooms_axes[i]
            j = i + 1
            for j in range(j, rooms_cnt):
                room_cmp = rooms_axes[j]
                axes_c = room_cmp
                a_box = list(axes)
                b_box = list(axes_c)
                iou, giou, center_inter, margin_inter = GIOU_v1(a_box, b_box)
                key = str(i + pos) + '_' + str(j + pos)
                iou_dict[key] = [iou, giou, center_inter, margin_inter]

        for ed in eds:
            s, w, d = ed
            key = str(s) + '_' + str(d)
            if w == 1:
                iou_pos[key] = iou_dict[key]
            else:
                iou_neg[key] = iou_dict[key]

        for k, v in iou_dict.items():
            if k in iou_pos.keys() or k in iou_neg.keys():
                continue
            iou_invalid[k] = v
        pos += rooms_cnt

    return iou_pos, iou_neg, iou_invalid
Exemple #3
0
def compute_iou_list_v2(masks,
                        given_w,
                        nd_to_sample,
                        ed_to_sample,
                        tag='fake',
                        im_size=256):
    maps_batch = masks.detach().cpu().numpy()
    edges_batch = given_w.detach().cpu().numpy()
    batch_size = torch.max(nd_to_sample) + 1
    pos = 0
    iou_pos = {}
    iou_neg = {}
    iou_invalid = {}
    iou_dict = {}
    mks_idx = 0
    for b in range(batch_size):
        inds_nd = np.where(nd_to_sample == b)  #b ~ b_index #根据坐标获取位置
        inds_ed = np.where(ed_to_sample == b)
        mks = maps_batch[inds_nd]
        eds = edges_batch[inds_ed]

        rooms_axes = []
        for mk in mks:
            x0, y0, x1, y1 = mask_to_bb(mk)
            rooms_axes.append([x0, y0, x1, y1, masks[mks_idx]])
            mks_idx += 1

        rooms_cnt = len(rooms_axes)

        for i in range(rooms_cnt):
            axes = rooms_axes[i]
            j = i + 1
            for j in range(j, rooms_cnt):
                room_cmp = rooms_axes[j]
                axes_c = room_cmp
                iou, giou, center_inter, margin_inter = GIOU_v2(axes, axes_c)
                #iou_c ,giou_c ,center_inter_c ,margin_inter_c = GIOU_v1(axes,axes_c) #debug

                key = str(i + pos) + '_' + str(j + pos)
                iou_dict[key] = [iou, giou, center_inter, margin_inter]
                #,[iou_c ,giou_c ,center_inter_c ,margin_inter_c]]#debug

        for ed in eds:
            s, w, d = ed
            key = str(s) + '_' + str(d)
            if w == 1:
                iou_pos[key] = iou_dict[key]
            else:
                iou_neg[key] = iou_dict[key]

        for k, v in iou_dict.items():
            if k in iou_pos.keys() or k in iou_neg.keys():
                continue
            iou_invalid[k] = v
        pos += rooms_cnt

    return iou_pos, iou_neg, iou_invalid
Exemple #4
0
def compute_sparsity_penalty_v1(masks, nd_to_sample, criterion):
    ret_tensor = torch.zeros(masks.shape[0])
    maps_batch = masks.detach().cpu().numpy()
    batch_size = torch.max(nd_to_sample) + 1
    ret = []
    mks_idx = 0
    for b in range(batch_size):
        inds_nd = np.where(nd_to_sample == b)  #b ~ b_index #根据坐标获取位置
        mks = maps_batch[inds_nd]

        for mk in mks:
            new_mask = torch.zeros(mk.shape)
            mk_np = np.around(mk, decimals=1)  #精度太低导致弱值发散
            m_x0, m_y0, m_x1, m_y1 = mask_to_bb(mk)
            if [m_x0, m_y0, m_x1, m_y1] == [0, 0, 0, 0]:
                ret_tensor[mks_idx] = torch.tensor(0.)
                mks_idx += 1
                continue

            if mk_np[m_y0:m_y1,
                     m_x0:m_x1][mk_np[m_y0:m_y1, m_x0:m_x1] > 0].size == 0:
                ret_tensor[mks_idx] = torch.tensor(0.)
                mks_idx += 1
                continue
            avg_np = mk_np[m_y0:m_y1,
                           m_x0:m_x1][mk_np[m_y0:m_y1, m_x0:m_x1] > 0].mean()
            #avg = mk[fy1:fy2,fx1:fx2][mk[fy1:fy2,fx1:fx2]>0].mean()
            sp_d = avg_np  ### 对于gen_mk[0] 还是存在问题
            ####max list center axes######
            x_max_l, y_max_l = np.where(mk_np == np.max(mk_np))
            x_max = np.median(x_max_l)
            y_max = np.median(y_max_l)
            ############get noising area########################
            ####step 1 get pos area masking ################
            mk_peak_area = (mk - sp_d)
            mk[mk_peak_area > 0] = 0.
            noise_x_axis, noise_y_axis = np.array(np.where((mk > 0)))
            for idx, v in enumerate(noise_x_axis):
                dist = math.sqrt(
                    abs(v - x_max)**2 + abs(noise_y_axis[idx] - y_max)**2)
                new_mask[v][noise_y_axis[idx]] = masks[mks_idx][v][
                    noise_y_axis[idx]] * dist
            penalty_sum = torch.sum(new_mask[new_mask > 0])
            penalty_rate = ((masks[mks_idx][m_y0:m_y1, m_x0:m_x1] < 0).nonzero(
                as_tuple=False).shape[0] / abs((m_y1 - m_y0) * (m_x1 - m_x0)))
            penalty = penalty_rate * penalty_sum
            ret_tensor[mks_idx] = penalty
            mks_idx += 1

    object_ = torch.zeros(ret_tensor.shape[-1])
    #return criterion(ret_tensor,object_)
    return ret_tensor.norm(p=1)
Exemple #5
0
def transfer_edges(rooms_mks, nodes, edges, im_size=256):
    edges = edges.to(torch.float32)
    room_axes = {}
    for i, mk in enumerate(rooms_mks):
        room_type = np.where(nodes[i] == 1)[0][0]
        r = im_size / mk.shape[-1]
        room_axes[room_type] = np.array(mask_to_bb(mk)) * r

    for i, ed in enumerate(edges):
        if (int(ed[0]) in room_axes.keys()) and (int(ed[2])
                                                 in room_axes.keys()):
            box1 = room_axes[int(ed[0])]
            box2 = room_axes[int(ed[2])]
            iou, Giou = GIOU(np.array([box1]), np.array([box2]))
            edges[i][1] = torch.tensor(Giou[0], dtype=torch.float32)[0]

    return edges
Exemple #6
0
def compute_sparsity_penalty(masks, given_w, nd_to_sample, criterion):
    maps_batch = masks.detach().cpu().numpy()
    edges_batch = given_w.detach().cpu().numpy()
    batch_size = torch.max(nd_to_sample) + 1
    ret = []
    mks_idx = 0
    for b in range(batch_size):
        inds_nd = np.where(nd_to_sample == b)  #b ~ b_index #根据坐标获取位置
        mks = maps_batch[inds_nd]

        rooms_axes = []
        for mk in mks:
            x0, y0, x1, y1 = mask_to_bb(mk)
            empty_area = compute_empty_area(masks[mks_idx], [x0, y0, x1, y1])
            mks_idx += 1
            ret.append(empty_area)
    ret_tensor = transfer_list_to_tensor(ret)
    object_ = torch.zeros(ret_tensor.shape[-1])
    return criterion(ret_tensor, object_)
    # Configure input
    real_mks = Variable(mks.type(Tensor))
    given_nds = Variable(nds.type(Tensor))
    given_eds = eds

    # Sample noise as generator input
    layouts_imgs_tensor = []

    # reconstruct
    z_shape = [real_mks.shape[0], opt.latent_dim]
    z = Variable(Tensor(np.random.normal(0, 1, tuple(z_shape))))

    with torch.no_grad():
        gen_mks = generator(z, given_nds, given_eds)
        gen_bbs = np.array(
            [np.array(mask_to_bb(mk))
             for mk in gen_mks.detach().cpu()]) / float(gen_mks.shape[-1])
        real_bbs = np.array(
            [np.array(mask_to_bb(mk)) for mk in real_mks.detach().cpu()])

    real_nodes = np.where(given_nds.detach().cpu() == 1)[-1]
    g_pred = retrieve_connections(real_nodes, gen_bbs)
    g_true = [real_nodes, eds.detach().cpu().numpy()]

    # build predicted graph
    G_pred = nx.Graph()
    colors_G = []

    for k, label in enumerate(g_pred[0]):
        _type = label + 1
        G_pred.add_nodes_from([(k, {'label': _type})])
     break
     
 # Unpack batch
 mks, nds, eds, nd_to_sample, ed_to_sample = batch
 
 # Configure input
 real_mks = Variable(mks.type(Tensor))
 given_nds = Variable(nds.type(Tensor))
 given_eds = eds
 for k in range(opt.num_variations):
     print('var num {}'.format(k))
     # plot images
     z = Variable(Tensor(np.random.normal(0, 1, (real_mks.shape[0], opt.latent_dim))))
     with torch.no_grad():
         gen_mks = generator(z, given_nds, given_eds)
         gen_bbs = np.array([np.array(mask_to_bb(mk)) for mk in gen_mks.detach().cpu()])
         real_bbs = np.array([np.array(mask_to_bb(mk)) for mk in real_mks.detach().cpu()])
         real_nodes = np.where(given_nds.detach().cpu()==1)[-1]
     gen_bbs = gen_bbs[np.newaxis, :, :]/32.0
     junctions = np.array(bb_to_vec(gen_bbs))[0, :, :]
     regions = np.array(bb_to_seg(gen_bbs))[0, :, :, :].transpose((1, 2, 0))
     fake_imgs_tensor = bb_to_img(gen_bbs, [given_nds, given_eds], nd_to_sample, ed_to_sample, im_size=IM_SIZE)
     graph = [real_nodes, None]
     
     if k == 0:
         graph_arr = draw_graph([real_nodes, eds.detach().cpu().numpy()])
         print(graph_arr.shape)
         final_images.append(torch.tensor(graph_arr))
 
         # place real 
         real_bbs = real_bbs[np.newaxis, :, :]/32.0