Example #1
0
    def save_results(self,
                     params,
                     results,
                     prefix_template,
                     brave=False,
                     skipbig=False):
        if prefix_template is None:
            return

        results_and_params = combine_dicts(
            (('p.', params.__dict__), ('r.', results.__dict__)))
        prefix = prefix_template % results_and_params

        if os.path.isdir(prefix):
            if prefix[-1] != '/':
                prefix += '/'  # append slash for dir-only template
        else:
            dirname = os.path.dirname(prefix)
            if dirname:
                mkdir_p(dirname)

        # Don't overwrite previous results
        if os.path.exists('%sinfo.txt' % prefix) and not brave:
            raise Exception('Cowardly refusing to overwrite ' +
                            '%sinfo.txt' % prefix)

        output_majority = False
        if output_majority:
            if results.majority_xx is not None:
                asimg = results.majority_xx[
                    self.channel_swap_to_rgb].transpose((1, 2, 0))
                saveimagescc('%smajority_X.jpg' % prefix, asimg, 0)
                saveimagesc('%smajority_Xpm.jpg' % prefix,
                            asimg + self._data_mean_rgb_img)  # PlusMean

        if results.best_xx is not None:
            asimg = results.best_xx[self.channel_swap_to_rgb].transpose(
                (1, 2, 0))
            saveimagescc('%sbest_X.jpg' % prefix, asimg, 0)
            saveimagesc('%sbest_Xpm.jpg' % prefix,
                        asimg + self._data_mean_rgb_img)  # PlusMean

        with open('%sinfo.txt' % prefix, 'w') as ff:
            print >> ff, params
            print >> ff
            print >> ff, results
        if not skipbig:
            with open('%sinfo_big.pkl' % prefix, 'w') as ff:
                pickle.dump((params, results), ff, protocol=-1)
        results.trim_arrays()
        with open('%sinfo.pkl' % prefix, 'w') as ff:
            pickle.dump((params, results), ff, protocol=-1)
    def save_results(self, params, results, prefix_template, brave = False, skipbig = False):
        if prefix_template is None:
            return

        results_and_params = combine_dicts((('p.', params.__dict__),
                                            ('r.', results.__dict__)))
        prefix = prefix_template % results_and_params
        
        if os.path.isdir(prefix):
            if prefix[-1] != '/':
                prefix += '/'   # append slash for dir-only template
        else:
            dirname = os.path.dirname(prefix)
            if dirname:
                mkdir_p(dirname)

        # Don't overwrite previous results
        if os.path.exists('%sinfo.txt' % prefix) and not brave:
            raise Exception('Cowardly refusing to overwrite ' + '%sinfo.txt' % prefix)

        output_majority = False
        if output_majority:
            if results.majority_xx is not None:
                asimg = results.majority_xx[self.channel_swap_to_rgb].transpose((1,2,0))
                saveimagescc('%smajority_X.jpg' % prefix, asimg, 0)
                saveimagesc('%smajority_Xpm.jpg' % prefix, asimg + self._data_mean_rgb_img)  # PlusMean

        if results.best_xx is not None:
            asimg = results.best_xx[self.channel_swap_to_rgb].transpose((1,2,0))
            saveimagescc('%sbest_X.jpg' % prefix, asimg, 0)
            saveimagesc('%sbest_Xpm.jpg' % prefix, asimg + self._data_mean_rgb_img)  # PlusMean

        with open('%sinfo.txt' % prefix, 'w') as ff:
            print >>ff, params
            print >>ff
            print >>ff, results
        if not skipbig:
            with open('%sinfo_big.pkl' % prefix, 'w') as ff:
                pickle.dump((params, results), ff, protocol=-1)
        results.trim_arrays()
        with open('%sinfo.pkl' % prefix, 'w') as ff:
            pickle.dump((params, results), ff, protocol=-1)
