Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import keras
- from keras.datasets import fashion_mnist
- import matplotlib.pyplot as plt
- from keras.models import Sequential,Model
- from keras.layers import Input, Dense, Activation,Conv2D,MaxPooling2D,Dropout,Flatten,Reshape,UpSampling2D,Deconvolution2D,Conv2DTranspose
- import numpy as np
- from keras.callbacks import TensorBoard
- %matplotlib inline
- #pip install jupyter-tensorboard
- #harus install tensorbor buat notebook
- import tensorflow.compat.v1 as tf
- tf.disable_v2_behavior()
- (x_train, y_train), (x_test, y_test) = fashion_mnist.load_data()
- input_img = Input(shape=(28,28,1))
- #encoder
- l1 = Conv2D(16, (3,3),strides = 1 , activation = 'relu',padding='same')(input_img)
- l1 = MaxPooling2D((2,2))(l1)
- l1 = Conv2D(8,(3,3),strides = 1, activation='relu',padding='same')(l1)
- l1 = MaxPooling2D((2,2),padding = 'same')(l1)
- #
- l1 = Conv2D(8,(3,3),strides = 1,activation = 'relu',padding='same')(l1)
- #decoder network
- l1 = Conv2DTranspose(8,(3,3),strides = 1,activation='relu',padding='same')(l1)
- l1 = UpSampling2D((2,2))(l1)
- l1 = Conv2DTranspose(16,(3,3),strides = 1,activation='relu',padding = 'same')(l1)
- l1 = UpSampling2D((2,2))(l1)
- l2 = Conv2DTranspose(1,(3,3),strides = 1, activation='sigmoid',padding ='same')(l1)
- model2 = Model(input_img, l2)
- model2.compile(optimizer='Adam', loss='binary_crossentropy')
- model2.summary()
- x_train_1=x_train/255.0
- from time import time
- tensorboard = TensorBoard(log_dir="logs/{}".format(time()))
- x_train_ = x_train_1 + 0.3 * np.random.normal(loc=0.0, scale=1.0, size=x_train.shape)
- x_train_ = np.clip(x_train_, 0., 1.)
- x_ = x_train_[:,:,:,np.newaxis]
- x_.shape
- np.max(x_[0])
- plt.imshow(np.reshape(x_train_[500],(28,28)),cmap='Greys')
- x_train.shape
- model2.fit(x_, x_train_1[:,:,:,np.newaxis], epochs = 10, batch_size = 256, callbacks = [tensorboard])
- pp = model2.predict(np.reshape(x_[0],(1,28,28,1)))
- pp = np.reshape(pp,(28,28))
- plt.imshow(pp,cmap='Greys')
- plt.imshow(np.reshape(x_[0],(28,28)),cmap='Greys')
- plt.imshow(x_train[0],cmap='Greys')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement