-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from IvanKuchin/development
Feature: rescale dataset to the target resolution and different zoom levels
- Loading branch information
Showing
5 changed files
with
129 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
__pycache__ | ||
logs | ||
predict | ||
predict.* | ||
*.h5 | ||
*.keras | ||
*.hdf5 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import tensorflow as tf | ||
import time | ||
import os | ||
import sys | ||
import inspect | ||
|
||
currentdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))) | ||
parentdir = os.path.dirname(currentdir) | ||
sys.path.insert(0, parentdir) | ||
|
||
from tools import resize_3d | ||
import config as config | ||
|
||
|
||
DEBUG_DATALOADER = True | ||
DEBUG_DATA_LOADING_PERFORMANCE = False | ||
|
||
# cutout and resize 3-d tensor with shape [w,h,d] | ||
# 1) cut overheads off from top_left to bottom_right + 1 | ||
# top_left and bottom_right will be present in the final shape | ||
# 2) resize shape from step(1) to final shape | ||
# final shape taken form the config | ||
def cutout_and_resize_tensor(tensor, top_left, bottom_right): | ||
# assert tensor.ndim == 3 | ||
t = tensor[top_left[0]:bottom_right[0] + 1, top_left[1]:bottom_right[1] + 1, top_left[2]:bottom_right[2] + 1] | ||
t = tf.squeeze(t) | ||
# assert tf.rank(t) == 3 | ||
final_shape = tf.constant([config.IMAGE_DIMENSION_X, config.IMAGE_DIMENSION_Y, config.IMAGE_DIMENSION_Z]) | ||
t = resize_3d.resize_3d_image(t, final_shape) | ||
return t | ||
|
||
|
||
# cutout and resize 3-d tensor with shape [w,h,d] | ||
# cut overheads off from top_left to bottom_right + 1 | ||
# percentages are used to calculate from top_left and bottom_right | ||
# | ||
# for example, if top_left = [0,0,0] and bottom_right = [100,100,100] | ||
# pancreas volume from [10,10,10] to [90,90,90] | ||
# if all percentages 0.1 (which is 10%) then | ||
# the cut will be from [1,1,1] to [99,99,99] | ||
def cut_and_resize_including_pancreas(data, label, top_left_percentage, bottom_right_percentage): | ||
|
||
start_prep = time.time() | ||
top_left_label_position = tf.reduce_min(tf.where(label == 1), axis=0) | ||
bottom_right_label_position = tf.reduce_max(tf.where(label == 1), axis=0) | ||
# random_offset_top_left = tf.random.uniform(shape = [3], minval = [0.0, 0.0, 0.0], maxval = tf.cast(top_left_label_position, dtype=tf.float32)) | ||
top_left_offset = top_left_percentage * tf.cast(top_left_label_position, dtype=tf.float32) | ||
top_left_offset = tf.cast(top_left_offset, dtype = tf.int32) | ||
# random_offset_bottom_right = tf.random.uniform(shape = [3], minval = tf.cast(bottom_right_label_position, dtype=tf.float32), maxval = tf.cast(tf.shape(data), dtype=tf.float32)) | ||
bottom_right_offset = tf.cast(tf.shape(data), dtype=tf.float32) - bottom_right_percentage * tf.cast(tf.shape(data) - tf.cast(bottom_right_label_position, dtype=tf.int32), dtype=tf.float32) | ||
bottom_right_offset = tf.cast(bottom_right_offset, dtype = tf.int32) | ||
finish_prep = time.time() | ||
|
||
if DEBUG_DATALOADER: | ||
print("\tpancreas shape:", (bottom_right_label_position - top_left_label_position).numpy()) | ||
print("\ttop_left_label_position:", top_left_label_position.numpy(), "bottom_right_label_position:", bottom_right_label_position.numpy()) | ||
print(f"\toriginal shape: {label.shape}") | ||
print(f"\tpercentages: top_left: {top_left_percentage:.2f}, bottom_right: {bottom_right_percentage:.2f}") | ||
print("\toffset_top_left:", top_left_offset.numpy(), "bottom_right_offset:", bottom_right_offset.numpy()) | ||
print("\tslice shape:", (bottom_right_offset - top_left_offset + 1).numpy()) | ||
|
||
start_data = time.time() | ||
_data = cutout_and_resize_tensor(data, top_left_offset, bottom_right_offset) | ||
finish_data = time.time() | ||
|
||
start_label = time.time() | ||
_label = cutout_and_resize_tensor(label, top_left_offset, bottom_right_offset) | ||
finish_label = time.time() | ||
|
||
if DEBUG_DATA_LOADING_PERFORMANCE: | ||
print(f"\tDATA_LOADING_PERFORMANCE: prep: {finish_prep - start_prep:.1f} data: {finish_data - start_data:.1f} label: {finish_label - start_label:.1f}") | ||
|
||
if DEBUG_DATALOADER: | ||
print("\t_data shape:", _data.shape, "_label shape:", _label.shape) | ||
|
||
return _data, _label | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters