def cluster_changepoints(self):
        """
		Clusters changepoints specified in self.list_of_cp.
		"""

        print "Clustering changepoints..."
        print "L1 ", str(len(self.list_of_cp) / constants.DPGMM_DIVISOR_L1), " ALPHA: ", self.ALPHA_L1

        if constants.REMOTE == 1:
            if self.fit_DPGMM:
                dpgmm = mixture.DPGMM(
                    n_components=int(len(self.list_of_cp) / constants.DPGMM_DIVISOR_L1),
                    covariance_type="diag",
                    n_iter=10000,
                    alpha=self.ALPHA_L1,
                    thresh=1e-4,
                )
            if self.fit_GMM:
                gmm = mixture.GMM(n_components=self.n_components_L1, covariance_type="full", n_iter=5000, thresh=0.01)
        elif constants.REMOTE == 2:
            gmm = mixture.GMM(n_components=self.n_components_L1, covariance_type="full", thresh=0.01)
        else:
            gmm = mixture.GMM(n_components=self.n_components_L1, covariance_type="full")

        if self.fit_GMM:
            gmm.fit(self.changepoints)
            predictions_gmm = gmm.predict(self.changepoints)
            print "L1: Clusters in GMM", len(set(predictions_gmm))
            predictions = predictions_gmm

        if self.fit_DPGMM:
            predictions = []
            while True:
                print "Inside loop"
                dpgmm.fit(self.changepoints)
                predictions = dpgmm.predict(self.changepoints)
                if len(set(predictions)) > 1:
                    break

            print "L1: Clusters in DP-GMM", len(set(predictions))

        for i in range(len(predictions)):
            label = constants.alphabet_map[predictions[i] + 1]
            self.map_cp2cluster[i] = label
            utils.dict_insert_list(label, i, self.map_level1_cp)
            demonstration = self.map_cp2demonstrations[i]
            frm = self.map_cp2frm[i]
            try:
                surgeme = self.map_frm2surgeme[demonstration][frm]
            except KeyError as e:
                print e
                sys.exit()

            utils.print_and_write(("%3d   %s   %s   %3d   %3d\n" % (i, label, demonstration, frm, surgeme)), self.log)
Exemplo n.º 2
0
	def cluster_changepoints(self):
		"""
		Clusters changepoints specified in self.list_of_cp.
		"""

		print "Clustering changepoints..."
		print "L1 ", str(len(self.list_of_cp)/constants.DPGMM_DIVISOR_L1)," ALPHA: ", self.ALPHA_L1

		if constants.REMOTE == 1:
			if self.fit_DPGMM:
				dpgmm = mixture.DPGMM(n_components = int(len(self.list_of_cp)/constants.DPGMM_DIVISOR_L1), covariance_type='diag', n_iter = 10000, alpha = self.ALPHA_L1, thresh= 1e-4)
			if self.fit_GMM:
				gmm = mixture.GMM(n_components = self.n_components_L1, covariance_type='full', n_iter=5000, thresh = 0.01)
		elif constants.REMOTE == 2:
			gmm = mixture.GMM(n_components = self.n_components_L1, covariance_type='full', thresh = 0.01)
		else:
			gmm = mixture.GMM(n_components = self.n_components_L1, covariance_type='full')

		if self.fit_GMM:
			gmm.fit(self.changepoints)
			predictions_gmm = gmm.predict(self.changepoints)
			print "L1: Clusters in GMM",len(set(predictions_gmm))
			predictions = predictions_gmm

		if self.fit_DPGMM:
			predictions = []
			while True:
				print "Inside loop"
				dpgmm.fit(self.changepoints)
				predictions = dpgmm.predict(self.changepoints)
				if len(set(predictions)) > 1:
					break

			print "L1: Clusters in DP-GMM", len(set(predictions))

		for i in range(len(predictions)):
			label = constants.alphabet_map[predictions[i] + 1]
			self.map_cp2cluster[i] = label
			utils.dict_insert_list(label, i, self.map_level1_cp)
			demonstration = self.map_cp2demonstrations[i]
			frm = self.map_cp2frm[i]
			try:
				surgeme = self.map_frm2surgeme[demonstration][frm]
			except KeyError as e:
				print e
				sys.exit()

			utils.print_and_write(("%3d   %s   %s   %3d   %3d\n" % (i, label, demonstration, frm, surgeme)), self.log)
Exemplo n.º 3
0
def post_evaluation_all(metrics_W, metrics_Z, metrics_ZW, file, fname, list_of_demonstrations):

	utils.print_and_write("\nXXXXXXXXXXXX Metric for Kinematics W XXXXXXXXXXXX\n", file)
	list_of_frms_W = evaluate_single_modal(metrics_W, file)

	utils.print_and_write("\nXXXXXXXXXXXX Metric for Visual Z XXXXXXXXXXXX\n", file)
	list_of_frms_Z = evaluate_single_modal(metrics_Z, file)

	utils.print_and_write("\nXXXXXXXXXXXX Metric for Kinematics + Visual ZW XXXXXXXXXXXX\n", file)
	list_of_frms_ZW = evaluate_multi_modal(metrics_ZW, file)

	list_of_dtw_values_W = []
	list_of_dtw_values_Z = []
	list_of_dtw_values_ZW = []
	list_of_dtw_values_W_normalized = []
	list_of_dtw_values_Z_normalized = []
	list_of_dtw_values_ZW_normalized = []
	list_of_lengths = []

	for demonstration in list_of_demonstrations:
		list_of_frms_demonstration_W = list_of_frms_W[demonstration]
		list_of_frms_demonstration_Z = list_of_frms_Z[demonstration]
		list_of_frms_demonstration_ZW = list_of_frms_ZW[demonstration]

		assert len(list_of_frms_demonstration_W) == len(list_of_demonstrations) - 1
		assert len(list_of_frms_demonstration_Z) == len(list_of_demonstrations) - 1
		assert len(list_of_frms_demonstration_ZW) == len(list_of_demonstrations) - 1
		data_W = {}
		data_Z = {}
		data_ZW = {}

		for i in range(len(list_of_frms_demonstration_W)):
			data_W[i] = list_of_frms_demonstration_W[0]
			data_Z[i] = list_of_frms_demonstration_Z[0]
			data_ZW[i] = list_of_frms_demonstration_ZW[0]

		save_fig = constants.PATH_TO_CLUSTERING_RESULTS + demonstration + "_" + fname + "_A.jpg"
		save_fig2 = constants.PATH_TO_CLUSTERING_RESULTS + demonstration + "_" + fname + "_B.jpg"

		dtw_score_W, dtw_score_Z, dtw_score_ZW, dtw_score_W_normalized, dtw_score_Z_normalized, dtw_score_ZW_normalized, length = broken_barh.plot_broken_barh_all(demonstration,
			data_W, data_Z, data_ZW, save_fig, save_fig2)
		list_of_dtw_values_W.append(dtw_score_W)
		list_of_dtw_values_Z.append(dtw_score_Z)
		list_of_dtw_values_ZW.append(dtw_score_ZW)
		list_of_dtw_values_W_normalized.append(dtw_score_W_normalized)
		list_of_dtw_values_Z_normalized.append(dtw_score_Z_normalized)
		list_of_dtw_values_ZW_normalized.append(dtw_score_ZW_normalized)
		list_of_lengths.append(length)

	utils.print_and_write_2("dtw_score_W", np.mean(list_of_dtw_values_W), np.std(list_of_dtw_values_W), file)
	utils.print_and_write_2("dtw_score_Z", np.mean(list_of_dtw_values_Z), np.std(list_of_dtw_values_Z), file)
	utils.print_and_write_2("dtw_score_ZW", np.mean(list_of_dtw_values_ZW), np.std(list_of_dtw_values_ZW), file)
	utils.print_and_write_2("dtw_score_W_normalized", np.mean(list_of_dtw_values_W_normalized), np.std(list_of_dtw_values_W_normalized), file)
	utils.print_and_write_2("dtw_score_Z_normalized", np.mean(list_of_dtw_values_Z_normalized), np.std(list_of_dtw_values_Z_normalized), file)
	utils.print_and_write_2("dtw_score_ZW_normalized", np.mean(list_of_dtw_values_ZW_normalized), np.std(list_of_dtw_values_ZW_normalized), file)
	utils.print_and_write(str(list_of_lengths), file)
Exemplo n.º 4
0
	# list_of_demonstrations = ["011_01", "011_02", "011_03", "011_04", "011_05"]
	list_of_demonstrations = ["plane_6", "plane_7", "plane_8", "plane_9", "plane_10"]

	# list_of_demonstrations = ['Suturing_E001', 'Suturing_E002','Suturing_E003', 'Suturing_E004', 'Suturing_E005']

	combinations = clustering.get_list_of_demo_combinations(list_of_demonstrations)

	feat_fname = args.visual

	metrics_W = []
	metrics_Z = []
	metrics_ZW = []

	log = open(constants.PATH_TO_CLUSTERING_RESULTS + args.fname + ".txt", "wb")

	utils.print_and_write("\nXXXXXXXXXXXX Kinematics W XXXXXXXXXXXX\n", log)
	print "\nXXXXXXXXXXXX Kinematics W XXXXXXXXXXXX\n"

	for i in range(len(combinations)):
		utils.print_and_write("\n----------- Combination #" + str(i) + " -------------\n", log)
		print "\n----------- Combination #" + str(i) + " -------------\n"
		print combinations[i]
		mc = KinematicsClustering(False, list(combinations[i]), args.fname + str(i), log, False, feat_fname)
		metrics_W.append(mc.do_everything())

	utils.print_and_write("\nXXXXXXXXXXXX Visual Z XXXXXXXXXXXX\n", log)
	print "\nXXXXXXXXXXXX Visual Z XXXXXXXXXXXX\n"

	for i in range(len(combinations)):
		utils.print_and_write("\n----------- Combination #" + str(i) + " -------------\n", log)
		print "\n----------- Combination #" + str(i) + " -------------\n"
def post_evaluation(metrics, file, fname, vision_mode):

	mutual_information_1 = []
	normalized_mutual_information_1 = []
	adjusted_mutual_information_1 = []
	homogeneity_1 = []
	precision_1_micro = []
	recall_1_micro = []
	precision_1_macro = []
	recall_1_macro = []
	precision_1_weighted = []
	recall_1_weighted = []
	silhouette_scores_global = []
	silhouette_scores_weighted = []

	dunn1_level_1 = []
	dunn2_level_1 = []
	dunn3_level_1 = []

	list_of_frms = {}

	for elem in metrics:

		mutual_information_1.append(elem[0]["mutual_info_score"])
		normalized_mutual_information_1.append(elem[0]["normalized_mutual_info_score"])
		adjusted_mutual_information_1.append(elem[0]["adjusted_mutual_info_score"])
		homogeneity_1.append(elem[0]["homogeneity_score"])

		silhouette_scores_global.append(elem[1])
		silhouette_scores_weighted.append(elem[6])

		dunn1_level_1.append(elem[2]["level1"])
		dunn2_level_1.append(elem[3]["level1"])
		dunn3_level_1.append(elem[4]["level1"])

		precision_1_micro.append(elem[0]["precision_micro"])
		precision_1_macro.append(elem[0]["precision_macro"])
		precision_1_weighted.append(elem[0]["precision_weighted"])

		recall_1_micro.append(elem[0]["recall_micro"])
		recall_1_macro.append(elem[0]["recall_macro"])
		recall_1_weighted.append(elem[0]["recall_weighted"])

		viz = elem[5]
		for demonstration in viz.keys():
			utils.dict_insert_list(demonstration, viz[demonstration], list_of_frms)

	utils.print_and_write_2("precision_1_micro", np.mean(precision_1_micro), np.std(precision_1_micro), file)
	utils.print_and_write_2("precision_1_macro", np.mean(precision_1_macro), np.std(precision_1_macro), file)
	utils.print_and_write_2("precision_1_weighted", np.mean(precision_1_weighted), np.std(precision_1_weighted), file)

	utils.print_and_write_2("recall_1_micro", np.mean(recall_1_micro), np.std(recall_1_micro), file)
	utils.print_and_write_2("recall_1_macro", np.mean(recall_1_macro), np.std(recall_1_macro), file)
	utils.print_and_write_2("recall_1_weighted", np.mean(recall_1_weighted), np.std(recall_1_weighted), file)

	utils.print_and_write_2("mutual_info", np.mean(mutual_information_1), np.std(mutual_information_1), file)
	utils.print_and_write_2("normalized_mutual_info", np.mean(normalized_mutual_information_1), np.std(normalized_mutual_information_1), file)
	utils.print_and_write_2("adjusted_mutual_info", np.mean(adjusted_mutual_information_1), np.std(adjusted_mutual_information_1), file)
	utils.print_and_write_2("silhouette_scores_global", np.mean(silhouette_scores_global), np.std(silhouette_scores_global), file)
	utils.print_and_write_2("silhouette_scores_weighted", np.mean(silhouette_scores_weighted), np.std(silhouette_scores_weighted), file)

	utils.print_and_write_2("homogeneity", np.mean(homogeneity_1), np.std(homogeneity_1), file)

	utils.print_and_write_2("dunn1", np.mean(dunn1_level_1), np.std(dunn1_level_1), file)
	utils.print_and_write_2("dunn2", np.mean(dunn2_level_1), np.std(dunn2_level_1), file)
	utils.print_and_write_2("dunn3", np.mean(dunn3_level_1), np.std(dunn3_level_1), file)

	list_of_dtw_values = []
	list_of_norm_dtw_values = []
	list_of_lengths = []

	if vision_mode:
		T = constants.N_COMPONENTS_TIME_Z
	else:
		T = constants.N_COMPONENTS_TIME_W

	pickle.dump(list_of_frms, open(constants.PATH_TO_CLUSTERING_RESULTS + fname + "_.p", "wb"))

	for demonstration in list_of_demonstrations:
		try:
			list_of_frms_demonstration = list_of_frms[demonstration]

			data = {}

			for i in range(len(list_of_frms_demonstration)):
				data[i] = [elem[0] for elem in list_of_frms_demonstration[i]]

			dtw_score, normalized_dtw_score, length = broken_barh.plot_broken_barh(demonstration, data,
				constants.PATH_TO_CLUSTERING_RESULTS + demonstration +"_" + fname + ".jpg", T)
			list_of_dtw_values.append(dtw_score)
			list_of_norm_dtw_values.append(normalized_dtw_score)
			list_of_lengths.append(length)
		except:
			print demonstration
			pass

	utils.print_and_write_2("dtw_score", np.mean(list_of_dtw_values), np.std(list_of_dtw_values), file)
	utils.print_and_write_2("dtw_score_normalized", np.mean(list_of_norm_dtw_values), np.std(list_of_norm_dtw_values), file)
	utils.print_and_write(str(list_of_lengths), file)
