Ejemplo n.º 1
0
        def train_generator():
            while True:
                for start in range(0, nTrain, self.batch_size):
                    x_batch = []
                    y_batch = []
                    end = min(start + self.batch_size, nTrain)

                    for i in range(start, end):
                        x = X_train[i]
                        if flag_expand_chan:
                            x = expand_chan(x)
                        x = train_datagen.random_transform(x)
                        # train_datagen.fit(x[np.newaxis,...])
                        # x = train_datagen.standardize(x)
                        # x = (x - np.mean(x, axis=(0,1))) / np.std(x, axis=(0,1))
                        # x = transformations(x, np.random.randint(3))
                        x = random_crop(x, (self.height, self.width))
                        x_batch.append(x)
                        # x_batch.append(train_datagen.random_transform(X_train[i]))
                        y_batch.append(y_train[i])

                    # for id in ids_train_batch.values:
                    #     # j = np.random.randint(self.nAug)
                    #     img = cv2.imread(INPUT_PATH + 'train_hq/{}.jpg'.format(id))
                    #     img = cv2.resize(img, (self.input_width, self.input_height), interpolation=cv2.INTER_LINEAR)
                    #     # img = transformations2(img, j)
                    #     mask = np.array(Image.open(INPUT_PATH + 'train_masks_fixed/{}_mask.gif'.format(id)), dtype=np.uint8)
                    #     mask = cv2.resize(mask, (self.input_width, self.input_height), interpolation=cv2.INTER_LINEAR)
                    #     # mask = transformations2(mask, j)
                    #     img = randomHueSaturationValue(img,
                    #                                    hue_shift_limit=(-50, 50),
                    #                                    sat_shift_limit=(-5, 5),
                    #                                    val_shift_limit=(-15, 15))
                    #     img, mask = randomShiftScaleRotate(img, mask,
                    #                                        shift_limit=(-0.0625, 0.0625),
                    #                                        scale_limit=(-0.1, 0.1),
                    #                                        rotate_limit=(-0, 0))
                    #     img, mask = randomHorizontalFlip(img, mask)
                    #     if self.factor != 1:
                    #         img = cv2.resize(img, (self.input_dim//self.factor, self.input_dim//self.factor), interpolation=cv2.INTER_LINEAR)
                    #     # draw(img, mask)
                    #
                    #     if self.direct_result:
                    #         mask = np.expand_dims(mask, axis=2)
                    #         x_batch.append(img)
                    #         y_batch.append(mask)
                    #     else:
                    #         target = np.zeros((mask.shape[0], mask.shape[1], self.nb_classes))
                    #         for k in range(self.nb_classes):
                    #             target[:,:,k] = (mask == k)
                    #         x_batch.append(img)
                    #         y_batch.append(target)
                    #
                    # x_batch = np.array(x_batch, np.float32) / 255.0
                    # y_batch = np.array(y_batch, np.float32)
                    x_batch = np.array(x_batch, np.float32)
                    y_batch = np.array(y_batch, np.float32)
                    yield x_batch, y_batch
