def __init__(self, in_nc=3, nf=50, num_modules=4, out_nc=3, upscale=4): """ Constructor, see class documentation for more details. Parameters ---------- in_nc : int The number of input channels to the network, by default 3 nf : int The number of input feature channels per block of the network num_modules : int The number of blocks in the network, by default 4 out_nc : int The number of channels which should be output by the network, by default 3 upscale : int The amount of upscaling to be done, by default 4 """ super(RFDN, self).__init__() self.fea_conv = B.conv_layer(in_nc, nf, kernel_size=3) self.B1 = B.RFDB(in_channels=nf) self.B2 = B.RFDB(in_channels=nf) self.B3 = B.RFDB(in_channels=nf) self.B4 = B.RFDB(in_channels=nf) self.c = B.conv_block(nf * num_modules, nf, kernel_size=1, act_type='lrelu') self.LR_conv = B.conv_layer(nf, nf, kernel_size=3) upsample_block = B.pixelshuffle_block self.upsampler = upsample_block(nf, out_nc, upscale_factor=upscale) self.scale_idx = 0
def __init__(self, in_nc=3, nf=50, num_modules=4, out_nc=3, upscale=4): super(RFDN, self).__init__() self.fea_conv = B.conv_layer(in_nc, nf, kernel_size=3) self.B1 = B.RFDB(in_channels=nf) self.B2 = B.RFDB(in_channels=nf) self.B3 = B.RFDB(in_channels=nf) self.B4 = B.RFDB(in_channels=nf) self.c = B.conv_block(nf * num_modules, nf, kernel_size=1, act_type='lrelu') self.LR_conv = B.conv_layer(nf, nf, kernel_size=3) upsample_block = B.pixelshuffle_block self.upsampler = upsample_block(nf, out_nc, upscale_factor=4) self.scale_idx = 0
def CQA_attention(c, q, a, N, output_channel, c_maxlen, q_maxlen, a_maxlen, c_mask, q_mask, a_mask, dropout=0.0): ''' : param c: context, shape = (batch_size, context_max_sentence_length, vector_length) e.g.(32, 80, 50) : param q: question, shape = (batch_size, question_max_sentence_length, vector_length) e.g.(32, 40, 50) : param a: answer, shape = (batch_size, answer_max_sentence_length, vector_length) e.g.(32, 20, 50) : param N: int, batch_size : param c_maxlen: int, max_sentence_length of context : param q_maxlen: int, max_sentence_length of question : param a_maxlen: int, max_sentence_length of answer : param c_mask: context mask, shape = (batch_size, context_max_sentence_length,) e.g.(32,80) : param q_mask: question mask, shape = (batch_size, question_max_sentence_length,) e.g.(32,40) : param a_mask: answer mask, shape = (batch_size, answer_max_sentence_length,) e.g.(32,20) : return: attention tensor, shape = (batch_size, context_max_sentence_length, 4*vector_length) e.g.(32, 80, 200) ''' # cq_atten_outputs = CQ_attention_layer(c, q, N, c_maxlen, q_maxlen, scope = 'CQ_attention', dropout=0.0) # # e.g. cq_atten_outputs.shape = (32, 80, 200) # # use conv_layer to transform above shape(32, 80, 200) to shape(32, 80, 50) as following input # cq_outputs = conv_layer(cq_atten_outputs, 1, c.shape[2], 1, 'cq_transform') # cqa_atten_outputs = CQ_attention_layer(cq_outputs, a, N, c_maxlen, a_maxlen, scope='CQA_attention', dropout=0.0) # outputs = conv_layer(cqa_atten_outputs, 1, output_channel, 1, 'cqa_output') # return outputs c2q, c_c2q, c_q2c = CQ_attention_layer_with_mask(c, q, N, c_maxlen, q_maxlen, c_mask, q_mask, scope='CQ_attention', dropout=dropout) # e.g. cq_atten_outputs.shape = (32, 80, 200) # use conv_layer to transform above shape(32, 80, 200) to shape(32, 80, 50) as following input c2a, c_c2a, c_a2c = CQ_attention_layer_with_mask(c, a, N, c_maxlen, a_maxlen, c_mask, a_mask, scope='CA_attention', dropout=dropout) attention_outputs = [c, c2q, c2a] attention_outputs = tf.concat(attention_outputs, axis=-1) outputs = conv_layer(attention_outputs, 1, output_channel, 1, 'cqa_output') return outputs
def CQA_attention_v2(c, q, a, N, output_channel, c_maxlen, q_maxlen, a_maxlen, c_mask, q_mask, a_mask, dropout=0.0): c2q, c_c2q, c_q2c = CQ_attention_layer_with_mask(c, q, N, c_maxlen, q_maxlen, c_mask, q_mask, scope='CQ_attention', dropout=dropout) c = conv_layer(tf.concat([c2q, c_c2q, c_q2c], axis=-1), 1, c.shape[2], 1, 'inter_layer') a2c, a_a2c, a_c2a = CQ_attention_layer_with_mask(a, c, N, a_maxlen, c_maxlen, a_mask, c_mask, scope='AC_attention', dropout=dropout) attention_output = [a, a2c, a_a2c, a_c2a] attention_output = tf.concat(attention_output, axis=-1) output = conv_layer(attention_output, 1, output_channel, 1, 'cqa_output') return output
question_c = tf.placeholder(tf.int32, [batch_size, q_len, ch_len]) answer_c = tf.placeholder(tf.int32, [batch_size, a_len, ch_len]) context_ch = tf.reshape( embedding_layer(context_c, 95, 50, 'character_embedding'), [batch_size * c_len, ch_len, 50]) question_ch = tf.reshape( embedding_layer(question_c, 95, 50, 'character_embedding'), [batch_size * q_len, ch_len, 50]) answer_ch = tf.reshape( embedding_layer(answer_c, 95, 50, 'character_embedding'), [batch_size * a_len, ch_len, 50]) context_ch = tf.reduce_max(block.conv_layer(context_ch, 5, 50, 1, 'ch_conv', relu=True), axis=1) question_ch = tf.reduce_max(block.conv_layer(question_ch, 5, 50, 1, 'ch_conv', relu=True), axis=1) answer_ch = tf.reduce_max(block.conv_layer(answer_ch, 5, 50, 1, 'ch_conv',