Blame view

QCAE.py 5.94 KB
8a1d43c41   Parcollet Titouan   V1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
  #!/usr/bin/env python
  # -*- coding: utf-8 -*-
  # Author: Titouan Parcollet
  
  from keras.layers import Input, Dense, Conv2D, MaxPooling2D, UpSampling2D
  import time
  import sys
  import numpy as np
  import keras
  from keras.models import Sequential,Model
  from keras.callbacks import Callback
  from keras.layers.core import Dense, Activation
  from keras.layers import Dropout, Input
  import tensorflow as tf
  from keras.backend.tensorflow_backend import set_session
  from keras.layers import Conv2D, MaxPooling2D
  from keras.layers import Conv1D, MaxPooling1D
  import os
  from complexnn import *
  
  print("##############################################")
  print("## Quaternion Convolutional encoder-decoder ##")
  print("## Titouan Parcollet, LIA 2017              ##")
  print("##############################################")
  
  ########  Static parameters 
  # size        : Number of train sample to choose
  # 		        -> 100,1000,5000
  # batch_size  : batch size during training
  # train_epoch : number of epochs
  # CIFAR_*     : path to pre-processed data files
  # output_*    : path where the output layer will be saved
  # n_input     : size of the input vector CIFAR = 32 * 32 * 3
  # n_classes   : size of the output vector
  
  size = 100
  batch_size = 5
  train_epoch = 100
  n_input = 3072
  n_classes = 3072
  CIFAR_TRAINING = "Resources/Corpus/"+str(size)+"_train.dat"
  CIFAR_TEST = "Resources/Corpus/"+str(size)+"_test.dat"
  if not os.path.exists("Results"):
  	    os.makedirs("Results")
  output_test = open("Results/"+str(size)+"_test.dat", "w")
  output_train = open("Results/"+str(size)+"_train.dat", "w")
  
  ######## Loading datasets
  train_file = open(CIFAR_TRAINING, "r").readlines()
  test_file = open(CIFAR_TEST, "r").readlines()
  train_data = np.zeros( (len(train_file), n_input) )
  train_label = np.zeros((len(train_file), n_classes))
  test_data = np.zeros( (len(test_file), n_input) )
  test_label = np.zeros((len(test_file), n_classes))
  
  print("Loading dataset ...")
  line_cpt =0
  for line in train_file:
      data_cpt =0
      for splitted in line.split("\t")[0].split(" "):
          if(data_cpt < n_input):
              train_data[line_cpt][data_cpt] = float(splitted)
              data_cpt+=1
      data_cpt=0
     
      for splitted in line.split("\t")[1].split(" "):
          train_label[line_cpt][data_cpt] = int(splitted)
          data_cpt+=1
      line_cpt+=1
  
  line_cpt =0
  for line in test_file:
      data_cpt =0
      for splitted in line.split("\t")[0].split(" "):
          if(data_cpt<n_input):
              test_data[line_cpt][data_cpt] = float(splitted)
              data_cpt+=1
      data_cpt=0
      for splitted in line.split("\t")[1].split(" "):
          test_label[line_cpt][data_cpt] = int(splitted)
          data_cpt+=1
   
      data_cpt+=1
      line_cpt+=1
  print("Data loaded :D")
  
  
  ######## Normalizing pixels
  train_data = train_data.astype('float32')
  test_data = test_data.astype('float32')
  train_data /= 255
  test_data /= 255
  train_label /= 255
  test_label /= 255
  
  ######## Reformating to match Quaternion format
  # Q = r + x + y + z
  # Q = 0 + R + G + B
  
  test_data = test_data.reshape(test_data.shape[0],32,32,3)
  test_label = test_label.reshape(test_data.shape[0],32,32,3)
  train_data = train_data.reshape(train_data.shape[0],32,32,3)
  train_label = train_label.reshape(train_data.shape[0],32,32,3)
  
  x=train_data.shape[0]
  y=train_data.shape[1]
  z=train_data.shape[2]
  zeros = np.zeros((x,y,z,1))
  train_data = np.concatenate((zeros, train_data), axis = 3)
  
  x=test_data.shape[0]
  y=test_data.shape[1]
  z=test_data.shape[2]
  zeros = np.zeros((x,y,z,1))
  test_data = np.concatenate((zeros, test_data), axis = 3)
  
  x=train_label.shape[0]
  y=train_label.shape[1]
  z=train_label.shape[2]
  zeros = np.zeros((x,y,z,1))
  train_label = np.concatenate((zeros, train_label), axis = 3)
  
  x=test_label.shape[0]
  y=test_label.shape[1]
  z=test_label.shape[2]
  zeros = np.zeros((x,y,z,1))
  test_label = np.concatenate((zeros, test_label), axis = 3)
  
  
  ######## Building model
  # A QuaternionConv2D layer has the same parameters than a real valued
  # convolutional layer. Be certain that the input can be divided by 4
  
  input_img = Input(shape=(32, 32, 4))
  x = QuaternionConv2D(32, kernel_size=(3, 3), activation='relu', padding='same')(input_img)
  x = MaxPooling2D((2, 2), padding='same')(x)
  x = QuaternionConv2D(16, (3, 3), activation='relu', padding='same')(x)
  
  encoded = MaxPooling2D((2, 2), padding='same')(x)
  
  x = QuaternionConv2D(16, (3, 3), activation='relu', padding='same')(encoded)
  x = UpSampling2D((2, 2))(x)
  x = QuaternionConv2D(32, kernel_size=(3,3), strides=(1, 1), activation='relu', padding='same')(x)
  x = UpSampling2D((2, 2))(x)
  decoded = QuaternionConv2D(1, (3, 3),activation='sigmoid', name='conv_last', padding='same')(x)
  
  ######## Compiling model
  autoencoder = Model(input_img, decoded)
  autoencoder.compile(optimizer='adam', loss='binary_crossentropy')
  
  ######## Training
  loss_history_cb = autoencoder.fit(train_data, train_label,
                  epochs=train_epoch,
                  batch_size=batch_size)
  
  ######## Recording Loss History
  loss_history = loss_history_cb.history["loss"]
  loss = np.array(loss_history)
  np.savetxt("loss_history_Q.txt", loss, delimiter=",")
  
  
  ######## Write the outputs for each images
  # Accordingly to the need of the script create_rgb.py
  #
  
  modulo_cpt =0
  output = autoencoder.predict(test_data[0:99])
  for element in output:
      for value in element:
          for other in value:
              for rgb in other:
                  if modulo_cpt %4 != 0:
                      output_test.write(str(float(rgb)*255)+" ")
                  modulo_cpt += 1
      output_test.write("
  ")
  output_test.close()
  
  modulo_cpt = 0
  output = autoencoder.predict(train_data[0:4900])
  for element in output:
      for value in element:
          for other in value:
              for rgb in other:
                  if modulo_cpt %4 != 0:
                      output_train.write(str(float(rgb)*255)+" ")
                  modulo_cpt +=1
      output_train.write("
  ")
  output_train.close()
  
  ######## Finally generate the reconstructed RGB pictures
  print("Reconstructing images : create_rgb.py "+str(size))
  create_rgb.reconstruct(str(size))
  print("That's all Folks !")