def build_classifier(output_layers): classifier = AmazonKerasClassifier() classifier.add_conv_layer(img_resize) classifier.add_flatten_layer() classifier.add_ann_layer(output_layers) return classifier
#print("x_train: {}".format(x_train[1:])) #print("y_train: {}".format(y_train[1:])) #y_map #from keras.callbacks import ModelCheckpoint #filepath="weights.best.hdf5" #checkpoint = ModelCheckpoint(filepath, monitor='val_acc', verbose=1, save_best_only=True, mode='max') # ## Create the neural network definition # In[11]: classifier = AmazonKerasClassifier() classifier.add_conv_layer(img_resize) classifier.add_flatten_layer() classifier.add_ann_layer(len(y_map)) train_losses, val_losses, fbeta_score = classifier.train_model( x_train, y_train, epochs, batch_size, validation_split_size=validation_split_size) classifier.load_weight() print("Weights loaded") # ## Monitor the results # Check that we do not overfit by plotting the losses of the train and validation sets
x_train, y_train, y_map = data_helper.preprocess_train_data(train_jpeg_dir, train_csv_file, img_resize) # Free up all available memory space after this heavy operation gc.collect(); print("x_train shape: {}".format(x_train.shape)) print("y_train shape: {}".format(y_train.shape)) from tensorflow.contrib.keras.api.keras.callbacks import ModelCheckpoint filepath="weights.best.hdf5" checkpoint = ModelCheckpoint(filepath, monitor='val_acc', verbose=1, save_best_only=True) classifier = AmazonKerasClassifier() classifier.add_conv_layer(img_resize) classifier.add_flatten_layer() classifier.add_ann_layer(len(y_map)) train_losses, val_losses = [], [] epochs_arr = [20, 5, 5] learn_rates = [0.001, 0.0001, 0.00001] for learn_rate, epochs in zip(learn_rates, epochs_arr): tmp_train_losses, tmp_val_losses, fbeta_score = classifier.train_model(x_train, y_train, learn_rate, epochs, batch_size, validation_split_size=validation_split_size, train_callbacks=[checkpoint]) train_losses += tmp_train_losses val_losses += tmp_val_losses classifier.load_weights("weights.best.hdf5") print("Weights loaded") print (fbeta_score)