Exemplo n.º 1
0
def epoch(n, i, mode):
    # mode (str): train or test
    batch_ind = np.arange(i, i + batch_size)
    batch = np.float32(images[:, :, :, batch_ind]) / 255
    #batch_mask = np.copy(masks[:, :, :, batch_ind])
    thisNames = building_names[batch_ind[0]]
    this_bb = np.copy(bounding_boxes[batch_ind[0]])
    base_name = thisNames.split('_')[0] + '_' + thisNames.split('_')[1]
    #thisGT = np.copy(GT[:, :, batch_ind[0]])
    thisDWT = np.copy(DWT[:, :, batch_ind[0]])
    # prediction_np = sess.run(prediction,feed_dict={x:batch})
    [mapE, mapA, mapB, mapK,
     l2] = sess.run([predE, predA, predB, predK, l2loss], feed_dict={x: batch})
    mapA = np.maximum(mapA, 0)
    mapB = np.maximum(mapB, 0)
    mapK = np.maximum(mapK, 0)

    for j in range(batch_size):
        init_snake = thisDWT
        snake, snake_hist = snake_process(mapE, mapA, mapB, mapK, init_snake)
        # Get last layer gradients
        M = mapE.shape[0]
        N = mapE.shape[1]
        der1, der2 = derivatives_poly(snake)

        #der1_GT, der2_GT = derivatives_poly(thisGT)

        #grads_arrayE = mapE * 0.001
        #grads_arrayA = mapA * 0.001
        #grads_arrayB = mapB * 0.001
        #grads_arrayK = mapK * 0.001
        #grads_arrayE[:, :, 0, 0] -= draw_poly(snake, 1, [M, N],12) - draw_poly(thisGT, 1, [M, N],12)
        #grads_arrayA[:, :, 0, 0] -= (np.mean(der1) - np.mean(der1_GT))
        #grads_arrayB[:, :, 0, 0] -= (draw_poly(snake, der2, [M, N],12) - draw_poly(thisGT, der2_GT, [M, N],12))
        #mask_gt = draw_poly_fill(thisGT, [M, N])
        #mask_snake = draw_poly_fill(snake, [M, N])
        #grads_arrayK[:, :, 0, 0] -= mask_gt - mask_snake

        #intersection = (mask_gt+mask_snake) == 2
        #union = (mask_gt + mask_snake) >= 1
        #iou = np.sum(intersection) / np.sum(union)

    if do_plot:
        plot_snakes(snake, snake_hist, None, mapE, mapA, mapB, mapK, \
                        None, None, None, None, batch, None)
    if do_write_results:
        if all_snakes.__contains__(base_name):
            all_snakes[base_name]['snakes'].append(
                np.copy(snake) * im_size / out_size)
            all_snakes[base_name]['init'].append(thisDWT * im_size / out_size)
            all_snakes[base_name]['bb'].append(this_bb)
        else:
            all_snakes[base_name] = {}
            all_snakes[base_name]['snakes'] = [
                np.copy(snake) * im_size / out_size
            ]
            all_snakes[base_name]['init'] = [thisDWT * im_size / out_size]
            all_snakes[base_name]['bb'] = [this_bb]

    return
Exemplo n.º 2
0
def epoch(n, i, mode):
    # mode (str): train or test
    batch_ind = np.arange(i, i + batch_size)
    batch = np.float32(images[:, :, :, batch_ind]) / 255
    batch_mask = np.copy(masks[:, :, :, batch_ind])
    thisGT = np.copy(GT[:, :, batch_ind[0]])
    thisDWT = np.copy(DWT[:, :, batch_ind[0]])
    if mode is 'train':
        ang = np.random.rand() * 360
        for j in range(len(batch_ind)):
            for b in range(batch.shape[2]):
                batch[:, :, b, j] = imrotate(batch[:, :, b, j], ang)
            batch_mask[:, :, 0, j] = imrotate(batch_mask[:, :, 0, j],
                                              ang,
                                              resample='nearest')
        R = [[np.cos(ang * np.pi / 180),
              np.sin(ang * np.pi / 180)],
             [-np.sin(ang * np.pi / 180),
              np.cos(ang * np.pi / 180)]]
        thisGT -= out_size / 2
        thisGT = np.matmul(thisGT, R)
        thisGT += out_size / 2
        thisDWT -= out_size / 2
        thisDWT = np.matmul(thisDWT, R)
        thisDWT += out_size / 2
        thisGT = np.minimum(thisGT, out_size - 1)
        thisGT = np.maximum(thisGT, 0)
        thisDWT = np.minimum(thisDWT, out_size - 1)
        thisDWT = np.maximum(thisDWT, 0)
    # prediction_np = sess.run(prediction,feed_dict={x:batch})
    [mapE, mapA, mapB, mapK,
     l2] = sess.run([predE, predA, predB, predK, l2loss], feed_dict={x: batch})

    if mode is 'train':
        for j in range(mapK.shape[3]):
            mapK[:, :, 0, j] -= batch_mask[:, :, 0, j] * 0.5 - 0.5 / 2
    mapA = np.maximum(mapA, 0)
    mapB = np.maximum(mapB, 0)
    mapK = np.maximum(mapK, 0)
    # Do snake inference
    for j in range(batch_size):
        init_snake = thisDWT
        snake, snake_hist = snake_process(mapE, mapA, mapB, mapK, init_snake)
        # Get last layer gradients
        M = mapE.shape[0]
        N = mapE.shape[1]
        der1, der2 = derivatives_poly(snake)

        der1_GT, der2_GT = derivatives_poly(thisGT)

        grads_arrayE = mapE * 0.001
        grads_arrayA = mapA * 0.001
        grads_arrayB = mapB * 0.001
        grads_arrayK = mapK * 0.001
        grads_arrayE[:, :, 0,
                     0] -= draw_poly(snake, 1, [M, N], 12) - draw_poly(
                         thisGT, 1, [M, N], 12)
        grads_arrayA[:, :, 0, 0] -= (np.mean(der1) - np.mean(der1_GT))
        grads_arrayB[:, :, 0, 0] -= (draw_poly(snake, der2, [M, N], 12) -
                                     draw_poly(thisGT, der2_GT, [M, N], 12))
        mask_gt = draw_poly_fill(thisGT, [M, N])
        mask_snake = draw_poly_fill(snake, [M, N])
        grads_arrayK[:, :, 0, 0] -= mask_gt - mask_snake

        intersection = (mask_gt + mask_snake) == 2
        union = (mask_gt + mask_snake) >= 1
        iou = np.sum(intersection) / np.sum(union)
    if mode is 'train':
        tic = time.time()
        apply_gradients.run(
            feed_dict={
                x: batch,
                grad_predE: grads_arrayE,
                grad_predA: grads_arrayA,
                grad_predB: grads_arrayB,
                grad_predK: grads_arrayK,
                grad_l2loss: 1
            })
        #print('%.2f' % (time.time() - tic) + ' s apply gradients')
        #print('IoU = %.2f' % (iou))
    #if mode is 'test':
    #print('IoU = %.2f' % (iou))
    if do_plot and n >= 1:
        plot_snakes(snake, snake_hist, thisGT, mapE, np.maximum(mapA, 0), np.maximum(mapB, 0), mapK, \
                grads_arrayE, grads_arrayA, grads_arrayB, grads_arrayK, batch, batch_mask)
        #plt.show()
    return iou