2017-05-06 1 views
2

모델 병렬 처리가 아닌 keras (tensorflow backend)에서 데이터 병렬 처리를 찾고 있습니다. 비디오 파일에서 비디오 분류를 수행 중이므로 GPU에 크기 2의 배치 만 맞출 수 있습니다. 그래서, 더 나은 견적과 빠른 교육을 위해 배치 크기를 늘리기 위해 여러 GPU를 사용하는 방법이 궁금합니다. 이 작업을 수행하는 효과적인 방법을 제안 해 주시겠습니까?Keras의 데이터 병렬 처리

나는 12gb TitanX 한 개와 6gb Titan Black 한 개를 사용하고 있습니다.

감사

답변

1

이 그것을 할 한 가지 방법입니다 :

얻을 수 to_multi_gpu이 메소드를 model (단일 GPU를 통해 Keras 2.0을 사용하여 정의), 이상 같은 모델 (공유 매개 변수) 복제 된 것을 반환 여러 GPU. 새 모델에 대한 입력을 균등하게 슬라이스하고 각 슬라이스를 복제 된 모델 중 하나에 전달합니다. 모든 복제 된 모델의 출력이 마지막에 연결됩니다.

from keras import backend as K 
from keras.models import Model 
from keras.layers import Input 
from keras.layers.core import Lambda 
from keras.layers.merge import Concatenate 

def slice_batch(x, n_gpus, part): 
    """ 
    Divide the input batch into [n_gpus] slices, and obtain slice number [part]. 
    i.e. if len(x)=10, then slice_batch(x, 2, 1) will return x[5:]. 
    """ 
    sh = K.shape(x) 
    L = sh[0] // n_gpus 
    if part == n_gpus - 1: 
     return x[part*L:] 
    return x[part*L:(part+1)*L] 


def to_multi_gpu(model, n_gpus=2): 
    """ 
    Given a keras [model], return an equivalent model which parallelizes 
    the computation over [n_gpus] GPUs. 

    Each GPU gets a slice of the input batch, applies the model on that slice 
    and later the outputs of the models are concatenated to a single tensor, 
    hence the user sees a model that behaves the same as the original. 
    """ 
    with tf.device('/cpu:0'): 
     x = Input(model.input_shape[1:], name=model.input_names[0]) 

    towers = [] 
    for g in range(n_gpus): 
     with tf.device('/gpu:' + str(g)): 
      slice_g = Lambda(slice_batch, 
          lambda shape: shape, 
          arguments={'n_gpus':n_gpus, 'part':g})(x) 
      towers.append(model(slice_g)) 

    with tf.device('/cpu:0'): 
     merged = Concatenate(axis=0)(towers) 

    return Model(inputs=[x], outputs=[merged])