def __getitem__(self, index):

        # Load images
        image0, (height,
                 width) = data_utils.load_image(self.image0_paths[index],
                                                shape=self.shape,
                                                normalize=self.normalize)

        if self.image1_paths[index] is not None:
            image1, _ = data_utils.load_image(self.image1_paths[index],
                                              shape=self.shape,
                                              normalize=self.normalize)
        else:
            image1 = np.zeros_like(image0)

        # Default KITTI baseline, focal length
        scale_factor = \
            np.asarray(0.30725988566827694, np.float32) / np.asarray(width, np.float32)

        scale_factor = np.reshape(scale_factor,
                                  list(scale_factor.shape) + [1, 1, 1])

        # Load class map
        if self.class_mask_paths[index] is not None:
            class_mask = data_utils.load_class_mask(
                self.class_mask_paths[index], shape=self.shape)
        else:
            class_mask = np.ones([1] + list(self.shape), dtype=np.float32)

        return image0, image1, scale_factor, class_mask
    def __getitem__(self, index):
        # Load image
        image0, (height, width) = data_utils.load_image(
            self.image0_paths[index],
            shape=self.shape,
            normalize=self.normalize)
        image1, _  = data_utils.load_image(
            self.image1_paths[index],
            shape=self.shape,
            normalize=self.normalize)

        # Load camera (focal length and baseline)
        camera = np.load(self.camera_paths[index]).astype(np.float32)
        camera = np.prod(camera) / np.asarray(width, np.float32)
        camera = np.reshape(camera, list(camera.shape) + [1, 1, 1])

        # Color augmentation
        if self.augment and np.random.uniform(0.0, 1.0, 1) > 0.50:
            image0, image1 = augment_image_color(
                [image0, image1],
                color_range=self.color_range,
                intensity_range=self.intensity_range,
                gamma_range=self.gamma_range,
                normalize=self.normalize)

        return image0, image1, camera
    def evaluate():
        attention_plot = np.zeros((caption_max_length, attention_features_shape))

        hidden = decoder.reset_state(batch_size=1)

        temp_input = tf.expand_dims(load_image(image)[0], 0)
        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]))

        image_features_encoder = encoder(img_tensor_val)

        dec_input = tf.expand_dims([tokenizer.word_index['<start>']], 0)
        result = []
        for i in range(caption_max_length):
            predictions, hidden, attention_weights = decoder(dec_input, image_features_encoder, hidden)

            attention_plot[i] = tf.reshape(attention_weights, (-1,)).numpy()

            predicted_id = tf.argmax(predictions[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
Esempio n. 4
0
    def __getitem__(self, index):

        shape_resize = (self.n_height, self.n_width)

        # Load images
        image0 = data_utils.load_image(self.image0_paths[index],
                                       shape=shape_resize,
                                       normalize=self.normalize)

        image1 = data_utils.load_image(self.image1_paths[index],
                                       shape=shape_resize,
                                       normalize=self.normalize)

        # Load ground truth
        ground_truth = data_utils.load_disparity(
            self.ground_truth_paths[index], shape=shape_resize)

        return image0, image1, ground_truth
Esempio n. 5
0
from face_recognition import get_face
import data_utils as d

pred = get_face(d.get_embeddings([d.load_image('data/test/IMG_1271.JPG', 96)]))
Esempio n. 6
0
                               transforms.ToTensor(),
                               transforms.Lambda(lambda x: x.mul(255))])
train_set = datasets.ImageFolder(args.dataset_path, transform)
data_loader = DataLoader(dataset=train_set, num_workers=args.threads, batch_size=args.batchSize, shuffle=True)


print('===> Building model')
model = models.ImageTransformNet()
if cuda:
    model.cuda()

optimizer = optim.Adam(model.parameters(), lr=args.lr)
model.train()

print('===> Loading Style Image')
style_image = load_image(args.style_image, args.image_size)
style_image_batch = style_image.repeat(args.batchSize, 1, 1, 1)
style_image_batch = batch_rgb_to_bgr(style_image_batch)
if cuda:
    style_image_batch = style_image_batch.cuda()
xs = Variable(style_image_batch, volatile=True)

print('===> Training model')
def train(epoch):
    epoch_loss = 0
    for iteration, batch in enumerate(data_loader):
        x = Variable(batch[0])
        x = batch_rgb_to_bgr(x)
        if cuda:
            x = x.cuda()
        
Esempio n. 7
0
current_dir = os.getcwd()
num_classes = 2
mean = np.load("./d/Carvana/mean_of_traindata.npy")

##### Computational Graph - Start #####

X = tf.placeholder(tf.float32, shape=(1, 256, 256, 1), name="myInput")
net_logits = unet(X, num_classes)

##### Computational Graph - End #####

with tf.Session() as sess:
    saver = tf.train.Saver()
    saver.restore(sess, "./tmp/without_batch/model.ckpt")
    print("Model Loaded")

    print(tf.trainable_variables())

    # Load image as numpy array using data_utils
    img = load_image("./Carvana/train/0d53224da2b7_04.jpg")
    # Zero mean image based on training data
    img = img - np.expand_dims(np.expand_dims(mean, axis=3), axis=0)
    img = img/255
    print(img.shape)

    # TODO: Add batch prediction.

    image = sess.run(pixel_wise_softmax(net_logits), feed_dict={X: img})
    # print(f"shape of net output is = {logits.shape}")
    save_image(image=image, name="test_prediction.png")
