-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit ef770a9
Showing
44 changed files
with
1,251 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# ML |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
source ./venv/bin/activate | ||
deactivate | ||
|
||
|
||
752 venv/bin/python regression/testfroze.py | ||
753 venv/bin/python regression/fuel.py | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
import pathlib | ||
|
||
import matplotlib.pyplot as plt | ||
import numpy as np | ||
import pandas as pd | ||
import seaborn as sns | ||
|
||
import tensorflow as tf | ||
|
||
from tensorflow import keras | ||
from tensorflow.keras import layers | ||
|
||
from tensorflow.python.framework.convert_to_constants import convert_variables_to_constants_v2 | ||
|
||
print(tf.__version__) | ||
|
||
|
||
import tensorflow_docs as tfdocs | ||
import tensorflow_docs.plots | ||
import tensorflow_docs.modeling | ||
|
||
|
||
#import dataset | ||
dataset_path = keras.utils.get_file("auto-mpg.data", "http://archive.ics.uci.edu/ml/machine-learning-databases/auto-mpg/auto-mpg.data") | ||
print("dataset_path:"+dataset_path) | ||
column_names = ['MPG','Cylinders','Displacement','Horsepower','Weight', | ||
'Acceleration', 'Model Year', 'Origin'] | ||
raw_dataset = pd.read_csv(dataset_path, names=column_names, | ||
na_values = "?", comment='\t', | ||
sep=" ", skipinitialspace=True) | ||
|
||
dataset = raw_dataset.copy() | ||
#print(dataset) | ||
|
||
dataset.isna().sum() | ||
|
||
|
||
dataset = dataset.dropna() | ||
|
||
#print(dataset) | ||
|
||
train_dataset = dataset.sample(frac=0.8,random_state=0) | ||
test_dataset = dataset.drop(train_dataset.index) | ||
print ("test_dataset[0]:") | ||
print (test_dataset[:1]) | ||
|
||
|
||
|
||
sns.pairplot(train_dataset[["MPG", "Cylinders", "Displacement", "Weight"]], diag_kind="kde") | ||
|
||
plt.show() | ||
|
||
train_stats = train_dataset.describe() | ||
train_stats.pop("MPG") | ||
train_stats = train_stats.transpose() | ||
train_stats | ||
print("train_stats:") | ||
|
||
print(train_stats) | ||
|
||
train_labels = train_dataset.pop('MPG') | ||
test_labels = test_dataset.pop('MPG') | ||
|
||
|
||
def norm(x): | ||
return (x - train_stats['mean']) / train_stats['std'] | ||
normed_train_data = norm(train_dataset) | ||
normed_test_data = norm(test_dataset) | ||
|
||
print("length key:"+str(len(train_dataset.keys()))) | ||
def build_model(): | ||
model = keras.Sequential([ | ||
layers.Dense(64, activation='relu', input_shape=[len(train_dataset.keys())]), | ||
layers.Dense(64, activation='relu'), | ||
layers.Dense(1) | ||
]) | ||
|
||
optimizer = tf.keras.optimizers.RMSprop(0.001) | ||
|
||
model.compile(loss='mse', | ||
optimizer=optimizer, | ||
metrics=['mae', 'mse']) | ||
return model | ||
|
||
model = build_model() | ||
|
||
|
||
example_batch = normed_train_data[:10] | ||
example_result = model.predict(example_batch) | ||
example_result | ||
|
||
EPOCHS = 1000 | ||
|
||
history = model.fit( | ||
normed_train_data, train_labels, | ||
epochs=EPOCHS, validation_split = 0.2, verbose=0, | ||
callbacks=[tfdocs.modeling.EpochDots()]) | ||
|
||
|
||
hist = pd.DataFrame(history.history) | ||
hist['epoch'] = history.epoch | ||
hist.tail() | ||
|
||
|
||
plotter = tfdocs.plots.HistoryPlotter(smoothing_std=2) | ||
|
||
|
||
plotter.plot({'Basic': history}, metric = "mae") | ||
plt.ylim([0, 10]) | ||
plt.ylabel('MAE [MPG]') | ||
plt.show() | ||
|
||
|
||
for i in range (1): | ||
print("normed_test_data:") | ||
print(normed_test_data[:1]) | ||
print(normed_test_data.shape) | ||
print("normed_test_data:") | ||
|
||
|
||
test_predictions = model.predict(normed_test_data).flatten() | ||
|
||
a = plt.axes(aspect='equal') | ||
plt.scatter(test_labels, test_predictions) | ||
print(test_labels) | ||
print('sep') | ||
print(test_predictions) | ||
plt.xlabel('True Values [MPG]') | ||
plt.ylabel('Predictions [MPG]') | ||
lims = [0, 50] | ||
plt.xlim(lims) | ||
plt.ylim(lims) | ||
plt.plot(lims, lims) | ||
plt.show() | ||
|
||
|
||
|
||
|
||
# Save model to SavedModel format | ||
tf.saved_model.save(model, "./models") | ||
|
||
|
||
# Convert Keras model to ConcreteFunction | ||
full_model = tf.function(lambda x: model(x)) | ||
full_model = full_model.get_concrete_function( | ||
tf.TensorSpec(model.inputs[0].shape, model.inputs[0].dtype)) | ||
|
||
# Get frozen ConcreteFunction | ||
frozen_func = convert_variables_to_constants_v2(full_model) | ||
frozen_func.graph.as_graph_def() | ||
|
||
layers = [op.name for op in frozen_func.graph.get_operations()] | ||
print("-" * 50) | ||
print("Frozen model layers: ") | ||
for layer in layers: | ||
print(layer) | ||
|
||
print("-" * 50) | ||
print("Frozen model inputs: ") | ||
print(frozen_func.inputs) | ||
print("Frozen model outputs: ") | ||
print(frozen_func.outputs) | ||
|
||
# Save frozen graph from frozen ConcreteFunction to hard drive | ||
tf.io.write_graph(graph_or_graph_def=frozen_func.graph, | ||
logdir="./frozen_models", | ||
name="frozen_graph.pb", | ||
as_text=False) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
|
||
import tensorflow as tf | ||
from tensorflow import keras | ||
import numpy as np | ||
|
||
|
||
|
||
# https://www.tensorflow.org/guide/migrate | ||
# https://www.tensorflow.org/api_docs/python/tf/graph_util/import_graph_def?version=stable | ||
def wrap_frozen_graph(graph_def, inputs, outputs, print_graph=False): | ||
def _imports_graph_def(): | ||
tf.compat.v1.import_graph_def(graph_def, name="") | ||
|
||
wrapped_import = tf.compat.v1.wrap_function(_imports_graph_def, []) | ||
import_graph = wrapped_import.graph | ||
|
||
print("-" * 50) | ||
print("Frozen model layers: ") | ||
layers = [op.name for op in import_graph.get_operations()] | ||
if print_graph == True: | ||
for layer in layers: | ||
print(layer) | ||
print("-" * 50) | ||
|
||
return wrapped_import.prune( | ||
tf.nest.map_structure(import_graph.as_graph_element, inputs), | ||
tf.nest.map_structure(import_graph.as_graph_element, outputs)) | ||
|
||
|
||
def main(): | ||
|
||
|
||
test_labels =("Cylinders", "Displacement", "Horsepower", "Weight", "Acceleration", "Model Year","Origin") | ||
|
||
test_data= [[1.483887,1.865988,2.23462,1.018782, -2.530891,-1.604642, -0.715676]] | ||
|
||
# Load frozen graph using TensorFlow 1.x functions | ||
with tf.io.gfile.GFile("./frozen_models/frozen_graph.pb", "rb") as f: | ||
graph_def = tf.compat.v1.GraphDef() | ||
loaded = graph_def.ParseFromString(f.read()) | ||
|
||
# Wrap frozen graph to ConcreteFunctions | ||
frozen_func = wrap_frozen_graph(graph_def=graph_def, | ||
inputs=["x:0"], | ||
outputs=["Identity:0"], | ||
print_graph=True) | ||
|
||
print("-" * 50) | ||
print("Frozen model inputs: ") | ||
print(frozen_func.inputs) | ||
print("Frozen model outputs: ") | ||
print(frozen_func.outputs) | ||
|
||
# Get predictions for test images | ||
predictions = frozen_func(x=tf.constant(test_data))[0] | ||
|
||
# Print the prediction for the first image | ||
print("-" * 50) | ||
print("Example prediction reference:") | ||
print(predictions[0].numpy()) | ||
|
||
|
||
if __name__ == "__main__": | ||
|
||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import matplotlib | ||
import matplotlib.pyplot as plt | ||
|
||
print ("Backend: "+matplotlib.rcParams['backend']) | ||
|
||
plt.plot([1,2,1]) | ||
plt.show() |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
cmake_minimum_required(VERSION 3.10) | ||
project(example) | ||
|
||
set(CMAKE_CXX_STANDARD 17) | ||
add_executable(example main.cpp ../../src/Model.cpp ../../src/Tensor.cpp) | ||
find_package( OpenCV REQUIRED ) | ||
target_include_directories(example PRIVATE ../../include) | ||
target_link_libraries (example -ltensorflow ${OpenCV_LIBS}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// | ||
// Created by sergio on 16/05/19. | ||
// | ||
|
||
#include "../../include/Model.h" | ||
#include "../../include/Tensor.h" | ||
#include <opencv2/opencv.hpp> | ||
#include <numeric> | ||
#include <iomanip> | ||
|
||
int main() { | ||
Model model("../ssd_inception/frozen_inference_graph.pb"); | ||
Tensor outNames1{model, "num_detections"}; | ||
Tensor outNames2{model, "detection_scores"}; | ||
Tensor outNames3{model, "detection_boxes"}; | ||
Tensor outNames4{model, "detection_classes"}; | ||
|
||
Tensor inpName{model, "image_tensor"}; | ||
|
||
|
||
// Read image | ||
cv::Mat img, inp; | ||
img = cv::imread("../test.jpg", cv::IMREAD_COLOR); | ||
|
||
int rows = img.rows; | ||
int cols = img.cols; | ||
|
||
cv::resize(img, inp, cv::Size(300, 300)); | ||
cv::cvtColor(inp, inp, cv::COLOR_BGR2RGB); | ||
|
||
// Put image in Tensor | ||
std::vector<uint8_t > img_data; | ||
img_data.assign(inp.data, inp.data + inp.total() * inp.channels()); | ||
inpName.set_data(img_data, {1, 300, 300, 3}); | ||
|
||
model.run(inpName, {&outNames1, &outNames2, &outNames3, &outNames4}); | ||
|
||
// Visualize detected bounding boxes. | ||
int num_detections = (int)outNames1.get_data<float>()[0]; | ||
for (int i=0; i<num_detections; i++) { | ||
int classId = (int)outNames4.get_data<float>()[i]; | ||
float score = outNames2.get_data<float>()[i]; | ||
auto bbox_data = outNames3.get_data<float>(); | ||
std::vector<float> bbox = {bbox_data[i*4], bbox_data[i*4+1], bbox_data[i*4+2], bbox_data[i*4+3]}; | ||
if (score > 0.3) { | ||
float x = bbox[1] * cols; | ||
float y = bbox[0] * rows; | ||
float right = bbox[3] * cols; | ||
float bottom = bbox[2] * rows; | ||
|
||
cv::rectangle(img, {(int)x, (int)y}, {(int)right, (int)bottom}, {125, 255, 51}, 2); | ||
} | ||
} | ||
|
||
cv::imshow("Image", img); | ||
cv::waitKey(0); | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
cmake_minimum_required(VERSION 3.10) | ||
project(example) | ||
|
||
set(CMAKE_CXX_STANDARD 17) | ||
add_executable(example main.cpp ../../src/Model.cpp ../../src/Tensor.cpp) | ||
target_include_directories(example PRIVATE ../../include) | ||
target_link_libraries (example -ltensorflow) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import tensorflow as tf | ||
|
||
def example_1(): | ||
|
||
# Two simple inputs | ||
a = tf.placeholder(tf.float32, shape=(1, 100), name="input_a") | ||
b = tf.placeholder(tf.float32, shape=(1, 100), name="input_b") | ||
|
||
# Output | ||
c = tf.add(a, b, name='result') | ||
|
||
# To add an init operation to the model | ||
i = tf.initializers.global_variables() | ||
|
||
# Write the model definition | ||
with open('models/load_model.pb', 'wb') as f: | ||
f.write(tf.get_default_graph().as_graph_def().SerializeToString()) | ||
|
||
|
||
if __name__ == "__main__": | ||
example_1() |
Oops, something went wrong.