Processing Only One Image Uses All of Ram - python

I am running this code in google colab (I also tried at my local). I use only one image and it uses all of the ram. Do I do something wrong? Is it normal to use 16 gb ram? I added %matplotlib inline and it still crashes?
import cv2
import matplotlib.pyplot as plt
import numpy as np
f = cv2.imread('/content/gdrive/MyDrive/grass.png')
f = cv2.cvtColor(f, cv2.COLOR_BGR2GRAY).astype(float)
plt.imshow(f)
plt.colorbar()
def gauss1(sigma, width):
hwidth = round((width-1)/2)
x = np.arange(-hwidth, hwidth+1,1)
g = np.exp(-x**2/(2*sigma**2))
return g/np.sum(g)
g1=gauss1(2,11)
g1=np.reshape(g1,(1,-1))
plt.imshow(g1)
f1 = cv2.filter2D(f,-1,g1)
fig = plt.figure(figsize=(10, 20))
fig.add_subplot(1,2,1)
plt.imshow(f)
fig.add_subplot(1,2,2)
plt.imshow(f1)
It does not even produce last code snippet's output.

Use:
If you're using terminal/console, use plt.show() to show the matplotlib output.
Use %matplotlib inline if you are using any ipython notebook kernel such as jupyter notebooks, google colab or kaggle notebooks.

Related

jupyter notebook won't show images

#%%
from Utils.ConfigProvider import ConfigProvider
import os
import cv2
import numpy as np
from matplotlib import pyplot as plt
config = ConfigProvider.config()
and
#%%
inspected = cv2.imread(config.data.inspected_image_path, 0)
reference = cv2.imread(config.data.reference_image_path, 0)
diff = np.abs(inspected - reference)
plt.figure()
plt.title('inspected')
plt.imshow(inspected)
plt.show()
note config.data.inspected_image_path and config.data.reference_image_path are valid paths.
No errors appear, but no images are shown as well.
Running the same code from a python file does show the image.
I have something missing from the notebook.
This happens both when running using jupyter notebook and directly from PyCharm (pro)
How do I get to see images? all other answers I found just tell me to plt.show() but this obviously does not work.
I don't mind a cv2 solution as well.
You need to set a matplotlib backend.
You can do this with
%matplotlib inline
If you want to be able to interact with the plot, use
%matplotlib notebook

Why can't I create a 3d scatter plot in Python on my desktop?

I am trying to use the 3D scatter plot object in python. And I have successfully done this on my laptop. However, I can not copy and paste code onto my desktop. When I do this I get an error. I will attach my the section of my code below that is giving me trouble. I am using Anaconda to run my code. I will note that my laptop uses python 3.6 and my desktop uses 3.7, but I do not think that is causing it. The error I is get is as follows. "ValueError: Unknown projection '3d'"
import numpy as np
from scipy import optimize
import time
from sklearn.metrics import mean_squared_error
import matplotlib.pyplot as plt
import pandas as pd
from sklearn import preprocessing
from sklearn.svm import SVR
import multiprocessing as mp
from obj_class import objective_class
import pdb
import scipy.integrate as integrate
def create3d():
grid_matrix = np.array([[1,1,1,1],[2,2,2,2],[3,3,3,3]])
fig = plt.figure()
ax = plt.axes(projection='3d')
p = ax.scatter3D(grid_matrix[:,0],grid_matrix[:,1] ,grid_matrix[:,2] , c=grid_matrix[:,3], cmap='viridis')
cb = fig.colorbar(p)
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
ax.set_title(' Scatter Plot')
In order to use a 3d projection in matplotlib <= 3.1 you need to import axes3d, i.e.
from mpl_toolkits.mplot3d import Axes3D
From matplotlib >= 3.2, no extra import is necessary. So possibly you are running different matplotlib versions on both computers.
If you are running your code within an iPython kernel, Jupyter notebook for example,
then you only need to perform each import once and you will be able to run any code which relies on said import until the kernel is shutdown. However, in order to run the script in a self contained fashion you will need that import included in your script.

widget for function input is not shown in nbviewer

I've written a python module and would like to make some of the functions available via a jupyter notebook. For this I would like to visualise (plot) the functions. The notebook should be interactive, i.e. the user can change inputs and gets the corresponding result / plot back.
I've tried to make a use case work as I've never used jupyter before. I followed this. Here is the code from their site:
%matplotlib inline
from ipywidgets import interact
import matplotlib.pyplot as plt
import numpy as np
def f(m, b):
plt.figure(2)
x = np.linspace(-10, 10, num=1000)
plt.plot(x, m * x + b)
plt.ylim(-5, 5)
plt.show()
interactive_plot = interactive(f, m=(-2.0, 2.0), b=(-3, 3, 0.5))
output = interactive_plot.children[-1]
output.layout.height = '350px'
interactive_plot
locally everything is working fine. As soon as I upload it to github and want to show it on nbviewer (can be found here) the interactive sliders don't work. What am I doing wrong?
Is there a restriction for this on the python version?

Imagegrid in Jupyter notebook