Ejemplo n.º 2
0
def augment_operations(crops, cropped_img, cropped_dpth, resized_img,
                       resized_dpth, augmented_imgs, augmented_gts):

    for i in range(crops):
        crop_img, col_cj, row_ci = Intrinsic.random_crop(
            np.asarray(cropped_img), (224, 304))
        resize_img = Image.fromarray(crop_img.astype(np.uint8), 'RGB').resize(
            (304, 228), Image.BILINEAR)
        crop_dpth = np.asarray(cropped_dpth)[row_ci:row_ci + 224,
                                             col_cj:col_cj + 304]
        resize_dpth = Image.fromarray(crop_dpth).resize((160, 128),
                                                        Image.NEAREST)
        resize_dpth = np.expand_dims(resize_dpth, axis=3)
        augmented_imgs.append(np.asarray(resize_img))
        augmented_gts.append(np.asarray(resize_dpth))

    angle = np.random.randint(-10, 10)
    rotate = iaa.Affine(rotate=angle)
    rotated_img = rotate.augment_image(np.asarray(cropped_img))
    rotated_img = Image.fromarray(rotated_img.astype(np.uint8), 'RGB').resize(
        (304, 228), Image.BILINEAR)
    rotated_dpth = rotate.augment_image(np.asarray(cropped_dpth))
    rotated_dpth = Image.fromarray(rotated_dpth).resize((160, 128),
                                                        Image.NEAREST)
    rotated_dpth = np.expand_dims(rotated_dpth, axis=3)
    augmented_imgs.append(np.asarray(rotated_img))
    augmented_gts.append(np.asarray(rotated_dpth))

    colorjitter = iaa.AddToHueAndSaturation((-60, 60))
    enhanced_img = colorjitter.augment_image(np.asarray(resized_img))
    augmented_imgs.append(np.asarray(enhanced_img))
    augmented_gts.append(np.asarray(resized_dpth))

    swapped_img = np.array(resized_img)
    red = swapped_img[:, :, 0].copy()
    green = swapped_img[:, :, 1].copy()
    swapped_img[:, :, 1] = red
    swapped_img[:, :, 0] = green
    augmented_imgs.append(np.asarray(swapped_img))
    augmented_gts.append(np.asarray(resized_dpth))

    brghtperturbator = iaa.Multiply(
        (0.8, 1.2), per_channel=0.2)  #50-150% of original value
    perturbed_img = brghtperturbator.augment_image(np.asarray(resized_img))
    augmented_imgs.append(np.asarray(perturbed_img))
    augmented_gts.append(np.asarray(resized_dpth))

    flp2 = iaa.HorizontalFlip(1)
    flipped_img2 = flp2.augment_image(np.asarray(resized_img))
    flipped_dpth2 = flp2.augment_image(np.asarray(resized_dpth))
    augmented_imgs.append(np.asarray(flipped_img2))
    augmented_gts.append(np.asarray(flipped_dpth2))
Ejemplo n.º 3
0
    def test_ensemble(self, is_training=False):
        if is_training:
            images = self.train_images
        else:
            images = self.test_images
        test_array_shape = images.shape  # (8424, 75, 75, 2)
        preds = 0
        nTest = len(images)
        K = 5
        print(images.shape)
        print("# test images: ", nTest)
        self.test_predictions = 0
        for fold_id in range(self.num_folds):
            kfold_weights_path = '../weights/best_weights_{}_{}.hdf5'.format(self.base_model, fold_id)
            self.model.load_weights(kfold_weights_path)

            # make predictions with TTA

            # test_predictions = self.model.predict(images)
            # preds += test_predictions[:, 1] / float(K*self.num_folds)
            #
            # for k in range(K-1):
            #     aug_test_images = images
            #     for i in range(nTest):
            #         aug_test_images[i] = transformations(aug_test_images[i], k)
            #
            #     test_predictions = self.model.predict(aug_test_images)
            #     preds += test_predictions[:,1] / float(K*self.num_folds)


            aug_test_images = np.random.randn(test_array_shape[0], self.height, self.width, test_array_shape[-1] + 1*flag_expand_chan)
            for k in range(K):
                for i in range(nTest):
                    x = images[i]
                    if flag_expand_chan:
                        x = expand_chan(x)
                    # x = (x - np.mean(x, axis=(0, 1))) / np.std(x, axis=(0, 1))

                    aug_test_images[i] = random_crop(x, (self.height, self.width))
                    # test_predictions = self.model.predict(images)
                self.test_predictions += self.model.predict(aug_test_images) / float(K * self.num_folds)

        # save the predictions csv file
        if is_training:
            pred_df = self.train_df[['id']].copy()
            pred_df['is_iceberg'] = np.clip(self.test_predictions[:, 1], 0, 1)
            pred_df['gt'] = self.train_df[['is_iceberg']].copy()
            pred_df.to_csv('../features/features_ensemble_{}.csv'.format(self.base_model), index=False)
        else:
            pred_df = self.test_df[['id']].copy()
            pred_df['is_iceberg'] = np.clip(self.test_predictions[:, 1], 0, 1)
            pred_df.to_csv('../submit/predictions_ensemble_{}.csv'.format(self.base_model), index=False)
