def load_sprite_image(img_path, rows_cols, n_sprites=None): '''Load a 2D (3D with color channels) sprite image where (rows,cols) = rows_cols, slices, and returns as a 3D tensor (4D with color channels). Sprite shape is computed automatically. If n_sprites is not given, it is assumed to be rows*cols. Return as 3D tensor with shape (n_sprites, sprite_height, sprite_width, sprite_channels). ''' rows, cols = rows_cols if n_sprites is None: n_sprites = rows * cols img = caffe_load_image(img_path, color=True, as_uint=True) assert img.shape[ 0] % rows == 0, 'sprite image has shape %s which is not divisible by rows_cols %' % ( img.shape, rows_cols) assert img.shape[ 1] % cols == 0, 'sprite image has shape %s which is not divisible by rows_cols %' % ( img.shape, rows_cols) sprite_height = img.shape[0] / rows sprite_width = img.shape[1] / cols sprite_channels = img.shape[2] ret = np.zeros((n_sprites, sprite_height, sprite_width, sprite_channels), dtype=img.dtype) for idx in xrange(n_sprites): # Row-major order ii = idx / cols jj = idx % cols ret[idx] = img[ii * sprite_height:(ii + 1) * sprite_height, jj * sprite_width:(jj + 1) * sprite_width, :] return ret
def load_sprite_image(img_path, rows_cols, n_sprites = None): '''Load a 2D (3D with color channels) sprite image where (rows,cols) = rows_cols, slices, and returns as a 3D tensor (4D with color channels). Sprite shape is computed automatically. If n_sprites is not given, it is assumed to be rows*cols. Return as 3D tensor with shape (n_sprites, sprite_height, sprite_width, sprite_channels). ''' rows,cols = rows_cols if n_sprites is None: n_sprites = rows * cols img = caffe_load_image(img_path, color = True, as_uint = True) assert img.shape[0] % rows == 0, 'sprite image has shape %s which is not divisible by rows_cols %' % (img.shape, rows_cols) assert img.shape[1] % cols == 0, 'sprite image has shape %s which is not divisible by rows_cols %' % (img.shape, rows_cols) sprite_height = img.shape[0] / rows sprite_width = img.shape[1] / cols sprite_channels = img.shape[2] ret = np.zeros((n_sprites, sprite_height, sprite_width, sprite_channels), dtype = img.dtype) for idx in xrange(n_sprites): # Row-major order ii = idx / cols jj = idx % cols ret[idx] = img[ii*sprite_height:(ii+1)*sprite_height, jj*sprite_width:(jj+1)*sprite_width, :] return ret
def get_image_from_files(settings, unit_folder_path, should_crop_to_corner, resize_shape, first_only, captions = [], values = []): try: # list unit images unit_images_path = sorted(glob.glob(unit_folder_path)) mega_image = np.zeros((resize_shape[0], resize_shape[1], 3), dtype=np.uint8) # if no images if not unit_images_path: return mega_image if first_only: unit_images_path = [unit_images_path[0]] # load all images unit_images = [caffe_load_image(unit_image_path, color=True, as_uint=True) for unit_image_path in unit_images_path] if settings.caffevis_clear_negative_activations: # clear images with 0 value if values: for i in range(len(values)): if values[i] < float_info.epsilon: unit_images[i] *= 0 if should_crop_to_corner: unit_images = [crop_to_corner(img, 2) for img in unit_images] num_images = len(unit_images) images_per_axis = int(np.math.ceil(np.math.sqrt(num_images))) padding_pixel = 1 if first_only: single_resized_image_shape = (resize_shape[0] - 2*padding_pixel, resize_shape[1] - 2*padding_pixel) else: single_resized_image_shape = ((resize_shape[0] / images_per_axis) - 2*padding_pixel, (resize_shape[1] / images_per_axis) - 2*padding_pixel) unit_images = [ensure_uint255_and_resize_without_fit(unit_image, single_resized_image_shape) for unit_image in unit_images] # build mega image should_add_caption = (len(captions) == num_images) defaults = {'face': settings.caffevis_score_face, 'fsize': settings.caffevis_score_fsize, 'clr': to_255(settings.caffevis_score_clr), 'thick': settings.caffevis_score_thick} for i in range(num_images): # add caption if we have exactly one for each image if should_add_caption: loc = settings.caffevis_score_loc[::-1] # Reverse to OpenCV c,r order fs = FormattedString(captions[i], defaults) cv2_typeset_text(unit_images[i], [[fs]], loc) cell_row = i / images_per_axis cell_col = i % images_per_axis mega_image_height_start = 1 + cell_row * (single_resized_image_shape[0] + 2 * padding_pixel) mega_image_height_end = mega_image_height_start + single_resized_image_shape[0] mega_image_width_start = 1 + cell_col * (single_resized_image_shape[1] + 2 * padding_pixel) mega_image_width_end = mega_image_width_start + single_resized_image_shape[1] mega_image[mega_image_height_start:mega_image_height_end, mega_image_width_start:mega_image_width_end,:] = unit_images[i] return mega_image except: print '\nAttempted to load files from %s but failed. ' % unit_folder_path # set black image as place holder return np.zeros((resize_shape[0], resize_shape[1], 3), dtype=np.uint8) pass return
def run(self): print 'JPGVisLoadingThread.run called' while not self.is_timed_out(): with self.state.lock: if self.state.quit: break #print 'JPGVisLoadingThread.run: caffe_net_state is:', self.state.caffe_net_state #print 'JPGVisLoadingThread.run loop: next_frame: %s, caffe_net_state: %s, back_enabled: %s' % ( # 'None' if self.state.next_frame is None else 'Avail', # self.state.caffe_net_state, # self.state.back_enabled) jpgvis_to_load_key = self.state.jpgvis_to_load_key if jpgvis_to_load_key is None: time.sleep(self.loop_sleep) continue state_layer, state_selected_unit, data_shape = jpgvis_to_load_key # Load three images: images = [None] * 3 # Resize each component images only using one direction as # a constraint. This is straightforward but could be very # wasteful (making an image much larger then much smaller) # if the proportions of the stacked image are very # different from the proportions of the data pane. #resize_shape = (None, data_shape[1]) if self.settings.caffevis_jpgvis_stack_vert else (data_shape[0], None) # As a heuristic, instead just assume the three images are of the same shape. if self.settings.caffevis_jpgvis_stack_vert: resize_shape = (data_shape[0]/3, data_shape[1]) else: resize_shape = (data_shape[0], data_shape[1]/3) # 0. e.g. regularized_opt/conv1/conv1_0037_montage.jpg jpg_path = os.path.join(self.settings.caffevis_unit_jpg_dir, 'regularized_opt', state_layer, '%s_%04d_montage.jpg' % (state_layer, state_selected_unit)) try: img = caffe_load_image(jpg_path, color = True) img_corner = crop_to_corner(img, 2) images[0] = ensure_uint255_and_resize_to_fit(img_corner, resize_shape) except IOError: print '\nAttempted to load file %s but failed. To supress this warning, remove layer "%s" from settings.caffevis_jpgvis_layers' % (jpg_path, state_layer) pass # 1. e.g. max_im/conv1/conv1_0037.jpg jpg_path = os.path.join(self.settings.caffevis_unit_jpg_dir, 'max_im', state_layer, '%s_%04d.jpg' % (state_layer, state_selected_unit)) try: img = caffe_load_image(jpg_path, color=True) images[1] = ensure_uint255_and_resize_to_fit(img, resize_shape) except IOError: print '\nAttempted to load file %s but failed.' % (jpg_path) pass # 2. e.g. max_deconv/conv1/conv1_0037.jpg try: jpg_path = os.path.join(self.settings.caffevis_unit_jpg_dir, 'max_deconv', state_layer, '%s_%04d.jpg' % (state_layer, state_selected_unit)) img = caffe_load_image(jpg_path, color = True) images[2] = ensure_uint255_and_resize_to_fit(img, resize_shape) except IOError: pass # Prune images that were not found: images = [im for im in images if im is not None] # Stack together if len(images) > 0: print 'Stacking:', [im.shape for im in images] stack_axis = 0 if self.settings.caffevis_jpgvis_stack_vert else 1 img_stacked = np.concatenate(images, axis = stack_axis) print 'Stacked:', img_stacked.shape img_resize = ensure_uint255_and_resize_to_fit(img_stacked, data_shape) print 'Resized:', img_resize.shape else: img_resize = np.zeros(shape=(0,)) # Sentinal value when image is not found. self.cache.set(jpgvis_to_load_key, img_resize) with self.state.lock: self.state.jpgvis_to_load_key = None self.state.drawing_stale = True print 'JPGVisLoadingThread.run: finished'
def run(self): print 'JPGVisLoadingThread.run called' while not self.is_timed_out(): with self.state.lock: if self.state.quit: break #print 'JPGVisLoadingThread.run: caffe_net_state is:', self.state.caffe_net_state #print 'JPGVisLoadingThread.run loop: next_frame: %s, caffe_net_state: %s, back_enabled: %s' % ( # 'None' if self.state.next_frame is None else 'Avail', # self.state.caffe_net_state, # self.state.back_enabled) jpgvis_to_load_key = self.state.jpgvis_to_load_key if jpgvis_to_load_key is None: time.sleep(self.loop_sleep) continue state_layer, state_selected_unit, data_shape = jpgvis_to_load_key # Load three images: images = [None] * 3 # Resize each component images only using one direction as # a constraint. This is straightforward but could be very # wasteful (making an image much larger then much smaller) # if the proportions of the stacked image are very # different from the proportions of the data pane. #resize_shape = (None, data_shape[1]) if self.settings.caffevis_jpgvis_stack_vert else (data_shape[0], None) # As a heuristic, instead just assume the three images are of the same shape. if self.settings.caffevis_jpgvis_stack_vert: resize_shape = (data_shape[0] / 3, data_shape[1]) else: resize_shape = (data_shape[0], data_shape[1] / 3) # 0. e.g. regularized_opt/conv1/conv1_0037_montage.jpg jpg_path = os.path.join( self.settings.caffevis_unit_jpg_dir, 'regularized_opt', state_layer, '%s_%04d_montage.jpg' % (state_layer, state_selected_unit)) try: img = caffe_load_image(jpg_path, color=True) img_corner = crop_to_corner(img, 2) images[0] = ensure_uint255_and_resize_to_fit( img_corner, resize_shape) except IOError: print '\nAttempted to load file %s but failed. To supress this warning, remove layer "%s" from settings.caffevis_jpgvis_layers' % ( jpg_path, state_layer) pass # 1. e.g. max_im/conv1/conv1_0037.jpg jpg_path = os.path.join( self.settings.caffevis_unit_jpg_dir, 'max_im', state_layer, '%s_%04d.jpg' % (state_layer, state_selected_unit)) try: img = caffe_load_image(jpg_path, color=True) images[1] = ensure_uint255_and_resize_to_fit(img, resize_shape) except IOError: print '\nAttempted to load file %s but failed.' % (jpg_path) pass # 2. e.g. max_deconv/conv1/conv1_0037.jpg try: jpg_path = os.path.join( self.settings.caffevis_unit_jpg_dir, 'max_deconv', state_layer, '%s_%04d.jpg' % (state_layer, state_selected_unit)) img = caffe_load_image(jpg_path, color=True) images[2] = ensure_uint255_and_resize_to_fit(img, resize_shape) except IOError: pass # Prune images that were not found: images = [im for im in images if im is not None] # Stack together if len(images) > 0: print 'Stacking:', [im.shape for im in images] stack_axis = 0 if self.settings.caffevis_jpgvis_stack_vert else 1 img_stacked = np.concatenate(images, axis=stack_axis) print 'Stacked:', img_stacked.shape img_resize = ensure_uint255_and_resize_to_fit( img_stacked, data_shape) print 'Resized:', img_resize.shape else: img_resize = np.zeros( shape=(0, )) # Sentinal value when image is not found. self.cache.set(jpgvis_to_load_key, img_resize) with self.state.lock: self.state.jpgvis_to_load_key = None self.state.drawing_stale = True print 'JPGVisLoadingThread.run: finished'