Exemplo n.º 6
0
def post_evaluation_all(metrics_W, metrics_Z, metrics_ZW, file, fname,
                        list_of_demonstrations):

    utils.print_and_write(
        "\nXXXXXXXXXXXX Metric for Kinematics W XXXXXXXXXXXX\n", file)
    list_of_frms_W = evaluate_single_modal(metrics_W, file)

    utils.print_and_write("\nXXXXXXXXXXXX Metric for Visual Z XXXXXXXXXXXX\n",
                          file)
    list_of_frms_Z = evaluate_single_modal(metrics_Z, file)

    utils.print_and_write(
        "\nXXXXXXXXXXXX Metric for Kinematics + Visual ZW XXXXXXXXXXXX\n",
        file)
    list_of_frms_ZW = evaluate_multi_modal(metrics_ZW, file)

    list_of_dtw_values_W = []
    list_of_dtw_values_Z = []
    list_of_dtw_values_ZW = []
    list_of_dtw_values_W_normalized = []
    list_of_dtw_values_Z_normalized = []
    list_of_dtw_values_ZW_normalized = []
    list_of_lengths = []

    for demonstration in list_of_demonstrations:
        list_of_frms_demonstration_W = list_of_frms_W[demonstration]
        list_of_frms_demonstration_Z = list_of_frms_Z[demonstration]
        list_of_frms_demonstration_ZW = list_of_frms_ZW[demonstration]

        assert len(
            list_of_frms_demonstration_W) == len(list_of_demonstrations) - 1
        assert len(
            list_of_frms_demonstration_Z) == len(list_of_demonstrations) - 1
        assert len(
            list_of_frms_demonstration_ZW) == len(list_of_demonstrations) - 1
        data_W = {}
        data_Z = {}
        data_ZW = {}

        for i in range(len(list_of_frms_demonstration_W)):
            data_W[i] = list_of_frms_demonstration_W[0]
            data_Z[i] = list_of_frms_demonstration_Z[0]
            data_ZW[i] = list_of_frms_demonstration_ZW[0]

        save_fig = constants.PATH_TO_CLUSTERING_RESULTS + demonstration + "_" + fname + "_A.jpg"
        save_fig2 = constants.PATH_TO_CLUSTERING_RESULTS + demonstration + "_" + fname + "_B.jpg"

        dtw_score_W, dtw_score_Z, dtw_score_ZW, dtw_score_W_normalized, dtw_score_Z_normalized, dtw_score_ZW_normalized, length = broken_barh.plot_broken_barh_all(
            demonstration, data_W, data_Z, data_ZW, save_fig, save_fig2)
        list_of_dtw_values_W.append(dtw_score_W)
        list_of_dtw_values_Z.append(dtw_score_Z)
        list_of_dtw_values_ZW.append(dtw_score_ZW)
        list_of_dtw_values_W_normalized.append(dtw_score_W_normalized)
        list_of_dtw_values_Z_normalized.append(dtw_score_Z_normalized)
        list_of_dtw_values_ZW_normalized.append(dtw_score_ZW_normalized)
        list_of_lengths.append(length)

    utils.print_and_write_2("dtw_score_W", np.mean(list_of_dtw_values_W),
                            np.std(list_of_dtw_values_W), file)
    utils.print_and_write_2("dtw_score_Z", np.mean(list_of_dtw_values_Z),
                            np.std(list_of_dtw_values_Z), file)
    utils.print_and_write_2("dtw_score_ZW", np.mean(list_of_dtw_values_ZW),
                            np.std(list_of_dtw_values_ZW), file)
    utils.print_and_write_2("dtw_score_W_normalized",
                            np.mean(list_of_dtw_values_W_normalized),
                            np.std(list_of_dtw_values_W_normalized), file)
    utils.print_and_write_2("dtw_score_Z_normalized",
                            np.mean(list_of_dtw_values_Z_normalized),
                            np.std(list_of_dtw_values_Z_normalized), file)
    utils.print_and_write_2("dtw_score_ZW_normalized",
                            np.mean(list_of_dtw_values_ZW_normalized),
                            np.std(list_of_dtw_values_ZW_normalized), file)
    utils.print_and_write(str(list_of_lengths), file)
Exemplo n.º 7
0
if not save_seg:
    save_raw = save_stitched = False

classes = tuple(range(n_classes))

with tf.Session(config=config) as sess:
    init = tf.global_variables_initializer()
    init.run()

    if not os.path.isdir(save_path):
        os.makedirs(save_path)
    log_fname = os.path.join(save_path, 'log_{:s}.txt'.format(getDateTime()))

    if save_seg:
        print_and_write('Saving segmentation results to: {}'.format(save_path),
                        log_fname)
    else:
        print_and_write('Not saving segmentation results', log_fname)

    print_and_write('Saving log to: {}'.format(log_fname), log_fname)

    save_path_raw = os.path.join(save_path, 'raw')
    if save_raw:
        print_and_write('Saving raw labels to: {}\n'.format(save_path_raw))
        if not os.path.isdir(save_path_raw):
            os.makedirs(save_path_raw)

    if weights_path.endswith('/'):
        ckpt_path = tf.train.latest_checkpoint(weights_path)
    else:
        ckpt_path = weights_path
Exemplo n.º 8
0
def validate(val_loader,
             model,
             criterion,
             epoch=None,
             writer=None,
             pre_transformation=None,
             logfile=None,
             summaryfile=None,
             force_avgpool=False,
             last_conv_layer_PCA=None,
             unfold=None,
             multi_scale=False,
             whitening=None,
             compare_with_fista=False):
    batch_time = AverageMeter()
    data_time = AverageMeter()
    pre_transformation_time = AverageMeter()
    losses = AverageMeter()
    top1 = AverageMeter()
    top5 = AverageMeter()

    model.eval()

    with torch.no_grad():
        end = time.time()
        for i, (input, target) in enumerate(val_loader):

            input = input.cuda()

            if pre_transformation is not None:
                input = pre_transformation(input)

            target = target.cuda(args.gpu, non_blocking=True)

            output = model(input)

            loss = criterion(output, target)

            # measure accuracy and record loss
            acc1, acc5 = accuracy(output, target, topk=(1, 5))
            losses.update(loss.item(), input.size(0))
            top1.update(acc1[0], input.size(0))
            top5.update(acc5[0], input.size(0))

            # measure elapsed time
            batch_time.update(time.time() - end)
            end = time.time()

            if i % args.print_freq == 0:
                print_and_write(
                    'Validation Epoch: [{0}][{1}/{2}]\t'
                    'Time {batch_time.val:.1f} ({batch_time.avg:.1f})\t'
                    'Data {data_time.val:.1f} ({data_time.avg:.1f})\t'
                    'preproc. {pre_transformation_time.val:.1f}({pre_transformation_time.avg:.1f})\t'
                    'Tot. Loss {loss.val:.2f} ({loss.avg:.2f})\t'
                    'Acc@1 {top1.val:.1f} ({top1.avg:.1f})\t'
                    'Acc@5 {top5.val:.1f} ({top5.avg:.1f})\t'.format(
                        epoch,
                        i,
                        len(val_loader),
                        batch_time=batch_time,
                        data_time=data_time,
                        pre_transformation_time=pre_transformation_time,
                        loss=losses,
                        top1=top1,
                        top5=top5),
                    logfile=logfile)

        print_and_write(
            'Validation Epoch {}, * Acc@1 {:.2f} Acc@5 {:.2f}'.format(
                epoch, top1.avg, top5.avg),
            logfile=logfile)

    return top1.avg, top5.avg
