def discriminator(self, x, name='discriminator_img', is_training=True): ## 64,64,3 with tf.variable_scope(name, reuse=tf.AUTO_REUSE): x = ly.conv2d(x, 64, strides=2, use_bias=True, name='d_conv_0') ## 32,32,64 x = ly.batch_normal(x, name='d_bn_0', is_training=is_training) x = ly.relu(x, 0.2) x = ly.conv2d(x, 128, strides=2, use_bias=True, name='d_conv_1') ## 16,16,128 x = ly.batch_normal(x, name='d_bn_1', is_training=is_training) x = ly.relu(x, 0.2) x = ly.conv2d(x, 256, strides=2, use_bias=True, name='d_conv_2') ## 8,8,256 x = ly.batch_normal(x, name='d_bn_2', is_training=is_training) x = ly.relu(x, 0.2) x = ly.conv2d(x, 512, strides=2, use_bias=True, name='d_conv_3') ## 4,4,512 x = ly.batch_normal(x, name='d_bn_3', is_training=is_training) x = ly.relu(x, 0.2) x = ly.fc(x, 1, name='fc_0') x = tf.nn.sigmoid(x) return x
def gateActivation(self, x, channel, kernel, stride, label, name): fx = layer.conv2d(x, channel, kernel, stride, activation=None, padding='SAME', name=name+'_gate-filter') fx = tf.reshape(fx, [-1, channel]) fh = tf.layers.dense(label, channel, activation=None, bias_initializer=tf.constant_initializer(0.1)) gx = layer.conv2d(x, channel, kernel, stride, activation=None, padding='SAME', name=name+'_gate-gate') gx = tf.reshape(gx, [-1, channel]) gh = tf.layers.dense(label, channel, activation=None, bias_initializer=tf.constant_initializer(0.1)) output = tf.multiply(tf.nn.tanh(fx+fh), tf.nn.sigmoid(gx+gh)) return output
def build_model(self): self.net = {} self.net['input'] = tf.transpose(self.x, perm=(0, 2, 3, 1)) if self.network_type == 'cnn': self.net['conv1'] = conv2d(self.net['input'], 16, kernel=(8, 8), stride=(4, 4), name='conv1') self.net['conv2'] = conv2d(self.net['conv1'], 32, kernel=(4, 4), stride=(2, 2), name='conv2') self.net['feature'] = linear(self.net['conv2'], 256, name='fc1') else: # MLP for testing self.net['fc1'] = linear(self.net['input'], 50, init_b=tf.constant_initializer(0.0), name='fc1') self.net['feature'] = linear(self.net['fc1'], 50, init_b=tf.constant_initializer(0.0), name='fc2') self.net['value'] = tf.reshape(linear( self.net['feature'], 1, activation=None, name='value', init_b=tf.constant_initializer(0.0)), shape=(-1, )) self.net['logits'] = linear(self.net['feature'], self.n_outputs, activation=None, name='logits', init_b=tf.constant_initializer(0.0)) self.net['policy'] = tf.nn.softmax(self.net['logits'], name='policy') self.net['log_policy'] = tf.nn.log_softmax(self.net['logits'], name='log_policy') self.vars = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, tf.get_variable_scope().name)
def gatingsignal2d(x, kernal, phase, height=None, width=None, scope=None): """this is simply 1x1x1 convolution, bn, activation,Gating Signal(Query) :param x: :param kernal:(1,1,1,inputfilters,outputfilters) :param phase: :param drop: :param image_z: :param height: :param width: :param scope: :return: """ with tf.name_scope(scope): W = weight_xavier_init(shape=kernal, n_inputs=kernal[0] * kernal[1] * kernal[2], n_outputs=kernal[-1], activefunction='relu', variable_name=scope + 'conv_W') B = bias_variable([kernal[-1]], variable_name=scope + 'conv_B') conv = conv2d(x, W) + B conv = normalizationlayer(conv, is_train=phase, height=height, width=width, norm_type='group', scope=scope) conv = tf.nn.relu(conv) return conv
def SharePart(input, drop_out_rate): def pre_process(input): rgb_scaled = input Mean = [103.939,116.779,123.68] red,green,blue = tf.split(rgb_scaled,3,3) bgr = tf.concat([ red - Mean[2], green - Mean[1], blue - Mean[0]],3) return bgr input = pre_process(input) with tf.variable_scope('Share_Part'): conv1 = l.conv2d('conv1',input,(11,11),96,strides = [1,4,4,1],decay = (0.0,0.0),pad='VALID',Init = MODEL_INIT['conv1']) maxpool1 = l.max_pooling('maxpool',conv1,3,2) norm1 = tf.nn.lrn(maxpool1,depth_radius=2,alpha=2e-05,beta=0.75,name='conv1') conv2 = l.conv2d_with_group('conv2',norm1,(5,5),256,2,decay = (0.0,0.0),pad = 'SAME', Init = MODEL_INIT['conv2']) maxpool2 = l.max_pooling('maxpool2',conv2,3,2) norm2 = tf.nn.lrn(maxpool2,depth_radius=2,alpha=2e-05,beta=0.75,name='conv2') conv3 = l.conv2d('conv3',norm2,(3,3),384,pad = 'SAME',Init = MODEL_INIT['conv3']) conv4 = l.conv2d_with_group('conv4',conv3,(3,3),384,2,pad = 'SAME',Init = MODEL_INIT['conv4']) conv5 = l.conv2d_with_group('conv5',conv4,(3,3),256,2,pad = 'SAME',Init = MODEL_INIT['conv5']) maxpool5 = l.max_pooling('maxpool5',conv5,3,2) print maxpool5.shape dim=1 shape = maxpool5.get_shape().as_list() for d in shape[1:]: dim*=d reshape = tf.reshape(maxpool5,[-1,dim]) fc6 = l.fully_connect('fc6',reshape,4096,Init = MODEL_INIT['fc6']) fc6 = l.dropout('drop_6',fc6,drop_out_rate) fc7 = l.fully_connect('fc7',fc6,4096,Init = MODEL_INIT['fc7']) fc7 = l.dropout('drop_7',fc7,drop_out_rate) return fc7
def forward(self, input_images): input_images = tf.reshape(input_images, [-1, 28, 28, 1]) with tf.variable_scope('conv2d_1'): W1 = tf.Variable(tf.random_normal([3, 3, 1, 32]), name='w1') # 卷积核3x3,输入通道1,输出通道32(卷积核个数) L1 = layer.conv2d(input_images, W1, strides=1) L1 = layer.max_pool(L1, ksize=2, strides=2) with tf.variable_scope('conv2d_2'): # 第2层卷积,输入图片数据(?, 14, 14, 32) W2 = tf.Variable(tf.random_normal([3, 3, 32, 64], stddev=0.01), name='w2') # 卷积核3x3,输入通道32,输出通道64 L2 = layer.conv2d(L1, W2, strides=1) L2 = layer.max_pool(L2, ksize=2, strides=2) with tf.variable_scope('fc'): logits = layer.fully_connected(L2, 10) return logits
def classifier(input, label): # x = tf.layers.dense(input, 128, activation=tf.nn.sigmoid, bias_initializer=tf.constant_initializer(0.1)) x = tf.reshape(input, [-1, 1, segmentLength, 1]) x = layer.conv2d(x, 32, [1, 512], [1, 250], layer.prelu, name='classifier-L{}'.format(0)) x = layer.conv2d(x, 64, [1, 7], [1, 2], layer.prelu, name='classifier-L{}'.format(1)) # x = tf.layers.dense(input, 128, bias_initializer=tf.constant_initializer(0.01)) x = tf.layers.flatten(x) x = tf.layers.dense(x, 100, bias_initializer=tf.constant_initializer(0.01)) x = tf.layers.dense(x, 10, bias_initializer=tf.constant_initializer(0.01)) loss = tf.nn.softmax_cross_entropy_with_logits(logits=x, labels=label) correct_prediction = tf.equal(tf.argmax(x, 1), tf.argmax(label, 1)) accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) return loss, accuracy
def regnition(self, input): x = tf.reshape(input, [-1, 1, tstep*N, 1]) c = self.arch['channel'] k = self.arch['kernel'] s = self.arch['stride'] for i in range(len(c)): x = layer.conv2d(x, c[i], k[i], s[i], tf.nn.relu, 'SAME', name='cnn-L{}'.format(i)) flat = tf.reshape(x, [-1, 100*c[1]]) y = tf.layers.dense(flat, 10, bias_initializer=tf.constant_initializer(0.1)) return y
def encoder(self, x): x = tf.reshape(x, [-1, 500, 1, 1]) unit = self.arch['encoder'] c = unit['channel'] k = unit['kernel'] s = unit['stride'] for i in range(len(c)): x = layer.conv2d(x, c[i], k[i], s[i], tf.nn.relu, padding='SAME', name='encoder-L{}'.format(i)) x = tf.layers.flatten(x) z_mu = tf.layers.dense(x, self.arch['z_dim']) z_var = tf.layers.dense(x, self.arch['z_dim']) return z_mu, z_var
def discriminator(self, input): x = tf.reshape(input, [-1, 1, tstep*L, 1]) net = self.arch['discriminator'] c = net['channel'] k = net['kernel'] s = net['stride'] for i in range(len(c)): x = layer.conv2d(x, c[i], k[i], s[i], tf.nn.relu, 'SAME', name='dis-L{}'.format(i)) flat = tf.contrib.layers.flatten(x) y = tf.layers.dense(flat, 32, activation=tf.nn.relu, bias_initializer=tf.constant_initializer(0.1)) y = tf.layers.dense(y, 1, bias_initializer=tf.constant_initializer(0.1)) return y
def conv_relu(x, kernalshape, scope=None): with tf.name_scope(scope): W = weight_xavier_init(shape=kernalshape, n_inputs=kernalshape[0] * kernalshape[1] * kernalshape[2], n_outputs=kernalshape[-1], activefunction='relu', variable_name=str(scope) + 'W') B = bias_variable([kernalshape[-1]], variable_name=str(scope) + 'B') conv = conv2d(x, W) + B conv = tf.nn.relu(conv) return conv
def __add_layer(self, v: int, out_channels: int, module): previous_nodes = [f"{u}" for (_, u) in self.g_inv.edges([v])] if v in self.starts: module.add_input_node(f"{v}") elif v in self.ends: module.add_node(f"{v}", previous=previous_nodes, module=FlattenLinear(self.network_output_sizes[v])) elif self.__is_concat_node(v): if self.__is_concat_flatten_node(v): module.add_node(f"{v}", previous=previous_nodes, module=ConcatFlatten(out_channels)) elif self.__is_concat_conv_node(v): k, s = find_conv_layer(self.node_input_sizes[v], self.node_output_sizes[v], self.kernel_sizes, self.strides) module.add_node(f"{v}", previous=previous_nodes, module=ConcatConv(out_channels=out_channels, kernel_size=k, stride=s)) else: module.add_node(f"{v}", previous=previous_nodes, module=Concatenate()) elif self.__is_flatten_node(v): module.add_node(f"{v}", previous=previous_nodes, module=FlattenLinear(out_channels)) elif self.node_output_sizes[v] != self.node_input_sizes[v]: k, s = find_conv_layer(self.node_input_sizes[v], self.node_output_sizes[v], self.kernel_sizes, self.strides) module.add_node(f"{v}", previous=previous_nodes, module=conv2d(out_channels=out_channels, kernel_size=k, stride=s)) elif self.node_input_dimensions[v] == 1: module.add_node( f"{v}", previous=previous_nodes, module=self.__get_identity_or_linear_at_random(out_channels)) elif self.node_input_dimensions[v] == 4: module.add_node(f"{v}", previous=previous_nodes, module=self.__get_identity_or_relu_at_random())
def conv_sigmod(x, kernalshape, scope=None): with tf.name_scope(scope): W = weight_xavier_init(shape=kernalshape, n_inputs=kernalshape[0] * kernalshape[1] * kernalshape[2], n_outputs=kernalshape[-1], activefuncation='sigomd', variable_name=scope + 'W') B = bias_variable([kernalshape[-1]], variable_name=scope + 'B') conv = conv2d(x, W) + B conv = tf.nn.sigmoid(conv) return conv
def regnition(self, input): net = self.arch['regnizer'] c = net['channel'] k = net['kernel'] s = net['stride'] latentSize = self.arch['z_dim'] x = tf.reshape(input, [-1, latentSize, 1, 1]) for i in range(len(c)): x = layer.conv2d(x, c[i], k[i], s[i], tf.nn.relu, name='cnn-L{}'.format(i)) flat = tf.layers.flatten(x) y = tf.layers.dense(flat, 10, bias_initializer=tf.constant_initializer(0.1)) return y
def build_model(self): self.net = {} self.net['input'] = tf.transpose(self.x, perm=(0, 2, 3, 1)) if self.network_type == 'cnn': self.net['conv1'] = conv2d(self.net['input'], 16, kernel=(8, 8), stride=(4, 4), name='conv1') self.net['conv2'] = conv2d(self.net['conv1'], 32, kernel=(4, 4), stride=(2, 2), name='conv2') self.net['feature'] = linear(self.net['conv2'], 256, name='fc1') else: # MLP for testing self.net['fc1'] = linear(self.net['input'], 50, init_b = tf.constant_initializer(0.0), name='fc1') self.net['feature'] = linear(self.net['fc1'], 50, init_b = tf.constant_initializer(0.0), name='fc2') num_units = self.net['feature'].get_shape().as_list()[-1] self.lstm = tf.contrib.rnn.BasicLSTMCell(num_units=num_units, forget_bias=0.0, state_is_tuple=True) self.init_state = self.lstm.zero_state(batch_size=1, dtype=tf.float32) step_size = tf.shape(self.x)[:1] feature = tf.expand_dims(self.net['feature'], axis=0) lstm_outputs, lstm_state = tf.nn.dynamic_rnn(self.lstm, feature, initial_state=self.init_state, sequence_length=step_size, time_major=False) outputs = tf.reshape(lstm_outputs, shape=(-1, num_units)) self.final_state = lstm_state self.net['value'] = tf.reshape(linear(outputs, 1, activation=None, name='value', init_b = tf.constant_initializer(0.0)), shape=(-1,)) self.net['logits'] = linear(outputs, self.n_outputs, activation=None, name='logits', init_b = tf.constant_initializer(0.0)) self.net['policy'] = tf.nn.softmax(self.net['logits'], name='policy') self.net['log_policy'] = tf.nn.log_softmax(self.net['logits'], name='log_policy') self.vars = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, tf.get_variable_scope().name)
def __init__(self, input_size=(3, 32, 32), num_classes=10): super(AlanNet, self).__init__() self.seen = 0 self.features = nn.Sequential( layer.conv2d(input_size[0], 64, kernel_size=3, stride=1, padding=1, lr=None, activation=relu), #input nn.MaxPool2d(kernel_size=2, stride=2, padding=0), #input/2 layer.conv2d(64, 128, kernel_size=3, stride=1, padding=1, lr=None, activation=relu), nn.MaxPool2d(kernel_size=2, stride=2, padding=0), #input/4 layer.conv2d(128, 256, kernel_size=3, stride=1, padding=1, lr=None, activation=relu), layer.conv2d(256, 256, kernel_size=3, stride=1, padding=1, lr=None, activation=relu), layer.conv2d(256, 256, kernel_size=3, stride=1, padding=1, lr=None, activation=relu), layer.conv2d(256, 512, kernel_size=2, stride=2, padding=0, lr=None, activation=None), #input/8 ) self.classifier = nn.Sequential( nn.Dropout(), layer.linear(512 * input_size[1] * input_size[2] // 64, 512, None, relu), nn.Dropout(), layer.linear(512, 256, None, activation=relu), nn.Dropout(), layer.linear(256, num_classes, None)) if use_cuda: self.cuda()
def __call__(self, input): with tf.variable_scope(self.name, reuse=self.reuse): input = ly.conv2d(input, 64, strides=2, name='conv_0') ## (-1,150,150,64) input = ly.batch_normal(input, name='bn_0') input = tf.nn.leaky_relu(input) input = ly.conv2d(input, 128, strides=2, name='conv_1') ## (-1,75,75,128) input = ly.batch_normal(input, name='bn_1') input = tf.nn.leaky_relu(input) input = ly.conv2d(input, 256, strides=2, name='conv_2') ## (-1,38,38,256) input = ly.batch_normal(input, name='bn_2') input = tf.nn.leaky_relu(input) input = ly.conv2d(input, 512, strides=2, name='conv_3') ## (-1,19,19,512) input = ly.batch_normal(input, name='bn_3') input = tf.nn.leaky_relu(input) print(input.shape) input = ly.conv2d(input, 512, strides=2, name='conv_4') ## (-1,10,10,512) input = ly.batch_normal(input, name='bn_4') input = tf.nn.leaky_relu(input) ## avg input = tf.reduce_mean(input, axis=[1, 2]) input = tf.nn.dropout(input, keep_prob=0.5) input = ly.fc(input, 1, name='fc_0') # input = ly.batch_normal(input, name='bn_5') input = tf.nn.sigmoid(input) return input
def encoder(x): x = tf.reshape(x, [-1, tstep, N, 1]) unit = arch['encoder'] c = unit['channel'] k = unit['kernel'] s = unit['stride'] with tf.variable_scope('encoder', reuse=tf.AUTO_REUSE): for l in range(len(c)): x = layer.conv2d(x, c[l], k[l], s[l], layer.prelu, name='encoder-L{}'.format(l)) x = tf.reshape(x, [-1, 25 * 64]) z_mu = tf.layers.dense(x, arch['z_dim']) z_var = tf.layers.dense(x, arch['z_dim']) return z_mu, z_var
def encoder(self, input): net = self.arch['encoder'] c = net['channel'] k = net['kernel'] s = net['stride'] unit_z = self.arch['z_dim'] featureSize = self.arch['featureSize'] x = tf.reshape(input, [-1, featureSize, 1, 1]) for l in range(len(c)): x = layer.conv2d(x, c[l], k[l], s[l], activation=layer.prelu, name='encoder-L{}'.format(l)) x = tf.layers.flatten(x) z_mu = tf.layers.dense(x, unit_z) z_var = tf.layers.dense(x, unit_z) return z_mu, z_var
def conv_bn_relu_drop(x, kernalshape, phase, drop_conv, height=None, width=None, scope=None): with tf.name_scope(scope): W = weight_xavier_init(shape=kernalshape, n_inputs=kernalshape[0] * kernalshape[1] * kernalshape[2], n_outputs=kernalshape[-1], activefunction='relu', variable_name=str(scope) + 'W') B = bias_variable([kernalshape[-1]], variable_name=str(scope) + 'B') conv = conv2d(x, W) + B conv = normalizationlayer(conv, phase, height=height, width=width, norm_type='group', scope=scope) conv = tf.nn.dropout(tf.nn.relu(conv), drop_conv) return conv
def run(model, train_set, vali_set, test_set): for epoch in range(1, 300): train_loss = train(model, train_set) vali_loss = validation(model, vali_set) accuracy = test(model, test_set) print("epoch:", epoch, "\ttrain_loss:", train_loss, "\tvali_loss:", vali_loss, "\taccuracy:", accuracy) lr = 0.01 model = net.model(optimizer.Adam(lr=lr)) # 30 66 #model = net.model(optimizer.GradientDescent(lr=lr)) #30번에 32퍼 학,검,테 데이터셋 128개일때 model.add(nn.conv2d(filters=32, kernel_size=[3,3], strides=[1,1], w_init=init.he)) model.add(nn.relu()) model.add(nn.maxpool2d(kernel_size=[2,2], strides=[2,2])) model.add(nn.dropout(0.6)) model.add(nn.conv2d(filters=64, kernel_size=[3,3], strides=[1,1], w_init=init.he)) model.add(nn.relu()) model.add(nn.maxpool2d(kernel_size=[2,2], strides=[2,2])) model.add(nn.dropout(0.6)) model.add(nn.conv2d(filters=128, kernel_size=[3,3], strides=[1,1], w_init=init.he)) model.add(nn.relu()) model.add(nn.maxpool2d(kernel_size=[2,2], strides=[2,2])) model.add(nn.dropout(0.6)) model.add(nn.flatten())
# only ujse first 1000 samples for training xarr = xarr[:1000] yarr = yarr[:1000] # graphing variables xbar = [] ybar = [] correct = 0 loss = 0 # training loop for epoch in range(10): for image in range(1000): # convolution operation with max pooling input = layer.convert_to_2d_image(xarr[image]) conv0 = layer.conv2d(input, filter1) relu0 = layer.RELU(conv0) max0 = layer.maxpool(relu0) conv1 = layer.conv2d(max0, filter2) relu1 = layer.RELU(conv1) max1 = layer.maxpool(relu1) # fully connected l0 = layer.flatten(max1) l0 = layer.dropout(l0, .5) z = layer.forward_connected(l0, syn0, bias0) l1 = layer.RELU(z) l1 = layer.dropout(l1, .5) l2 = layer.forward_connected(l1, syn1, bias1) l2 = layer.softmax(l2) # define target matrix target = np.zeros([10, 1])
def build_model(self, images, keep_prob): # Convolution layer x_image = tf.reshape(images, [-1, self.n_in[0], self.n_in[1], 3]) with tf.variable_scope("Discriminator") as scope: with tf.variable_scope("conv_layer1") as scope: output = layer.conv2d(x=x_image, stride=2, filter_size=[ self.filter_size[0], self.filter_size[1], 3, self.layers[0] ], i=1, BatchNorm=True) output = activation.leakyReLU(output) tf.summary.histogram("conv_layer1", output) with tf.variable_scope("conv_layer2") as scope: # ResidualBlock output = layer.ResidualBlock(x=output, stride=1, filter_size=[ self.filter_size[0], self.filter_size[1], self.layers[0], self.layers[1] ], i=str(2) + '_' + str(1), BatchNorm=True) output = layer.ResidualBlock(x=output, stride=1, filter_size=[ self.filter_size[0], self.filter_size[1], self.layers[0], self.layers[1] ], i=str(2) + '_' + str(2), BatchNorm=True) output = layer.conv2d(x=output, stride=2, filter_size=[ self.filter_size[0], self.filter_size[1], self.layers[0], self.layers[1] ], i=2, BatchNorm=True) output = activation.leakyReLU(output) output = tf.nn.dropout(output, keep_prob) tf.summary.histogram("conv_layer2", output) with tf.variable_scope("conv_layer3") as scope: output = layer.conv2d(x=output, stride=2, filter_size=[ self.filter_size[0], self.filter_size[1], self.layers[1], self.layers[2] ], i=3, BatchNorm=True) output = activation.leakyReLU(output) tf.summary.histogram("conv_layer3", output) h_fc_1 = tf.nn.dropout(output, keep_prob) # Fc1 output = layer.fc(h_fc_1, self.labels, "", BatchNorm=False) return output
def attngatingblock(x, g, inputfilters, outfilters, scale_factor, phase, height=None, width=None, scope=None): """ take g which is the spatially smaller signal, do a conv to get the same number of feature channels as x (bigger spatially) do a conv on x to also get same feature channels (theta_x) then, upsample g to be same size as x add x and g (concat_xg) relu, 1x1x1 conv, then sigmoid then upsample the final - this gives us attn coefficients :param x: :param g: :param inputfilters: :param outfilters: :param scale_factor:2 :param scope: :return: """ with tf.name_scope(scope): kernalx = (1, 1, inputfilters, outfilters) Wx = weight_xavier_init(shape=kernalx, n_inputs=kernalx[0] * kernalx[1] * kernalx[2], n_outputs=kernalx[-1], activefunction='relu', variable_name=scope + 'conv_Wx') Bx = bias_variable([kernalx[-1]], variable_name=scope + 'conv_Bx') theta_x = conv2d(x, Wx, scale_factor) + Bx kernalg = (1, 1, inputfilters, outfilters) Wg = weight_xavier_init(shape=kernalg, n_inputs=kernalg[0] * kernalg[1] * kernalg[2], n_outputs=kernalg[-1], activefunction='relu', variable_name=scope + 'conv_Wg') Bg = bias_variable([kernalg[-1]], variable_name=scope + 'conv_Bg') phi_g = conv2d(g, Wg) + Bg add_xg = resnet_Add(theta_x, phi_g) act_xg = tf.nn.relu(add_xg) kernalpsi = (1, 1, outfilters, 1) Wpsi = weight_xavier_init(shape=kernalpsi, n_inputs=kernalpsi[0] * kernalpsi[1] * kernalpsi[2], n_outputs=kernalpsi[-1], activefunction='relu', variable_name=scope + 'conv_Wpsi') Bpsi = bias_variable([kernalpsi[-1]], variable_name=scope + 'conv_Bpsi') psi = conv2d(act_xg, Wpsi) + Bpsi sigmoid_psi = tf.nn.sigmoid(psi) upsample_psi = upsample2d(sigmoid_psi, scale_factor=scale_factor, scope=scope + "resampler") # Attention: upsample_psi * x # upsample_psi = layers.Lambda(lambda x, repnum: K.repeat_elements(x, repnum, axis=4), # arguments={'repnum': outfilters})(upsample_psi) gat_x = tf.multiply(upsample_psi, x) kernal_gat_x = (1, 1, outfilters, outfilters) Wgatx = weight_xavier_init(shape=kernal_gat_x, n_inputs=kernal_gat_x[0] * kernal_gat_x[1] * kernal_gat_x[2], n_outputs=kernal_gat_x[-1], activefunction='relu', variable_name=scope + 'conv_Wgatx') Bgatx = bias_variable([kernalpsi[-1]], variable_name=scope + 'conv_Bgatx') gat_x_out = conv2d(gat_x, Wgatx) + Bgatx gat_x_out = normalizationlayer(gat_x_out, is_train=phase, height=height, width=width, norm_type='group', scope=scope) return gat_x_out
Trainfilepath = '../../vcc2016/TFrecords/raw/Train/' Testfilepath = '../../vcc2016/TFrecords/raw/Test/' filename = 'SF1/100002' tstep = 100 length = 80 sampleRate = 16000 segmentLength = tstep * length source = tf.placeholder(tf.float32, [None, segmentLength]) label = tf.placeholder(tf.float32, [ None, ]) ReshapeToCNN = tf.reshape(source, [-1, 1, segmentLength, 1]) cnn_op1 = conv2d(ReshapeToCNN, 128, [1, 512], [1, 250], tf.nn.relu, 'same', 'cnn_L1') SquzToLSTM = tf.squeeze(cnn_op1, [1]) lstm_cell_1 = tf.contrib.rnn.BasicLSTMCell(250, forget_bias=1.0) with tf.variable_scope('lstm_cell_1'): lstm_out, _ = tf.nn.dynamic_rnn(lstm_cell_1, SquzToLSTM, dtype=tf.float32) recover = tf.reshape(lstm_out, [-1, segmentLength]) trainables = tf.trainable_variables() loss = tf.nn.l2_loss(recover - source) train_L2_loss = tf.train.AdamOptimizer(0.0001).minimize(loss) sess = tf.InteractiveSession() tf.global_variables_initializer().run()
def create_conv_net(x, keep_prob, channels, layers, features_root=16, filter_size=3, pool_size=2, training=True): """ 주어진 파라미터를 이용해서 convolution u-net 그래프 생성 함 :param x: input tensor, shape [?,nx,ny,channels] :param keep_prob: dropout probability tensor :param channels: number of channels in the input image :param layers: number of layers in the net :param features_root: number of features in the first layer :param filter_size: size of the convolution filter :param pool_size: size of the max pooling operation :param summaries: Flag if summaries should be created """ logging.info( "Layers {layers}, features {features}, filter size {filter_size}x{filter_size}, pool size: {pool_size}x{pool_size}" .format(layers=layers, features=features_root, filter_size=filter_size, pool_size=pool_size)) # Placeholder for the input image with tf.name_scope("preprocessing"): nx = tf.shape(x)[1] ny = tf.shape(x)[2] x_image = tf.reshape(x, tf.stack([-1, nx, ny, channels])) in_node = x_image batch_size = tf.shape(x_image)[0] weights = [] biases = [] convs = [] pools = OrderedDict() deconv = OrderedDict() dw_h_convs = OrderedDict() up_h_convs = OrderedDict() in_size = 1000 size = in_size # down layers for layer in range(0, layers): with tf.name_scope("down_conv_{}".format(str(layer))): features = 2**layer * features_root stddev = np.sqrt(2 / (filter_size**2 * features)) if layer == 0: w1 = weight_variable( [filter_size, filter_size, channels, features], stddev, name="w1") else: w1 = weight_variable( [filter_size, filter_size, features // 2, features], stddev, name="w1") w2 = weight_variable( [filter_size, filter_size, features, features], stddev, name="w2") b1 = bias_variable([features], name="b1") b2 = bias_variable([features], name="b2") conv1 = conv2d(in_node, w1, b1, keep_prob) tmp_h_conv = tf.nn.relu( tf.layers.batch_normalization(conv1, training=training)) conv2 = conv2d(tmp_h_conv, w2, b2, keep_prob) dw_h_convs[layer] = tf.nn.relu( tf.layers.batch_normalization(conv2, training=training)) weights.append((w1, w2)) biases.append((b1, b2)) convs.append((conv1, conv2)) size -= 4 if layer < layers - 1: pools[layer] = max_pool(dw_h_convs[layer], pool_size) in_node = pools[layer] size /= 2 in_node = dw_h_convs[layers - 1] # up layers for layer in range(layers - 2, -1, -1): with tf.name_scope("up_conv_{}".format(str(layer))): features = 2**(layer + 1) * features_root stddev = np.sqrt(2 / (filter_size**2 * features)) wd = weight_variable_devonc( [pool_size, pool_size, features // 2, features], stddev, name="wd") bd = bias_variable([features // 2], name="bd") h_deconv = tf.nn.relu(deconv2d(in_node, wd, pool_size) + bd) h_deconv_concat = crop_and_concat(dw_h_convs[layer], h_deconv) deconv[layer] = h_deconv_concat w1 = weight_variable( [filter_size, filter_size, features, features // 2], stddev, name="w1") w2 = weight_variable( [filter_size, filter_size, features // 2, features // 2], stddev, name="w2") b1 = bias_variable([features // 2], name="b1") b2 = bias_variable([features // 2], name="b2") conv1 = conv2d(h_deconv_concat, w1, b1, keep_prob) h_conv = tf.nn.relu( tf.layers.batch_normalization(conv1, training=training)) conv2 = conv2d(h_conv, w2, b2, keep_prob) in_node = tf.nn.relu( tf.layers.batch_normalization(conv2, training=training)) up_h_convs[layer] = in_node weights.append((w1, w2)) biases.append((b1, b2)) convs.append((conv1, conv2)) size *= 2 size -= 4 # Output Map with tf.name_scope("output_map"): weight = weight_variable( [1, 1, features_root, 1], stddev) # 불량 CLASS의 MAP만 Loss함수에 들어가므로 channel은 "1"이 됨. bias = bias_variable([1], name="bias") conv = conv2d(in_node, weight, bias, tf.constant(1.0)) output_map = tf.squeeze(tf.nn.sigmoid(conv), axis=-1) up_h_convs["out"] = output_map variables = [] for w1, w2 in weights: variables.append(w1) variables.append(w2) for b1, b2 in biases: variables.append(b1) variables.append(b2) return output_map, variables, int(in_size - size)
arch = { 'channel': [16, 32], 'kernel': [[1, 512], [1, 3]], 'stride': [[1, 40], [1, 2]] } source = tf.placeholder(tf.float32, shape=[None, segmentLength]) label = tf.placeholder(tf.float32, shape=[None, speakerN]) x = tf.reshape(source, [-1, 1, segmentLength, 1]) tS = time.time() for i in range(len(arch['channel'])): x = layer.conv2d(x, arch['channel'][i], arch['kernel'][i], arch['stride'][i], tf.nn.relu, name='cnn-L{}'.format(i)) flat = tf.reshape(x, [-1, 100 * arch['channel'][1]]) y = tf.layers.dense(flat, 10, bias_initializer=tf.constant_initializer(0.1)) loss = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits(labels=label, logits=y)) train_step = tf.train.AdamOptimizer(0.0001).minimize(loss) correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(label, 1)) accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) batchSize = 128 sess = tf.InteractiveSession()
def __call__(self, input): with tf.variable_scope(self.name, reuse=self.reuse): input = ly.conv2d(input, 64, kernel_size=7, strides=1, name='g_conv2d_0') input = ly.batch_normal(input, name='g_bn_0') input = tf.nn.relu(input) input = ly.conv2d(input, 128, kernel_size=3, strides=2, name='g_conv2d_1') input = ly.batch_normal(input, name='g_bn_1') input = tf.nn.relu(input) input = ly.conv2d(input, 256, kernel_size=3, strides=2, name='g_conv2d_2') input = ly.batch_normal(input, name='g_bn_2') input = tf.nn.relu(input) ### resnet for i in range(8): cell = ly.conv2d(input, 256, kernel_size=3, strides=1, name='g_conv2d_res_%s' % i) cell = ly.batch_normal(cell, name='g_res_%s' % i) cell = tf.nn.relu(cell) input = cell input = ly.deconv2d(input, kernel_size=3, strides=2, name='g_deconv2d_0') input = ly.batch_normal(input, name='g_bn_3') input = tf.nn.relu(input) input = ly.deconv2d(input, kernel_size=3, strides=2, name='g_deconv2d_1') input = ly.batch_normal(input, name='g_bn_4') input = tf.nn.relu(input) input = ly.conv2d(input, 3, kernel_size=7, strides=1, name='g_conv2d_3') input = ly.batch_normal(input, name='g_bn_5') input = tf.nn.tanh(input) input = tf.image.resize_images(input, (299, 299)) return input ## (-1,28,28,1)
def classify(self, d_opt=None, name='classify', is_training=True): ### 64,64,1 with tf.variable_scope(name, reuse=tf.AUTO_REUSE): x = tf.pad(self.input_img, [[0, 0], [5, 5], [5, 5], [0, 0]], "REFLECT") x = ly.conv2d(x, 64, kernal_size=11, name='conv_0', padding='VALID', use_bias=True) x = ly.batch_normal(x, name='bn_0', is_training=is_training) x = ly.relu(x) x = ly.maxpooling2d(x) ## 32,32,64 x = tf.pad(x, [[0, 0], [3, 3], [3, 3], [0, 0]], "REFLECT") x = ly.conv2d(x, 128, kernal_size=7, name='conv_1', padding='VALID', use_bias=True) x = ly.batch_normal(x, name='bn_1', is_training=is_training) x = ly.relu(x) x = ly.maxpooling2d(x) ## 16,16,128 x = tf.pad(x, [[0, 0], [2, 2], [2, 2], [0, 0]], "REFLECT") x = ly.conv2d(x, 256, kernal_size=5, name='conv_2', padding='VALID', use_bias=True) x = ly.batch_normal(x, name='bn_2', is_training=is_training) x = ly.relu(x) x = ly.maxpooling2d(x) ## 8,8,256 x = tf.pad(x, [[0, 0], [1, 1], [1, 1], [0, 0]], "REFLECT") x = ly.conv2d(x, 512, kernal_size=3, name='conv_3', padding='VALID', use_bias=True) x = ly.batch_normal(x, name='bn_3', is_training=is_training) x = ly.relu(x) x = tf.pad(x, [[0, 0], [1, 1], [1, 1], [0, 0]], "REFLECT") x = ly.conv2d(x, 512, kernal_size=3, name='conv_4', padding='VALID', use_bias=True) x = ly.batch_normal(x, name='bn_4', is_training=is_training) x = ly.relu(x) x = ly.maxpooling2d(x) ## 4,4,512 x = ly.fc(x, 1024, name='fc_0', use_bias=True) x = ly.batch_normal(x, name='bn_5', is_training=is_training) x = ly.relu(x) x = tf.nn.dropout(x, keep_prob=0.5) x = ly.fc(x, self.class_num, name='fc_1', use_bias=True) self.pred_x_index = tf.argmax(tf.nn.softmax(x), axis=-1) self.pred_x_value = tf.reduce_max(tf.nn.softmax(x), axis=-1) if (is_training): cross_loss = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits_v2( labels=self.input_label, logits=x), axis=0) l2_loss = 0.0005 * tf.reduce_sum([ tf.nn.l2_loss(var) for var in self.get_single_var('classify/fc') ]) loss = cross_loss + l2_loss self.summaries.append(tf.summary.scalar('loss', loss)) _grad = d_opt.compute_gradients( loss, var_list=self.get_vars('classify')) train_op = d_opt.apply_gradients(_grad) return train_op