卷積自編碼器

卷積自編碼器利用了傳統自編碼器的無監督的學習方式,結合了卷積神經網絡的卷積和池化操作,從而實現特徵提取,最後通過stack,實現一個深層的神經網絡。
具體文章可以參考:

Masci J, Meier U, Cireşan D, et al. Stacked convolutional auto-encoders for hierarchical feature extraction[C]//International Conference on Artificial Neural Networks. Springer Berlin Heidelberg, 2011: 52-59.

無監督學習

無監督學習可以在沒有標記的情況下去學習樣本的特徵

The main purpose of unsupervised learning method is to extract generally useful features from unlabelled, to detect and remove input redundancies and to preserve only essential aspects of the data in robust and discriminative representations.

而卷積自編碼器創建的目的就在於,利用卷積神經網絡的卷積和池化操作,實現特徵不變性提取(invariant feature)的無監督特徵提取。

卷積神經網絡和傳統自編碼器

卷積神經網絡由一個由卷積和池化組成的神經網絡。卷積的作用相當於一個濾波器,而池化則是提取不變特徵。其網絡結構如下圖所示:
卷積網絡
自編碼器則是一個由輸入層,隱含層,輸出層所構成的神經網絡,其結構如下圖所示:
自編碼器
通過利用輸入層與輸出層之間的映射關係,實現樣本重構,從而提取特徵。

卷積自編碼器

假設我們有k個卷積核,每個卷積核由參數wkbk 組成,用和hk 表示卷積層,則

hk=σ(xwk+bk)
將得到的hk 進行特徵重構,可以得到下式:
y=σ(hkw^k+c)
將輸入的樣本和最終利用特徵重構得出來的結果進行歐幾里得距離比較,通過BP算法進行優化,就可以得到一個完整的卷積自編碼器(CAE)
E=12n(xiyi)2

代碼

這個是在github找到的一個基於keras的代碼:

def getModel():
    input_img = Input(shape=(48, 48, 1))
    x = Convolution2D(16, 3, 3, activation='relu', border_mode='same', dim_ordering='tf')(input_img)
    x = MaxPooling2D((2, 2), border_mode='same', dim_ordering='tf')(x)
    x = Convolution2D(32, 3, 3, activation='relu', border_mode='same', dim_ordering='tf')(input_img)
    x = MaxPooling2D((2, 2), border_mode='same', dim_ordering='tf')(x)
    x = Convolution2D(64, 3, 3, activation='relu', border_mode='same', dim_ordering='tf')(x)
    encoded = MaxPooling2D((2, 2), border_mode='same', dim_ordering='tf')(x)
    #6x6x32 -- bottleneck
    x = UpSampling2D((2, 2), dim_ordering='tf')(encoded)
    x = Convolution2D(32, 3, 3, activation='relu', border_mode='same', dim_ordering='tf')(x)
    x = UpSampling2D((2, 2), dim_ordering='tf')(x)
    x = Convolution2D(16, 3, 3, activation='relu', border_mode='same', dim_ordering='tf')(x)
    decoded = Convolution2D(3, 3, 3, activation='relu', border_mode='same', dim_ordering='tf')(x)

    #Create model
    autoencoder = Model(input_img, decoded)
    return autoencoder

# Trains the model for 10 epochs
def trainModel():
    # Load dataset
    print("Loading dataset...")
    x_train_gray, x_train, x_test_gray, x_test = getDataset()

    # Create model description
    print("Creating model...")
    model = getModel()
    model.compile(optimizer='rmsprop', loss='binary_crossentropy',metrics=['accuracy'])

    # Train model
    print("Training model...")
    model.fit(x_train_gray, x_train, nb_epoch=10, batch_size=148, shuffle=True, validation_data=(x_test_gray, x_test), callbacks=[TensorBoard(log_dir='/tmp/tb', histogram_freq=0, write_graph=False)])

    # Evaluate loaded model on test data
    print("Evaluating model...")
    score = model.evaluate(x_train_gray, x_train, verbose=0)
    print "%s: %.2f%%" % (model.metrics_names[1], score[1]*100)

    # Serialize model to JSON
    print("Saving model...")
    model_json = model.to_json()
    with open("model.json", "w") as json_file:
        json_file.write(model_json)

    # Serialize weights to HDF5
    print("Saving weights...")
    model.save_weights("model.h5")
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章