Exemplo n.º 9
0
def main():
    global args, best_acc1, best_acc5, best_epoch_acc1, best_epoch_acc5
    args = parser.parse_args()

    logs_dir = args.logs_dir
    if not os.path.exists(logs_dir):
        os.makedirs(logs_dir)

    checkpoint_savedir = args.checkpoint_savedir
    if not os.path.exists(checkpoint_savedir):
        os.makedirs(checkpoint_savedir)

    logfile = os.path.join(
        logs_dir, 'training_{}_b_{}_lrfreq_{}.log'.format(
            args.arch, args.batch_size, args.learning_rate_adjust_frequency))

    summaryfile = os.path.join(logs_dir, 'summary_file.txt')

    checkpoint_savefile = os.path.join(
        checkpoint_savedir,
        '{}_batchsize_{}_lrfreq_{}_traincropsize_{}_fft{}.pth.tar'.format(
            args.arch, args.batch_size, args.learning_rate_adjust_frequency,
            args.train_cropsize, args.scattering_fft))
    best_checkpoint_savefile = os.path.join(
        checkpoint_savedir,
        '{}_batchsize_{}_lrfreq_{}_traincropsize_{}_best_fft{}.pth.tar'.format(
            args.arch, args.batch_size, args.learning_rate_adjust_frequency,
            args.train_cropsize, args.scattering_fft))

    if args.seed is not None:
        random.seed(args.seed)
        torch.manual_seed(args.seed)
        cudnn.deterministic = True
        warnings.warn('You have chosen to seed training. '
                      'This will turn on the CUDNN deterministic setting, '
                      'which can slow down your training considerably! '
                      'You may see unexpected behavior when restarting '
                      'from checkpoints.')

    if args.gpu is not None:
        warnings.warn('You have chosen a specific GPU. This will completely '
                      'disable data parallelism.')

    args.distributed = args.world_size > 1

    if args.distributed:
        dist.init_process_group(backend=args.dist_backend,
                                init_method=args.dist_url,
                                world_size=args.world_size)

    dictionary = None

    max_order = 2 if args.scattering_order2 else 1
    print_and_write("=> Scattering transform order {}, J {}".format(
        max_order, args.scattering_J),
                    logfile=logfile)
    pre_transformation = torch.nn.DataParallel(
        Scattering2D(J=args.scattering_J,
                     shape=(args.train_cropsize, args.train_cropsize),
                     max_order=max_order,
                     oversampling=args.scattering_oversampling)).cuda()
    if args.arch == 'bagnetscattering':
        arch_log = "=> creating model 'bagnetscattering' oversampling {} width {}, {} iterations".format(
            args.scattering_oversampling, args.layer_width, args.n_iterations)
        model = BagNetScattering(J=args.scattering_J,
                                 N=args.train_cropsize,
                                 layer_width=args.layer_width,
                                 order2=args.scattering_order2,
                                 n_iterations=args.n_iterations,
                                 first_layer_kernel_size=args.conv1,
                                 skip_stride=args.stride1)
    if args.arch == 'scatteringlinear':
        arch_log = "=> creating model 'scatteringlinear' oversampling {} fft {}".format(
            args.scattering_oversampling, args.scattering_fft)
        J = args.scattering_J
        oversampling = args.scattering_oversampling
        if oversampling == 0:
            n_space = int(224 // 2**J)
        elif oversampling == 1:
            n_space = int(224 // 2**(J - 1)) + 2
        model = ScatteringLinear(n_space=n_space,
                                 J=args.scattering_J,
                                 order2=args.scattering_order2,
                                 use_fft=args.scattering_fft)

    else:
        print("=> unknown model '{}'".format(args.arch))
        return

    print_and_write(arch_log, logfile=logfile)

    print_and_write(
        "=> learning rate {}, lr thetas {}, {} epochs, decay frequency {}".
        format(args.lr, args.lr_thetas, args.epochs,
               args.learning_rate_adjust_frequency),
        logfile=logfile)

    model = torch.nn.DataParallel(model).cuda()

    # define loss function (criterion)
    criterion = nn.CrossEntropyLoss().cuda(args.gpu)

    # define the optimizer
    if args.arch == 'bagscatnetlistaproj':
        params_list = [
            {
                'params': model.module.proj.parameters()
            },
            {
                'params': model.module.skip.parameters()
            },
            {
                'params': model.module.conv.parameters()
            },
            {
                'params': model.module.fc.parameters()
            },
            {
                'params': model.module.bn.parameters()
            },
            {
                'params': model.module.bn0.parameters()
            },
            {
                'params': model.module.thetas,
                'lr': 0.,
                'name': 'thetas',
                'weight_decay': 0
            },
        ]
    elif args.arch == 'bagscatnetlista2proj':
        params_list = [
            {
                'params': model.module.proj.parameters()
            },
            {
                'params': model.module.WT.parameters()
            },
            {
                'params': model.module.D.parameters()
            },
            {
                'params': model.module.fc.parameters()
            },
            {
                'params': model.module.bn.parameters()
            },
            {
                'params': model.module.bn0.parameters()
            },
            {
                'params': model.module.thetas,
                'lr': 0.,
                'name': 'thetas',
                'weight_decay': 0
            },
        ]
    if args.arch == 'bagscatnetistaproj':
        params_list = [
            {
                'params': model.module.bn0.parameters()
            },
            {
                'params': model.module.proj.parameters()
            },
            {
                'params': model.module.dictionary
            },
            {
                'params': model.module.fc.parameters()
            },
            {
                'params': model.module.bn.parameters()
            },
            {
                'params': model.module.thetas,
                'lr': 0.,
                'name': 'thetas',
                'weight_decay': 0
            },
        ]
    if args.arch == 'bagscatnetslistaproj':
        params_list = [
            {
                'params': model.module.bn0.parameters()
            },
            {
                'params': model.module.proj.parameters()
            },
            {
                'params': model.module.dictionary
            },
            {
                'params': model.module.fc.parameters()
            },
            {
                'params': model.module.bn.parameters()
            },
            {
                'params': model.module.theta,
                'lr': 0.,
                'name': 'thetas',
                'weight_decay': 0
            },
            {
                'params': model.module.gammas,
                'lr': 0.,
                'name': 'thetas',
                'weight_decay': 0
            },
        ]
    else:
        params_list = model.parameters()

    optimizer = torch.optim.SGD(params_list,
                                lr=args.lr,
                                momentum=args.momentum,
                                weight_decay=args.weight_decay)

    # optionally resume from a checkpoint
    if args.resume:
        if os.path.isfile(args.resume):
            print("=> loading checkpoint '{}'".format(args.resume))
            checkpoint = torch.load(args.resume)
            if args.start_epoch == 0:
                args.start_epoch = checkpoint['epoch']
            best_acc1 = checkpoint['best_acc1']
            model.load_state_dict(checkpoint['state_dict'])
            if not args.new_optimizer:
                optimizer.load_state_dict(checkpoint['optimizer'])
            print("=> loaded checkpoint '{}' (epoch {})".format(
                args.resume, checkpoint['epoch']))
        else:
            print("=> no checkpoint found at '{}'".format(args.resume))

    if args.inspect_model:
        pdb.set_trace()

    cudnn.benchmark = True

    # Data loading code

    if args.nb_classes < 1000:
        if args.random_seed is not None:
            torch.manual_seed(args.random_seed)

        train_indices = list(
            np.load('utils_sampling/imagenet_train_class_indices.npy'))
        val_indices = list(
            np.load('utils_sampling/imagenet_val_class_indices.npy'))
        classes_names = torch.load('utils_sampling/labels_dict')
        if args.class_indices is not None:
            class_indices = torch.load(args.class_indices)
        else:
            perm = torch.randperm(1000)
            class_indices = perm[:args.nb_classes].tolist()
        train_indices_full = [
            x for i in range(len(class_indices))
            for x in range(train_indices[class_indices[i]], train_indices[
                class_indices[i] + 1])
        ]
        val_indices_full = [
            x for i in range(len(class_indices))
            for x in range(val_indices[class_indices[i]], val_indices[
                class_indices[i] + 1])
        ]
        classes_indices_file = os.path.join(logs_dir,
                                            'classes_indices_selected')
        classes_names_file = os.path.join(logs_dir, 'classes_names_selected')
        classes_file_txt = os.path.join(logs_dir, 'classes_selected.txt')
        f = open(classes_file_txt, 'w')
        selected_classes_names = [classes_names[i] for i in class_indices]
        torch.save(class_indices, classes_indices_file)
        torch.save(selected_classes_names, classes_names_file)
        print('\nSelected {} classes indices:  {}\n'.format(
            args.nb_classes, class_indices))
        print('Selected {} classes names:  {}\n'.format(
            args.nb_classes, selected_classes_names))
        if args.random_seed is not None:
            print('Random seed used {}\n'.format(args.random_seed))

        print('Selected {} classes indices:  {}\n'.format(
            args.nb_classes, class_indices),
              file=f)
        print('Selected {} classes names:  {}\n'.format(
            args.nb_classes, selected_classes_names),
              file=f)
        if args.random_seed is not None:
            print('Random seed used {}\n'.format(args.random_seed), file=f)
        f.close()

    traindir = os.path.join(args.data, 'train')
    valdir = os.path.join(args.data, 'val')
    normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],
                                     std=[0.229, 0.224, 0.225])

    train_dataset = datasets.ImageFolder(
        traindir,
        transforms.Compose([
            transforms.RandomResizedCrop(args.train_cropsize),
            # transforms.Resize(args.val_resize),
            # transforms.RandomCrop(args.train_cropsize),
            transforms.RandomHorizontalFlip(),
            transforms.ToTensor(),
            normalize,
        ]))

    if args.nb_classes < 1000:
        train_dataset = torch.utils.data.Subset(train_dataset,
                                                train_indices_full)

    if args.distributed:
        train_sampler = torch.utils.data.distributed.DistributedSampler(
            train_dataset)
    else:
        train_sampler = None

    train_loader = torch.utils.data.DataLoader(train_dataset,
                                               batch_size=args.batch_size,
                                               shuffle=(train_sampler is None),
                                               num_workers=args.workers,
                                               pin_memory=True,
                                               sampler=train_sampler)

    val_dataset = datasets.ImageFolder(
        valdir,
        transforms.Compose([
            transforms.Resize(args.val_resize),
            transforms.CenterCrop(args.train_cropsize),
            transforms.ToTensor(),
            normalize,
        ]))

    if args.nb_classes < 1000:
        val_dataset = torch.utils.data.Subset(val_dataset, val_indices_full)

    val_loader = torch.utils.data.DataLoader(val_dataset,
                                             batch_size=args.batch_size,
                                             shuffle=False,
                                             num_workers=args.workers,
                                             pin_memory=True)

    if args.evaluate:
        validate(val_loader,
                 model,
                 criterion,
                 pre_transformation=pre_transformation,
                 compare_with_fista=args.compare_with_fista)
        return

    for epoch in range(args.start_epoch, args.epochs):
        if args.distributed:
            train_sampler.set_epoch(epoch)

        adjust_learning_rate(
            optimizer,
            epoch,
            adjust_frequency=args.learning_rate_adjust_frequency)

        # train for one epoch
        train(train_loader,
              model,
              criterion,
              optimizer,
              epoch,
              pre_transformation=pre_transformation,
              logfile=logfile,
              dictionary=dictionary)

        # evaluate on validation set
        acc1, acc5 = validate(val_loader,
                              model,
                              criterion,
                              epoch=epoch,
                              pre_transformation=pre_transformation,
                              logfile=logfile)

        # remember best acc@1 and save checkpoint
        is_best = acc1 > best_acc1
        best_acc1 = max(acc1, best_acc1)
        if is_best:
            best_epoch_acc1 = epoch
        if acc5 > best_acc5:
            best_acc5 = acc5
            best_epoch_acc5 = epoch
        save_checkpoint(
            {
                'epoch': epoch + 1,
                'arch': args.arch,
                'state_dict': model.state_dict(),
                'best_acc1': best_acc1,
                'optimizer': optimizer.state_dict(),
            },
            is_best,
            checkpoint_filename=checkpoint_savefile,
            best_checkpoint_filename=best_checkpoint_savefile)

    sum_up_log = "Best top 1 accuracy {:.2f} at epoch {}, best top 5 accuracy {:.2f} at epoch {}".format(
        best_acc1, best_epoch_acc1, best_acc5, best_epoch_acc5)
    print_and_write(sum_up_log, logfile=logfile)
    print_and_write(sum_up_log, logfile=summaryfile)
