<jupyter_start><jupyter_text>Model<jupyter_code>model = Sequential()

model.add(Reshape((28,28,1), input_shape=(28,28)))
model.add(Lambda(lambda x: K.cast(x, "float32")/255))
model.add(Conv2D(8,kernel_size=(2,2)))
model.add(ReLU())
model.add(MaxPool2D(pool_size=(3,3)))
model.add(Conv2D(8,kernel_size=(2,2)))
model.add(ReLU())
model.add(MaxPool2D(pool_size=(3,3)))
model.add(Flatten())
model.add(Dense(10, activation='sigmoid'))

opt = Adam(learning_rate=0.00005)
model.compile(loss='categorical_crossentropy', optimizer=opt, metrics=['accuracy'])
model.summary()<jupyter_output>Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
reshape (Reshape)            (None, 28, 28, 1)         0         
_________________________________________________________________
lambda (Lambda)              (None, 28, 28, 1)         0         
_________________________________________________________________
conv2d (Conv2D)              (None, 27, 27, 8)         40        
_________________________________________________________________
re_lu (ReLU)                 (None, 27, 27, 8)         0         
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 9, 9, 8)           0         
_________________________________________________________________
conv2d_1 (Conv2D)            (None, 8, 8, 8)           264       
________________________________________________________[...]<jupyter_text>Training<jupyter_code>scheduler = LearningRateScheduler(lambda x: 5e-5 if x<80 else 5e-6)
model.fit(X_train,y_train, epochs=100, batch_size=32, validation_data=(X_test,y_test), callbacks=[scheduler])<jupyter_output>Train on 54000 samples, validate on 6000 samples
Epoch 1/100
54000/54000 [==============================] - 9s 173us/sample - loss: 2.2655 - acc: 0.1264 - val_loss: 2.2194 - val_acc: 0.2097
Epoch 2/100
54000/54000 [==============================] - 9s 176us/sample - loss: 2.1299 - acc: 0.2839 - val_loss: 2.0118 - val_acc: 0.3477
Epoch 3/100
54000/54000 [==============================] - 10s 184us/sample - loss: 1.8376 - acc: 0.4206 - val_loss: 1.6837 - val_acc: 0.4693
Epoch 4/100
54000/54000 [==============================] - 10s 188us/sample - loss: 1.5641 - acc: 0.5001 - val_loss: 1.4742 - val_acc: 0.5238
Epoch 5/100
54000/54000 [==============================] - 9s 174us/sample - loss: 1.3954 - acc: 0.5496 - val_loss: 1.3373 - val_acc: 0.5657
Epoch 6/100
54000/54000 [==============================] - 9s 173us/sample - loss: 1.2783 - acc: 0.5846 - val_loss: 1.2354 - val_acc: 0.5990
Epoch 7/100
54000/54000 [==============================] - 10s 185us/sample - loss: 1.1906 - acc: 0.613[...]<jupyter_text>Original acc<jupyter_code>preds = model.predict(X_test)
res = np.argmax(preds,axis=1) == np.argmax(y_test,axis=1)
np.sum(res)/len(res)<jupyter_output><empty_output><jupyter_text>Save<jupyter_code>model.save("model.h5")<jupyter_output><empty_output><jupyter_text>Convert to TF format<jupyter_code>def freeze_session(session, keep_var_names=None, output_names=None, clear_devices=True):
    """
    Freezes the state of a session into a pruned computation graph.

    Creates a new computation graph where variable nodes are replaced by
    constants taking their current value in the session. The new graph will be
    pruned so subgraphs that are not necessary to compute the requested
    outputs are removed.
    @param session The TensorFlow session to be frozen.
    @param keep_var_names A list of variable names that should not be frozen,
                          or None to freeze all the variables in the graph.
    @param output_names Names of the relevant graph outputs.
    @param clear_devices Remove the device directives from the graph for better portability.
    @return The frozen graph definition.
    """
    graph = session.graph
    with graph.as_default():
        freeze_var_names = list(set(v.op.name for v in tf.global_variables()).difference(keep_var_names or []))
        output_names = output_names or []
        output_names += [v.op.name for v in tf.global_variables()]
        input_graph_def = graph.as_graph_def()
        if clear_devices:
            for node in input_graph_def.node:
                node.device = ""
        frozen_graph = tf.graph_util.convert_variables_to_constants(
            session, input_graph_def, output_names, freeze_var_names)
        return frozen_graph
k_inputs = [out.op.name for out in model.inputs]
k_outputs = [out.op.name for out in model.outputs]
k_inputs, k_outputs
frozen_graph = freeze_session(K.get_session(), output_names=k_outputs)
tf.train.write_graph(frozen_graph, "tf_model", "my_model.pb", as_text=False)<jupyter_output><empty_output><jupyter_text>TODO: To TRT<jupyter_code>graph = tf.Graph()
sess = tf.InteractiveSession(graph = graph)

with tf.gfile.GFile("./tf_model/my_model.pb", 'rb') as f:
    graph_def = tf.GraphDef()
    graph_def.ParseFromString(f.read())
converter = trt.TrtGraphConverter(
            input_graph_def=graph_def,
            nodes_blacklist=k_outputs,
            max_workspace_size_bytes=1 << 32,
            precision_mode='INT8',
            minimum_segment_size=2,
            is_dynamic_op=True,
            maximum_cached_engines=100)
trt_frozen_graph = converter.convert()
type(trt_frozen_graph)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15402886899323953, 'fraction_numerical': 0.06705645898044948, 'mean_word_length': 4.04516129032258, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '33913', 'license': 'MIT', 'n_tokens_mistral': 1971, 'n_tokens_neox': 1707, 'n_words': 380, 'path': 'KERAS_train_TF_TRT.ipynb', 'repo_name': 'bwosh/keras_to_trt', 'size': '5473'}