def reduce_by_kMIQP(algoname, res, source_file, save_path=None): outputs = [] k = 10 pd = [] div = [] xa = [] for li in np.arange(0, 1, 0.05): lamb = li xa.append(lamb) for one_tuple in tqdm(res, ncols=77): r, M = _preprocess(one_tuple) if algoname == 'greedy': vx, max_res = greedy_kMIQP(r, M, lamb, k=k) elif algoname == 'gurobi': vx, max_res = gurobi_kMIQP(r, M, lamb, k=k) #vx, max_res = kMIQP(r, M, lamb, k=k) groundtruth, preds, scores = one_tuple preds = [preds[x] for x in vx] outputs.append((groundtruth, preds, max_res)) if save_path is None: prec, jacc = 0.0, 0.0 for groundtruth, preds, scores in outputs: preds = preds[:k] jacc += jaccard(preds) prec += precision(groundtruth, preds) pd.append(prec * 100 / len(outputs)) div.append(jacc / len(outputs)) else: with open(save_path, 'wb') as f: pickle.dump(outputs, f, pickle.HIGHEST_PROTOCOL) return xa, pd, div
def reduce_by_kMIQP(res, source_file, save_path=None): outputs = [] k = 10 pd = [] div = [] xa = [] for li in np.arange(0, 10, 0.5): lamb = li xa.append(lamb) for one_tuple in tqdm(res, ncols=77): r, M = _preprocess(one_tuple) vx, max_res = kMIQP(r, M, lamb, k=k) groundtruth, preds, scores = one_tuple preds = [preds[x] for x in vx] outputs.append((groundtruth, preds, max_res)) if save_path is None: prec, jacc = 0.0, 0.0 for groundtruth, preds, scores in outputs: jacc += jaccard(preds) prec += precision(groundtruth, preds) pd.append(prec * 100 / len(outputs)) div.append(jacc / len(outputs)) else: with open(save_path, 'wb') as f: pickle.dump(outputs, f, pickle.HIGHEST_PROTOCOL) fig = plt.figure() ax1 = fig.add_subplot(111) freqpd = [0.2599] * 20 freqdiv = [0.0444] * 20 l1, = ax1.plot(xa, freqpd, label='freq-p@' + str(k), color='darkviolet', marker='o') l2, = ax1.plot(xa, pd, label='gurobi-p@' + str(k), color='r', marker='o') #ax1.legend(loc=1) ax1.set_ylabel('p@' + str(k)) ax2 = ax1.twinx() l3, = ax2.plot(xa, div, label="gurobi-diversity", color='g', marker='*') l4, = ax2.plot(xa, freqdiv, label="freq-diversity", color='y', marker='*') #ax2.legend(loc=2) ax2.set_ylabel('diversity') ax1.set_xlabel('lamb(ele)') print(xa) print(pd) print(div) my_xticks = np.arange(0, 101, 10) plt.xticks(my_xticks) plt.legend(handles=[ l1, l2, l3, l4, ], loc='best') plt.show()
def metrics_calculator(masks, preds, mode_average=True, additional=False): batch_size, masks, predictions = mtr.standardize_for_metrics(masks, preds) accuracy_score = mtr.accuracy(batch_size, masks, predictions, mode_average) if additional: roc_auc_score = mtr.roc_auc(batch_size, masks, predictions, mode_average) jaccard_score = mtr.jaccard(batch_size, masks, predictions, mode_average) sens_score, spec_score, prec_score, f1_score = mtr.confusion_matrix( batch_size, masks, predictions, mode_average) pr_auc_score = mtr.precision_recall_auc(batch_size, masks, predictions, mode_average) iou_score = mtr.fast_hist(predictions, masks, 2) return roc_auc_score, accuracy_score, jaccard_score, sens_score, spec_score, prec_score, f1_score, pr_auc_score, iou_score return accuracy_score
def _watch_converge(res): random.shuffle(res) tqdmInput = tqdm(res, ncols=77, leave=True) prec, jacc = 0.0, 0.0 for iter, one_tuple in enumerate(tqdmInput): r, M = _preprocess(one_tuple) vx, max_res = kMIQP(r, M, lamb=1.0, k=10) groundtruth, preds, scores = one_tuple preds = [preds[x] for x in vx] prec += precision(groundtruth, preds) jacc += jaccard(preds) tqdmInput.set_description('Prec@10: %.3f%% Div: %.3f' % (prec * 100 / (iter + 1), jacc / (iter + 1)))
def main(flag, k): if flag == 'clo': source_path = '../data/bundle_clo.pkl' elif flag == 'ele': source_path = '../data/bundle_ele.pkl' else: assert False with open(source_path, 'rb') as f: train_set = pickle.load(f) test_set = pickle.load(f) cate_list = pickle.load(f) bundle_map = pickle.load(f) (user_count, item_count, cate_count, bundle_count, bundle_rank, _) = pickle.load(f) gen_groundtruth_data = pickle.load(f) freq = Counter() for t in train_set: if len(bundle_map[t[2]]) >= 2: t = bundle_map[t[2]] freq.update(subsets(t)) # for i in range(len(t)): # for j in range(i+1, len(t)): # freq.update([tuple([t[i], t[j]])]) preds = freq.most_common(k) preds = [[i for i in t[0]] for t in preds] total, jacc, prec = 0, jaccard(preds), 0.0 for uid, hist, pos in gen_groundtruth_data: groundtruth = list(bundle_map[pos]) prec += precision(groundtruth, preds) total += 1 print(flag, 'P@%d: %.4f%%\tDiv: %.4f' % (k, prec*100/total, -jacc))
def calculate_metrics(patient_id, spacing, label_arr_org, pred_arr_org, HAUSDORFF_PERCENT, OVERLAP_TOLERANCE, SURFACE_DICE_TOLERANCE): """ metric calculation cleanup in test.py. """ result = {} result["patient_id"] = patient_id # result["precision"] = precision(label_arr_org, pred_arr_org) result["recall"] = recall(label_arr_org, pred_arr_org) result["jaccard"] = jaccard(label_arr_org, pred_arr_org) result["dice"] = dice(label_arr_org, pred_arr_org) result["segmentation_score"] = segmentation_score(label_arr_org, pred_arr_org, spacing) bbox_metrics = calculate_bbox_metrics(label_arr_org, pred_arr_org, spacing) result = append_helper( result, ["x_distance", "y_distance", "z_distance", "distance"], bbox_metrics) surface_dice_metrics = surface_dice(label_arr_org, pred_arr_org, spacing, HAUSDORFF_PERCENT, OVERLAP_TOLERANCE, SURFACE_DICE_TOLERANCE) result = append_helper(result, [ "average_surface_distance_gt_to_pr", "average_surface_distance_pr_to_gt", "robust_hausdorff", "overlap_fraction_gt_with_pr", "overlap_fraction_pr_with_gt", "surface_dice" ], surface_dice_metrics) # get bbox center (indices) of prediction for next segmentation step for axes in ["X", "Y", "Z"]: for location in ["min", "center", "max", "length"]: result["prediction_{}_{}".format( axes, location )] = bbox_metrics["prediction_bbox_metrics"][axes][location] return result, result["dice"], bbox_metrics
def calculate_similarity(self, pair_key, lines): sum_xx, sum_xy, sum_yy, sum_x, sum_y, n = (0.0, 0.0, 0.0, 0.0, 0.0, 0) n_x, n_y = 0, 0 item_pair, co_ratings = pair_key, lines item_xname, item_yname = item_pair for item_x, item_y, nx_count, ny_count in lines: sum_xx += item_x * item_x sum_yy += item_y * item_y sum_xy += item_x * item_y sum_y += item_y sum_x += item_x n += 1 n_x = int(ny_count) n_y = int(nx_count) corr_sim = correlation(n, sum_xy, sum_x, sum_y, sum_xx, sum_yy) reg_corr_sim = regularized_correlation(n, sum_xy, sum_x, sum_y, sum_xx, sum_yy, PRIOR_COUNT, PRIOR_CORRELATION) cos_sim = cosine(sum_xy, sqrt(sum_xx), sqrt(sum_yy)) jaccard_sim = jaccard(n, n_x, n_y) yield (item_xname, item_yname), (corr_sim, cos_sim, reg_corr_sim, jaccard_sim, n)
def inference(dataset, segm_net, learn_step=0.005, num_iter=500, dae_dict_updates= {}, training_dict={}, data_augmentation=False, which_set='test', ae_h=False, full_im_ft=False, savepath=None, loadpath=None, test_from_0_255=False): # # Update DAE parameters # dae_dict = {'kind': 'fcn8', 'dropout': 0.0, 'skip': True, 'unpool_type':'standard', 'n_filters': 64, 'conv_before_pool': 1, 'additional_pool': 0, 'concat_h': ['input'], 'noise': 0.0, 'from_gt': True, 'temperature': 1.0, 'layer': 'probs_dimshuffle', 'exp_name': '', 'bn': 0} dae_dict.update(dae_dict_updates) # # Prepare load/save directories # exp_name = build_experiment_name(segm_net, data_aug=data_augmentation, ae_h=ae_h, **dict(dae_dict.items() + training_dict.items())) # exp_name += '_ftsmall' if full_im_ft else '' if savepath is None: raise ValueError('A saving directory must be specified') savepath = os.path.join(savepath, dataset, exp_name, 'img_plots', which_set) loadpath = os.path.join(loadpath, dataset, exp_name) if not os.path.exists(savepath): os.makedirs(savepath) else: print('\033[93m The following folder already exists {}. ' 'It will be overwritten in a few seconds...\033[0m'.format( savepath)) print('Saving directory : ' + savepath) with open(os.path.join(savepath, "config.txt"), "w") as f: for key, value in locals().items(): f.write('{} = {}\n'.format(key, value)) # # Define symbolic variables # input_x_var = T.tensor4('input_x_var') # tensor for input image batch input_concat_h_vars = [T.tensor4()] * len(dae_dict['concat_h']) # tensor for hidden repr batch (input dae) y_hat_var = T.tensor4('pred_y_var') target_var = T.tensor4('target_var') # tensor for target batch # # Build dataset iterator # data_iter = load_data(dataset, {}, one_hot=True, batch_size=[10, 5, 10], return_0_255=test_from_0_255, which_set=which_set) colors = data_iter.cmap n_batches_test = data_iter.nbatches n_classes = data_iter.non_void_nclasses void_labels = data_iter.void_labels nb_in_channels = data_iter.data_shape[0] void = n_classes if any(void_labels) else n_classes+1 # # Build networks # # Build segmentation network print 'Building segmentation network' if segm_net == 'fcn8': fcn = buildFCN8(nb_in_channels, input_var=input_x_var, n_classes=n_classes, void_labels=void_labels, path_weights=WEIGHTS_PATH+dataset+'/fcn8_model.npz', trainable=False, load_weights=True, layer=dae_dict['concat_h']+[dae_dict['layer']]) padding = 100 elif segm_net == 'densenet': fcn = build_fcdensenet(input_x_var, nb_in_channels=nb_in_channels, n_classes=n_classes, layer=dae_dict['concat_h']) padding = 0 elif segm_net == 'fcn_fcresnet': raise NotImplementedError else: raise ValueError # Build DAE with pre-trained weights print 'Building DAE network' if dae_dict['kind'] == 'standard': nb_features_to_concat=fcn[0].output_shape[1] dae = buildDAE(input_concat_h_vars, y_hat_var, n_classes, nb_features_to_concat=nb_features_to_concat, padding=padding, trainable=True, void_labels=void_labels, load_weights=True, path_weights=loadpath, model_name='dae_model_best.npz', out_nonlin=softmax, concat_h=dae_dict['concat_h'], noise=dae_dict['noise'], n_filters=dae_dict['n_filters'], conv_before_pool=dae_dict['conv_before_pool'], additional_pool=dae_dict['additional_pool'], dropout=dae_dict['dropout'], skip=dae_dict['skip'], unpool_type=dae_dict['unpool_type'], bn=dae_dict['bn']) elif dae_dict['kind'] == 'fcn8': dae = buildFCN8_DAE(input_concat_h_vars, y_hat_var, n_classes, nb_in_channels=n_classes, path_weights=loadpath, model_name='dae_model_best.npz', trainable=True, load_weights=True, pretrained=True, pascal=False, concat_h=dae_dict['concat_h'], noise=dae_dict['noise']) elif dae_dict['kind'] == 'contextmod': dae = buildDAE_contextmod(input_concat_h_vars, y_hat_var, n_classes, path_weights=loadpath, model_name='dae_model_best.npz', trainable=True, load_weights=True, out_nonlin=softmax, noise=dae_dict['noise'], concat_h=dae_dict['concat_h']) else: raise ValueError('Unknown dae kind') # # Define and compile theano functions # print "Defining and compiling theano functions" # predictions and theano functions pred_fcn = lasagne.layers.get_output(fcn, deterministic=True, batch_norm_use_averages=False) pred_fcn_fn = theano.function([input_x_var], pred_fcn) pred_dae = lasagne.layers.get_output(dae, deterministic=True) pred_dae_fn = theano.function(input_concat_h_vars+[y_hat_var], pred_dae) # Reshape iterative inference output to b01,c y_hat_dimshuffle = y_hat_var.dimshuffle((0, 2, 3, 1)) sh = y_hat_dimshuffle.shape y_hat_2D = y_hat_dimshuffle.reshape((T.prod(sh[:3]), sh[3])) # Reshape iterative inference output to b01,c target_var_dimshuffle = target_var.dimshuffle((0, 2, 3, 1)) sh2 = target_var_dimshuffle.shape target_var_2D = target_var_dimshuffle.reshape((T.prod(sh2[:3]), sh2[3])) # derivative of energy wrt input and theano function de = - (pred_dae - y_hat_var) de_fn = theano.function(input_concat_h_vars+[y_hat_var], de) # metrics and theano functions test_loss = squared_error(y_hat_var, target_var, void) test_acc = accuracy(y_hat_2D, target_var_2D, void_labels, one_hot=True) test_jacc = jaccard(y_hat_2D, target_var_2D, n_classes, one_hot=True) val_fn = theano.function([y_hat_var, target_var], [test_acc, test_jacc, test_loss]) # # Infer # print 'Start infering' rec_tot = 0 rec_tot_fcn = 0 rec_tot_dae = 0 acc_tot = 0 acc_tot_fcn = 0 jacc_tot = 0 jacc_tot_fcn = 0 acc_tot_dae = 0 jacc_tot_dae = 0 print 'Inference step: '+str(learn_step)+ 'num iter '+str(num_iter) for i in range(n_batches_test): info_str = "Batch %d out of %d" % (i+1, n_batches_test) print '-'*30 print '*'*5 + info_str + '*'*5 print '-'*30 # Get minibatch X_test_batch, L_test_batch = data_iter.next() L_test_batch = L_test_batch.astype(_FLOATX) # Compute fcn prediction y and h pred_test_batch = pred_fcn_fn(X_test_batch) Y_test_batch = pred_test_batch[-1] H_test_batch = pred_test_batch[:-1] # Compute metrics before iterative inference acc_fcn, jacc_fcn, rec_fcn = val_fn(Y_test_batch, L_test_batch) acc_tot_fcn += acc_fcn jacc_tot_fcn += jacc_fcn rec_tot_fcn += rec_fcn Y_test_batch_fcn = Y_test_batch print_results('>>>>> FCN:', rec_tot_fcn, acc_tot_fcn, jacc_tot_fcn, i+1) # Compute dae output and metrics after dae Y_test_batch_dae = pred_dae_fn(*(H_test_batch+[Y_test_batch])) acc_dae, jacc_dae, rec_dae = val_fn(Y_test_batch_dae, L_test_batch) acc_tot_dae += acc_dae jacc_tot_dae += jacc_dae rec_tot_dae += rec_dae print_results('>>>>> FCN+DAE:', rec_tot_dae, acc_tot_dae, jacc_tot_dae, i+1) Y_test_batch_ii = [] for im in range(X_test_batch.shape[0]): print('-----------------------') h_im = [el[np.newaxis, im] for el in H_test_batch] y_im = Y_test_batch[np.newaxis, im] t_im = L_test_batch[np.newaxis, im] # Iterative inference for it in range(num_iter): # Compute gradient grad = de_fn(*(h_im+[y_im])) # Update prediction y_im = y_im - learn_step * grad # Clip prediction y_im = np.clip(y_im, 0.0, 1.0) norm = np.linalg.norm(grad, axis=1).mean() if norm < _EPSILON: break acc_iter, jacc_iter, rec_iter = val_fn(y_im, t_im) print rec_iter, acc_iter, np.nanmean(jacc_iter[0, :]/jacc_iter[1, :]) Y_test_batch_ii += [y_im] Y_test_batch_ii = np.concatenate(Y_test_batch_ii, axis=0) # Compute metrics acc, jacc, rec = val_fn(Y_test_batch_ii, L_test_batch) acc_tot += acc jacc_tot += jacc rec_tot += rec print_results('>>>>> ITERATIVE INFERENCE:', rec_tot, acc_tot, jacc_tot, i+1) np.savez(savepath+'batch'+str(i)+'.npz', X=X_test_batch, L=L_test_batch, Y_ii=Y_test_batch_ii, Y_fcn=Y_test_batch_fcn) # Save images # save_img(X_test_batch, # L_test_batch, # Y_test_batch_ii, # Y_test_batch_fcn, # savepath, 'batch' + str(i), # void_labels, colors) # Print summary of how things went print('-------------------------------------------------------------------') print('------------------------------SUMMARY------------------------------') print('-------------------------------------------------------------------') print_results('>>>>> FCN:', rec_tot_fcn, acc_tot_fcn, jacc_tot_fcn, i+1) print_results('>>>>> FCN+DAE:', rec_tot_dae, acc_tot_dae, jacc_tot_dae, i+1) print_results('>>>>> ITERATIVE INFERENCE:', rec_tot, acc_tot, jacc_tot, i+1) # Compute per class jaccard jacc_perclass_fcn = jacc_tot_fcn[0, :]/jacc_tot_fcn[1, :] jacc_perclass = jacc_tot[0, :]/jacc_tot[1, :] print ">>>>> Per class jaccard:" labs = data_iter.mask_labels for i in range(len(labs)-len(void_labels)): class_str = ' ' + labs[i] + ' : fcn -> %f, ii -> %f' class_str = class_str % (jacc_perclass_fcn[i], jacc_perclass[i]) print class_str # Move segmentations if savepath != loadpath: print('Copying images to {}'.format(loadpath)) copy_tree(savepath, os.path.join(loadpath, 'img_plots', which_set))
def inference(dataset, segm_net, which_set='val', num_iter=5, Bilateral=True, savepath=None, loadpath=None, test_from_0_255=False): # # Define symbolic variables # input_x_var = T.tensor4('input_x_var') y_hat_var = T.tensor4('pred_y_var') target_var = T.tensor4('target_var') # # Build dataset iterator # data_iter = load_data(dataset, {}, one_hot=True, batch_size=[10, 10, 10], return_0_255=test_from_0_255, which_set=which_set) colors = data_iter.cmap n_batches_test = data_iter.nbatches n_classes = data_iter.non_void_nclasses void_labels = data_iter.void_labels # # Prepare saving directory # savepath = os.path.join(savepath, dataset, segm_net, 'img_plots', 'crf', str(num_iter), which_set) loadpath = os.path.join(loadpath, dataset, segm_net, 'img_plots', 'crf', str(num_iter), which_set) if not os.path.exists(savepath): os.makedirs(savepath) # # Build network # print 'Building segmentation network' if segm_net == 'fcn8': fcn = buildFCN8(3, input_var=input_x_var, n_classes=n_classes, void_labels=void_labels, path_weights=WEIGHTS_PATH + dataset + '/fcn8_model.npz', trainable=False, load_weights=True, layer=['probs_dimshuffle']) padding = 100 elif segm_net == 'densenet': fcn = build_fcdensenet(input_x_var, nb_in_channels=3, n_classes=n_classes, layer=[]) padding = 0 elif segm_net == 'fcn_fcresnet': raise NotImplementedError else: raise ValueError # # Define and compile theano functions # print "Defining and compiling theano functions" # predictions of fcn pred_fcn = lasagne.layers.get_output(fcn, deterministic=True, batch_norm_use_averages=False)[0] # function to compute output of fcn pred_fcn_fn = theano.function([input_x_var], pred_fcn) # reshape fcn output to b,01c y_hat_dimshuffle = y_hat_var.dimshuffle((0, 2, 3, 1)) sh = y_hat_dimshuffle.shape y_hat_2D = y_hat_dimshuffle.reshape((T.prod(sh[:3]), sh[3])) # reshape target to b01,c target_var_dimshuffle = target_var.dimshuffle((0, 2, 3, 1)) sh2 = target_var_dimshuffle.shape target_var_2D = target_var_dimshuffle.reshape((T.prod(sh2[:3]), sh2[3])) # metrics to evaluate iterative inference test_acc = accuracy(y_hat_2D, target_var_2D, void_labels, one_hot=True) test_jacc = jaccard(y_hat_2D, target_var_2D, n_classes, one_hot=True) # functions to compute metrics val_fn = theano.function([y_hat_var, target_var], [test_acc, test_jacc]) # # Infer # print 'Start infering' acc_tot_crf = 0 acc_tot_fcn = 0 jacc_tot_crf = 0 jacc_tot_fcn = 0 for i in range(n_batches_test): info_str = "Batch %d out of %d" % (i + 1, n_batches_test) print info_str # Get minibatch X_test_batch, L_test_batch = data_iter.next() L_test_batch = L_test_batch.astype(_FLOATX) # Compute fcn prediction Y_test_batch = pred_fcn_fn(X_test_batch) # Compute metrics before CRF acc_fcn, jacc_fcn = val_fn(Y_test_batch, L_test_batch) acc_tot_fcn += acc_fcn jacc_tot_fcn += jacc_fcn Y_test_batch_fcn = Y_test_batch Y_test_batch_crf = [] for im in range(X_test_batch.shape[0]): # CRF d = dcrf.DenseCRF2D(Y_test_batch.shape[3], Y_test_batch.shape[2], n_classes) sm = Y_test_batch[im, 0:n_classes, :, :] sm = sm.reshape((n_classes, -1)) img = X_test_batch[im] img = np.transpose(img, (1, 2, 0)) img = (255 * img).astype('uint8') img2 = np.asarray(img, order='C') # set unary potentials (neg log probability) U = unary_from_softmax(sm) d.setUnaryEnergy(U) # set pairwise potentials # This adds the color-independent term, features are the # locations only. Smoothness kernel. # sxy: gaussian x, y std # compat: ways to weight contributions, a number for potts compatibility, # vector for diagonal compatibility, an array for matrix compatibility # kernel: kernel used, CONST_KERNEL, FULL_KERNEL, DIAG_KERNEL # normalization: NORMALIZE_AFTER, NORMALIZE_BEFORE, # NO_NORMALIZAITION, NORMALIZE_SYMMETRIC d.addPairwiseGaussian(sxy=(3, 3), compat=3, kernel=dcrf.DIAG_KERNEL, normalization=dcrf.NORMALIZE_SYMMETRIC) if Bilateral: # Appearance kernel. This adds the color-dependent term, i.e. features # are (x,y,r,g,b). # im is an image-array, e.g. im.dtype == np.uint8 and im.shape == (640,480,3) # to set sxy and srgb perform grid search on validation set d.addPairwiseBilateral(sxy=(3, 3), srgb=(13, 13, 13), rgbim=img2, compat=10, kernel=dcrf.DIAG_KERNEL, normalization=dcrf.NORMALIZE_SYMMETRIC) # inference Q = d.inference(num_iter) Q = np.reshape( Q, (n_classes, Y_test_batch.shape[2], Y_test_batch.shape[3])) Y_test_batch_crf += [np.expand_dims(Q, axis=0)] # Save images Y_test_batch = np.concatenate(Y_test_batch_crf, axis=0) # Compute metrics after CRF acc_crf, jacc_crf = val_fn(Y_test_batch, L_test_batch) acc_tot_crf += acc_crf jacc_tot_crf += jacc_crf # save_img(X_test_batch.astype(_FLOATX), L_test_batch, Y_test_batch, # Y_test_batch_fcn, savepath, 'batch' + str(i), void_labels, colors) np.savez(savepath + 'batch' + str(i) + '.npz', X=X_test_batch, L=L_test_batch, Y_crf=Y_test_batch, Y_fcn=Y_test_batch_fcn) acc_test_crf = acc_tot_crf / (n_batches_test) jacc_test_perclass_crf = jacc_tot_crf[0, :] / jacc_tot_crf[1, :] jacc_test_crf = np.nanmean(jacc_test_perclass_crf) acc_test_fcn = acc_tot_fcn / n_batches_test jacc_test_perclass_fcn = jacc_tot_fcn[0, :] / jacc_tot_fcn[1, :] jacc_test_fcn = np.nanmean(jacc_test_perclass_fcn) out_str = "TEST: acc crf %f, jacc crf %f, acc fcn %f, jacc fcn %f" out_str = out_str % (acc_test_crf, jacc_test_crf, acc_test_fcn, jacc_test_fcn) print ">>>>> Per class jaccard:" labs = data_iter.mask_labels for i in range(len(labs) - len(void_labels)): class_str = ' ' + labs[i] + ' : fcn -> %f, crf -> %f' class_str = class_str % (jacc_test_perclass_fcn[i], jacc_test_perclass_crf[i]) print class_str print out_str # Move segmentations if savepath != loadpath: print('Copying images to {}'.format(loadpath)) copy_tree(savepath, loadpath) return jacc_test_perclass_crf
def train(dataset, segm_net, learning_rate=0.005, lr_anneal=1.0, weight_decay=1e-4, num_epochs=500, max_patience=100, optimizer='rmsprop', training_loss=['squared_error'], batch_size=[10, 1, 1], ae_h=False, dae_dict_updates={}, data_augmentation={}, savepath=None, loadpath=None, resume=False, train_from_0_255=False, lmb=1, full_im_ft=False): # # Update DAE parameters # dae_dict = {'kind': 'fcn8', 'dropout': 0.0, 'skip': True, 'unpool_type': 'standard', 'n_filters': 64, 'conv_before_pool': 1, 'additional_pool': 0, 'concat_h': ['input'], 'noise': 0.0, 'from_gt': True, 'temperature': 1.0, 'path_weights': '', 'layer': 'probs_dimshuffle', 'exp_name': '', 'bn': 0} dae_dict.update(dae_dict_updates) # # Prepare load/save directories # exp_name = build_experiment_name(segm_net, training_loss=training_loss, data_aug=bool(data_augmentation), learning_rate=learning_rate, lr_anneal=lr_anneal, weight_decay=weight_decay, optimizer=optimizer, ae_h=ae_h, **dae_dict) if savepath is None: raise ValueError('A saving directory must be specified') loadpath_init = os.path.join(loadpath, dataset, exp_name) exp_name += '_ft' if full_im_ft else '' loadpath = os.path.join(loadpath, dataset, exp_name) savepath = os.path.join(savepath, dataset, exp_name) if not os.path.exists(savepath): os.makedirs(savepath) else: print('\033[93m The following folder already exists {}. ' 'It will be overwritten in a few seconds...\033[0m'.format( savepath)) print('Saving directory : ' + savepath) with open(os.path.join(savepath, "config.txt"), "w") as f: for key, value in locals().items(): f.write('{} = {}\n'.format(key, value)) # # Define symbolic variables # input_x_var = T.tensor4('input_x_var') # tensor for input image batch input_mask_var = T.tensor4('input_mask_var') # tensor for segmentation bach (input dae) input_concat_h_vars = [T.tensor4()] * len(dae_dict['concat_h']) # tensor for hidden repr batch (input dae) target_var = T.tensor4('target_var') # tensor for target batch # learning_rate = learning_rate*0.1 if full_im_ft else learning_rate # learning_rate = 0.01 print learning_rate lr = theano.shared(np.float32(learning_rate), 'learning_rate') # # Build dataset iterator # train_iter, val_iter, _ = load_data(dataset, data_augmentation, one_hot=True, batch_size=batch_size, return_0_255=train_from_0_255, ) n_batches_train = train_iter.nbatches n_batches_val = val_iter.nbatches n_classes = train_iter.non_void_nclasses void_labels = train_iter.void_labels nb_in_channels = train_iter.data_shape[0] void = n_classes if any(void_labels) else n_classes+1 # # Build networks # # Check that model and dataset get along print 'Checking options' assert (segm_net == 'fcn8' and dataset == 'camvid') or \ (segm_net == 'densenet' and dataset == 'camvid') assert (data_augmentation['crop_size'] == None and full_im_ft) or not full_im_ft # Build segmentation network print 'Building segmentation network' if segm_net == 'fcn8': layer_out = copy.copy(dae_dict['concat_h']) layer_out += [copy.copy(dae_dict['layer'])] if not dae_dict['from_gt'] else [] fcn = buildFCN8(nb_in_channels, input_x_var, n_classes=n_classes, void_labels=void_labels, path_weights=WEIGHTS_PATH+dataset+'/fcn8_model.npz', load_weights=True, layer=layer_out) padding = 100 elif segm_net == 'densenet': fcn = build_fcdensenet(input_x_var, nb_in_channels=nb_in_channels, n_classes=n_classes, layer=dae_dict['concat_h'], from_gt=dae_dict['from_gt']) padding = 0 elif segm_net == 'fcn_fcresnet': raise NotImplementedError else: raise ValueError # Build DAE network print 'Building DAE network' if ae_h and dae_dict['kind'] != 'standard': raise ValueError('Plug&Play not implemented for ' + dae_dict['kind']) if ae_h and 'pool' not in dae_dict['concat_h'][-1]: raise ValueError('Plug&Play version needs concat_h to be different than input') ae_h = ae_h and 'pool' in dae_dict['concat_h'][-1] if dae_dict['kind'] == 'standard': nb_features_to_concat=fcn[0].output_shape[1] dae = buildDAE(input_concat_h_vars, input_mask_var, n_classes, nb_features_to_concat=nb_features_to_concat, padding=padding, trainable=True, void_labels=void_labels, load_weights=resume or full_im_ft, path_weights=loadpath_init, model_name='dae_model_best.npz', out_nonlin=softmax, concat_h=dae_dict['concat_h'], noise=dae_dict['noise'], n_filters=dae_dict['n_filters'], conv_before_pool=dae_dict['conv_before_pool'], additional_pool=dae_dict['additional_pool'], dropout=dae_dict['dropout'], skip=dae_dict['skip'], unpool_type=dae_dict['unpool_type'], bn=dae_dict['bn'], ae_h=ae_h) elif dae_dict['kind'] == 'fcn8': dae = buildFCN8_DAE(input_concat_h_vars, input_mask_var, n_classes, nb_in_channels=n_classes, trainable=True, load_weights=resume, pretrained=True, pascal=True, concat_h=dae_dict['concat_h'], noise=dae_dict['noise'], dropout=dae_dict['dropout'], path_weights=os.path.join('/'.join(loadpath_init.split('/')[:-1]), dae_dict['path_weights']), model_name='dae_model_best.npz') elif dae_dict['kind'] == 'contextmod': dae = buildDAE_contextmod(input_concat_h_vars, input_mask_var, n_classes, path_weights=loadpath_init, model_name='dae_model.npz', trainable=True, load_weights=resume, out_nonlin=softmax, noise=dae_dict['noise'], concat_h=dae_dict['concat_h']) else: raise ValueError('Unknown dae kind') # # Define and compile theano functions # # training functions print "Defining and compiling training functions" # fcn prediction fcn_prediction = lasagne.layers.get_output(fcn, deterministic=True, batch_norm_use_averages=False) # select prediction layers (pooling and upsampling layers) dae_all_lays = lasagne.layers.get_all_layers(dae) if dae_dict['kind'] != 'contextmod': dae_lays = [l for l in dae_all_lays if isinstance(l, Pool2DLayer) or isinstance(l, CroppingLayer) or isinstance(l, ElemwiseSumLayer) or l == dae_all_lays[-1]] # dae_lays = dae_lays[::2] else: dae_lays = [l for l in dae_all_lays if isinstance(l, DilatedConv2DLayer) or l == dae_all_lays[-1]] if ae_h: h_ae_idx = [i for i, el in enumerate(dae_lays) if el.name == 'h_to_recon'][0] h_hat_idx = [i for i, el in enumerate(dae_lays) if el.name == 'h_hat'][0] # predictions dae_prediction_all = lasagne.layers.get_output(dae_lays, batch_norm_use_averages=False) dae_prediction = dae_prediction_all[-1] dae_prediction_h = dae_prediction_all[:-1] test_dae_prediction_all = lasagne.layers.get_output(dae_lays, deterministic=True, batch_norm_use_averages=False) test_dae_prediction = test_dae_prediction_all[-1] test_dae_prediction_h = test_dae_prediction_all[:-1] # fetch h and h_hat if needed if ae_h: h = dae_prediction_all[h_ae_idx] h_hat = dae_prediction_all[h_hat_idx] h_test = test_dae_prediction_all[h_ae_idx] h_hat_test = test_dae_prediction_all[h_hat_idx] # loss loss = 0 test_loss = 0 # Convert DAE prediction to 2D dae_prediction_2D = dae_prediction.dimshuffle((0, 2, 3, 1)) sh = dae_prediction_2D.shape dae_prediction_2D = dae_prediction_2D.reshape((T.prod(sh[:3]), sh[3])) test_dae_prediction_2D = test_dae_prediction.dimshuffle((0, 2, 3, 1)) sh = test_dae_prediction_2D.shape test_dae_prediction_2D = test_dae_prediction_2D.reshape((T.prod(sh[:3]), sh[3])) # Convert target to 2D target_var_2D = target_var.dimshuffle((0, 2, 3, 1)) sh = target_var_2D.shape target_var_2D = target_var_2D.reshape((T.prod(sh[:3]), sh[3])) if 'crossentropy' in training_loss: # Compute loss loss += crossentropy(dae_prediction_2D, target_var_2D, void_labels, one_hot=True) test_loss += crossentropy(test_dae_prediction_2D, target_var_2D, void_labels, one_hot=True) if 'dice' in training_loss: loss += dice_loss(dae_prediction, target_var, void_labels) test_loss += dice_loss(test_dae_prediction, target_var, void_labels) test_mse_loss = squared_error(test_dae_prediction, target_var, void) if 'squared_error' in training_loss: mse_loss = squared_error(dae_prediction, target_var, void) loss += lmb*mse_loss test_loss += lmb*test_mse_loss # Add intermediate losses if 'squared_error_h' in training_loss: # extract input layers and create dictionary dae_input_lays = [l for l in dae_all_lays if isinstance(l, InputLayer)] inputs = {dae_input_lays[0]: target_var[:, :void, :, :], dae_input_lays[-1]:target_var[:, :void, :, :]} for idx, val in enumerate(input_concat_h_vars): inputs[dae_input_lays[idx+1]] = val test_dae_prediction_all_gt = lasagne.layers.get_output(dae_lays, inputs=inputs, deterministic=True, batch_norm_use_averages=False) test_dae_prediction_h_gt = test_dae_prediction_all_gt[:-1] loss += squared_error_h(dae_prediction_h, test_dae_prediction_h_gt) test_loss += squared_error_h(test_dae_prediction_h, test_dae_prediction_h_gt) # compute jaccard jacc = jaccard(dae_prediction_2D, target_var_2D, n_classes, one_hot=True) test_jacc = jaccard(test_dae_prediction_2D, target_var_2D, n_classes, one_hot=True) # if reconstructing h add the corresponding loss terms if ae_h: loss += squared_error_L(h, h_hat).mean() test_loss += squared_error_L(h_test, h_hat_test).mean() # network parameters params = lasagne.layers.get_all_params(dae, trainable=True) # optimizer if optimizer == 'rmsprop': updates = lasagne.updates.rmsprop(loss, params, learning_rate=lr) elif optimizer == 'adam': updates = lasagne.updates.adam(loss, params, learning_rate=lr) else: raise ValueError('Unknown optimizer') # functions train_fn = theano.function(input_concat_h_vars + [input_mask_var, target_var], loss, updates=updates) fcn_fn = theano.function([input_x_var], fcn_prediction) val_fn = theano.function(input_concat_h_vars + [input_mask_var, target_var], [test_loss, test_jacc, test_mse_loss]) err_train = [] err_valid = [] jacc_val_arr = [] mse_val_arr = [] patience = 0 # # Train # # Training main loop print "Start training" for epoch in range(num_epochs): # Single epoch training and validation start_time = time.time() cost_train_tot = 0 # Train for i in range(n_batches_train): # Get minibatch X_train_batch, L_train_batch = train_iter.next() L_train_batch = L_train_batch.astype(_FLOATX) #####uncomment if you want to control the feasability of pooling#### # max_n_possible_pool = np.floor(np.log2(np.array(X_train_batch.shape[2:]).min())) # # check if we don't ask for more poolings than possible # assert n_pool+additional_pool < max_n_possible_pool #################################################################### # h prediction H_pred_batch = fcn_fn(X_train_batch) if dae_dict['from_gt']: Y_pred_batch = L_train_batch[:, :void, :, :] else: Y_pred_batch = H_pred_batch[-1] H_pred_batch = H_pred_batch[:-1] # Training step cost_train = train_fn(*(H_pred_batch + [Y_pred_batch, L_train_batch])) cost_train_tot += cost_train err_train += [cost_train_tot / n_batches_train] # Validation cost_val_tot = 0 jacc_val_tot = 0 mse_val_tot = 0 for i in range(n_batches_val): # Get minibatch X_val_batch, L_val_batch = val_iter.next() L_val_batch = L_val_batch.astype(_FLOATX) # h prediction H_pred_batch = fcn_fn(X_val_batch) if dae_dict['from_gt']: Y_pred_batch = L_val_batch[:, :void, :, :] else: Y_pred_batch = H_pred_batch[-1] H_pred_batch = H_pred_batch[:-1] # Validation step cost_val, jacc_val, mse_val = val_fn(*(H_pred_batch + [Y_pred_batch, L_val_batch])) cost_val_tot += cost_val jacc_val_tot += jacc_val mse_val_tot += mse_val err_valid += [cost_val_tot / n_batches_val] jacc_val_arr += [np.mean(jacc_val_tot[0, :] / jacc_val_tot[1, :])] mse_val_arr += [mse_val_tot / n_batches_val] out_str = "EPOCH %i: Avg epoch training cost train %f, cost val %f," + \ " jacc val %f, mse val % f took %f s" out_str = out_str % (epoch, err_train[epoch], err_valid[epoch], jacc_val_arr[epoch], mse_val_arr[epoch], time.time() - start_time) print out_str with open(os.path.join(savepath, "output.log"), "a") as f: f.write(out_str + "\n") # update learning rate lr.set_value(float(lr.get_value() * lr_anneal)) # Early stopping and saving stuff if epoch == 0: best_err_val = err_valid[epoch] best_jacc_val = jacc_val_arr[epoch] best_mse_val = mse_val_arr[epoch] elif epoch > 0 and err_valid[epoch] < best_err_val: best_err_val = err_valid[epoch] best_jacc_val = jacc_val_arr[epoch] best_mse_val = mse_val_arr[epoch] patience = 0 np.savez(os.path.join(savepath, 'dae_model_best.npz'), *lasagne.layers.get_all_param_values(dae)) np.savez(os.path.join(savepath, 'dae_errors_best.npz'), err_train, err_valid, jacc_val_arr, mse_val_arr) else: patience += 1 np.savez(os.path.join(savepath, 'dae_model_last.npz'), *lasagne.layers.get_all_param_values(dae)) np.savez(os.path.join(savepath, 'dae_errors_last.npz'), err_train, err_valid, jacc_val_arr, mse_val_arr) # Finish training if patience has expired or max nber of epochs # reached if patience == max_patience or epoch == num_epochs - 1: # Copy files to loadpath if savepath != loadpath: print('Copying model and other training files to {}'.format( loadpath)) copy_tree(savepath, loadpath) # End print(' Training Done !') return
nlayers = len(lasagne.layers.get_all_params(simple_net_output)) lasagne.layers.set_all_param_values(simple_net_output, param_values[:nlayers]) print 'Done assigning weights' # In[33]: print "Defining and compiling test functions" test_prediction = lasagne.layers.get_output(simple_net_output[0], deterministic=True) test_loss = categorical_crossentropy(test_prediction, target_var) test_loss = test_loss.mean() test_acc, test_acc_per_sample = accuracy(test_prediction, target_var, void_labels) test_jacc = jaccard(test_prediction, target_var, n_classes) test_fn = theano.function( [input_var, target_var], [test_loss, test_acc, test_jacc, test_acc_per_sample]) print "Done" # In[34]: #Function computing the prediction with current parameters (for visualization) pred = theano.function([input_var], lasagne.layers.get_output(net['probs_reshape'], deterministic=True)) # In[35]:
def train(learn_step=0.001, weight_decay=1e-4, num_epochs=500, max_patience=100, data_augmentation={}, savepath=None, loadpath=None, batch_size=None, resume=False, conv_before_pool=[1], n_conv_bottom=1, merge_type='sum', n_filters=64, filter_size=3, pool_size=2, dropout=0.5, shuffle_at_each_epoch=True, minibatches_subset=0): # # Prepare load/save directories # exp_name = 'fcn1D' exp_name += '_dataugm' if bool(data_augmentation) else '' exp_name += '_' + str(conv_before_pool) exp_name += '_' + str(n_filters) + 'filt' exp_name += '_' + 'batchs=' + (str(batch_size) if batch_size is not None else 'none') exp_name += '_' + 'botconv=' + str(n_conv_bottom) exp_name += '_' + 'lrate=' + str(learn_step) exp_name += '_' + 'pat=' + str(max_patience) exp_name += '_' + 'merge=' + merge_type exp_name += '_' + 'fsize=' + str(filter_size) exp_name += '_' + 'psize=' + str(pool_size) exp_name += ('_noshuffle' + str(minibatches_subset) + 'batch') if not shuffle_at_each_epoch else '' if savepath is None: raise ValueError('A saving directory must be specified') dataset = 'cortical_layers' savepath = os.path.join(savepath, dataset, exp_name) loadpath = os.path.join(loadpath, dataset, exp_name) print 'Savepath : ' + str(savepath) print 'Loadpath : ' + str(loadpath) if not os.path.exists(savepath): os.makedirs(savepath) else: print( '\033[93m The following folder already exists {}. ' 'It will be overwritten in a few seconds...\033[0m'.format( savepath)) print('Saving directory : ' + savepath) with open(os.path.join(savepath, "config.txt"), "w") as f: for key, value in locals().items(): f.write('{} = {}\n'.format(key, value)) # # Define symbolic variables # input_var = T.tensor3('input_var') #n_example*nb_in_channels*ray_size target_var = T.ivector('target_var') #n_example*ray_size # # Build dataset iterator # train_iter = CorticalLayersDataset( which_set='train', batch_size=batch_size[0], data_augm_kwargs=data_augmentation, shuffle_at_each_epoch=shuffle_at_each_epoch, return_one_hot=False, return_01c=False, return_list=True, use_threads=True) val_iter = CorticalLayersDataset( which_set='valid', batch_size=batch_size[1], shuffle_at_each_epoch=shuffle_at_each_epoch, return_one_hot=False, return_01c=False, return_list=True, use_threads=True) #Test set not configured yet (additional data?) test_iter = None n_batches_train = train_iter.nbatches n_batches_val = val_iter.nbatches n_batches_test = test_iter.nbatches if test_iter is not None else 0 n_classes = train_iter.non_void_nclasses void_labels = train_iter.void_labels nb_in_channels = train_iter.data_shape[0] print('------------------------------') print('Learning rate ' + str(learn_step)) print('Max patience ' + str(max_patience)) print('Batch size ' + str(batch_size)) print('Shuffle ? ' + str(shuffle_at_each_epoch) + ', Minibatch subset? ' + str(minibatches_subset)) print "Batch. train: %d, val %d, test %d" % (n_batches_train, n_batches_val, n_batches_test) print "Nb of classes: %d" % (n_classes) print "Nb. of input channels: %d" % (nb_in_channels) # # Build network # convmodel = buildFCN_1D(input_var, n_classes=n_classes, n_in_channels=nb_in_channels, conv_before_pool=conv_before_pool, n_conv_bottom=n_conv_bottom, merge_type=merge_type, n_filters=n_filters, filter_size=filter_size, pool_size=pool_size, dropout=dropout, layer=['probs']) # To print each layer, uncomment this: lays = lasagne.layers.get_all_layers(convmodel) for l in lays: print l, l.output_shape #import pdb; pdb.set_trace() # # Define and compile theano functions # print "Defining and compiling training functions" print convmodel[0] prediction = lasagne.layers.get_output(convmodel[0]) loss = categorical_crossentropy(prediction, target_var) loss = loss.mean() #loss = crossentropy(prediction, target_var, void_labels) if weight_decay > 0: weightsl2 = regularize_network_params(convmodel, lasagne.regularization.l2) loss += weight_decay * weightsl2 train_acc = accuracy(prediction, target_var, void_labels) params = lasagne.layers.get_all_params(convmodel, trainable=True) updates = lasagne.updates.adam(loss, params, learning_rate=learn_step) train_fn = theano.function([input_var, target_var], [loss, train_acc], updates=updates) print "Defining and compiling test functions" test_prediction = lasagne.layers.get_output(convmodel, deterministic=True)[0] test_loss = categorical_crossentropy(test_prediction, target_var) test_loss = test_loss.mean() #test_loss = crossentropy(test_prediction, target_var, void_labels) test_acc = accuracy(test_prediction, target_var, void_labels) test_jacc = jaccard(test_prediction, target_var, n_classes) val_fn = theano.function([input_var, target_var], [test_loss, test_acc, test_jacc]) # # Train # err_train = [] acc_training = [] err_valid = [] acc_valid = [] jacc_valid = [] patience = 0 print train_iter.next() import pdb pdb.set_trace() # Training main loop print "Start training" for epoch in range(num_epochs): #print('epoch' + str(epoch)) # Single epoch training and validation start_time = time.time() cost_train_tot = 0 acc_train_tot = 0 # Train if minibatches_subset > 0: n_batches_val = minibatches_subset n_batches_train = minibatches_subset for i in range(n_batches_train): # Get minibatch X_train_batch, L_train_batch = train_iter.next() L_train_batch = np.reshape(L_train_batch, np.prod(L_train_batch.shape)) # Training step cost_train, acc_train = train_fn(X_train_batch, L_train_batch) cost_train_tot += cost_train acc_train_tot += acc_train err_train += [cost_train_tot / n_batches_train] acc_training += [acc_train_tot / n_batches_train] # Validation cost_val_tot = 0 acc_val_tot = 0 jacc_val_tot = np.zeros((2, n_classes)) for i in range(n_batches_val): # Get minibatch X_val_batch, L_val_batch = val_iter.next() L_val_batch = np.reshape(L_val_batch, np.prod(L_val_batch.shape)) # Validation step cost_val, acc_val, jacc_val = val_fn(X_val_batch, L_val_batch) cost_val_tot += cost_val acc_val_tot += acc_val jacc_val_tot += jacc_val err_valid += [cost_val_tot / n_batches_val] acc_valid += [acc_val_tot / n_batches_val] jacc_perclass_valid = jacc_val_tot[0, :] / jacc_val_tot[1, :] jacc_valid += [np.mean(jacc_perclass_valid)] out_str = "EPOCH %i: Avg cost train %f, acc train %f"+\ ", cost val %f, acc val %f, jacc val %f took %f s" out_str = out_str % (epoch, err_train[epoch], acc_training[epoch], err_valid[epoch], acc_valid[epoch], jacc_valid[epoch], time.time() - start_time) print out_str with open(os.path.join(savepath, "fcn1D_output.log"), "a") as f: f.write(out_str + "\n") # Early stopping and saving stuff if epoch == 0: best_jacc_val = jacc_valid[epoch] elif epoch > 1 and jacc_valid[epoch] > best_jacc_val: print('saving best model:') best_jacc_val = jacc_valid[epoch] patience = 0 np.savez(os.path.join(savepath, 'new_fcn1D_model_best.npz'), *lasagne.layers.get_all_param_values(convmodel)) np.savez(os.path.join(savepath, "fcn1D_errors_best.npz"), err_train=err_train, acc_train=acc_training, err_valid=err_valid, acc_valid=acc_valid, jacc_valid=jacc_valid) else: patience += 1 print('saving last model') np.savez(os.path.join(savepath, 'new_fcn1D_model_last.npz'), *lasagne.layers.get_all_param_values(convmodel)) np.savez(os.path.join(savepath, "fcn1D_errors_last.npz"), err_train=err_train, acc_train=acc_training, err_valid=err_valid, acc_valid=acc_valid, jacc_valid=jacc_valid) # Finish training if patience has expired or max nber of epochs # reached if patience == max_patience or epoch == num_epochs - 1: if test_iter is not None: # Load best model weights with np.load(os.path.join(savepath, 'new_fcn1D_model_best.npz')) as f: param_values = [ f['arr_%d' % i] for i in range(len(f.files)) ] nlayers = len(lasagne.layers.get_all_params(convmodel)) lasagne.layers.set_all_param_values(convmodel, param_values[:nlayers]) # Test cost_test_tot = 0 acc_test_tot = 0 jacc_num_test_tot = np.zeros((1, n_classes)) jacc_denom_test_tot = np.zeros((1, n_classes)) for i in range(n_batches_test): # Get minibatch X_test_batch, L_test_batch = test_iter.next() L_test_batch = np.reshape(L_test_batch, np.prod(L_test_batch.shape)) # Test step cost_test, acc_test, jacc_test = \ val_fn(X_test_batch, L_test_batch) jacc_num_test, jacc_denom_test = jacc_test acc_test_tot += acc_test cost_test_tot += cost_test jacc_num_test_tot += jacc_num_test jacc_denom_test_tot += jacc_denom_test err_test = cost_test_tot / n_batches_test acc_test = acc_test_tot / n_batches_test jacc_test = np.mean(jacc_num_test_tot / jacc_denom_test_tot) out_str = "FINAL MODEL: err test % f, acc test %f, jacc test %f" out_str = out_str % (err_test, acc_test, jacc_test) print out_str if savepath != loadpath: print('Copying model and other training files to {}'.format( loadpath)) copy_tree(savepath, loadpath) # End return
def test(args, test_list, model_list, net_input_shape): if args.weights_path == '': weights_path = join(args.check_dir, args.output_name + '_validation_best_model_' + args.time + '.hdf5') else: weights_path = join(args.data_root_dir, args.weights_path) if args.dataset == 'brats': RESOLUTION = 240 elif args.dataset == 'heart': RESOLUTION = 320 else: RESOLUTION = 512 output_dir = join(args.data_root_dir, 'results', args.net, 'split_' + str(args.split_num)) raw_out_dir = join(output_dir, 'raw_output') fin_out_dir = join(output_dir, 'final_output') fig_out_dir = join(output_dir, 'qual_figs') try: makedirs(raw_out_dir) except: pass try: makedirs(fin_out_dir) except: pass try: makedirs(fig_out_dir) except: pass if len(model_list) > 1: eval_model = model_list[1] else: eval_model = model_list[0] try: eval_model.load_weights(weights_path) except Exception as e: print(e) assert False, 'Unable to find weights path. Testing with random weights.' print_summary(model=eval_model, positions=[.38, .65, .75, 1.]) # Set up placeholders outfile = '' if args.compute_dice: dice_arr = np.zeros((len(test_list))) outfile += 'dice_' if args.compute_jaccard: jacc_arr = np.zeros((len(test_list))) outfile += 'jacc_' if args.compute_assd: assd_arr = np.zeros((len(test_list))) outfile += 'assd_' surf_arr = np.zeros((len(test_list)), dtype=str) dice2_arr = np.zeros((len(test_list), args.out_classes-1)) # Testing the network print('Testing... This will take some time...') with open(join(output_dir, args.save_prefix + outfile + 'scores.csv'), 'wb') as csvfile: writer = csv.writer(csvfile, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL) dice_results_csv = open(join(output_dir, args.save_prefix + outfile + 'dice_scores.csv'), 'wb') dice_writer = csv.writer(dice_results_csv, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL) row = ["Scan Name"] for i in range(1,args.out_classes): row.append("Dice_{}".format(i)) dice_writer.writerow(row) row = ['Scan Name'] if args.compute_dice: row.append('Dice Coefficient') if args.compute_jaccard: row.append('Jaccard Index') if args.compute_assd: row.append('Average Symmetric Surface Distance') writer.writerow(row) for i, img in enumerate(tqdm(test_list)): sitk_img = sitk.ReadImage(join(args.data_root_dir, 'imgs', img[0])) img_data = sitk.GetArrayFromImage(sitk_img) num_slices = img_data.shape[0] if args.dataset == 'brats': num_slices = img_data.shape[1]#brats img_data = np.rollaxis(img_data,0,4) print(args.dataset) output_array = eval_model.predict_generator(generate_test_batches(args.data_root_dir, [img], net_input_shape, batchSize=1, numSlices=args.slices, subSampAmt=0, stride=1, dataset = args.dataset, num_output_classes=args.out_classes), steps=num_slices, max_queue_size=1, workers=1, use_multiprocessing=False, verbose=1) print('out' + str(output_array[0].shape)) if args.net.find('caps') != -1: if args.dataset == 'brats': output = output_array[0][:,8:-8,8:-8] recon = output_array[1][:,8:-8,8:-8] else: output = output_array[0][:,:,:] recon = output_array[1][:,:,:] else: if args.dataset == 'brats': output = output_array[:,8:-8,8:-8,:] else: output = output_array[:,:,:,:] if args.out_classes == 1: output_raw = output.reshape(-1,RESOLUTION,RESOLUTION,1) #binary else: output_raw = output output = oneHot2LabelMax(output) out = output.astype(np.int64) if args.out_classes == 1: outputOnehot = out.reshape(-1,RESOLUTION,RESOLUTION,1) #binary else: outputOnehot = np.eye(args.out_classes)[out].astype(np.uint8) output_img = sitk.GetImageFromArray(output) print('Segmenting Output') output_bin = threshold_mask(output, args.thresh_level) output_mask = sitk.GetImageFromArray(output_bin) slice_img = sitk.Image(RESOLUTION,RESOLUTION,num_slices, sitk.sitkUInt8) output_img.CopyInformation(slice_img) output_mask.CopyInformation(slice_img) #output_img.CopyInformation(sitk_img) #output_mask.CopyInformation(sitk_img) print('Saving Output') if args.dataset != 'luna': sitk.WriteImage(output_img, join(raw_out_dir, img[0][:-7] + '_raw_output' + img[0][-7:])) sitk.WriteImage(output_mask, join(fin_out_dir, img[0][:-7] + '_final_output' + img[0][-7:])) # Load gt mask sitk_mask = sitk.ReadImage(join(args.data_root_dir, 'masks', img[0])) gt_data = sitk.GetArrayFromImage(sitk_mask) label = gt_data.astype(np.int64) if args.out_classes == 1: gtOnehot = label.reshape(-1,RESOLUTION,RESOLUTION,1) #binary gt_label = label else: gtOnehot = np.eye(args.out_classes)[label].astype(np.uint8) gt_label = label if args.net.find('caps') != -1: create_recon_image(args, recon, img_data, gtOnehot, i=i) create_activation_image(args, output_raw, gtOnehot, slice_num=output_raw.shape[0] // 2, index=i) # Plot Qual Figure print('Creating Qualitative Figure for Quick Reference') f, ax = plt.subplots(2, 3, figsize=(10, 5)) colors = ['Greys', 'Greens', 'Reds', 'Blues'] fileTypeLength = 7 print(img_data.shape) print(outputOnehot.shape) if args.dataset == 'brats': #img_data = img_data[3] #caps? img_data = img_data[:,:,:,3] #isensee # Prediction plots ax[0,0].imshow(img_data[num_slices // 3, :, :], alpha=1, cmap='gray') for class_num in range(1, outputOnehot.shape[3]): mask = outputOnehot[num_slices // 3, :, :, class_num] mask = np.ma.masked_where(mask == 0, mask) ax[0,0].imshow(mask, alpha=0.7, cmap=colors[class_num], vmin = 0, vmax = 1) ax[0,0].set_title('Slice {}/{}'.format(num_slices // 3, num_slices)) ax[0,0].axis('off') ax[0,1].imshow(img_data[num_slices // 2, :, :], alpha=1, cmap='gray') for class_num in range(1, outputOnehot.shape[3]): mask = outputOnehot[num_slices // 2, :, :, class_num] mask = np.ma.masked_where(mask == 0, mask) ax[0,1].imshow(mask, alpha=0.7, cmap=colors[class_num], vmin = 0, vmax = 1) ax[0,1].set_title('Slice {}/{}'.format(num_slices // 2, num_slices)) ax[0,1].axis('off') ax[0,2].imshow(img_data[num_slices // 2 + num_slices // 4, :, :], alpha=1, cmap='gray') for class_num in range(1, outputOnehot.shape[3]): mask = outputOnehot[num_slices // 2 + num_slices // 4, :, :, class_num] mask = np.ma.masked_where(mask == 0, mask) ax[0,2].imshow(mask, alpha=0.7, cmap=colors[class_num], vmin = 0, vmax = 1) ax[0,2].set_title( 'Slice {}/{}'.format(num_slices // 2 + num_slices // 4, num_slices)) ax[0,2].axis('off') # Ground truth plots ax[1,0].imshow(img_data[num_slices // 3, :, :], alpha=1, cmap='gray') ax[1,0].set_title('Slice {}/{}'.format(num_slices // 3, num_slices)) for class_num in range(1, gtOnehot.shape[3]): mask = gtOnehot[num_slices // 3, :, :, class_num] mask = np.ma.masked_where(mask == 0, mask) ax[1,0].imshow(mask, alpha=0.7, cmap=colors[class_num], vmin = 0, vmax = 1) ax[1,0].axis('off') ax[1,1].imshow(img_data[num_slices // 2, :, :], alpha=1, cmap='gray') ax[1,1].set_title('Slice {}/{}'.format(num_slices // 2, num_slices)) for class_num in range(1, gtOnehot.shape[3]): mask = gtOnehot[num_slices // 2, :, :, class_num] mask = np.ma.masked_where(mask == 0, mask) ax[1,1].imshow(mask, alpha=0.7, cmap=colors[class_num], vmin = 0, vmax = 1) ax[1,1].axis('off') ax[1,2].imshow(img_data[num_slices // 2 + num_slices // 4, :, :], alpha=1, cmap='gray') ax[1,2].set_title( 'Slice {}/{}'.format(num_slices // 2 + num_slices // 4, num_slices)) for class_num in range(1, gtOnehot.shape[3]): mask = gtOnehot[num_slices // 2 + num_slices // 4, :, :, class_num] mask = np.ma.masked_where(mask == 0, mask) ax[1,2].imshow(mask, alpha=0.7, cmap=colors[class_num], vmin = 0, vmax = 1) ax[1,2].axis('off') fig = plt.gcf() fig.suptitle(img[0][:-fileTypeLength]) plt.savefig(join(fig_out_dir, img[0][:-fileTypeLength] + '_qual_fig' + '.png'), format='png', bbox_inches='tight') plt.close('all') else: sitk.WriteImage(output_img, join(raw_out_dir, img[0][:-4] + '_raw_output' + img[0][-4:])) sitk.WriteImage(output_mask, join(fin_out_dir, img[0][:-4] + '_final_output' + img[0][-4:])) # Load gt mask sitk_mask = sitk.ReadImage(join(args.data_root_dir, 'masks', img[0])) gt_data = sitk.GetArrayFromImage(sitk_mask) f, ax = plt.subplots(1, 3, figsize=(15, 5)) ax[0].imshow(img_data[img_data.shape[0] // 3, :, :], alpha=1, cmap='gray') ax[0].imshow(output_bin[img_data.shape[0] // 3, :, :], alpha=0.5, cmap='Reds') #ax[0].imshow(gt_data[img_data.shape[0] // 3, :, :], alpha=0.2, cmap='Reds') ax[0].set_title('Slice {}/{}'.format(img_data.shape[0] // 3, img_data.shape[0])) ax[0].axis('off') ax[1].imshow(img_data[img_data.shape[0] // 2, :, :], alpha=1, cmap='gray') ax[1].imshow(output_bin[img_data.shape[0] // 2, :, :], alpha=0.5, cmap='Reds') #ax[1].imshow(gt_data[img_data.shape[0] // 2, :, :], alpha=0.2, cmap='Reds') ax[1].set_title('Slice {}/{}'.format(img_data.shape[0] // 2, img_data.shape[0])) ax[1].axis('off') ax[2].imshow(img_data[img_data.shape[0] // 2 + img_data.shape[0] // 4, :, :], alpha=1, cmap='gray') ax[2].imshow(output_bin[img_data.shape[0] // 2 + img_data.shape[0] // 4, :, :], alpha=0.5, cmap='Reds') #ax[2].imshow(gt_data[img_data.shape[0] // 2 + img_data.shape[0] // 4, :, :], alpha=0.2, # cmap='Reds') ax[2].set_title( 'Slice {}/{}'.format(img_data.shape[0] // 2 + img_data.shape[0] // 4, img_data.shape[0])) ax[2].axis('off') fig = plt.gcf() fig.suptitle(img[0][:-4]) plt.savefig(join(fig_out_dir, img[0][:-4] + '_qual_fig' + '.png'), format='png', bbox_inches='tight') output_label = oneHot2LabelMax(outputOnehot) row = [img[0][:-4]] dice_row = [img[0][:-4]] if args.compute_dice: print('Computing Dice') dice_arr[i] = dc(outputOnehot, gtOnehot) print('\tDice: {}'.format(dice_arr[i])) row.append(dice_arr[i]) if args.compute_jaccard: print('Computing Jaccard') jacc_arr[i] = jaccard(outputOnehot, gtOnehot) print('\tJaccard: {}'.format(jacc_arr[i])) row.append(jacc_arr[i]) if args.compute_assd: print('Computing ASSD') assd_arr[i] = assd(outputOnehot, gtOnehot, voxelspacing=sitk_img.GetSpacing(), connectivity=1) print('\tASSD: {}'.format(assd_arr[i])) row.append(assd_arr[i]) try: spacing = np.array(sitk_img.GetSpacing()) if args.dataset == 'brats': spacing = spacing[1:] surf = compute_surface_distances(label, out, spacing) surf_arr[i] = str(surf) assd_score = calc_assd_scores(output_label, gt_label, args.out_classes, spacing) print(assd_score) print('\tSurface distance ' + str(surf_arr[i])) except: print("surf failed") pass #dice2_arr[i] = compute_dice_coefficient(gtOnehot, outputOnehot) dice2_arr[i] = calc_dice_scores(output_label, gt_label, args.out_classes) for score in dice2_arr[i]: dice_row.append(score) dice_writer.writerow(dice_row) print('\tMSD Dice: {}'.format(dice2_arr[i])) writer.writerow(row) dice_row = ['Average Scores'] avgs = np.mean(dice2_arr, axis=0) for avg in avgs: dice_row.append(avg) dice_writer.writerow(dice_row) row = ['Average Scores'] if args.compute_dice: row.append(np.mean(dice_arr)) if args.compute_jaccard: row.append(np.mean(jacc_arr)) if args.compute_assd: row.append(np.mean(assd_arr)) row.append(surf_arr) row.append(np.mean(dice2_arr)) writer.writerow(row) dice_results_csv.close() print('Done.')
def jaccardLoss(y_true, y_pred): # Loss function is negative return -jaccard(y_true, y_pred)
if weight_decay > 0: weightsl2 = regularize_network_params(simple_net_output, lasagne.regularization.l2) loss += weight_decay * weightsl2 # train_acc, train_sample_acc = accuracy(prediction, target_var, void_labels) params = lasagne.layers.get_all_params(simple_net_output, trainable=True) updates = lasagne.updates.adam(loss, params, learning_rate=learn_step) train_fn = theano.function([input_var, target_var], [loss, predictions], updates=updates) #, profile=True) # compute jaccard with the predicted segmentation obtained by converting # the offsets to a segmentation jacc = jaccard(predicted_seg, target_seg, n_classes) jacc_fn = theano.function([predicted_seg, target_seg], jacc) print "Done" print "Defining and compiling valid functions" valid_predictions = lasagne.layers.get_output(simple_net_output, deterministic=True) valid_predictions = T.concatenate(valid_predictions, axis=1) valid_loss = squared_error(predictions, target_var) valid_loss = valid_loss.mean() valid_fn = theano.function([input_var, target_var], [valid_loss, valid_predictions]) #,profile=True) print "Done"
mask_folder = 'data/masks/' if not os.path.exists('data/masks1/'): os.makedirs('data/masks1/') accuracies = [] jaccards = [] for i in range(1, 61): file = str(i).zfill(3) print(image_folder + file + "_image.png") img = cv2.imread(image_folder + file + "_image.png") mask = cv2.imread(mask_folder + file + "_mask.png") NIR = np.float32(img[:, :, 1]) red = np.float32(img[:, :, 0]) up = NIR - red down = NIR + red true_mask = mask[:, :, 0] indexes = (NIR - red) / (NIR + red) pred_mask = (indexes < 330 / 1000) * 255 acc = accuracy(pred_mask, true_mask) jacc = jaccard(pred_mask / 255, true_mask / 255) cv2.imwrite('data/masks1/' + file + '_mask.png', pred_mask) accuracies.append(acc) jaccards.append(jacc) print("Average Accuracy: ", np.average(accuracies)) print("Average Jaccard Index: ", np.average(jaccards))
def nextFrame(arg): """ Function called for each successive animation frame; arg is the frame number """ global mmax, pmax, ADs, ADList, AVG_DIST, SpecDisp, SpecMaint, SpecGrowth global fixed, p, BurnIn, t, num_sims, width, height, Rates, u0, rho, ux, uy global n0, nN, nS, nE, nW, nNE, nNW, nSE, nSW, SpColorDict, GrowthDict, N_RD global P_RD, C_RD, DispDict, MaintDict, one9th, four9ths, one36th, barrier global gmax, dmax, maintmax, IndIDs, Qs, IndID, IndTimeIn, IndExitAge, IndX global IndY, Ind_scatImage, SpeciesIDs, EnvD, TY, tracer_scatImage, TTimeIn global TIDs, TExitAge, TX, RTypes, RX, RY, RID, RIDs, RVals, RTimeIn, RExitAge global resource_scatImage, bN, bS, bE, bW, bNE, bNW, bSE, bSW, ct1, Mu, Maint global motion, reproduction, speciation, seedCom, m, r, nNi, nP, nC, rmax, sim global RAD, splist, N, ct, splist2, WTs, Jcs, Sos, RDens, RDiv, RRich, S, ES global Ev, BP, SD, Nm, sk, T, R, LowerLimit, prod_i, prod_q, viscosity, alpha global Ts, Rs, PRODIs, Ns, TTAUs, INDTAUs, RDENs, RDIVs, RRICHs, Ss, ESs, EVs global BPs, SDs, NMAXs, SKs, MUs, MAINTs, PRODNs, PRODPs, PRODCs, lefts, bottoms global Gs, Ms, NRs, PRs, CRs, Ds, RTAUs, GrowthList, MaintList, N_RList, P_RList global C_RList, DispList, amp, freq, flux, pulse, phase, disturb, envgrads global barriers, MainFactorDict, RPFDict ct += 1 #plot_system = 'yes' plot_system = 'no' # fluctuate flow according to amplitude, frequency, & phase u1 = u0 + u0*(amp * sin(2*pi * ct * freq + phase)) if u1 > 1: u1 == 1.0 # Fluid dynamics nN, nS, nE, nW, nNE, nNW, nSE, nSW, barrier = LBM.stream([nN, nS, nE, nW, nNE, nNW, nSE, nSW, barrier]) rho, ux, uy, n0, nN, nS, nE, nW, nNE, nNW, nSE, nSW = LBM.collide(viscosity, rho, ux, uy, n0, nN, nS, nE, nW, nNE, nNW, nSE, nSW, u0) # Inflow of resources if motion == 'white_noise' or motion == 'brown_noise': u1 = 2 RTypes, RVals, RX, RY, RIDs, RID, RTimeIn = bide.ResIn(motion, RTypes, RVals, RX, RY, RID, RIDs, RTimeIn, r, rmax, nNi, nP, nC, width, height, u1) # resource flow Lists = [RTypes, RIDs, RID, RVals] if len(RTypes) > 0: if motion == 'fluid': RTypes, RX, RY, RExitAge, RIDs, RID, RTimeIn, RVals = bide.fluid_movement('resource', Lists, RTimeIn, RExitAge, RX, RY, ux, uy, width, height, u0) else: RTypes, RX, RY, RExitAge, RIDs, RID, RTimeIn, RVals = bide.nonfluid_movement('resource', motion, Lists, RTimeIn, RExitAge, RX, RY, ux, uy, width, height, u0) # Inflow of individuals (immigration) if ct == 1: SpeciesIDs, IndX, IndY, MaintDict, MainFactorDict, RPFDict, EnvD, GrowthDict, DispDict, SpColorDict, IndIDs, IndID, IndTimeIn, Qs, N_RD, P_RD, C_RD, GrowthList, MaintList, N_RList, P_RList, C_RList, DispList, ADList = bide.immigration(mmax, pmax, dmax, gmax, maintmax, motion, seedCom, 1, SpeciesIDs, IndX, IndY, width, height, MaintDict, MainFactorDict, RPFDict, EnvD, envgrads, GrowthDict, DispDict, SpColorDict, IndIDs, IndID, IndTimeIn, Qs, N_RD, P_RD, C_RD, nNi, nP, nC, u1, alpha, GrowthList, MaintList, N_RList, P_RList, C_RList, DispList, ADList) else: SpeciesIDs, IndX, IndY, MaintDict, MainFactorDict, RPFDict, EnvD, GrowthDict, DispDict, SpColorDict, IndIDs, IndID, IndTimeIn, Qs, N_RD, P_RD, C_RD, GrowthList, MaintList, N_RList, P_RList, C_RList, DispList, ADList = bide.immigration(mmax, pmax, dmax, gmax, maintmax, motion, 1, m, SpeciesIDs, IndX, IndY, width, height, MaintDict, MainFactorDict, RPFDict, EnvD, envgrads, GrowthDict, DispDict, SpColorDict, IndIDs, IndID, IndTimeIn, Qs, N_RD, P_RD, C_RD, nNi, nP, nC, u1, alpha, GrowthList, MaintList, N_RList, P_RList, C_RList, DispList, ADList) # dispersal Lists = [SpeciesIDs, IndIDs, IndID, Qs, DispDict, GrowthList, MaintList, N_RList, P_RList, C_RList, DispList, ADList] if len(SpeciesIDs) > 0: if motion == 'fluid': SpeciesIDs, IndX, IndY, IndExitAge, IndIDs, IndID, IndTimeIn, Qs, GrowthList, MaintList, N_RList, P_RList, C_RList, DispList, ADList, Qs = bide.fluid_movement('individual', Lists, IndTimeIn, IndExitAge, IndX, IndY, ux, uy, width, height, u0) else: SpeciesIDs, IndX, IndY, IndExitAge, IndIDs, IndID, IndTimeIn, Qs, GrowthList, MaintList, N_RList, P_RList, C_RList, DispList, ADList = bide.nonfluid_movement('individual', motion, Lists, IndTimeIn, IndExitAge, IndX, IndY, ux, uy, width, height, u0) # Chemotaxis #SpeciesIDs, Qs, IndIDs, ID, TimeIn, X, Y, GrowthDict, DispDict, GrowthList, MaintList, N_RList, P_RList, C_RList, DispList, ADList = bide.chemotaxis(reproduction, speciation, SpeciesIDs, Qs, IndIDs, IndID, IndTimeIn, IndX, IndY, width, height, GrowthDict, DispDict, SpColorDict, N_RD, P_RD, C_RD, MaintDict, MainFactorDict, RPFDict, EnvD, envgrads, nNi, nP, nC, GrowthList, MaintList, N_RList, P_RList, C_RList, DispList, ADList) # Forage #SpeciesIDs, Qs, IndIDs, ID, TimeIn, X, Y, GrowthDict, DispDict, GrowthList, MaintList, N_RList, P_RList, C_RList, DispList, ADList = bide.density_forage(RVals, RX, RY, reproduction, speciation, SpeciesIDs, Qs, IndIDs, IndID, IndTimeIn, IndX, IndY, width, height, GrowthDict, DispDict, SpColorDict, N_RD, P_RD, C_RD, MaintDict, MainFactorDict, RPFDict, EnvD, envgrads, nNi, nP, nC, GrowthList, MaintList, N_RList, P_RList, C_RList, DispList, ADList) PRODI, PRODN, PRODC, PRODP = 0, 0, 0, 0 p1, TNQ1, TPQ1, TCQ1 = metrics.getprod(Qs) # Consume #RTypes, RVals, RIDs, RID, RTimeIn, RExitAge, RX, RY, SpeciesIDs, Qs, IndIDs, IndID, IndTimeIn, IndX, IndY, GrowthList, MaintList, N_RList, P_RList, C_RList, DispList, ADList = bide.consume(RTypes, RVals, RIDs, RID, RX, RY, RTimeIn, RExitAge, SpeciesIDs, Qs, IndIDs, IndID, IndTimeIn, IndX, IndY, width, height, GrowthDict, N_RD, P_RD, C_RD, DispDict, GrowthList, MaintList, N_RList, P_RList, C_RList, DispList, ADList) # Reproduction SpeciesIDs, Qs, IndIDs, ID, TimeIn, X, Y, GrowthDict, DispDict, GrowthList, MaintList, N_RList, P_RList, C_RList, DispList, ADList = bide.reproduce(reproduction, speciation, SpeciesIDs, Qs, IndIDs, IndID, IndTimeIn, IndX, IndY, width, height, GrowthDict, DispDict, SpColorDict, N_RD, P_RD, C_RD, MaintDict, MainFactorDict, RPFDict, EnvD, envgrads, nNi, nP, nC, GrowthList, MaintList, N_RList, P_RList, C_RList, DispList, ADList) # maintenance #SpeciesIDs, X, Y, IndExitAge, IndIDs, IndTimeIn, Qs, GrowthList, MaintList, N_RList, P_RList, C_RList, DispList, ADList = bide.maintenance(SpeciesIDs, IndX, IndY, IndExitAge, SpColorDict, MaintDict, MainFactorDict, RPFDict, EnvD, IndIDs, IndTimeIn, Qs, GrowthList, MaintList, N_RList, P_RList, C_RList, DispList, ADList) # transition to or from dormancy Sp_IDs, IDs, Qs, GrowthList, MaintList, ADList = bide.transition(SpeciesIDs, IndIDs, Qs, GrowthList, MaintList, MainFactorDict, RPFDict, ADList) p2, TNQ2, TPQ2, TCQ2 = metrics.getprod(Qs) PRODI = p2 - p1 PRODN = TNQ2 - TNQ1 PRODP = TPQ2 - TPQ1 PRODC = TCQ2 - TCQ1 # disturbance #if np.random.binomial(1, disturb*u0) == 1: SpeciesIDs, X, Y, IndExitAge, IndIDs, IndTimeIn, Qs, GrowthList, MaintList, N_RList, P_RList, C_RList, DispList, ADList = bide.decimate(SpeciesIDs, IndX, IndY, IndExitAge, SpColorDict, MaintDict, MainFactorDict, RPFDict, EnvD, IndIDs, IndTimeIn, Qs, GrowthList, MaintList, N_RList, P_RList, C_RList, DispList, ADList) ax = fig.add_subplot(111) plt.tick_params(axis='both', which='both', bottom='off', top='off', left='off', right='off', labelbottom='off', labelleft='off') if len(SpeciesIDs) >= 1: RAD, splist = bide.GetRAD(SpeciesIDs) else: RAD, splist, N, S = [], [], 0, 0 N, S, tt, rr = sum(RAD), len(RAD), len(TIDs), len(RIDs) numD = ADList.count('d') if N != len(ADList): print N, len(SpeciesIDs), len(ADList) print "N != len(ADList)" sys.exit() if N > 0: Title = ['Individuals consume resources, grow, reproduce, and die as they move through the environment. \nAverage speed on the x-axis is '+str(u0)+' units per time step. '+str(len(TExitAge))+' tracers have passed through.\nN: '+str(N)+', S: '+str(S)+', tracers: '+str(tt)+', resources: '+str(rr)+', ct: '+str(ct)+', %dormant: '+str(round((numD/N)*100, 2))] else: Title = ['Individuals consume resources, grow, reproduce, and die as they move through the environment. \nAverage speed on the x-axis is '+str(u0)+' units per time step. '+str(len(TExitAge))+' tracers have passed through.\nN: '+str(N)+', S: '+str(S)+', tracers: '+str(tt)+', resources: '+str(rr)+', ct: '+str(ct)+', %dormant: nan'] txt.set_text(' '.join(Title)) ax.set_ylim(0, height) ax.set_xlim(0, width) if plot_system == 'yes': ##### PLOTTING THE SYSTEM ############################################## resource_scatImage.remove() tracer_scatImage.remove() Ind_scatImage.remove() colorlist = [] sizelist = [] for i, val in enumerate(SpeciesIDs): if ADList[i] == 'a': colorlist.append('red') elif ADList[i] == 'd': colorlist.append('0.3') sizelist.append(min(Qs[i]) * 1000) resource_scatImage = ax.scatter(RX, RY, s = RVals*100, c = 'w', edgecolor = 'SpringGreen', lw = 0.6, alpha=0.3) Ind_scatImage = ax.scatter(IndX, IndY, s = sizelist, c = colorlist, edgecolor = '0.2', lw = 0.2, alpha=0.9) tracer_scatImage = ax.scatter(TX, TY, s = 200, c = 'r', marker='*', lw=0.0, alpha=0.6) Ns.append(N) if N == 0 and BurnIn == 'not done': Ns = [Ns[-1]] # only keep the most recent N value BurnIn = 'done' if ct > 200 and BurnIn == 'not done': if len(Ns) > 100: AugmentedDickeyFuller = sta.adfuller(Ns) val, p = AugmentedDickeyFuller[0:2] if p >= 0.05: Ns.pop(0) elif p < 0.05 or isnan(p) == True: BurnIn = 'done' Ns = [Ns[-1]] # only keep the most recent N value if ct > 300 and BurnIn == 'not done': Ns = [Ns[-1]] # only keep the most recent N value BurnIn = 'done' if BurnIn == 'done': PRODIs.append(PRODI) PRODNs.append(PRODN) PRODPs.append(PRODP) PRODCs.append(PRODC) if len(RExitAge) > 0: RTAUs.append(mean(RExitAge)) if len(IndExitAge) > 0: INDTAUs.append(mean(IndExitAge)) if len(TExitAge) > 0: TTAUs.append(mean(TExitAge)) # Examining the resource RAD if len(RTypes) > 0: RRAD, Rlist = bide.GetRAD(RTypes) RDens = len(RTypes)/(height*width) RDiv = float(metrics.Shannons_H(RRAD)) RRich = len(Rlist) RDENs.append(RDens) RDIVs.append(RDiv) RRICHs.append(RRich) # Number of tracers, resource particles, and individuals T, R, N = len(TIDs), len(RIDs), len(SpeciesIDs) Ts.append(T) Rs.append(R) Ss.append(S) if N >= 1: if R >= 1: q = min([10, R]) avg_dist = spatial.avg_dist(IndX, RX, IndY, RY, q) avg_dist = spatial.nearest_neighbor(IndX, RX, IndY, RY, q) AVG_DIST.append(avg_dist) spD = DispDict.values() spM = MaintDict.values() spMF = MainFactorDict.values() spRPF = RPFDict.values() spG = GrowthDict.values() SpecDisp.append(mean(spD)) SpecMaint.append(mean(spM)) SpecGrowth.append(mean(spG)) RAD, splist = bide.GetRAD(SpeciesIDs) RAD, splist = zip(*sorted(zip(RAD, splist), reverse=True)) RAD = list(RAD) S = len(RAD) Ss.append(S) # Evenness, Dominance, and Rarity measures Ev = metrics.e_var(RAD) EVs.append(Ev) ES = metrics.e_simpson(RAD) ESs.append(ES) if len(Ns) == 1: splist2 = list(splist) if len(Ns) > 1: wt = metrics.WhittakersTurnover(splist, splist2) jc = metrics.jaccard(splist, splist2) so = metrics.sorensen(splist, splist2) splist2 = list(splist) WTs.append(wt) Jcs.append(jc) Sos.append(so) Nm, BP = [max(RAD), Nm/N] NMAXs.append(Nm) BPs.append(BP) SD = metrics.simpsons_dom(RAD) SDs.append(SD) sk = stats.skew(RAD) SKs.append(sk) Gs.append(mean(GrowthList)) Ms.append(mean(MaintList)) Ds.append(mean(DispList)) numD = ADList.count('d') ADs.append(numD/len(ADList)) Nmeans = [sum(x)/len(x) for x in zip(*N_RList)] NRs.append(mean(Nmeans)) Pmeans = [sum(x)/len(x) for x in zip(*P_RList)] PRs.append(mean(Pmeans)) Cmeans = [sum(x)/len(x) for x in zip(*C_RList)] CRs.append(mean(Cmeans)) #process = psutil.Process(os.getpid()) #mem = round(process.get_memory_info()[0] / float(2 ** 20), 1) # return the memory usage in MB if len(Ns) > 100: t = time.clock() - t #print sim, ' N:', int(round(mean(Ns))), 'S:', int(round(mean(Ss))), 'WT:', round(mean(WTs),2), ': flow:', u0, 'time:', round(t,1), 'seconds', ': Ttaus:',round(mean(TTimeIn)), round(mean(TExitAge)), ': Etau:', round(width/u0) #' MB:',int(round(mem)), 'p-val =', round(p,3) #print sim, ' N:', int(round(mean(Ns))), 'S:', int(round(mean(Ss))), ': flow:', u0, 'time:', round(t,1), 'seconds', ' height:', str(height), ' Avg dist:', round(mean(AVG_DIST),3), ' f(dormant):',round(mean(ADs),3) print sim, ' N:', int(round(mean(Ns))), 'S:', int(round(mean(Ss))), ' flow:', u0, 'time:', round(t,1), 'Ttaus:', round(mean(TExitAge),2), ': Etau:', round((width-1)/u0,2), 'dormant:', round(mean(ADs),3) t = time.clock() SString = str(splist).strip('()') RADString = str(RAD).strip('()') RADString = str(RAD).strip('[]') IndRTD = str(IndExitAge).strip('[]') TRTD = str(TExitAge).strip('[]') RRTD = str(RExitAge).strip('[]') OUT1 = open(GenPath + '/SimData.csv','a') #TTAUs = np.mean(TExitAge), np.mean(TTimeIn) outlist = [sim,motion,mean(PRODIs),mean(PRODNs),mean(PRODPs),mean(PRODCs),r,nNi,nP,nC,rmax,gmax,maintmax,dmax,barriers,alpha,seedCom,u0,width-0.2,height,viscosity,N,m,mean(RTAUs), mean(TExitAge) ,mean(INDTAUs),mean(RDENs),mean(RDIVs),mean(RRICHs),mean(Ss),mean(ESs),mean(EVs),mean(BPs),mean(SDs),mean(NMAXs),mean(SKs),T,R,speciation,mean(WTs),mean(Jcs),mean(Sos),mean(Gs),mean(Ms),mean(NRs),mean(PRs),mean(CRs),mean(Ds),amp,flux,freq,phase,disturb, mean(SpecGrowth), mean(SpecDisp), mean(SpecMaint), mean(AVG_DIST), mean(ADs)] outlist = str(outlist).strip('[]') print>>OUT1, outlist OUT1.close() ct1 += 1 ct = 0 Rates = np.roll(Rates, -1, axis=0) u0 = Rates[0] n0, nN, nS, nE, nW, nNE, nNW, nSE, nSW, barrier, rho, ux, uy, bN, bS, bE, bW, bNE, bNW, bSE, bSW = LBM.SetLattice(u0, viscosity, width, height, lefts, bottoms, barriers) u1 = u0 + u0*(amp * sin(2*pi * ct * freq + phase)) RDens, RDiv, RRich, S, ES, Ev, BP, SD, Nm, sk, Mu, Maint, ct, IndID, RID, N, ct1, T, R, PRODI, PRODQ = [0]*21 ADList, ADs, AVG_DIST, SpecDisp, SpecMaint, SpecGrowth, SpColorList, GrowthList, MaintList, N_RList, P_RList, C_RList, RColorList, DispList = [list([]) for _ in xrange(14)] RAD, splist, IndTimeIn, SpeciesIDs, IndX, IndY, IndIDs, Qs, IndExitAge, TX, TY, TExitAge, TIDs, TTimeIn, RX, RY, RIDs, RTypes, RExitAge, RTimeIn, RVals, Gs, Ms, NRs, PRs, CRs, Ds, Ts, Rs, PRODIs, PRODNs, PRODPs, PRODCs, Ns, RTAUs, TTAUs, INDTAUs, RDENs, RDIVs, RRICHs, Ss, ESs, EVs,BPs, SDs, NMAXs, SKs, MUs, MAINTs, WTs, Jcs, Sos, splist2 = [list([]) for _ in xrange(53)] p = 0 BurnIn = 'not done' #if u0 in Rates: if u0 == max(Rates): print '\n' sim += 1 if sim > num_sims: print "model finished" sys.exit() width, height, alpha, motion, reproduction, speciation, seedCom, m, r, nNi, nP, nC, rmax, gmax, maintmax, dmax, amp, freq, flux, pulse, phase, disturb, envgrads, barriers, Rates, pmax, mmax = rp.get_rand_params(fixed) for i in range(barriers): lefts.append(np.random.uniform(0.2, .8)) bottoms.append(np.random.uniform(0.1, 0.7)) n0, nN, nS, nE, nW, nNE, nNW, nSE, nSW, barrier, rho, ux, uy, bN, bS, bE, bW, bNE, bNW, bSE, bSW = LBM.SetLattice(u0, viscosity, width, height, lefts, bottoms, barriers) u1 = u0 + u0*(amp * sin(2*pi * ct * freq + phase)) SpColorDict, GrowthDict, MaintDict, MainFactorDict, RPFDict, EnvD, N_RD, P_RD, C_RD, RColorDict, DispDict, EnvD = {}, {}, {}, {}, {}, {}, {}, {}, {}, {} ####################### REPLACE ENVIRONMENT ######################## ax = fig.add_subplot(111)
def train(dataset, learn_step=0.005, weight_decay=1e-4, num_epochs=500, max_patience=100, data_augmentation={}, savepath=None, loadpath=None, early_stop_class=None, batch_size=None, resume=False, train_from_0_255=False): # # Prepare load/save directories # exp_name = 'fcn8_' + 'data_aug' if bool(data_augmentation) else '' if savepath is None: raise ValueError('A saving directory must be specified') savepath = os.path.join(savepath, dataset, exp_name) loadpath = os.path.join(loadpath, dataset, exp_name) print savepath print loadpath if not os.path.exists(savepath): os.makedirs(savepath) else: print( '\033[93m The following folder already exists {}. ' 'It will be overwritten in a few seconds...\033[0m'.format( savepath)) print('Saving directory : ' + savepath) with open(os.path.join(savepath, "config.txt"), "w") as f: for key, value in locals().items(): f.write('{} = {}\n'.format(key, value)) # # Define symbolic variables # input_var = T.tensor4('input_var') target_var = T.ivector('target_var') # # Build dataset iterator # if batch_size is not None: bs = batch_size else: bs = [10, 1, 1] train_iter, val_iter, test_iter = \ load_data(dataset, data_augmentation, one_hot=False, batch_size=bs, return_0_255=train_from_0_255) n_batches_train = train_iter.nbatches n_batches_val = val_iter.nbatches n_batches_test = test_iter.nbatches if test_iter is not None else 0 n_classes = train_iter.non_void_nclasses void_labels = train_iter.void_labels nb_in_channels = train_iter.data_shape[0] print "Batch. train: %d, val %d, test %d" % (n_batches_train, n_batches_val, n_batches_test) print "Nb of classes: %d" % (n_classes) print "Nb. of input channels: %d" % (nb_in_channels) # # Build network # convmodel = buildFCN8(nb_in_channels, input_var, n_classes=n_classes, void_labels=void_labels, trainable=True, load_weights=resume, pascal=True, layer=['probs']) # # Define and compile theano functions # print "Defining and compiling training functions" prediction = lasagne.layers.get_output(convmodel)[0] loss = crossentropy(prediction, target_var, void_labels) if weight_decay > 0: weightsl2 = regularize_network_params(convmodel, lasagne.regularization.l2) loss += weight_decay * weightsl2 params = lasagne.layers.get_all_params(convmodel, trainable=True) updates = lasagne.updates.adam(loss, params, learning_rate=learn_step) train_fn = theano.function([input_var, target_var], loss, updates=updates) print "Defining and compiling test functions" test_prediction = lasagne.layers.get_output(convmodel, deterministic=True)[0] test_loss = crossentropy(test_prediction, target_var, void_labels) test_acc = accuracy(test_prediction, target_var, void_labels) test_jacc = jaccard(test_prediction, target_var, n_classes) val_fn = theano.function([input_var, target_var], [test_loss, test_acc, test_jacc]) # # Train # err_train = [] err_valid = [] acc_valid = [] jacc_valid = [] patience = 0 # Training main loop print "Start training" for epoch in range(num_epochs): # Single epoch training and validation start_time = time.time() cost_train_tot = 0 # Train for i in range(n_batches_train): # Get minibatch X_train_batch, L_train_batch = train_iter.next() L_train_batch = np.reshape(L_train_batch, np.prod(L_train_batch.shape)) # Training step cost_train = train_fn(X_train_batch, L_train_batch) out_str = "cost %f" % (cost_train) cost_train_tot += cost_train err_train += [cost_train_tot / n_batches_train] # Validation cost_val_tot = 0 acc_val_tot = 0 jacc_val_tot = np.zeros((2, n_classes)) for i in range(n_batches_val): # Get minibatch X_val_batch, L_val_batch = val_iter.next() L_val_batch = np.reshape(L_val_batch, np.prod(L_val_batch.shape)) # Validation step cost_val, acc_val, jacc_val = val_fn(X_val_batch, L_val_batch) acc_val_tot += acc_val cost_val_tot += cost_val jacc_val_tot += jacc_val err_valid += [cost_val_tot / n_batches_val] acc_valid += [acc_val_tot / n_batches_val] jacc_perclass_valid = jacc_val_tot[0, :] / jacc_val_tot[1, :] if early_stop_class == None: jacc_valid += [np.mean(jacc_perclass_valid)] else: jacc_valid += [jacc_perclass_valid[early_stop_class]] out_str = "EPOCH %i: Avg epoch training cost train %f, cost val %f" +\ ", acc val %f, jacc val %f took %f s" out_str = out_str % (epoch, err_train[epoch], err_valid[epoch], acc_valid[epoch], jacc_valid[epoch], time.time() - start_time) print out_str with open(os.path.join(savepath, "fcn8_output.log"), "a") as f: f.write(out_str + "\n") # Early stopping and saving stuff if epoch == 0: best_jacc_val = jacc_valid[epoch] elif epoch > 1 and jacc_valid[epoch] > best_jacc_val: best_jacc_val = jacc_valid[epoch] patience = 0 np.savez(os.path.join(savepath, 'new_fcn8_model_best.npz'), *lasagne.layers.get_all_param_values(convmodel)) np.savez(os.path.join(savepath + "fcn8_errors_best.npz"), err_valid, err_train, acc_valid, jacc_valid) else: patience += 1 np.savez(os.path.join(savepath, 'new_fcn8_model_last.npz'), *lasagne.layers.get_all_param_values(convmodel)) np.savez(os.path.join(savepath + "fcn8_errors_last.npz"), err_valid, err_train, acc_valid, jacc_valid) # Finish training if patience has expired or max nber of epochs # reached if patience == max_patience or epoch == num_epochs - 1: if test_iter is not None: # Load best model weights with np.load(os.path.join(savepath, 'new_fcn8_model_best.npz')) as f: param_values = [ f['arr_%d' % i] for i in range(len(f.files)) ] nlayers = len(lasagne.layers.get_all_params(convmodel)) lasagne.layers.set_all_param_values(convmodel, param_values[:nlayers]) # Test cost_test_tot = 0 acc_test_tot = 0 jacc_num_test_tot = np.zeros((1, n_classes)) jacc_denom_test_tot = np.zeros((1, n_classes)) for i in range(n_batches_test): # Get minibatch X_test_batch, L_test_batch = test_iter.next() L_test_batch = np.reshape(L_test_batch, np.prod(L_test_batch.shape)) # Test step cost_test, acc_test, jacc_test = \ val_fn(X_test_batch, L_test_batch) jacc_num_test, jacc_denom_test = jacc_test acc_test_tot += acc_test cost_test_tot += cost_test jacc_num_test_tot += jacc_num_test jacc_denom_test_tot += jacc_denom_test err_test = cost_test_tot / n_batches_test acc_test = acc_test_tot / n_batches_test jacc_test = np.mean(jacc_num_test_tot / jacc_denom_test_tot) out_str = "FINAL MODEL: err test % f, acc test %f, jacc test %f" out_str = out_str % (err_test, acc_test, jacc_test) print out_str if savepath != loadpath: print('Copying model and other training files to {}'.format( loadpath)) copy_tree(savepath, loadpath) # End return
import cv2 import numpy as np from metrics import jaccard, accuracy image_folder = 'data/images/' mask_folder = 'data/masks/' jaccards = [] accuracies = [] for i in range(1, 61): file = str(i).zfill(3) print(image_folder + file+ "_image.png") img = cv2.imread(image_folder + file+ "_image.png") mask = cv2.imread(mask_folder + file+"_mask.png") color = ('b', 'g', 'r') histr = cv2.calcHist([img], [1], None, [256], [0, 256]) minis = 100+np.argmin(histr[100:110]) new_img = (img[:, :, 1] < minis) * 255 new_img = new_img.astype(np.uint8) acc = accuracy(new_img, mask[:,:,0]) accuracies.append(acc) jacc = jaccard(new_img / 255, mask[:, :, 0] / 255) jaccards.append(jacc) print("Average Accuracy: ", np.average(accuracies)) print("Average Jaccard Index: ", np.average(jaccards))
deterministic=True) valid_loss_reg = weighted_crossentropy(valid_prediction_reg, target_var_reg, weight_vector_reg) valid_loss_reg = valid_loss_reg.mean() # segmentation loss valid_loss_seg = weighted_crossentropy(valid_prediction_seg, target_var_seg, weight_vector_seg) valid_loss_seg = valid_loss_seg.mean() valid_loss = valid_loss_seg+valid_loss_reg valid_acc_reg = accuracy_regions(valid_prediction_reg, target_var_reg) valid_acc_seg, valid_sample_acc_seg = accuracy(valid_prediction_seg, target_var_seg, void_labels) valid_jacc = jaccard(valid_prediction_seg, target_var_seg, n_classes) valid_fn = theano.function([input_var, target_var_reg, target_var_seg, weight_vector_reg, weight_vector_seg], [valid_loss, valid_acc_reg, valid_acc_seg, valid_sample_acc_seg, valid_jacc]) print "Done" # Training loop parameters plot_results_train = False #from the training set plot_results_valid = False #from the validation set treshold = 0.7 # for extracting the very incorrect labelled samples ratios=[0.80,0.85, 0.90] #ratios for the per sample accuracy err_train = [] acc_train_reg = []
def test(dataset, segm_net, which_set='val', data_aug=False, savepath=None, loadpath=None, test_from_0_255=False): # # Define symbolic variables # input_x_var = T.tensor4('input_var') target_var = T.tensor4('target_var') # # Build dataset iterator # data_iter = load_data(dataset, {}, one_hot=True, batch_size=[10, 10, 10], return_0_255=test_from_0_255, which_set=which_set) colors = data_iter.cmap n_batches_test = data_iter.nbatches n_classes = data_iter.non_void_nclasses void_labels = data_iter.void_labels nb_in_channels = data_iter.data_shape[0] void = n_classes if any(void_labels) else n_classes+1 # # Build segmentation network # print ' Building segmentation network' if segm_net == 'fcn8': fcn = buildFCN8(nb_in_channels, input_var=input_x_var, n_classes=n_classes, void_labels=void_labels, path_weights=WEIGHTS_PATH+dataset+'/fcn8_model.npz', trainable=False, load_weights=True, layer=['probs_dimshuffle']) elif segm_net == 'densenet': fcn = build_fcdensenet(input_x_var, nb_in_channels=nb_in_channels, n_classes=n_classes, layer=[], output_d='4d') elif segm_net == 'fcn_fcresnet': raise NotImplementedError else: raise ValueError # # Define and compile theano functions # print "Defining and compiling test functions" test_prediction = lasagne.layers.get_output(fcn, deterministic=True, batch_norm_use_averages=False)[0] # Reshape iterative inference output to b01,c test_prediction_dimshuffle = test_prediction.dimshuffle((0, 2, 3, 1)) sh = test_prediction_dimshuffle.shape test_prediction_2D = test_prediction_dimshuffle.reshape((T.prod(sh[:3]), sh[3])) # Reshape iterative inference output to b01,c target_var_dimshuffle = target_var.dimshuffle((0, 2, 3, 1)) sh2 = target_var_dimshuffle.shape target_var_2D = target_var_dimshuffle.reshape((T.prod(sh2[:3]), sh2[3])) test_loss = squared_error(test_prediction, target_var, void) test_acc = accuracy(test_prediction_2D, target_var_2D, void_labels, one_hot=True) test_jacc = jaccard(test_prediction_2D, target_var_2D, n_classes, one_hot=True) val_fn = theano.function([input_x_var, target_var], [test_acc, test_jacc, test_loss]) pred_fcn_fn = theano.function([input_x_var], test_prediction) # Iterate over test and compute metrics print "Testing" acc_test_tot = 0 mse_test_tot = 0 jacc_num_test_tot = np.zeros((1, n_classes)) jacc_denom_test_tot = np.zeros((1, n_classes)) for i in range(n_batches_test): # Get minibatch X_test_batch, L_test_batch = data_iter.next() Y_test_batch = pred_fcn_fn(X_test_batch) L_test_batch = L_test_batch.astype(_FLOATX) # L_test_batch = np.reshape(L_test_batch, np.prod(L_test_batch.shape)) # Test step acc_test, jacc_test, mse_test = val_fn(X_test_batch, L_test_batch) jacc_num_test, jacc_denom_test = jacc_test acc_test_tot += acc_test mse_test_tot += mse_test jacc_num_test_tot += jacc_num_test jacc_denom_test_tot += jacc_denom_test # Save images # save_img(X_test_batch, L_test_batch, Y_test_batch, # savepath, n_classes, 'batch' + str(i), # void_labels, colors) acc_test = acc_test_tot/n_batches_test mse_test = mse_test_tot/n_batches_test jacc_per_class = jacc_num_test_tot / jacc_denom_test_tot jacc_per_class = jacc_per_class[0] jacc_test = np.mean(jacc_per_class) out_str = "FINAL MODEL: acc test %f, jacc test %f, mse test %f" out_str = out_str % (acc_test, jacc_test, mse_test) print out_str print ">>> Per class jaccard:" labs = data_iter.mask_labels for i in range(len(labs)-len(void_labels)): class_str = ' ' + labs[i] + ' : %f' class_str = class_str % (jacc_per_class[i]) print class_str
def nextFrame(arg): """ Function called for each successive animation frame; arg is the frame number """ global ADs, ADList, AVG_DIST, SpecDisp, SpecMaint, SpecGrowth, fixed, p, BurnIn, t, num_sims, width, height, Rates, u0, rho, ux, uy, n0, nN, nS, nE, nW, nNE, nNW, nSE, nSW, SpColorDict, GrowthDict, N_RD, P_RD, C_RD, DispDict, MaintDict, one9th, four9ths, one36th, barrier, gmax, dmax, maintmax, IndIDs, Qs, IndID, IndTimeIn, IndExitAge, IndX, IndY, Ind_scatImage, SpeciesIDs, EnvD, TY, tracer_scatImage, TTimeIn, TIDs, TExitAge, TX, RTypes, RX, RY, RID, RIDs, RVals, RTimeIn, RExitAge, resource_scatImage, bN, bS, bE, bW, bNE, bNW, bSE, bSW, ct1, Mu, Maint, motion, reproduction, speciation, seedCom, m, r, nNi, nP, nC, rmax, sim, RAD, splist, N, ct, splist2, WTs, Jcs, Sos, RDens, RDiv, RRich, S, ES, Ev, BP, SD, Nm, sk, T, R, LowerLimit, prod_i, prod_q, viscosity, alpha, Ts, Rs, PRODIs, Ns, TTAUs, INDTAUs, RDENs, RDIVs, RRICHs, Ss, ESs, EVs, BPs, SDs, NMAXs, SKs, MUs, MAINTs, PRODNs, PRODPs, PRODCs, lefts, bottoms, Gs, Ms, NRs, PRs, CRs, Ds, RTAUs, GrowthList, MaintList, N_RList, P_RList, C_RList, DispList, amp, freq, flux, pulse, phase, disturb, envgrads, barriers ct += 1 #plot_system = 'yes' plot_system = 'no' # fluctuate flow according to amplitude, frequency, & phase u1 = u0 + u0 * (amp * sin(2 * pi * ct * freq + phase)) if u1 > 1: u1 == 1.0 # Fluid dynamics nN, nS, nE, nW, nNE, nNW, nSE, nSW, barrier = LBM.stream( [nN, nS, nE, nW, nNE, nNW, nSE, nSW, barrier]) rho, ux, uy, n0, nN, nS, nE, nW, nNE, nNW, nSE, nSW = LBM.collide( viscosity, rho, ux, uy, n0, nN, nS, nE, nW, nNE, nNW, nSE, nSW, u0) # Inflow of tracers if motion == 'white_noise' or motion == 'brown_noise': numt = 10 TIDs, TTimeIn, TX, TY = bide.NewTracers(numt, motion, TIDs, TX, TY, TTimeIn, width, height, 2) elif ct == 1: numt = 10 TIDs, TTimeIn, TX, TY = bide.NewTracers(numt, motion, TIDs, TX, TY, TTimeIn, width, height, 2) else: numt = 1 TIDs, TTimeIn, TX, TY = bide.NewTracers(numt, motion, TIDs, TX, TY, TTimeIn, width, height, u0) # moving tracer particles if len(TIDs) > 0: if motion == 'fluid': TIDs, TX, TY, TExitAge, TTimeIn = bide.fluid_movement( 'tracer', TIDs, TTimeIn, TExitAge, TX, TY, ux, uy, width, height, u0) else: TIDs, TX, TY, TExitAge, TTimeIn = bide.nonfluid_movement( 'tracer', motion, TIDs, TTimeIn, TExitAge, TX, TY, ux, uy, width, height, u0) # Inflow of resources if motion == 'white_noise' or motion == 'brown_noise': u1 = 2 RTypes, RVals, RX, RY, RIDs, RID, RTimeIn = bide.ResIn( motion, RTypes, RVals, RX, RY, RID, RIDs, RTimeIn, r, rmax, nNi, nP, nC, width, height, u1) # resource flow Lists = [RTypes, RIDs, RID, RVals] if len(RTypes) > 0: if motion == 'fluid': RTypes, RX, RY, RExitAge, RIDs, RID, RTimeIn, RVals = bide.fluid_movement( 'resource', Lists, RTimeIn, RExitAge, RX, RY, ux, uy, width, height, u0) else: RTypes, RX, RY, RExitAge, RIDs, RID, RTimeIn, RVals = bide.nonfluid_movement( 'resource', motion, Lists, RTimeIn, RExitAge, RX, RY, ux, uy, width, height, u0) # Inflow of individuals (immigration) if ct == 1: SpeciesIDs, IndX, IndY, MaintDict, EnvD, GrowthDict, DispDict, SpColorDict, IndIDs, IndID, IndTimeIn, Qs, N_RD, P_RD, C_RD, GrowthList, MaintList, N_RList, P_RList, C_RList, DispList, ADList = bide.immigration( dmax, gmax, maintmax, motion, seedCom, 1, SpeciesIDs, IndX, IndY, width, height, MaintDict, EnvD, envgrads, GrowthDict, DispDict, SpColorDict, IndIDs, IndID, IndTimeIn, Qs, N_RD, P_RD, C_RD, nNi, nP, nC, u1, alpha, GrowthList, MaintList, N_RList, P_RList, C_RList, DispList, ADList) else: SpeciesIDs, IndX, IndY, MaintDict, EnvD, GrowthDict, DispDict, SpColorDict, IndIDs, IndID, IndTimeIn, Qs, N_RD, P_RD, C_RD, GrowthList, MaintList, N_RList, P_RList, C_RList, DispList, ADList = bide.immigration( dmax, gmax, maintmax, motion, 1, m, SpeciesIDs, IndX, IndY, width, height, MaintDict, EnvD, envgrads, GrowthDict, DispDict, SpColorDict, IndIDs, IndID, IndTimeIn, Qs, N_RD, P_RD, C_RD, nNi, nP, nC, u1, alpha, GrowthList, MaintList, N_RList, P_RList, C_RList, DispList, ADList) # dispersal Lists = [ SpeciesIDs, IndIDs, IndID, Qs, DispDict, GrowthList, MaintList, N_RList, P_RList, C_RList, DispList, ADList, Qs ] if len(SpeciesIDs) > 0: if motion == 'fluid': SpeciesIDs, IndX, IndY, IndExitAge, IndIDs, IndID, IndTimeIn, Qs, GrowthList, MaintList, N_RList, P_RList, C_RList, DispList, ADList, Qs = bide.fluid_movement( 'individual', Lists, IndTimeIn, IndExitAge, IndX, IndY, ux, uy, width, height, u0) else: SpeciesIDs, IndX, IndY, IndExitAge, IndIDs, IndID, IndTimeIn, Qs, GrowthList, MaintList, N_RList, P_RList, C_RList, DispList, ADList, Qs = bide.nonfluid_movement( 'individual', motion, Lists, IndTimeIn, IndExitAge, IndX, IndY, ux, uy, width, height, u0) # Chemotaxis #SpeciesIDs, Qs, IndIDs, ID, TimeIn, X, Y, GrowthDict, DispDict, GrowthList, MaintList, N_RList, P_RList, C_RList, DispList, ADList = bide.chemotaxis(reproduction, speciation, SpeciesIDs, Qs, IndIDs, IndID, IndTimeIn, IndX, IndY, width, height, GrowthDict, DispDict, SpColorDict, N_RD, P_RD, C_RD, MaintDict, EnvD, envgrads, nNi, nP, nC, GrowthList, MaintList, N_RList, P_RList, C_RList, DispList, ADList) # Forage #SpeciesIDs, Qs, IndIDs, ID, TimeIn, X, Y, GrowthDict, DispDict, GrowthList, MaintList, N_RList, P_RList, C_RList, DispList, ADList = bide.density_forage(RVals, RX, RY, reproduction, speciation, SpeciesIDs, Qs, IndIDs, IndID, IndTimeIn, IndX, IndY, width, height, GrowthDict, DispDict, SpColorDict, N_RD, P_RD, C_RD, MaintDict, EnvD, envgrads, nNi, nP, nC, GrowthList, MaintList, N_RList, P_RList, C_RList, DispList, ADList) PRODI, PRODN, PRODC, PRODP = 0, 0, 0, 0 p1, TNQ1, TPQ1, TCQ1 = metrics.getprod(Qs) # Consume RTypes, RVals, RIDs, RID, RTimeIn, RExitAge, RX, RY, SpeciesIDs, Qs, IndIDs, IndID, IndTimeIn, IndX, IndY, GrowthList, MaintList, N_RList, P_RList, C_RList, DispList, ADList = bide.consume( RTypes, RVals, RIDs, RID, RX, RY, RTimeIn, RExitAge, SpeciesIDs, Qs, IndIDs, IndID, IndTimeIn, IndX, IndY, width, height, GrowthDict, N_RD, P_RD, C_RD, DispDict, GrowthList, MaintList, N_RList, P_RList, C_RList, DispList, ADList) # Reproduction SpeciesIDs, Qs, IndIDs, ID, TimeIn, X, Y, GrowthDict, DispDict, GrowthList, MaintList, N_RList, P_RList, C_RList, DispList, ADList = bide.reproduce( reproduction, speciation, SpeciesIDs, Qs, IndIDs, IndID, IndTimeIn, IndX, IndY, width, height, GrowthDict, DispDict, SpColorDict, N_RD, P_RD, C_RD, MaintDict, EnvD, envgrads, nNi, nP, nC, GrowthList, MaintList, N_RList, P_RList, C_RList, DispList, ADList) # maintenance SpeciesIDs, X, Y, IndExitAge, IndIDs, IndTimeIn, Qs, GrowthList, MaintList, N_RList, P_RList, C_RList, DispList, ADList = bide.maintenance( SpeciesIDs, IndX, IndY, IndExitAge, SpColorDict, MaintDict, EnvD, IndIDs, IndTimeIn, Qs, GrowthList, MaintList, N_RList, P_RList, C_RList, DispList, ADList) # transition to or from dormancy Sp_IDs, IDs, Qs, GrowthList, MaintList, ADList = bide.transition( SpeciesIDs, IndIDs, Qs, GrowthList, MaintList, ADList) p2, TNQ2, TPQ2, TCQ2 = metrics.getprod(Qs) PRODI = p2 - p1 PRODN = TNQ2 - TNQ1 PRODP = TPQ2 - TPQ1 PRODC = TCQ2 - TCQ1 # disturbance if np.random.binomial(1, disturb * u0) == 1: SpeciesIDs, X, Y, IndExitAge, IndIDs, IndTimeIn, Qs, GrowthList, MaintList, N_RList, P_RList, C_RList, DispList, ADList = bide.decimate( SpeciesIDs, IndX, IndY, IndExitAge, SpColorDict, MaintDict, EnvD, IndIDs, IndTimeIn, Qs, GrowthList, MaintList, N_RList, P_RList, C_RList, DispList, ADList) ax = fig.add_subplot(111) plt.tick_params(axis='both', which='both', bottom='off', top='off', left='off', right='off', labelbottom='off', labelleft='off') if len(SpeciesIDs) >= 1: RAD, splist = bide.GetRAD(SpeciesIDs) else: RAD, splist, N, S = [], [], 0, 0 N, S, tt, rr = sum(RAD), len(RAD), len(TIDs), len(RIDs) numD = ADList.count('d') if N != len(ADList): print N, len(SpeciesIDs), len(ADList) print "N != len(ADList)" sys.exit() if N > 0: Title = [ 'Individuals consume resources, grow, reproduce, and die as they move through the environment. \nAverage speed on the x-axis is ' + str(u0) + ' units per time step. ' + str(len(TExitAge)) + ' tracers have passed through.\nN: ' + str(N) + ', S: ' + str(S) + ', tracers: ' + str(tt) + ', resources: ' + str(rr) + ', ct: ' + str(ct) + ', %dormant: ' + str(round((numD / N) * 100, 2)) ] else: Title = [ 'Individuals consume resources, grow, reproduce, and die as they move through the environment. \nAverage speed on the x-axis is ' + str(u0) + ' units per time step. ' + str(len(TExitAge)) + ' tracers have passed through.\nN: ' + str(N) + ', S: ' + str(S) + ', tracers: ' + str(tt) + ', resources: ' + str(rr) + ', ct: ' + str(ct) + ', %dormant: nan' ] txt.set_text(' '.join(Title)) ax.set_ylim(0, height) ax.set_xlim(0, width) if plot_system == 'yes': ##### PLOTTING THE SYSTEM ############################################## resource_scatImage.remove() tracer_scatImage.remove() Ind_scatImage.remove() colorlist = [] sizelist = [] for i, val in enumerate(SpeciesIDs): if ADList[i] == 'a': colorlist.append('red') elif ADList[i] == 'd': colorlist.append('0.3') sizelist.append(min(Qs[i]) * 1000) resource_scatImage = ax.scatter(RX, RY, s=RVals * 100, c='w', edgecolor='SpringGreen', lw=0.6, alpha=0.3) Ind_scatImage = ax.scatter(IndX, IndY, s=sizelist, c=colorlist, edgecolor='0.2', lw=0.2, alpha=0.9) tracer_scatImage = ax.scatter(TX, TY, s=200, c='r', marker='*', lw=0.0, alpha=0.6) Ns.append(N) if N == 0 and BurnIn == 'not done': Ns = [Ns[-1]] # only keep the most recent N value BurnIn = 'done' if ct > 200 and BurnIn == 'not done': if len(Ns) > 100: AugmentedDickeyFuller = sta.adfuller(Ns) val, p = AugmentedDickeyFuller[0:2] if p >= 0.05: Ns.pop(0) elif p < 0.05 or isnan(p) == True: BurnIn = 'done' Ns = [Ns[-1]] # only keep the most recent N value if ct > 300 and BurnIn == 'not done': Ns = [Ns[-1]] # only keep the most recent N value BurnIn = 'done' if BurnIn == 'done': PRODIs.append(PRODI) PRODNs.append(PRODN) PRODPs.append(PRODP) PRODCs.append(PRODC) if len(RExitAge) > 0: RTAUs.append(mean(RExitAge)) if len(IndExitAge) > 0: INDTAUs.append(mean(IndExitAge)) if len(TExitAge) > 0: TTAUs.append(mean(TExitAge)) # Examining the resource RAD if len(RTypes) > 0: RRAD, Rlist = bide.GetRAD(RTypes) RDens = len(RTypes) / (height * width) RDiv = float(metrics.Shannons_H(RRAD)) RRich = len(Rlist) RDENs.append(RDens) RDIVs.append(RDiv) RRICHs.append(RRich) # Number of tracers, resource particles, and individuals T, R, N = len(TIDs), len(RIDs), len(SpeciesIDs) Ts.append(T) Rs.append(R) Ss.append(S) if N >= 1: if R >= 1: q = min([10, R]) #avg_dist = spatial.avg_dist(X, RX, Y, RY, q) avg_dist = spatial.nearest_neighbor(X, RX, Y, RY, q) AVG_DIST.append(avg_dist) spD = DispDict.values() spM = MaintDict.values() spG = GrowthDict.values() SpecDisp.append(mean(spD)) SpecMaint.append(mean(spM)) SpecGrowth.append(mean(spG)) RAD, splist = bide.GetRAD(SpeciesIDs) RAD, splist = zip(*sorted(zip(RAD, splist), reverse=True)) RAD = list(RAD) S = len(RAD) Ss.append(S) # Evenness, Dominance, and Rarity measures Ev = metrics.e_var(RAD) EVs.append(Ev) ES = metrics.e_simpson(RAD) ESs.append(ES) if len(Ns) == 1: splist2 = list(splist) if len(Ns) > 1: wt = metrics.WhittakersTurnover(splist, splist2) jc = metrics.jaccard(splist, splist2) so = metrics.sorensen(splist, splist2) splist2 = list(splist) WTs.append(wt) Jcs.append(jc) Sos.append(so) Nm, BP = [max(RAD), Nm / N] NMAXs.append(Nm) BPs.append(BP) SD = metrics.simpsons_dom(RAD) SDs.append(SD) sk = stats.skew(RAD) SKs.append(sk) Gs.append(mean(GrowthList)) Ms.append(mean(MaintList)) Ds.append(mean(DispList)) numD = ADList.count('d') ADs.append(numD / len(ADList)) Nmeans = [sum(x) / len(x) for x in zip(*N_RList)] NRs.append(mean(Nmeans)) Pmeans = [sum(x) / len(x) for x in zip(*P_RList)] PRs.append(mean(Pmeans)) Cmeans = [sum(x) / len(x) for x in zip(*C_RList)] CRs.append(mean(Cmeans)) #process = psutil.Process(os.getpid()) #mem = round(process.get_memory_info()[0] / float(2 ** 20), 1) # return the memory usage in MB if len(Ns) > 100: t = time.clock() - t #print sim, ' N:', int(round(mean(Ns))), 'S:', int(round(mean(Ss))), 'WT:', round(mean(WTs),2), ': flow:', u0, 'time:', round(t,1), 'seconds', ': Ttaus:',round(mean(TTimeIn)), round(mean(TExitAge)), ': Etau:', round(width/u0) #' MB:',int(round(mem)), 'p-val =', round(p,3) #print sim, ' N:', int(round(mean(Ns))), 'S:', int(round(mean(Ss))), ': flow:', u0, 'time:', round(t,1), 'seconds', ' height:', str(height), ' Avg dist:', round(mean(AVG_DIST),3), ' f(dormant):',round(mean(ADs),3) print sim, ' N:', int(round(mean(Ns))), 'S:', int(round( mean(Ss))), ' flow:', u0, 'time:', round( t, 1), 'Ttaus:', round(mean(TExitAge), 2), ': Etau:', round( (width - 1) / u0, 2), 'dormant:', round(mean(ADs), 3) t = time.clock() SString = str(splist).strip('()') RADString = str(RAD).strip('()') RADString = str(RAD).strip('[]') IndRTD = str(IndExitAge).strip('[]') TRTD = str(TExitAge).strip('[]') RRTD = str(RExitAge).strip('[]') OUT1 = open(GenPath + 'examples/SimData.csv', 'a') OUT2 = open(GenPath + 'examples/RADs.csv', 'a') OUT3 = open(GenPath + 'examples/Species.csv', 'a') OUT4 = open(GenPath + 'examples/IndRTD.csv', 'a') OUT5 = open(GenPath + 'examples/TracerRTD.csv', 'a') OUT6 = open(GenPath + 'examples/ResRTD.csv', 'a') #TTAUs = np.mean(TExitAge), np.mean(TTimeIn) outlist = [ sim, motion, mean(PRODIs), mean(PRODNs), mean(PRODPs), mean(PRODCs), r, nNi, nP, nC, rmax, gmax, maintmax, dmax, barriers, alpha, seedCom, u0, width - 0.2, height, viscosity, N, m, mean(RTAUs), mean(TExitAge), mean(INDTAUs), mean(RDENs), mean(RDIVs), mean(RRICHs), mean(Ss), mean(ESs), mean(EVs), mean(BPs), mean(SDs), mean(NMAXs), mean(SKs), T, R, speciation, mean(WTs), mean(Jcs), mean(Sos), mean(Gs), mean(Ms), mean(NRs), mean(PRs), mean(CRs), mean(Ds), amp, flux, freq, phase, disturb, mean(SpecGrowth), mean(SpecDisp), mean(SpecMaint), mean(AVG_DIST), mean(ADs) ] outlist = str(outlist).strip('[]') print >> OUT1, outlist print >> OUT2, RADString print >> OUT3, SString print >> OUT4, ct1, ',', sim, ',', IndRTD print >> OUT5, ct1, ',', sim, ',', TRTD print >> OUT6, ct1, ',', sim, ',', RRTD OUT1.close() OUT2.close() OUT3.close() OUT4.close() OUT5.close() OUT6.close() ct1 += 1 ct = 0 Rates = np.roll(Rates, -1, axis=0) u0 = Rates[0] n0, nN, nS, nE, nW, nNE, nNW, nSE, nSW, barrier, rho, ux, uy, bN, bS, bE, bW, bNE, bNW, bSE, bSW = LBM.SetLattice( u0, viscosity, width, height, lefts, bottoms, barriers) u1 = u0 + u0 * (amp * sin(2 * pi * ct * freq + phase)) RDens, RDiv, RRich, S, ES, Ev, BP, SD, Nm, sk, Mu, Maint, ct, IndID, RID, N, ct1, T, R, PRODI, PRODQ = [ 0 ] * 21 ADList, ADs, AVG_DIST, SpecDisp, SpecMaint, SpecGrowth, SpColorList, GrowthList, MaintList, N_RList, P_RList, C_RList, RColorList, DispList = [ list([]) for _ in xrange(14) ] RAD, splist, IndTimeIn, SpeciesIDs, IndX, IndY, IndIDs, Qs, IndExitAge, TX, TY, TExitAge, TIDs, TTimeIn, RX, RY, RIDs, RTypes, RExitAge, RTimeIn, RVals, Gs, Ms, NRs, PRs, CRs, Ds, Ts, Rs, PRODIs, PRODNs, PRODPs, PRODCs, Ns, RTAUs, TTAUs, INDTAUs, RDENs, RDIVs, RRICHs, Ss, ESs, EVs, BPs, SDs, NMAXs, SKs, MUs, MAINTs, WTs, Jcs, Sos, splist2 = [ list([]) for _ in xrange(53) ] p = 0 BurnIn = 'not done' #if u0 in Rates: if u0 == max(Rates): print '\n' sim += 1 if sim > num_sims: print "simplex finished" sys.exit() width, height, alpha, motion, reproduction, speciation, seedCom, m, r, nNi, nP, nC, rmax, gmax, maintmax, dmax, amp, freq, flux, pulse, phase, disturb, envgrads, barriers, Rates = rp.get_rand_params( fixed) for i in range(barriers): lefts.append(np.random.uniform(0.2, .8)) bottoms.append(np.random.uniform(0.1, 0.7)) n0, nN, nS, nE, nW, nNE, nNW, nSE, nSW, barrier, rho, ux, uy, bN, bS, bE, bW, bNE, bNW, bSE, bSW = LBM.SetLattice( u0, viscosity, width, height, lefts, bottoms, barriers) u1 = u0 + u0 * (amp * sin(2 * pi * ct * freq + phase)) SpColorDict, GrowthDict, MaintDict, EnvD, N_RD, P_RD, C_RD, RColorDict, DispDict, EnvD = {}, {}, {}, {}, {}, {}, {}, {}, {}, {} ####################### REPLACE ENVIRONMENT ######################## ax = fig.add_subplot(111)
# training function train_fn = theano.function([input_var, target_var], [loss, train_acc, train_sample_acc, prediction], updates=updates) print "Done" print "Defining and compiling valid functions" valid_prediction = lasagne.layers.get_output(simple_net_output[0], deterministic=True) valid_loss = dice_loss(valid_prediction, target_var) valid_loss = valid_loss.mean() valid_acc, valid_sample_acc = accuracy(valid_prediction, target_var, void_labels) valid_jacc = jaccard(valid_prediction, target_var, n_classes) valid_fn = theano.function( [input_var, target_var], [valid_loss, valid_acc, valid_sample_acc, valid_jacc, valid_prediction]) print "Done" # Training loop parameters plot_results_train = False #from the training set plot_results_valid = False #from the validation set treshold = 0.7 # for extracting the very incorrect labelled samples ratios = [0.80, 0.85, 0.90] #ratios for the per sample accuracy err_train = [] acc_train = []
def evaluating(self, model, dataset, split): """ input: model: (object) pytorch model dataset: (object) dataset split: (str) split of dataset in ['train', 'val', 'test'] return [overall_accuracy, precision, recall, f1-score, jaccard, kappa] """ args = self.args oa, precision, recall, f1, jac, kappa = 0, 0, 0, 0, 0, 0 model.eval() data_loader = DataLoader(dataset, args.batch_size, num_workers=4, shuffle=False) batch_iterator = iter(data_loader) steps = len(dataset) // args.batch_size start = time.time() for step in range(steps): x, y = next(batch_iterator) x = Variable(x, volatile=True) y = Variable(y, volatile=True) if args.cuda: x = x.cuda() y = y.cuda() # calculate pixel accuracy of generator gen_y = model(x) if self.is_multi: gen_y = gen_y[0] oa += metrics.overall_accuracy(gen_y.data, y.data) precision += metrics.precision(gen_y.data, y.data) recall += metrics.recall(gen_y.data, y.data) f1 += metrics.f1_score(gen_y.data, y.data) jac += metrics.jaccard(gen_y.data, y.data) kappa += metrics.kappa(gen_y.data, y.data) _time = time.time() - start if not os.path.exists(os.path.join(Logs_DIR, 'statistic')): os.makedirs(os.path.join(Logs_DIR, 'statistic')) # recording performance of the model nb_samples = steps * args.batch_size basic_info = [ self.date, self.method, self.epoch, self.iter, nb_samples, _time ] basic_info_names = [ 'date', 'method', 'epochs', 'iters', 'nb_samples', 'time(sec)' ] perform = [ round(idx / steps, 3) for idx in [oa, precision, recall, f1, jac, kappa] ] perform_names = [ "overall_accuracy", "precision", "recall", "f1-score", "jaccard", "kappa" ] cur_log = pd.DataFrame([basic_info + perform], columns=basic_info_names + perform_names) # save performance if os.path.exists( os.path.join(Logs_DIR, 'statistic', "{}.csv".format(split))): logs = pd.read_csv( os.path.join(Logs_DIR, 'statistic', "{}.csv".format(split))) else: logs = pd.DataFrame([]) logs = logs.append(cur_log, ignore_index=True) logs.to_csv(os.path.join(Logs_DIR, 'statistic', "{}.csv".format(split)), index=False, float_format='%.3f')
def train( epochs: int, models_dir: Path, x_cities: List[CityData], y_city: List[CityData], mask_dir: Path, ): model = UNet11().cuda() optimizer = Adam(model.parameters(), lr=3e-4) scheduler = ReduceLROnPlateau(optimizer, patience=4, factor=0.25) min_loss = sys.maxsize criterion = nn.BCEWithLogitsLoss() train_data = DataLoader(TrainDataset(x_cities, mask_dir), batch_size=4, num_workers=4, shuffle=True) test_data = DataLoader(TestDataset(y_city, mask_dir), batch_size=6, num_workers=4) for epoch in range(epochs): print(f'Epoch {epoch}, lr {optimizer.param_groups[0]["lr"]}') print(f" Training") losses = [] ious = [] jaccs = [] batch_iterator = enumerate(train_data) model = model.train().cuda() for i, (x, y) in tqdm(batch_iterator): optimizer.zero_grad() x = x.cuda() y = y.cuda() y_real = y.view(-1).float() y_pred = model(x) y_pred_probs = torch.sigmoid(y_pred).view(-1) loss = 0.75 * criterion(y_pred.view( -1), y_real) + 0.25 * dice_loss(y_pred_probs, y_real) iou_ = iou(y_pred_probs.float(), y_real.byte()) jacc_ = jaccard(y_pred_probs.float(), y_real) ious.append(iou_.item()) losses.append(loss.item()) jaccs.append(jacc_.item()) loss.backward() optimizer.step() print( f"Loss: {np.mean(losses)}, IOU: {np.mean(ious)}, jacc: {np.mean(jaccs)}" ) model = model.eval().cuda() losses = [] ious = [] jaccs = [] with torch.no_grad(): batch_iterator = enumerate(test_data) for i, (x, y) in tqdm(batch_iterator): x = x.cuda() y = y.cuda() y_real = y.view(-1).float() y_pred = model(x) y_pred_probs = torch.sigmoid(y_pred).view(-1) loss = 0.75 * criterion(y_pred.view( -1), y_real) + 0.25 * dice_loss(y_pred_probs, y_real) iou_ = iou(y_pred_probs.float(), y_real.byte()) jacc_ = jaccard(y_pred_probs.float(), y_real) ious.append(iou_.item()) losses.append(loss.item()) jaccs.append(jacc_.item()) test_loss = np.mean(losses) print( f"Loss: {np.mean(losses)}, IOU: {np.mean(ious)}, jacc: {np.mean(jaccs)}" ) scheduler.step(test_loss) if test_loss < min_loss: min_loss = test_loss save_model(model, epoch, models_dir / y_city[0].name)