def _build_decoder(self): """ Builds the decoder's list""" decoder = [ DenseLayer(num_units=128), LeakyReLU(), DenseLayer(num_units=self.input_dim) ] return decoder
def _build_encoder(self): """ Buils the encoder's list """ encoder = [ DenseLayer(num_units=128, input_shape=self.input_dim), LeakyReLU(), DenseLayer(num_units=self.latent_factors) ] return encoder
def addLeakyReLU(self, **kwargs): """ Add leaky ReLU activation layer. (with leakiness=0.01) """ input_layer = self.input_layer if not self.all_layers \ else self.all_layers[-1] self.n_leaky_relu_layers += 1 name = "leaky_relu%i" % self.n_leaky_relu_layers new_layer = LeakyReLU(input_layer, name=name, **kwargs) self.all_layers += (new_layer, )
ReLU(), Dropout(0.3), FullyConnected(48, config.NUM_CLASSES, he_uniform_init, use_weight_norm=True), ] xavier_and_lrelu = [ FullyConnected(config.INPUT_DIM, 192, xavier_uniform_init, use_weight_norm=True, weight_decay=1e-7), BatchNorm(input_dim=192), LeakyReLU(), Dropout(0.4), FullyConnected(192, 96, xavier_uniform_init, use_weight_norm=True, weight_decay=1e-7), BatchNorm(input_dim=96), LeakyReLU(), Dropout(0.4), FullyConnected(96, 48, xavier_uniform_init, use_weight_norm=True), BatchNorm(input_dim=48), LeakyReLU(), Dropout(0.4), FullyConnected(48, config.NUM_CLASSES,
def network_definition(self): # (multi_views, time, self.batch_size, 3, self.img_h, self.img_w), self.x = tensor6() self.is_x_tensor4 = False img_w = self.img_w img_h = self.img_h n_gru_vox = 4 # n_vox = self.n_vox n_convfilter = [16, 32, 64, 64, 64, 64] n_fc_filters = [1024] n_deconvfilter = [64, 64, 64, 16, 8, 2] # Set the size to be 64x4x4x4 s_shape = (self.batch_size, n_gru_vox, n_deconvfilter[0], n_gru_vox, n_gru_vox) # Dummy 3D grid hidden representations prev_s = InputLayer(s_shape) input_shape = (self.batch_size, 3, img_w, img_h) s_shape_1d = ( cfg.batch, n_fc_filters[0], ) lstm1d_all = [] def get_viewfeats(x_curr): lstm1d_all.append(LSTM1D(x_curr, input_shape)) params_temp = get_trainable_params() self.params_lst.append(len(params_temp)) ''' count = 0 for p in params: count += 1 self.param_count print('num of params %d' %count) ''' return lstm1d_all[-1].feat() view_features_shape = (self.batch_size, n_fc_filters[0]) view_features, _ = theano.scan(get_viewfeats, sequences=[self.x]) self.view_features = view_features fc7 = InputLayer(view_features_shape) t_x_s_update = FCConv3DLayer( prev_s, fc7, (n_deconvfilter[0], n_deconvfilter[0], 3, 3, 3), isTrainable=True) t_x_s_reset = FCConv3DLayer( prev_s, fc7, (n_deconvfilter[0], n_deconvfilter[0], 3, 3, 3), isTrainable=True) rll = time_features[0] time_last = time_all[-1] reset_gate = SigmoidLayer(t_x_s_reset) rs = EltwiseMultiplyLayer(reset_gate, prev_s) t_x_rs = FCConv3DLayer(rs, fc7, (n_deconvfilter[0], n_deconvfilter[0], 3, 3, 3), isTrainable=True) def view_rec_test(x_curr, prev_s_tensor, prev_in_gate_tensor): count = 0 params = get_trainable_params() for p in params: count += 1 print('view rec test : num of params %d' % count) rect8_ = InputLayer(view_features_shape, x_curr) prev_s_ = InputLayer(s_shape, prev_s_tensor) t_x_s_update_ = FCConv3DLayer( prev_s_, rect8_, (n_deconvfilter[0], n_deconvfilter[0], 3, 3, 3), params=t_x_s_update.params, isTrainable=True) t_x_s_reset_ = FCConv3DLayer( prev_s_, rect8_, (n_deconvfilter[0], n_deconvfilter[0], 3, 3, 3), params=t_x_s_reset.params, isTrainable=True) update_gate_ = SigmoidLayer(t_x_s_update_) comp_update_gate_ = ComplementLayer(update_gate_) reset_gate_ = SigmoidLayer(t_x_s_reset_) rs_ = EltwiseMultiplyLayer(reset_gate_, prev_s_) t_x_rs_ = FCConv3DLayer( rs_, rect8_, (n_deconvfilter[0], n_deconvfilter[0], 3, 3, 3), params=t_x_rs.params, isTrainable=True) tanh_t_x_rs_ = TanhLayer(t_x_rs_) gru_out_ = AddLayer( EltwiseMultiplyLayer(update_gate_, prev_s_), EltwiseMultiplyLayer(comp_update_gate_, tanh_t_x_rs_)) return gru_out_.output, update_gate_.output s_update, _ = theano.scan( view_rec_test, sequences=[ view_features ], # along with images, feed in the index of the current frame outputs_info=[ tensor.zeros_like(np.zeros(s_shape), dtype=theano.config.floatX), tensor.zeros_like(np.zeros(s_shape), dtype=theano.config.floatX) ]) update_all = s_update[-1] s_all = s_update[0] s_last = s_all[-1] #s_last = np.random.rand(self.batch_size, n_gru_vox, n_deconvfilter[0], n_gru_vox, n_gru_vox) self.gru_s = InputLayer(s_shape, s_last) unpool7 = Unpool3DLayer(self.gru_s) self.conv7a = Conv3DLayer(unpool7, (n_deconvfilter[1], 3, 3, 3)) self.rect7a = LeakyReLU(self.conv7a) self.conv7b = Conv3DLayer(self.rect7a, (n_deconvfilter[1], 3, 3, 3)) self.rect7 = LeakyReLU(self.conv7b) self.res7 = AddLayer(unpool7, self.rect7) print('unpool7 => input_shape %s, output_shape %s)' % (unpool7._input_shape, unpool7._output_shape)) unpool8 = Unpool3DLayer(self.res7) conv8a = Conv3DLayer(unpool8, (n_deconvfilter[2], 3, 3, 3)) rect8a = LeakyReLU(conv8a) self.conv8b = Conv3DLayer(rect8a, (n_deconvfilter[2], 3, 3, 3)) self.rect8 = LeakyReLU(self.conv8b) self.res8 = AddLayer(unpool8, self.rect8) print('unpool8 => input_shape %s, output_shape %s)' % (unpool8._input_shape, unpool8._output_shape)) unpool12 = Unpool3DLayer(self.res8) conv12a = Conv3DLayer(unpool12, (n_deconvfilter[2], 3, 3, 3)) rect12a = LeakyReLU(conv12a) self.conv12b = Conv3DLayer(rect12a, (n_deconvfilter[2], 3, 3, 3)) self.rect12 = LeakyReLU(self.conv12b) self.res12 = AddLayer(unpool12, self.rect12) print('unpool12 => input_shape %s, output_shape %s)' % (unpool12._input_shape, unpool12._output_shape)) unpool9 = Unpool3DLayer(self.res12) self.conv9a = Conv3DLayer(unpool9, (n_deconvfilter[3], 3, 3, 3)) self.rect9a = LeakyReLU(self.conv9a) self.conv9b = Conv3DLayer(self.rect9a, (n_deconvfilter[3], 3, 3, 3)) self.rect9 = LeakyReLU(self.conv9b) self.conv9c = Conv3DLayer(unpool9, (n_deconvfilter[3], 1, 1, 1)) self.res9 = AddLayer(self.conv9c, self.rect9) print('unpool9 => input_shape %s, output_shape %s)' % (unpool9._input_shape, unpool9._output_shape)) unpool10 = Unpool3DLayer(self.res9) self.conv10a = Conv3DLayer(unpool10, (n_deconvfilter[4], 3, 3, 3)) self.rect10a = LeakyReLU(self.conv10a) self.conv10b = Conv3DLayer(self.rect10a, (n_deconvfilter[4], 3, 3, 3)) self.rect10 = LeakyReLU(self.conv10b) self.conv10c = Conv3DLayer(self.rect10a, (n_deconvfilter[4], 3, 3, 3)) self.res10 = AddLayer(self.conv10c, self.rect10) print('unpool9 => input_shape %s, output_shape %s)' % (unpool10._input_shape, unpool10._output_shape)) self.conv11 = Conv3DLayer(self.res10, (n_deconvfilter[5], 3, 3, 3)) #self.conv11 = TanhLayer(conv11) print( 'Conv11 = Conv3DLayer(x, (%s, 3, 3, 3) => input_shape %s, output_shape %s)' % (n_deconvfilter[5], self.conv11._input_shape, self.conv11._output_shape)) #self.conv11 = np.random.rand(cfg.batch, 128, 2, 128, 128) softmax_loss = SoftmaxWithLoss3D(self.conv11.output) self.softloss = softmax_loss self.loss = softmax_loss.loss(self.y) self.error = softmax_loss.error(self.y) self.params = get_trainable_params() self.output = softmax_loss.prediction() #update_all = [1,2,3] self.activations = [update_all]
def recurrence(x_curr, prev_s_tensor, prev_in_gate_tensor): # Scan function cannot use compiled function. input_ = InputLayer(input_shape, x_curr) conv1a_ = ConvLayer(input_, (n_convfilter[0], 7, 7), params=conv1a.params) rect1a_ = LeakyReLU(conv1a_) conv1b_ = ConvLayer(rect1a_, (n_convfilter[0], 3, 3), params=conv1b.params) rect1_ = LeakyReLU(conv1b_) pool1_ = PoolLayer(rect1_) conv2a_ = ConvLayer(pool1_, (n_convfilter[1], 3, 3), params=conv2a.params) rect2a_ = LeakyReLU(conv2a_) conv2b_ = ConvLayer(rect2a_, (n_convfilter[1], 3, 3), params=conv2b.params) rect2_ = LeakyReLU(conv2b_) conv2c_ = ConvLayer(pool1_, (n_convfilter[1], 1, 1), params=conv2c.params) res2_ = AddLayer(conv2c_, rect2_) pool2_ = PoolLayer(res2_) conv3a_ = ConvLayer(pool2_, (n_convfilter[2], 3, 3), params=conv3a.params) rect3a_ = LeakyReLU(conv3a_) conv3b_ = ConvLayer(rect3a_, (n_convfilter[2], 3, 3), params=conv3b.params) rect3_ = LeakyReLU(conv3b_) conv3c_ = ConvLayer(pool2_, (n_convfilter[2], 1, 1), params=conv3c.params) res3_ = AddLayer(conv3c_, rect3_) pool3_ = PoolLayer(res3_) conv4a_ = ConvLayer(pool3_, (n_convfilter[3], 3, 3), params=conv4a.params) rect4a_ = LeakyReLU(conv4a_) conv4b_ = ConvLayer(rect4a_, (n_convfilter[3], 3, 3), params=conv4b.params) rect4_ = LeakyReLU(conv4b_) pool4_ = PoolLayer(rect4_) conv5a_ = ConvLayer(pool4_, (n_convfilter[4], 3, 3), params=conv5a.params) rect5a_ = LeakyReLU(conv5a_) conv5b_ = ConvLayer(rect5a_, (n_convfilter[4], 3, 3), params=conv5b.params) rect5_ = LeakyReLU(conv5b_) conv5c_ = ConvLayer(pool4_, (n_convfilter[4], 1, 1), params=conv5c.params) res5_ = AddLayer(conv5c_, rect5_) pool5_ = PoolLayer(res5_) conv6a_ = ConvLayer(pool5_, (n_convfilter[5], 3, 3), params=conv6a.params) rect6a_ = LeakyReLU(conv6a_) conv6b_ = ConvLayer(rect6a_, (n_convfilter[5], 3, 3), params=conv6b.params) rect6_ = LeakyReLU(conv6b_) res6_ = AddLayer(pool5_, rect6_) pool6_ = PoolLayer(res6_) flat6_ = FlattenLayer(pool6_) fc7_ = TensorProductLayer(flat6_, n_fc_filters[0], params=fc7.params) rect7_ = LeakyReLU(fc7_) prev_s_ = InputLayer(s_shape_1d, prev_s_tensor) #print(self.prev_s_._output_shape) t_x_s_update_ = FCConv1DLayer(prev_s_, rect7_, n_fc_filters[0], params=self.t_x_s_update.params, isTrainable=True) t_x_s_reset_ = FCConv1DLayer(prev_s_, rect7_, n_fc_filters[0], params=self.t_x_s_reset.params, isTrainable=True) update_gate_ = SigmoidLayer(t_x_s_update_) comp_update_gate_ = ComplementLayer(update_gate_) reset_gate_ = SigmoidLayer(t_x_s_reset_) rs_ = EltwiseMultiplyLayer(reset_gate_, prev_s_) t_x_rs_ = FCConv1DLayer(rs_, rect7_, n_fc_filters[0], params=self.t_x_rs.params, isTrainable=True) tanh_t_x_rs_ = TanhLayer(t_x_rs_) gru_out_ = AddLayer( EltwiseMultiplyLayer(update_gate_, prev_s_), EltwiseMultiplyLayer(comp_update_gate_, tanh_t_x_rs_)) return gru_out_.output, update_gate_.output