2017-09-25 126 views
1

我想製作一個以圖像+圖像+值爲輸入的神經網絡,對圖像執行卷積+積分,然後對結果進行線性變換。我可以在凱拉斯做到這一點嗎?非標準輸入的神經網絡

+0

圖像的順序和數量是否固定? –

+0

@ Craig.Li是的,正好兩個 – UpmostScarab

回答

1

這在結構上類似於克雷格李的回答,但在圖像,圖像,值格式,並且不使用VGG16,只是一個香草CNN。這些是3個獨立的網絡,其輸出在單獨處理後連接在一起,並且生成的連接向量通過最終層傳遞,包括來自所有輸入的信息。

input_1 = Input(data_1.shape[1:], name = 'input_1') 
conv_branch_1 = Conv2D(filters, (kernel_size, kernel_size), 
       activation = LeakyReLU())(conv_branch_1) 
conv_branch_1 = MaxPooling2D(pool_size = (2,2))(conv_branch_1) 
conv_branch_1 = Flatten()(conv_branch_1) 

input_2 = Input(data_2.shape[1:], name = 'input_2') 
conv_branch_2 = Conv2D(filters, (kernel_size, kernel_size), 
       activation = LeakyReLU())(conv_branch_2) 
conv_branch_2 = MaxPooling2D(pool_size = (2,2))(conv_branch_2) 
conv_branch_2 = Flatten()(conv_branch_2) 

value_input = Input(value_data.shape[1:], name = 'value_input') 
fc_branch = Dense(80, activation=LeakyReLU())(value_input) 

merged_branches = concatenate([conv_branch_1, conv_branch_2, fc_branch]) 
merged_branches = Dense(60, activation=LeakyReLU())(merged_branches) 
merged_branches = Dropout(0.25)(merged_branches) 
merged_branches = Dense(30, activation=LeakyReLU())(merged_branches) 

merged_branches = Dense(1, activation='sigmoid')(merged_branches) 

model = Model(inputs=[input_1, input_2, value_input], outputs=[merged_branches]) 

#if binary classification do this otherwise whatever loss you need 

model.compile(loss='binary_crossentropy') 
1

假設你的圖像是RGB型,圖像的形狀(寬度,高度,3),你可以用numpy像合併兩個圖像:

import numpy as np 
    from PIL import Image 

    img1 = Image.open('image1.jpg') 
    img2 = Image.open('imgae2.jpg') 

    img1 = img1.resize((width,height)) 
    img2 = img2.resize((width,height)) 

    img1_arr = np.asarray(img1,dtype='int32') 
    img2_arr = np.asarray(img2,dtype='int32') 

    #shape of img_arr is (width,height,6) 
    img_arr = np.concatenate((img1_arr,img2_arr),axis=2) 

以這種方式結合兩幅圖像,我們只增加通道,所以我們仍然可以在前兩個軸上進行卷積運算。

UPDATE: 我猜你的意思是多任務模式,你想卷積後兩幅圖像合併,Keras有concatenate()可以做到這一點。

input_tensor = Input(shape=(channels, img_width, img_height)) 
    # Task1 on image1 
    conv_model1 = VGG16(input_tensor=input_tensor, weights=None, include_top=False, classes=classes, 
        input_shape=(channels, img_width, img_height)) 
    conv_output1 = conv_model1.output 
    flatten1 = Flatten()(conv_output1) 
    # Task2 on image2 
    conv_model2 = VGG16(input_tensor=input_tensor, weights=None, include_top=False, classes=classes, 
        input_shape=(channels, img_width, img_height)) 
    conv_output2 = conv_model2.output 
    flatten2 = Flatten()(conv_output2) 
    # Merge the output 
    merged = concatenate([conv_output1, conv_output2], axis=1) 
    merged = Dense(classes,activation='softmax')(merged) 

    # add some Dense layers and Dropout, 
    final_model = Model(inputs=[input_tensor,input_tensor],outputs=merged) 
+0

謝謝你的回答,但是我想對任一圖像的卷積有不同的權重。所以我希望它更像是最終連接的不同層次。 – UpmostScarab

+0

再次感謝您的回答。 convolutionBoy更接近我想要的(因此得名)。 – UpmostScarab