def run_for_eval(input_image): H, W, channel = input_image.shape # < in case that input_image is smaller than crop_size > dif_height = H - crop_size dif_width = W - crop_size if dif_height < 0 or dif_width < 0: input_image = helper.numpy_pad_image(input_image, dif_height, dif_width) H, W, channel = input_image.shape # < split > split_crops = [] heights = helper.decide_intersection(H, crop_size) widths = helper.decide_intersection(W, crop_size) for height in heights: for width in widths: image_crop = input_image[height:height + crop_size, width:width + crop_size] split_crops.append(image_crop[np.newaxis, :]) feed_dict = {} splitters = chunks(split_crops, gpu_num()) for list_index in range(len(splitters) - 1): piece_crops = np.concatenate( split_crops[splitters[list_index]:splitters[list_index + 1]]) feed_dict[eval_image_pl[list_index]] = piece_crops for i in range(gpu_num()): if eval_image_pl[i] not in feed_dict.keys(): feed_dict[eval_image_pl[i]] = np.zeros( (1, crop_size, crop_size, 3), np.float32) proba_crops_pieces = sess.run(eval_probas_op, feed_dict=feed_dict) proba_crops = np.concatenate(proba_crops_pieces) # < reassemble > reassemble = np.zeros((H, W, eval_image_reader.num_classes), np.float32) index = 0 for height in heights: for width in widths: reassemble[height:height + crop_size, width:width + crop_size] += proba_crops[index] index += 1 # < crop to original image > if dif_height < 0 or dif_width < 0: reassemble = helper.numpy_crop_image(reassemble, dif_height, dif_width) return reassemble
def run_once(input_image): H, W, channel = input_image.shape # < in case that input_image is smaller than crop_size > dif_height = H - crop_size dif_width = W - crop_size if dif_height < 0 or dif_width < 0: input_image = helper.numpy_pad_image(input_image, dif_height, dif_width) H, W, channel = input_image.shape # < split this image into crops > split_crops = [] heights = helper.decide_intersection(H, crop_size) widths = helper.decide_intersection(W, crop_size) for height in heights: for width in widths: image_crop = input_image[height:height + crop_size, width:width + crop_size] split_crops.append(image_crop[np.newaxis, :]) # < > num_chunks = int((len(split_crops) - 1) / FLAGS.batch_size) + 1 proba_crops_list = [] for chunk_i in range(num_chunks): feed_dict = {} start = chunk_i * FLAGS.batch_size end = min((chunk_i + 1) * FLAGS.batch_size, len(split_crops)) feed_dict[images_pl[0]] = np.concatenate(split_crops[start:end]) proba_crops_part = sess.run(eval_probas_op, feed_dict=feed_dict) proba_crops_list.append(proba_crops_part[0]) proba_crops = np.concatenate(proba_crops_list) # < reassemble > reassemble = np.zeros((H, W, num_classes), np.float32) index = 0 for height in heights: for width in widths: reassemble[height:height + crop_size, width:width + crop_size] += proba_crops[index] index += 1 # < crop to original image > if dif_height < 0 or dif_width < 0: reassemble = helper.numpy_crop_image(reassemble, dif_height, dif_width) return reassemble