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(np.copy(images[:, :, :, batch_ind])) / 255
    batch_mask = np.copy(masks[:, :, :, batch_ind])
    thisGT = np.copy(GT[:, :, 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
        thisGT = np.minimum(thisGT, out_size - 1)
        thisGT = np.maximum(thisGT, 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)
    #print('%.2f' % (time.time() - tic) + ' s tf inference')
    if mode is 'train':
        for j in range(mapK.shape[3]):
            mapK[:, :, 0, j] -= batch_mask[:, :, 0, j] * 0.5 - 0.5 / 2
    # Do snake inference
    s = np.linspace(0, 2 * np.pi, L)
    init_u = out_size / 2 + 5 * np.cos(s)
    init_v = out_size / 2 + 5 * np.sin(s)
    import pdb
    pdb.set_trace()  ## DEBUG ##
    init_u = init_u.reshape([L, 1])
    init_v = init_v.reshape([L, 1])
    init_snake = np.array([init_u[:, 0], init_v[:, 0]]).T
    for j in range(batch_size):
        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.01
        grads_arrayA = mapA * 0.01
        grads_arrayB = mapB * 0.01
        grads_arrayK = mapK * 0.01
        grads_arrayE[:, :, 0, 0] -= draw_poly(snake, 1, [M, N], 4) - draw_poly(
            thisGT, 1, [M, N], 4)
        grads_arrayA[:, :, 0, 0] -= (np.mean(der1) - np.mean(der1_GT))
        grads_arrayB[:, :, 0, 0] -= (draw_poly(snake, der2, [M, N], 4) -
                                     draw_poly(thisGT, der2_GT, [M, N], 4))
        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)
        area_gt = np.sum(mask_gt > 0)
        area_snake = np.sum(mask_snake > 0)
    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 >= 50 and mode is 'test':
        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, area_gt, area_snake, snake
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(np.copy(images[:, :, :, batch_ind]))
    thisGT = np.copy(ground_thruths[:, :, 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)
        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
        thisGT = np.minimum(thisGT, out_size - 1)
        thisGT = np.maximum(thisGT, 0)
    [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)

    s = np.linspace(0, 2 * np.pi, L)
    init_u = out_size / 2 + 5 * np.cos(s)
    init_v = out_size / 2 + 5 * np.sin(s)
    init_snake = np.array([init_u, init_v]).T
    for j in range(batch_size):
        snake = Active_contour.active_contour(mapE[:,:,0,j], mapA[:,:,0,j], mapB[:,:,0,j], mapK[:,:,0,j], 1, \
                  init_snake, 25, Force_coefficient = 1, Balloon_coefficient = 1, h_= (3*im_size / L)**2, px=1, d0=0.0, d1=0.0)

        M = mapE.shape[0]
        N = mapE.shape[1]
        der1, der2 = derivatives_poly(snake)

        der1_GT, der2_GT = derivatives_poly(thisGT)
        grads_arrayE = mapE * 0.0  # A tensor of the same dimensions filled with zeros.
        grads_arrayA = mapA * 0.0
        grads_arrayB = mapB * 0.0
        grads_arrayK = mapK * 0.0
        grads_arrayE[:, :, 0, j] = -(draw_poly(snake, 1, [M, N], 4) -
                                     draw_poly(thisGT, 1, [M, N], 4))
        grads_arrayA[:, :, 0, j] = -(np.mean(der1) - np.mean(der1_GT))
        grads_arrayB[:, :, 0, j] = -(draw_poly(snake, der2, [M, N], 4) -
                                     draw_poly(thisGT, der2_GT, [M, N], 4))
        mask_gt = draw_poly_fill(thisGT, [M, N])
        mask_snake = draw_poly_fill(snake, [M, N])
        grads_arrayK[:, :, 0, j] = -(mask_gt - mask_snake)

        plt.subplot(121)
        plt.imshow(batch[:, :, :, j])
        #plt.show()
        #plt.imshow(draw_poly(thisGT, 1, [M, N],4))
        #plt.show()
        #plt.imshow(draw_poly_fill(thisGT, [M, N]))
        #plt.show()
        plt.subplot(122)
        plt.imshow(mapE[:, :, 0, 0])
        plt.colorbar()
        plt.show()

        intersection = (mask_gt + mask_snake) == 2
        union = (mask_gt + mask_snake) >= 1
        iou = np.sum(intersection) / np.sum(union)
        area_gt = np.sum(mask_gt > 0)
        area_snake = np.sum(mask_snake > 0)
        print('Area ratio: ', area_snake / area_gt)

    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
            })
    return iou, area_gt, area_snake, snake
Exemplo n.º 3
0
def epoch(n,i,mode):
    # mode (str): train or test
    batch_ind = np.arange(i,i+batch_size)
    batch = np.copy(images[:, :, :, batch_ind])
    batch_mask = np.copy(masks[:, :, :, batch_ind])
    thisGT = np.copy(GT[:, :, 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
    # prediction_np = sess.run(prediction,feed_dict={x:batch})
    tic = time.time()
    [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)
    #print('%.2f' % (time.time() - tic) + ' s tf inference')
    if mode is 'train':
        for j in range(mapK.shape[3]):
            mapK[:, :, 0, j] -= batch_mask[:, :, 0, j] * 0.5 - 0.5 / 2
        # mapE_aug[:,:,0,j] = mapE[:,:,0,j]+np.maximum(0,20-batch_dists[:,:,0,j])*max_val/50
    # Do snake inference
    s = np.linspace(0, 2 * np.pi, L)
    init_u = out_size / 2 + 5 * np.cos(s)
    init_v = out_size / 2 + 5 * np.sin(s)
    init_u = init_u.reshape([L, 1])
    init_v = init_v.reshape([L, 1])
    init_snake = np.array([init_u[:, 0], init_v[:, 0]]).T
    for j in range(batch_size):
        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.01
        grads_arrayA = mapA * 0.01
        grads_arrayB = mapB * 0.01
        grads_arrayK = mapK * 0.01
        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>=99 : # end_epoch=100
        # fig0, fig1, fig2, fig3 = plot_snakes(snake, snake_hist, thisGT, mapE, mapA, mapB, mapK, \
        #        grads_arrayE, grads_arrayA, grads_arrayB, grads_arrayK, batch, batch_mask)
        fig = save_plot_snakes(snake, snake_hist, thisGT, mapE, mapA, mapB, mapK, grads_arrayE, grads_arrayA, grads_arrayB, grads_arrayK, batch, batch_mask, 'snake_'+ str(i+1).zfill(3) +'_epoch_'+ str(n).zfill(3))
        
        fig.savefig(model_path+'results/'+'snake_'+str(i+1).zfill(3)+'_epoch_'+str(n).zfill(3)+'.png')  
        
        #plt.show()
    return iou,snake