Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app_base.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#! /usr/bin/env python
#! /usr/bin/env python3

class BaseApp(object):
'''Base App class.'''
Expand Down
80 changes: 40 additions & 40 deletions caffevis/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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



Expand All @@ -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

Expand All @@ -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,
Expand All @@ -58,15 +58,15 @@ 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:

filename, file_extension = os.path.splitext(settings.caffevis_data_mean)
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":

Expand All @@ -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:
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -185,32 +185,32 @@ 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()

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:
Expand All @@ -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'])
Expand Down Expand Up @@ -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')]
Expand Down Expand Up @@ -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:
Expand Down
16 changes: 8 additions & 8 deletions caffevis/caffe_proc_thread.py
Original file line number Diff line number Diff line change
Expand Up @@ -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



Expand All @@ -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
Expand All @@ -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:
Expand Down Expand Up @@ -110,15 +110,15 @@ 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):
#print '**** Doing deconv with %s diffs in [%s,%s]' % (backprop_layer, diffs.min(), diffs.max())
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:
Expand All @@ -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
Expand Down
8 changes: 4 additions & 4 deletions caffevis/caffevis_app_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
24 changes: 12 additions & 12 deletions caffevis/caffevis_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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
Expand All @@ -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, :]
Expand Down Expand Up @@ -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')

8 changes: 4 additions & 4 deletions caffevis/jpg_vis_loading_thread.py
Original file line number Diff line number Diff line change
Expand Up @@ -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



Expand All @@ -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:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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')
Loading