-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathDataAugmentation.py
42 lines (34 loc) · 1.36 KB
/
DataAugmentation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
from random import choice
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import json
# load training dataframe
data2 = pd.read_json('train.json')
X_band_1=np.array([np.array(band).astype(np.float32).reshape(75, 75) for band in data2["band_1"]])
X_band_2=np.array([np.array(band).astype(np.float32).reshape(75, 75) for band in data2["band_2"]])
channel_3 = X_band_1 + X_band_2
train_data = np.concatenate([X_band_1[:, :, :, np.newaxis],
X_band_2[:, :, :, np.newaxis],
channel_3[:, :, :, np.newaxis]], axis=-1)
train_targets = np.array(data2["is_iceberg"])
init_data = list(map(lambda x, y: [x, y], train_data, train_targets))
aug_images = []
for image, target in init_data:
img_v = np.flip(image, 0)
img_h = np.flip(image, 1)
img_rot = np.rot90(image, k=choice([1, 3]), axes=(0, 1))
img_prep = np.roll(image, shift=5, axis=(0, 1))
aug_images.append((img_v, target))
aug_images.append((img_h, target))
aug_images.append((img_rot, target))
aug_images.append((img_prep, target))
final_data = np.array(init_data + aug_images)
plt.imshow(init_data[1][0][:, :, 0])
plt.imshow(aug_images[4][0][:, :, 0])
plt.imshow(aug_images[5][0][:, :, 0])
plt.imshow(aug_images[6][0][:, :, 0])
plt.imshow(aug_images[7][0][:, :, 0])
plt.show()
# save data as file
np.save("aug_data", final_data)