how to print tensorflow graph to svg or png file? - svg

I know that I can use tensorboard to visualize graphs. But in some cases, I may not want to open tensorboard is there a way to directly generate a png of svg file to visualize tensorflow? Thanks.
import tensorflow as tf
g = tf.Graph()
with g.as_default():
x = tf.constant(3.)
import pprint
pprint.pprint(tf.get_default_graph())

Related

How to save images using matplotlib without displaying them?

I have multiple(in millions) numpy 2-D arrays which need to be saved. One can save an individual image like this:
import numpy as np
import matplotlib.pyplot as plt
surface_profile = np.empty((50,50)) #numpy array to be saved
plt.figure()
plt.imshow(surface_profile)
save_filename='filename.png'
plt.savefig(save_filename)
However this process also displays the image which I don't require. If I keep saving million images like this, I should somehow avoid imshow() function of matplotlib.
Any help???
PS: I forgot to mention that I am using Spyder.
Your problem is using plt.imshow(surface_profile) to create the image as this will always display the image as well.
This can be done using PIL, try the following,
from PIL import Image
import numpy as np
surface_profile = np.empty((50,50)) #numpy array to be saved
im = Image.fromarray(surface_profile)
im = im.convert('RGB')
save_filename="filename.png"
im.save(save_filename, "PNG")

Attempting to convert an image to grayscale, or better, binary

My basic plan here is to create an image recognition software that tracks the size of different bubbles. I basically have a compilation of pictures that constitute a video. I have it working as of right now using PIMS to import the files I need and place them into an array (rawframes). I can print my picture.
import numpy as np
import pandas as pd
import pims
from pims import pipeline
import trackpy as tp
import os
import matplotlib as mpl
import matplotlib.pyplot as plt
#pipeline
def binary(frame):
return frame[:, :, 1]
id_example = 1
rawframes = pims.ImageSequence(os.path.join('BubbleSize/90FoamQuality/DryFoams', 'T20190411_002_ (*).jpg'), process_func=binary)
plt.imshow(rawframes[id_example])
What I am trying to do here is convert the images from regular into black and white. I have not used many of the things I imported yet I know, this is a very preliminary step.
However, below is a before and after image comparison. Can someone help me out or walk me through these steps here? I get lost when it comes to filtering the images through python.
edit --> when I change my pipeline function to the below, I get the same green image
edit2 --> printing frame.shape and frame.dtype in binary pipeline respectively

OpenCV and Matplotlib not returning bounding box on object detection Python script

I am experimenting with openCV and object detection using Python 3.6 on Ubuntu Linux 18.04. I have found this simple Python code at this website that claims to accomplish image detection.
import cv2
import matplotlib.pyplot as plt
import cvlib as cv
from cvlib.object_detection import draw_bbox
im = cv2.imread('/home/gerry/Pictures/guyonstreet.jpg')
bbox, label,conf = cv.detect_common_objects(im)
output_image = draw_bbox(im, bbox, label, conf)
plt.imshow(output_image)
plt.show()
I have installed the necessary libraries without issue as shown on the website. I can run the code without errors. Unfortunately the resulting image fails to show the expected bounding box identifying the object. Below is a screenshot of what the code returned when I ran the experiment on a person. I get similar results when I use an image of fruit.
Why is my code not returning the bounding box identifying the object?

Using download_data() and untar_data() in fastai library

I downloaded Fashion MNIST dataset from kaggle using dowload_data() function in fastai library.
downloaded_data = download_data("https://www.kaggle.com/zalando-research/fashionmnist/download")
output -
PosixPath('/root/.fastai/data/download.tgz')
download_data saves it as .tgz file, now I use untar_data().
path = untar_data('/root/.fastai/data/download.tgz')
output -
PosixPath('/root/.fastai/data/download.tgz')
Which did not extract .tgz file. How do I use this dataset in fastai library?
In fastai library, the download_data gives you a pathlib.PosixPath file, not the exact file, you need to use another unzipping library to extract the data.
If you just need the MNIST data from fast ai, here's an easier way:
from fastai import datasets
import gzip, pickle
MNIST_URL='http://deeplearning.net/data/mnist/mnist.pkl'
path = datasets.download_data(MNIST_URL, ext='.gz')
with gzip.open(path, 'rb') as f:
((x_train, y_train), (x_valid, y_valid), _) = pickle.load(f, encoding='latin-1')

Graphviz.Source not rendering in Jupyter Notebook

After exporting a .dot file using scikit-learn's handy export_graphviz function.
I am trying to render the dot file using Graphviz into a cell in my Jupyter Notebook:
import graphviz
from IPython.display import display
with open("tree_1.dot") as f:
dot_graph = f.read()
display(graphviz.Source(dot_graph))
However the out[ ] is just an empty cell.
I am using graphviz 0.5 (pip then conda installed), iPython 5.1, and Python 3.5
The dot file looks correct here are the first characters:
digraph Tree {\nnode [shape=box, style="filled", color=
iPython display seems to work for other objects including Matplotlib plots and Pandas dataframes.
I should note the example on Graphviz' site also doesn't work.
It's possible that since you posted this, changes were made so you might want to update your libraries if that's possible.
The versions of relevance here I used are:
Python 2.7.10
IPython 5.1.0
graphviz 0.7.1
If you have a well formed .dot file, you can display it to the jupyter out[.] cell by the following:
import graphviz
with open("tree_1.dot") as f:
dot_graph = f.read()
# remove the display(...)
graphviz.Source(dot_graph)
this solution allows you to insert DOT text directly (without saving it to file first)
# convert a DOT source into graph directly
import graphviz
from IPython.display import display
source= '''\
digraph sample {
A[label="AL"]
B[label="BL"]
C[label="CL"]
A->B
B->C
B->D
D->C
C->A
}
'''
print (source)
gvz=graphviz.Source(source)
# produce PDF
#gvz.view()
print (gvz.source)
display(gvz)
Try to use pydotplus.
import pydotplus
by (1.1) Importing the .dot from outside
pydot_graph = pydotplus.graph_from_dot_file("clf.dot")
or (1.2) Directly using the .export_graphviz output
dt = tree.DecisionTreeClassifier()
dt = clf.fit(x,y)
dt_graphviz = tree.export_graphviz(dt, out_file = None)
pydot_graph = pydotplus.graph_from_dot_data(dt_graphviz)
(2.) and than display the pyplot graph using
from IPython.display import Image
Image(pydot_graph.create_png())
try to reinstall graphviz
conda remove graphviz
conda install python-graphviz
graphviz.Source(dot_graph).view()
graphviz.Source(dot_graph).view()

Resources