Ejemplo n.º 4
0
            def val_generator():
                while True:
                    for start in range(0, nVal, self.batch_size):
                        x_batch = []
                        y_batch = []
                        end = min(start + self.batch_size, nVal)

                        for i in range(start, end):
                            x = X_val[i]
                            if flag_expand_chan:
                                x = expand_chan(x)
                            x = val_datagen.random_transform(x)
                            # x = (x - np.mean(x, axis=(0, 1))) / np.std(x, axis=(0, 1))

                            x = random_crop(x, (self.height, self.width), center=True)
                            x_batch.append(x)
                            y_batch.append(y_val[i])
                        x_batch = np.array(x_batch, np.float32)
                        y_batch = np.array(y_batch, np.float32)
                        yield x_batch, y_batch
Ejemplo n.º 5
0
    def test(self):
        # make predictions
        test_array_shape = self.test_images.shape # (8424, 75, 75, 2)
        aug_test_images = np.random.randn(test_array_shape[0], self.height, self.width, test_array_shape[-1] + flag_expand_chan)

        self.model.load_weights('../weights/best_weights_{}.hdf5'.format(self.base_model))

        nTest = len(self.test_images)
        K = 5
        print("# test images: ", nTest)
        test_predictions = 0

        # test_datagen = ImageDataGenerator(
        #     featurewise_center=True,
        #     featurewise_std_normalization=True,
        #     horizontal_flip=True,
        #     vertical_flip=True
        # )

        for k in range(K):
            for i in range(nTest):
                x = self.test_images[i]
                if flag_expand_chan:
                    x = expand_chan(x)
                # test_datagen.fit(x[np.newaxis,...])
                # x = test_datagen.standardize(x)
                # x = (x - np.mean(x, axis=(0, 1))) / np.std(x, axis=(0, 1))

                aug_test_images[i] = random_crop(x, (self.height, self.width))
                # test_predictions = self.model.predict(self.test_images)
            test_predictions += self.model.predict(aug_test_images) / float(K)


        # save the predictions csv file
        pred_df = self.test_df[['id']].copy()
        pred_df['is_iceberg'] = test_predictions[:,1]
        pred_df.to_csv('../submit/predictions.csv', index = False)