Esempio n. 8
0
def main():
    """
    Wrapper to run the classification task
    """
    # Parse command-line arguments
    parser = build_parser()
    options = parser.parse_args()

    if options.mode == "gen_data":
        # Split the data into train/dev/test sets
        split_data()

        # Load the data and reshape for training and evaluation
        X, y_media, y_emotion = load_data(update=options.update,
                                          remove_broken=options.remove_broken)

        for set_type in ["train", "dev", "test"]:
            total_media = np.sum(y_media[set_type], axis=0)
            total_emotion = np.sum(y_emotion[set_type], axis=0)

            print(f"Total images for each media category in {set_type} set:")
            for v, k in enumerate(MEDIA_LABELS):
                print(f"\t{k}: {total_media[v]}")
            print(f"Total images for each emotion category in {set_type} set:")
            for v, k in enumerate(EMOTION_LABELS):
                print(f"\t{k}: {total_emotion[v]}")

    elif options.mode == "train":
        # Create directory to save the results
        results_dir = "results"
        if not os.path.exists("./" + results_dir):
            os.makedirs("./" + results_dir)
        # Check if the given log folder already exists
        results_subdirs = os.listdir("./" + results_dir)
        if not options.log_folder:
            raise Exception(
                'Please specify log_folder argument to store results.')
        elif options.log_folder in results_subdirs:
            raise Exception('The given log folder already exists.')
        else:
            # Create a folder for each training run
            log_folder = os.path.join(results_dir, options.log_folder)
            os.makedirs(log_folder)

        # Load the data and organize into three tuples (train, val/dev, test)
        # Each tuple consists of input arrays, media labels, and emotion labels
        train_data, val_data, test_data = load_data(DATA_DIR, INPUT_FILE,
                                                    MEDIA_LABEL_FILE,
                                                    EMOTION_LABEL_FILE)

        # Preprocess the data
        train_dset, val_dset, test_dset = preprocess(
            train_data,
            val_data,
            test_data,
            augment=options.augment,
            train_stats_dir=TRAIN_STATS_DIR)

        # Specify the device:
        if options.device == "cpu":
            device = "/cpu:0"
        elif options.device == "gpu":
            device = "/device:GPU:0"

        # Train the model
        train(train_dset,
              val_dset,
              log_folder=log_folder,
              device=device,
              batch_size=64,
              num_epochs=100,
              model_type=options.model_type)

    elif options.mode == "test":
        # Load the data and organize into three tuples (train, val/dev, test)
        # Each tuple consists of input arrays, media labels, and emotion labels
        train_data, val_data, test_data = load_data(DATA_DIR, INPUT_FILE,
                                                    MEDIA_LABEL_FILE,
                                                    EMOTION_LABEL_FILE)
        # Preprocess the data
        if os.path.isfile(os.path.join(TRAIN_STATS_DIR, "train_stats.npz")):
            print(
                "Preprocess test data using saved statistics from train data..."
            )
            train_stats_file = os.path.join(TRAIN_STATS_DIR, "train_stats.npz")
            test_dset = preprocess_from_file(train_stats_file,
                                             test_data,
                                             augment=options.augment)
        else:
            print("Preprocess test data using train data...")
            train_dset, val_dset, test_dset = preprocess(
                train_data,
                val_data,
                test_data,
                augment=options.augment,
                train_stats_dir=TRAIN_STATS_DIR)

        # Specify the device:
        if options.device == "cpu":
            device = "/cpu:0"
        elif options.device == "gpu":
            device = "/device:GPU:0"

        # Load the model
        model_path = os.path.join("test_models", options.model_name)
        evaluate_test(model_path,
                      options.model_type,
                      test_dset,
                      batch_size=64,
                      confusion_mat=options.confusion_mat)

    elif options.mode == "ensemble":
        # Load the data and organize into three tuples (train, val/dev, test)
        # Each tuple consists of input arrays, media labels, and emotion labels
        train_data, val_data, test_data = load_data(DATA_DIR, INPUT_FILE,
                                                    MEDIA_LABEL_FILE,
                                                    EMOTION_LABEL_FILE)
        # Preprocess the data
        if os.path.isfile(os.path.join(TRAIN_STATS_DIR, "train_stats.npz")):
            print(
                "Preprocess test data using saved statistics from train data..."
            )
            train_stats_file = os.path.join(TRAIN_STATS_DIR, "train_stats.npz")
            test_dset = preprocess_from_file(train_stats_file,
                                             test_data,
                                             augment=options.augment)
        else:
            print("Preprocess test data using train data...")
            train_dset, val_dset, test_dset = preprocess(
                train_data,
                val_data,
                test_data,
                augment=options.augment,
                train_stats_dir=TRAIN_STATS_DIR)
        # Specify the device:
        if options.device == "cpu":
            device = "/cpu:0"
        elif options.device == "gpu":
            device = "/device:GPU:0"

        if not options.ensemble_folder:
            raise Exception(
                'Please specify ensemble_folder argument to find ensemble folders.'
            )
        elif len(os.listdir(options.ensemble_folder)) == 0:
            raise Exception('Ensemble folder is empty.')

        # Evaluate the ensemble
        evaluate_ensemble(options.ensemble_folder,
                          test_dset,
                          batch_size=64,
                          confusion_mat=options.confusion_mat)

    elif options.mode == "test_single":
        x_test = load_image(
            os.path.join('stylized_images_configs', options.image))
        train_stats_file = os.path.join(TRAIN_STATS_DIR, "train_stats.npz")
        x_test = preprocess_image(train_stats_file,
                                  x_test,
                                  augment=options.augment)

        model_path = os.path.join("test_models", options.model_name)
        predict_image(x_test, model_path)