前提・実現したいこと
Kerasを用いたノイズ除去を行いたく、あるサイトに載っているコードを参考にしています。
発生しているエラーの原因、改善方法を知りたいです。
https://gangannikki.hatenadiary.jp/entry/2019/01/19/000000
こちらのサイトです。
発生している問題・エラーメッセージ
最後の行に関するエラー
Undefined variable 'plot_history'
該当のソースコード
ptyhon
1import numpy as np 2import matplotlib.pyplot as plt 3from keras.datasets import mnist 4from keras.layers import Input, Conv2D, MaxPooling2D, UpSampling2D, BatchNormalization, Activation 5from keras.models import Model, load_model 6from keras.utils import plot_model 7from keras.callbacks import CSVLogger, EarlyStopping, TensorBoard, ModelCheckpoint 8 9# データのロード・分割 10(x_train, y_train), (x_test, y_test) = mnist.load_data() # (60000, 28, 28), (10000, 28, 28) 11x_train = x_train.astype('float32') / 255. 12x_test = x_test.astype('float32') / 255. 13x_train = np.reshape( x_train, (len(x_train), 28, 28, 1) ) # (使用数,チャネル数,x,y)=(60000, 1, 28, 28) 14x_test = np.reshape( x_test, (len(x_test), 28, 28, 1) ) 15 16""" 17 ノイズの付与 18""" 19noise_factor = 0.5 20 21# 平均0、標準偏差1の正規分布 22x_train_noisy = x_train + noise_factor * np.random.normal( loc=0., scale=1., size=x_train.shape ) 23x_test_noisy = x_test + noise_factor * np.random.normal( loc=0., scale=1., size=x_test.shape ) 24 25x_train_noisy = np.clip( x_train_noisy, 0., 1. ) 26x_test_noisy = np.clip( x_test_noisy, 0., 1. ) 27 28""" 29 モデルの生成、学習 30""" 31def model( x_train_noisy, x_train, x_test_noisy, x_test ): 32 # 28×28、グレースケール 33 input_img = Input( shape=( 28, 28, 1) ) 34 35 # Encode_Conv1 36 x = Conv2D( 32, (3, 3), padding='same', activation='relu' )(input_img) 37 x = MaxPooling2D( ( 2, 2), padding='same' )(x) 38 # Encode_Conv2 39 x = Conv2D( 32, (3, 3), padding='same', activation='relu' )(x) 40 encoded = MaxPooling2D( ( 2, 2), padding='same' )(x) 41 42 # Decode_Conv1 43 x = Conv2D( 32, (3, 3), padding='same', activation='relu' )(encoded) 44 x = UpSampling2D(( 2, 2))(x) 45 # Decode_Conv2 46 x = Conv2D( 32, (3, 3), padding='same', activation='relu' )(x) 47 x = UpSampling2D(( 2, 2))(x) 48 decoded = Conv2D( 1, (3, 3), padding='same', activation='sigmoid' )(x) 49 50 autoencoder = Model( input_img, decoded ) 51 autoencoder.compile( optimizer='adam', 52 loss='binary_crossentropy', 53 metrics=["accuracy"] ) 54 55 # アーキテクチャの可視化 56 autoencoder.summary() # ディスプレイ上に表示 57 plot_model( autoencoder, to_file="architecture.png") 58 59 # モデルの学習 60 epochs = 10 61 batch_size = 128 62 tensor_board = TensorBoard( "./logs", histogram_freq=0, write_graph=True, write_images=True ) 63 64 history = autoencoder.fit( x_train_noisy, x_train, 65 epochs=epochs, 66 batch_size=batch_size, 67 shuffle=True, 68 validation_data=(x_test_noisy,x_test), 69 callbacks=[ tensor_board ] ) 70 71 # 学習のグラフ化 72 plot_history( history, epochs )
試したこと
始めたばかりで、理解が不十分なため、何をすればよいかわかっていません。
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。
あなたの回答
tips
プレビュー