Ejemplo n.º 6
0
def metric_images(im_dir, indices):

    fx, fy, cx, cy = Intrinsic.initIntrinsic()
    final_fx, final_fy, final_cx, final_cy = Intrinsic.resizeIntrinsic(
        228, 304, fx, fy, cx, cy, 2.0)
    orig_metric_cord = Intrinsic.findMetricCordinates(final_fx, final_fy,
                                                      final_cx, final_cy, 304,
                                                      228)

    # 3 crops and resize all crops to 304x228
    crop1_fx, crop1_fy, crop1_cx, crop1_cy = Intrinsic.crop_and_resizeIntrinsic(
        423, 567, 468, 624, fx, fy, cx, cy, 1.85)  # 90% crop
    crop2_fx, crop2_fy, crop2_cx, crop2_cy = Intrinsic.crop_and_resizeIntrinsic(
        375, 500, 468, 624, fx, fy, cx, cy, 1.64)  # 80% crop
    crop3_fx, crop3_fy, crop3_cx, crop3_cy = Intrinsic.crop_and_resizeIntrinsic(
        354, 472, 468, 624, fx, fy, cx, cy, 1.55)  # 75% crop

    crop_metric_cord1 = Intrinsic.findMetricCordinates(crop1_fx, crop1_fy,
                                                       crop1_cx, crop1_cy, 304,
                                                       228)
    crop_metric_cord2 = Intrinsic.findMetricCordinates(crop2_fx, crop2_fy,
                                                       crop2_cx, crop2_cy, 304,
                                                       228)
    crop_metric_cord3 = Intrinsic.findMetricCordinates(crop3_fx, crop3_fy,
                                                       crop3_cx, crop3_cy, 304,
                                                       228)

    f = h5py.File(im_dir)
    augmented_imgs = []
    augmented_gts = []
    ia.seed(1)
    #print(indices.shape)
    for index in indices:
        img_file = f['images'][index - 1]
        dpth_file = f['depths'][index - 1].T

        img_reshaped = np.transpose(img_file, (2, 1, 0))

        img = Image.fromarray(img_reshaped.astype(np.uint8), 'RGB')
        dpth = Image.fromarray(dpth_file.astype(np.float64))

        border = (8, 6, 8, 6)  # left, up, right, bottom
        cropped_img = ImageOps.crop(img, border)
        cropped_dpth = ImageOps.crop(dpth, border)

        resized_img = cropped_img.resize((304, 228), Image.BILINEAR)
        resized_dpth = cropped_dpth.resize((160, 128), Image.NEAREST)
        resized_dpth = np.expand_dims(resized_dpth, axis=3)
        resized_dpth_arr = np.array(resized_dpth)
        resized_dpth_arr[resized_dpth_arr < 0.5] = 0.0

        #print('Img shape ' + str(np.shape(np.asarray(resized_img))) )
        #print('Metric shape ' + str(np.shape(metric_cord)) )

        transfrmd_img = np.concatenate(
            (np.asarray(resized_img), orig_metric_cord), axis=2)

        #crops = 3
        #for i in range(crops):
        crop_img1, col_cj, row_ci = Intrinsic.random_crop(
            np.asarray(cropped_img), (423, 564))
        crop_img2, col_cj2, row_ci2 = Intrinsic.random_crop(
            np.asarray(cropped_img), (375, 500))
        crop_img3, col_cj3, row_ci3 = Intrinsic.random_crop(
            np.asarray(cropped_img), (354, 472))

        resize_img1 = Image.fromarray(crop_img1.astype(np.uint8),
                                      'RGB').resize((304, 228), Image.BILINEAR)
        resize_img2 = Image.fromarray(crop_img2.astype(np.uint8),
                                      'RGB').resize((304, 228), Image.BILINEAR)
        resize_img3 = Image.fromarray(crop_img3.astype(np.uint8),
                                      'RGB').resize((304, 228), Image.BILINEAR)

        crop_transfrmd_img1 = np.concatenate(
            (np.asarray(resize_img1), crop_metric_cord1), axis=2)
        crop_transfrmd_img2 = np.concatenate(
            (np.asarray(resize_img2), crop_metric_cord2), axis=2)
        crop_transfrmd_img3 = np.concatenate(
            (np.asarray(resize_img3), crop_metric_cord3), axis=2)

        crop_dpth1 = np.asarray(cropped_dpth)[row_ci:row_ci + 423,
                                              col_cj:col_cj + 564]
        crop_dpth2 = np.asarray(cropped_dpth)[row_ci2:row_ci2 + 375,
                                              col_cj2:col_cj2 + 500]
        crop_dpth3 = np.asarray(cropped_dpth)[row_ci3:row_ci3 + 354,
                                              col_cj3:col_cj3 + 472]

        resize_dpth1 = Image.fromarray(crop_dpth1).resize((160, 128),
                                                          Image.NEAREST)
        resize_dpth2 = Image.fromarray(crop_dpth2).resize((160, 128),
                                                          Image.NEAREST)
        resize_dpth3 = Image.fromarray(crop_dpth3).resize((160, 128),
                                                          Image.NEAREST)

        resize_dpth1 = np.expand_dims(resize_dpth1, axis=3)
        resize_dpth2 = np.expand_dims(resize_dpth2, axis=3)
        resize_dpth3 = np.expand_dims(resize_dpth3, axis=3)

        augmented_imgs.append(np.asarray(crop_transfrmd_img1))
        augmented_gts.append(np.asarray(resize_dpth1))
        augmented_imgs.append(np.asarray(crop_transfrmd_img2))
        augmented_gts.append(np.asarray(resize_dpth2))
        augmented_imgs.append(np.asarray(crop_transfrmd_img3))
        augmented_gts.append(np.asarray(resize_dpth3))

        augmented_imgs.append(np.asarray(transfrmd_img))
        augmented_gts.append(np.asarray(resized_dpth_arr))

        #crops = 3
        #augment_operations(crops, cropped_img, cropped_dpth, resized_img, resized_dpth, augmented_imgs, augmented_gts)
    #print('Shape ----- ' + str(np.shape(augmented_imgs)))
    augmented_imgs = np.array(augmented_imgs, dtype=np.uint8)
    augmented_gts = np.asarray(augmented_gts)

    return augmented_imgs, augmented_gts