Advertisement
Guest User

incerrrr

a guest
Nov 13th, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.99 KB | None | 0 0
  1. import keras
  2. from keras.datasets import fashion_mnist
  3. import matplotlib.pyplot as plt
  4. from keras.models import Sequential,Model
  5. from keras.layers import Input, Dense, Activation,Conv2D,MaxPooling2D,Dropout,Flatten,Reshape,UpSampling2D,Deconvolution2D,Conv2DTranspose
  6. import numpy as np
  7. from keras.callbacks import TensorBoard
  8. %matplotlib inline
  9. #pip install jupyter-tensorboard
  10. #harus install tensorbor buat notebook
  11. import tensorflow.compat.v1 as tf
  12. tf.disable_v2_behavior()
  13.  
  14. (x_train, y_train), (x_test, y_test) = fashion_mnist.load_data()
  15.  
  16. input_img = Input(shape=(28,28,1))
  17. #encoder
  18. l1 = Conv2D(16, (3,3),strides = 1 , activation = 'relu',padding='same')(input_img)
  19. l1 = MaxPooling2D((2,2))(l1)
  20. l1 = Conv2D(8,(3,3),strides = 1, activation='relu',padding='same')(l1)
  21. l1 = MaxPooling2D((2,2),padding = 'same')(l1)
  22. #
  23. l1 = Conv2D(8,(3,3),strides = 1,activation = 'relu',padding='same')(l1)
  24. #decoder network
  25. l1 = Conv2DTranspose(8,(3,3),strides = 1,activation='relu',padding='same')(l1)
  26. l1 = UpSampling2D((2,2))(l1)
  27. l1 = Conv2DTranspose(16,(3,3),strides = 1,activation='relu',padding = 'same')(l1)
  28. l1 = UpSampling2D((2,2))(l1)
  29. l2 = Conv2DTranspose(1,(3,3),strides = 1, activation='sigmoid',padding ='same')(l1)
  30.  
  31. model2 = Model(input_img, l2)
  32. model2.compile(optimizer='Adam', loss='binary_crossentropy')
  33. model2.summary()
  34.  
  35. x_train_1=x_train/255.0
  36. from time import time
  37. tensorboard = TensorBoard(log_dir="logs/{}".format(time()))
  38.  
  39. x_train_ = x_train_1 + 0.3 * np.random.normal(loc=0.0, scale=1.0, size=x_train.shape)
  40. x_train_ = np.clip(x_train_, 0., 1.)
  41. x_ = x_train_[:,:,:,np.newaxis]
  42. x_.shape
  43.  
  44. np.max(x_[0])
  45. plt.imshow(np.reshape(x_train_[500],(28,28)),cmap='Greys')
  46. x_train.shape
  47. model2.fit(x_, x_train_1[:,:,:,np.newaxis], epochs = 10, batch_size = 256, callbacks = [tensorboard])
  48.  
  49. pp = model2.predict(np.reshape(x_[0],(1,28,28,1)))
  50. pp = np.reshape(pp,(28,28))
  51. plt.imshow(pp,cmap='Greys')
  52. plt.imshow(np.reshape(x_[0],(28,28)),cmap='Greys')
  53. plt.imshow(x_train[0],cmap='Greys')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement