2017-12-05 17 views
0

저는 Keras에 이미지에 여러 클래스를 할당하는 컨벌루션 그물을 만들고 있습니다. 이미지에 9 개의 관심 지점이 있다면 3 가지 방법 중 하나로 분류 할 수 있습니다. 27 출력 뉴런에 softmax 활성화를 추가하면 각 연속 뉴런에 대한 확률을 계산할 수 있습니다.뉴런의 서브 세트에 softmax를 적용하십시오.

그렇게 할 수 있습니까? 나는 큰 softmax 레이어를 간단히 추가 할 수는 있지만, 이것은 모든 출력 뉴런에 대한 확률 분포를 초래할 것이며 이는 내 응용 프로그램에 너무 광범위합니다.

답변

2

가장 순진한 구현에서는 데이터를 바꿀 수 있으며 설명 된 바를 정확히 알 수 있습니다 : "연속적인 각 트리플에 대한 확률".

당신은 (batch_size,27) 모양 (27 개) 클래스와 출력을 가지고 그것을 바꿀 :

model.add(Reshape((9,3))) 
model.add(Activation('softmax')) 

테이크 케어뿐만 아니라 당신의 y_true 데이터를 바꿀 수 있습니다. 그들은 대략 정적이 있다면 당신은 아마 (그 위치에 따라 insterest의 9 점을 분리하는 것, 더 정교한 솔루션에서

model.add(Reshape((27,)) 

: 또는 원형을 복원 모델의 또 다른 모양 변경을 추가 위치)를 만들고 병렬 경로를 만듭니다. 예를 들어, 9 개의 위치가 균등 한 간격의 사각형이라고 가정하고 해당 세그먼트에 대해 동일한 넷 및 클래스를 사용하려고한다고 가정합니다.

inputImage = Input((height,width,channels)) 

#supposing the width and height are multiples of 3, for easiness in this example 
recHeight = height//3 
recWidth = width//3 

#create layers here without calling them 
someConv1 = Conv2D(...) 
someConv2 = Conv2D(...) 
flatten = Flatten() 
classificator = Dense(..., activation='softmax') 

outputs = [] 
for i in range(3): 
    for j in range(3): 
     fromH = i*recHeight 
     toH = fromH + recHeight 
     fromW = j*recWidth 
     toW = fromW + recWidth 
     imagePart = Lambda(
          lambda x: x[:,fromH:toH, fromW:toW,:], 
          output_shape=(recHeight,recWidth,channels) 
         )(inputImage) 

     #using the same net and classes for all segments 
     #if this is not true, create new layers here instead of using the same 
     output = someConv1(imagePart) 
     output = someConv2(output) 
     output = flatten(output) 
     output = classificator(output) 
     outputs.append(output) 

outputs = Concatenate()(outputs) 

model = Model(inputImage,outputs)