How to use plot_model to convert a model as png? - keras

I'm new to this and still learning about how to build a simple BiLSTM model for my time series prediction, i somehow manage to create one and now i want to convert the model summary into a png file.
Here is my code
from tensorflow.keras.models import Sequential
model_opendf=Sequential()
model_opendf.add(Bidirectional(LSTM(100, activation='relu', input_shape=(100,1))))
model_opendf.add(Dense(1))
model_opendf.compile(loss="mean_squared_error", optimizer="adam")
from keras.utils.vis_utils import plot_model
model_opendf.build(input_shape=(100,1))
model_opendf.summary()
plot_model(model_opendf, to_file='lstm_model.jpg')
the result is like this
It feels empty, is there anything else that i can use to populate with plot_model? and how to convert the image instantly into png?

Here is a reference link for plot_model, you can add show_shapes and show_layer_names like this:
from keras.utils.vis_utils import plot_model
model_opendf.build(input_shape=(100,1))
model_opendf.summary()
plot_model(model_opendf, to_file='lstm_model.png', show_shapes=True, show_layer_names=True)
And if you want to convert the output from jpg to png, or any other image file, you can just type the image format you want in this case, change it from jpg to png.
Your model should looks like this:

Related

Spliting an image dataset

I have an image dataset (folder of jpg images) .I would like to split it : 70 % for train and 30% for test randomly.
So I write this simple script:
from sklearn.model_selection import train_test_split
path = ".\dataset"
output_split=train_test_split(path,path,test_size=0.2)
But I don't find anything in folder "output_split"
So where I store output of spliting (train and test)?
I recommend using tf.dataset in your project. You can reference this link here for documentation:
https://www.tensorflow.org/api_docs/python/tf/keras/utils/image_dataset_from_directory
And this link for a example of the function use:
https://keras.io/api/preprocessing/image/
Please remember to use the same seed for both training and (testing/validation).
Please note that no changes will happen to your files in your local.

Image size in DefaultPredictor of Detectron2

For object detection, I'm using detectron2.
I want to fix the input image size so I made my customized dataloader:
def build_train_loader(cls, cfg):
dataloader = build_detection_train_loader(cfg,
mapper=DatasetMapper(cfg, is_train=True, augmentations=[
T.Resize((1200, 1200))
]))
What I wonder is for the prediction, I can use the DefaultPredictor of detectron2 and resize my images to (1200, 1200) as prepossessing before sending to the predictor?
Or the DefaultPredictor is resizing the image before the prediction and I have to override a function to resize to (1200, 1200)?
You have to preprocess the images yourself or to write your own predictor that will apply the resize before calling the model.
The DefaultPredictor applies a ResizeShortestEdge transform (that can be configured in the config file), but this is not exactly what you want.

How To Import The MNIST Dataset From Local Directory Using PyTorch

I am writing a code of a well-known problem MNIST database of handwritten digits in PyTorch. I downloaded the train and testing dataset (from the main website) including the labeled dataset. The dataset format is t10k-images-idx3-ubyte.gz and after extract t10k-images-idx3-ubyte. My dataset folder looks like
MINST
Data
train-images-idx3-ubyte.gz
train-labels-idx1-ubyte.gz
t10k-images-idx3-ubyte.gz
t10k-labels-idx1-ubyte.gz
Now, I wrote a code to load data like bellow
def load_dataset():
data_path = "/home/MNIST/Data/"
xy_trainPT = torchvision.datasets.ImageFolder(
root=data_path, transform=torchvision.transforms.ToTensor()
)
train_loader = torch.utils.data.DataLoader(
xy_trainPT, batch_size=64, num_workers=0, shuffle=True
)
return train_loader
My code is showing Supported extensions are: .jpg,.jpeg,.png,.ppm,.bmp,.pgm,.tif,.tiff,.webp
How can I solve this problem and I also want to check that my images are loaded (just a figure contains the first 5 images) from the dataset?
Read this Extract images from .idx3-ubyte file or GZIP via Python
Update
You can import data using this format
xy_trainPT = torchvision.datasets.MNIST(
root="~/Handwritten_Deep_L/",
train=True,
download=True,
transform=torchvision.transforms.Compose([torchvision.transforms.ToTensor()]),
)
Now, what is happening at download=True first your code will check at the root directory (your given path) contains any datasets or not.
If no then datasets will be downloaded from the web.
If yes this path already contains a dataset then your code will work using the existing dataset and will not download from the internet.
You can check, first give a path without any dataset (data will be downloaded from the internet), and then give another path which already contains dataset data will not be downloaded.
Welcome to stackoverflow !
The MNIST dataset is not stored as images, but in a binary format (as indicated by the ubyte extension). Therefore, ImageFolderis not the type dataset you want. Instead, you will need to use the MNIST dataset class. It could even download the data if you had not done it already :)
This is a dataset class, so just instantiate with the proper root path, then put it as the parameter of your dataloader and everything should work just fine.
If you want to check the images, just use the getmethod of the dataloader, and save the result as a png file (you may need to convert the tensor to a numpy array first).

how to print tensorflow graph to svg or png file?

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())

tensorflow tensorflow.contrib.learn.Estimator load trained model

After I call this function like this:
from tensorflow.contrib import learn
#----------------------------------------
#Do some process here
#----------------------------------------
classifier = learn.Estimator(model_fn=bag_of_words_model,model_dir='F:/data')
classifier.fit(feature_train, target_train, steps=1000)
I will have some file in my folder "F:/data" like this
And I wonder Do I have anyway to reuse this model ? Like move to new computer and use this to predict new data. Sorry for my bad English. Thanks for all the answers!! Hope you all have a nice day.
In a script where you want to reuse your model, redefine/import bag_of_words_model again and define
classifier_loaded = learn.Estimator(model_fn=bag_of_words_model, model_dir='F:/data')
Tensorflow will reload the graph and import the weights into the graph and you can just use it as
classifier_loaded.predict(input_fn=input_fn)
or continue the training with
claissifier_loaded.fit(feature_train, target_train)

Resources