Skip to content
This repository has been archived by the owner on Feb 28, 2023. It is now read-only.

Commit

Permalink
[ADD] Generator class
Browse files Browse the repository at this point in the history
  • Loading branch information
riZZZhik committed Sep 18, 2020
1 parent 15a79c3 commit 46be83d
Show file tree
Hide file tree
Showing 8 changed files with 150 additions and 14 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,6 @@ dmypy.json
# Pyre type checker
.pyre/

# User ignores
dataset/sources/~$Labels.docx
ml/generator.py
utils.py
jupyter_config_itmo.py
2 changes: 1 addition & 1 deletion .idea/Facades.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 15 additions & 9 deletions facades.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@
keras.backend.set_image_data_format('channels_last')


class Facades: # TODO: Predict # TODO: Comments
class Facades: # TODO: Predict
"""Class for segmentation buildings' facades using Deep Learning"""
def __init__(self, dim=(720, 1080), n_channels=3, use_datagenerator=True):
"""Initialize main parameters of Facades class
"""Initialize main parameters of Facades class s
:param dim: Images resolution in format (height, width)
:param n_channels: Number of channels in image (1 or 3)
"""
Expand Down Expand Up @@ -78,11 +79,12 @@ def training_init(self, dataset_dir="dataset", batch_size=8):

# Initialize data generators
seed = 909 # to transform image and masks with same augmentation parameter.
image_generator = data_datagen.flow_from_directory(dataset_dir + "train/data/", class_mode=None, seed=seed,
batch_size=batch_size, target_size=self.dim)
masks_generator = masks_datagen.flow_from_directory(dataset_dir + "train/masks/", class_mode=None, seed=seed,
batch_size=batch_size, target_size=self.dim,
color_mode="grayscale")
image_generator = data_datagen.flow_from_directory(dataset_dir + "train/data/", class_mode=None,
seed=seed, batch_size=batch_size,
target_size=self.dim)
masks_generator = masks_datagen.flow_from_directory(dataset_dir + "train/masks/", class_mode=None,
seed=seed, batch_size=batch_size,
target_size=self.dim, color_mode="grayscale")

self.train_generator = zip(image_generator, masks_generator)

Expand Down Expand Up @@ -117,11 +119,10 @@ def training_init(self, dataset_dir="dataset", batch_size=8):
self.model = Model(base_model.input, output_layer)
# keras.utils.plot_model(self.model, 'model.png', show_shapes=True)

def train(self, epochs=1, number_of_GPUs=0, remote_monitor_ip="localhost:4959"):
def train(self, epochs=1, number_of_GPUs=0):
"""Train model
:param epochs: Number of epochs to train model
:param number_of_GPUs: Number of devices to train on multiple GPUs
:param remote_monitor_ip: IP where to run Keras RemoteMonitor (default: localhost:4959) # TODO
"""
# Check training init before train
if not self.training_init_check:
Expand Down Expand Up @@ -151,6 +152,11 @@ def train(self, epochs=1, number_of_GPUs=0, remote_monitor_ip="localhost:4959"):


if __name__ == "__main__":
# Initialize main class
facades = Facades((512, 512))

# Initialize training dataset
facades.training_init("dataset/", batch_size=5)

# Run model training
facades.train(10)
2 changes: 1 addition & 1 deletion jupyter_config_itmo.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
c.NotebookApp.port = 4958
c.NotebookApp.password = u'sha1:1236633a1c55:9f2f5745eb1da1304369e01b65e5ad6d64f1cb13'

c.NotebookApp.open_browser = False
c.NotebookApp.open_browser = False
1 change: 1 addition & 0 deletions ml/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
from .generator import Generator
from .residual_model import residual_model
52 changes: 52 additions & 0 deletions ml/generator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import numpy as np
from cv2 import imread
import os
from keras.utils import Sequence


class Generator(Sequence):
"""Generates data for Keras"""

def __init__(self, dataset_dir, batch_size=16, dim=(1920, 1080), n_channels=3, shuffle=True):
"""Initialization"""
data_folder = dataset_dir + "/data"
self.data_paths = os.listdir(data_folder)

labels_path = dataset_dir + "/masks"
self.labels_paths = os.listdir(labels_path)

self.batch_size = batch_size
self.dim = dim
self.n_channels = n_channels

self.shuffle = shuffle

self.len = len(self.data_paths)

def __len__(self):
"""Denotes the number of batches per epoch
:return: number of batches per epoch
"""
return len(np.floor(self.len / self.batch_size))

def __getitem__(self, index):
"""Generate one batch of data
:param index: index of the batch
:return: X and y when fitting. X only when predicting
"""
# Generate indexes of the batch
indexes = self.indexes[index * self.batch_size:(index + 1) * self.batch_size]

# Download data by indexes
X, y = [], []
for i in indexes:
X.append(imread(self.data_paths[i]))
y.append(get_label(self.labels_paths[i]))

return X, y

def on_epoch_end(self):
"""Updates indexes after each epoch"""
self.indexes = np.arange(self.len)
if self.shuffle:
np.random.shuffle(self.indexes)
77 changes: 77 additions & 0 deletions utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import os

import numpy as np
from PIL import Image
from tqdm import tqdm


# def fix_dataset_labels(labels_path, colors=None):
# if colors is None:
# colors = { # RGB
# (0, 0, 255): 0, # Wall
# (0, 0, 170): 1, # Background
# (255, 255, 0): 2, # Window (closed)
# (0, 85, 255): 2, # Window
# (0, 170, 255): 7, # Door
# (170, 0, 0): 8, # Shop
# (170, 255, 85): 3, # Balcony
# (255, 85, 0): 6, # Molding
# (255, 3, 0): 11, # Pillar
# (0, 255, 255): 12, # Cornice
# (85, 255, 170): 4, # Sill
# (255, 170, 0): 9, # Deco
# # (): 11, # Blind
# }
#
# try:
# os.mkdir(labels_path + "_FIXED/")
# except:
# pass
#
# files = [f for f in os.listdir(labels_path) if f[-4:] in [".jpg", ".png"]]
# for id, image_name in enumerate(tqdm(files)):
# image_path = labels_path + "/" + image_name
# image = cv.imread(image_path)
#
# for h in range(len(image)):
# for w in range(len(image[h])):
# c = image[h][w]
# for key, value in colors.items():
# if c[2] in range(key[0] - 3, key[0] + 3) and c[1] in range(key[1] - 2, key[1] + 3) and \
# c[0] in range(key[2] - 2, key[2] + 3):
# image[h][w] = value
# break
#
# image = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
# cv.imwrite(labels_path + "_FIXED/" + image_name, image)


# def show_batch_photos(facades_class):
# x_batch, y_batch = next(facades_class.train_generator)
# for i in range(0, facades_class.batch_size):
# x = x_batch[i]
# y = y_batch[i]
#
# plt.imshow(x)
# plt.show() # TODO: Check needing two
# plt.imshow(y)
# plt.show()


def load_images_from_folder(dir, shape):
files = [file for file in os.listdir(dir) if file.endswith((".png", ".jpg"))]
result = []

for image_name in tqdm(files, dir):
image_path = dir + "/" + image_name
image = Image.open(image_path)
image_resized = image.resize(shape[:-1], Image.ANTIALIAS)
image_numpy = np.array(image_resized)
if shape[-1] == 1:
image_numpy = np.expand_dims(image_numpy, axis=-1)

result.append(image_numpy)

return np.array(result)


0 comments on commit 46be83d

Please sign in to comment.