Computer Vision News - May 2021

18 Computer Vision Tool *The functions highlighted in green are customised and are not provided here. The first is only used to create a classification model and compile it, while the second takes as inputs ground truth labels and predicted ones and saves a ROC plot in the folder specified by save_dir. Add class weighted cost function Without changing the data, one can also work on adjusting the cost function. The first option is to make the classifier aware of the imbalanced data by incorporating the weights of the classes into the loss. This will work by giving a higher weight to the under-represented classes, so that the “network takes more attention to them”. The few lines below use the function class_weight from the scikitlearn library, which finds the right weights based on the distribution of classes. 3 , mode='min') callbacks_list = [checkpoint, reduce_lr_loss, earlyStopping] # FIT THE MODEL history_model = model.fit(inputs[train], targets[train], epochs=no_epochs, callbacks=callbacks_list, validation_data=(inputs[test], targets[test])) # Get the dictionary containing each metric and the loss for each epoch history_dict = history_model.history # convert the history.history dict to a pandas DataFrame: hist_df = pd.DataFrame(history_dict) # save to json: hist_json_file = save_dir+'history_'+str(fold_no)+'.json' with open(hist_json_file, mode='w') as f: hist_df.to_json(f) y_ints = [y.argmax() for y in targets[train]] weights = dict(zip(np.unique(y_ints), class_weight.compute_class_weight( 'balanced', np.unique(y_ints), y_ints))) print ("Class weights:", weights) history_model = model.fit(inputs[train], targets[train], class_weight=weights, epochs=no_epochs, callbacks=callbacks_list, validation_data=(inputs[test], targets[test]))

RkJQdWJsaXNoZXIy NTc3NzU=