recfield = 2 nfilter1 = 32 x = T.matrix(name='x', dtype=theano.config.floatX) y = T.matrix(name='y', dtype=theano.config.floatX) layer0 = x.reshape((bs, 3, imL, imL)) conv1 = ConvLayer(input = layer0, image_shape = (bs, 3, imL, imL), filter_shape =(nfilter1, 3, filterL, filterL), pool_shape = (recfield, recfield), flatten = True, actfun=tanh, tag='_conv1') outL = np.floor((imL-filterL+1.)/recfield).astype(np.int) fc2 = FCLayer(input=conv1.output(), n_in=nfilter1*outL*outL, n_out=imL*imL, actfun=None, tag='_fc2') params_cmb = conv1.params + fc2.params #params_cmb = fc0.params + fc2.params #ypred = fc2.output().reshape((bs, imL, imL)) ypred = fc2.output() model = GeneralModel(input=x, output=ypred, target=y, params=params_cmb, regularizers = 0, cost_func=mean_cross_entropy_map, error_func=mean_sqr_map) sgd = sgd_optimizer(data = msra, model = model, batch_size=bs, learning_rate=0.5,
filter_shape =(nfilter1, 3, filterL, filterL), pool_shape = (recfield, recfield), flatten = False, actfun=tanh, tag='_conv1') outL1 = np.floor((imL-filterL+1.)/recfield).astype(np.int) conv2 = ConvLayer(input = conv1.output(), image_shape=(bs, nfilter1, outL1, outL1), filter_shape = (nfilter2, nfilter1, filterL2, filterL2), pool_shape=(recfield, recfield), flatten=True, actfun=tanh, tag='_conv2') outL2 = np.floor((outL1-filterL2+1.)/recfield).astype(np.int) fc3 = FCLayer(input=conv2.output(), n_in=nfilter2*outL2*outL2, n_out=imL*imL, tag='_fc3') fc4 = FCLayer(input=fc3.output(), n_in=imL*imL, n_out=imL*imL, actfun=sigmoid, tag='_fc4') params_cmb = conv1.params + conv2.params + fc3.params + fc4.params #params_cmb = fc0.params + fc2.params #ypred = fc2.output().reshape((bs, imL, imL)) ypred = fc4.output() model = GeneralModel(input=x, data = msra, output=ypred, target=y, params=params_cmb, cost_func=mean_nneq_cross_entropy, error_func=mean_sqr, regularizers = 0, batch_size=bs) sgd = sgd_optimizer(data = msra,
def train_DNN(viper, lr=1e-3): bs = 50 n_train, n_feat = viper.train_feat.shape[:2] x = T.matrix(name='x', dtype=theano.config.floatX) y = T.vector(name='y', dtype=theano.config.floatX) # y = T.ivector(name='y') # layer0 = x.reshape(viper.train_feat.shape) if viper.tag is 'headsholder': lr = 1e-5 fc1 = FCLayer(input=x, n_in=n_feat, n_out=256, actfun=tanh, tag='_fc1') fc2 = FCLayer(input=fc1.output(), n_in=256, n_out=128, actfun=tanh, tag='_fc2') fc3 = FCLayer(input=fc2.output(), n_in=128, n_out=1, actfun=sigmoid, tag='_fc3') params_cmb = fc1.params + fc2.params + fc3.params # ypred = fc3.output() ypred = fc3.output().flatten() # regularization reg = 0 # reg = 0.001*((fc1.W**2).sum() \ # + (fc2.W**2).sum() \ # + (fc3.W**2).sum()) elif viper.tag is 'upperbody': lr = 1e-5 fc1 = FCLayer(input=x, n_in=n_feat, n_out=4096, actfun=tanh, tag='_fc1') fc2 = FCLayer(input=fc1.output(), n_in=4096, n_out=1024, actfun=tanh, tag='_fc2') fc3 = FCLayer(input=fc2.output(), n_in=1024, n_out=256, actfun=tanh, tag='_fc3') fc4 = FCLayer(input=fc3.output(), n_in=256, n_out=1, actfun=sigmoid, tag='_fc4') params_cmb = fc1.params + fc2.params + fc3.params + fc4.params # ypred = fc3.output() ypred = fc4.output().flatten() reg = 0 model = GeneralModel(input=x, output=ypred, target=y, params=params_cmb, regularizers=reg, cost_func=cross_entropy, error_func=abs_error, batch_size=bs) sgd = sgd_optimizer(data=viper, model=model, batch_size=bs, learning_rate=lr, n_epochs=200) train_loss, valid_loss, test_loss = sgd.fit_viper() pl.figure(1) pl.plot(train_loss, color='b', label='train erorr') pl.plot(valid_loss, color='g', label='valid error') pl.plot(test_loss, color='r', label='test error') pl.legend() pl.savefig('../data_viper/dnn_train.jpg') return model
def main(): ''' pipeline for supervised salience training ''' if os.path.isdir('../data_viper/'): datapath = '../data_viper/' else: datapath = '../data/' # prepare training data for supervised salience training datafile_viper = datapath + 'viper.pkl' if not os.path.isfile(datafile_viper): viper = DataMan_viper_small() viper.make_data() savefile(datafile_viper, viper) viper = loadfile(datafile_viper) viper = change_label(viper) viper.train_feat = viper.get_pixeldata('train') viper.valid_feat = viper.get_pixeldata('valid') viper.test_feat = viper.get_pixeldata('test') bs = 100 imL = 10 nfilter1 = 16 filterL = 3 x = T.tensor4(name='x', dtype=theano.config.floatX) y = T.ivector(name='y') # layer0 = x.reshape((bs, 3, imL, imL)) conv1 = ConvLayer(input=x, image_shape=(bs, 3, imL, imL), filter_shape=(nfilter1, 3, filterL, filterL), flatten=True, actfun=tanh, tag='_convpool1') # outL = np.floor((imL-filterL+1.)/recfield).astype(np.int) outL = imL-filterL+1 # nfilter3 = 16 # filterL3 = 3 # conv3 = ConvLayer(input=conv2.output(), image_shape=(bs, nfilter2, outL2, outL2), # filter_shape=(nfilter3, nfilter2, filterL3, filterL3), # flatten=True, # actfun=tanh, # tag='_conv3') # # outL3 = outL2-filterL3+1 fc2 = FCLayer(input=conv1.output(), n_in=nfilter1*outL*outL, n_out=256, actfun=tanh, tag='_fc2') fc3 = FCLayer(input=fc2.output(), n_in=256, n_out=10, actfun=sigmoid, tag='_fc3') params_cmb = conv1.params + fc2.params + fc3.params # ypred = fc3.output().flatten() ypred = fc3.output() model = GeneralModel(input=x, data=viper, output=ypred, target=y, params=params_cmb, regularizers=0, cost_func=negative_log_likelihood, error_func=sqr_error, batch_size=bs) sgd = sgd_optimizer(data=viper, model=model, batch_size=bs, learning_rate=0.001, n_epochs=500) sgd.fit_viper() filepath = datapath + 'model/model.pkl' savefile(filepath, model) os.system('ls -lh ' + filepath)