Skip to content
This repository was archived by the owner on Dec 21, 2023. It is now read-only.

Commit c8b7be6

Browse files
authored
Minor Clean Ups (#3014)
* Fix formatting issues * Remove obsolete method
1 parent 0b86e07 commit c8b7be6

File tree

6 files changed

+78
-117
lines changed

6 files changed

+78
-117
lines changed

src/python/turicreate/test/test_activity_classifier.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from numbers import Number
1616
from . import util as test_util
1717
import pytest
18-
from turicreate.toolkits._internal_utils import _mac_ver, _read_env_var_cpp
18+
from turicreate.toolkits._internal_utils import _mac_ver
1919
from turicreate.toolkits._main import ToolkitError as _ToolkitError
2020
import uuid
2121

src/python/turicreate/test/test_object_detector.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@
1919
from turicreate.toolkits._main import ToolkitError as _ToolkitError
2020
from turicreate.toolkits._internal_utils import (
2121
_raise_error_if_not_sarray,
22-
_mac_ver,
23-
_read_env_var_cpp,
22+
_mac_ver
2423
)
2524
from six import StringIO
2625
import coremltools

src/python/turicreate/test/test_one_shot_object_detector.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@
1919
from turicreate.toolkits._main import ToolkitError as _ToolkitError
2020
from turicreate.toolkits._internal_utils import (
2121
_raise_error_if_not_sarray,
22-
_mac_ver,
23-
_read_env_var_cpp,
22+
_mac_ver
2423
)
2524
import coremltools
2625

src/python/turicreate/toolkits/_internal_utils.py

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,6 @@
3333
}
3434

3535

36-
def _read_env_var_cpp(var_name):
37-
"""
38-
Reads environment variables used to enable/disable the new C++
39-
implementations of existing Python toolkits.
40-
"""
41-
# Default to using the C++ implementation for toolkits that have one.
42-
# TODO: Remove the old implementations, and this function, once the dust has
43-
# settled.
44-
import os as _os
45-
46-
return bool(int(_os.environ.get(var_name, "1")))
47-
48-
4936
def _toolkit_serialize_summary_struct(model, sections, section_titles):
5037
"""
5138
Serialize model summary into a dict with ordered lists of sections and section titles

src/python/turicreate/toolkits/_model.py

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@
1818
import turicreate.extensions as _extensions
1919
from turicreate.extensions import _wrap_function_return
2020
from turicreate.toolkits._internal_utils import (
21-
_toolkit_serialize_summary_struct,
22-
_read_env_var_cpp,
21+
_toolkit_serialize_summary_struct
2322
)
2423
from turicreate.util import _make_internal_url
2524
from turicreate.toolkits._main import ToolkitError
@@ -31,18 +30,6 @@
3130

3231
MODEL_NAME_MAP = {}
3332

34-
# Object detector use C++ codepath
35-
OD_USE_CPP = _read_env_var_cpp("TURI_OD_USE_CPP_PATH")
36-
37-
# Activity Classifier use C++ codepath
38-
AC_USE_CPP = _read_env_var_cpp("TURI_AC_USE_CPP_PATH")
39-
40-
# Style Transfer use C++ codepath
41-
ST_USE_CPP = _read_env_var_cpp("TURI_ST_USE_CPP_PATH")
42-
43-
# Drawing Classifier use C++ codepath
44-
DC_USE_CPP = _read_env_var_cpp("TURI_DC_USE_CPP_PATH")
45-
4633

4734
def load_model(location):
4835
"""
@@ -125,35 +112,35 @@ def load_model(location):
125112
model_version = model_data["model_version"]
126113
del model_data["model_version"]
127114

128-
if name == "activity_classifier" and AC_USE_CPP:
115+
if name == "activity_classifier":
129116
import turicreate.toolkits.libtctensorflow
130117

131118
model = _extensions.activity_classifier()
132119
model.import_from_custom_model(model_data, model_version)
133120
return cls(model)
134121

135-
if name == "object_detector" and OD_USE_CPP:
122+
if name == "object_detector":
136123
import turicreate.toolkits.libtctensorflow
137124

138125
model = _extensions.object_detector()
139126
model.import_from_custom_model(model_data, model_version)
140127
return cls(model)
141128

142-
if name == "style_transfer" and ST_USE_CPP:
129+
if name == "style_transfer":
143130
import turicreate.toolkits.libtctensorflow
144131

145132
model = _extensions.style_transfer()
146133
model.import_from_custom_model(model_data, model_version)
147134
return cls(model)
148135

149-
if name == "drawing_classifier" and DC_USE_CPP:
136+
if name == "drawing_classifier":
150137
import turicreate.toolkits.libtctensorflow
151138

152139
model = _extensions.drawing_classifier()
153140
model.import_from_custom_model(model_data, model_version)
154141
return cls(model)
155142

156-
if name == "one_shot_object_detector" and OD_USE_CPP:
143+
if name == "one_shot_object_detector":
157144
import turicreate.toolkits.libtctensorflow
158145

159146
od_cls = MODEL_NAME_MAP["object_detector"]

src/python/turicreate/toolkits/_tf_model.py

Lines changed: 69 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -13,101 +13,90 @@
1313

