|
1 | 1 | import os
|
2 | 2 | import tensorflow as tf
|
| 3 | +from tensorflow.python.client import timeline |
3 | 4 | import wandb
|
4 | 5 | from wandb.keras import WandbCallback
|
5 | 6 |
|
|
44 | 45 | model.add(tf.keras.layers.Flatten())
|
45 | 46 | model.add(tf.keras.layers.Dense(config.dense_layer_size, activation='relu'))
|
46 | 47 | model.add(tf.keras.layers.Dense(num_classes, activation='softmax'))
|
| 48 | + |
| 49 | +run_options = tf.RunOptions(trace_level=tf.RunOptions.FULL_TRACE) |
| 50 | +run_metadata = tf.RunMetadata() |
47 | 51 | model.compile(loss='categorical_crossentropy', optimizer='adam',
|
| 52 | + options=run_options, run_metadata=run_metadata, |
48 | 53 | metrics=['accuracy'])
|
49 | 54 | # log the number of total parameters
|
50 | 55 | config.total_params = model.count_params()
|
51 | 56 | print("Total params: ", config.total_params)
|
| 57 | + |
52 | 58 | model.fit(X_train, y_train, validation_data=(X_test, y_test),
|
53 | 59 | epochs=config.epochs,
|
54 | 60 | callbacks=[WandbCallback(data_type="image", save_model=False),
|
55 | 61 | tf.keras.callbacks.TensorBoard(log_dir=wandb.run.dir)])
|
56 | 62 | model.save('cnn.h5')
|
57 | 63 |
|
| 64 | +# Write performance profile |
| 65 | +tl = timeline.Timeline(run_metadata.step_stats) |
| 66 | +with open('profile.json', 'w') as f: |
| 67 | + f.write(tl.generate_chrome_trace_format()) |
| 68 | + |
58 | 69 | # Convert to TensorFlow Lite model.
|
59 | 70 | converter = tf.lite.TFLiteConverter.from_keras_model_file('cnn.h5')
|
60 | 71 | converter.optimizations = [tf.lite.Optimize.OPTIMIZE_FOR_SIZE]
|
|
0 commit comments