Beispiel #1
0
def encode():
    output, output_steps = encoder(encode_data.get(), encode_radio.get(), encode_secded.get(), int(encode_bits_option.get()))
    output_text.set(output)
    encode_explanation = ""
    for i in output_steps:
        encode_explanation = encode_explanation + i
    tex.delete('1.0', tk.END)
    tex.insert(tk.END, encode_explanation)
    tex.see(tk.END)
Beispiel #2
0
def step():
    count_step = count_step_var.get()
    output, output_steps = encoder(encode_data.get(), encode_radio.get(), encode_secded.get(),
                                   int(encode_bits_option.get()))
    encode_explanation = ""
    counter = 0
    for i in output_steps:
        if counter <= count_step:
            encode_explanation = encode_explanation + i
        counter += 1
    output_text.set(output)
    tex.delete('1.0', tk.END)
    tex.insert(tk.END, encode_explanation)
    tex.see(tk.END)
    count_step_var.set(count_step + 1)
Beispiel #3
0
    def __init__(self, flags):
        cl_mc = [int(i) for i in flags.cl_mc.split(',')]
        en_shared_c = [int(i) for i in flags.en_shared_c.split(',')]
        de_mc = [int(i) for i in flags.de_mc.split(',')]

        self.nIter_ = 0
        self.device_ = 'cuda' if torch.cuda.is_available() else 'cpu'
        self.clonalP_ = clonalP(n_site=flags.nsite,
                                n_clone=flags.nclone,
                                fc_midChannels=cl_mc)

        self.encoder_ = encoder(n_site=flags.nsite,
                                n_clone=flags.nclone,
                                shared_fc_chans=en_shared_c,
                                p_drop=flags.en_pd,
                                device=self.device_,
                                n_dim=flags.ndim)

        self.nn_decoder_ = nn_decoder(n_dim=flags.ndim,
                                      n_site=flags.nsite,
                                      fc_mid_channels=de_mc)

        self.tree_decoder_ = tree_decoder(n_clone=flags.nclone,
                                          n_site=flags.nsite,
                                          device=self.device_)

        self.clonalP_.to(self.device_)
        self.encoder_.to(self.device_)
        self.nn_decoder_.to(self.device_)
        self.tree_decoder_.to(self.device_)
        self.recon_loss_tree = 0.0
        self.tree_loss = 0.0

        if flags.init:
            init_weights(self.clonalP_, flags.init, 0.02)
            init_weights(self.encoder_, flags.init, 0.02)
            init_weights(self.nn_decoder_, flags.init, 0.02)
            init_weights(self.tree_decoder_, flags.init, 0.02)

        self.optim_vae_ = torch.optim.Adam(itertools.chain(
            self.encoder_.parameters(), self.nn_decoder_.parameters(),
            self.clonalP_.parameters()),
                                           lr=flags.lr_vae)
        self.optim_tree_ = torch.optim.Adam(self.tree_decoder_.parameters(),
                                            lr=flags.lr_tree)
        self.flags_ = flags
        self.n_clone_ = flags.nclone
def evaluate(test_data,encoder,decoder, max_length, tokenizer, image_features_extract_model):
    images = test_data[0]
    captions = test_data[1]
    smoothie = SmoothingFunction().method4
    score1 = []  # weights - 0.25 0.25 0.25 0.25
    score2 = []  # weights - 0    0.33 0.33 0.33
    score3 = []  # weights - 0  0.5 0.5 0
    for image, caption in zip(images, captions):
        real_caption = ' '.join([tokenizer.index_word[i] for i in caption if i not in [0]])
        hidden = decoder.reset_state(batch_size=1)
        temp_input = tf.expand_dims(load_image(image)[0], 0)
        #image_features_extract_model = img_extract_model()
        img_tensor_val = image_features_extract_model(temp_input)
        img_tensor_val = tf.reshape(img_tensor_val, (img_tensor_val.shape[0], -1, img_tensor_val.shape[3]))
        features = encoder(img_tensor_val)
        dec_input = tf.expand_dims([tokenizer.word_index['<start>']], 0)
        result = []
        for i in range(max_length):
            #predictions, hidden = decoder(dec_input, features, hidden)
            predictions, hidden, attention_weights = decoder(dec_input, features, hidden)
            predicted_id = tf.random.categorical(predictions, 1)[0][0].numpy()
            result.append(tokenizer.index_word[predicted_id])
            if tokenizer.index_word[predicted_id] == '<end>':
                result.insert(0,"<start>")
                result = " ".join(result)
                score1.append(bleu([real_caption], result, smoothing_function=smoothie,weights=(0.25, 0.25, 0.25, 0.25)))
                score2.append(bleu([real_caption], result, smoothing_function=smoothie, weights=(0, 0.33, 0.33, 0.33)))
                score3.append(bleu([real_caption], result, smoothing_function=smoothie, weights=(0, 0.5, 0.5, 0)))

                print("Score1 \n",bleu([real_caption], result, smoothing_function=smoothie,weights=(0.25, 0.25, 0.25, 0.25)))
                print("Score2 \n",bleu([real_caption], result, smoothing_function=smoothie, weights=(0, 0.33, 0.33, 0.33)))
                print("Score3 \n", bleu([real_caption], result, smoothing_function=smoothie, weights=(0, 0., 0.5, 0)))
                print("Image name",image)
                print("Real caption",real_caption)
                print("Result",result)
                print("Scores are ",score1, score2, score3)
                break
            dec_input = tf.expand_dims([predicted_id], 0)
    score1.append(bleu([real_caption], result, smoothing_function=smoothie, weights=(0.25, 0.25, 0.25, 0.25)))
    score2.append(bleu([real_caption], result, smoothing_function=smoothie, weights=(0, 0.33, 0.33, 0.33)))
    score3.append(bleu([real_caption], result, smoothing_function=smoothie, weights=(0, 0.5, 0.5, 0)))
    print("Real caption outside", real_caption)
    print("Result outside", result)
    print("Score outside", score1, score2, score3)
    return statistics.mean(score1), statistics.mean(score2), statistics.mean(score3)
