{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Using TensorFlow backend.\n" ] } ], "source": [ "from keras.models import Sequential\n", "from keras.layers.convolutional import Conv1D\n", "from keras.layers import Dense, Flatten, Activation, Dropout, AveragePooling1D\n", "from keras.initializers import VarianceScaling\n", "from keras.models import model_from_json\n", "\n", "from deeplift.layers import NonlinearMxtsMode\n", "import deeplift.conversion.kerasapi_conversion as kc\n", "\n" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "#create a model given a longest sequence length\n", "def base_model(longestLength): \n", " # create model\n", " model = Sequential()\n", " model.add(Conv1D(64, 3, activation = 'relu', input_shape=(longestLength,4)))\n", " model.add(Flatten())\n", " model.add(Dense(100, activation='linear'))\n", " model.add(Dropout(rate = 0.5))\n", " model.add(Dense(1, activation='linear'))\n", " model.add(Activation('sigmoid'))\n", " # Compile model\n", " model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['acc'])\n", " return model\n", "\n" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "scrolled": true }, "outputs": [], "source": [ "combinedLen = 1500\n", "#kerasClass_baseModel = deeps_Model(combinedLen)\n", "kerasClass_baseModel = base_model(combinedLen)\n" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{\"class_name\": \"Sequential\", \"config\": {\"name\": \"sequential_1\", \"layers\": [{\"class_name\": \"Conv1D\", \"config\": {\"name\": \"conv1d_1\", \"trainable\": true, \"batch_input_shape\": [null, 1948, 4], \"dtype\": \"float32\", \"filters\": 64, \"kernel_size\": [3], \"strides\": [1], \"padding\": \"valid\", \"data_format\": \"channels_last\", \"dilation_rate\": [1], \"activation\": \"relu\", \"use_bias\": true, \"kernel_initializer\": {\"class_name\": \"VarianceScaling\", \"config\": {\"scale\": 1.0, \"mode\": \"fan_avg\", \"distribution\": \"uniform\", \"seed\": null}}, \"bias_initializer\": {\"class_name\": \"Zeros\", \"config\": {}}, \"kernel_regularizer\": null, \"bias_regularizer\": null, \"activity_regularizer\": null, \"kernel_constraint\": null, \"bias_constraint\": null}}, {\"class_name\": \"Flatten\", \"config\": {\"name\": \"flatten_1\", \"trainable\": true, \"data_format\": \"channels_last\"}}, {\"class_name\": \"Dense\", \"config\": {\"name\": \"dense_1\", \"trainable\": true, \"units\": 100, \"activation\": \"linear\", \"use_bias\": true, \"kernel_initializer\": {\"class_name\": \"VarianceScaling\", \"config\": {\"scale\": 1.0, \"mode\": \"fan_avg\", \"distribution\": \"uniform\", \"seed\": null}}, \"bias_initializer\": {\"class_name\": \"Zeros\", \"config\": {}}, \"kernel_regularizer\": null, \"bias_regularizer\": null, \"activity_regularizer\": null, \"kernel_constraint\": null, \"bias_constraint\": null}}, {\"class_name\": \"Dropout\", \"config\": {\"name\": \"dropout_1\", \"trainable\": true, \"rate\": 0.5, \"noise_shape\": null, \"seed\": null}}, {\"class_name\": \"Dense\", \"config\": {\"name\": \"dense_2\", \"trainable\": true, \"units\": 1, \"activation\": \"linear\", \"use_bias\": true, \"kernel_initializer\": {\"class_name\": \"VarianceScaling\", \"config\": {\"scale\": 1.0, \"mode\": \"fan_avg\", \"distribution\": \"uniform\", \"seed\": null}}, \"bias_initializer\": {\"class_name\": \"Zeros\", \"config\": {}}, \"kernel_regularizer\": null, \"bias_regularizer\": null, \"activity_regularizer\": null, \"kernel_constraint\": null, \"bias_constraint\": null}}, {\"class_name\": \"Activation\", \"config\": {\"name\": \"activation_1\", \"trainable\": true, \"activation\": \"sigmoid\"}}]}, \"keras_version\": \"2.2.4\", \"backend\": \"tensorflow\"}\n" ] } ], "source": [ "\n", "#load the keras model\n", "keras_model_json = \"JSONkerasbase_model.json\"\n", "keras_model_weights = \"H5kerasbase_model.h5\"\n", "jsonString = open(keras_model_json).read()\n", "print(jsonString)\n", "keras_model = model_from_json(jsonString)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "\n" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "string indices must be integers", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0mh5_file\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mkeras_model_weights\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0mjson_file\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mkeras_model_json\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 6\u001b[0;31m nonlinear_mxts_mode=NonlinearMxtsMode.DeepLIFT_GenomicsDefault)\n\u001b[0m\u001b[1;32m 7\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m~/.local/share/virtualenvs/SeniorProject-AFH1tCxG/lib/python3.6/site-packages/deeplift/conversion/kerasapi_conversion.py\u001b[0m in \u001b[0;36mconvert_model_from_saved_files\u001b[0;34m(h5_file, json_file, yaml_file, **kwargs)\u001b[0m\n\u001b[1;32m 361\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mlayer_config\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mlayer_configs\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 362\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 363\u001b[0;31m \u001b[0mlayer_name\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mlayer_config\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m\"config\"\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m\"name\"\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 364\u001b[0m \u001b[0;32massert\u001b[0m \u001b[0mlayer_name\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mmodel_weights\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;31m\\\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 365\u001b[0m (\"Layer \"+layer_name+\" is in the layer names but not in the \"\n", "\u001b[0;31mTypeError\u001b[0m: string indices must be integers" ] } ], "source": [ "#import the model from \"KerasModelBuilder_runFirst\" note that the model needs to have the 'layers' parent removed first\n", "#this will throw a TypeError \"string indices must be integers\" on ln 363 kersapi_conversion.py\n", "deepLIFT_model = kc.convert_model_from_saved_files(\n", " h5_file=keras_model_weights,\n", " json_file=keras_model_json,\n", " nonlinear_mxts_mode=NonlinearMxtsMode.DeepLIFT_GenomicsDefault)\n", "\n" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "nonlinear_mxts_mode is set to: DeepLIFT_GenomicsDefault\n", "For layer 0 the preceding linear layer is preact_0 of type Conv1D;\n", "In accordance with nonlinear_mxts_mode=DeepLIFT_GenomicsDefault we are setting the NonlinearMxtsMode to Rescale\n", "Heads-up: I assume sigmoid is the output layer, not an intermediate one; if it's an intermediate layer then please bug me and I will implement the grad func\n", "For layer 5 the preceding linear layer is 4 of type Dense;\n", "In accordance with nonlinear_mxts_modeDeepLIFT_GenomicsDefault we are setting the NonlinearMxtsMode to RevealCancel\n" ] } ], "source": [ "#load the keras model\n", "\n", "keras_model_json = \"JSONkerasbase_modelAdjusted.json\"\n", "keras_model_weights = \"H5kerasbase_model.h5\"\n", "\n", "#import the model from \"KerasModelBuilder_runFirst\" note that the model needs to have the 'layers' parent removed first\n", "deepLIFT_model = kc.convert_model_from_saved_files(\n", " h5_file=keras_model_weights,\n", " json_file=keras_model_json,\n", " nonlinear_mxts_mode=NonlinearMxtsMode.DeepLIFT_GenomicsDefault)\n", "\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "SeniorProject-AFH1tCxG", "language": "python", "name": "seniorproject-afh1tcxg" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.4" } }, "nbformat": 4, "nbformat_minor": 2 }