def check_increment_and_load_image(self):
        with self.lock:
            if (self.static_file_idx_increment == 0 and
                self.static_file_idx is not None and
                not self.latest_frame_is_from_cam and
                self.latest_static_frame is not None):
                # Skip if a static frame is already loaded and there is no increment
                return

            # available_files - local list of files
            if self.settings.static_files_input_mode == "directory":
                available_files = self.get_files_from_directory()
            elif self.settings.static_files_input_mode == "image_list":
                available_files = self.get_files_from_image_list()
            elif self.settings.static_files_input_mode == "siamese_image_list":
                available_files = self.get_files_from_siamese_image_list()
            else:
                raise Exception(('Error: setting static_files_input_mode has invalid option (%s)' %
                                (self.settings.static_files_input_mode) ))

            #print 'Found files:'
            #for filename in available_files:
            #    print '   %s' % filename
            assert len(available_files) != 0, ('Error: No files found in %s matching %s (current working directory is %s)' %
                                               (self.settings.static_files_dir, self.settings.static_files_regexp, os.getcwd()))
            if self.static_file_idx is None:
                self.static_file_idx = 0
            self.static_file_idx = (self.static_file_idx + self.static_file_idx_increment) % len(available_files)
            self.static_file_idx_increment = 0
            if self.latest_static_filename != available_files[self.static_file_idx] or self.latest_static_frame is None:
                self.latest_static_filename = available_files[self.static_file_idx]

                if self.settings.static_files_input_mode == "siamese_image_list":
                    # loading two images for siamese network
                    im1 = cv2_read_file_rgb(os.path.join(self.settings.static_files_dir, self.latest_static_filename[0]))
                    im2 = cv2_read_file_rgb(os.path.join(self.settings.static_files_dir, self.latest_static_filename[1]))
                    if not self.static_file_stretch_mode:
                        im1 = crop_to_square(im1)
                        im2 = crop_to_square(im2)

                    im = (im1,im2)

                else:
                    im = cv2_read_file_rgb(os.path.join(self.settings.static_files_dir, self.latest_static_filename))
                    if not self.static_file_stretch_mode:
                        im = crop_to_square(im)

                self.latest_static_frame = im
            self._increment_and_set_frame(self.latest_static_frame, False)
예제 #2
0
 def check_increment_and_load_image(self):
     with self.lock:
         if (self.static_file_idx_increment == 0 and
             self.static_file_idx is not None and
             not self.latest_frame_is_from_cam and
             self.latest_static_frame is not None):
             # Skip if a static frame is already loaded and there is no increment
             return
         available_files = []
         match_flags = re.IGNORECASE if self.settings.static_files_ignore_case else 0
         for filename in os.listdir(self.settings.static_files_dir):
             if re.match(self.settings.static_files_regexp, filename, match_flags):
                 available_files.append(filename)
         #print 'Found files:'
         #for filename in available_files:
         #    print '   %s' % filename
         assert len(available_files) != 0, ('Error: No files found in %s matching %s (current working directory is %s)' %
                                            (self.settings.static_files_dir, self.settings.static_files_regexp, os.getcwd()))
         if self.static_file_idx is None:
             self.static_file_idx = 0
         self.static_file_idx = (self.static_file_idx + self.static_file_idx_increment) % len(available_files)
         self.static_file_idx_increment = 0
         if self.latest_static_filename != available_files[self.static_file_idx] or self.latest_static_frame is None:
             self.latest_static_filename = available_files[self.static_file_idx]
             im = cv2_read_file_rgb(os.path.join(self.settings.static_files_dir, self.latest_static_filename))
             if not self.static_file_stretch_mode:
                 im = crop_to_square(im)
             self.latest_static_frame = im
         self._increment_and_set_frame(self.latest_static_frame, False)