Example #3
0
    def save_results(self,
                     params,
                     results,
                     prefix_template,
                     brave=False,
                     skipbig=False,
                     skipsmall=False):
        if prefix_template is None:
            return

        for batch_index in range(params.batch_size):

            [best_X_name, best_Xpm_name, majority_X_name, majority_Xpm_name, info_name, info_pkl_name, info_big_pkl_name] = \
                self.generate_output_names(batch_index, params, results, prefix_template, self.settings.caffevis_outputs_dir)

            # Don't overwrite previous results
            if os.path.exists(info_name) and not brave:
                raise Exception('Cowardly refusing to overwrite ' + info_name)

            output_majority = False
            if output_majority:
                # NOTE: this section wasn't tested after changes to code, so some minor index tweaking are in order
                if results[batch_index].majority_xx is not None:
                    asimg = results[batch_index].majority_xx[
                        self.channel_swap_to_rgb].transpose((1, 2, 0))
                    saveimagescc(majority_X_name, asimg, 0)
                    saveimagesc(majority_Xpm_name,
                                asimg + self._data_mean_rgb_img)  # PlusMean

            if results[batch_index].best_xx is not None:
                # results[batch_index].best_xx.shape is (6,224,224)

                def save_output(data, channel_swap_to_rgb, best_X_image_name):
                    # , best_Xpm_image_name, data_mean_rgb_img):
                    asimg = data[channel_swap_to_rgb].transpose((1, 2, 0))
                    saveimagescc(best_X_image_name, asimg, 0)

                # get center position, relative to layer, of best maximum
                [temp_ii, temp_jj] = results[batch_index].idxmax[
                    results[batch_index].best_ii][1:3]

                is_spatial = params.is_spatial
                layer_name = params.push_layer
                size_ii, size_jj = get_max_data_extent(self.net, self.settings,
                                                       layer_name, is_spatial)
                data_size_ii, data_size_jj = self.net.blobs['data'].data.shape[
                    2:4]

                [out_ii_start, out_ii_end, out_jj_start, out_jj_end, data_ii_start, data_ii_end, data_jj_start, data_jj_end] = \
                    compute_data_layer_focus_area(is_spatial, temp_ii, temp_jj, self.settings, layer_name, size_ii, size_jj, data_size_ii, data_size_jj)

                selected_input_index = self.find_selected_input_index(
                    layer_name)

                out_arr = extract_patch_from_image(
                    results[batch_index].best_xx, self.net,
                    selected_input_index, self.settings, data_ii_end,
                    data_ii_start, data_jj_end, data_jj_start, out_ii_end,
                    out_ii_start, out_jj_end, out_jj_start, size_ii, size_jj)

                if self.settings.is_siamese:
                    save_output(
                        out_arr,
                        channel_swap_to_rgb=self.channel_swap_to_rgb[[0, 1,
                                                                      2]],
                        best_X_image_name=best_X_name)
                else:
                    save_output(out_arr,
                                channel_swap_to_rgb=self.channel_swap_to_rgb,
                                best_X_image_name=best_X_name)

                if self.settings.optimize_image_generate_plus_mean:
                    out_arr_pm = extract_patch_from_image(
                        results[batch_index].best_xx + self.batched_data_mean,
                        self.net, selected_input_index, self.settings,
                        data_ii_end, data_ii_start, data_jj_end, data_jj_start,
                        out_ii_end, out_ii_start, out_jj_end, out_jj_start,
                        size_ii, size_jj)

                    if self.settings.is_siamese:
                        save_output(
                            out_arr_pm,
                            channel_swap_to_rgb=self.channel_swap_to_rgb[[
                                0, 1, 2
                            ]],
                            best_X_image_name=best_Xpm_name)
                    else:
                        save_output(
                            out_arr_pm,
                            channel_swap_to_rgb=self.channel_swap_to_rgb,
                            best_X_image_name=best_Xpm_name)

            with open(info_name, 'w') as ff:
                print >> ff, params
                print >> ff
                print >> ff, results[batch_index]
            if not skipbig:
                with open(info_big_pkl_name, 'w') as ff:
                    pickle.dump((params, results[batch_index]),
                                ff,
                                protocol=-1)
            if not skipsmall:
                results[batch_index].trim_arrays()
                with open(info_pkl_name, 'w') as ff:
                    pickle.dump((params, results[batch_index]),
                                ff,
                                protocol=-1)