Beispiel #1
0
def validate(val_loader, model, criterion, save_images, epoch):
    '''Validate model on data in val_loader'''
    print('Starting validation.')

    # Prepare value counters and timers
    batch_time = AverageMeter()
    data_time = AverageMeter()
    losses = AverageMeter()
    
    # Switch model to validation mode
    model.eval()
    
    # Run through validation set
    end = time.time()
    for i, (input_gray, input_ab, target) in enumerate(val_loader):
        
        # Use GPU if available
        target = target.cuda() if use_gpu else target
        input_gray_variable = Variable(input_gray, volatile=True).cuda() if use_gpu else Variable(input_gray, volatile=True)
        input_ab_variable = Variable(input_ab, volatile=True).cuda() if use_gpu else Variable(input_ab, volatile=True)
        target_variable = Variable(target, volatile=True).cuda() if use_gpu else Variable(target, volatile=True)
       
        # Record time to load data (above)
        data_time.update(time.time() - end)

        # Run forward pass
        output_ab = model(input_gray_variable) # throw away class predictions
        loss = criterion(output_ab, input_ab_variable) # check this!
        
        # Record loss and measure accuracy
        losses.update(loss.data[0], input_gray.size(0))

        # Save images to file
        if save_images:
            for j in range(len(output_ab)):
                save_path = {'grayscale': 'outputs/gray/', 'colorized': 'outputs/color/'}
                save_name = 'img-{}-epoch-{}.jpg'.format(i * val_loader.batch_size + j, epoch)
                visualize_image(input_gray[j], ab_input=output_ab[j].data, show_image=False, save_path=save_path, save_name=save_name)

        # Record time to do forward passes and save images
        batch_time.update(time.time() - end)
        end = time.time()
        
        # Print model accuracy -- in the code below, val refers to both value and validation
        if i % args.print_freq == 0:
            print('Validate: [{0}/{1}]\t'
                  'Time {batch_time.val:.3f} ({batch_time.avg:.3f})\t'
                  'Loss {loss.val:.4f} ({loss.avg:.4f})\t'.format(
                   i, len(val_loader), batch_time=batch_time, loss=losses))

    print('Finished validation.')
    return losses.avg
    def visualize_predictions(self, image=None, name=None, dataset=None, gt=None, pred=None):
        """
        Helper function to visualize predictions
        """
        dataset_viz = {}
        dataset_viz['img'] = image
        dataset_viz['name'] = name
        dataset_viz['split'] = np.ones(image.shape[0])
        dataset_viz['dataset'] = dataset
        dataset_viz['bbox_coords'] = np.zeros([image.shape[0], 4, 4])
        dataset_viz['num_persons'] = np.ones([image.shape[0], 1])
        dataset_viz['gt'] = gt
        dataset_viz['pred'] = pred

        dataset_viz = self.dataset_obj.recreate_images(gt=True, pred=True, external=True, ext_data=dataset_viz)
        visualize_image(dataset_viz, bbox=False, uv=True)

        return None
Beispiel #3
0
                labels.append(one_hot_label)
                test_data.append(img)
            except:
                break
    return np.asarray(test_data).reshape(-1, 256, 256, 1), np.asarray(labels)


if __name__ == '__main__':
    from utils import visualize_image

    test_data, test_label = load_test_data_1C()
    train_data, train_label = load_train_data_1C()
    # print(test_data, test_label)
    print(train_data, train_label)
    train_label_sum = [0, 0, 0, 0, 0]
    for i in train_label:
        for k, j in enumerate(i):
            if j == 1.0:
                train_label_sum[k] += 1
                break
    print(train_label)
    print(train_label_sum)
    for i in range(20):
        for idx, value in enumerate(train_label[i]):
            if value == 1.0:
                label = idx + 1
                break
        img = visualize_image(train_data[i].reshape(256, 256),
                              visual=False,
                              title='index:{} - label:{}'.format(i, label))