Exemplo n.º 10
0
def validate(val_loader, model, criterion, epoch, args, logfile, summaryfile, writer):
    batch_time = AverageMeter('Time', ':.1f')
    data_time = AverageMeter('Data', ':.1f')
    losses = AverageMeter('Loss', ':.2f')
    top1 = AverageMeter('Acc@1', ':.1f')
    top5 = AverageMeter('Acc@5', ':.1f')
    progress = ProgressMeter(
        len(val_loader),
        [batch_time, data_time, losses, top1, top5],
        prefix="Validation Epoch: [{}]".format(epoch))

    if args.arch in ['sparsescatnet', 'sparsescatnetw']:
        rec_losses_rel = AverageMeter('Rec. losses', ':.2f')
        progress.add(rec_losses_rel)
        sparsities = AverageMeterTensor(args.n_iterations)
        support_sizes = AverageMeterTensor(args.n_iterations)
        support_diffs = AverageMeterTensor(args.n_iterations)

    # switch to evaluate mode
    model.eval()

    with torch.no_grad():
        end = time.time()
        for i, (input, target) in enumerate(val_loader):
            # measure data loading time
            data_time.update(time.time() - end)

            input = input.cuda(non_blocking=True)
            target = target.cuda(non_blocking=True)

            # compute output
            if args.arch in ['sparsescatnet', 'sparsescatnetw']:
                output, _, sparsity, support_size, support_diff, rec_loss_rel = model(input)
            else:
                output = model(input)

            loss = criterion(output, target)

            # measure accuracy and record loss
            acc1, acc5 = accuracy(output, target, topk=(1, 5))
            losses.update(loss.item(), input.size(0))
            top1.update(acc1[0], input.size(0))
            top5.update(acc5[0], input.size(0))

            # Record useful indicators for ISTC
            if args.arch in ['sparsescatnet', 'sparsescatnetw']:
                rec_losses_rel.update(rec_loss_rel.mean().item(), input.size(0))
                if len(sparsity) > args.n_iterations:  # multi-GPU
                    sparsities.update(sparsity.reshape(-1, args.n_iterations).mean(dim=0).cpu().numpy(), input.size(0))
                    support_sizes.update(support_size.reshape(-1, args.n_iterations).mean(dim=0).cpu().numpy(),
                                         input.size(0))
                    support_diffs.update(support_diff.reshape(-1, args.n_iterations).mean(dim=0).cpu().numpy(),
                                         input.size(0))

                else:
                    sparsities.update(sparsity.cpu().numpy(), input.size(0))
                    support_sizes.update(support_size.cpu().numpy(), input.size(0))
                    support_diffs.update(support_diff.cpu().numpy(), input.size(0))

            # measure elapsed time
            batch_time.update(time.time() - end)
            end = time.time()

            if i % args.print_freq == 0:
                print_and_write('\n', logfile)
                progress.display(i, logfile)

                # Print ISTC model information
                if args.arch in ['sparsescatnet', 'sparsescatnetw']:
                    print_and_write('Sparsity {}\t Support size {}\t Support diff {}'
                                    .format(
                        np.array2string(sparsities.avg, formatter={'float_kind': lambda x: "%.1f" % x}),
                        np.array2string(support_sizes.avg, formatter={'float_kind': lambda x: "%.0f" % x}),
                        np.array2string(support_diffs.avg, formatter={'float_kind': lambda x: "%.0f" % x})),
                                    logfile)

                    lambda_0 = model.module.istc.lambda_0.data.clone().cpu()
                    lambdas = model.module.istc.lambdas.data.clone().cpu().numpy()
                    lambda_star = model.module.istc.lambda_star.data.clone().cpu()
                    gamma = model.module.istc.gamma.data.clone().cpu()

                    gram = torch.matmul(model.module.istc.w_weight.data[..., 0, 0].t(),
                                        model.module.istc.dictionary_weight.data[..., 0, 0]).cpu().numpy()

                    gram = np.abs(gram)
                    for k in range(args.dictionary_size):
                        gram[k, k] = 0

                    max_coherence = np.max(gram)
                    median_coherence = np.median(gram)

                    print_and_write('Lambda_0: {:.3f}'.format(lambda_0.item()), logfile)
                    with np.printoptions(precision=2, suppress=True):
                        print_and_write('Lambdas: {}'.format(lambdas.reshape(args.n_iterations - 1)), logfile)
                    print_and_write('Lambda_star: {:.3f}'.format(lambda_star.item()), logfile)
                    print_and_write('Gamma: {:.2f}'.format(gamma.item()), logfile)
                    print_and_write("Max coherence {:.3f}, median coherence {:.3f}".format(max_coherence,
                                                                                           median_coherence),
                                    logfile)

    # Print statistics summary
    print_and_write('\n Validation Epoch {}, * Acc@1 {:.2f} Acc@5 {:.2f}'.format(epoch, top1.avg, top5.avg), logfile)

    if args.arch in ['sparsescatnet', 'sparsescatnetw']:
        print_and_write('Rec loss rel {:.2f}\t Sparsity {}\t Support size {}\t Support diff {}'.
                        format(rec_losses_rel.avg, np.array2string(sparsities.avg, formatter={
            'float_kind': lambda x: "%.1f" % x}), np.array2string(support_sizes.avg, formatter={
            'float_kind': lambda x: "%.0f" % x}), np.array2string(support_diffs.avg, formatter={
            'float_kind': lambda x: "%.0f" % x})), logfile)

        count = 0
        for i in range(args.dictionary_size):
            if model.module.istc.dictionary_weight.data[:, i].norm(p=2) < 0.99 or \
                    model.module.istc.dictionary_weight.data[:, i].norm(p=2) > 1.01:
                count += 1

        if count == 0:
            print_and_write("Dictionary atoms well normalized", logfile)
        else:
            print_and_write("{} dictionary atoms not well normalized".format(count), logfile)

        gram = torch.matmul(model.module.istc.w_weight.data[..., 0, 0].t(),
                            model.module.istc.dictionary_weight.data[..., 0, 0]).cpu().numpy()

        if args.arch == 'sparsescatnetw':
            count = 0
            for i in range(args.dictionary_size):
                if gram[i, i] < 0.99 or gram[i, i] > 1.01:
                    count += 1
            if count == 0:
                print_and_write("W^T D diagonal elements well equal to 1", logfile)
            else:
                print_and_write("{} W^T D diagonal elements not equal to 1".format(count),
                                logfile)

        gram = np.abs(gram)
        for i in range(args.dictionary_size):
            gram[i, i] = 0

        print_and_write("Max coherence {:.3f}, median coherence {:.3f}".
                        format(np.max(gram), np.median(gram)), logfile)

    if (epoch % args.learning_rate_adjust_frequency) == (args.learning_rate_adjust_frequency-1):
        print_and_write('Validation Epoch {} before learning rate adjustment n° {}, * Acc@1 {:.2f} Acc@5 {:.2f}'
                        .format(epoch, epoch // args.learning_rate_adjust_frequency+1, top1.avg, top5.avg), summaryfile)
        if args.arch in ['sparsescatnet', 'sparsescatnetw']:
            print_and_write('Rec loss rel {:.2f}\t Sparsity {}\t Support size {}\t Support diff {}'.
                            format(rec_losses_rel.avg, np.array2string(sparsities.avg, formatter={
                'float_kind': lambda x: "%.1f" % x}), np.array2string(support_sizes.avg, formatter={
                'float_kind': lambda x: "%.0f" % x}), np.array2string(support_diffs.avg, formatter={
                'float_kind': lambda x: "%.0f" % x})), summaryfile)

    if epoch == 0:
        print_and_write('First epoch, * Acc@1 {:.2f} Acc@5 {:.2f}'.format(top1.avg, top5.avg), summaryfile)
        if args.arch in ['sparsescatnet', 'sparsescatnetw']:
            print_and_write('Rec loss rel {:.2f}\t Sparsity {}\t Support size {}\t Support diff {}'.
                            format(rec_losses_rel.avg, np.array2string(sparsities.avg, formatter={
                'float_kind': lambda x: "%.1f" % x}), np.array2string(support_sizes.avg, formatter={
                'float_kind': lambda x: "%.0f" % x}), np.array2string(support_diffs.avg, formatter={
                'float_kind': lambda x: "%.0f" % x})), summaryfile)

    if epoch == args.epochs-1:
        print_and_write('Final epoch {}, * Acc@1 {:.2f} Acc@5 {:.2f}'.format(epoch, top1.avg, top5.avg), summaryfile)
        if args.arch in ['sparsescatnet', 'sparsescatnetw']:
            print_and_write('Rec loss rel {:.2f}\t Sparsity {}\t Support size {}\t Support diff {}'.
                            format(rec_losses_rel.avg, np.array2string(sparsities.avg, formatter={
                'float_kind': lambda x: "%.1f" % x}), np.array2string(support_sizes.avg, formatter={
                'float_kind': lambda x: "%.0f" % x}), np.array2string(support_diffs.avg, formatter={
                'float_kind': lambda x: "%.0f" % x})), summaryfile)

    if writer is not None:
        writer.add_scalar('top5_val', top5.avg, global_step=epoch)
        writer.add_scalar('top1_val', top1.avg, global_step=epoch)

    return top1.avg, top5.avg
Exemplo n.º 11
0
def train(train_loader, model, criterion, optimizer, epoch, args, logfile, writer):
    batch_time = AverageMeter('Time', ':.1f')
    data_time = AverageMeter('Data', ':.1f')
    losses = AverageMeter('Loss', ':.2f')
    top1 = AverageMeter('Acc@1', ':.1f')
    top5 = AverageMeter('Acc@5', ':.1f')
    progress = ProgressMeter(
        len(train_loader),
        [batch_time, data_time, losses, top1, top5],
        prefix="Epoch: [{}]".format(epoch))

    if args.arch in ['sparsescatnet', 'sparsescatnetw']:
        rec_losses_rel = AverageMeter('Rec. losses', ':.2f')
        progress.add(rec_losses_rel)
        sparsities = AverageMeterTensor(args.n_iterations)
        support_sizes = AverageMeterTensor(args.n_iterations)
        support_diffs = AverageMeterTensor(args.n_iterations)


    # switch to train mode
    model.train()

    end = time.time()
    for i, (input, target) in enumerate(train_loader):
        # measure data loading time
        data_time.update(time.time() - end)

        input = input.cuda(non_blocking=True)
        target = target.cuda(non_blocking=True)

        # compute output
        if args.arch in ['sparsescatnet', 'sparsescatnetw']:
            output, lambda_0_max_batch, sparsity, support_size, support_diff, rec_loss_rel = model(input)
        else:
            output = model(input)

        loss = criterion(output, target)

        # measure accuracy and record loss
        acc1, acc5 = accuracy(output, target, topk=(1, 5))
        losses.update(loss.item(), input.size(0))
        top1.update(acc1[0], input.size(0))
        top5.update(acc5[0], input.size(0))

        # Record useful indicators for ISTC
        if args.arch in ['sparsescatnet', 'sparsescatnetw']:
            lambda_0_max = lambda_0_max_batch.mean().item()
            rec_losses_rel.update(rec_loss_rel.mean().item(), input.size(0))
            if len(sparsity) > args.n_iterations:  # multi-GPU
                sparsities.update(sparsity.reshape(-1, args.n_iterations).mean(dim=0).cpu().numpy(), input.size(0))
                support_sizes.update(support_size.reshape(-1, args.n_iterations).mean(dim=0).cpu().numpy(), input.size(0))
                support_diffs.update(support_diff.reshape(-1, args.n_iterations).mean(dim=0).cpu().numpy(), input.size(0))

            else:
                sparsities.update(sparsity.cpu().numpy(), input.size(0))
                support_sizes.update(support_size.cpu().numpy(), input.size(0))
                support_diffs.update(support_diff.cpu().numpy(), input.size(0))


        # compute gradient and do SGD step
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()

        # measure elapsed time
        batch_time.update(time.time() - end)
        end = time.time()

        if i % args.print_freq == 0:
            print_and_write('\n', logfile)
            progress.display(i, logfile)

            # Print ISTC model information
            if args.arch in ['sparsescatnet', 'sparsescatnetw']:
                print_and_write('Sparsity {}\t Support size {}\t Support diff {}'
                                .format(np.array2string(sparsities.avg, formatter={'float_kind':lambda x: "%.1f" % x}),
                                        np.array2string(support_sizes.avg, formatter={'float_kind': lambda x: "%.0f" % x}),
                                        np.array2string(support_diffs.avg, formatter={'float_kind': lambda x: "%.0f" % x})),
                                        logfile)

                with torch.no_grad():
                    lambda_0 = model.module.istc.lambda_0.data.clone().cpu()
                    lambdas = model.module.istc.lambdas.data.clone().cpu().numpy()
                    log_lambda_0 = model.module.istc.log_lambda_0.data.clone()  # Need to keep it on GPU for
                    # log_lambda_0 update with epsilon lambda 0. Other information can be put on CPU
                    lambda_star = model.module.istc.lambda_star.data.clone().cpu()
                    gamma = model.module.istc.gamma.data.clone().cpu()

                    gram = torch.matmul(model.module.istc.w_weight.data[..., 0, 0].t(),
                                        model.module.istc.dictionary_weight.data[..., 0, 0]).cpu().numpy()

                    gram = np.abs(gram)
                    for k in range(args.dictionary_size):
                        gram[k, k] = 0

                    max_coherence = np.max(gram)
                    median_coherence = np.median(gram)

                    print_and_write('Lambda_0_max: {:.3f}'.format(lambda_0_max), logfile)
                    print_and_write('Lambda_0: {:.3f}'.format(lambda_0.item()), logfile)
                    with np.printoptions(precision=2, suppress=True):
                        print_and_write('Lambdas: {}'.format(lambdas.reshape(args.n_iterations - 1)), logfile)
                    print_and_write('Lambda_star: {:.3f}'.format(lambda_star.item()), logfile)
                    print_and_write('Gamma: {:.2f}'.format(gamma.item()), logfile)
                    print_and_write("Max coherence {:.3f}, median coherence {:.3f}".format(max_coherence,
                                                                                           median_coherence), logfile)

        # update lambda_0
        if args.arch in ['sparsescatnet', 'sparsescatnetw']:
            if args.epsilon_lambda_0 > 0:
                model.module.istc.log_lambda_0.data = log_lambda_0 + args.epsilon_lambda_0 * \
                                                      (np.log(lambda_0_max) - log_lambda_0)

    # Print statistics summary
    print_and_write('\n Train Epoch {}, * Acc@1 {:.2f} Acc@5 {:.2f}'.format(epoch, top1.avg, top5.avg), logfile)

    if args.arch in ['sparsescatnet', 'sparsescatnetw']:
        print_and_write('Rec loss rel {:.2f}\t Sparsity {}\t Support size {}\t Support diff {}'.
                        format(rec_losses_rel.avg, np.array2string(sparsities.avg, formatter={
                        'float_kind': lambda x: "%.1f" % x}), np.array2string(support_sizes.avg, formatter={
                        'float_kind': lambda x: "%.0f" % x}), np.array2string(support_diffs.avg, formatter={
                        'float_kind': lambda x: "%.0f" % x})), logfile)

    if writer is not None:
        writer.add_scalar('top5_train', top5.avg, global_step=epoch)
        writer.add_scalar('top1_train', top1.avg, global_step=epoch)
Exemplo n.º 12
0
def main_worker(args):
    best_acc1 = 0
    best_acc5 = 0
    best_epoch_acc1 = 0
    best_epoch_acc5 = 0

    logs_dir = args.logdir
    if not os.path.exists(logs_dir):
        os.makedirs(logs_dir)

    checkpoint_savedir = args.savedir
    if not os.path.exists(checkpoint_savedir):
        os.makedirs(checkpoint_savedir)

    logfile = os.path.join(logs_dir, 'training_{}_b_{}_lrfreq_{}.log'.format(
        args.arch, args.batch_size, args.learning_rate_adjust_frequency))

    summaryfile = os.path.join(logs_dir, 'summary_file.txt')

    checkpoint_savefile = os.path.join(checkpoint_savedir, '{}_batchsize_{}_lrfreq_{}.pth.tar'.format(
        args.arch, args.batch_size, args.learning_rate_adjust_frequency))

    best_checkpoint_savefile = os.path.join(checkpoint_savedir,'{}_batchsize_{}_lrfreq_{}_best.pth.tar'.format(
                                                args.arch, args.batch_size, args.learning_rate_adjust_frequency))

    writer = SummaryWriter(logs_dir)

    # Data loading code
    ###########################################################################################
    traindir = os.path.join(args.data, 'train')
    valdir = os.path.join(args.data, 'val')
    normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],
                                     std=[0.229, 0.224, 0.225])

    train_dataset = datasets.ImageFolder(traindir, transforms.Compose([
        transforms.RandomResizedCrop(224),
        transforms.RandomHorizontalFlip(),
        transforms.ToTensor(),
        normalize,
    ]))

    val_dataset = datasets.ImageFolder(valdir, transforms.Compose([
        transforms.Resize(256),
        transforms.CenterCrop(224),
        transforms.ToTensor(),
        normalize,
    ]))

    # can use a subset of all classes (specified in a file or randomly chosen)
    if args.nb_classes < 1000:
        train_indices = list(np.load('utils_sampling/imagenet_train_class_indices.npy'))
        val_indices = list(np.load('utils_sampling/imagenet_val_class_indices.npy'))
        classes_names = torch.load('utils_sampling/labels_dict')
        if args.class_indices is not None:
            class_indices = torch.load(args.class_indices)
        else:
            perm = torch.randperm(1000)
            class_indices = perm[:args.nb_classes].tolist()
        train_indices_full = [x for i in range(len(class_indices)) for x in range(train_indices[class_indices[i]],
                                                                                  train_indices[class_indices[i] + 1])]
        val_indices_full = [x for i in range(len(class_indices)) for x in range(val_indices[class_indices[i]],
                                                                                val_indices[class_indices[i] + 1])]
        classes_indices_file = os.path.join(logs_dir, 'classes_indices_selected')
        selected_classes_names = [classes_names[i] for i in class_indices]
        torch.save(class_indices, classes_indices_file)
        print_and_write('Selected {} classes indices:  {}'.format(args.nb_classes, class_indices), logfile,
                        summaryfile)
        print_and_write('Selected {} classes names:  {}'.format(args.nb_classes, selected_classes_names), logfile,
                        summaryfile)
        if args.random_seed is not None:
            print_and_write('Random seed used {}'.format(args.random_seed), logfile, summaryfile)

        train_dataset = torch.utils.data.Subset(train_dataset, train_indices_full)
        val_dataset = torch.utils.data.Subset(val_dataset, val_indices_full)

    train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=args.batch_size, shuffle=True,
                                               num_workers=args.workers, pin_memory=True)

    val_loader = torch.utils.data.DataLoader(val_dataset, batch_size=args.batch_size, shuffle=False,
                                             num_workers=args.workers, pin_memory=True)
    ###########################################################################################

    # Model creation
    ###########################################################################################
    if args.arch in model_names:

        n_space = 224
        nb_channels_in = 3

        # create scattering
        J = args.scattering_J
        L_ang = args.scat_angles

        max_order = 2 if args.scattering_order2 else 1

        if args.scattering_wph:
            A = args.scattering_nphases
            scattering = ScatteringTorch2D_wph(J=J, shape=(224, 224), L=L_ang, A=A, max_order=max_order,
                                               backend=args.backend)
        else:
            scattering = Scattering2D(J=J, shape=(224, 224), L=L_ang, max_order=max_order,
                                      backend=args.backend)
        # Flatten scattering
        scattering = nn.Sequential(scattering, nn.Flatten(1, 2))

        if args.scattering_wph:
            nb_channels_in += 3 * A * L_ang * J
        else:
            nb_channels_in += 3 * L_ang * J

        if max_order == 2:
            nb_channels_in += 3 * (L_ang ** 2) * J * (J - 1) // 2

        n_space = n_space // (2 ** J)
    ###########################################################################################

        # create linear proj
        # Standardization (can also be performed with BatchNorm2d(affine=False))
        if not os.path.exists('standardization'):
            os.makedirs('standardization')
        std_file = 'standardization/ImageNet2012_scattering_J{}_order{}_wph_{}_nphases_{}_nb_classes_{}.pth.tar'.format(
            args.scattering_J, 2 if args.scattering_order2 else 1, args.scattering_wph,
            args.scattering_nphases if args.scattering_wph else 0, args.nb_classes)

        if os.path.isfile(std_file):
            print_and_write("=> loading scattering mean and std '{}'".format(std_file), logfile)
            std_dict = torch.load(std_file)
            mean_std = std_dict['mean']
            stding_mat = std_dict['matrix']
        else:
            mean_std, stding_mat, std = compute_stding_matrix(train_loader, scattering, logfile)
            print_and_write("=> saving scattering mean and std '{}'".format(std_file), logfile)
            std_dict = {'mean': mean_std, 'std': std, 'matrix': stding_mat}
            torch.save(std_dict, std_file)

        standardization = Rescaling(mean_std, stding_mat)
        # standardization = nn.BatchNorm2d(nb_channels_in, affine=False)

        if args.arch in ['sparsescatnet', 'sparsescatnetw']:
            proj = nn.Conv2d(nb_channels_in, args.L_proj_size, kernel_size=args.L_kernel_size, stride=1,
                             padding=0, bias=False)
            nb_channels_in = args.L_proj_size
            linear_proj = LinearProj(standardization, proj, args.L_kernel_size)
        else:  # scatnet
            proj = nn.Identity()
            linear_proj = LinearProj(standardization, proj, 0)

        ###########################################################################################

        # Create ISTC (when applicable)
        if args.arch in ['sparsescatnet', 'sparsescatnetw']:
        ###########################################################################################
            if args.arch == 'sparsescatnet':
                arch_log = "=> creating model SparseScatNet with phase scattering {}, linear projection " \
                           "(projection dimension {}), ISTC with {} iterations, dictionary size {}, classifier {} " \
                           "pipeline".format(args.scattering_wph, args.L_proj_size, args.n_iterations,
                                             args.dictionary_size, args.classifier_type)

                istc = ISTC(nb_channels_in, dictionary_size=args.dictionary_size, n_iterations=args.n_iterations,
                            lambda_0=args.lambda_0, lambda_star=args.lambda_star, lambda_star_lb=args.lambda_star_lb,
                            grad_lambda_star=args.grad_lambda_star, epsilon_lambda_0=args.epsilon_lambda_0,
                            output_rec=args.output_rec)

            elif args.arch == 'sparsescatnetw':
                arch_log = "=> creating model SparseScatNetW with phase scattering {}, linear projection " \
                           "(projection dimension {}), ISTCW with {} iterations, dictionary size {}, classifier {} " \
                           "pipeline".format(args.scattering_wph, args.L_proj_size, args.n_iterations,
                                             args.dictionary_size, args.classifier_type)

                istc = ISTC(nb_channels_in, dictionary_size=args.dictionary_size, n_iterations=args.n_iterations,
                            lambda_0=args.lambda_0, lambda_star=args.lambda_star, lambda_star_lb=args.lambda_star_lb,
                            grad_lambda_star=args.grad_lambda_star, epsilon_lambda_0=args.epsilon_lambda_0,
                            output_rec=args.output_rec, use_W=True)

            if not args.output_rec:
                nb_channels_in = args.dictionary_size

        elif args.arch == 'scatnet':
            arch_log = "=> creating model ScatNet with phase scattering {} and classifier {}".\
                format(args.scattering_wph, args.classifier_type)

        # Create classifier
        ###########################################################################################

        classifier = Classifier(n_space, nb_channels_in, classifier_type=args.classifier_type,
                                nb_classes=1000, nb_hidden_units=args.nb_hidden_units, nb_l_mlp=args.nb_l_mlp,
                                dropout_p_mlp=args.dropout_p_mlp, avg_ker_size=args.avg_ker_size)

        # Create model
        ###########################################################################################
        if args.arch in ['sparsescatnet', 'sparsescatnetw']:
            model = SparseScatNet(scattering, linear_proj, istc, classifier, return_full_inf=True)  # print model info

        elif args.arch == 'scatnet':
            model = nn.Sequential(scattering, linear_proj, classifier)
    else:
        print_and_write("Unknown model", logfile, summaryfile)
        return

    print_and_write(arch_log, logfile, summaryfile)
    print_and_write('Number of epochs {}, learning rate decay epochs {}'.format(args.epochs,
                                                                                args.learning_rate_adjust_frequency),
                                                                                logfile, summaryfile)

    # DataParallel will divide and allocate batch_size to all available GPUs
    model = torch.nn.DataParallel(model).cuda()

    # define loss function (criterion) and optimizer
    criterion = nn.CrossEntropyLoss().cuda()

    optimizer = torch.optim.SGD(model.parameters(), args.lr, momentum=args.momentum, weight_decay=args.weight_decay)

    # optionally resume from a checkpoint
    if args.resume:
        if os.path.isfile(args.resume):
            print_and_write("=> loading checkpoint '{}'".format(args.resume), logfile, summaryfile)
            checkpoint = torch.load(args.resume)
            args.start_epoch = checkpoint['epoch']
            best_acc1 = checkpoint['best_acc1']
            model.load_state_dict(checkpoint['state_dict'])
            optimizer.load_state_dict(checkpoint['optimizer'])
            print_and_write("=> loaded checkpoint '{}' (epoch {})".format(args.resume, checkpoint['epoch']), logfile,
                            summaryfile)
        else:
            print_and_write("=> no checkpoint found at '{}'".format(args.resume), logfile, summaryfile)

    cudnn.benchmark = True

    if args.arch in ['sparsescatnet', 'sparsescatnetw']:

        lambda_0_max = compute_lambda_0(train_loader, model).item()

        if args.l0_inf_init:
            with torch.no_grad():
                model.module.istc.lambda_0.data.fill_(lambda_0_max)
                model.module.istc.log_lambda_0.data = torch.log(model.module.istc.lambda_0.data)
                model.module.istc.gamma.data.fill_(np.power(args.lambda_star / lambda_0_max, 1. / args.n_iterations))
                model.module.istc.log_gamma.data = torch.log(model.module.istc.gamma.data)
                for i in range(args.n_iterations - 1):
                    model.module.istc.lambdas.data[i] = lambda_0_max * (model.module.istc.gamma.data ** (i + 1))
                    model.module.istc.log_lambdas.data[i] = torch.log(model.module.istc.lambdas.data[i])

        print_and_write('Lambda star lower bound {:.3f}'.format(args.lambda_star_lb), logfile, summaryfile)
        print_and_write('epsilon lambda_0 {}'.format(args.epsilon_lambda_0), logfile, summaryfile)

        with torch.no_grad():
            with np.printoptions(precision=2, suppress=True):
                    lambda_0_init = model.module.istc.lambda_0.data.cpu().item()
                    print_and_write('Lambda_0 init {:.2f}'.format(lambda_0_init), logfile, summaryfile)

                    lambdas_init = model.module.istc.lambdas.data.cpu().numpy()
                    print_and_write('Lambdas init {}'.format(lambdas_init), logfile, summaryfile)

                    print_and_write('Lambda_star init {:.2f}'.format(args.lambda_star), logfile, summaryfile)

                    gamma_init = model.module.istc.gamma.data.cpu().item()
                    print_and_write('Gamma init {:.2f}'.format(gamma_init), logfile, summaryfile)

            count = 0
            for i in range(args.dictionary_size):
                if model.module.istc.dictionary_weight.data[:, i].norm(p=2) < 0.99 or \
                        model.module.istc.dictionary_weight.data[:, i].norm(p=2) > 1.01:
                    count += 1

            if count == 0:
                print_and_write("Dictionary atoms initially well normalized", logfile,summaryfile)
            else:
                print_and_write("{} dictionary atoms not initially well normalized".format(count), logfile, summaryfile)

            gram = torch.matmul(model.module.istc.w_weight.data[..., 0, 0].t(),
                                      model.module.istc.dictionary_weight.data[..., 0, 0]).cpu().numpy()

            if args.arch == 'sparsescatnetw':
                count = 0
                for i in range(args.dictionary_size):
                    if gram[i, i] < 0.99 or gram[i, i] > 1.01:
                        count += 1
                if count == 0:
                    print_and_write("W^T D diagonal elements well equal to 1", logfile, summaryfile)
                else:
                    print_and_write("{} W^T D diagonal elements not equal to 1".format(count),
                                    logfile, summaryfile)

            gram = np.abs(gram)
            for i in range(args.dictionary_size):
                gram[i, i] = 0

            print_and_write("Initial max coherence {:.3f}, median coherence {:.3f}".
                            format(np.max(gram), np.median(gram)), logfile, summaryfile)

    if args.evaluate:
        print_and_write("Evaluating model at epoch {}...".format(args.start_epoch), logfile)
        validate(val_loader, model, criterion, args.start_epoch, args, logfile, summaryfile, writer)
        return

    for epoch in range(args.start_epoch, args.epochs):
        adjust_learning_rate(optimizer, epoch, args)

        # train for one epoch
        train(train_loader, model, criterion, optimizer, epoch, args, logfile, writer)

        # evaluate on validation set
        acc1, acc5 = validate(val_loader, model, criterion, epoch, args, logfile, summaryfile, writer)

        # remember best acc@1 and save checkpoint
        is_best = acc1 > best_acc1
        best_acc1 = max(acc1, best_acc1)
        if is_best:
            best_epoch_acc1 = epoch
        if acc5 > best_acc5:
            best_acc5 = acc5
            best_epoch_acc5 = epoch
        save_checkpoint({
            'epoch': epoch + 1,
            'arch': args.arch,
            'state_dict': model.state_dict(),
            'best_acc1': best_acc1,
            'optimizer': optimizer.state_dict(),
        }, is_best, checkpoint_filename=checkpoint_savefile, best_checkpoint_filename=best_checkpoint_savefile)

    if args.arch in ['sparsescatnet', 'sparsescatnetw']:
        with torch.no_grad():
            with np.printoptions(precision=2, suppress=True):

                lambda_0_final = model.module.istc.lambda_0.data.cpu().item()
                print_and_write('Lambda_0 final {:.2f}'.format(lambda_0_final), logfile, summaryfile)

                lambdas_final = model.module.istc.lambdas.data.cpu().numpy()
                print_and_write('Lambdas final {}'.format(lambdas_final), logfile, summaryfile)

                lambda_star_final = model.module.istc.lambda_star.data.cpu().item()
                print_and_write('Lambda_star final {:.2f}'.format(lambda_star_final), logfile, summaryfile)

                gamma_final = model.module.istc.gamma.data.cpu().item()
                print_and_write('Gamma final {:.2f}'.format(gamma_final), logfile, summaryfile)

            count = 0
            for i in range(args.dictionary_size):
                if model.module.istc.dictionary_weight.data[:, i].norm(p=2) < 0.99 or \
                        model.module.istc.dictionary_weight.data[:, i].norm(p=2) > 1.01:
                    count += 1

            if count == 0:
                print_and_write("Dictionary atoms finally well normalized", logfile, summaryfile)
            else:
                print_and_write("{} dictionary atoms not finally well normalized".format(count), logfile, summaryfile)

            gram = torch.matmul(model.module.istc.w_weight.data[..., 0, 0].t(),
                                model.module.istc.dictionary_weight.data[..., 0, 0]).cpu().numpy()

            if args.arch == 'sparsescatnetw':
                count = 0
                for i in range(args.dictionary_size):
                    if gram[i, i] < 0.99 or gram[i, i] > 1.01:
                        count += 1
                if count == 0:
                    print_and_write("W^T D diagonal elements well equal to 1", logfile, summaryfile)
                else:
                    print_and_write("{} W^T D diagonal elements not equal to 1".format(count),
                                    logfile, summaryfile)

            gram = np.abs(gram)
            for i in range(args.dictionary_size):
                gram[i, i] = 0

            print_and_write("Final max coherence {:.3f}, median coherence {:.3f}".
                            format(np.max(gram), np.median(gram)), logfile, summaryfile)

    print_and_write(
        "Best top 1 accuracy {:.2f} at epoch {}, best top 5 accuracy {:.2f} at epoch {}".format(best_acc1,
                                                                                                best_epoch_acc1,
                                                                                                best_acc5,
                                                                                                best_epoch_acc5),
        logfile, summaryfile)
