forked from joaquin-mari/Augsburg-Developers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_hackathon.py
82 lines (64 loc) · 2.83 KB
/
data_hackathon.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import sys
import os
import cv2
import glob
import numpy as np
import subprocess
from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.keras.applications import InceptionV3
from tensorflow.keras.utils import to_categorical
def install(package):
subprocess.check_call([sys.executable, "-m", "pip", "install", package])
try:
from sklearn.preprocessing import LabelEncoder
except ImportError:
install('scikit-learn')
from sklearn.preprocessing import LabelEncoder
def load_images_and_labels(train_dir, lidar_dir, label_conversion_func):
train_images = []
train_depthmaps = []
train_labels = []
for label_dir in os.listdir(train_dir):
for filename in os.listdir(os.path.join(train_dir, label_dir)):
file_root, _ = os.path.splitext(filename)
# Load and process the image
img = cv2.imread(os.path.join(train_dir, label_dir, filename))
img = cv2.resize(img, (150, 150))
img = img / 255.0 # Normalization
train_images.append(img)
train_labels.append(label_conversion_func(label_dir))
# Load and process a corresponding depth map from LIDAR data
try:
depthmap = np.load(os.path.join(lidar_dir, file_root + ".npy"))
depthmap = cv2.resize(depthmap, (150, 150))
depthmap = depthmap / np.max(depthmap) # normalize to 0-1
train_depthmaps.append(depthmap[..., np.newaxis])
except:
train_depthmaps.append(np.zeros((150, 150, 1)))
train_images = np.array(train_images)
train_depthmaps = np.array(train_depthmaps)
train_labels = np.array(train_labels)
train_data = np.concatenate([train_images, train_depthmaps], axis=-1)
return train_data, train_labels
train_dir = 'path_to_your_training_data_directory'
lidar_dir = 'path_to_your_LIDAR_data_directory'
train_images, train_labels = load_images_and_labels(train_dir, lidar_dir, your_label_conversion_func)
le = LabelEncoder()
train_labels = le.fit_transform(train_labels)
train_labels = to_categorical(train_labels)
base_model = InceptionV3(input_shape = (150, 150, 4), include_top = False, weights = None)
for layer in base_model.layers:
layer.trainable = False
x = layers.Flatten()(base_model.output)
x = layers.Dense(1024, activation='relu')(x)
x = layers.Dropout(0.2)(x)
x = layers.Dense(train_labels.shape[1], activation='softmax')(x)
model = keras.models.Model(base_model.input, x)
model.compile(optimizer = keras.optimizers.RMSprop(lr=0.0001),
loss = 'categorical_crossentropy',
metrics = ['acc'])
history = model.fit(train_images,
train_labels,
epochs=20)
model.save('food_model.h5')