def epoch(n, i, mode): # mode (str): train or test batch_ind = np.arange(i, i + batch_size) batch = images[batch_ind, :, :, :] batch_labels = onehot_labels[batch_ind, :, :, ] batch_mask = building_mask[batch_ind, :, :, ] if mode is 'train': ang = np.random.rand() * 360 for j in range(len(batch_ind)): for b in range(batch.shape[3]): batch[j, :, :, b] = imrotate(batch[j, :, :, b], ang) batch_labels[j, :, :, b] = imrotate(batch_labels[j, :, :, b], ang, resample='nearest') # prediction_np = sess.run(prediction,feed_dict={x:batch}) tic = time.time() #print('%.2f' % (time.time() - tic) + ' s tf inference') if mode is 'train': _, loss, loss_l2, res = sess.run( [optimizer, cross_entropy, l2loss, pred], feed_dict={ x_: batch, y_: batch_labels }) prediction = np.int32(res[:, :, :, 1] >= np.amax(res, axis=3)) if mode is 'test': res = sess.run(pred, feed_dict={x_: batch}) prediction = np.int32(res[:, :, :, 1] >= np.amax(res, axis=3)) g = np.abs(np.linspace(-1, 1, out_size)) G0, G1 = np.meshgrid(g, g) d = (1 - np.sqrt(G0 * G0 + G1 * G1)) for j in range(len(batch_ind)): val = np.max(d * prediction[j, :, :]) seed_im = np.int32(d * prediction[j, :, :] == val) if val > 0: prediction[j, :, :] = skimage.morphology.reconstruction( seed_im, prediction[j, :, :]) if do_plot: plt.imshow(res[0, :, :, :]) plt.show() intersection = (batch_mask[:, :, :, 0] + prediction) == 2 union = (batch_mask[:, :, :, 0] + prediction) >= 1 iou = np.sum(intersection) / np.sum(union) area_gt = np.sum(batch_mask[:, :, :, 0] > 0) area_snake = np.sum(prediction > 0) if do_save_results: scipy.misc.imsave( model_path + 'results/seg_' + str(i).zfill(3) + '.png', np.uint8(prediction[0, :, :] * 255)) return iou, area_gt, area_snake
def epoch(i, mode): # mode (str): train or test batch_ind = np.arange(i, i + batch_size) batch = images[batch_ind, :, :, :] batch_labels = onehot_labels[batch_ind, :, :, ] if mode is 'train': ang = np.random.rand() * 360 for j in range(len(batch_ind)): for b in range(batch.shape[3]): batch[j, :, :, b] = imrotate(batch[j, :, :, b], ang) batch_labels[j, :, :, b] = imrotate(batch_labels[j, :, :, b], ang, resample='nearest') # prediction_np = sess.run(prediction,feed_dict={x:batch}) #print('%.2f' % (time.time() - tic) + ' s tf inference') if mode is 'train': _, loss, res = sess.run([optimizer, cross_entropy, pred], feed_dict={ x_: batch, y_: batch_labels }) prediction = np.int32(res[:, :, :, 1] >= np.amax(res, axis=3)) if mode is 'test': res = sess.run(pred, feed_dict={x_: batch}) prediction = np.int32(res[:, :, :, 1] >= np.amax(res, axis=3)) seed_im = np.zeros((out_size, out_size)) g = np.abs(np.linspace(-1, 1, out_size)) G0, G1 = np.meshgrid(g, g) d = (1 - np.sqrt(G0 * G0 + G1 * G1)) for j in range(len(batch_ind)): val = np.max(d * prediction[j, :, :]) seed_im = np.int32(d * prediction[j, :, :] == val) prediction[j, :, :] = skimage.morphology.reconstruction( seed_im, prediction[j, :, :]) #plt.imshow(res[0,:,:,:]) #plt.show() intersection = (batch_labels[:, :, :, 1] + prediction) == 2 union = (batch_labels[:, :, :, 1] + prediction) >= 1 iou = np.sum(intersection) / np.sum(union) if mode is 'train': iou_train[len(iou_train) - 1] += iou if mode is 'test': iou_test[len(iou_test) - 1] += iou
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
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
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