在编码器和解码器keras上拆分自动编码器

时间:2019-02-28 15:21:43

标签: python machine-learning keras neural-network autoencoder

我正在尝试为以下应用创建自动编码器:

  1. 训练模型
  2. 拆分编码器和解码器
  3. 可视化压缩数据(编码器)
  4. 使用任意压缩数据获取输出(解码器)
from keras.layers import Input, Dense, Conv2D, MaxPooling2D, UpSampling2D
from keras.models import Model
from keras import backend as K
from keras.datasets import mnist
import numpy as np

(x_train, _), (x_test, _) = mnist.load_data()

x_train = x_train.astype('float32') / 255.
x_train = x_train[:100,:,:,]
x_test = x_test.astype('float32') / 255.
x_test = x_train
x_train = np.reshape(x_train, (len(x_train), 28, 28, 1))  # adapt this if using `channels_first` image data format
x_test = np.reshape(x_test, (len(x_test), 28, 28, 1))  # adapt this if using `channels_first` image data format
 input_img = Input(shape=(28, 28, 1))  # adapt this if using `channels_first` image data format

x = Conv2D(32, (3, 3), activation='relu', padding='same')(input_img)
x = MaxPooling2D((2, 2), padding='same')(x)
x = Conv2D(32, (3, 3), activation='relu', padding='same')(x)
encoded = MaxPooling2D((2, 2), padding='same')(x)

# at this point the representation is (7, 7, 32)

decoder = Conv2D(32, (3, 3), activation='relu', padding='same')(encoded)
x = UpSampling2D((2, 2))(decoder)
x = Conv2D(32, (3, 3), activation='relu', padding='same')(x)
x = UpSampling2D((2, 2))(x)
decoded = Conv2D(1, (3, 3), activation='sigmoid', padding='same')(x)

autoencoder = Model(input_img, decoded(encoded(input_img)))
autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy')

autoencoder.fit(x_train, x_train,
                epochs=10,
                batch_size=128,
                shuffle=True,
                validation_data=(x_test, x_test),
                #callbacks=[TensorBoard(log_dir='/tmp/tb', histogram_freq=0, write_graph=False)]
               )

如何拆分训练并按训练后的重量进行拆分?

1 个答案:

答案 0 :(得分:0)

制作编码器:

 private void dataGridView_SelectionChanged(object sender, EventArgs e)
 {
     foreach (DataGridViewRow row in dataGridView.SelectedRows) 
     {
         string value1 = row.Cells[0].Value.ToString();
         string value2 = row.Cells[1].Value.ToString();
         //...
     } 
 }

制作解码器:

import requests
from time import sleep

API_KEY = '2captchaapi'  # Your 2captcha API KEY
site_key = '2captcha site key'  # site-key, read the 2captcha docs on how to get this
url = 'site'  # example url
proxy = 'proxy'  # example proxy

proxy = {'http': 'http://' + proxy, 'https': 'https://' + proxy}

s = requests.Session()


# here we post site key to 2captcha to get captcha ID (and we parse it here too)
captcha_id = s.post(
    "http://2captcha.com/in.php?key={}&method=userrecaptcha&googlekey={}&pageurl={}".format(API_KEY, site_key, url), proxies=proxy).text.split('|')[1]
# then we parse gresponse from 2captcha response
recaptcha_answer = s.get("http://2captcha.com/res.php?key={}&action=get&id={}".format(API_KEY, captcha_id), proxies=proxy).text
print("solving ref captcha...")
while 'CAPCHA_NOT_READY' in recaptcha_answer:
    sleep(5)
    recaptcha_answer = s.get("http://2captcha.com/res.php?key={}&action=get&id={}".format(API_KEY, captcha_id), proxies=proxy).text

recaptcha_answer = recaptcha_answer.split('|')[1]

print(recaptcha_answer)

payload = {
    'signup-form[votes]':                       '',
    'signin-form[subs]':                        '',
    'signin-form[post_referer]':                'site',
    'signup-form[step2][hidden_captcha]':       '',
    'signup-form[details][login]':              'name@gmail.com',
    'signup-form[details][profile_name]':       'name1',
    'signup-form[details][password]':           'secret44%',
    'signup-form[details][password_confirm]':   'secret44%',
    'signup-form[details][tos_pp]':             'on',
    'signup-form[step2][optional][age]':        '24',
    'signup-form[step2][optional][sex]':        'Man',
    'signup-form[step2][optional][country]':    'france',
    'signup-form[step2][optional][language]':   'french',
    'signup-form[step2][profilepic][file]':     '',
    'g-recaptcha-response':                     recaptcha_answer
}


# then send the post request to the url
response = s.post(url, payload, verify=False)


print(response.text)

制作自动编码器:

input_img = Input(shape=(28, 28, 1))

x = Conv2D(32, (3, 3), activation='relu', padding='same')(input_img)
x = MaxPooling2D((2, 2), padding='same')(x)
x = Conv2D(32, (3, 3), activation='relu', padding='same')(x)
encoded = MaxPooling2D((2, 2), padding='same')(x)

encoder = Model(input_img, encoded)

现在,您可以使用任何方式使用它们。

  1. 训练自动编码器
  2. 使用编码器和解码器