-
Notifications
You must be signed in to change notification settings - Fork 4
/
cifar10-example.py
170 lines (142 loc) · 5.53 KB
/
cifar10-example.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# Imports
import tensorflow as tf
import tensorflow_hub as hub
import pandas as pd
import numpy as np
import imageio.v3 as iio
# from multiprocessing import Pool # , Process
from cerebros.simplecerebrosrandomsearch.simple_cerebros_random_search\
import SimpleCerebrosRandomSearch
import pendulum
from cerebros.units.units import DenseUnit
from cerebros.denseautomlstructuralcomponent.dense_automl_structural_component\
import zero_7_exp_decay, zero_95_exp_decay, simple_sigmoid
from ast import literal_eval
### Global configurables:
INPUT_SHAPES = [(32, 32, 3)] # resize from ]
RESIZE_TO = (224, 224, 3)
# Read in the data set and make it useable
ciphar10_metadata = pd.read_csv("cifar10-mini/file_metadata.csv")
ciphar10_train = ciphar10_metadata.query("data_set == 'train'")
ciphar10_test = ciphar10_metadata.query("data_set == 'test'")
def make_dataset(dataset):
images = []
labels = []
for i in np.arange(ciphar10_metadata.shape[0]):
imfile = ciphar10_metadata.loc[i]['file_name']
# Debug delete
# print(f"$$$$: attempting file: {imfile}")
img = iio.imread(imfile)
images.append(np.array(img))
labels.append(int(ciphar10_metadata.loc[i]['label']))
data_tensor = tf.constant(images)
labels_tensor = tf.constant(labels)
labels_tensor_ohe = tf.one_hot(indices=labels_tensor,
depth=10)
print(f"labels_tensor_ohe shape: {labels_tensor_ohe.shape}")
print(f"data_tensor shape: {data_tensor.shape}")
return data_tensor, labels_tensor_ohe
selected_x_train, selected_y_train_ohe =\
make_dataset(ciphar10_train)
# Cerebros configurables:
activation = 'elu'
predecessor_level_connection_affinity_factor_first = 40
predecessor_level_connection_affinity_factor_main = 65
max_consecutive_lateral_connections = 2
p_lateral_connection = 0.22299
num_lateral_connection_tries_per_unit = 1
learning_rate = 0.000129686
epochs = 7 # [1, 100]
batch_size = 27
maximum_levels = 8 # [3,7]
maximum_units_per_level = 10 # [2,10]
maximum_neurons_per_unit = 2 # [2,20]
## ### replace with this
base_new = tf.keras.applications.MobileNetV3Large(
input_shape=None,
alpha=1.0,
minimalistic=False,
include_top=True,
weights="imagenet",
input_tensor=None,
classes=1000,
pooling=None,
dropout_rate=0.2,
classifier_activation="softmax",
include_preprocessing=True,
)
for layer in base_new.layers:
layer.trainable = True
last_relevant_layer = base_new.layers[-2]
# last_relevant_layer_extracted = last_relevant_layer #.output[0][0][0]
base_embedding = tf.keras.Model(inputs=base_new.layers[0].input,
outputs=last_relevant_layer.output)
image_input_0 = tf.keras.layers.Input(shape=INPUT_SHAPES[0])
resizing = tf.keras.layers.Resizing(
height=RESIZE_TO[0],
width=RESIZE_TO[1],
interpolation='bilinear',
crop_to_aspect_ratio=False)
resized = resizing(image_input_0)
embedded = base_embedding(resized)
embedding_model = tf.keras.Model(image_input_0,
embedded)
# Final training task
TIME = pendulum.now(tz='America/New_York').__str__()[:16]\
.replace('T', '_')\
.replace(':', '_')\
.replace('-', '_')
PROJECT_NAME = f'{TIME}_cerebros_auto_ml_test'
# Cerebros parameters:
training_x = [selected_x_train]
train_labels = [selected_y_train_ohe]
OUTPUT_SHAPES = [10]
meta_trial_number = str(int(np.random.random() * 10 ** 12))
cerebros_automl = SimpleCerebrosRandomSearch(
unit_type=DenseUnit,
input_shapes=INPUT_SHAPES,
output_shapes=OUTPUT_SHAPES,
training_data=training_x,
labels=train_labels,
validation_split=0.35,
direction='maximize',
metric_to_rank_by="val_top_1_categorical_accuracy",
minimum_levels=2,
maximum_levels=maximum_levels,
minimum_units_per_level=1,
maximum_units_per_level=maximum_units_per_level,
minimum_neurons_per_unit=1,
maximum_neurons_per_unit=maximum_neurons_per_unit,
activation=activation,
final_activation='softmax',
number_of_architecture_moities_to_try=2,
number_of_tries_per_architecture_moity=1,
minimum_skip_connection_depth=1,
maximum_skip_connection_depth=7,
predecessor_level_connection_affinity_factor_first=predecessor_level_connection_affinity_factor_first,
predecessor_level_connection_affinity_factor_first_rounding_rule='ceil',
predecessor_level_connection_affinity_factor_main=predecessor_level_connection_affinity_factor_main,
predecessor_level_connection_affinity_factor_main_rounding_rule='ceil',
predecessor_level_connection_affinity_factor_decay_main=zero_7_exp_decay,
seed=8675309,
max_consecutive_lateral_connections=max_consecutive_lateral_connections,
gate_after_n_lateral_connections=3,
gate_activation_function=simple_sigmoid,
p_lateral_connection=p_lateral_connection,
p_lateral_connection_decay=zero_95_exp_decay,
num_lateral_connection_tries_per_unit=num_lateral_connection_tries_per_unit,
learning_rate=learning_rate,
loss=tf.keras.losses.CategoricalCrossentropy(),
metrics=[tf.keras.metrics.TopKCategoricalAccuracy(
k=1, name='top_1_categorical_accuracy')
],
epochs=epochs,
project_name=f"{PROJECT_NAME}_meta_{meta_trial_number}",
# use_multiprocessing_for_multiple_neural_networks=False, # pull this param
model_graphs='model_graphs',
batch_size=batch_size,
meta_trial_number=meta_trial_number,
base_models=[embedding_model])
val_top_1_categorical_accuracy =\
cerebros_automl.run_random_search()
print(val_top_1_categorical_accuracy)