See the python script of Keras model

mansour_ebrahimmansour_ebrahim MemberPosts:22Contributor II
edited July 2019 inHelp
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 Administrator, Moderator, Employee, MemberPosts:57Community 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 Moderator, MemberPosts:1,207Unicorn
    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.

    我不确定是否有人在RM Keras上工作the focus is now on the Deep learning extension based on DL4J.

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

    Be Safe. Follow precautions and Maintain Social Distancing

  • jacobcybulskijacobcybulski Member, University ProfessorPosts:391Unicorn
    edited July 2019
    @mansour_ebrahimI 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
    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 =模型。评估(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([
    ['', 'acc', 'loss', 'val_acc', 'val_loss'],
    ['', round(train_score[1], 4), round(train_score[0], 4),
    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:])

    #准备评价历史
    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
    import keras
    from keras.models import Sequential
    from keras.layers import Dense, Dropout, Flatten
    from keras.layers import Conv2D, MaxPooling2D
    from keras.callbacks import TensorBoard
    from keras.utils import plot_model
    from keras.models import load_model
    from datetime import datetime
    import numpy as np
    import pandas as pd
    import json

    def rm_main(train_data, test_data):

    ### Set training defaults, could be parametrised
    batch_size = 128
    epochs = 12

    ### Get data for training and testing
    x_train = train_data['x']
    y_train = train_data['y']
    x_test = test_data['x']
    y_test = test_data['y']
    img_rows = train_data['rows']
    img_cols = train_data['cols']
    input_shape = train_data['shape']
    num_classes = len(y_train[0])

    print('Training shape:', input_shape)
    print(x_train.shape[0], 'training samples')
    print(x_test.shape[0], 'test samples')
    print(num_classes, 'classes')

    ### Create a model
    mpath = '%{mpath}'
    print('Loading model from: '+mpath)
    if not mpath.isspace() and mpath != '':
    # Load existing model
    model = load_model(mpath)
    model_name = mpath
    else:
    # Create a new model architecture
    model = Sequential()
    model.add(Conv2D(32, kernel_size=(3, 3),
    activation='relu',
    input_shape=input_shape))
    model.add(Conv2D(64, (3, 3), activation='relu'))
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(Dropout(0.25))
    model.add(Flatten())
    model.add(Dense(128, activation='relu'))
    model.add(Dropout(0.5))
    model.add(Dense(num_classes, activation='softmax'))

    model.compile(loss=keras.losses.categorical_crossentropy,
    optimizer=keras.optimizers.Adadelta(),
    metrics=['accuracy'])

    # Train the model and retain epoch evaluation
    history = model.fit(x_train, y_train,
    batch_size=batch_size,
    epochs=epochs,
    verbose=2,
    validation_data=(x_test, y_test),
    callbacks=[TensorBoard(log_dir='/tmp/keras_logs')])

    # Save the model, its architecture (in JSON) and its weights
    random_name = datetime.now().strftime("%Y%m%d_%H%M%S")
    model_name = "/tmp/keras_model_"+

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

    Good luck -- Jacob
    varunm1
  • jacobcybulskijacobcybulski Member, University ProfessorPosts:391Unicorn
    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 Moderator, MemberPosts:1,207Unicorn
    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 (K eras_Extension)

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

    Be Safe. Follow precautions and Maintain Social Distancing

  • jacobcybulskijacobcybulski Member, University ProfessorPosts:391Unicorn
    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 ProfessorPosts:391Unicorn
    @varunm1I can see now that perhaps@mansour_ebrahimasked 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.
    varunm1
  • sgenzersgenzer Administrator, Moderator, Employee, RapidMiner Certified Analyst, Community Manager, Member, University Professor, PM ModeratorPosts:2,959Community Manager
    @jacobcybulskiI've been looking for a nice example of a solution to NMIST. Can I post this on the community repo?
    varunm1
  • varunm1varunm1 Moderator, MemberPosts:1,207Unicorn
    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 ProfessorPosts:391Unicorn
    @sgenzer我之前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 ProfessorPosts:391Unicorn
    @varunm1 @sgenzerjust 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 Moderator, MemberPosts:1,207Unicorn
    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@pschlundersupport to look into this in earlier threads, he might be working on it (not sure). Thread herehttps://community.www.turtlecreekpls.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@sgenzermight have other ideas for the community samples.

    Thanks

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

    Be Safe. Follow precautions and Maintain Social Distancing

Sign InorRegisterto comment.