예제 #3
0
 def check_increment_and_load_image(self):
     with self.lock:
         if (self.static_file_idx_increment == 0 and
             self.static_file_idx is not None and
             not self.latest_frame_is_from_cam and
             self.latest_static_frame is not None):
             # Skip if a static frame is already loaded and there is no increment
             return
         available_files = []
         match_flags = re.IGNORECASE if self.settings.static_files_ignore_case else 0
         for filename in os.listdir(self.settings.static_files_dir):
             if re.match(self.settings.static_files_regexp, filename, match_flags):
                 available_files.append(filename)
         #print 'Found files:'
         #for filename in available_files:
         #    print '   %s' % filename
         assert len(available_files) != 0, ('Error: No files found in %s matching %s (current working directory is %s)' %
                                            (self.settings.static_files_dir, self.settings.static_files_regexp, os.getcwd()))
         if self.static_file_idx is None:
             self.static_file_idx = 0
         self.static_file_idx = (self.static_file_idx + self.static_file_idx_increment) % len(available_files)
         self.static_file_idx_increment = 0
         if self.latest_static_filename != available_files[self.static_file_idx] or self.latest_static_frame is None:
             self.latest_static_filename = available_files[self.static_file_idx]
             im = cv2_read_file_rgb(os.path.join(self.settings.static_files_dir, self.latest_static_filename))
             if not self.static_file_stretch_mode:
                 im = crop_to_center(im)
                 # im = crop_to_square(im)
             self.latest_static_frame = im
         self._increment_and_set_frame(self.latest_static_frame, False)
         self.latest_mask = np.zeros(self.latest_static_frame.shape[0:2])
         self.latest_mask.fill(255)
    def check_increment_and_load_image(self):
        with self.lock:
            if (self.static_file_idx_increment == 0
                    and self.static_file_idx is not None
                    and not self.latest_frame_is_from_cam
                    and self.latest_static_frame is not None):
                # Skip if a static frame is already loaded and there is no increment
                return

            # available_files - local list of files
            if self.settings.static_files_input_mode == "directory":
                available_files = self.get_files_from_directory()
            elif self.settings.static_files_input_mode == "image_list":
                available_files = self.get_files_from_image_list()
            elif self.settings.static_files_input_mode == "siamese_image_list":
                available_files = self.get_files_from_siamese_image_list()
            else:
                raise Exception((
                    'Error: setting static_files_input_mode has invalid option (%s)'
                    % (self.settings.static_files_input_mode)))

            #print 'Found files:'
            #for filename in available_files:
            #    print '   %s' % filename
            assert len(available_files) != 0, (
                'Error: No files found in %s matching %s (current working directory is %s)'
                % (self.settings.static_files_dir,
                   self.settings.static_files_regexp, os.getcwd()))
            if self.static_file_idx is None:
                self.static_file_idx = 0
            self.static_file_idx = (
                self.static_file_idx +
                self.static_file_idx_increment) % len(available_files)
            self.static_file_idx_increment = 0
            if self.latest_static_filename != available_files[
                    self.static_file_idx] or self.latest_static_frame is None:
                self.latest_static_filename = available_files[
                    self.static_file_idx]

                if self.settings.static_files_input_mode == "siamese_image_list":
                    # loading two images for siamese network
                    im1 = cv2_read_file_rgb(
                        os.path.join(self.settings.static_files_dir,
                                     self.latest_static_filename[0]))
                    im2 = cv2_read_file_rgb(
                        os.path.join(self.settings.static_files_dir,
                                     self.latest_static_filename[1]))
                    if not self.static_file_stretch_mode:
                        im1 = crop_to_square(im1)
                        im2 = crop_to_square(im2)

                    im = (im1, im2)

                else:
                    im = cv2_read_file_rgb(
                        os.path.join(self.settings.static_files_dir,
                                     self.latest_static_filename))
                    if not self.static_file_stretch_mode:
                        im = crop_to_square(im)

                self.latest_static_frame = im
            self._increment_and_set_frame(self.latest_static_frame, False)
예제 #5
0
    def _save_activations(self, panes):
        """
        When the user presses `x`, we save the current activation for each layer to a file. This works for both
          regular activation, and when we've locked to one specific neuron. Note that this is giving RGB values,
          so I'm not sure if those are tweaked versions of the originals.

        import numpy as np
        import pandas as pd
        labels = [i.replace('\n','') for i in open('labels.txt').readlines()]
        df = pd.read_pickle('experiments/nolock.pk')
        
        for i in zip(df['image'], [labels[a.mean(axis=(1,2,3)).argmax()] for a in df['prob']]):
             print i

        """
        
        # Code for getting available images, copied from `input_fetcher`
        available_files = []
        match_flags = re.IGNORECASE if self.settings.static_files_ignore_case else 0
        for filename in os.listdir(self.settings.static_files_dir):
            if re.match(self.settings.static_files_regexp, filename, match_flags):
                available_files.append(filename)

        out_dir = self.settings.out_dir
        if not os.path.isdir(out_dir): os.mkdir(out_dir)

        import pandas as pd
        row_data = []
        import time
        t1 = time.time()

        n = 0
        ncap = 100

        for static_filename in available_files:
            if n > ncap: break

            p = os.path.join(self.settings.static_files_dir, static_filename)
            if static_filename.split('.')[-1] in ('.txt','.sh') or os.path.getsize(p) < 3000:
                print static_filename
                continue

            # Load image from `static_filename`
            try:
                im = cv2_read_file_rgb(p)
            except:
                print 'ERROR: ', static_filename
                continue
            if not self.settings.static_file_stretch_mode:
                im = crop_to_square(im)

            # Update state image
            with self.state.lock:
                self.state.next_frame = im

            # Get meta info about image from pickle file, add to row dict
            img_name = static_filename.split('.')[0]
            if 'meta.pk' in os.listdir(self.settings.static_files_dir): 
                if not hasattr(self, 'meta'):
                    import pickle
                    meta = open(self.settings.static_files_dir + '/meta.pk', 'r')
                    self.meta = pickle.load(meta)

                row = self.meta[img_name]
            else:
                row = {}
                row['image'] = img_name

            # Get activation RGB values for each layer
            for layer in self.state._layers:
                if layer in self.settings.save_layers:
                    with self.state.lock:
                        self.state.layer = layer
                    layer_data = self._draw_layer_pane(panes['caffevis_layers'])
                    if layer == 'fc8':
                        layer_data = layer_data[:,0,0,0]
                    row[layer] = layer_data

            row_data.append(row)
            n += 1


        # Collect rows into pandas dataframe, indexed by: ['image', 'conv1', 'pool1', ...]
        df = pd.DataFrame(row_data)

        # If we've locked this to a specific unit...
        if self.state.backprop_selection_frozen:
            locked_neuron = self.state.backprop_layer + ':' + self.state.backprop_unit
            s = out_dir + '/' + locked_neuron + '.pk'
        else:
            s = out_dir + '/nolock.pk'

        df.to_pickle(s)
        print 'File saved to: ' + s
        t2 = time.time() - t1
        print 'time: {}'.format(t2)

        f = open(out_dir + '/labels.txt','w')
        f.write('\n'.join(self.labels))