1414
@_six.add_metaclass(_abc.ABCMeta)
1515
class TensorFlowModel(object):
16-
1716
"""
18-
Base Class for neural networks written in tensorflow used to abstract across model
19-
architectures. It defines the computational graph and initialize a session to run the graph.
20-
21-
Make placeholders for input and targets
22-
self.data = tf.placeholder()
23-
self.target = tf.placeholder()
24-
25-
Make dictionaries for weights and biases
26-
self.weights = {
27-
'conv' : tf.Variable()
28-
'dense0' : tf.Variable()
29-
}
30-
self.biases = {
31-
'conv' : tf.Variable()
32-
'dense0' : tf.Variable()
33-
}
34-
35-
36-
Make the graph
37-
conv = tf.nn.conv1d(self.data, self.weights['conv'], ..)
38-
dense = tf.add(tf.matmul() + self.bias())
39-
...
40-
41-
Make loss_op with the loss and train_op with the optimizer
42-
loss_op =
43-
train_op =
44-
45-
Define Session
46-
self.sess = tf.Session()
47-
"""
17+
Base Class for neural networks written in tensorflow used to abstract across model
18+
architectures. It defines the computational graph and initialize a session to run the graph.
19+
20+
Make placeholders for input and targets
21+
self.data = tf.placeholder()
22+
self.target = tf.placeholder()
23+
24+
Make dictionaries for weights and biases
25+
self.weights = {
26+
'conv' : tf.Variable()
27+
'dense0' : tf.Variable()
28+
}
29+
self.biases = {
30+
'conv' : tf.Variable()
31+
'dense0' : tf.Variable()
32+
}
33+
34+
Make the graph
35+
conv = tf.nn.conv1d(self.data, self.weights['conv'], ..)
36+
dense = tf.add(tf.matmul() + self.bias())
37+
...
38+
39+
Make loss_op with the loss and train_op with the optimizer
40+
loss_op =
41+
train_op =
42+
43+
Define Session
44+
self.sess = tf.Session()
45+
"""
4846

4947
@_abc.abstractmethod
5048
def __init__(self):
49+
"""
50+
Train will do a forward and backward pass and update weights
51+
This accepts a dictionary that has feature/target as key and
52+
the numpy arrays as value corresponding to them respectively.
53+
It returns a dictionary of loss and output (probabilities)
54+
This matches model backend train
55+
56+
Argument : A dictionary of input and true labels
57+
Returns : A dictionary of expected output (toolkit specific)
58+
59+
It will train a mini batch by running the optimizer in the session
60+
Running the optimizer is thepart that does back propogation
61+
self.sess.run([train_op, loss_op, ..], feed_dict= {self.data = ..., self.target= ..})
62+
"""
5163
raise NotImplementedError
5264

53-
"""
54-
Train will do a forward and backward pass and update weights
55-
This accepts a dictionary that has feature/target as key and
56-
the numpy arrays as value corresponding to them respectively.
57-
It returns a dictionary of loss and output (probabilities)
58-
This matches model backend train
59-
60-
Argument : A dictionary of input and true labels
61-
Returns : A dictionary of expected output (toolkit specific)
62-
63-
It will train a mini batch by running the optimizer in the session
64-
Running the optimizer is thepart that does back propogation
65-
self.sess.run([train_op, loss_op, ..], feed_dict= {self.data = ..., self.target= ..})
66-
67-
"""
68-
6965
def train(self, feed_dict):
66+
"""
67+
Predict does only a forward pass and does not update any weights
68+
This accepts a dictionary that has feature/target as key and
69+
the numpy arrays as value corresponding to them respectively.
70+
It also returns a dictionary of loss and output
71+
This matches the model backend predict
72+
73+
Argument : A dictionary of input and true labels
74+
Returns : A dictionary of expected output (toolkit specific)
75+
76+
It will calculate the specified outputs w
77+
self.sess.run([loss_op, ..], feed_dict= {self.data = ..., self.target= ..})
78+
"""
7079
raise NotImplementedError
7180

72-
"""
73-
Predict does only a forward pass and does not update any weights
74-
This accepts a dictionary that has feature/target as key and
75-
the numpy arrays as value corresponding to them respectively.
76-
It also returns a dictionary of loss and output
77-
This matches the model backend predict
78-
79-
Argument : A dictionary of input and true labels
80-
Returns : A dictionary of expected output (toolkit specific)
81-
82-
It will calculate the specified outputs w
83-
self.sess.run([loss_op, ..], feed_dict= {self.data = ..., self.target= ..})
84-
85-
"""
86-
8781
def predict(self, feed_dict):
88-
raise NotImplementedError
89-
90-
"""
91-
Exports the network weights in CoreML format.
92-
Returns : A dictionary of weight names as keys and
82+
"""
83+
Exports the network weights in CoreML format.
84+
Returns : A dictionary of weight names as keys and
9385
94-
layer_names = tf.trainable_variables()
95-
layer_weights = self.sess.run(tvars)
86+
layer_names = tf.trainable_variables()
87+
layer_weights = self.sess.run(tvars)
9688
97-
This will get you the layer names from tensorflow and their corresponding
98-
values. They need to be converted to CoreML format and stored back in a
99-
dictionary with their names and values of correct shapes.
100-
101-
102-
"""
89+
This will get you the layer names from tensorflow and their corresponding
90+
values. They need to be converted to CoreML format and stored back in a
91+
dictionary with their names and values of correct shapes.
92+
"""
93+
raise NotImplementedError
10394

10495
def export_weights(self):
96+
"""
97+
Sets the optimizer to learn at the specified learning rate or using a learning rate scheduler.
98+
"""
10599
raise NotImplementedError
106100

107-
"""
108-
Sets the optimizer to learn at the specified learning rate or using a learning rate scheduler.
109-
110-
"""
111-
112101
def set_learning_rate(self, learning_rate):
113102
raise NotImplementedError

0 commit comments

Comments
 (0)