Views
No views yet
Lambda layers has been discarded because they do not allow the use of external libraries that do not work with tensors, since we want to use the functions provided by OpenCV and NumPy.1filters = {
2 "original": lambda x: x,
3 "red": lambda x: data.getImageTensor(x, (330, 0, 0), (360, 255, 255)) + data.getImageTensor(x, (0, 0, 0), (50, 255, 255)),
4 "green": lambda x: data.getImageTensor(x, (60, 0, 0), (130, 255, 255)),
5 "blue": lambda x: data.getImageTensor(x, (180, 0, 0), (270, 255, 255)),
6}scripts/Data.py file as follows:1def detectColor(self, image, lower, upper):
2 if tf.is_tensor(image):
3 temp_image = image.numpy().copy() # Used for training
4 else:
5 temp_image = image.copy() # Used for displaying the image
6
7 hsv_image = temp_image.copy()
8 hsv_image = cv.cvtColor(hsv_image, cv.COLOR_RGB2HSV)
9 mask = cv.inRange(hsv_image, lower, upper)
10
11 result = temp_image.copy()
12 result[np.where(mask == 0)] = 0
13
14 return result
15
16def getImageTensor(self, images, lower, upper):
17 results = []
18
19 for img in images:
20 results.append(np.expand_dims(self.detectColor(img, lower, upper), axis=0))
21
22 return np.concatenate(results, axis=0)
scripts/Model.py file in the following function:1def create_model():
2 class FilterLayer(layers.Layer):
3 def __init__(self, filter, **kwargs):
4 self.filter = filter
5
6 super(FilterLayer, self).__init__(name="filter_layer", **kwargs)
7
8 def call(self, image):
9 shape = image.shape
10 [image, ] = tf.py_function(self.filter, [image], [tf.float32])
11 image = backend.stop_gradient(image)
12 image.set_shape(shape)
13
14 return image
15
16 def get_config(self):
17 return super().get_config()
18
19 model = models.Sequential()
20
21 model.add(layers.Input(shape=(215, 538, 3)))
22 model.add(FilterLayer(filter=self.filter))
23
24 model.add(layers.Conv2D(32, (3, 3), activation="relu"))
25 model.add(layers.MaxPooling2D(pool_size=(2, 2)))
26
27 model.add(layers.Conv2D(32, (3, 3), activation="relu"))
28 model.add(layers.GlobalAveragePooling2D())
29
30 model.add(layers.Dropout(rate=0.4))
31 model.add(layers.Dense(32, activation="relu"))
32 model.add(layers.Dropout(rate=0.4))
33 model.add(layers.Dense(2, activation="softmax"))
34
35 return model