The Altair Community is migrating to a new platform to provide a better experience for you. In preparation for the migration, the Altair Community is on read-only mode from October 28 - November 6, 2024. Technical support via cases will continue to work as is. For any urgent requests from Students/Faculty members, please submit the form linked here

See the python script of Keras model

mansour_ebrahimmansour_ebrahim Member Posts: 22 Contributor II
edited July 2019 in Help
I do wonder whether it would be possible to see the python script of Kear's model in Rapidminer? I tried python script operator but it didn't work.
Regards.
Mansour

Answers

  • MarlaBotMarlaBot Employee-RapidMiner, Member Posts: 57 Community Manager
    Hi @mansour_ebrahim - this is MarlaBot. I found these great videos on our RapidMiner Academy that you may find helpful:
    Instructional Video: Python and PyCharm in RapidMiner (Viewing time: ~5m)
    Please LIKE my comment if it helps! 👇

    MarlaBot <3
  • varunm1varunm1 Member Posts: 1,207 Unicorn
    edited July 2019
    Hello @mansour_ebrahim

    Right now, you can load the Keras model created in python into rapidminer but I don't see any option to see the python script of Keras model created in rapidminer.

    I am not sure if anyone working on Keras in RM as the focus is now on the Deep learning extension based on DL4J.

    @pschlunder any comment here?
    Regards,
    Varun
    https://www.varunmandalapu.com/

    Be Safe. Follow precautions and Maintain Social Distancing

  • jacobcybulskijacobcybulski Member, University Professor Posts: 391 Unicorn
    edited July 2019
    @mansour_ebrahim I regularly use Keras in RapidMiner. I think Keras extension is currently more mature than the new Deep Learning. The main problem with Keras extension is that it relies on the external ML stack which includes NVIDIA cuDNN, Python, Tensorflow and Keras. The Deep Learning extension is much simpler to set up. Both can take advantage of a GPU. You can also run Keras or Tensorflow from the Python extension, which I assume is what you are after. Here is a simple example how to do so.

    First have a look at a sample of how to arrange the Python elements within a RapidMiner process, which aims at solving the well-known MNIST problem.

    Note that Load MNIST gets the data, Existing Model macro provides an optional path to the saved model (I think I later replaced this by Generate Data by User Specification), Keras Model creates a model, Keras Evaluate measures the model performance, and Keras History Plot produces a nice chart of the training and validation history. I attach the Python script of the main process and you'll probably figure out how to do the rest.

    Keras Model - Python script
    <code>		<code><code>    datetime.now().strftime("%Y%m%d_%H%M%S")+".h5"
    model.save(model_name)
    model.save_weights("/tmp/keras_weights_"+random_name+".h5")
    with open("/tmp/keras_model_"+random_name+".json", 'w') as f:
    json.dump(model.to_json(), f, ensure_ascii=False)

    ### Evaluate the trained model

    # Evaluate the model on training data and test data
    train_score = model.evaluate(x_train, y_train, verbose=0)
    valid_score = model.evaluate(x_test, y_test, verbose=0)

    ### Prepare results

    # Return the model location
    model_spec = pd.DataFrame.from_dict({'Keras_Model': [model_name]})

    # Prepare evaluation results
    eval_data = np.array([
    <code> ['', 'acc', 'loss', 'val_acc', 'val_loss'],
    <code> ['', round(train_score[1], 4), round(train_score[0], 4),
    <code> round(valid_score[1], 4), round(valid_score[0], 4)]])

    eval_result = pd.DataFrame(data=eval_data[1:,1:],
    index=eval_data[1:,0],
    columns=eval_data[0,1:])

    # Prepare evaluation history
    if not mpath.isspace() and mpath != '':
    df_history = eval_result
    else:
    df_history = pd.DataFrame.from_dict(history.history)


    return model_spec, df_history, eval_resultfrom __future__ import print_function<br>import keras<br>from keras.models import Sequential<br>from keras.layers import Dense, Dropout, Flatten<br>from keras.layers import Conv2D, MaxPooling2D<br>from keras.callbacks import TensorBoard<br>from keras.utils import plot_model<br>from keras.models import load_model<br>from datetime import datetime<br>import numpy as np<br>import pandas as pd<br>import json <br><br>def rm_main(train_data, test_data):<br><br> ### Set training defaults, could be parametrised<br> batch_size = 128<br> epochs = 12<br> <br> ### Get data for training and testing<br> x_train = train_data['x']<br> y_train = train_data['y']<br> x_test = test_data['x']<br> y_test = test_data['y']<br> img_rows = train_data['rows']<br> img_cols = train_data['cols']<br> input_shape = train_data['shape']<br> num_classes = len(y_train[0])<br><br> print('Training shape:', input_shape)<br> print(x_train.shape[0], 'training samples')<br> print(x_test.shape[0], 'test samples')<br> print(num_classes, 'classes')<br><br> ### Create a model<br> mpath = '%{mpath}'<br> print('Loading model from: '+mpath)<br> if not mpath.isspace() and mpath != '':<br> # Load existing model<br> model = load_model(mpath)<br> model_name = mpath<br> else: <br> # Create a new model architecture<br> model = Sequential()<br> model.add(Conv2D(32, kernel_size=(3, 3),<br> activation='relu',<br> input_shape=input_shape))<br> model.add(Conv2D(64, (3, 3), activation='relu'))<br> model.add(MaxPooling2D(pool_size=(2, 2)))<br> model.add(Dropout(0.25))<br> model.add(Flatten())<br> model.add(Dense(128, activation='relu'))<br> model.add(Dropout(0.5))<br> model.add(Dense(num_classes, activation='softmax'))<br> <br> model.compile(loss=keras.losses.categorical_crossentropy,<br> optimizer=keras.optimizers.Adadelta(),<br> metrics=['accuracy'])<br> <br> # Train the model and retain epoch evaluation<br> history = model.fit(x_train, y_train,<br> batch_size=batch_size,<br> epochs=epochs,<br> verbose=2,<br> validation_data=(x_test, y_test),<br> callbacks=[TensorBoard(log_dir='/tmp/keras_logs')])<br> <br> # Save the model, its architecture (in JSON) and its weights<br> random_name = datetime.now().strftime("%Y%m%d_%H%M%S")<br> model_name = "/tmp/keras_model_"+<br>

    The main difficulty of Python scripting in RapidMiner is getting data in and out. The rest is rather standard.

    Good luck -- Jacob
  • jacobcybulskijacobcybulski Member, University Professor Posts: 391 Unicorn
    Also note that if you wanted to do an honest testing of the model at the end, you'd probably use the "test data" for this and split the "training data" into two parts, i.e. one to train the model and another one to validate it during model fitting.
  • varunm1varunm1 Member Posts: 1,207 Unicorn
    edited July 2019
    @jacobcybulski

    Is the script written using python extension or generated from the keras model built using RM keras extension?

    If its the latter, please inform how you did it? This will be helpful.

    Update: Sorry, for some reason I was not able to see the image earlier, Now I got it. You were using python scripting operators in RM. I assumed that you generated script after building a model using Keras extension (Keras_Extension)

    Thanks
    Regards,
    Varun
    https://www.varunmandalapu.com/

    Be Safe. Follow precautions and Maintain Social Distancing

  • jacobcybulskijacobcybulski Member, University Professor Posts: 391 Unicorn
    Indeed, this Python code was all crafted by hand. There is probably one more small detail worth mentioning - the Keras model is passed from Keras Model to Keras Evaluate by passing the path to the model saved on disk. At the time this seemed like the simplest solution, however, it should be possible to pass the model weights or a serialized model via RM ports.
  • jacobcybulskijacobcybulski Member, University Professor Posts: 391 Unicorn
    @varunm1 I can see now that perhaps @mansour_ebrahim asked about the script of the Keras model as it is generated from Keras Extension in RapidMiner. No, I am not aware how to retrieve it from the Keras Model operator. However, the "lay" port returns the text which looks alike Python code - perhaps it could be adapted for use in Python.
  • sgenzersgenzer Administrator, Moderator, Employee-RapidMiner, RapidMiner Certified Analyst, Community Manager, Member, University Professor, PM Moderator Posts: 2,959 Community Manager
    @jacobcybulski I've been looking for a nice example of a solution to NMIST. Can I post this on the community repo?
  • varunm1varunm1 Member Posts: 1,207 Unicorn
    Hello @jacobcybulski

    If possible can you do this "it should be possible to pass the model weights or a serialized model via RM ports." and provide complete XML code or .rmp file to Scott so that he will place this in community samples and many users can refer it a for keras application with python scripting in RM.

    Thank you.
    Regards,
    Varun
    https://www.varunmandalapu.com/

    Be Safe. Follow precautions and Maintain Social Distancing

  • jacobcybulskijacobcybulski Member, University Professor Posts: 391 Unicorn
    @sgenzer I have previously posted a MNIST example using Keras here on the forum and it later appeared in the new edition of the RapidMiner related book Data Science: Concept and Practice by Kotu and Deshpande (with acknowledgement of course). I am happy to show an improved version of this one. I will also try reworking this example to use Deep Learning extension, however, I cannot see the way to "reshape" data to use convolutional layer - perhaps some clever way of using the DL tensors would work. Both examples require some Python to read images in. 
  • jacobcybulskijacobcybulski Member, University Professor Posts: 391 Unicorn
    @varunm1 @sgenzer just a clarification, you'd want to see specifically a Keras example of serialising a model or a generic Python example of passing a model between two Python operators? 
  • varunm1varunm1 Member Posts: 1,207 Unicorn
    Hello,

    I cannot see the way to "reshape" data to use convolutional layer 

    This is the one that bugged me with the new deep learning extension for CNN and I requested @pschlunder support to look into this in earlier threads, he might be working on it (not sure). Thread here https://community.rapidminer.com/discussion/54816/regarding-input-shape-of-data-into-cnn-deep-learning-extension#latest

     you'd want to see specifically a Keras example of serializing a model or a generic Python example of passing a model between two Python operators?

    For the current process, my interest would be related to keras example with serializing a model as I see many users asking questions related to keras, even though the questions are related to keras model extension, as it is not maintained currently we can refer to the example (from you) with python scripting if they particularly want to use keras library, instead of DL4J (new Deep learning extension). But anyway @sgenzer might have other ideas for the community samples.

    Thanks

    Regards,
    Varun
    https://www.varunmandalapu.com/

    Be Safe. Follow precautions and Maintain Social Distancing

Sign In or Register to comment.