Exemplo n.º 13
0
		file = open(constants.PATH_TO_CLUSTERING_RESULTS + args.output_fname + ".txt", "wb")
		for i, elem in enumerate(combinations):
			print "---- Kinematics + Vision Combination #"+ str(i + 1) + "----"
			mc = TSCDL_multimodal(False, list(elem), args.visual_feature, args.output_fname)
			all_metrics.append(mc.do_everything())

		print "----------- CALCULATING THE ODDS ------------"
		post_evaluation_multimodal(all_metrics, file, args.output_fname, list_of_demonstrations, args.visual_feature)
		file.close()

	elif args.mode == "W":

		file = open(constants.PATH_TO_CLUSTERING_RESULTS + args.output_fname + ".txt", "wb")
		for i, elem in enumerate(combinations):
			utils.print_and_write("\n----------- Kinematics-only Combination #" + str(i + 1) + " -------------\n", file)
			print "\n----------- Kinematics-only Combination #" + str(i + 1) + " -------------\n"
			print elem
			mc = TSCDL_singlemodal(False, list(elem), args.output_fname + str(i), file, False, None)
			all_metrics.append(mc.do_everything())

		print "----------- CALCULATING THE ODDS ------------"
		post_evaluation_singlemodal(all_metrics, file, args.output_fname, False, list_of_demonstrations)
		file.close()

	elif args.mode == "Z":
		check_visual_features(args)

		file = open(constants.PATH_TO_CLUSTERING_RESULTS + args.output_fname + ".txt", "wb")
		for i, elem in enumerate(combinations):
			utils.print_and_write("\n----------- Vision-only Combination #" + str(i + 1) + " -------------\n", file)