def get_plot_attention(image,encoder,decoder, max_length, tokenizer):
    attention_plot = np.zeros((max_length, attention_features_shape))
    hidden = decoder.reset_state(batch_size=1)
    temp_input = tf.expand_dims(load_image(image)[0], 0)
    image_features_extract_model = img_extract_model()
    img_tensor_val = image_features_extract_model(temp_input)
    img_tensor_val = tf.reshape(img_tensor_val, (img_tensor_val.shape[0], -1, img_tensor_val.shape[3]))
    features = encoder(img_tensor_val)
    dec_input = tf.expand_dims([tokenizer.word_index['<start>']], 0)
    result = []
    for i in range(max_length):
        predictions, hidden, attention_weights = decoder(dec_input, features, hidden)

        attention_plot[i] = tf.reshape(attention_weights, (-1,)).numpy()
        predicted_id = tf.random.categorical(predictions, 1)[0][0].numpy()
        result.append(tokenizer.index_word[predicted_id])
        if tokenizer.index_word[predicted_id] == '<end>':
            return result, attention_plot
        dec_input = tf.expand_dims([predicted_id], 0)
    attention_plot = attention_plot[:len(result), :]
    return result, attention_plot
def decoder(data_string, data_type, secded, message_size):
    # Check for input errors and convert to binary
    valid_binary = ['0', '1']
    valid_decimal = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
    valid_hex = [
        '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd',
        'e', 'f'
    ]

    data_string = data_string.lower().strip()

    if data_type == 'binary':
        for i in range(len(data_string)):
            if not data_string[i] in valid_binary:
                return 'Error: invalid binary character'
    elif data_type == 'hex':
        for i in range(len(data_string)):
            if not data_string[i] in valid_hex:
                return 'Error: invalid hex character'
        data_string = hex_to_binary_string(data_string)
    else:
        for i in range(len(data_string)):
            if not data_string[i] in valid_decimal:
                return 'Error: invalid decimal character'
        data_string = decimal_to_binary_string(data_string)
    # Split secded for check later
    if secded:
        secded_bit = data_string[len(data_string) - 1]
        data_string = data_string[0:len(data_string) - 1]

    # Check if message size is valid
    num_parity_bits = len(data_string) - message_size
    if 2**num_parity_bits < message_size + num_parity_bits + 1 or 2**(
            num_parity_bits - 1) >= message_size + num_parity_bits + 1:
        return "Error: invalid message_size for input data of length " + str(
            len(data_string))

    # Determine positioning of parity bits
    parity_positions = []
    for i in range(num_parity_bits):
        parity_positions.append(2**i - 1)

    # Construct supposed initial message
    message = ""
    for i in range(len(data_string)):
        if not i in parity_positions:
            message += data_string[i]

    # Calculate parity bits
    calculated_parity, messages = encoder(message, 'binary', secded,
                                          message_size)
    test = np.array(list(data_string))
    if secded:
        data_string = data_string + secded_bit
    if calculated_parity != data_string:
        bitNumber = ''
        for i in range(num_parity_bits):
            bitNumber = str(calculate_parity_value(test, i + 1)) + bitNumber

        if (parity_bit_fails(data_string)) and secded and num_errors(
                calculated_parity[:len(calculated_parity) - 1],
                data_string[:len(data_string) - 1]) >= 1:
            print("Single bit corruption")

        if num_errors(calculated_parity[:len(calculated_parity) - 1],
                      data_string[:len(data_string) - 1]) >= 1 and secded and (
                          not parity_bit_fails(data_string)):
            return "Double error secded"

        if parity_bit_fails(data_string) and num_errors(
                calculated_parity[:len(calculated_parity) - 1],
                data_string[:len(data_string) - 1]) == 0:
            return "Parity bit failed, message is " + message

        if secded:
            data_string = data_string[0:len(data_string) - 1]
        correct_list = list(data_string)
        correct_list[int(bitNumber, 2) -
                     1] = '0' if correct_list[int(bitNumber, 2) -
                                              1] == '1' else '1'
        corrected_string = ''.join(correct_list)
        corrected_message = ''
        for i in range(len(corrected_string)):
            if not i in parity_positions:
                corrected_message += corrected_string[i]

        return "Error at " + str(int(bitNumber,
                                     2)) + ", message is " + corrected_message

    return "Message is " + message