diff --git a/app_base.py b/app_base.py index 255855c22..f90755ab0 100644 --- a/app_base.py +++ b/app_base.py @@ -1,4 +1,4 @@ -#! /usr/bin/env python +#! /usr/bin/env python3 class BaseApp(object): '''Base App class.''' diff --git a/caffevis/app.py b/caffevis/app.py index 582da50f8..5a34e8e5b 100644 --- a/caffevis/app.py +++ b/caffevis/app.py @@ -6,17 +6,17 @@ import cv2 import numpy as np import time -import StringIO +import io from misc import WithTimer from numpy_cache import FIFOLimitedArrayCache from app_base import BaseApp from image_misc import norm01, norm01c, norm0255, tile_images_normalize, ensure_float01, tile_images_make_tiles, ensure_uint255_and_resize_to_fit, get_tiles_height_width, get_tiles_height_width_ratio from image_misc import FormattedString, cv2_typeset_text, to_255 -from caffe_proc_thread import CaffeProcThread -from jpg_vis_loading_thread import JPGVisLoadingThread -from caffevis_app_state import CaffeVisAppState -from caffevis_helper import get_pretty_layer_name, read_label_file, load_sprite_image, load_square_sprite_image, check_force_backward_true +from .caffe_proc_thread import CaffeProcThread +from .jpg_vis_loading_thread import JPGVisLoadingThread +from .caffevis_app_state import CaffeVisAppState +from .caffevis_helper import get_pretty_layer_name, read_label_file, load_sprite_image, load_square_sprite_image, check_force_backward_true @@ -25,7 +25,7 @@ class CaffeVisApp(BaseApp): def __init__(self, settings, key_bindings): super(CaffeVisApp, self).__init__(settings, key_bindings) - print 'Got settings', settings + print('Got settings', settings) self.settings = settings self.bindings = key_bindings @@ -46,10 +46,10 @@ def __init__(self, settings, key_bindings): import caffe if settings.caffevis_mode_gpu: caffe.set_mode_gpu() - print 'CaffeVisApp mode (in main thread): GPU' + print('CaffeVisApp mode (in main thread): GPU') else: caffe.set_mode_cpu() - print 'CaffeVisApp mode (in main thread): CPU' + print('CaffeVisApp mode (in main thread): CPU') self.net = caffe.Classifier( settings.caffevis_deploy_prototxt, settings.caffevis_network_weights, @@ -58,7 +58,7 @@ def __init__(self, settings, key_bindings): raw_scale = self._range_scale, ) - if isinstance(settings.caffevis_data_mean, basestring): + if isinstance(settings.caffevis_data_mean, str): # If the mean is given as a filename, load the file try: @@ -66,7 +66,7 @@ def __init__(self, settings, key_bindings): if file_extension == ".npy": # load mean from numpy array self._data_mean = np.load(settings.caffevis_data_mean) - print "Loaded mean from numpy file, data_mean.shape: ", self._data_mean.shape + print("Loaded mean from numpy file, data_mean.shape: ", self._data_mean.shape) elif file_extension == ".binaryproto": @@ -76,27 +76,27 @@ def __init__(self, settings, key_bindings): blob.ParseFromString(data) self._data_mean = np.array(caffe.io.blobproto_to_array(blob)) self._data_mean = np.squeeze(self._data_mean) - print "Loaded mean from binaryproto file, data_mean.shape: ", self._data_mean.shape + print("Loaded mean from binaryproto file, data_mean.shape: ", self._data_mean.shape) else: # unknown file extension, trying to load as numpy array self._data_mean = np.load(settings.caffevis_data_mean) - print "Loaded mean from numpy file, data_mean.shape: ", self._data_mean.shape + print("Loaded mean from numpy file, data_mean.shape: ", self._data_mean.shape) except IOError: - print '\n\nCound not load mean file:', settings.caffevis_data_mean - print 'Ensure that the values in settings.py point to a valid model weights file, network' - print 'definition prototxt, and mean. To fetch a default model and mean file, use:\n' - print '$ cd models/caffenet-yos/' - print '$ ./fetch.sh\n\n' + print('\n\nCound not load mean file:', settings.caffevis_data_mean) + print('Ensure that the values in settings.py point to a valid model weights file, network') + print('definition prototxt, and mean. To fetch a default model and mean file, use:\n') + print('$ cd models/caffenet-yos/') + print('$ ./fetch.sh\n\n') raise input_shape = self.net.blobs[self.net.inputs[0]].data.shape[-2:] # e.g. 227x227 # Crop center region (e.g. 227x227) if mean is larger (e.g. 256x256) excess_h = self._data_mean.shape[1] - input_shape[0] excess_w = self._data_mean.shape[2] - input_shape[1] assert excess_h >= 0 and excess_w >= 0, 'mean should be at least as large as %s' % repr(input_shape) - self._data_mean = self._data_mean[:, (excess_h/2):(excess_h/2+input_shape[0]), - (excess_w/2):(excess_w/2+input_shape[1])] + self._data_mean = self._data_mean[:, (excess_h//2):(excess_h//2+input_shape[0]), + (excess_w//2):(excess_w//2+input_shape[1])] elif settings.caffevis_data_mean is None: self._data_mean = None else: @@ -132,7 +132,7 @@ def _populate_net_layer_info(self): keyboard navigation). ''' self.net_layer_info = {} - for key in self.net.blobs.keys(): + for key in list(self.net.blobs.keys()): self.net_layer_info[key] = {} # Conv example: (1, 96, 55, 55) # FC example: (1, 1000) @@ -171,7 +171,7 @@ def get_heartbeats(self): return [self.proc_thread.heartbeat, self.jpgvis_thread.heartbeat] def quit(self): - print 'CaffeVisApp: trying to quit' + print('CaffeVisApp: trying to quit') with self.state.lock: self.state.quit = True @@ -185,24 +185,24 @@ def quit(self): raise Exception('CaffeVisApp: Could not join proc_thread; giving up.') self.proc_thread = None - print 'CaffeVisApp: quitting.' + print('CaffeVisApp: quitting.') def _can_skip_all(self, panes): - return ('caffevis_layers' not in panes.keys()) + return ('caffevis_layers' not in list(panes.keys())) def handle_input(self, input_image, panes): if self.debug_level > 1: - print 'handle_input: frame number', self.handled_frames, 'is', 'None' if input_image is None else 'Available' + print('handle_input: frame number', self.handled_frames, 'is', 'None' if input_image is None else 'Available') self.handled_frames += 1 if self._can_skip_all(panes): return with self.state.lock: if self.debug_level > 1: - print 'CaffeVisApp.handle_input: pushed frame' + print('CaffeVisApp.handle_input: pushed frame') self.state.next_frame = input_image if self.debug_level > 1: - print 'CaffeVisApp.handle_input: caffe_net_state is:', self.state.caffe_net_state + print('CaffeVisApp.handle_input: caffe_net_state is:', self.state.caffe_net_state) def redraw_needed(self): return self.state.redraw_needed() @@ -210,7 +210,7 @@ def redraw_needed(self): def draw(self, panes): if self._can_skip_all(panes): if self.debug_level > 1: - print 'CaffeVisApp.draw: skipping' + print('CaffeVisApp.draw: skipping') return False with self.state.lock: @@ -222,7 +222,7 @@ def draw(self, panes): if do_draw: if self.debug_level > 1: - print 'CaffeVisApp.draw: drawing' + print('CaffeVisApp.draw: drawing') if 'caffevis_control' in panes: self._draw_control_pane(panes['caffevis_control']) @@ -321,26 +321,26 @@ def _draw_status_pane(self, pane): 'thick': self.settings.caffevis_status_thick} loc = self.settings.caffevis_status_loc[::-1] # Reverse to OpenCV c,r order - status = StringIO.StringIO() + status = io.StringIO() fps = self.proc_thread.approx_fps() with self.state.lock: - print >>status, 'pattern' if self.state.pattern_mode else ('back' if self.state.layers_show_back else 'fwd'), - print >>status, '%s:%d |' % (self.state.layer, self.state.selected_unit), + print('pattern' if self.state.pattern_mode else ('back' if self.state.layers_show_back else 'fwd'), end=' ', file=status) + print('%s:%d |' % (self.state.layer, self.state.selected_unit), end=' ', file=status) if not self.state.back_enabled: - print >>status, 'Back: off', + print('Back: off', end=' ', file=status) else: - print >>status, 'Back: %s' % ('deconv' if self.state.back_mode == 'deconv' else 'bprop'), - print >>status, '(from %s_%d, disp %s)' % (self.state.backprop_layer, + print('Back: %s' % ('deconv' if self.state.back_mode == 'deconv' else 'bprop'), end=' ', file=status) + print('(from %s_%d, disp %s)' % (self.state.backprop_layer, self.state.backprop_unit, - self.state.back_filt_mode), - print >>status, '|', - print >>status, 'Boost: %g/%g' % (self.state.layer_boost_indiv, self.state.layer_boost_gamma) + self.state.back_filt_mode), end=' ', file=status) + print('|', end=' ', file=status) + print('Boost: %g/%g' % (self.state.layer_boost_indiv, self.state.layer_boost_gamma), file=status) if fps > 0: - print >>status, '| FPS: %.01f' % fps + print('| FPS: %.01f' % fps, file=status) if self.state.extra_msg: - print >>status, '|', self.state.extra_msg + print('|', self.state.extra_msg, file=status) self.state.extra_msg = '' strings = [FormattedString(line, defaults) for line in status.getvalue().split('\n')] @@ -580,7 +580,7 @@ def _draw_jpgvis_pane(self, pane): # Some may be missing this setting self.settings.caffevis_jpgvis_layers except: - print '\n\nNOTE: you need to upgrade your settings.py and settings_local.py files. See README.md.\n\n' + print('\n\nNOTE: you need to upgrade your settings.py and settings_local.py files. See README.md.\n\n') raise if self.settings.caffevis_jpgvis_remap and state_layer in self.settings.caffevis_jpgvis_remap: diff --git a/caffevis/caffe_proc_thread.py b/caffevis/caffe_proc_thread.py index 82b4bb7dc..a1ea68764 100644 --- a/caffevis/caffe_proc_thread.py +++ b/caffevis/caffe_proc_thread.py @@ -4,7 +4,7 @@ from codependent_thread import CodependentThread from misc import WithTimer -from caffevis_helper import net_preproc_forward +from .caffevis_helper import net_preproc_forward @@ -30,7 +30,7 @@ def __init__(self, settings, net, state, loop_sleep, pause_after_keys, heartbeat def run(self): - print 'CaffeProcThread.run called' + print('CaffeProcThread.run called') frame = None import caffe @@ -40,10 +40,10 @@ def run(self): # CaffeProcThread thread; it is also set in the main thread. if self.mode_gpu: caffe.set_mode_gpu() - print 'CaffeVisApp mode (in CaffeProcThread): GPU' + print('CaffeVisApp mode (in CaffeProcThread): GPU') else: caffe.set_mode_cpu() - print 'CaffeVisApp mode (in CaffeProcThread): CPU' + print('CaffeVisApp mode (in CaffeProcThread): CPU') while not self.is_timed_out(): with self.state.lock: @@ -110,7 +110,7 @@ def run(self): try: self.net.backward_from_layer(backprop_layer, diffs, zero_higher = True) except AttributeError: - print 'ERROR: required bindings (backward_from_layer) not found! Try using the deconv-deep-vis-toolbox branch as described here: https://github.com/yosinski/deep-visualization-toolbox' + print('ERROR: required bindings (backward_from_layer) not found! Try using the deconv-deep-vis-toolbox branch as described here: https://github.com/yosinski/deep-visualization-toolbox') raise else: with WithTimer('CaffeProcThread:deconv', quiet = self.debug_level < 1): @@ -118,7 +118,7 @@ def run(self): try: self.net.deconv_from_layer(backprop_layer, diffs, zero_higher = True) except AttributeError: - print 'ERROR: required bindings (deconv_from_layer) not found! Try using the deconv-deep-vis-toolbox branch as described here: https://github.com/yosinski/deep-visualization-toolbox' + print('ERROR: required bindings (deconv_from_layer) not found! Try using the deconv-deep-vis-toolbox branch as described here: https://github.com/yosinski/deep-visualization-toolbox') raise with self.state.lock: @@ -135,8 +135,8 @@ def run(self): else: time.sleep(self.loop_sleep) - print 'CaffeProcThread.run: finished' - print 'CaffeProcThread.run: processed %d frames fwd, %d frames back' % (self.frames_processed_fwd, self.frames_processed_back) + print('CaffeProcThread.run: finished') + print('CaffeProcThread.run: processed %d frames fwd, %d frames back' % (self.frames_processed_fwd, self.frames_processed_back)) def approx_fps(self): '''Get the approximate frames per second processed by this diff --git a/caffevis/caffevis_app_state.py b/caffevis/caffevis_app_state.py index 904096d53..8690fda8c 100644 --- a/caffevis/caffevis_app_state.py +++ b/caffevis/caffevis_app_state.py @@ -10,13 +10,13 @@ def __init__(self, net, settings, bindings, net_layer_info): self.lock = Lock() # State is accessed in multiple threads self.settings = settings self.bindings = bindings - self._layers = net.blobs.keys() + self._layers = list(net.blobs.keys()) self._layers = self._layers[1:] # chop off data layer if hasattr(self.settings, 'caffevis_filter_layers'): for name in self._layers: if self.settings.caffevis_filter_layers(name): - print ' Layer filtered out by caffevis_filter_layers: %s' % name - self._layers = filter(lambda name: not self.settings.caffevis_filter_layers(name), self._layers) + print(' Layer filtered out by caffevis_filter_layers: %s' % name) + self._layers = [name for name in self._layers if not self.settings.caffevis_filter_layers(name)] self.net_layer_info = net_layer_info self.layer_boost_indiv_choices = self.settings.caffevis_boost_indiv_choices # 0-1, 0 is noop self.layer_boost_gamma_choices = self.settings.caffevis_boost_gamma_choices # 0-inf, 1 is noop @@ -106,7 +106,7 @@ def handle_key(self, key): elif tag == 'pattern_mode': self.pattern_mode = not self.pattern_mode if self.pattern_mode and not hasattr(self.settings, 'caffevis_unit_jpg_dir'): - print 'Cannot switch to pattern mode; caffevis_unit_jpg_dir not defined in settings.py.' + print('Cannot switch to pattern mode; caffevis_unit_jpg_dir not defined in settings.py.') self.pattern_mode = False elif tag == 'show_back': # If in pattern mode: switch to fwd/back. Else toggle fwd/back mode diff --git a/caffevis/caffevis_helper.py b/caffevis/caffevis_helper.py index b2880f5bf..c054d7f5c 100644 --- a/caffevis/caffevis_helper.py +++ b/caffevis/caffevis_helper.py @@ -34,7 +34,7 @@ def get_pretty_layer_name(settings, layer_name): if hasattr(settings, 'caffevis_layer_pretty_name_fn'): ret = settings.caffevis_layer_pretty_name_fn(ret) if ret != layer_name: - print ' Prettified layer name: "%s" -> "%s"' % (layer_name, ret) + print(' Prettified layer name: "%s" -> "%s"' % (layer_name, ret)) return ret @@ -53,7 +53,7 @@ def crop_to_corner(img, corner, small_padding = 1, large_padding = 2): assert corner in (0,1,2,3), 'specify corner 0, 1, 2, or 3' assert img.shape[0] == img.shape[1], 'img is not square' assert img.shape[0] % 2 == 0, 'even number of pixels assumption violated' - half_size = img.shape[0]/2 + half_size = img.shape[0]//2 big_ii = 0 if corner in (0,1) else 1 big_jj = 0 if corner in (0,2) else 1 tp = small_padding + large_padding @@ -77,14 +77,14 @@ def load_sprite_image(img_path, rows_cols, n_sprites = None): 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_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): + for idx in range(n_sprites): # Row-major order - ii = idx / cols + ii = idx // cols jj = idx % cols ret[idx] = img[ii*sprite_height:(ii+1)*sprite_height, jj*sprite_width:(jj+1)*sprite_width, :] @@ -114,10 +114,10 @@ def check_force_backward_true(prototxt_file): break if not found: - print '\n\nWARNING: the specified prototxt' - print '"%s"' % prototxt_file - print 'does not contain the line "force_backward: true". This may result in backprop' - print 'and deconv producing all zeros at the input layer. You may want to add this line' - print 'to your prototxt file before continuing to force backprop to compute derivatives' - print 'at the data layer as well.\n\n' + print('\n\nWARNING: the specified prototxt') + print('"%s"' % prototxt_file) + print('does not contain the line "force_backward: true". This may result in backprop') + print('and deconv producing all zeros at the input layer. You may want to add this line') + print('to your prototxt file before continuing to force backprop to compute derivatives') + print('at the data layer as well.\n\n') diff --git a/caffevis/jpg_vis_loading_thread.py b/caffevis/jpg_vis_loading_thread.py index 1bc69c52a..0c3a134cd 100644 --- a/caffevis/jpg_vis_loading_thread.py +++ b/caffevis/jpg_vis_loading_thread.py @@ -4,7 +4,7 @@ from codependent_thread import CodependentThread from image_misc import caffe_load_image, ensure_uint255_and_resize_to_fit -from caffevis_helper import crop_to_corner +from .caffevis_helper import crop_to_corner @@ -23,7 +23,7 @@ def __init__(self, settings, state, cache, loop_sleep, heartbeat_required): self.debug_level = 0 def run(self): - print 'JPGVisLoadingThread.run called' + print('JPGVisLoadingThread.run called') while not self.is_timed_out(): with self.state.lock: @@ -69,7 +69,7 @@ def run(self): 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) + 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 @@ -114,4 +114,4 @@ def run(self): self.state.jpgvis_to_load_key = None self.state.drawing_stale = True - print 'JPGVisLoadingThread.run: finished' + print('JPGVisLoadingThread.run: finished') diff --git a/codependent_thread.py b/codependent_thread.py index 39e9ef0eb..88fa97606 100644 --- a/codependent_thread.py +++ b/codependent_thread.py @@ -19,7 +19,7 @@ def is_timed_out(self): with self.heartbeat_lock: now = time.time() if now - self.last_beat > self.heartbeat_timeout: - print '%s instance %s timed out after %s seconds (%s - %s = %s)' % (self.__class__.__name__, self, self.heartbeat_timeout, now, self.last_beat, now - self.last_beat) + print('%s instance %s timed out after %s seconds (%s - %s = %s)' % (self.__class__.__name__, self, self.heartbeat_timeout, now, self.last_beat, now - self.last_beat)) return True else: return False diff --git a/find_maxes/caffe_misc.py b/find_maxes/caffe_misc.py index d126650bb..fbe1bf6e3 100644 --- a/find_maxes/caffe_misc.py +++ b/find_maxes/caffe_misc.py @@ -8,22 +8,22 @@ def shownet(net): '''Print some stats about a net and its activations''' - print '%-41s%-31s%s' % ('', 'acts', 'act diffs') - print '%-45s%-31s%s' % ('', 'params', 'param diffs') - for k, v in net.blobs.items(): + print('%-41s%-31s%s' % ('', 'acts', 'act diffs')) + print('%-45s%-31s%s' % ('', 'params', 'param diffs')) + for k, v in list(net.blobs.items()): if k in net.params: params = net.params[k] for pp, blob in enumerate(params): if pp == 0: - print ' ', 'P: %-5s'%k, + print(' ', 'P: %-5s'%k, end=' ') else: - print ' ' * 11, - print '%-32s' % repr(blob.data.shape), - print '%-30s' % ('(%g, %g)' % (blob.data.min(), blob.data.max())), - print '(%g, %g)' % (blob.diff.min(), blob.diff.max()) - print '%-5s'%k, '%-34s' % repr(v.data.shape), - print '%-30s' % ('(%g, %g)' % (v.data.min(), v.data.max())), - print '(%g, %g)' % (v.diff.min(), v.diff.max()) + print(' ' * 11, end=' ') + print('%-32s' % repr(blob.data.shape), end=' ') + print('%-30s' % ('(%g, %g)' % (blob.data.min(), blob.data.max())), end=' ') + print('(%g, %g)' % (blob.diff.min(), blob.diff.max())) + print('%-5s'%k, '%-34s' % repr(v.data.shape), end=' ') + print('%-30s' % ('(%g, %g)' % (v.data.min(), v.data.max())), end=' ') + print('(%g, %g)' % (v.diff.min(), v.diff.max())) @@ -112,10 +112,10 @@ def convert_region(self, from_layer, to_layer, region, verbose = False): for ii in range(from_idx, to_idx, -1): converter = self.converters[ii] if verbose: - print 'pushing', self.names[ii], 'region', ret, 'through converter' + print('pushing', self.names[ii], 'region', ret, 'through converter') ret = converter(ret) if verbose: - print 'Final region at ', self.names[to_idx], 'is', ret + print('Final region at ', self.names[to_idx], 'is', ret) return ret diff --git a/find_maxes/crop_max_patches.py b/find_maxes/crop_max_patches.py index 20161ace9..4398cea8d 100644 --- a/find_maxes/crop_max_patches.py +++ b/find_maxes/crop_max_patches.py @@ -2,7 +2,7 @@ import argparse import ipdb as pdb -import cPickle as pickle +import pickle as pickle from loaders import load_imagenet_mean, load_labels, caffe from jby_misc import WithTimer diff --git a/find_maxes/find_max_acts.py b/find_maxes/find_max_acts.py index c86eb2285..36e46a10f 100644 --- a/find_maxes/find_max_acts.py +++ b/find_maxes/find_max_acts.py @@ -2,7 +2,7 @@ import argparse import ipdb as pdb -import cPickle as pickle +import pickle as pickle from loaders import load_imagenet_mean, load_labels, caffe from jby_misc import WithTimer diff --git a/find_maxes/jby_misc.py b/find_maxes/jby_misc.py index bc95f4667..dad86de23 100644 --- a/find_maxes/jby_misc.py +++ b/find_maxes/jby_misc.py @@ -24,17 +24,17 @@ def __enter__(self): def __exit__(self, *args): if not self.quiet: titlestr = self.title + ' ' if self.title else '' - print 'Elapsed %sreal: %.06f, sys: %.06f' % ((titlestr,) + self.elapsed()) + print('Elapsed %sreal: %.06f, sys: %.06f' % ((titlestr,) + self.elapsed())) def misc_main(): - print 'Running quick demo' + print('Running quick demo') - print ' -> Before with statement' + print(' -> Before with statement') with WithTimer('sleepy time'): - print ' -> About to sleep' + print(' -> About to sleep') time.sleep(.5) - print ' -> Done sleeping' - print ' -> After with statement' + print(' -> Done sleeping') + print(' -> After with statement') if __name__ == '__main__': diff --git a/find_maxes/loaders.py b/find_maxes/loaders.py index a714da701..f911ef50b 100644 --- a/find_maxes/loaders.py +++ b/find_maxes/loaders.py @@ -6,7 +6,7 @@ caffe_root = '../../' # this file is expected to be in {caffe_root}/experiments/something import sys loadpath = caffe_root + 'python_cpu' -print '= = = CAFFE LOADER: LOADING CPU VERSION from path: %s = = =' % loadpath +print('= = = CAFFE LOADER: LOADING CPU VERSION from path: %s = = =' % loadpath) sys.path.insert(0, loadpath) # Use CPU compiled code for backprop vis/etc import caffe caffe.set_mode_cpu() @@ -27,9 +27,9 @@ def load_trained_net(model_prototxt = None, model_weights = None): model_prototxt = load_dir + 'deploy_1.prototxt' model_weights = load_dir + 'caffe_imagenet_train_iter_450000' - print 'LOADER: loading net:' - print ' ', model_prototxt - print ' ', model_weights + print('LOADER: loading net:') + print(' ', model_prototxt) + print(' ', model_weights) net = caffe.Classifier(model_prototxt, model_weights) #net.set_phase_test() diff --git a/find_maxes/max_tracker.py b/find_maxes/max_tracker.py index 8bfd427de..131b3d60c 100644 --- a/find_maxes/max_tracker.py +++ b/find_maxes/max_tracker.py @@ -67,7 +67,7 @@ def update(self, blob, image_idx, image_class): #insertion_idx = zeros((n_channels,)) #pdb.set_trace() - for ii in xrange(n_channels): + for ii in range(n_channels): idx = np.searchsorted(self.max_vals[ii], data_unroll[ii, maxes[ii]]) if idx == 0: # Smaller than all 10 @@ -128,17 +128,17 @@ def load_file_list(filelist): def scan_images_for_maxes(net, datadir, filelist, n_top): image_filenames, image_labels = load_file_list(filelist) - print 'Scanning %d files' % len(image_filenames) - print ' First file', os.path.join(datadir, image_filenames[0]) + print('Scanning %d files' % len(image_filenames)) + print(' First file', os.path.join(datadir, image_filenames[0])) tracker = NetMaxTracker(n_top = n_top) - for image_idx in xrange(len(image_filenames)): + for image_idx in range(len(image_filenames)): filename = image_filenames[image_idx] image_class = image_labels[image_idx] #im = caffe.io.load_image('../../data/ilsvrc12/mini_ilsvrc_valid/sized/ILSVRC2012_val_00000610.JPEG') do_print = (image_idx % 100 == 0) if do_print: - print '%s Image %d/%d' % (datetime.now().ctime(), image_idx, len(image_filenames)) + print('%s Image %d/%d' % (datetime.now().ctime(), image_idx, len(image_filenames))) with WithTimer('Load image', quiet = not do_print): im = caffe.io.load_image(os.path.join(datadir, filename)) with WithTimer('Predict ', quiet = not do_print): @@ -146,7 +146,7 @@ def scan_images_for_maxes(net, datadir, filelist, n_top): with WithTimer('Update ', quiet = not do_print): tracker.update(net, image_idx, image_class) - print 'done!' + print('done!') return tracker @@ -156,10 +156,10 @@ def save_representations(net, datadir, filelist, layer, first_N = None): if first_N is None: first_N = len(image_filenames) assert first_N <= len(image_filenames) - image_indices = range(first_N) - print 'Scanning %d files' % len(image_indices) + image_indices = list(range(first_N)) + print('Scanning %d files' % len(image_indices)) assert len(image_indices) > 0 - print ' First file', os.path.join(datadir, image_filenames[image_indices[0]]) + print(' First file', os.path.join(datadir, image_filenames[image_indices[0]])) indices = None rep = None @@ -168,7 +168,7 @@ def save_representations(net, datadir, filelist, layer, first_N = None): image_class = image_labels[image_idx] do_print = (image_idx % 10 == 0) if do_print: - print '%s Image %d/%d' % (datetime.now().ctime(), image_idx, len(image_indices)) + print('%s Image %d/%d' % (datetime.now().ctime(), image_idx, len(image_indices))) with WithTimer('Load image', quiet = not do_print): im = caffe.io.load_image(os.path.join(datadir, filename)) with WithTimer('Predict ', quiet = not do_print): @@ -181,7 +181,7 @@ def save_representations(net, datadir, filelist, layer, first_N = None): indices[ii] = image_idx rep[ii] = net.blobs[layer].data[0] - print 'done!' + print('done!') return indices,rep @@ -207,8 +207,8 @@ def output_max_patches(max_tracker, net, layer, idx_begin, idx_end, num_top, dat rc = RegionComputer() image_filenames, image_labels = load_file_list(filelist) - print 'Loaded filenames and labels for %d files' % len(image_filenames) - print ' First file', os.path.join(datadir, image_filenames[0]) + print('Loaded filenames and labels for %d files' % len(image_filenames)) + print(' First file', os.path.join(datadir, image_filenames[0])) num_top_in_mt = mt.max_locs.shape[1] assert num_top <= num_top_in_mt, 'Requested %d top images but MaxTracker contains only %d' % (num_top, num_top_in_mt) @@ -225,7 +225,7 @@ def output_max_patches(max_tracker, net, layer, idx_begin, idx_end, num_top, dat if do_info: info_filename = os.path.join(unit_dir, 'info.txt') info_file = open(info_filename, 'w') - print >>info_file, '# is_conv val image_idx image_class i(if is_conv) j(if is_conv) filename' + print('# is_conv val image_idx image_class i(if is_conv) j(if is_conv) filename', file=info_file) # iterate through maxes from highest (at end) to lowest for max_idx_0 in range(num_top): @@ -238,7 +238,7 @@ def output_max_patches(max_tracker, net, layer, idx_begin, idx_end, num_top, dat filename = image_filenames[im_idx] do_print = (max_idx_0 == 0) if do_print: - print '%s Output file/image(s) %d/%d' % (datetime.now().ctime(), cc * num_top, n_total_images) + print('%s Output file/image(s) %d/%d' % (datetime.now().ctime(), cc * num_top, n_total_images)) if mt.is_conv: # Compute the focus area of the data layer @@ -272,12 +272,12 @@ def output_max_patches(max_tracker, net, layer, idx_begin, idx_end, num_top, dat if do_info: - print >>info_file, 1 if mt.is_conv else 0, '%.6f' % mt.max_vals[channel_idx, max_idx], + print(1 if mt.is_conv else 0, '%.6f' % mt.max_vals[channel_idx, max_idx], end=' ', file=info_file) if mt.is_conv: - print >>info_file, '%d %d %d %d' % tuple(mt.max_locs[channel_idx, max_idx]), + print('%d %d %d %d' % tuple(mt.max_locs[channel_idx, max_idx]), end=' ', file=info_file) else: - print >>info_file, '%d %d' % tuple(mt.max_locs[channel_idx, max_idx]), - print >>info_file, filename + print('%d %d' % tuple(mt.max_locs[channel_idx, max_idx]), end=' ', file=info_file) + print(filename, file=info_file) if not (do_maxes or do_deconv or do_deconv_norm or do_backprop or do_backprop_norm): continue @@ -293,7 +293,7 @@ def output_max_patches(max_tracker, net, layer, idx_begin, idx_end, num_top, dat else: reproduced_val = net.blobs[layer].data[0,channel_idx] if abs(reproduced_val - recorded_val) > .1: - print 'Warning: recorded value %s is suspiciously different from reproduced value %s. Is the filelist the same?' % (recorded_val, reproduced_val) + print('Warning: recorded value %s is suspiciously different from reproduced value %s. Is the filelist the same?' % (recorded_val, reproduced_val)) if do_maxes: #grab image from data layer, not from im (to ensure preprocessing / center crop details match between image and deconv/backprop) @@ -315,7 +315,7 @@ def output_max_patches(max_tracker, net, layer, idx_begin, idx_end, num_top, dat out_arr = np.zeros((3,size_ii,size_jj), dtype='float32') out_arr[:, out_ii_start:out_ii_end, out_jj_start:out_jj_end] = net.blobs['data'].diff[0,:,data_ii_start:data_ii_end,data_jj_start:data_jj_end] if out_arr.max() == 0: - print 'Warning: Deconv out_arr in range', out_arr.min(), 'to', out_arr.max(), 'ensure force_backward: true in prototxt' + print('Warning: Deconv out_arr in range', out_arr.min(), 'to', out_arr.max(), 'ensure force_backward: true in prototxt') if do_deconv: with WithTimer('Save img ', quiet = not do_print): save_caffe_image(out_arr, os.path.join(unit_dir, 'deconv_%03d.png' % max_idx_0), @@ -334,7 +334,7 @@ def output_max_patches(max_tracker, net, layer, idx_begin, idx_end, num_top, dat out_arr = np.zeros((3,size_ii,size_jj), dtype='float32') out_arr[:, out_ii_start:out_ii_end, out_jj_start:out_jj_end] = net.blobs['data'].diff[0,:,data_ii_start:data_ii_end,data_jj_start:data_jj_end] if out_arr.max() == 0: - print 'Warning: Deconv out_arr in range', out_arr.min(), 'to', out_arr.max(), 'ensure force_backward: true in prototxt' + print('Warning: Deconv out_arr in range', out_arr.min(), 'to', out_arr.max(), 'ensure force_backward: true in prototxt') if do_backprop: with WithTimer('Save img ', quiet = not do_print): save_caffe_image(out_arr, os.path.join(unit_dir, 'backprop_%03d.png' % max_idx_0), diff --git a/image_misc.py b/image_misc.py index 6c940d8cc..b5d10b27c 100644 --- a/image_misc.py +++ b/image_misc.py @@ -1,4 +1,4 @@ -#! /usr/bin/env python +#! /usr/bin/env python3 import cv2 import numpy as np @@ -80,11 +80,11 @@ def crop_to_square(frame): i_size,j_size = frame.shape[0],frame.shape[1] if j_size > i_size: # landscape - offset = (j_size - i_size) / 2 + offset = (j_size - i_size) // 2 return frame[:,offset:offset+i_size,:] else: # portrait - offset = (i_size - j_size) / 2 + offset = (i_size - j_size) // 2 return frame[offset:offset+j_size,:,:] @@ -419,7 +419,7 @@ def cv2_typeset_text(data, lines, loc, between = ' ', string_spacing = 0, line_s if fs.align == 'right': locx += fs.width - boxsize[0] elif fs.align == 'center': - locx += (fs.width - boxsize[0])/2 + locx += (fs.width - boxsize[0])//2 #print 'right boundary is', locx + boxsize[0], '(%s)' % fs.string # print 'HERE' right_edge = locx + boxsize[0] diff --git a/input_fetcher.py b/input_fetcher.py index 95eb7a9fd..0370fa1f5 100644 --- a/input_fetcher.py +++ b/input_fetcher.py @@ -53,28 +53,28 @@ def __init__(self, settings): def bind_camera(self): # Due to OpenCV limitations, this should be called from the main thread - print 'InputImageFetcher: bind_camera starting' + print('InputImageFetcher: bind_camera starting') if self.no_cam_present: - print 'InputImageFetcher: skipping camera bind (device: None)' + print('InputImageFetcher: skipping camera bind (device: None)') else: self.bound_cap_device = cv2.VideoCapture(self.capture_device) if self.bound_cap_device.isOpened(): - print 'InputImageFetcher: capture device %s is open' % self.capture_device + print('InputImageFetcher: capture device %s is open' % self.capture_device) else: - print '\n\nWARNING: InputImageFetcher: capture device %s failed to open! Camera will not be available!\n\n' % self.capture_device + print('\n\nWARNING: InputImageFetcher: capture device %s failed to open! Camera will not be available!\n\n' % self.capture_device) self.bound_cap_device = None self.no_cam_present = True - print 'InputImageFetcher: bind_camera finished' + print('InputImageFetcher: bind_camera finished') def free_camera(self): # Due to OpenCV limitations, this should be called from the main thread if self.no_cam_present: - print 'InputImageFetcher: skipping camera free (device: None)' + print('InputImageFetcher: skipping camera free (device: None)') else: - print 'InputImageFetcher: freeing camera' + print('InputImageFetcher: freeing camera') del self.bound_cap_device # free the camera self.bound_cap_device = None - print 'InputImageFetcher: camera freed' + print('InputImageFetcher: camera freed') def set_mode_static(self): with self.lock: @@ -83,7 +83,7 @@ def set_mode_static(self): def set_mode_cam(self): with self.lock: if self.no_cam_present: - print 'WARNING: ignoring set_mode_cam, no cam present' + print('WARNING: ignoring set_mode_cam, no cam present') else: self.static_file_mode = False assert self.bound_cap_device != None, 'Call bind_camera first' @@ -137,7 +137,7 @@ def run(self): time.sleep(self.sleep_after_read_frame) #print 'Reading one frame took', time.time() - start_time - print 'InputImageFetcher: exiting run method' + print('InputImageFetcher: exiting run method') #print 'InputImageFetcher: read', self.read_frames, 'frames' def get_frame(self): diff --git a/keys.py b/keys.py index 6293604a3..28c44240d 100644 --- a/keys.py +++ b/keys.py @@ -68,9 +68,9 @@ if key_code in key_patterns[mask]: old_label = key_patterns[mask][code] if old_label != label: - print 'Warning: key_patterns[%s][%s] old value %s being overwritten with %s' % (mask, key_code, old_label, label) + print('Warning: key_patterns[%s][%s] old value %s being overwritten with %s' % (mask, key_code, old_label, label)) if key_code != (key_code & mask): - print 'Warning: key_code %s for key label %s will never trigger using mask %s' % (key_code, label, mask) + print('Warning: key_code %s for key label %s will never trigger using mask %s' % (key_code, label, mask)) key_patterns[mask][key_code] = label #if not label in key_patterns[mask]: # key_patterns[mask][label] = set() diff --git a/live_vis.py b/live_vis.py index 9dde3be99..62e138ac0 100644 --- a/live_vis.py +++ b/live_vis.py @@ -1,4 +1,4 @@ -#! /usr/bin/env python +#! /usr/bin/env python3 import sys import importlib @@ -11,7 +11,7 @@ try: import cv2 except ImportError: - print 'Error: Could not import cv2, please install it first.' + print('Error: Could not import cv2, please install it first.') raise from misc import WithTimer @@ -53,12 +53,12 @@ def __init__(self, settings): for module_path, app_name in settings.installed_apps: module = importlib.import_module(module_path) - print 'got module', module + print('got module', module) app_class = getattr(module, app_name) - print 'got app', app_class + print('got app', app_class) self.app_classes[app_name] = app_class - for app_name, app_class in self.app_classes.iteritems(): + for app_name, app_class in self.app_classes.items(): app = app_class(settings, self.bindings) self.apps[app_name] = app self.help_mode = False @@ -102,7 +102,7 @@ def init_window(self): (max_i,max_j,1)) #print 'BUFFER IS:', self.window_buffer.shape, self.window_buffer.min(), self.window_buffer.max() - for _,pane in self.panes.iteritems(): + for _,pane in self.panes.items(): pane.data = self.window_buffer[pane.i_begin:pane.i_end, pane.j_begin:pane.j_end] # Allocate help pane @@ -125,8 +125,8 @@ def run_loop(self): self.input_updater.start() heartbeat_functions = [self.input_updater.heartbeat] - for app_name, app in self.apps.iteritems(): - print 'Starting app:', app_name + for app_name, app in self.apps.items(): + print('Starting app:', app_name) app.start() heartbeat_functions.extend(app.get_heartbeats()) @@ -178,13 +178,13 @@ def run_loop(self): key,do_redraw = self.handle_key_pre_apps(key) redraw_needed |= do_redraw imshow_needed |= do_redraw - for app_name, app in self.apps.iteritems(): + for app_name, app in self.apps.items(): with WithTimer('%s:handle_key' % app_name, quiet = self.debug_level < 1): key = app.handle_key(key, self.panes) key = self.handle_key_post_apps(key) if self.quit: break - for app_name, app in self.apps.iteritems(): + for app_name, app in self.apps.items(): redraw_needed |= app.redraw_needed() # Grab latest frame from input_updater thread @@ -204,7 +204,7 @@ def run_loop(self): since_keypress >= self.settings.keypress_pause_handle_iterations) if frame_for_apps is not None and do_handle_input: # Pass frame to apps for processing - for app_name, app in self.apps.iteritems(): + for app_name, app in self.apps.items(): with WithTimer('%s:handle_input' % app_name, quiet = self.debug_level < 1): app.handle_input(latest_frame_data, self.panes) frame_for_apps = None @@ -214,7 +214,7 @@ def run_loop(self): (since_keypress >= self.settings.keypress_pause_redraw_iterations or since_redraw >= self.settings.redraw_at_least_every)) if redraw_needed and do_redraw: - for app_name, app in self.apps.iteritems(): + for app_name, app in self.apps.items(): with WithTimer('%s:draw' % app_name, quiet = self.debug_level < 1): imshow_needed |= app.draw(self.panes) redraw_needed = False @@ -224,8 +224,8 @@ def run_loop(self): if imshow_needed: # Only redraw pane debug if display will be updated if hasattr(self.settings, 'debug_window_panes') and self.settings.debug_window_panes: - for pane_name,pane in self.panes.iteritems(): - print pane_name, pane + for pane_name,pane in self.panes.items(): + print(pane_name, pane) pane.data[:] = pane.data * .5 line = [FormattedString('%s |' % pane_name, self.debug_pane_defaults), FormattedString('pos: %d,%d |' % (pane.i_begin, pane.j_begin), self.debug_pane_defaults), @@ -255,7 +255,7 @@ def run_loop(self): # Extra sleep just for debugging. In production all main loop sleep should be in cv2.waitKey. #time.sleep(2) - print '\n\nTrying to exit run_loop...' + print('\n\nTrying to exit run_loop...') self.input_updater.quit = True self.input_updater.join(.01 + float(self.settings.input_updater_sleep_after_read_frame) * 5) if self.input_updater.is_alive(): @@ -263,11 +263,11 @@ def run_loop(self): else: self.input_updater.free_camera() - for app_name, app in self.apps.iteritems(): - print 'Quitting app:', app_name + for app_name, app in self.apps.items(): + print('Quitting app:', app_name) app.quit() - print 'Input thread joined and apps quit; exiting run_loop.' + print('Input thread joined and apps quit; exiting run_loop.') def handle_key_pre_apps(self, key): tag = self.bindings.get_tag(key) @@ -289,10 +289,10 @@ def handle_key_pre_apps(self, key): self.help_mode = not self.help_mode elif tag == 'stretch_mode': self.input_updater.toggle_stretch_mode() - print 'Stretch mode is now', self.input_updater.static_file_stretch_mode + print('Stretch mode is now', self.input_updater.static_file_stretch_mode) elif tag == 'debug_level': self.debug_level = (self.debug_level + 1) % 3 - for app_name, app in self.apps.iteritems(): + for app_name, app in self.apps.items(): app.set_debug(self.debug_level) else: return key, False @@ -308,12 +308,12 @@ def handle_key_post_apps(self, key): key_label, masked_vals = self.bindings.get_key_label_from_keycode(key, extra_info = True) masked_vals_pp = ', '.join(['%d (%s)' % (mv, hex(mv)) for mv in masked_vals]) if key_label is None: - print 'Got key code %d (%s), did not match any known key (masked vals tried: %s)' % (key, hex(key), masked_vals_pp) + print('Got key code %d (%s), did not match any known key (masked vals tried: %s)' % (key, hex(key), masked_vals_pp)) elif tag is None: - print 'Got key code %d (%s), matched key "%s", but key is not bound to any function' % (key, hex(key), key_label) + print('Got key code %d (%s), matched key "%s", but key is not bound to any function' % (key, hex(key), key_label)) else: - print 'Got key code %d (%s), matched key "%s", bound to "%s", but nobody handled "%s"' % ( - key, hex(key), key_label, tag, tag) + print('Got key code %d (%s), matched key "%s", bound to "%s", but nobody handled "%s"' % ( + key, hex(key), key_label, tag, tag)) def display_frame(self, frame): if self.settings.static_files_input_mode == "siamese_image_list": @@ -350,10 +350,10 @@ def draw_help(self): locy = cv2_typeset_text(self.help_pane.data, lines, loc, line_spacing = self.settings.help_line_spacing) - for app_name, app in self.apps.iteritems(): + for app_name, app in self.apps.items(): locy = app.draw_help(self.help_pane, locy) if __name__ == '__main__': - print 'You probably want to run ./run_toolbox.py instead.' + print('You probably want to run ./run_toolbox.py instead.') diff --git a/misc.py b/misc.py index a9f43d5c6..3a07758b1 100644 --- a/misc.py +++ b/misc.py @@ -1,4 +1,4 @@ -#! /usr/bin/env python +#! /usr/bin/env python3 import os import time @@ -26,7 +26,7 @@ def __enter__(self): def __exit__(self, *args): if not self.quiet: titlestr = (' ' + self.title) if self.title else '' - print 'Elapsed%s: wall: %.06f, sys: %.06f' % ((titlestr,) + self.elapsed()) + print('Elapsed%s: wall: %.06f, sys: %.06f' % ((titlestr,) + self.elapsed())) @@ -46,7 +46,7 @@ def combine_dicts(dicts_tuple): '''Combines multiple dictionaries into one by adding a prefix to keys''' ret = {} for prefix,dictionary in dicts_tuple: - for key in dictionary.keys(): + for key in list(dictionary.keys()): ret['%s%s' % (prefix, key)] = dictionary[key] return ret @@ -57,6 +57,6 @@ def tsplit(string, no_empty_strings, *delimiters): pattern = '|'.join(map(re.escape, delimiters)) strings = re.split(pattern, string) if no_empty_strings: - strings = filter(None, strings) + strings = [_f for _f in strings if _f] - return strings \ No newline at end of file + return strings diff --git a/optimize/gradient_optimizer.py b/optimize/gradient_optimizer.py index e170a12cf..ad9833948 100755 --- a/optimize/gradient_optimizer.py +++ b/optimize/gradient_optimizer.py @@ -1,9 +1,9 @@ -#! /usr/bin/env python +#! /usr/bin/env python3 import os import errno import pickle -import StringIO +import io from pylab import * from scipy.ndimage.filters import gaussian_filter @@ -43,7 +43,7 @@ def __init__(self, **kwargs): self.__dict__.update(default_params) - for key,val in kwargs.iteritems(): + for key,val in kwargs.items(): assert key in self.__dict__, 'Unknown param: %s' % key self.__dict__[key] = val @@ -69,10 +69,10 @@ def _validate_and_normalize(self): self.push_unit = (self.push_channel,) + self.push_spatial def __str__(self): - ret = StringIO.StringIO() - print >>ret, 'FindParams:' + ret = io.StringIO() + print('FindParams:', file=ret) for key in sorted(self.__dict__.keys()): - print >>ret, '%30s: %s' % (key, self.__dict__[key]) + print('%30s: %s' % (key, self.__dict__[key]), file=ret) return ret.getvalue() @@ -89,7 +89,7 @@ def __init__(self): self.x0 = None self.majority_obj = None self.majority_xx = None - self.best_obj = None + self.best_obj = 0 self.best_xx = None self.last_obj = None self.last_xx = None @@ -130,14 +130,14 @@ def trim_arrays(self): containing first couple values; useful for saving results as a reasonably sized pickle file. ''' - for key,val in self.__dict__.iteritems(): + for key,val in self.__dict__.items(): if isinstance(val, ndarray): valstr = '%s array [%s, %s, ...]' % (val.shape, val.flatten()[0], val.flatten()[1]) self.__dict__[key] = 'Trimmed %s' % valstr def __str__(self): - ret = StringIO.StringIO() - print >>ret, 'FindResults:' + ret = io.StringIO() + print('FindResults:', file=ret) for key in sorted(self.__dict__.keys()): val = self.__dict__[key] if isinstance(val, list) and len(val) > 4: @@ -146,7 +146,7 @@ def __str__(self): valstr = '%s array [%s, %s, ...]' % (val.shape, val.flatten()[0], val.flatten()[1]) else: valstr = '%s' % val - print >>ret, '%30s: %s' % (key, valstr) + print('%30s: %s' % (key, valstr), file=ret) return ret.getvalue() @@ -170,14 +170,14 @@ def __init__(self, net, data_mean, labels = None, label_layers = None, channel_s def run_optimize(self, params, prefix_template = None, brave = False, skipbig = False): '''All images are in Caffe format, e.g. shape (3, 227, 227) in BGR order.''' - print '\n\nStarting optimization with the following parameters:' - print params + print('\n\nStarting optimization with the following parameters:') + print(params) x0 = self._get_x0(params) xx, results = self._optimize(params, x0) self.save_results(params, results, prefix_template, brave = brave, skipbig = skipbig) - print results.meta_result + print(results.meta_result) return xx @@ -213,12 +213,12 @@ def _optimize(self, params, x0): if is_conv: if params.push_spatial == (0,0): - recommended_spatial = (data_shape[2]/2, data_shape[3]/2) - print ('WARNING: A unit on a conv layer (%s) is being optimized, but push_spatial\n' + recommended_spatial = (data_shape[2]//2, data_shape[3]//2) + print(('WARNING: A unit on a conv layer (%s) is being optimized, but push_spatial\n' 'is %s, so the upper-left unit in the channel is selected. To avoid edge\n' 'effects, you might want to optimize a non-edge unit instead, e.g. the center\n' 'unit by using `--push_spatial "%s"`\n' - % (params.push_layer, params.push_spatial, recommended_spatial)) + % (params.push_layer, params.push_spatial, recommended_spatial))) else: assert params.push_spatial == (0,0), 'For FC layers, spatial indices must be (0,0)' @@ -255,18 +255,18 @@ def _optimize(self, params, x0): # 3. Print progress if ii > 0: if params.lr_policy == 'progress': - print '%-4d progress predicted: %g, actual: %g' % (ii, pred_prog, obj - old_obj) + print('%-4d progress predicted: %g, actual: %g' % (ii, pred_prog, obj - old_obj)) else: - print '%-4d progress: %g' % (ii, obj - old_obj) + print('%-4d progress: %g' % (ii, obj - old_obj)) else: - print '%d' % ii + print('%d' % ii) old_obj = obj push_label_str = ('(%s)' % push_label) if is_labeled_unit else '' max_label_str = ('(%s)' % self.labels[idxmax[0]]) if is_labeled_unit else '' - print ' push unit: %16s with value %g %s' % (params.push_unit, acts[params.push_unit], push_label_str) - print ' Max idx: %16s with value %g %s' % (idxmax, valmax, max_label_str) - print ' X:', xx.min(), xx.max(), norm(xx) + print(' push unit: %16s with value %g %s' % (params.push_unit, acts[params.push_unit], push_label_str)) + print(' Max idx: %16s with value %g %s' % (idxmax, valmax, max_label_str)) + print(' X:', xx.min(), xx.max(), norm(xx)) # 4. Do backward pass to get gradient @@ -278,9 +278,9 @@ def _optimize(self, params, x0): backout = self.net.backward_from_layer(params.push_layer, diffs if is_conv else diffs[:,:,0,0]) grad = backout['data'].copy() - print ' grad:', grad.min(), grad.max(), norm(grad) + print(' grad:', grad.min(), grad.max(), norm(grad)) if norm(grad) == 0: - print 'Grad exactly 0, failed' + print('Grad exactly 0, failed') results.meta_result = 'Metaresult: grad 0 failure' break @@ -292,13 +292,13 @@ def _optimize(self, params, x0): desired_prog = min(params.lr_params['early_prog'], late_prog) prog_lr = desired_prog / norm(grad)**2 lr = min(params.lr_params['max_lr'], prog_lr) - print ' desired progress:', desired_prog, 'prog_lr:', prog_lr, 'lr:', lr + print(' desired progress:', desired_prog, 'prog_lr:', prog_lr, 'lr:', lr) pred_prog = lr * dot(grad.flatten(), grad.flatten()) elif params.lr_policy == 'progress': # straight progress-based lr prog_lr = params.lr_params['desired_prog'] / norm(grad)**2 lr = min(params.lr_params['max_lr'], prog_lr) - print ' desired progress:', params.lr_params['desired_prog'], 'prog_lr:', prog_lr, 'lr:', lr + print(' desired progress:', params.lr_params['desired_prog'], 'prog_lr:', prog_lr, 'lr:', lr) pred_prog = lr * dot(grad.flatten(), grad.flatten()) elif params.lr_policy == 'constant': # constant fixed learning rate @@ -315,7 +315,7 @@ def _optimize(self, params, x0): if params.blur_every is not 0 and params.blur_radius > 0: if params.blur_radius < .3: - print 'Warning: blur-radius of .3 or less works very poorly' + print('Warning: blur-radius of .3 or less works very poorly') #raise Exception('blur-radius of .3 or less works very poorly') if ii % params.blur_every == 0: for channel in range(3): @@ -386,12 +386,12 @@ def save_results(self, params, results, prefix_template, brave = False, skipbig 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 + print(params, file=ff) + print(file=ff) + print(results, file=ff) if not skipbig: - with open('%sinfo_big.pkl' % prefix, 'w') as ff: + with open('%sinfo_big.pkl' % prefix, 'wb') as ff: pickle.dump((params, results), ff, protocol=-1) results.trim_arrays() - with open('%sinfo.pkl' % prefix, 'w') as ff: + with open('%sinfo.pkl' % prefix, 'wb') as ff: pickle.dump((params, results), ff, protocol=-1) diff --git a/optimize_image.py b/optimize_image.py index a555d9e02..477a5a611 100755 --- a/optimize_image.py +++ b/optimize_image.py @@ -1,4 +1,4 @@ -#! /usr/bin/env python +#! /usr/bin/env python3 import os import sys @@ -147,22 +147,22 @@ def main(): # Load mean data_mean = eval(args.mean) - if isinstance(data_mean, basestring): + if isinstance(data_mean, str): # If the mean is given as a filename, load the file try: data_mean = np.load(data_mean) except IOError: - print '\n\nCound not load mean file:', data_mean - print 'To fetch a default model and mean file, use:\n' - print ' $ cd models/caffenet-yos/' - print ' $ cp ./fetch.sh\n\n' - print 'Or to use your own mean, change caffevis_data_mean in settings_local.py or override by running with `--mean MEAN_FILE` (see --help).\n' + print('\n\nCound not load mean file:', data_mean) + print('To fetch a default model and mean file, use:\n') + print(' $ cd models/caffenet-yos/') + print(' $ cp ./fetch.sh\n\n') + print('Or to use your own mean, change caffevis_data_mean in settings_local.py or override by running with `--mean MEAN_FILE` (see --help).\n') raise # Crop center region (e.g. 227x227) if mean is larger (e.g. 256x256) excess_h = data_mean.shape[1] - data_size[0] excess_w = data_mean.shape[2] - data_size[1] assert excess_h >= 0 and excess_w >= 0, 'mean should be at least as large as %s' % repr(data_size) - data_mean = data_mean[:, (excess_h/2):(excess_h/2+data_size[0]), (excess_w/2):(excess_w/2+data_size[1])] + data_mean = data_mean[:, (excess_h//2):(excess_h//2+data_size[0]), (excess_w//2):(excess_w//2+data_size[1])] elif data_mean is None: pass else: @@ -172,7 +172,7 @@ def main(): while len(data_mean.shape) < 3: data_mean = np.expand_dims(data_mean, -1) - print 'Using mean:', repr(data_mean) + print('Using mean:', repr(data_mean)) # Load network sys.path.insert(0, os.path.join(args.caffe_root, 'python')) diff --git a/run_toolbox.py b/run_toolbox.py index b342fd4ae..f79351e7b 100755 --- a/run_toolbox.py +++ b/run_toolbox.py @@ -1,17 +1,18 @@ -#! /usr/bin/env python +#! /usr/bin/env python3 import os from live_vis import LiveVis from bindings import bindings try: +# from . import settings import settings except: - print '\nError importing settings.py. Check the error message below for more information.' - print "If you haven't already, you'll want to copy one of the settings_local.template-*.py files" - print 'to settings_local.py and edit it to point to your caffe checkout. E.g. via:' - print - print ' $ cp models/caffenet-yos/settings_local.template-caffenet-yos.py settings_local.py' - print ' $ < edit settings_local.py >\n' + print('\nError importing settings.py. Check the error message below for more information.') + print("If you haven't already, you'll want to copy one of the settings_local.template-*.py files") + print('to settings_local.py and edit it to point to your caffe checkout. E.g. via:') + print() + print(' $ cp models/caffenet-yos/settings_local.template-caffenet-yos.py settings_local.py') + print(' $ < edit settings_local.py >\n') raise if not os.path.exists(settings.caffevis_caffe_root): @@ -24,7 +25,7 @@ def main(): help_keys, _ = bindings.get_key_help('help_mode') quit_keys, _ = bindings.get_key_help('quit') - print '\n\nRunning toolbox. Push %s for help or %s to quit.\n\n' % (help_keys[0], quit_keys[0]) + print('\n\nRunning toolbox. Push %s for help or %s to quit.\n\n' % (help_keys[0], quit_keys[0])) lv.run_loop() diff --git a/settings.py b/settings.py index 69c8a51bb..9c8f2ab30 100644 --- a/settings.py +++ b/settings.py @@ -95,9 +95,9 @@ # All window configuation information is now contained in the # window_panes variable. Print if desired: if debug_window_panes: - print 'Final window panes and locations/sizes (i, j, i_size, j_size):' + print('Final window panes and locations/sizes (i, j, i_size, j_size):') for pane in window_panes: - print ' Pane: %s' % repr(pane) + print(' Pane: %s' % repr(pane)) help_pane_loc = locals().get('help_pane_loc', (.07, .07, .86, .86)) # as a fraction of main window window_background = locals().get('window_background', (.2, .2, .2)) @@ -215,11 +215,11 @@ caffevis_deploy_prototxt = caffevis_deploy_prototxt.replace('%DVT_ROOT%', dvt_root) if 'caffevis_network_weights' in locals(): caffevis_network_weights = caffevis_network_weights.replace('%DVT_ROOT%', dvt_root) -if isinstance(caffevis_data_mean, basestring): +if isinstance(caffevis_data_mean, str): caffevis_data_mean = caffevis_data_mean.replace('%DVT_ROOT%', dvt_root) -if isinstance(caffevis_labels, basestring): +if isinstance(caffevis_labels, str): caffevis_labels = caffevis_labels.replace('%DVT_ROOT%', dvt_root) -if isinstance(caffevis_unit_jpg_dir, basestring): +if isinstance(caffevis_unit_jpg_dir, str): caffevis_unit_jpg_dir = caffevis_unit_jpg_dir.replace('%DVT_ROOT%', dvt_root) # Pause Caffe forward/backward computation for this many seconds after a keypress. This is to keep the processor free for a brief period after a keypress, which allow the interface to feel much more responsive. After this period has passed, Caffe resumes computation, in CPU mode often occupying all cores. Default: .1 diff --git a/test_keys.py b/test_keys.py index abbbebc29..c3c406e31 100755 --- a/test_keys.py +++ b/test_keys.py @@ -19,30 +19,30 @@ import sys import cv2 -import keys -from bindings import bindings +from . import keys +from .bindings import bindings img = cv2.imread('input_images/ILSVRC2012_val_00000610.jpg') # load example image def check_key(key_str): - print ' Press key %5s: ' % key_str, + print(' Press key %5s: ' % key_str, end=' ') sys.stdout.flush() while True: keycode = cv2.waitKey(0) label, masked_vals = bindings.get_key_label_from_keycode(keycode, extra_info = True) if label and ('shift' in label or 'ctrl' in label): - print '(ignoring modifier %s)' % label, + print('(ignoring modifier %s)' % label, end=' ') sys.stdout.flush() else: break masked_vals_pp = ', '.join(['%d (%s)' % (mv, hex(mv)) for mv in masked_vals]) if label == key_str: - print ' %d (%s) matched %s' % (keycode, hex(keycode), label) + print(' %d (%s) matched %s' % (keycode, hex(keycode), label)) elif label is not None: - print '* %d (%s) failed, matched key %s (masked vals tried: %s)' % (keycode, hex(keycode), label, masked_vals_pp) + print('* %d (%s) failed, matched key %s (masked vals tried: %s)' % (keycode, hex(keycode), label, masked_vals_pp)) else: - print '* %d (%s) failed, no match found (masked vals tried: %s)' % (keycode, hex(keycode), masked_vals_pp) + print('* %d (%s) failed, no match found (masked vals tried: %s)' % (keycode, hex(keycode), masked_vals_pp)) #print 'Got:', label #found = False #for k,v in keys.Key.__dict__.iteritems(): @@ -58,7 +58,7 @@ def check_key(key_str): def main(): - print 'Click on the picture and then carefully push the following keys:' + print('Click on the picture and then carefully push the following keys:') cv2.imshow('img',img) check_key('j') check_key('k')