Exemplo n.º 14
0
                start_epoch = int(ckpt_path.split('-')[-1]) + 1
            except:
                pass
            if load_log:
                last_log_file = os.path.join(save_path, load_log)
            else:
                log_file_list = [os.path.join(save_path, k) for k in os.listdir(save_path) if
                                 k.startswith('log_') and k.endswith('.txt')]

                if len(log_file_list) <= 1:
                    print('No previous log files found')
                    last_log_file = ''
                else:
                    log_file_list.sort()
                    last_log_file = log_file_list[-2]
            print_and_write('Loading previous log from {}\n'.format(last_log_file), log_fname)
            try:
                if last_log_file:
                    last_log_line = open(last_log_file, 'r').readlines()[-1]
                    last_log_data = [k.strip() for k in last_log_line.split() if k.strip()]

                    print('last_log_data:\n {}'.format(last_log_data))

                    min_loss_data = last_log_data[7].split('(')
                    max_pix_acc_data = last_log_data[11].split('(')

                    min_loss = float(min_loss_data[0])
                    min_loss_epoch = int(min_loss_data[1].replace(')', ''))

                    max_pix_acc = float(max_pix_acc_data[0])
                    mean_pix_acc = float(last_log_data[9])
Exemplo n.º 15
0
                                  args.output_fname)
            all_metrics.append(mc.do_everything())

        print "----------- CALCULATING THE ODDS ------------"
        post_evaluation_multimodal(all_metrics, file, args.output_fname,
                                   list_of_demonstrations, args.visual_feature)
        file.close()

    elif args.mode == "W":

        file = open(
            constants.PATH_TO_CLUSTERING_RESULTS + args.output_fname + ".txt",
            "wb")
        for i, elem in enumerate(combinations):
            utils.print_and_write(
                "\n----------- Kinematics-only Combination #" + str(i + 1) +
                " -------------\n", file)
            print "\n----------- Kinematics-only Combination #" + str(
                i + 1) + " -------------\n"
            print elem
            mc = TSCDL_singlemodal(False, list(elem),
                                   args.output_fname + str(i), file, False,
                                   None)
            all_metrics.append(mc.do_everything())

        print "----------- CALCULATING THE ODDS ------------"
        post_evaluation_singlemodal(all_metrics, file, args.output_fname,
                                    False, list_of_demonstrations)
        file.close()

    elif args.mode == "Z":
Exemplo n.º 16
0
	if args.debug == 'y':
		DEBUG = True
		list_of_demonstrations = ['Suturing_E001','Suturing_E002']
	else:
		DEBUG = False

		list_of_demonstrations = constants.config.get("list_of_demonstrations")

	vision_mode = False
	feat_fname = None
	if args.visual:
		vision_mode = True
		feat_fname = args.visual

	combinations = get_list_of_demo_combinations(list_of_demonstrations)

	all_metrics = []
	log = open(constants.PATH_TO_CLUSTERING_RESULTS + args.fname + ".txt", "wb")

	for i in range(len(combinations)):
		utils.print_and_write("\n----------- Combination #" + str(i) + " -------------\n", log)
		print "\n----------- Combination #" + str(i) + " -------------\n"
		print combinations[i]
		mc = TSCDL_singlemodal(DEBUG, list(combinations[i]), args.fname + str(i), log, vision_mode, feat_fname)
		all_metrics.append(mc.do_everything())

	print "----------- CALCULATING THE ODDS ------------"
	post_evaluation_singlemodal(all_metrics, log, args.fname, vision_mode, list_of_demonstrations)

	log.close()
Exemplo n.º 17
0
 def display(self, batch, logfile):
     entries = [self.prefix + self.batch_fmtstr.format(batch)]
     entries += [str(meter) for meter in self.meters]
     print_and_write('\t'.join(entries), logfile)
Exemplo n.º 18
0
	def cluster_metrics(self):
		"""
		Computes metrics and returns as single dictionary.
		"""
		labels_true, labels_pred = self.prepare_labels()

		metric_funcs = [adjusted_rand_score, adjusted_mutual_info_score, normalized_mutual_info_score, 
		mutual_info_score, homogeneity_score, completeness_score]

		# ------ Label-based metrics ------
		utils.print_and_write("\n\nPred= L1 Labels     Metric\n\n", self.log)

		for metric in metric_funcs:

			score_1 = round(Decimal(metric(labels_true, labels_pred)), 2)
			key =  repr(metric).split()[1]
			self.label_based_scores_1[key] = score_1

			utils.print_and_write(("%3.3f        %s\n" % (score_1, key)), self.log)

		# ------ Precision & Recall ------
		utils.print_and_write("\n Precision & Recall scores \n", self.log)
		for ave in ["micro", "macro", "weighted"]:
			key = "precision_" + ave
			score_1 = utils.nsf(precision_score(labels_true, labels_pred, average = ave))

			self.label_based_scores_1[key] = score_1

			utils.print_and_write("%3.3f        %s\n" % (round(Decimal(score_1), 2), key), self.log)

			key = "recall_" + ave
			score_1 = utils.nsf(recall_score(labels_true, labels_pred, average = ave))

			self.label_based_scores_1[key] = score_1

			utils.print_and_write("%3.3f        %s\n" % (round(Decimal(score_1), 2), key), self.log)

		utils.print_and_write("\nDunn Scores1\n", self.log)

		# ------ Dunn Scores # 1------
		for layer in sorted(self.dunn_scores_1):
			score = self.dunn_scores_1[layer]
			utils.print_and_write("%3.3f        %s\n" % (round(Decimal(score), 2), layer), self.log)

		utils.print_and_write("\nDunn Scores2\n", self.log)

		# ------ Dunn Scores # 2------
		for layer in sorted(self.dunn_scores_2):
			score = self.dunn_scores_2[layer]
			utils.print_and_write("%3.3f        %s\n" % (round(Decimal(score), 2), layer), self.log)

		utils.print_and_write("\nDunn Scores3\n", self.log)

		# ------ Dunn Scores #3 ------
		for layer in sorted(self.dunn_scores_3):
			score = self.dunn_scores_3[layer]
			utils.print_and_write("%3.3f        %s\n" % (round(Decimal(score), 2), layer), self.log)

		# ------ Visualizing changepoints on broken barh ------
		viz = {}

		for cp in self.list_of_cp:
			cp_all_data = (self.map_cp2frm[cp], self.map_cp2cluster[cp], self.map_cp2surgemetransitions[cp], self.map_cp2surgemes[cp])
			utils.dict_insert_list(self.map_cp2demonstrations[cp], cp_all_data, viz)

		data = [self.label_based_scores_1, self.silhouette_score_global, self.dunn_scores_1,
		self.dunn_scores_2, self.dunn_scores_3, viz, self.silhouette_score_weighted]

		return data
		# list_of_demonstrations = ["plane_6", "plane_7", "plane_8", "plane_9", "plane_10"]

		# list_of_demonstrations = ['people_2', 'people_3', 'people_4', 'people_5', 'people_6']

		# list_of_demonstrations = ['people2_2', 'people2_3', 'people2_4', 'people2_5', 'people2_6']

		list_of_demonstrations = constants.config.get("list_of_demonstrations")

	vision_mode = False
	feat_fname = None
	if args.visual:
		vision_mode = True
		feat_fname = args.visual

	combinations = get_list_of_demo_combinations(list_of_demonstrations)

	all_metrics = []
	log = open(constants.PATH_TO_CLUSTERING_RESULTS + args.fname + ".txt", "wb")

	for i in range(len(combinations)):
		utils.print_and_write("\n----------- Combination #" + str(i) + " -------------\n", log)
		print "\n----------- Combination #" + str(i) + " -------------\n"
		print combinations[i]
		mc = KinematicsClustering(DEBUG, list(combinations[i]), args.fname + str(i), log, vision_mode, feat_fname)
		all_metrics.append(mc.do_everything())

	print "----------- CALCULATING THE ODDS ------------"
	post_evaluation(all_metrics, log, args.fname, vision_mode)

	log.close()
