def discriminator(x, y, share=False): with tf.variable_scope("d_init", reuse=share): input_data = tf.concat([x, y], axis=3) with tf.variable_scope("discriminator", reuse=share): with tf.variable_scope("conv1", reuse=share): output = tf_tools.conv2d(input_data, discriminator_conv1_size, strides=[1, 2, 2, 1]) output = tf_tools.leaky_relu(output) with tf.variable_scope("conv2", reuse=share): output = tf_tools.conv2d(output, discriminator_conv2_size, strides=[1, 2, 2, 1]) output = tf_tools.leaky_relu(output) with tf.variable_scope("conv3", reuse=share): output = tf_tools.conv2d(output, discriminator_conv3_size, strides=[1, 2, 2, 1]) output = tf_tools.leaky_relu(output) with tf.variable_scope("conv4", reuse=share): output = tf_tools.conv2d(output, discriminator_conv4_size, strides=[1, 2, 2, 1]) output = tf_tools.leaky_relu(output) with tf.variable_scope("conv5", reuse=share): output = tf_tools.conv2d(output, discriminator_conv5_size, strides=[1, 2, 2, 1]) output = tf_tools.sigmoid(output) output = tf.reshape(output, shape=[-1, 1]) return output
def build(self, input, times=3): with tf.variable_scope(self.name): size = self.out_size[0] * self.out_size[1] output = tf_tools.layer(input, out_size=size, activation='relu', normal=True, name='layer1') output = tf.reshape( output, shape=[-1, self.out_size[0], self.out_size[1], 1]) out_channel = self.channel_min for index in range(times): output = tf_tools.conv2d(output, out_channel=out_channel, activation='relu', normal=True, name='conv_{}'.format(index)) out_channel = out_channel * self.channel_rate output = tf_tools.conv2d(output, out_channel=self.out_size[-1], name='output') self.var_list = tf.trainable_variables( scope=tf.get_variable_scope().name) return output
def build_rpn(self, net): self._anchor_component() rpn = tf_tools.conv2d(net, out_channel=512, activation='relu', name='rpn_conv') rpn_cls_score = tf_tools.conv2d(rpn, out_channel=self._num_anchors * 2, kernel_size=1, pad='VALID', name='rpn_cls_score') rpn_cls_score_reshape = _reshape_layer(rpn_cls_score, 2, 'rpn_cls_score_reshape') rpn_cls_prob_reshape = _softmax_layer(rpn_cls_score_reshape, "rpn_cls_prob_reshape") rpn_cls_prob = _reshape_layer(rpn_cls_prob_reshape, self._num_anchors * 2, "rpn_cls_prob") rpn_bbox_pred = tf_tools.conv2d(rpn, out_channel=self._num_anchors * 4, kernel_size=1, pad='VALID', name='rpn_bbox_pred') return rpn_cls_prob, rpn_bbox_pred, rpn_cls_score, rpn_cls_score_reshape
def build_network(self): x_enc = tf_tool.conv2d(self.input, out_channel=32, activation='leaky_relu', init='xavier', name="conv{}".format(0)) for idx in range(int(5)): originel_add = x_enc x_enc = tf_tool.conv2d(x_enc, out_channel=32, activation='leaky_relu', init='xavier', name="add_res_conv1_{}".format(idx)) x_enc = tf_tool.conv2d(x_enc, out_channel=32, activation='leaky_relu', init='xavier', name="add_res_conv2_{}".format(idx)) + originel_add add_enc = tf.reshape(x_enc, [self.batch_size, self.max_time, 32 * np.prod(self.states_shape)]) add_enc = tf_tool.layer(add_enc, out_size=256, activation='leaky_relu', init='xavier', name="layer{}".format(1)) lstm_in = add_enc lstm = tf.nn.rnn_cell.BasicLSTMCell(256, state_is_tuple=True) def make_init(batch_size): c_init = np.zeros((batch_size, lstm.state_size.c), np.float32) h_init = np.zeros((batch_size, lstm.state_size.h), np.float32) return [c_init, h_init] self.state_init = keydefaultdict(make_init) c_in = tf.placeholder(tf.float32, [None, lstm.state_size.c], name="lstm_c_in") h_in = tf.placeholder(tf.float32, [None, lstm.state_size.h], name="lstm_h_in") self.init_state = [c_in, h_in] state_in = tf.nn.rnn_cell.LSTMStateTuple(c_in, h_in) outputs, states = tf.nn.dynamic_rnn(lstm, lstm_in, initial_state=state_in, time_major=False) self.lstm_c, self.lstm_h = states acts_logit = tf_tool.layer(outputs, out_size=self.n_actions, init='xavier', name='acts_prob') self.acts_prob = tf.nn.softmax(acts_logit) self.acts_log_prob = tf.nn.log_softmax(acts_logit)
def build_network(self): x_enc = tf_tool.conv2d(self.input, activation='leaky_relu', out_channel=32, name="conv{}".format(0)) for idx in range(int(5)): originel_add = x_enc x_enc = tf_tool.conv2d(x_enc, out_channel=32, activation='leaky_relu', init='xavier', name="add_res_conv1_{}".format(idx)) x_enc = tf_tool.conv2d( x_enc, out_channel=32, activation='leaky_relu', init='xavier', name="add_res_conv2_{}".format(idx)) + originel_add add_enc = tf.reshape( x_enc, [self.batch_size, self.max_time, 32 * np.prod(self.states_shape)]) add_enc = tf_tool.layer(add_enc, out_size=256, activation='leaky_relu', init='xavier', name="layer{}".format(1)) lstm_in = add_enc lstm = tf.nn.rnn_cell.BasicLSTMCell(256, name='lstm', state_is_tuple=True) initial_state = lstm.zero_state(self.batch_size, dtype=tf.float32) outputs, states = tf.nn.dynamic_rnn(lstm, lstm_in, initial_state=initial_state, time_major=False) self.value = tf_tool.layer(outputs, out_size=1, name='value')
def build(self, input, times=3, reuse=False, normal=True): output = input with tf.variable_scope(self.name, reuse=reuse): out_channel = self.channel_min for index in range(times): output = tf_tools.conv2d(output, out_channel=out_channel, activation='relu', normal=normal, name='conv_{}'.format(index)) output = tf_tools.max_pool(output, name='pool_{}'.format(index)) out_channel = out_channel * self.channel_rate output = tf.layers.flatten(output) output = tf_tools.layer(output, out_size=self.out_size, name='output') self.var_list = tf.trainable_variables(scope=tf.get_variable_scope().name) return output
def generator(x, y): with tf.variable_scope("g_init"): #x = tf.tile(x, [1, 1, 1, channel]) input_data = tf.concat([x, y], axis=3) with tf.variable_scope("generator"): with tf.variable_scope("conv1"): output = tf_tools.conv2d(input_data, generator_conv1_size) output = tf_tools.leaky_relu(output) with tf.variable_scope("conv2"): output = tf_tools.conv2d(output, generator_conv2_size) output = tf_tools.leaky_relu(output) with tf.variable_scope("conv3"): output = tf_tools.conv2d(output, generator_conv3_size) output = tf_tools.leaky_relu(output) with tf.variable_scope("conv4"): output = tf_tools.conv2d(output, generator_conv4_size) output = tf_tools.leaky_relu(output) with tf.variable_scope("conv5"): output = tf_tools.conv2d(output, generator_conv5_size) output = tf_tools.tanh(output) return output
def bind_network(self): state_enc = tf_tool.conv2d(self.input, out_channel=32, activation='leaky_relu', init='xavier', name="conv{}".format(0)) action_enc = tf_tool.layer(tf.expand_dims(self.last_action, -1), activation='leaky_relu', init='xavier', out_size=32) action_enc = tf.reshape(action_enc, [-1, 1, 1, 32]) add_enc = state_enc + action_enc for idx in range(int(3)): add_enc = tf_tool.conv2d(add_enc, out_channel=32, activation='leaky_relu', init='xavier', name="add_enc_conv1_{}".format(idx)) for idx in range(int(8)): originel_add = add_enc add_enc = tf_tool.conv2d(add_enc, out_channel=32, activation='leaky_relu', init='xavier', name="add_res_conv1_{}".format(idx)) add_enc = tf_tool.conv2d( add_enc, out_channel=32, activation='leaky_relu', init='xavier', name="add_res_conv2_{}".format(idx)) + originel_add add_enc = tf.reshape( add_enc, [self.batch_size, self.max_time, 32 * np.prod(self.state_shape)]) add_enc = tf_tool.layer(add_enc, out_size=128, activation='leaky_relu', init='xavier', name="layer{}".format(1)) lstm_in = add_enc lstm = tf.nn.rnn_cell.BasicLSTMCell(512) def make_init(batch_size): c_init = np.zeros((batch_size, lstm.state_size.c), np.float32) h_init = np.zeros((batch_size, lstm.state_size.h), np.float32) return [c_init, h_init] self.state_init = keydefaultdict(make_init) c_in = tf.placeholder(tf.float32, [None, lstm.state_size.c], name="lstm_c_in") h_in = tf.placeholder(tf.float32, [None, lstm.state_size.h], name="lstm_h_in") self.init_state = [c_in, h_in] state_in = tf.nn.rnn_cell.LSTMStateTuple(c_in, h_in) outputs, states = tf.nn.dynamic_rnn(lstm, lstm_in, initial_state=state_in, time_major=False) self.lstm_c, self.lstm_h = states with tf.variable_scope('Actor'): self.acts = tf_tool.layer(tf.nn.relu(outputs), out_size=self.n_actions, init='xavier', name='acts_prob') self.acts_prob = tf.nn.softmax(self.acts) self.acts_log_prob = tf.nn.log_softmax(self.acts) with tf.variable_scope('Critic'): self.value = tf_tool.layer(outputs, out_size=1, name='value')
def bind_model(self): s_shape = tf.shape(self.state) batch_size, max_time = s_shape[0], s_shape[1] state = tf.concat([self.state, self.condition], axis=-1) #state = self.state s_shape = list(self.state_shape) s_shape[-1] = int(state.get_shape()[-1]) state = tf.reshape(state, [-1] + s_shape) last_action = self.last_action with tf.variable_scope('Policy'): state_enc = tf_tool.conv2d(state, out_channel=32, activation='leaky_relu', init='xavier', name="conv{}".format(0)) last_action_enc = tf_tool.layer(tf.expand_dims(last_action, -1), activation='leaky_relu', init='xavier', out_size=32) last_action_enc = tf.reshape(last_action_enc, [-1, 1, 1, 32]) add_enc = state_enc + last_action_enc for idx in range(int(3)): add_enc = tf_tool.conv2d(add_enc, out_channel=32, activation='leaky_relu', init='xavier', name="add_enc_conv1_{}".format(idx)) for idx in range(int(8)): originel_add = add_enc add_enc = tf_tool.conv2d(add_enc, out_channel=32, activation='leaky_relu', init='xavier', name="add_res_conv1_{}".format(idx)) add_enc = tf_tool.conv2d( add_enc, out_channel=32, activation='leaky_relu', init='xavier', name="add_res_conv2_{}".format(idx)) + originel_add add_enc = tf.reshape(add_enc, [ self.batch_size, self.max_time, 32 * np.prod(self.state_shape) ]) add_enc = tf_tool.layer(add_enc, out_size=256, activation='leaky_relu', init='xavier', name="layer{}".format(1)) lstm_in = add_enc lstm = tf.nn.rnn_cell.BasicLSTMCell(512, name='lstm') def make_init(batch_size): c_init = np.zeros((batch_size, lstm.state_size.c), np.float32) h_init = np.zeros((batch_size, lstm.state_size.h), np.float32) return [c_init, h_init] self.state_init = keydefaultdict(make_init) c_in = tf.placeholder(tf.float32, [None, lstm.state_size.c], name="lstm_c_in") h_in = tf.placeholder(tf.float32, [None, lstm.state_size.h], name="lstm_h_in") self.init_state = [c_in, h_in] state_in = tc.LSTMStateTuple(c_in, h_in) outputs, self.states = tf.nn.dynamic_rnn(lstm, lstm_in, initial_state=state_in, time_major=False) self.lstm_c, self.lstm_h = self.states with tf.variable_scope('Actor'): acts = tf_tool.layer(tf.nn.relu(outputs), out_size=self.n_actions, init='xavier', name='acts_prob') with tf.variable_scope('Critic'): value = tf_tool.layer(outputs, out_size=1, name='value') return acts, value
def buind_head(self): with tf.variable_scope("vgg_16"): with tf.variable_scope("conv1"): output = tf_tools.conv2d(self.input_img, out_channel=64, activation='relu', name='conv1_1/') output = tf_tools.conv2d(output, out_channel=64, activation='relu', name='conv1_2/') output = tf_tools.max_pool(output, name='pool1') with tf.variable_scope("conv2"): output = tf_tools.conv2d(output, out_channel=128, activation='relu', name='conv2_1/') output = tf_tools.conv2d(output, out_channel=128, activation='relu', name='conv2_2/') output = tf_tools.max_pool(output, name='pool2') with tf.variable_scope("conv3"): output = tf_tools.conv2d(output, out_channel=256, activation='relu', name='conv3_1/') output = tf_tools.conv2d(output, out_channel=256, activation='relu', name='conv3_2/') output = tf_tools.max_pool(output, name='pool3') with tf.variable_scope("conv4"): output = tf_tools.conv2d(output, out_channel=512, activation='relu', name='conv4_1/') output = tf_tools.conv2d(output, out_channel=512, activation='relu', name='conv4_2/') output = tf_tools.max_pool(output, name='pool4') with tf.variable_scope("conv5"): output = tf_tools.conv2d(output, out_channel=512, activation='relu', name='conv5_1/') output = tf_tools.conv2d(output, out_channel=512, activation='relu', name='conv5_2/') output = tf_tools.conv2d(output, out_channel=512, activation='relu', name='conv5_3/') return output