def process_study(study_id, annotations, out_dir, nstack):
volumes_metadata = isotropic_volumes_metadata[study_id]
isometric_volume = np.load('../data_proc/stage1/isotropic_volumes_1mm/{}.npy'.format(study_id))
mean = np.mean(isometric_volume).astype(np.float32)
std = np.std(isometric_volume).astype(np.float32)
resize_factor = np.divide(volumes_metadata['volume_resampled_shape'], volumes_metadata['volume_shape'])
coords_list = []
for a in annotations:
d = a['data']
z = int(round(resize_factor[0] * a['sliceNum']))
y0 = resize_factor[1] * d['y']
y1 = resize_factor[1] * (d['y'] + d['height'])
x0 = resize_factor[2] * d['x']
x1 = resize_factor[2] * (d['x'] + d['width'])
coords_list.append((z, y0, y1, x0, x1))
samples = []
for coords in coords_list:
z, y0, y1, x0, x1 = coords
for i in range(40):
sample_id = uuid4()
rand_y0 = max(0, int(round(y0 - random.randint(0, 32))))
rand_y1 = min(isometric_volume.shape[1], int(round(y1 + random.randint(0, 32))))
rand_x0 = max(0, int(round(x0 - random.randint(0, 32))))
rand_x1 = min(isometric_volume.shape[2], int(round(x1 + random.randint(0, 32))))
patch = []
for zi in range(nstack):
patch.append(resize(isometric_volume[z+zi, rand_y0:rand_y1, rand_x0:rand_x1], [32, 32],
mode='edge', clip=True, preserve_range=True))
patch = np.array(patch, dtype=np.float32)
patch = (patch - mean) / (std + 1e-7)
patch = np.moveaxis(patch, 0, 2)
bb_x = (x0 - rand_x0) / (rand_x1 - rand_x0)
bb_y = (y0 - rand_y0) / (rand_y1 - rand_y0)
bb_w = (x1 - x0) / (rand_x1 - rand_x0)
bb_h = (y1 - y0) / (rand_y1 - rand_y0)
samples.append((patch, bb_x, bb_y, bb_w, bb_h))
joblib.dump(samples, os.path.join(out_dir, 'samples', '{}.pkl'.format(study_id)))
return len(samples)
评论列表
文章目录