{"action":"created","issue":{"url":"https://api.github.com/repos/opencv/opencv/issues/9682","repository_url":"https://api.github.com/repos/opencv/opencv","labels_url":"https://api.github.com/repos/opencv/opencv/issues/9682/labels{/name}","comments_url":"https://api.github.com/repos/opencv/opencv/issues/9682/comments","events_url":"https://api.github.com/repos/opencv/opencv/issues/9682/events","html_url":"https://github.com/opencv/opencv/issues/9682","id":259527632,"number":9682,"title":"(dnn/tensorflow) Unknown layer type StridedSlice in populateNet","user":{"login":"kbarni","id":26119925,"avatar_url":"https://avatars2.githubusercontent.com/u/26119925?v=4","gravatar_id":"","url":"https://api.github.com/users/kbarni","html_url":"https://github.com/kbarni","followers_url":"https://api.github.com/users/kbarni/followers","following_url":"https://api.github.com/users/kbarni/following{/other_user}","gists_url":"https://api.github.com/users/kbarni/gists{/gist_id}","starred_url":"https://api.github.com/users/kbarni/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/kbarni/subscriptions","organizations_url":"https://api.github.com/users/kbarni/orgs","repos_url":"https://api.github.com/users/kbarni/repos","events_url":"https://api.github.com/users/kbarni/events{/privacy}","received_events_url":"https://api.github.com/users/kbarni/received_events","type":"User","site_admin":false},"labels":[{"id":635087908,"url":"https://api.github.com/repos/opencv/opencv/labels/category:%20dnn","name":"category: dnn","color":"ededed","default":false},{"id":9731371,"url":"https://api.github.com/repos/opencv/opencv/labels/duplicate","name":"duplicate","color":"404040","default":true}],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":7,"created_at":"2017-09-21T15:00:15Z","updated_at":"2018-05-30T17:13:37Z","closed_at":null,"author_association":"NONE","body":"##### System information (version)\r\n- OpenCV => 3.3\r\n- Operating System / Platform => Ubuntu 16.10\r\n- Compiler => gcc 6.3\r\n\r\n##### Detailed description\r\n\r\nI created a DNN network in Keras, very similar to LeNet (Conv->MaxPool->Conv->MaxPool->Conv->MaxPool->Flatten (Reshape)->Dense->Output(2 classes, Softmax)\r\n\r\nThe last level of the network was defined in the following way:\r\n\r\n        model.add(\r\n            Dense(\r\n                units = 2,\r\n                activation=\"softmax\")\r\n        )\r\n\r\n\r\nWhen I tried to export it to TensorFlow, Keras introduced at the last step a `StridedSlice` operation (I don't really know why).\r\n\r\nAs this type of layer is not implemented, OpenCV throws an exception when trying to load the *.pb* file\r\n\r\nCan you implement this layer type to OpenCV? \r\nOr is there a workaround to eliminate this layer type when creating the network?\r\n\r\n##### Steps to reproduce\r\n\r\nThe **.pb** [final_net.zip](https://github.com/opencv/opencv/files/1321685/final_net.zip) file is attached to this report.\r\n\r\nPython code to generate the network (very similar to https://github.com/opencv/opencv_contrib/issues/1241 )\r\n\r\n    adam = Adam(lr = l_r)\r\n    model = Sequential()\r\n\r\n    #Conv2D\r\n    model.add(\r\n        Conv2D(\r\n            filters = nb_filters[0],\r\n            kernel_size = kernel_size[0],\r\n            strides = (1,1),\r\n            padding = \"same\",\r\n            activation = \"relu\",\r\n            kernel_initializer=\"random_normal\",\r\n            input_shape = (x_shape, y_shape,3)\r\n            )\r\n        )\r\n\r\n    model.add( Dropout(drop[0]) )\r\n    #MaxPooling2D\r\n    model.add( MaxPooling2D( pool_size = (2,2) ) )\r\n    model.add( Dropout(drop[1]) )\r\n\r\n    #Conv2D\r\n    model.add(\r\n        Conv2D(\r\n            filters = nb_filters[1],\r\n            kernel_size = kernel_size[1],\r\n            strides = (1,1),\r\n            padding=\"same\",\r\n            activation=\"relu\",\r\n            kernel_initializer=\"random_normal\")\r\n        )\r\n    model.add( Dropout(drop[2]) )\r\n\r\n    #MaxPooling2D\r\n    model.add( MaxPooling2D( pool_size = (2,2) ) )\r\n    model.add( Dropout(drop[3]) )\r\n\r\n    #Conv2D\r\n    model.add(\r\n        Conv2D(\r\n            filters = nb_filters[2],\r\n            kernel_size = kernel_size[2],\r\n            strides = (1,1),\r\n            padding=\"same\",\r\n            activation = \"relu\",\r\n            kernel_initializer=\"random_normal\")\r\n        )\r\n    model.add(Dropout(drop[4]) )\r\n\r\n    #MaxPooling2D\r\n    model.add( MaxPooling2D( pool_size = (2,2) )  )\r\n    model.add( Dropout(drop[5]) )\r\n\r\n    # Changed Flatten to Reshape \r\n    model.add( Reshape(1024,) )\r\n\r\n    #Output \r\n    model.add(\r\n        Dense(\r\n            units = 2,\r\n            activation=\"softmax\")\r\n        )\r\n\r\n\r\n    model.compile(\r\n        optimizer = adam,\r\n        loss = \"binary_crossentropy\",\r\n        metrics = [\"accuracy\"])\r\n\r\nCode to save the network:\r\n\r\n    sess = K.get_session()\r\n    K.set_learning_phase(0)\r\n    net_model = load_model(weight_file_path)\r\n    pred = [None]*num_output\r\n    pred_node_names = [None]*num_output\r\n    for i in range(num_output):\r\n        pred_node_names[i] = prefix_output_node_names_of_final_network+str(i)\r\n        pred[i] = tf.identity(net_model.output[i], name=pred_node_names[i])\r\n    print('output nodes names are: ', pred_node_names)\r\n\r\n    constant_graph = graph_util.convert_variables_to_constants(sess, sess.graph.as_graph_def(), pred_node_names)\r\n    graph_io.write_graph(constant_graph, output_fld, output_graph_name, as_text=False)\r\n    print('saved the constant graph (ready for inference) at:',osp.join(output_fld, output_graph_name))\r\n"},"comment":{"url":"https://api.github.com/repos/opencv/opencv/issues/comments/393243009","html_url":"https://github.com/opencv/opencv/issues/9682#issuecomment-393243009","issue_url":"https://api.github.com/repos/opencv/opencv/issues/9682","id":393243009,"user":{"login":"kbarni","id":26119925,"avatar_url":"https://avatars2.githubusercontent.com/u/26119925?v=4","gravatar_id":"","url":"https://api.github.com/users/kbarni","html_url":"https://github.com/kbarni","followers_url":"https://api.github.com/users/kbarni/followers","following_url":"https://api.github.com/users/kbarni/following{/other_user}","gists_url":"https://api.github.com/users/kbarni/gists{/gist_id}","starred_url":"https://api.github.com/users/kbarni/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/kbarni/subscriptions","organizations_url":"https://api.github.com/users/kbarni/orgs","repos_url":"https://api.github.com/users/kbarni/repos","events_url":"https://api.github.com/users/kbarni/events{/privacy}","received_events_url":"https://api.github.com/users/kbarni/received_events","type":"User","site_admin":false},"created_at":"2018-05-30T17:13:37Z","updated_at":"2018-05-30T17:13:37Z","author_association":"NONE","body":"I confirm @rodrigo2019 's observation: Reshape doesn't work anymore in Keras (I'm using 2.1.6); it's converted in *Shape/StridedSlice* operations that aren't recognized by OpenCV anymore.\r\n\r\nTo be more specific: I converted the same network with keras 2.0.8 and 2.1.6 to tensorflow. The first one is read by OpenCV (3.4.1), the second one gives an error (`(-2) Unknown layer type Shape in op reshape_1/Shape in function populateNet`)\r\n\r\nI'm trying to use some workaround by transforming the shape operation into a constant and the stridedslice into an identity, but the results aren't good atm.\r\n\r\nCan you add these two operations to the DNN module, so the networks can be used directly?"}}
{"actor":{"display_login":"kbarni"}}