def test_feed_backward():
    # Setting up neural network
    network = nn.OneHiddenLayer(784, 128, 10)
    network.load("output/weights/network_one_128.pickle")

    labels = np.array([[1.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00],
                       [0.00, 1.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00],
                       [0.00, 0.00, 1.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00],
                       [0.00, 0.00, 0.00, 1.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00],
                       [0.00, 0.00, 0.00, 0.00, 1.00, 0.00, 0.00, 0.00, 0.00, 0.00],
                       [0.00, 0.00, 0.00, 0.00, 0.00, 1.00, 0.00, 0.00, 0.00, 0.00],
                       [0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 1.00, 0.00, 0.00, 0.00],
                       [0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 1.00, 0.00, 0.00],
                       [0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 1.00, 0.00],
                       [0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 1.00]])

    images = network.feed_backward(labels) * 255

    utl.visualize_image(images[0], "Zero")
    utl.visualize_image(images[1], "One")
    utl.visualize_image(images[2], "Two")
    utl.visualize_image(images[3], "Three")
    utl.visualize_image(images[4], "Four")
    utl.visualize_image(images[5], "Five")
    utl.visualize_image(images[6], "Six")
    utl.visualize_image(images[7], "Seven")
    utl.visualize_image(images[8], "Eight")
    utl.visualize_image(images[9], "Nine")
