나는 회전을 통해 데이터를 보충하고 각 입력 볼륨의 크기를 다양하게하는 신경망을 연구 중입니다.4 차원 배열로 Numpy 3D 가변 크기 목록을 변환
백업 해 드리겠습니다. 네트워크 입력은 3D 볼륨입니다. 가변 크기의 3D 볼륨을 생성 한 다음 입력 볼륨이 일정하도록 0으로 각 볼륨을 채 웁니다. here에서 패딩에 문제가 있는지 확인하십시오 (현재 해결됨).
가변 크기의 3D 볼륨을 생성하고 목록에 추가 한 다음 목록을 수십 개의 배열로 변환합니다. 이 시점에서, 패딩
input_augmented_matrix = []
label_augmented_matrix = []
for i in range(n_volumes):
if i % 50 == 0:
print ("Augmenting step #" + str(i))
slice_index = randint(0,n_input)
z_max = randint(5,n_input)
z_rand = randint(3,5)
z_min = z_max - z_rand
x_max = randint(75, n_input_x)
x_rand = randint(60, 75)
x_min = x_max - x_rand
y_max = randint(75, n_input_y)
y_rand = randint(60, 75)
y_min = y_max - y_rand
random_rotation = randint(1,4) * 90
for j in range(2):
temp_volume = np.empty((z_rand, x_rand, y_rand))
k = 0
for z in range(z_min, z_max):
l = 0
for x in range(x_min, x_max):
m = 0
for y in range(y_min, y_max):
if j == 0:
#input volume
try:
temp_volume[k][l][m] = input_matrix[z][x][y]
except:
pdb.set_trace()
else:
#ground truth volume
temp_volume[k][l][m] = label_matrix[z][x][y]
m = m + 1
l = l + 1
k = k + 1
temp_volume = np.asarray(temp_volume)
temp_volume = np.rot90(temp_volume,random_rotation)
if j == 0:
input_augmented_matrix.append(temp_volume)
else:
label_augmented_matrix.append(temp_volume)
input_augmented_matrix = np.asarray(input_augmented_matrix)
label_augmented_matrix = np.asarray(label_augmented_matrix)
이 때 input_augmented_matrix
의 치수는 다음 코드이어서 (N,)
I 패드이다 .. 그래서 4D 튜플로 변환이 아무 의미 발생하지 않았다. .. 이때
for i in range(n_volumes):
print("Padding volume #" + str(i))
input_augmented_matrix[i] = np.lib.pad(input_augmented_matrix[i], ((0,n_input_z - int(input_augmented_matrix[i][:,0,0].shape[0])),
(0,n_input_x - int(input_augmented_matrix[i][0,:,0].shape[0])),
(0,n_input_y - int(input_augmented_matrix[i][0,0,:].shape[0]))),
'constant', constant_values=0)
label_augmented_matrix[i] = np.lib.pad(label_augmented_matrix[i], ((0,n_input_z - int(label_augmented_matrix[i][:,0,0].shape[0])),
(0,n_input_x - int(label_augmented_matrix[i][0,:,0].shape[0])),
(0,n_input_y - int(label_augmented_matrix[i][0,0,:].shape[0]))),
'constant', constant_values=0)
, 치수는 여전히 목록의 모든 요소가 일정하더라도 (N,)
이다. 예를 들어, input_augmented_matrix[0] = input_augmented_matrix[1]
현재 루프를 통해 새로운 배열을 만들지 만 너무 오래 걸리므로이를 자동화하는 방법을 선호합니다. 다음 코드를 사용하면됩니다 ...
input_4d = np.empty((n_volumes, n_input_z, n_input_x, n_input_y))
label_4d = np.empty((n_volumes, n_input_z, n_input_x, n_input_y))
for i in range(n_volumes):
print("Converting to 4D tuple #" + str(i))
for j in range(n_input_z):
for k in range(n_input_x):
for l in range(n_input_y):
input_4d[i][j][k][l] = input_augmented_matrix[i][j][k][l]
label_4d[i][j][k][l] = label_augmented_matrix[i][j][k][l]
더 빠르고 명확한 방법이 있습니까?
도움 주셔서 감사합니다! 이것은 훨씬 더 좋아 보인다 : 그리고 이것은 tensorflow 네트워크를위한 데이터 클래스이다. –
@KendallWeihe 당신을 환영한다! 문제가 해결되었거나 뭔가 놓쳤습니다. – Charlie