2016-08-16 4 views
0

나는 회전을 통해 데이터를 보충하고 각 입력 볼륨의 크기를 다양하게하는 신경망을 연구 중입니다.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] 

더 빠르고 명확한 방법이 있습니까?

답변

1

나는이 부분

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_input = input_matrix[z_min:z_max, x_min:x_max, y_min:y_max] 
temp_label = label_matrix[z_min:z_max, x_min:x_max, y_min:y_max] 

하고 여기에 다음

temp_input = np.rot90(temp_input, random_rotation) 
temp_label = np.rot90(temp_label, random_rotation) 

input_augmented_matrix.append(temp_input) 
label_augmented_matrix.append(temp_label) 

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) 
0을 수행 할 shape 속성은 모든 차원 난 당신이 NumPy의 마법 인덱싱 코드의 마지막 부분을 리팩토링 할 준비가 지금 생각
ia_shape = input_augmented_matrix[i].shape 
input_augmented_matrix[i] = np.lib.pad(
    input_augmented_matrix[i], 
    ((0, n_input_z - ia_shape[0]), 
    (0, n_input_x - ia_shape[1])), 
    (0, n_input_y - ia_shape[2]))), 
    'constant', 
    constant_values=0) 

으로 배열하면 크기를 제공하기 때문에

더 나은는이 작업을 수행합니다.

내 일반적인 제안 : 루프의 당신의 캐스케이드 같은 들여 쓰기를 피하기 위해 코드의 반복 부품

  • 사용하는 기능;
  • 너무 많이 중첩 된 루프가 필요하다면 재귀에 대해 생각해보십시오.
  • NumPy의 능력 탐색 공식적으로 documentation : 그들은 정말 흥미 롭습니다. 예를 들어 indexing이이 작업에 도움이됩니다.
  • PyLintFlake8 패키지를 사용하여 코드의 품질을 검사하십시오.

신경망을 직접 작성 하시겠습니까? 아니면 일부 패턴 인식 작업을 해결하고 싶습니까? SciPy 라이브러리에는 필요한 것을 포함 할 수 있으며 NumPy을 기반으로합니다.

+0

도움 주셔서 감사합니다! 이것은 훨씬 더 좋아 보인다 : 그리고 이것은 tensorflow 네트워크를위한 데이터 클래스이다. –

+0

@KendallWeihe 당신을 환영한다! 문제가 해결되었거나 뭔가 놓쳤습니다. – Charlie

관련 문제