Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ipython embed script #1431

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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
18 changes: 16 additions & 2 deletions pylearn2/scripts/mlp/predict_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
from theano import tensor as T
from theano import function

if __name__ == "__main__":

def main():
try:
model_path = sys.argv[1]
test_path = sys.argv[2]
Expand All @@ -28,7 +29,10 @@
print "Usage: predict.py <model file> <test file> <output file>"
print " predict.py saved_model.pkl test_x.csv predictions.csv\n"
quit(-1)
run(model_path, test_path, out_path)


def run(model_path, test_path, out_path, headers=False, cast_to_float32=False, first_col_label=False):
print "loading model..."

try:
Expand All @@ -52,7 +56,14 @@

# x is a numpy array
# x = pickle.load(open(test_path, 'rb'))
x = np.loadtxt(test_path, delimiter=',') # no labels in the file
# only skip first row if labels in the file
skiprows = 1 if headers else 0
x = np.loadtxt(test_path, delimiter=',', skiprows=skiprows)
if cast_to_float32:
x = x.astype(np.float32)

if first_col_label:
x = x[:,1:]

y = f(x)

Expand All @@ -61,3 +72,6 @@
np.savetxt(out_path, y, fmt='%d')


if __name__ == "__main__":
main()

29 changes: 18 additions & 11 deletions pylearn2/scripts/plot_monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,12 @@ def main():
parser.add_argument("model_paths", nargs='+')
options = parser.parse_args()
model_paths = options.model_paths
options_out = options.out

if options.out is not None:
plot_monitor(model_paths, options_out)

def plot_monitor(model_paths=[], options_out=None, show_codes=None, x_axis='example'):
if options_out is not None:
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
Expand All @@ -93,7 +97,7 @@ def main():
raise
this_model_channels = model.monitor.channels

if len(sys.argv) > 2:
if len(model_paths) > 1:
postfix = ":" + model_names[i]
else:
postfix = ""
Expand All @@ -117,18 +121,16 @@ def main():
codebook['<'+channel_name+'>'] = channel_name
sorted_codes.append(code)

x_axis = 'example'
print 'set x_axis to example'

if len(channels.values()) == 0:
print "there are no channels to plot"
break

# If there is more than one channel in the monitor ask which ones to
# plot
prompt = len(channels.values()) > 1

if prompt:
if show_codes:
final_codes = show_codes
elif prompt:

# Display the codebook
for code in sorted_codes:
Expand Down Expand Up @@ -221,6 +223,8 @@ def main():
else:
final_codes ,= set(codebook.keys())

print 'set x_axis to %s' % x_axis

colors = ['b', 'g', 'r', 'c', 'm', 'y', 'k']
styles = list(colors)
styles += [color+'--' for color in colors]
Expand All @@ -232,7 +236,10 @@ def main():
# plot the requested channels
for idx, code in enumerate(sorted(final_codes)):

channel_name= codebook[code]
if code in codebook.values():
channel_name = code
else:
channel_name = codebook[code]
channel = channels[channel_name]

y = np.asarray(channel.val_record)
Expand Down Expand Up @@ -276,12 +283,12 @@ def main():
# 0.046 is the size of 1 legend box
fig.subplots_adjust(bottom=0.11 + 0.046 * len(final_codes))

if options.out is None:
if options_out is None:
plt.show()
else:
plt.savefig(options.out)
plt.savefig(options_out)

if not prompt:
if not prompt or show_codes:
break

if __name__ == "__main__":
Expand Down
6 changes: 5 additions & 1 deletion pylearn2/utils/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,11 @@ def show(image):
If ndarray, integer formats are assumed to use 0-255
and float formats are assumed to use 0-1
"""
viewer_command = string.preprocess('${PYLEARN2_VIEWER_COMMAND}')

if viewer_command == 'inline':
return imview(image)

if hasattr(image, '__array__'):
#do some shape checking because PIL just raises a tuple indexing error
#that doesn't make it very clear what the problem is
Expand Down Expand Up @@ -160,7 +165,6 @@ def show(image):
#

image.save(name)
viewer_command = string.preprocess('${PYLEARN2_VIEWER_COMMAND}')
if os.name == 'nt':
subprocess.Popen(viewer_command + ' ' + name +' && del ' + name,
shell=True)
Expand Down