def make_MCpredictionBIS(self, input_img, args): im_shape = np.shape(input_img) if len(input_img.shape) != 4: input_img = np.array([input_img]) args['4_MCdrop'] = True my_dataset, new_net = instantiate_net(args, Train=False) new_net.MCDropout = True layer_dict = dict([(layer.name, layer) for layer in self.model.layers]) newlayers = [layer.name for layer in new_net.model.layers] for idx, layer_name in enumerate(newlayers): if 'Dropout' not in layer_name: if layer_name in layer_dict and len( layer_dict[layer_name].get_weights()) > 0: new_net.model.layers[idx].set_weights( layer_dict[layer_name].get_weights()) n_pred = 30 mean_pred, var_pred = np.zeros( input_img.shape, dtype='uint8'), np.zeros(input_img.shape) for i, input_im in enumerate(input_img): these_pred = np.zeros( (n_pred, input_im.shape[0], input_im.shape[1], 1 if self.Grayscale else 3), dtype='uint8') for j in range(n_pred): these_pred[j] = new_net.make_prediction(input_im) mean_pred[i] = np.mean(these_pred, axis=0) var_pred[i] = np.var(these_pred, axis=0) return mean_pred, var_pred
def call_training(this_exp): my_dataset, my_net = instantiate_net(this_exp) print( colored('Start to train the network for experiment : %s.', 'red') % (my_net.exp_name)) my_net.fit(this_exp, my_dataset) print(colored('The network is trained.', 'red')) my_net.print_learning_curves() print('curves printed') my_net.load_weights(my_net.exp_name) my_net.evaluate(my_dataset)
def evaluate_net(args): if args['4_ROC'] or args['4_ROC_pixel']: for this_exp in args['4_exp']: this_args = read_json(root_path + '/Experiments/' + this_exp + '/00_description.json') my_dataset, my_net = instantiate_net(this_args) my_net.load_weights(this_exp) my_net.evaluate(my_dataset, print_pred=args['4_ROC_printpred'], image_wise=args['4_ROC'], pixel_wise=args['4_ROC_pixel']) if args['4_MCdrop'] or args['4_MCdrop_pixel']: for this_exp in args['4_exp']: this_args = read_json(root_path + '/Experiments/' + this_exp + '/00_description.json') this_args['4_MCdrop'] = True _, my_net = instantiate_net(this_args) my_net.load_weights(this_exp) my_net.evaluate_MCDropout(this_args, print_pred=args['4_MCdrop_printpred'], image_wise=args['4_MCdrop'], pixel_wise=args['4_MCdrop_pixel'])
def evaluate_MCDropout(self, args, print_pred=True, pixel_wise = False, ROC=True, norms=['l0', 'l1', 'l2', 'linf']): args['4_MCdrop'] = True my_dataset, new_net = instantiate_net(args, Train=False) new_net.MCDropout = True # Compute the predictions used = set() subsets = [x for x in my_dataset.test_label_name if x not in used and (used.add(x) or True)] all_mask = {} for this_subset in subsets: all_mask[this_subset] = my_dataset.mask[my_dataset.test_label_name==this_subset] # Infer all the test images print('-------start Inference---------') print('xxxxxx', self.exp_name) all_pred = self.make_MCprediction(my_dataset, new_net, print_pred) print('-------end Inference---------') # Compute the ROC curve for each type of anomaly subsets.remove('clean') for this_subset in subsets: y = np.concatenate( (all_pred['clean']['variance'], all_pred[this_subset]['variance']) ) x = np.zeros(y.shape) masks = np.concatenate( (all_mask['clean'], all_mask[this_subset]) ) binary_test_labels = my_dataset.test_label binary_test_labels = np.concatenate(( my_dataset.test_label[my_dataset.test_label_name=='clean'], my_dataset.test_label[my_dataset.test_label_name==this_subset])) binary_test_labels[binary_test_labels ==2] = 1 if not pixel_wise: all_TP, all_FP = compute_ROC(x, y, binary_test_labels) result_path = root_path + '/Results_MCDropout/' + self.exp_name + '/ROC_clean_vs_' + this_subset write_json({'all_TP': all_TP, 'all_FP': all_FP}, result_path + '.json') print_ROC(result_path + '.json', result_path + '.png', AUC=True) else : print('------', this_subset) TP, FP = compute_ROC_pixel_wise(x, y, masks) result_path = root_path + '/Results_MCDropout/' + self.exp_name + '/PIXELWISE_ROC_clean_vs_' + this_subset write_json({'TP': TP, 'FP': FP}, result_path + '.json') print_PIXEL_WISE_ROC(result_path + '.json', result_path + '.png', AUC=True) # Compute combined ROC curve all_def = my_dataset.test_label_name[my_dataset.test_label ==2] used = set() subsets = [x for x in all_def if x not in used and (used.add(x) or True)] prediction = None for this_subset in subsets : if prediction is None: prediction = all_pred[this_subset]['variance'] else: prediction = np.vstack( (prediction, all_pred[this_subset]['variance'])) y = np.concatenate( ( all_pred['clean']['variance'], prediction) ) x = np.zeros(y.shape) masks = np.concatenate((my_dataset.mask[my_dataset.test_label_name=='clean'], my_dataset.mask[my_dataset.test_label==2])) binary_test_labels = np.concatenate(( my_dataset.test_label[my_dataset.test_label_name=='clean'], my_dataset.test_label[my_dataset.test_label==2])) binary_test_labels[binary_test_labels ==2] = 1 if not pixel_wise: all_TP, all_FP = compute_ROC(x, y, binary_test_labels) result_path = root_path + '/Results_MCDropout/' + self.exp_name + '/ROC_clean_vs_realDefaults' write_json({'all_TP': all_TP, 'all_FP': all_FP}, result_path + '.json') print_ROC(result_path + '.json', result_path + '.png', AUC=True) else : TP, FP = compute_ROC_pixel_wise(x, y, masks) result_path = root_path + '/Results_MCDropout/' + self.exp_name + '/PIXELWISE_ROC_clean_vs_realDefaults' write_json({'TP': TP, 'FP': FP}, result_path + '.json') print_PIXEL_WISE_ROC(result_path + '.json', result_path + '.png', AUC=True)