Reputation: 706
i'm making a convolutional neural network using keras for images classification.
i'm making a script which will allow user to train again the model with new data. As you know, model weight initialisation influence model performance, so I want to run automaticaly the same model (same architecture and same data) with different random value for weight initilisation and save the weight of the best model
What is a bit tricky is that i'm creating a .exe file and I can't
What I need is to create only one h5 file, correspoding to model with best performance
But I have no idea how i could do that
edit: about the code, even if my curent process is totaly normal, I add it for more visualisation
model = Sequential()
model.add(Conv2D(24,kernel_size=3,padding='same',activation='relu',
input_shape=(n,n,1)))
model.add(MaxPool2D())
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(X, activation='softmax'))
model.compile(optimizer="adam", loss="categorical_crossentropy",metrics=["accuracy"])
atch_size=256
es = EarlyStopping(monitor='val_loss', mode='min', verbose=1, patience=30) # Stop trainning si pas d'améliorationaprès n = patience
mc = ModelCheckpoint(ROOT+"\\model_weight_"+name+'_'+str(n)+".h5", monitor='val_loss', mode='min', verbose=1, save_best_only=True) #Sauvegarder meilleur modèle
train = model.fit_generator(image_gen.flow(train_X, train_label, batch_size=batch_size),epochs=400,verbose=1,validation_data=(valid_X, valid_label),class_weight=class_weights,callbacks=[es,mc],steps_per_epoch=len(train_X)/batch_size)
Upvotes: 0
Views: 703
Reputation: 2430
I want to speak many things here but first, this should be what you ask for.
mc = ModelCheckpoint(ROOT+"\\model_weight_"+name+"_{epoch:02d}-{val_loss:.2f}.h5", monitor='val_loss', mode='min', verbose=1, save_best_only=True)
Where val_loss
is metrics name Keras prints in training process(like val_acc
if you put metric=['accuracy']
).
More information https://keras.io/callbacks/.
And for deleting files. I didn't check the best weights in training part mainly because the save_best_only=True
, a loop still needed to check every epoch in train.history
.
import os
min_val_loss = float('inf')
best_file = ''
# Find the best weights from file name
for weightsfile in os.listdir(ROOT):
if int(weightsfile[-7:-3]) < min_val_loss: # Since I use .2f above, I use -7:-3 here
min_val_loss = int(weightsfile[-7:-3])
best_file = weightsfile
# delete everything in ROOT except the best.
for weightsfile in os.listdir(ROOT):
if weightsfile == best_file
continue # do nothing in this loop
os.remove(os.path.join(ROOT, weightsfile)) # I recommend you to use `os.path.join` instead of `SOMTHING+"\\"+ANOTHER`. It might fuck you up later when you move to Linux machine(like Colab).
Now, for what I want to speak. Actually the weights initialization doesn't much matter in Deep CNN(where there're 10 or 100 layers). In fact, I trained the same network like ResNet50
with same setting several times on the same data and it usually only differ for 2% or 3% for the accuracy(around 0.69 to 0.72). And the weights that perform the best on another validation set wasn't the 0.72 one.
So, it's better to use the already fine-tuned weights of some good models like ResNet
, DenseNet
, NasNet
or the most recent state-of-the-art EfficientNet
and waste time on trying different setting like optimizer or learning rate schedule. And most importantly train on free GPU machine like Colab.
Upvotes: 1