def test_custom_numbers():
    network = nn.OneHiddenLayer(784, 128, 10)
    network.load("output/weights/network_one_128.pickle")

    print("Testing with a local image")
    image = utl.load_image("Test_data/zero_1.png")
    utl.visualize_image(image, "TEST")
    probability = network.forward(image)
    print(np.argmax(probability))
    utl.plot_probability(probability[0])

    image = utl.load_image("Test_data/one_1.png")
    utl.visualize_image(image, "TEST")
    probability = network.forward(image)
    print(np.argmax(probability))
    # utl.plot_probability(probability[0])

    image = utl.load_image("Test_data/two_1.png")
    utl.visualize_image(image, "TEST")
    probability = network.forward(image)
    print(np.argmax(probability))
    # utl.plot_probability(probability[0])

    image = utl.load_image("Test_data/three_1.png")
    utl.visualize_image(image, "TEST")
    probability = network.forward(image)
    print(np.argmax(probability))
    # utl.plot_probability(probability[0])

    image = utl.load_image("Test_data/four_2.png")
    utl.visualize_image(image, "TEST")
    probability = network.forward(image)
    print(np.argmax(probability))
    # utl.plot_probability(probability[0])

    image = utl.load_image("Test_data/five_1.png")
    utl.visualize_image(image, "TEST")
    probability = network.forward(image)
    print(np.argmax(probability))
    # utl.plot_probability(probability[0])

    image = utl.load_image("Test_data/six_1.png")
    utl.visualize_image(image, "TEST")
    probability = network.forward(image)
    print(np.argmax(probability))
    # utl.plot_probability(probability[0])

    image = utl.load_image("Test_data/seven_1.png")
    utl.visualize_image(image, "TEST")
    probability = network.forward(image)
    print(np.argmax(probability))
    # utl.plot_probability(probability[0])

    image = utl.load_image("Test_data/eight_1.png")
    utl.visualize_image(image, "TEST")
    probability = network.forward(image)
    print(np.argmax(probability))
    # utl.plot_probability(probability[0])

    image = utl.load_image("Test_data/nine_1.png")
    utl.visualize_image(image, "TEST")
    probability = network.forward(image)
    print(np.argmax(probability))
    utl.plot_probability(probability[0])
    def test(self):
        # Loss Functions
        self.decrypt_err_eve = tf.reduce_mean(tf.square(self.secret - self.eve_output))
        self.decrypt_err_bob = tf.reduce_mean(tf.square(self.secret - self.bob_output))
        self.decrypt_err_alice = tf.reduce_mean(tf.square(self.msg - self.alice_output))


        
        # Train Eve for two minibatches to give it a slight computational edge
        no_of_examples = 5
        alice_test_error = []
        bob_test_error = []
        eve_test_error = []
        for i in range(no_of_examples):
            alice_decrypt_error, bob_decrypt_error, eve_decrypt_error = 0.0, 0.0, 0.0
            bs = self.batch_size
            msg_in_val, secret_val, key_val = gen_data(n=bs, msg_len=self.msg_len, secret_len=self.secret_len, key_len=self.random_seed_len)
            
            decrypt_err_alice, decrypt_err_bob, decrypt_err_eve,alice,bob,eve = self.sess.run([self.decrypt_err_alice, self.decrypt_err_bob, self.decrypt_err_eve,self.alice_output,self.bob_output,self.eve_output],
                                           feed_dict={self.msg: msg_in_val, self.secret: secret_val, self.seed: key_val})
            #eve_decrypt_error = min(eve_decrypt_error, decrypt_err)
            eve_decrypt_error = eve_decrypt_error + decrypt_err_eve
            bob_decrypt_error = bob_decrypt_error + decrypt_err_bob
            alice_decrypt_error = alice_decrypt_error + decrypt_err_alice

            print msg_in_val[0], key_val[0], alice[0],bob[0],eve[0]
            bob_test_error.append(bob_decrypt_error)
            eve_test_error.append(eve_decrypt_error)
            alice_test_error.append(alice_decrypt_error)

            visualize_image(convToInt(msg_in_val),self.file_name,bs,steg=False)
            visualize_image(convToInt(alice),self.file_name,bs,steg=True)





        print "here"
    
        f = open(self.file_name+".out.txt" ,'w')
        print "here2"
        for i in bob_test_error:
            f.write(str(i)+",")
        f.write("\n")
        for i in eve_test_error:
            f.write(str(i)+",")
        f.write("\n")
        for i in alice_test_error:
            f.write(str(i)+",")
        f.write("\n")
        f.write(str(np.mean(bob_test_error)) +", "+str(np.std(bob_test_error)))
        f.write("\n")
        f.write(str(np.mean(eve_test_error)) + ", " + str(np.std(eve_test_error)))
        f.write("\n")
        f.write(str(np.mean(alice_test_error)) + ", " + str(np.std(alice_test_error)))
        f.write("\n")
        f.close()
        print np.mean(bob_test_error), np.std(bob_test_error)
        print np.mean(eve_test_error), np.std(eve_test_error)
        print np.mean(alice_test_error), np.std(alice_test_error)

        return bob_decrypt_error, eve_decrypt_error, alice_decrypt_error
Beispiel #7
0
    checkpoint = ModelCheckpoint(filepath='best_model.h5',
                                 monitor='val_loss',
                                 mode='auto',
                                 save_best_only='True')
    callback_lists = [earlystop, checkpoint, losscalback, tensorboard]
    autoencoder = AutoEncoder()
    autoencoder.compile(optimizer='adam', loss='mse')
    autoencoder.summary()

    # training
    train_data, _ = load_train(shuffle_buffer_size=None)
    train_data_noisy, _ = load_train_moisy(shuffle_buffer_size=None)
    test_data, _ = load_test(shuffle_buffer_size=None)
    test_data_noisy, _ = load_test_moisy(shuffle_buffer_size=None)

    visualize_image(train_data[0].reshape(256, 256))
    visualize_image(train_data_noisy[0].reshape(256, 256))
    print((train_data_noisy[0] - train_data[0]).reshape(256, 256))

    autoencoder.fit(train_data_noisy,
                    train_data,
                    epochs=10000,
                    batch_size=16,
                    shuffle=True,
                    validation_data=(test_data_noisy, test_data),
                    callbacks=callback_lists)
    autoencoder = load_model('best_model.h5')
    decoded_imgs = autoencoder.predict(test_data_noisy)

    n = 20
    for i in range(n):