Exemplo n.º 20
0
def train(train_loader,
          model,
          criterion,
          optimizer,
          epoch,
          pre_transformation=None,
          logfile=None,
          dictionary=None,
          writer=None):
    batch_time = AverageMeter()
    data_time = AverageMeter()
    pre_transformation_time = AverageMeter()
    losses = AverageMeter()
    top1 = AverageMeter()
    top5 = AverageMeter()

    end = time.time()

    model.train()

    # if epoch == 0:
    # print_and_write('warm up for batch norms', logfile=logfile)
    # for i, (input, target) in enumerate(train_loader):
    # input = input.cuda()

    # if pre_transformation is not None:
    # with torch.no_grad():
    # input = pre_transformation(input)
    # output = model(input)
    # if i > 200:
    # break
    # end = time.time()

    for i, (input, target) in enumerate(train_loader):
        data_time.update(time.time() - end)

        input = input.cuda()

        # pre-processing
        if pre_transformation is not None:
            now = time.time()
            with torch.no_grad():
                input = pre_transformation(input)
            pre_transformation_time.update(time.time() - now)

        target = target.cuda(args.gpu, non_blocking=True)
        output = model(input)

        loss = criterion(output, target)

        acc1, acc5 = accuracy(output, target, topk=(1, 5))
        losses.update(loss.item(), input.size(0))
        top1.update(acc1[0], input.size(0))
        top5.update(acc5[0], input.size(0))

        # compute gradient and do SGD step
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()

        # measure elapsed time
        batch_time.update(time.time() - end)
        end = time.time()

        if i % args.print_freq == 0:
            print_and_write(
                'Epoch: [{0}][{1}/{2}]\t'
                'Time {batch_time.val:.1f} ({batch_time.avg:.1f})\t'
                'Data {data_time.val:.1f} ({data_time.avg:.1f})\t'
                'preproc. {pre_transformation_time.val:.1f}({pre_transformation_time.avg:.1f})\t'
                'Tot. Loss {loss.val:.2f} ({loss.avg:.2f})\t'
                'Acc@1 {top1.val:.1f} ({top1.avg:.1f})\t'
                'Acc@5 {top5.val:.1f} ({top5.avg:.1f})\t'.format(
                    epoch,
                    i,
                    len(train_loader),
                    batch_time=batch_time,
                    data_time=data_time,
                    pre_transformation_time=pre_transformation_time,
                    loss=losses,
                    top1=top1,
                    top5=top5),
                logfile=logfile)
	def cluster_evaluation(self):

		for cp in self.list_of_cp:
			demonstration = self.map_cp2demonstrations[cp]
			frm = self.map_cp2frm[cp]

			curr_surgeme = self.map_frm2surgeme[demonstration][frm]
			self.map_cp2surgemes[cp] = curr_surgeme

			ranges = sorted(parser.get_annotation_segments(constants.PATH_TO_DATA + constants.ANNOTATIONS_FOLDER
				+ demonstration + "_" + constants.CAMERA + ".p"))

			bin = utils.binary_search(ranges, frm)

			map_frm2surgeme_demonstration = self.map_frm2surgeme[demonstration]

			prev_end = bin[0] - 1
			next_start = bin[1] + 1
			prev_surgeme = map_frm2surgeme_demonstration[prev_end] if prev_end in map_frm2surgeme_demonstration else "start"
			next_surgeme = map_frm2surgeme_demonstration[next_start] if next_start in map_frm2surgeme_demonstration else "end"

			surgemetransition = None

			if abs(frm - (bin[0] - 1)) < abs(bin[1] + 1 - frm):
				surgemetransition = str(prev_surgeme) + "->" + str(curr_surgeme)
			else:
				surgemetransition = str(curr_surgeme) + "->" + str(next_surgeme)

			self.map_cp2surgemetransitions[cp] = surgemetransition

		self.cp_surgemes = set(self.map_cp2surgemes.values())

		# Initialize data structures
		table = {}
		for L1_cluster in self.map_level1_cp.keys():
			new_dict = {}
			for surgeme in self.cp_surgemes:
				new_dict[surgeme] = 0
			table[L1_cluster] = new_dict

		surgeme_count = {}
		for surgeme in self.cp_surgemes:
			surgeme_count[surgeme] = 0

		for L1_cluster in self.map_level1_cp.keys():
			list_of_cp_key = self.map_level1_cp[L1_cluster]
			for cp in list_of_cp_key:
				surgeme = self.map_frm2surgeme[self.map_cp2demonstrations[cp]][self.map_cp2frm[cp]]
				surgeme_count[surgeme] += 1
				curr_dict = table[L1_cluster]
				curr_dict[surgeme] += 1
				table[L1_cluster] = curr_dict

		final_clusters = list(set(self.map_level1_cp.keys()) - set(self.pruned_L1_clusters))

		confusion_matrix = "    "
		for surgeme in self.cp_surgemes:
			confusion_matrix = confusion_matrix + str(surgeme) + "     "

		utils.print_and_write('\n\n ---Confusion Matrix--- \n\n', self.log)
		utils.print_and_write(confusion_matrix, self.log)

		confusion_matrix = ""
		for L1_cluster in final_clusters:
			confusion_matrix = confusion_matrix + "\n" + L1_cluster + "   "
			for surgeme in self.cp_surgemes:
				# confusion_matrix += str(float("{0:.2f}".format(table[L1_cluster][surgeme] / float(surgeme_count[surgeme])))) + "   "
				confusion_matrix += str(round(Decimal(table[L1_cluster][surgeme] / float(surgeme_count[surgeme])), 2)) + "   "
			confusion_matrix += '\n'

		utils.print_and_write(confusion_matrix, self.log)
		utils.print_and_write("\n\n ---Surgeme Count--- \n\n", self.log)
		utils.print_and_write(repr(surgeme_count), self.log)
		utils.print_and_write("\n\n", self.log)
Exemplo n.º 22
0
def post_evaluation_multimodal(metrics, file, filename, list_of_demonstrations,
                               feat_fname):

    mutual_information_1 = []
    normalized_mutual_information_1 = []
    adjusted_mutual_information_1 = []
    homogeneity_1 = []
    precision_1_micro = []
    recall_1_micro = []
    precision_1_macro = []
    recall_1_macro = []
    precision_1_weighted = []
    recall_1_weighted = []

    mutual_information_2 = []
    normalized_mutual_information_2 = []
    adjusted_mutual_information_2 = []
    homogeneity_2 = []
    precision_2_micro = []
    recall_2_micro = []
    precision_2_macro = []
    recall_2_macro = []
    precision_2_weighted = []
    recall_2_weighted = []

    silhoutte_level_1_global = []
    silhoutte_level_1_weighted = []
    dunn1_level_1 = []
    dunn2_level_1 = []
    dunn3_level_1 = []

    silhoutte_level_2_global = []
    silhoutte_level_2_weighted = []
    dunn1_level_2 = []
    dunn2_level_2 = []
    dunn3_level_2 = []

    list_of_frms = {}

    for elem in metrics:
        precision_1_micro.append(elem[0]["precision_micro"])
        precision_2_micro.append(elem[1]["precision_micro"])

        precision_1_macro.append(elem[0]["precision_macro"])
        precision_2_macro.append(elem[1]["precision_macro"])

        precision_1_weighted.append(elem[0]["precision_weighted"])
        precision_2_weighted.append(elem[1]["precision_weighted"])

        recall_1_micro.append(elem[0]["recall_micro"])
        recall_2_micro.append(elem[1]["recall_micro"])

        recall_1_macro.append(elem[0]["recall_macro"])
        recall_2_macro.append(elem[1]["recall_macro"])

        recall_1_weighted.append(elem[0]["recall_weighted"])
        recall_2_weighted.append(elem[1]["recall_weighted"])

        mutual_information_1.append(elem[0]["mutual_info_score"])
        mutual_information_2.append(elem[1]["mutual_info_score"])

        normalized_mutual_information_1.append(
            elem[0]["normalized_mutual_info_score"])
        normalized_mutual_information_2.append(
            elem[1]["normalized_mutual_info_score"])

        adjusted_mutual_information_1.append(
            elem[0]["adjusted_mutual_info_score"])
        adjusted_mutual_information_2.append(
            elem[1]["adjusted_mutual_info_score"])

        homogeneity_1.append(elem[0]["homogeneity_score"])
        homogeneity_2.append(elem[1]["homogeneity_score"])

        silhoutte_level_1_global.append(elem[2]["level1"])
        dunn1_level_1.append(elem[3]["level1"])
        dunn2_level_1.append(elem[4]["level1"])
        dunn3_level_1.append(elem[5]["level1"])

        silhoutte_level_2_global += elem[6]
        dunn1_level_2 += elem[7]
        dunn2_level_2 += elem[8]
        dunn3_level_2 += elem[9]

        viz = elem[10]

        for demonstration in viz.keys():
            utils.dict_insert_list(demonstration, viz[demonstration],
                                   list_of_frms)

        silhoutte_level_1_weighted.append(elem[11]["level1"])
        silhoutte_level_2_weighted += elem[12]

    utils.print_and_write_2("precision_1_micro", np.mean(precision_1_micro),
                            np.std(precision_1_micro), file)
    utils.print_and_write_2("precision_2_micro", np.mean(precision_2_micro),
                            np.std(precision_2_micro), file)

    utils.print_and_write_2("precision_1_macro", np.mean(precision_1_macro),
                            np.std(precision_1_macro), file)
    utils.print_and_write_2("precision_2_macro", np.mean(precision_2_macro),
                            np.std(precision_2_macro), file)

    utils.print_and_write_2("precision_1_weighted",
                            np.mean(precision_1_weighted),
                            np.std(precision_1_weighted), file)
    utils.print_and_write_2("precision_2_weighted",
                            np.mean(precision_2_weighted),
                            np.std(precision_2_weighted), file)

    utils.print_and_write_2("recall_1_micro", np.mean(recall_1_micro),
                            np.std(recall_1_micro), file)
    utils.print_and_write_2("recall_2_micro", np.mean(recall_2_micro),
                            np.std(recall_2_micro), file)

    utils.print_and_write_2("recall_1_macro", np.mean(recall_1_macro),
                            np.std(recall_1_macro), file)
    utils.print_and_write_2("recall_2_macro", np.mean(recall_2_macro),
                            np.std(recall_2_macro), file)

    utils.print_and_write_2("recall_1_weighted", np.mean(recall_1_weighted),
                            np.std(recall_1_weighted), file)
    utils.print_and_write_2("recall_2_weighted", np.mean(recall_2_weighted),
                            np.std(recall_2_weighted), file)

    utils.print_and_write_2("mutual_info_1", np.mean(mutual_information_1),
                            np.std(mutual_information_1), file)
    utils.print_and_write_2("mutual_info_2", np.mean(mutual_information_2),
                            np.std(mutual_information_2), file)

    utils.print_and_write_2("normalized_mutual_info_1",
                            np.mean(normalized_mutual_information_1),
                            np.std(normalized_mutual_information_1), file)
    utils.print_and_write_2("normalized_mutual_info_2",
                            np.mean(normalized_mutual_information_2),
                            np.std(normalized_mutual_information_2), file)

    utils.print_and_write_2("adjusted_mutual_info_1",
                            np.mean(adjusted_mutual_information_1),
                            np.std(adjusted_mutual_information_1), file)
    utils.print_and_write_2("adjusted_mutual_info_2",
                            np.mean(adjusted_mutual_information_2),
                            np.std(adjusted_mutual_information_2), file)

    utils.print_and_write_2("homogeneity_1", np.mean(homogeneity_1),
                            np.std(homogeneity_1), file)
    utils.print_and_write_2("homogeneity_2", np.mean(homogeneity_2),
                            np.std(homogeneity_2), file)

    utils.print_and_write_2("silhoutte_level_1_global",
                            np.mean(silhoutte_level_1_global),
                            np.std(silhoutte_level_1_global), file)
    utils.print_and_write_2("silhoutte_level_2_global",
                            np.mean(silhoutte_level_2_global),
                            np.std(silhoutte_level_2_global), file)

    utils.print_and_write_2("silhoutte_level_1_weighted",
                            np.mean(silhoutte_level_1_weighted),
                            np.std(silhoutte_level_1_weighted), file)
    utils.print_and_write_2("silhoutte_level_2_weighted",
                            np.mean(silhoutte_level_2_weighted),
                            np.std(silhoutte_level_2_weighted), file)

    utils.print_and_write_2("dunn1_level1", np.mean(dunn1_level_1),
                            np.std(dunn1_level_1), file)
    utils.print_and_write_2("dunn2_level1", np.mean(dunn2_level_1),
                            np.std(dunn2_level_1), file)
    utils.print_and_write_2("dunn3_level1", np.mean(dunn3_level_1),
                            np.std(dunn3_level_1), file)

    utils.print_and_write_2("dunn1_level2", np.mean(dunn1_level_2),
                            np.std(dunn1_level_2), file)
    utils.print_and_write_2("dunn2_level2", np.mean(dunn2_level_2),
                            np.std(dunn2_level_2), file)
    utils.print_and_write_2("dunn3_level2", np.mean(dunn3_level_2),
                            np.std(dunn3_level_2), file)

    list_of_dtw_values = []
    list_of_norm_dtw_values = []
    list_of_lengths = []

    data_cache = {}

    for demonstration in list_of_demonstrations:
        try:
            list_of_frms_demonstration = list_of_frms[demonstration]

            data = {}

            for i in range(len(list_of_frms_demonstration)):
                data[i] = [elem[0] for elem in list_of_frms_demonstration[i]]

            dtw_score, normalized_dtw_score, length, labels_manual_d, colors_manual_d, labels_automatic_d, colors_automatic_d = broken_barh.plot_broken_barh(
                demonstration, data, constants.PATH_TO_CLUSTERING_RESULTS +
                demonstration + "_" + filename + ".jpg",
                constants.N_COMPONENTS_TIME_ZW)

            list_of_dtw_values.append(dtw_score)
            list_of_norm_dtw_values.append(normalized_dtw_score)
            list_of_lengths.append(length)

            # Inserting manual and annotations labels into data struct before dumping as pickle file
            cache_entry = {}
            cache_entry['changepoints'] = list_of_frms_demonstration
            cache_entry['plot_labels_manual'] = labels_manual_d
            cache_entry['plot_colors_manual'] = colors_manual_d
            cache_entry['plot_labels_automatic'] = labels_automatic_d
            cache_entry['plot_colors_automatic'] = colors_automatic_d
            data_cache[demonstration] = cache_entry

        except:
            print demonstration
            continue

    utils.print_and_write_2("dtw_score", np.mean(list_of_dtw_values),
                            np.std(list_of_dtw_values), file)
    utils.print_and_write_2("dtw_score_normalized",
                            np.mean(list_of_norm_dtw_values),
                            np.std(list_of_norm_dtw_values), file)
    utils.print_and_write(str(list_of_lengths), file)

    pickle.dump(
        data_cache,
        open(constants.PATH_TO_CLUSTERING_RESULTS + filename + "_.p", "wb"))
	def cluster_metrics(self):
		"""
		Computes metrics and returns as single dictionary.
		"""
		labels_true, labels_pred = self.prepare_labels()

		metric_funcs = [adjusted_rand_score, adjusted_mutual_info_score, normalized_mutual_info_score, 
		mutual_info_score, homogeneity_score, completeness_score]

		# ------ Label-based metrics ------
		utils.print_and_write("\n\nPred= L1 Labels     Metric\n\n", self.log)

		for metric in metric_funcs:

			score_1 = round(Decimal(metric(labels_true, labels_pred)), 2)
			key =  repr(metric).split()[1]
			self.label_based_scores_1[key] = score_1

			utils.print_and_write(("%3.3f        %s\n" % (score_1, key)), self.log)

		# ------ Precision & Recall ------
		utils.print_and_write("\n Precision & Recall scores \n", self.log)
		for ave in ["micro", "macro", "weighted"]:
			key = "precision_" + ave
			score_1 = utils.nsf(precision_score(labels_true, labels_pred, average = ave))

			self.label_based_scores_1[key] = score_1

			utils.print_and_write("%3.3f        %s\n" % (round(Decimal(score_1), 2), key), self.log)

			key = "recall_" + ave
			score_1 = utils.nsf(recall_score(labels_true, labels_pred, average = ave))

			self.label_based_scores_1[key] = score_1

			utils.print_and_write("%3.3f        %s\n" % (round(Decimal(score_1), 2), key), self.log)

		utils.print_and_write("\nDunn Scores1\n", self.log)

		# ------ Dunn Scores # 1------
		for layer in sorted(self.dunn_scores_1):
			score = self.dunn_scores_1[layer]
			utils.print_and_write("%3.3f        %s\n" % (round(Decimal(score), 2), layer), self.log)

		utils.print_and_write("\nDunn Scores2\n", self.log)

		# ------ Dunn Scores # 2------
		for layer in sorted(self.dunn_scores_2):
			score = self.dunn_scores_2[layer]
			utils.print_and_write("%3.3f        %s\n" % (round(Decimal(score), 2), layer), self.log)

		utils.print_and_write("\nDunn Scores3\n", self.log)

		# ------ Dunn Scores #3 ------
		for layer in sorted(self.dunn_scores_3):
			score = self.dunn_scores_3[layer]
			utils.print_and_write("%3.3f        %s\n" % (round(Decimal(score), 2), layer), self.log)

		# ------ Visualizing changepoints on broken barh ------
		viz = {}

		for cp in self.list_of_cp:
			cp_all_data = (self.map_cp2frm[cp], self.map_cp2cluster[cp], self.map_cp2surgemetransitions[cp], self.map_cp2surgemes[cp])
			utils.dict_insert_list(self.map_cp2demonstrations[cp], cp_all_data, viz)

		data = [self.label_based_scores_1, self.silhouette_score_global, self.dunn_scores_1,
		self.dunn_scores_2, self.dunn_scores_3, viz, self.silhouette_score_weighted]

		return data