I'm following an example from the matplotlib documentation on Imagegrid, and I'm trying to replicate it from within Jupyter notebook:
% matplotlib inline
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import ImageGrid
import numpy as np
im = np.arange(100)
im.shape = 10, 10
fig = plt.figure(1, (4., 4.))
grid = ImageGrid(fig, 111, # similar to subplot(111)
nrows_ncols=(2, 2), # creates 2x2 grid of axes
axes_pad=0.1, # pad between axes in inch.
)
for i in range(4):
grid[i].imshow(im) # The AxesGrid object work as a list of axes.
plt.show()
Expected output:
What I'm getting:
I'm not getting the grid of images, as you can see. What am I doing wrong?
EDIT
If I remove the %matplotlib inline option, I just get this (it's cell[1] to prove I restarted my kernel):
No plots shown.
I'm running matplotlib version 3.0.0, checked with conda list matplotlib, jupyter is 4.4.0, checked with jupyter --version. On Windows 10, Anaconda, python 3.6.
This is an issue with matplotlib 3.0.0. This has now been fixed, such that it will not occur in the upcoming 3.0.1 bugfix release.
In the meantime you have two options.
Revert to matplotlib 2.2.3
Decide to not crop the images when using %matplotlib inline. Do so via
%config InlineBackend.print_figure_kwargs = {'bbox_inches':None}
in IPython or Jupyter.
Remove
%matplotlib inline
and restart everything or put it in a separate cell as seen below. It appears that the magic command always needs to be run in a separate cell before the plotting and if it was run before the kernel needs to be restarted. See here
enter link description here
and it will work. %matplotlib inline is not necessary to render plots in jupyter it is just a convenience. plt.show() will render plots whenever it is called.
I have had this issue with some mpl in jupyter. I think the issue is that the magic command causes it to render any plot as soon as it is available as opposed to mpl which waits until it is told to render and how.
Full example code straight from the mpl example you linked in your question:
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import ImageGrid
import numpy as np
im = np.arange(100)
im.shape = 10, 10
fig = plt.figure(1, (4., 4.))
grid = ImageGrid(fig, 111, # similar to subplot(111)
nrows_ncols=(2, 2), # creates 2x2 grid of axes
axes_pad=0.1, # pad between axes in inch.
)
for i in range(4):
grid[i].imshow(im) # The AxesGrid object work as a list of axes.
plt.show() # Renders all available axes when called

Store and reload matplotlib.pyplot object

I work in an psudo-operational environment where we make new imagery on receipt of data. Sometimes when new data comes in, we need to re-open an image and update that image in order to create composites, add overlays, etc. In addition to adding to the image, this requires modification of titles, legends, etc.
Is there something built into matplotlib that would let me store and reload my matplotlib.pyplot object for later use? It would need to maintain access to all associated objects including figures, lines, legends, etc. Maybe pickle is what I'm looking for, but I doubt it.
As of 1.2 matplotlib ships with experimental pickling support. If you come across any issues with it, please let us know on the mpl mailing list or by opening an issue on github.com/matplotlib/matplotlib
HTH
EDIT: Added a simple example
import matplotlib.pyplot as plt
import numpy as np
import pickle
ax = plt.subplot(111)
x = np.linspace(0, 10)
y = np.exp(x)
plt.plot(x, y)
pickle.dump(ax, file('myplot.pickle', 'w'))
Then in a separate session:
import matplotlib.pyplot as plt
import pickle
ax = pickle.load(file('myplot.pickle'))
plt.show()
A small modification to Pelson's answer for people working on a Jupyterhub
Use %matplotlib notebook before loading the pickle. Using %matplotlib inline did not work for me in either jupyterhub or jupyter notebook. and gives a traceback ending in
AttributeError: 'module' object has no attribute 'new_figure_manager_given_figure'.
import matplotlib.pyplot as plt
import numpy as np
import pickle
%matplotlib notebook
ax = plt.subplot(111)
x = np.linspace(0, 10)
y = np.exp(x)
plt.plot(x, y)
with open('myplot.pkl','wb') as fid:
pickle.dump(ax, fid)
Then in a separate session:
import matplotlib.pyplot as plt
import pickle
%matplotlib notebook
with open('myplot.pkl','rb') as fid:
ax = pickle.load(fid)
plt.show()
I produced figures for a number of papers using matplotlib. Rather than thinking of saving the figure (as in MATLAB), I would write a script that plotted the data then formatted and saved the figure. In cases where I wanted to keep a local copy of the data (especially if I wanted to be able to play with it again) I found numpy.savez() and numpy.load() to be very useful.
At first I missed the shrink-wrapped feel of saving a figure in MATLAB, but after a while I have come to prefer this approach because it includes the data in a format that is available for further analysis.
Tested in jupyter notebook.
import matplotlib.pyplot as plt
import numpy as np
import pickle
fig, axes = plt.subplots(figsize=(20, 5),nrows=1, ncols=2)
x,y = np.arange(10), np.random.random(10)
axes[0].plot(x,y)
axes[1].plot(x,y)
plt.show()
# myfname = input("Save figure? [enter filename]: ")
myfname = 'test'
if (myfname!=''):
fig.savefig(f'./{myfname}.png')
print(f'file saved to ./{myfname}.png')
with open(f'./{myfname}.png.pkl','wb') as fid:
pickle.dump(fig, fid)
print(f'pickled to ./{myfname}.png.pkl')
###################################
####### in a separate session
myfname = 'test'
with open(f'./{myfname}.png.pkl', 'rb') as fh:
fig_loaded = pickle.load(fh)
print(fig_loaded.axes[0].lines[0].get_data()) # get data
fig_loaded # show fig
Did you try the pickle module? It serialises an object, dumps it to a file, and can reload it from the file later.

Categories

Resources