def create_resnet(x, num_blocks, num_outputs, training, activation=tf.nn.elu, output_activation=None, bottleneck=False, name=None, reuse=False): create_block = create_bottleneck_block if bottleneck else create_resnet_block unit = 256 if bottleneck else 64 name = "ResNET" if name is None else name with tf.variable_scope(name, reuse=reuse): b = layers.conv2d(x, 64, kernel_size=7, strides=2, activation=activation, padding="SAME", kernel_initializer=xavier_initializer()) b = layers.max_pooling2d(b, pool_size=3, strides=2, padding="SAME") for i, num_repeats in enumerate(num_blocks): b = create_block(b, unit*2**i, training, activation, skip_connection=True) for _ in range(num_repeats-1): b = create_block(b, unit*2**i, training, activation) b = downsample(b, unit*2**(i+1), training) # use global average pooling b = layers.conv2d(b, num_outputs, kernel_size=1, activation=None, padding="SAME") fts = tf.reduce_mean(b, [1, 2]) if output_activation is not None: fts = output_activation(fts) return fts
def _squeezenet2d(flat_input, keep_prob, n_classes): w_ini, b_ini, r_ini = initializers() x_multichannel = tf.reshape( flat_input, [-1, conf.img_dim[0], conf.img_dim[1], conf.num_ch]) net = conv2d( x_multichannel, filters=96, kernel_size=7, name='conv1', # keras SqueezeNet uses 3x3 kernel kernel_initializer=w_ini, bias_initializer=b_ini, kernel_regularizer=r_ini) net = max_pooling2d(net, pool_size=3, strides=2, name='maxpool1') net = fire_module2d(net, 16, 64, name='fire2') net = fire_module2d(net, 16, 64, name='fire3') net = fire_module2d(net, 32, 128, name='fire4') net = max_pooling2d(net, pool_size=3, strides=2, name='maxpool4') net = fire_module2d(net, 32, 128, name='fire5') net = fire_module2d(net, 48, 192, name='fire6') net = fire_module2d(net, 48, 192, name='fire7') net = fire_module2d(net, 64, 256, name='fire8') net = max_pooling2d(net, pool_size=3, strides=2, name='maxpool8') net = fire_module2d(net, 64, 256, name='fire9') net = tf.nn.dropout(net, keep_prob=keep_prob, name='dropout9') net = conv2d(net, n_classes, 1, 1, name='conv10', kernel_initializer=w_ini, bias_initializer=b_ini, kernel_regularizer=r_ini) logits = tf.reduce_mean(net, axis=[1, 2], name='global_avgpool10') return logits
def add_inference(self, inputs, training, nclass): df = 'channels_first' if self.data_format == 'NCHW' else 'channels_last' conv1 = layers.conv2d(inputs=inputs, filters=32, kernel_size=[5, 5], padding="same", data_format=df, activation=tf.nn.relu) pool1 = layers.max_pooling2d(inputs=conv1, pool_size=[2, 2], strides=2, data_format=df) conv2 = layers.conv2d(inputs=pool1, filters=64, kernel_size=[3, 3], padding="same", data_format=df, activation=tf.nn.relu) pool2 = layers.max_pooling2d(inputs=conv2, pool_size=[2, 2], strides=2, data_format=df) pool2_flat = tf.reshape(pool2, [-1, 7 * 7 * 64]) dense = layers.dense(inputs=pool2_flat, units=1024, activation=tf.nn.relu) dropout = layers.dropout(inputs=dense, rate=0.4, training=training) logits = layers.dense(inputs=dropout, units=nclass) return logits
def encode(self, x, z_dim, training=False): im_h, im_w, im_c = self.image_shape with tf.variable_scope("encoder", reuse=tf.AUTO_REUSE): h = x h = tf.reshape(h, (-1, im_h, im_w, im_c)) # TODO: h = tfl.conv2d( h, self.kernel_num, self.kernel_size, strides=1, padding="same") h = tf.nn.relu(h) h = tfl.conv2d( h, self.kernel_num, self.kernel_size, strides=2, padding="same") h = tf.nn.relu(h) # TODO: tu troche zmienilem h = tfl.conv2d( h, self.kernel_num * 2, self.kernel_size, strides=2, padding="same") h = tf.nn.relu(h) h = tfl.conv2d( h, self.kernel_num * 2, self.kernel_size, strides=2, padding="same") h = tf.nn.relu(h) h = tfl.flatten(h) h = tfl.dense(h, units=self.h_dim, activation=tf.nn.relu) z_mean = tfl.dense(h, units=z_dim, name='z_mean') # z_mean = tfl.batch_normalization(z_mean, training=training) return z_mean
def __discriminator__(self, image_input): with tf.variable_scope("discriminator", reuse=tf.AUTO_REUSE): cnn1 = layers.conv2d(image_input, filters=64, kernel_size=5, strides=(2, 2), padding="SAME") cnn1 = tf.nn.leaky_relu(cnn1) cnn2 = layers.conv2d(cnn1, filters=128, kernel_size=5, strides=(2, 2), padding="SAME") cnn2 = tf.nn.leaky_relu(cnn2) cnn3 = layers.conv2d(cnn2, filters=256, kernel_size=5, strides=(2, 2), padding="SAME") cnn3 = tf.nn.leaky_relu(cnn3) logits = layers.dense(layers.flatten(cnn3), 1) #print_Arch: print("Discriminator-Architecture") print("input:{}".format(image_input.shape)) print("cnn1:{}".format(cnn1.shape)) print("cnn2:{}".format(cnn2.shape)) print("cnn3:{}".format(cnn3.shape)) print("output:{}".format(logits.shape)) return logits
def _cnn(self, x): x = tf.cast(x, tf.float32) / 255. x = layers.conv2d(x, filters=32, kernel_size=8, strides=(4, 4), kernel_initializer=init, activation=tf.nn.relu) x = layers.conv2d(x, filters=64, kernel_size=4, strides=(2, 2), kernel_initializer=init, activation=tf.nn.relu) x = layers.conv2d(x, filters=64, kernel_size=3, strides=(1, 1), kernel_initializer=init, activation=tf.nn.relu) x = layers.flatten(x) return layers.dense(x, units=512, kernel_initializer=init, activation=tf.nn.relu)
def conv_net(inputs, conv1_kernels, kernel_size1, pool_size1, conv2_kernels, kernel_size2, pool_size2): # *** WARNING: HARDCODED SHAPES, CHANGE ACCORDINGLY *** in_layer = tf.reshape(inputs, [-1, 28, 28, 1]) # 1st conv layer conv1 = layers.conv2d(inputs=in_layer, filters=conv1_kernels, kernel_size=kernel_size1, padding='same', activation=tf.nn.relu) # # 1st pooling layer # pool1 = layers.max_pooling2d(inputs=conv1, # pool_size=pool_size1, # strides=2) # 2nd conv layer conv2 = layers.conv2d(inputs=conv1, filters=conv2_kernels, kernel_size=kernel_size2, padding='same', activation=tf.nn.relu) # # 2nd pooling layer # pool2 = layers.max_pooling2d(inputs=conv2, # pool_size=pool_size2, # strides=2) fl = layers.Flatten()(conv2) return fl
def __init__(self, sparse, guidance_map, params, reuse=False): with tf.variable_scope("Local", reuse=reuse) as scope: sparse = tf.reshape(sparse, [-1, 200, 200, 1]) guidance_map = tf.reshape(guidance_map, [-1, 200, 200, 1]) x = sparse + guidance_map x = conv2d(x, 32, (3, 3), strides=(2, 2), padding='same') x = relu(x) x = conv2d(x, 64, (3, 3), strides=(2, 2), padding='same') x = relu(x) x = conv2d_transpose(x, 64, (5, 5), strides=(2, 2), padding='same', use_bias=False) x = batch_normalization(x) x = relu(x) x = conv2d_transpose(x, 2, (5, 5), strides=(2, 2), padding='same', use_bias=False) self.output = x self.parameters = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope="Local")
def __init__(self, images, sparse, params, reuse=False): with tf.variable_scope("Global", reuse=reuse) as scope: sparse = tf.reshape(sparse, [-1, 200, 200, 1]) images = tf.reshape(images, [-1, 200, 200, 3]) x = tf.concat([images, sparse], axis=3) x = tf.cast(x, dtype=tf.float32) x = conv2d(x, 32, (3, 3), strides=(2, 2), padding='same') x = relu(x) x = conv2d(x, 64, (3, 3), strides=(2, 2), padding='same') x = relu(x) x = conv2d_transpose(x, 64, (5, 5), strides=(2, 2), padding='same', use_bias=False) x = batch_normalization(x) x = relu(x) x = conv2d_transpose(x, 3, (5, 5), strides=(2, 2), padding='same', use_bias=False) self.output = x self.parameters = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope="Global")
def res_block_2d(x, kernel_size, training, batch_norm=True): assert len(x.shape) == 4, "Input tensor must be 4-dimensional." filters = int(x.shape[3]) y = ly.conv2d(inputs=x, filters=filters, kernel_size=kernel_size, strides=1, padding='same') if batch_norm: y = ly.batch_normalization(y, training=training) y = k.layers.PReLU()(y) y = ly.conv2d(inputs=y, filters=filters, kernel_size=kernel_size, strides=1, padding='same') if batch_norm: y = ly.batch_normalization(y, training=training) return tf.add(x, y)
def __init__(self, model_name): self.state_size = state_size self.action_size = action_size self.input = tf.placeholder(shape=[None, self.state_size[0], self.state_size[1], self.state_size[2]], dtype=tf.float32) self.input_normalize = (self.input - (255.0 / 2)) / (255.0 / 2) with tf.variable_scope(name_or_scope=model_name): self.conv1 = layer.conv2d(inputs=self.input_normalize,filters=32, activation=tf.nn.relu,kernel_size=[8,8], strides=[4,4],padding="SAME") self.conv2 = layer.conv2d(inputs=self.conv1,filters=64, activation=tf.nn.relu,kernel_size=[4,4], strides=[2,2],padding="SAME") self.conv3 = layer.conv2d(inputs=self.conv2,filters=64, activation=tf.nn.relu,kernel_size=[3,3], strides=[1,1],padding="SAME") self.flat = layer.flatten(self.conv3) self.fc1 = layer.dense(self.flat,512,activation=tf.nn.relu) self.Q_Out = layer.dense(self.fc1,self.action_size,activation=None) self.predict = tf.argmax(self.Q_Out, 1) self.target_Q = tf.placeholder(shape=[None, self.action_size], dtype=tf.float32) self.loss = tf.losses.huber_loss(self.target_Q, self.Q_Out) self.UpdateModel = tf.train.AdamOptimizer(learning_rate).minimize(self.loss) self.trainable_var = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, model_name)
def _build_layers(self, inputs, num_outputs, options): with tf.name_scope("KhanElibolModel"): last_layer = layers.conv2d( inputs, 16, (4, 4), activation=tf.nn.relu) last_layer = layers.conv2d( last_layer, 32, (2, 2), activation=tf.nn.relu) last_layer = flatten(last_layer) last_layer = layers.dense( last_layer, 256, kernel_initializer=normc_initializer(0.01), activation = tf.nn.relu) output = layers.dense( last_layer, num_outputs, kernel_initializer=normc_initializer(0.01), activation = None) return output, last_layer
def build_discriminator(image, reuse=False): with tf.variable_scope("discriminator") as scope: if reuse: tf.get_variable_scope().reuse_variables() # t = image t = conv2d(inputs=t, filters=64, kernel_size=[5, 5], strides=2, padding="same", activation=my_leaky_relu) t = batch_normalization(inputs=t) t = conv2d(inputs=t, filters=128, kernel_size=[5, 5], strides=2, padding="same", activation=my_leaky_relu) t = batch_normalization(inputs=t) t = dropout(inputs=t, rate=DROP_RATE) t = flatten(inputs=t) t = dense(inputs=t, units=1, activation=tf.sigmoid) decision = t #print("\nD output shape: {}".format(decision.shape)) return decision
def deconv_block(self, input, filters, conv, padding, scope): with tf.variable_scope(scope): deconv1 = conv2d_transpose(input, filters, kernel_size=(3, 3), strides=[2, 2], padding=padding) deconv_shape = tf.shape(deconv1) conv_shape = tf.shape(conv) offsets = [ 0, (conv_shape[1] - deconv_shape[1]) // 2, (conv_shape[2] - deconv_shape[2]) // 2, 0 ] size = [-1, deconv_shape[1], deconv_shape[2], filters] conv_crop = tf.slice(conv, offsets, size) conv1 = tf.concat([deconv1, conv_crop], 3) bn = batch_normalization(conv1) drop = dropout(bn, .25) conv2 = conv2d(drop, filters, kernel_size=(3, 3), activation=tf.nn.relu, name='middle1', padding="SAME") conv3 = conv2d(conv2, filters, kernel_size=(3, 3), activation=tf.nn.relu, name='middle2', padding="SAME") return conv3
def _bottleneck_brick(incoming, nb_filters, is_training, scope, trainable=True): """ Code brick: conv --> conv . """ with tf.variable_scope(scope): code1 = layers.conv2d(incoming, filters=nb_filters, kernel_size=1, strides=1, padding='same', kernel_initializer=he_init, bias_initializer=b_init) code1_bn = layers.batch_normalization(code1, training=is_training, trainable=trainable) code1_act = tf.nn.relu(code1_bn) code2 = layers.conv2d(code1_act, filters=nb_filters, kernel_size=1, strides=1, padding='same', kernel_initializer=he_init, bias_initializer=b_init) code2_bn = layers.batch_normalization(code2, training=is_training, trainable=trainable) code2_act = tf.nn.relu(code2_bn) return code2_act
def main_branch(net, ref, depth, pooling, activation, is_training, lyr_name, data_format='channels_first', init=he_init): net_ = L.conv2d(net, depth, [5, 5], strides=1, padding='SAME', kernel_initializer=he_init, data_format=data_format, name='{}_Main_W1'.format(lyr_name)) net_ = activation(net_, name='{}_Main_A1'.format(lyr_name)) net_ = batch_norm(net_, is_training) net_ = L.conv2d(net_, depth, [5, 5], strides=1, padding='SAME', kernel_initializer=he_init, data_format=data_format, name='{}_Main_W2'.format(lyr_name)) net_ = batch_norm(net_, is_training) net_ = tf.add(net_, ref, name='{}_RefMainSum'.format(lyr_name)) net_ = activation(net_, name='{}_Main_A2'.format(lyr_name)) return net_
def _model_fn(self, input_shape, output_shape): X = tf.placeholder(tf.float32, (None, ) + input_shape, name="state") Y = tf.placeholder(tf.float32, (None, ) + output_shape, name="action_true") nn = X nn = L.conv2d(nn, 32, 8, strides=4, name="block1/conv1", activation=N.relu) nn = L.conv2d(nn, 64, 4, strides=2, name="block2/conv1") nn = L.conv2d(nn, 64, 3, strides=1, name="block3/conv1") nn = L.flatten(nn, name="flatten") nn = L.dense(nn, 512, activation=N.relu, name="fc1") value = L.dense(nn, 64, activation=N.relu, name="value_fc2") value = L.dense(value, 1, name="value") advantage = L.dense(nn, 64, activation=N.relu, name="advantage_fc2") advantage = L.dense(advantage, output_shape[0], name="advantage") q = tf.add( value, (advantage - tf.reduce_mean(advantage, axis=1, keepdims=True)), name="action") Y_pred = q return X, Y, Y_pred
def discriminator(x, reuse=False, alpha=0.2, training=True): """ Discriminator model, taking `x` as input. """ with tf.variable_scope('discriminator', reuse=reuse): x = conv2d(x, 32, 5, 2, padding='same') x = tf.maximum(alpha * x, x) x = conv2d(x, 64, 5, 2, padding='same') x = batch_normalization(x, training=training) x = tf.maximum(alpha * x, x) x = conv2d(x, 128, 5, 2, padding='same') x = batch_normalization(x, training=training) x = tf.maximum(alpha * x, x) x = conv2d(x, 256, 5, 2, padding='same') x = batch_normalization(x, training=training) x = tf.maximum(alpha * x, x) flatten = tf.reshape(x, (-1, 4 * 4 * 256)) logits = dense(flatten, 1) out = tf.sigmoid(logits) return logits
def _x(ip): x = batch_normalization(ip, **self.bn_kwargs) x = tf.nn.relu(x) if self.bottleneck: inter_channel = nb_filter * 4 x = conv2d(x, inter_channel, (1, 1), kernel_initializer='he_normal', padding='same', use_bias=False, **self.conv_kwargs) x = batch_normalization(x, **self.bn_kwargs) x = tf.nn.relu(x) x = conv2d(x, nb_filter, (3, 3), kernel_initializer='he_normal', padding='same', use_bias=False, **self.conv_kwargs) if self.dropout_rate: x = dropout(x, self.dropout_rate, training=self.training) return x
def generator(inp0, dim, name, reuse): # inp0 = tf.placeholder(tf.float32, [None, G.size, G.size, 4]) # G.size == 2 ** 8 with tf.variable_scope(name, reuse=reuse): ten1 = tl.conv2d(inp0, 1 * dim, 4, 2, 'same', activation=tf.nn.leaky_relu) ten2 = tl.conv2d(ten1, 2 * dim, 4, 2, 'same', activation=tf.nn.leaky_relu) ten3 = tl.conv2d(ten2, 4 * dim, 4, 2, 'same', activation=tf.nn.leaky_relu) ten4 = tl.conv2d(ten3, 8 * dim, 4, 2, 'same', activation=tf.nn.leaky_relu) ten5 = tl.conv2d(ten4, 16 * dim, 4, 2, 'same', activation=tf.nn.leaky_relu) ten6 = tl.conv2d(ten5, 32 * dim, 4, 2, 'same', activation=tf.nn.leaky_relu) ten7 = tf.pad(ten6, paddings=tf.constant([(0, 0), (2, 2), (2, 2), (0, 0)]), mode='REFLECT') ten7 = tl.conv2d(ten7, 32 * dim, 3, 1, 'valid', activation=leRU_batch_norm) ten7 = tl.conv2d(ten7, 32 * dim, 3, 1, 'valid', activation=leRU_batch_norm) ten7 = ten6 + ten7 ten6 = tl.conv2d_transpose(ten7, 64 * dim, 4, 2, 'same', activation=leRU_batch_norm) ten5 = tl.conv2d_transpose(ten6, 32 * dim, 4, 2, 'same', activation=leRU_batch_norm) ten4 = tl.conv2d_transpose(ten5, 16 * dim, 4, 2, 'same', activation=leRU_batch_norm) ten3 = tl.conv2d_transpose(ten4, 8 * dim, 4, 2, 'same', activation=leRU_batch_norm) ten2 = tl.conv2d_transpose(ten3, 4 * dim, 4, 2, 'same', activation=leRU_batch_norm) ten1 = tl.conv2d_transpose(ten2, 2 * dim, 4, 2, 'same', activation=leRU_batch_norm) ten1 = tl.conv2d(tf.concat((ten1, inp0), axis=3), 1 * dim, 3, 1, 'same', activation=leRU_batch_norm) ten1 = tl.conv2d(ten1, 3, 3, 1, 'same', activation=tf.nn.tanh) ten1 = ten1 * 0.505 + 0.5 return ten1
def create_residual_se_block(prefix, x, out_ch, training, stride=1, proj=False): # X --> CONV + BN + RELU + CONV + BN y = conv2d(x, out_ch, 3, padding=PADDING, strides=stride, data_format=DATA_FORMAT, name=prefix+'_conv1_3x3') y = batch_norm(y, decay=0.9, scale=True, epsilon=BN_EPS, is_training=training, data_format='NCHW') # batch_normalization(y, 1, name=prefix+'_bn1', training=training, epsilon=BN_EPS) y = tf.nn.relu(y) y = conv2d(y, out_ch, 3, padding=PADDING, strides=1, data_format=DATA_FORMAT, name=prefix+'_conv2_3x3') y = batch_norm(y, decay=0.9, scale=True, epsilon=BN_EPS, is_training=training, data_format='NCHW') # batch_normalization(y, 1, name=prefix+'_bn1', training=training, epsilon=BN_EPS) if proj: x = conv2d(x, out_ch, 1, padding=PADDING, strides=stride, data_format=DATA_FORMAT, name=prefix+'_proj_conv') x = batch_norm(x, decay=0.9, scale=True, is_training=training, epsilon=BN_EPS, data_format='NCHW') gap = create_squeeze_excitation_block(x, 32, 'se') y = gap * y y = x + y y = tf.nn.relu(y) return y
def res_block_2d(x, kernel_size, activation, training, batch_norm=True): assert len(x.shape) == 4, "Input tensor must be 4-dimensional." activation = activation.lower() filters = int(x.shape[3]) y = ly.conv2d(inputs=x, filters=filters, kernel_size=kernel_size, strides=1, padding='same') if batch_norm: y = ly.batch_normalization(y, training=training) y = nonlinear[activation](y) y = ly.conv2d(inputs=y, filters=filters, kernel_size=kernel_size, strides=1, padding='same') if batch_norm: y = ly.batch_normalization(y, training=training) return tf.add(x, y)
def dense_block(x, iter, two_conv, one_conv, is_train=False, name='denseblock'): with tf.variable_scope(name): net = x for i in range(iter): x = tl.batch_normalization(net, trainable=is_train, name=name + '_bn1/' + str(i)) x = tf.nn.relu(x) x = tl.conv2d(x, one_conv, (1, 1), padding='same', name=name + '_conv1/' + str(i)) x = tl.batch_normalization(x, trainable=is_train, name=name + '_bn2/' + str(i)) x = tf.nn.relu(x) x = tl.conv2d(x, two_conv, (3, 3), (1, 1), padding='same', name=name + '_conv2/' + str(i)) net = tf.concat([x, net], -1) return net
def __init__(self, state_size, action_size): self.states = tf.placeholder(tf.float32, shape=[None, *state_size], name="states") self.labels = tf.placeholder( tf.int32, shape=[None, 1], name="labels") # size 1 because sparse loss is used. # conv1 = layers.conv2d(self.states, filters=16, kernel_size=(8, 8), strides=(4, 4), activation='relu', # name="conv1"), # conv2 = layers.conv2d(conv1, filters=32, kernel_size=(4, 4), strides=(2, 2), activation='relu', name="conv2"), # flatten = layers.flatten(conv2), # dense = layers.dense(flatten, 256, activation='relu', name="features"), # # self.logits = layers.dense(dense, action_size, name="logits") # self.value = layers.dense(dense, 1, name="values") # conv1 = conv2d(self.states, filters=32, kernel_size=(3, 3), name='conv1') with tf.variable_scope('layers'): conv1 = layers.conv2d(self.states, filters=32, kernel_size=(3, 3), activation='relu', name='conv1') conv2 = layers.conv2d(conv1, filters=64, kernel_size=(3, 3), activation='relu', name='conv2') max_pool = layers.max_pooling2d(conv2, 2, 1, name='max_pool') drop_1 = layers.dropout(max_pool, 0.25, name='drop1') flatten = layers.flatten(drop_1, name='flatten') dense = layers.dense(flatten, 128, activation='relu', name='dense') drop2 = layers.dropout(dense, 0.5, name='drop2') logits = layers.dense(drop2, action_size, activation='softmax', name='logits') self.output = tf.nn.softmax(logits, name='output') # tf.one_hot(tf.arg_max(self.output, 1), depth=10) print(tf.arg_max(self.output, 1)) self.test = tf.one_hot(tf.arg_max(self.output, 1), depth=10) # input() self.cost = tf.losses.sparse_softmax_cross_entropy(self.labels, logits) self.acc, self.acc_op = tf.metrics.accuracy(self.labels, tf.arg_max(self.output, 1)) # self.grad = tf.gradients(self.cost, self.states, stop_gradients=[self.states]) self.grad = tf.gradients(self.cost, tf.trainable_variables()) self.optimizer = tf.train.AdamOptimizer() # print(tf.trainable_variables()) # print(tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope='layers')) # print(self.grad) self.apply_grad = self.optimizer.apply_gradients( zip( self.grad, tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope='layers')))
def discriminatorNet(inputs, is_training, use_batchNorm): idx = 0 f = inputs f = layers.conv2d(f, 64, kernel_size=4, strides=2, padding="SAME", name="conv_%d" % idx) if use_batchNorm: f = layers.batch_normalization(f, training=is_training, name="bn_%d" % idx) f = tf.nn.leaky_relu(f, alpha=0.01, name="lrelu_%d" % idx) idx += 1 f = layers.conv2d(f, 128, kernel_size=4, strides=2, padding="SAME", name="conv_%d" % idx) if use_batchNorm: f = layers.batch_normalization(f, training=is_training, name="bn_%d" % idx) f = tf.nn.leaky_relu(f, alpha=0.01, name="lrelu_%d" % idx) idx += 1 f = layers.flatten(f) f = layers.dense(f, 1024, name="dense_%d" % idx) f = tf.nn.leaky_relu(f, alpha=0.01, name="lrelu_%d" % idx) return f
def _film_layer(self, spatial_input, z_factor, scope='_film_layer'): with tf.variable_scope(scope): n_filters = 16 # self.n_channels # ( == 8 ) conv1 = layers.conv2d(spatial_input, filters=n_filters, kernel_size=3, strides=1, padding='same') conv1_act = tf.nn.leaky_relu(conv1) conv2 = layers.conv2d(conv1_act, filters=n_filters, kernel_size=3, strides=1, padding='same') conv2_act = tf.nn.leaky_relu(conv2) gamma_l2, beta_l2 = self._film_pred(z_factor, n_units=2 * n_filters) film = film_layer(conv2_act, gamma_l2, beta_l2) film_act = tf.nn.leaky_relu(film) film_sum = conv1_act + film_act return film_sum
def _model(self, inputs, mode, **config): x = inputs['image'] if config['data_format'] == 'channels_first': x = tf.transpose(x, [0, 3, 1, 2]) params = {'padding': 'SAME', 'data_format': config['data_format']} x = tfl.conv2d(x, 32, 5, activation=tf.nn.relu, name='conv1', **params) x = tfl.max_pooling2d(x, 2, 2, name='pool1', **params) x = tfl.conv2d(x, 64, 5, activation=tf.nn.relu, name='conv2', **params) x = tfl.max_pooling2d(x, 2, 2, name='pool2', **params) x = tfl.flatten(x) x = tfl.dense(x, 1024, activation=tf.nn.relu, name='fc1') x = tfl.dense(x, 10, name='fc2') if mode == Mode.TRAIN: return {'logits': x} else: return { 'logits': x, 'prob': tf.nn.softmax(x), 'pred': tf.argmax(x, axis=-1) }
def q_network(state, scope): with tf.variable_scope(scope): x = state c1 = conv2d( x, filters=8, kernel_size=(4, 4), ) for filters, kernel_size, strides, padding, activation in zip( CONV_FILTERS, CONV_KERNEL_SIZES, CONV_STRIDES, CONV_PADDINGS, CONV_ACTIVATIONS): x = conv2d(x, filters=filters, kernel_size=kernel_size, strides=strides, padding=padding, activation=activation) conv_flat = flatten(x) hidden = dense(conv_flat, HIDDEN_SIZE, activation=HIDDEN_ACTIVATION, kernel_initializer=K_INIT) outputs = dense(hidden, N_OUTPUTS, kernel_initializer=K_INIT) return outputs
def f_net(inputs): inputs = inputs[0] inputs = inputs / 128 - 1.0 # (640, 640, 3*n) -> () with tf.device('/gpu:0'): conv1 = layers.conv2d( inputs=inputs, filters=16, kernel_size=(8, 8), strides=1, kernel_regularizer=l2_regularizer(scale=1e-2), activation=tf.nn.relu, name='conv1') print conv1.shape pool1 = layers.max_pooling2d( inputs=conv1, pool_size=3, strides=4, name='pool1') print pool1.shape conv2 = layers.conv2d( inputs=pool1, filters=16, kernel_size=(5, 5), strides=1, kernel_regularizer=l2_regularizer(scale=1e-2), activation=tf.nn.relu, name='conv2') print conv2.shape pool2 = layers.max_pooling2d( inputs=conv2, pool_size=3, strides=3, name='pool2') print pool2.shape conv3 = layers.conv2d( inputs=pool2, filters=64, kernel_size=(3, 3), strides=1, kernel_regularizer=l2_regularizer(scale=1e-2), activation=tf.nn.relu, name='conv3') print conv3.shape pool3 = layers.max_pooling2d( inputs=conv3, pool_size=3, strides=2, name='pool3', ) print pool3.shape depth = pool3.get_shape()[1:].num_elements() inputs = tf.reshape(pool3, shape=[-1, depth]) print inputs.shape hid1 = layers.dense( inputs=inputs, units=256, activation=tf.nn.relu, kernel_regularizer=l2_regularizer(scale=1e-2), name='hid1') print hid1.shape hid2 = layers.dense( inputs=hid1, units=256, activation=tf.nn.relu, kernel_regularizer=l2_regularizer(scale=1e-2), name='hid2_adv') print hid2.shape adv = layers.dense( inputs=hid2, units=len(AGENT_ACTIONS), activation=None, kernel_initializer=tf.random_uniform_initializer(-3e-3, 3e-3), kernel_regularizer=l2_regularizer(scale=1e-2), name='adv') print adv.shape hid2 = layers.dense( inputs=hid1, units=256, activation=tf.nn.relu, kernel_regularizer=l2_regularizer(scale=1e-2), name='hid2_v') print hid2.shape v = layers.dense( inputs=hid2, units=1, activation=None, kernel_initializer=tf.random_uniform_initializer(-3e-3, 3e-3), kernel_regularizer=l2_regularizer(scale=1e-2), name='v') print v.shape q = tf.add(adv, v, name='q') print q.shape return {"q": q}
def res_block(self, x, num_filter, is_train, name): with tf.variable_scope(name, reuse=self.reuse): y = tf.pad(x, [[0, 0], [1, 1], [1, 1], [0, 0]], "REFLECT") y = layer.conv2d(y, num_filter, 3, 1, name='_res1') y = layer.batch_normalization(y, center=True, scale=True, training=is_train, name='_b1') y = tf.pad(y, [[0, 0], [1, 1], [1, 1], [0, 0]], "REFLECT") y = layer.conv2d(y, num_filter, 3, 1, name='_res2') y = layer.batch_normalization(y, center=True, scale=True, training=is_train, name='_b2') return x + y