@@ -17,6 +17,7 @@ import numpy as np
17
17
import tensorflow as tf
18
18
from tensorflow import keras
19
19
from tensorflow.keras.preprocessing import image_dataset_from_directory
20
+ from tensorflow.keras.preprocessing.image import img_to_array, array_to_img
20
21
{% if data_format == "Image files" %}
21
22
import urllib
22
23
import zipfile
@@ -77,7 +78,7 @@ batch_size = {{ batch_size }}
77
78
num_epochs = {{ num_epochs}}
78
79
79
80
{# TODO Add Image_Size #}
80
- img_size = (224,224 )
81
+ img_size = (160,160 )
81
82
img_shape = img_size + (3,)
82
83
83
84
# Set up logging.
@@ -103,9 +104,11 @@ def preprocess(data, name):
103
104
104
105
{% if data_format == "Image files" %}
105
106
# Read image files to tensorflow dataset.
106
- dataset = tf.keras.preprocessing.image.ImageDataGenerator(rescale=1./255, rotation_range=20, horizontal_flip=True)
107
- loader = dataset.flow_from_directory(data, target_size=img_size)
108
- {# TODO: Add more data_augmentation #}
107
+ dataset = image_dataset_from_directory(data,
108
+ shuffle=(name=="train"),
109
+ image_size=img_size)
110
+
111
+
109
112
{% elif data_format == "Numpy arrays" %}
110
113
images, labels = data
111
114
@@ -118,33 +121,41 @@ def preprocess(data, name):
118
121
# If images are grayscale, convert to RGB by duplicating channels.
119
122
if images.shape[1] == 1:
120
123
images = np.stack((images[:, 0],) * 3, axis=1)
121
- )
124
+
122
125
123
- {# This code could be improved #}
124
- Pil_image = []
126
+ images = np.rollaxis(images, 1, 4) # Reshape Image to channels_last
125
127
126
- for i in range(len(data[0])):
127
- Pil_image.append(tf.keras.preprocessing.image.array_to_img(images[i],data_format ="channels_first"))
128
- Pil_image[i] = Pil_image[i].resize((224,224)
129
- Pil_image[i] = tf.keras.preprocessing.image.img_to_array(Pil_image[i])
128
+ images = np.asarray([img_to_array(array_to_img(im, scale=False).resize((256,256))) for im in images])
130
129
131
- loader = tf.convert_to_tensor(Pil_image)
130
+ dataset = images, labels
132
131
{% endif %}
133
-
134
- return loader
132
+ return dataset
135
133
136
134
train_loader = preprocess(train_data, "train")
137
135
val_loader = preprocess(val_data, "val")
138
136
test_loader = preprocess(test_data, "test")
139
137
138
+ {{ header("data augmentation") }}
139
+
140
+ data_augmentation = tf.keras.Sequential([
141
+ tf.keras.layers.experimental.preprocessing.RandomFlip('horizontal'),
142
+ tf.keras.layers.experimental.preprocessing.RandomRotation(0.2),
143
+ ])
144
+
140
145
{{ header("Model") }}
141
146
142
- model = tf.keras.applications.MobileNetV2(input_shape=img_shape,
147
+ # Create the base model
148
+ base_model = tf.keras.applications.{{model_func}}(input_shape=img_shape,
143
149
include_top=True,
144
- weights='imagenet', classes=1000)
150
+ weights='imagenet')
151
+
145
152
153
+ # using the Keras Functional API
146
154
147
- model.trainable = True
155
+ inputs = tf.keras.Input(shape=img_shape)
156
+ x = data_augmentation(inputs)
157
+ x = tf.keras.applications.{{model_func}}.preprocess_input(x)
158
+ model = base_model(x)
148
159
149
160
model.compile(optimizer = tf.keras.optimizers.{{ optimizer }}(lr={{lr}}),
150
161
loss = "{{ loss }}",
@@ -153,12 +164,14 @@ model.compile(optimizer = tf.keras.optimizers.{{ optimizer }}(lr={{lr}}),
153
164
model.fit(train_loader,
154
165
batch_size={{batch_size}},
155
166
epochs={{num_epochs}},
156
- validation_data=val_loader
167
+ validation_data=val_loader,
157
168
{% if visualization_tool == "Tensorboard" and checkpoint %}
158
- , callbacks = [tensorboard_callback, checkpoint_dir]
169
+ callbacks = [tensorboard_callback, checkpoint_dir],
159
170
{% elif checkpoint %}
160
- , callbacks = [checkpoint_dir]
171
+ callbacks = [checkpoint_dir],
161
172
{% elif visualization_tool == "Tensorboard" %}
162
- , callbacks = [tensorboard_callback]
173
+ callbacks = [tensorboard_callback],
163
174
{% endif %}
164
175
)
176
+
177
+ model.evaluate(val_loader)
0 commit comments