1
+ import theano
2
+ import numpy as np
3
+ import scipy as sp
4
+ import pickle
5
+ import sys ,os
6
+ import glob
7
+ import optparse
8
+ HOME = os .environ ['HOME' ]
9
+ def parse_list (option , opt , value , parser ):
10
+ setattr (parser .values , option .dest , value .split (',' ))
11
+
12
+ if __name__ == "__main__" :
13
+ ### Hyperparameters and model save path
14
+ parser = optparse .OptionParser ()
15
+ parser .add_option ("--train" , action = "store_true" ,dest = "train_bool" ,default = False ,help = "Invoke training" )
16
+ parser .add_option ("--learning-rate" ,help = "learning rate" ,dest = "learning_rate" ,type = float ,default = 0.1 )
17
+ parser .add_option ("--noise-factor" ,help = "noise" ,dest = "noise_factor" ,type = float ,default = 0.0 )
18
+ parser .add_option ("--cool" ,action = "store_true" ,dest = "cool" ,default = False ,help = "Cool Learning Rate" )
19
+ parser .add_option ("--epochs" ,help = "epochs" ,dest = "epochs" ,type = int ,default = 1 )
20
+ parser .add_option ("--home-dir" ,help = "Home Directory" ,dest = "home_dir" ,type = str ,default = '/Users/talathi1/Work/Python/Git_Folder/caffe-tools/keras' )
21
+ parser .add_option ("--save-dir" ,help = "Save Directory" ,dest = "save_path" ,type = str ,default = None )
22
+ parser .add_option ("--model-file" ,help = "Trained Model Pickle File" ,dest = "weight_path" ,type = str ,default = None )
23
+ parser .add_option ("--memo" ,help = "Memo" ,dest = "base_memo" ,type = str ,default = None )
24
+ (opts ,args )= parser .parse_args ()
25
+
26
+ ## Example of training command:
27
+ #python mnist_conv_autoencoder.py -C --save-dir /Users/talathi1/Work/Python/Models/Test_Models --memo test --epochs 1 --data-path /Users/talathi1/Work/DataSets/mnist.pkl.gz --learning-rate 0.01 --train --classify --unpool_type 3
28
+
29
+ if not os .path .isdir (opts .home_dir ):
30
+ print ('Keras home directory not set' )
31
+ sys .exit (0 )
32
+ sys .path .append ('home_dir' )
33
+
34
+ import candle .candle_helper_functions as hf
35
+ reload (hf )
36
+ maps = hf .autoencoder_preprocess ()
37
+ #from keras.Helpermodules.mnist_autoencoder_helper_functions import mnist_conv_deconv_simple, mnist_conv_deconv_complex,mnist_autoencoder_preprocess,generate_figure
38
+
39
+ from keras .optimizers import SGD ,RMSprop
40
+ from keras .datasets import mnist
41
+ from keras .callbacks import LearningRateScheduler ,ModelCheckpoint
42
+ from keras .regularizers import l2 ,WeightRegularizer
43
+ from keras import callbacks
44
+ from keras .layers .advanced_activations import ELU
45
+ from keras .preprocessing .image import ImageDataGenerator
46
+
47
+ batch_size = 16
48
+ ##### Read Data ########
49
+ print ('Reading Data...' )
50
+ data_file = '%s/Work/DataSets/CANDLE/sim-numpy.npy' % HOME ### can code to read at the terminal
51
+ print 'Data File: %s' % data_file
52
+ print 'Data Format: [Num Samples, Num Molecules, Num Atoms, Position]'
53
+
54
+ X = np .load (data_file ) ### Data is: Samples, Molecules, Atoms, x-pos,y-pos,z-pos
55
+ ## Take center of mass for atoms:
56
+ X_A = X .mean (axis = 2 ) ## Data is: Samples, Molecules, x-pos,y-pos,z-pos
57
+ #X_train=X_A.reshape(X_A.shape[0],X_A.shape[1]*X_A.shape[2])
58
+ X_train = X_A [:,:,2 ] ## only consider z-dimension
59
+ y_train = X_train .copy ()
60
+ input_dim = X_train .shape [1 ]
61
+ mu , sigma = np .mean (X_train ), np .std (X_train )
62
+ mu = 0.0 ;sigma = 1.0
63
+ X_train = maps .renormalize (X_train ,mu ,sigma )
64
+ datagen = hf .ImageNoiseDataGenerator (corruption_level = opts .noise_factor ) ## Add some corruption to input data ## idead for denoising auto encoder
65
+
66
+ print ('X_train type and shape:' , X_train .dtype , X_train .shape )
67
+ print ('X_train.min():' , X_train .min ())
68
+ print ('X_train.max():' , X_train .max ())
69
+
70
+ ### Define Model, Solver and Compile ##########
71
+ print ('Define the model and compile' )
72
+ opt = SGD (lr = opts .learning_rate , decay = 0.0 , momentum = 0.975 , nesterov = True )
73
+
74
+ print ('using mlp network' )
75
+ model_type = 'mlp'
76
+ hidden_layers = [512 ,256 ,128 ,64 ,32 ,16 ]
77
+ model = hf .dense_auto (weights_path = opts .weight_path ,input_shape = (input_dim ,),nonlinearity = 'elu' ,hidden_layers = hidden_layers )
78
+
79
+ memo = '%s_%s_%0.5f' % (opts .base_memo ,model_type ,opts .learning_rate )
80
+
81
+ print 'Autoencoder Regression problem'
82
+ model .compile (optimizer = 'adadelta' , loss = 'mean_squared_error' )
83
+
84
+ #### Print Model Configuration ###########
85
+ num_layers = len (model .layers )
86
+ print '*' * 10 ,'Model Configuration' ,'*' * 10
87
+ for i in range (len (model .layers )):
88
+ print i ,': ' ,model .layers [i ].name , ':' , model .layers [i ].output_shape [:]
89
+
90
+ ### Set up for Training and Validation
91
+ total_epochs = opts .epochs
92
+ initial_lrate = opts .learning_rate
93
+ if opts .cool :
94
+ drop = 0.5
95
+ else :
96
+ drop = 1.0
97
+
98
+ epochs_drop = 1 + int (np .floor (total_epochs / 3 ))
99
+
100
+ def step_decay (epoch ):
101
+ global initial_lrate ,epochs_drop ,drop
102
+ lrate = initial_lrate * np .power (drop , np .floor ((1 + epoch )/ epochs_drop ))
103
+ return lrate
104
+ lr_scheduler = LearningRateScheduler (step_decay )
105
+
106
+ #### Train the Model
107
+ if opts .train_bool :
108
+ history = callbacks .History ()
109
+ if opts .save_path != None :
110
+ model_file = '%s/%s.hdf5' % (opts .save_path ,memo )
111
+ checkpointer = ModelCheckpoint (filepath = model_file , verbose = 1 )
112
+ callbacks = [history ,lr_scheduler ,checkpointer ]
113
+ else :
114
+ callbacks = [history ,lr_scheduler ]
115
+ model .fit_generator (datagen .flow (X_train , y_train , batch_size = batch_size ),\
116
+ samples_per_epoch = X_train .shape [0 ],nb_epoch = total_epochs ,callbacks = callbacks ,verbose = 1 )
117
+ loss_data = {'train' : history .history ['loss' ]}
118
+ if opts .save_path != None :
119
+ loss_file = '%s/%s.pkl' % (opts .save_path ,memo )
120
+ o = open (loss_file ,'wb' )
121
+ pickle .dump (loss_data ,o )
122
+ o .close ()
123
+
124
+
0 commit comments