Exemplo n.º 24
0
def post_evaluation_multimodal(metrics, file, filename, list_of_demonstrations, feat_fname):

	mutual_information_1 = []
	normalized_mutual_information_1 = []
	adjusted_mutual_information_1 = []
	homogeneity_1 = []
	precision_1_micro = []
	recall_1_micro = []
	precision_1_macro = []
	recall_1_macro = []
	precision_1_weighted = []
	recall_1_weighted = []

	mutual_information_2 = []
	normalized_mutual_information_2 = []
	adjusted_mutual_information_2 = []
	homogeneity_2 = []
	precision_2_micro = []
	recall_2_micro = []
	precision_2_macro = []
	recall_2_macro = []
	precision_2_weighted = []
	recall_2_weighted = []

	silhoutte_level_1_global = []
	silhoutte_level_1_weighted = []
	dunn1_level_1 = []
	dunn2_level_1 = []
	dunn3_level_1 = []

	silhoutte_level_2_global = []
	silhoutte_level_2_weighted = []
	dunn1_level_2 = []
	dunn2_level_2 = []
	dunn3_level_2 = []

	list_of_frms = {}

	for elem in metrics:
		precision_1_micro.append(elem[0]["precision_micro"])
		precision_2_micro.append(elem[1]["precision_micro"])

		precision_1_macro.append(elem[0]["precision_macro"])
		precision_2_macro.append(elem[1]["precision_macro"])

		precision_1_weighted.append(elem[0]["precision_weighted"])
		precision_2_weighted.append(elem[1]["precision_weighted"])

		recall_1_micro.append(elem[0]["recall_micro"])
		recall_2_micro.append(elem[1]["recall_micro"])

		recall_1_macro.append(elem[0]["recall_macro"])
		recall_2_macro.append(elem[1]["recall_macro"])

		recall_1_weighted.append(elem[0]["recall_weighted"])
		recall_2_weighted.append(elem[1]["recall_weighted"])

		mutual_information_1.append(elem[0]["mutual_info_score"])
		mutual_information_2.append(elem[1]["mutual_info_score"])

		normalized_mutual_information_1.append(elem[0]["normalized_mutual_info_score"])
		normalized_mutual_information_2.append(elem[1]["normalized_mutual_info_score"])

		adjusted_mutual_information_1.append(elem[0]["adjusted_mutual_info_score"])
		adjusted_mutual_information_2.append(elem[1]["adjusted_mutual_info_score"])

		homogeneity_1.append(elem[0]["homogeneity_score"])
		homogeneity_2.append(elem[1]["homogeneity_score"])

		silhoutte_level_1_global.append(elem[2]["level1"])
		dunn1_level_1.append(elem[3]["level1"])
		dunn2_level_1.append(elem[4]["level1"])
		dunn3_level_1.append(elem[5]["level1"])

		silhoutte_level_2_global += elem[6]
		dunn1_level_2 += elem[7]
		dunn2_level_2 += elem[8]
		dunn3_level_2 += elem[9]

		viz = elem[10]

		for demonstration in viz.keys():
			utils.dict_insert_list(demonstration, viz[demonstration], list_of_frms)

		silhoutte_level_1_weighted.append(elem[11]["level1"])
		silhoutte_level_2_weighted += elem[12]

	utils.print_and_write_2("precision_1_micro", np.mean(precision_1_micro), np.std(precision_1_micro), file)
	utils.print_and_write_2("precision_2_micro", np.mean(precision_2_micro), np.std(precision_2_micro), file)

	utils.print_and_write_2("precision_1_macro", np.mean(precision_1_macro), np.std(precision_1_macro), file)
	utils.print_and_write_2("precision_2_macro", np.mean(precision_2_macro), np.std(precision_2_macro), file)

	utils.print_and_write_2("precision_1_weighted", np.mean(precision_1_weighted), np.std(precision_1_weighted), file)
	utils.print_and_write_2("precision_2_weighted", np.mean(precision_2_weighted), np.std(precision_2_weighted), file)

	utils.print_and_write_2("recall_1_micro", np.mean(recall_1_micro), np.std(recall_1_micro), file)
	utils.print_and_write_2("recall_2_micro", np.mean(recall_2_micro), np.std(recall_2_micro), file)

	utils.print_and_write_2("recall_1_macro", np.mean(recall_1_macro), np.std(recall_1_macro), file)
	utils.print_and_write_2("recall_2_macro", np.mean(recall_2_macro), np.std(recall_2_macro), file)

	utils.print_and_write_2("recall_1_weighted", np.mean(recall_1_weighted), np.std(recall_1_weighted), file)
	utils.print_and_write_2("recall_2_weighted", np.mean(recall_2_weighted), np.std(recall_2_weighted), file)

	utils.print_and_write_2("mutual_info_1", np.mean(mutual_information_1), np.std(mutual_information_1), file)
	utils.print_and_write_2("mutual_info_2", np.mean(mutual_information_2), np.std(mutual_information_2), file)

	utils.print_and_write_2("normalized_mutual_info_1", np.mean(normalized_mutual_information_1), np.std(normalized_mutual_information_1), file)
	utils.print_and_write_2("normalized_mutual_info_2", np.mean(normalized_mutual_information_2), np.std(normalized_mutual_information_2), file)

	utils.print_and_write_2("adjusted_mutual_info_1", np.mean(adjusted_mutual_information_1), np.std(adjusted_mutual_information_1), file)
	utils.print_and_write_2("adjusted_mutual_info_2", np.mean(adjusted_mutual_information_2), np.std(adjusted_mutual_information_2), file)

	utils.print_and_write_2("homogeneity_1", np.mean(homogeneity_1), np.std(homogeneity_1), file)
	utils.print_and_write_2("homogeneity_2", np.mean(homogeneity_2), np.std(homogeneity_2), file)

	utils.print_and_write_2("silhoutte_level_1_global", np.mean(silhoutte_level_1_global), np.std(silhoutte_level_1_global), file)
	utils.print_and_write_2("silhoutte_level_2_global", np.mean(silhoutte_level_2_global), np.std(silhoutte_level_2_global), file)

	utils.print_and_write_2("silhoutte_level_1_weighted", np.mean(silhoutte_level_1_weighted), np.std(silhoutte_level_1_weighted), file)
	utils.print_and_write_2("silhoutte_level_2_weighted", np.mean(silhoutte_level_2_weighted), np.std(silhoutte_level_2_weighted), file)

	utils.print_and_write_2("dunn1_level1", np.mean(dunn1_level_1), np.std(dunn1_level_1), file)
	utils.print_and_write_2("dunn2_level1", np.mean(dunn2_level_1), np.std(dunn2_level_1), file)
	utils.print_and_write_2("dunn3_level1", np.mean(dunn3_level_1), np.std(dunn3_level_1), file)

	utils.print_and_write_2("dunn1_level2", np.mean(dunn1_level_2), np.std(dunn1_level_2), file)
	utils.print_and_write_2("dunn2_level2", np.mean(dunn2_level_2), np.std(dunn2_level_2), file)
	utils.print_and_write_2("dunn3_level2", np.mean(dunn3_level_2), np.std(dunn3_level_2), file)

	list_of_dtw_values = []
	list_of_norm_dtw_values = []
	list_of_lengths = []

	data_cache = {}

	for demonstration in list_of_demonstrations:
		try:
			list_of_frms_demonstration = list_of_frms[demonstration]

			data = {}

			for i in range(len(list_of_frms_demonstration)):
				data[i] = [elem[0] for elem in list_of_frms_demonstration[i]]

			dtw_score, normalized_dtw_score, length, labels_manual_d, colors_manual_d, labels_automatic_d, colors_automatic_d  = broken_barh.plot_broken_barh(demonstration, data,
				constants.PATH_TO_CLUSTERING_RESULTS + demonstration +"_" + filename + ".jpg", constants.N_COMPONENTS_TIME_ZW)

			list_of_dtw_values.append(dtw_score)
			list_of_norm_dtw_values.append(normalized_dtw_score)
			list_of_lengths.append(length)

			# Inserting manual and annotations labels into data struct before dumping as pickle file
			cache_entry = {}
			cache_entry['changepoints'] = list_of_frms_demonstration
			cache_entry['plot_labels_manual'] = labels_manual_d
			cache_entry['plot_colors_manual'] = colors_manual_d
			cache_entry['plot_labels_automatic'] = labels_automatic_d
			cache_entry['plot_colors_automatic'] = colors_automatic_d
			data_cache[demonstration] = cache_entry

		except:
			print demonstration
			continue

	utils.print_and_write_2("dtw_score", np.mean(list_of_dtw_values), np.std(list_of_dtw_values), file)
	utils.print_and_write_2("dtw_score_normalized", np.mean(list_of_norm_dtw_values), np.std(list_of_norm_dtw_values), file)
	utils.print_and_write(str(list_of_lengths), file)

	pickle.dump(data_cache, open(constants.PATH_TO_CLUSTERING_RESULTS + filename + "_.p", "wb"))
Exemplo n.º 25
0
    # list_of_demonstrations = ['Suturing_E001', 'Suturing_E002','Suturing_E003', 'Suturing_E004', 'Suturing_E005']

    combinations = clustering.get_list_of_demo_combinations(
        list_of_demonstrations)

    feat_fname = args.visual

    metrics_W = []
    metrics_Z = []
    metrics_ZW = []

    log = open(constants.PATH_TO_CLUSTERING_RESULTS + args.fname + ".txt",
               "wb")

    utils.print_and_write("\nXXXXXXXXXXXX Kinematics W XXXXXXXXXXXX\n", log)
    print "\nXXXXXXXXXXXX Kinematics W XXXXXXXXXXXX\n"

    for i in range(len(combinations)):
        utils.print_and_write(
            "\n----------- Combination #" + str(i) + " -------------\n", log)
        print "\n----------- Combination #" + str(i) + " -------------\n"
        print combinations[i]
        mc = KinematicsClustering(False, list(combinations[i]),
                                  args.fname + str(i), log, False, feat_fname)
        metrics_W.append(mc.do_everything())

    utils.print_and_write("\nXXXXXXXXXXXX Visual Z XXXXXXXXXXXX\n", log)
    print "\nXXXXXXXXXXXX Visual Z XXXXXXXXXXXX\n"

    for i in range(len(combinations)):