Advertisement
Guest User

Untitled

a guest
Mar 21st, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.74 KB | None | 0 0
  1. from keras.layers import Input, Dense, Conv2D, Conv3D, LSTM, Flatten, Conv2DTranspose, MaxPooling2D, UpSampling2D, Conv3DTranspose, UpSampling3D, MaxPooling3D
  2. from keras.models import Model, Sequential
  3. from keras.datasets import mnist
  4. import numpy as np
  5. from sklearn.model_selection import train_test_split
  6. from keras.utils import to_categorical
  7. # use Matplotlib (don't ask)
  8. import matplotlib.pyplot as plt
  9.  
  10.  
  11.  
  12.  
  13. # this is the size of our encoded representations
  14. encoding_dim = 64 # 32 floats -> compression of factor 24.5, assuming the input is 784 floats
  15. '''
  16. # this is our input placeholder
  17. input_img = Input(shape=(1440000,))
  18. # "encoded" is the encoded representation of the input
  19. encoded = Dense(encoding_dim, activation='relu')(input_img)
  20. # "decoded" is the lossy reconstruction of the input
  21. decoded = Dense(1440000, activation='sigmoid')(encoded)
  22.  
  23. # this model maps an input to its reconstruction
  24. autoencoder = Model(input_img, decoded)
  25.  
  26. # this model maps an input to its encoded representation
  27. encoder = Model(input_img, encoded)
  28.  
  29. # create a placeholder for an encoded (32-dimensional) input
  30. encoded_input = Input(shape=(encoding_dim,))
  31. # retrieve the last layer of the autoencoder model
  32. decoder_layer = autoencoder.layers[-1]
  33. # create the decoder model
  34. decoder = Model(encoded_input, decoder_layer(encoded_input))
  35. '''
  36.  
  37.  
  38.  
  39. autoencoder = Sequential()
  40. autoencoder.add(Conv3D(64, kernel_size=3, activation='relu', input_shape=(99,75,120,160)))
  41. autoencoder.add(MaxPooling3D(pool_size=2))
  42. autoencoder.add(Conv3D(32, kernel_size=3, activation='relu'))
  43. autoencoder.add(MaxPooling3D(pool_size=2))
  44. autoencoder.add(Conv3D(16, kernel_size=3, activation='relu'))
  45. autoencoder.add(MaxPooling3D(pool_size=2))
  46. '''
  47. autoencoder.add(Flatten())
  48. autoencoder.add(Dense(encoding_dim))
  49. '''
  50. autoencoder.add(Conv3DTranspose(16, kernel_size=3, activation='relu', data_format="channels_last"))
  51. autoencoder.add(UpSampling3D(size=2))
  52. autoencoder.add(Conv3DTranspose(32, kernel_size=3, activation='relu'))
  53. autoencoder.add(UpSampling3D(size=2))
  54. autoencoder.add(Conv3DTranspose(64, kernel_size=3, activation='relu'))
  55.  
  56. autoencoder.compile(optimizer='adagrad', loss='mse')
  57. #split data to test and train
  58. data = np.load(r"C:Usersshj_kDesktopProjecthandclapping.npy")
  59.  
  60. data = to_categorical(data)
  61. print (data.shape)
  62. (x_train,x_test) = train_test_split(data)
  63. #print("Xtrain",x_train)
  64. x_train = x_train.astype('float32') / 255.
  65. x_test = x_test.astype('float32') / 255.
  66.  
  67.  
  68. x_train = x_train.reshape((len(x_train), np.prod(x_train.shape[1:])))
  69. x_test = x_test.reshape((len(x_train), np.prod(x_train.shape[1:])))
  70. print (x_train.shape)
  71. print (x_test.shape)
  72.  
  73. autoencoder.fit(x_train, x_train,
  74. epochs=100,
  75. batch_size=25,
  76. shuffle=False,
  77. validation_data=(x_test, x_test))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement