Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

py-tensorflow-macos: Update to version 2.6.0, py-tensorflow-metal: Submission #12678

Merged
merged 6 commits into from Nov 2, 2021

Conversation

essandess
Copy link
Contributor

@essandess essandess commented Oct 25, 2021

Description

  • Update to version 2.6.0
  • Rename as py-tensorflow-macos per pypi
  • Obsolete old github version tensorflow_macos
  • Submit py-tensorflow-metal

cc: @cjones051073

Type(s)
  • bugfix
  • enhancement
  • security fix
Tested on

macOS 11.6 20G165 x86_64
Xcode 13.0 13A233

Verification

Have you

  • followed our Commit Message Guidelines?
  • squashed and minimized your commits?
  • checked that there aren't other open pull requests for the same change?
  • referenced existing tickets on Trac with full URL?
  • checked your Portfile with port lint?
  • tried existing tests with sudo port test?
  • tried a full install with sudo port -vst install?
  • tested basic functionality of all binary files?

TensorFlow Metal CNN Benchmark

import numpy as np, os, pandas as pd, platform, time, warnings

import tensorflow as tf
print(tf.__version__)

from tensorflow.python.framework.ops import disable_eager_execution, enable_eager_execution
from tensorflow.python.keras import backend

from numpy import unique
from numpy import argmax
from tensorflow.keras.datasets.mnist import load_data
from tensorflow.keras import Sequential
from tensorflow.keras.layers import Dense, Conv2D, MaxPool2D, Flatten, Dropout

from IPython.display import Image
from IPython.core.display import HTML 

print(tf.config.list_physical_devices('GPU'))
if tf.test.gpu_device_name():
    print('Default GPU Device: {}'.format(tf.test.gpu_device_name()))
else:
    print("Please install GPU version of TF")

import matplotlib as mpl, matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib.colors import LightSource
from mpl_toolkits.mplot3d import Axes3D

# boldify
# Legible plot style defaults
# http://matplotlib.org/api/matplotlib_configuration_api.html
# http://matplotlib.org/users/customizing.html
mpl.rcParams['figure.figsize'] = (12.0, 8)
mpl.rc('font',**{'family': 'sans-serif', 'weight': 'bold', 'size': 14})
mpl.rc('axes',**{'titlesize': 20, 'titleweight': 'bold', 'labelsize': 16, 'labelweight': 'bold'})
mpl.rc('legend',**{'fontsize': 14})
mpl.rc('figure',**{'titlesize': 16, 'titleweight': 'bold'})
mpl.rc('lines',**{'linewidth': 2.5, 'markersize': 18, 'markeredgewidth': 0})
mpl.rc('mathtext',**{'fontset': 'custom', 'rm': 'sans:bold', 'bf': 'sans:bold', 'it': 'sans:italic', 'sf': 'sans:bold', 'default': 'it'})
# plt.rc('text',usetex=False) # [default] usetex should be False
mpl.rcParams['text.latex.preamble'] = r'\usepackage{amsmath,sfmath} \boldmath'

prop_cycle = plt.rcParams['axes.prop_cycle']
colors_default = prop_cycle.by_key()['color']

(trainX, trainy), (testX, testy) = tf.keras.datasets.mnist.load_data()
print('Train: X=%s, y=%s' % (trainX.shape, trainy.shape))
print('Test: X=%s, y=%s' % (testX.shape, testy.shape))

# a single grayscale channel
trainX = trainX[…, np.newaxis]
testX = testX[…, np.newaxis]

in_shape = trainX.shape[1:]
print(in_shape)

n_classes = len(unique(trainy))
print(n_classes)

# Normalize
trainX = trainX.astype('float32') / 255.0
testX = testX.astype('float32') / 255.0

# Create CNN Model
model = Sequential()
model.add(Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_uniform', input_shape=in_shape))
model.add(MaxPool2D((2, 2), strides=(2,2)))
model.add(Conv2D(32, (2, 2), activation='relu', kernel_initializer='he_uniform'))
model.add(MaxPool2D((2, 2), strides=(2,2)))
model.add(Flatten())
model.add(Dense(500, activation='relu', kernel_initializer='he_uniform'))
# model.add(Dropout(0.5))
model.add(Dense(n_classes, activation='softmax'))
model.summary()

model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])

start_time = time.time()
model.fit(trainX, trainy, epochs=10, batch_size=2**12, verbose=1)
elapsed_time = time.time() - start_time

print(f"Time: {elapsed_time}")

# Evaluate
loss, acc = model.evaluate(testX, testy, verbose=0)
print('Accuracy: %.3f' % acc)


# Numpy comparison tests

# edges of all the image data
images_numpy = np.vstack([trainX, testX])

start_time = time.time()

images = tf.convert_to_tensor(images_numpy, dtype=np.float32)
grad_images = tf.image.sobel_edges(images)
grad_images_numpy = grad_images.numpy()

elapsed_time = time.time() - start_time

print(f"Time: {elapsed_time}")
print(grad_images_numpy.shape)

# Compare to `opencv`

import cv2

KERNEL_SIZE = 3

start_time = time.time()
gradx = np.array([gx for gx in map(lambda x: cv2.Sobel(x, cv2.CV_64F, 1, 0, ksize=KERNEL_SIZE), images_numpy)])
grady = np.array([gx for gx in map(lambda x: cv2.Sobel(x, cv2.CV_64F, 0, 1, ksize=KERNEL_SIZE), images_numpy)])
grad_images_numpy = np.stack([gradx, grady], axis=3)
elapsed_time = time.time() - start_time

print(f"Time: {elapsed_time}")
print(grad_images_numpy.shape)

@essandess
Copy link
Contributor Author

@essandess essandess commented Oct 27, 2021

Note this possible performance issue with the latest release of tensorflow-macos and tensorflow-metal: https://developer.apple.com/forums/thread/687654

Resolved by setting batch_size=2**12.

@essandess
Copy link
Contributor Author

@essandess essandess commented Oct 27, 2021

This is ready to merge to the best of my knowledge. I believe that the CI errors are timeouts.

@mascguy mascguy self-assigned this Oct 28, 2021
@mascguy mascguy requested a review from reneeotten Oct 28, 2021
python/py-tensorflow_macos/Portfile Show resolved Hide resolved
python/py-tensorflow/Portfile Show resolved Hide resolved
python/py-tensorflow-metal/Portfile Outdated Show resolved Hide resolved
* Update to version 2.6.0
* Rename as `py-tensorflow-macos` per pypi
python/py-tensorflow_macos/Portfile Outdated Show resolved Hide resolved
python/py-tensorflow_macos/Portfile Outdated Show resolved Hide resolved
python/py-tensorflow_macos/Portfile Outdated Show resolved Hide resolved
python/py-tensorflow_macos/Portfile Outdated Show resolved Hide resolved
python/py-tensorflow_macos/Portfile Outdated Show resolved Hide resolved
@essandess
Copy link
Contributor Author

@essandess essandess commented Nov 1, 2021

@reneeotten Thanks again for all the great feedback and corrections. I now believe that TF’stensorflow-macos version is good to go and that the performance issues mentioned above are essentially user errors. Therefore, I’ll push a version of tensorflow_macos that deprecates those ports with the method you recommend.

@essandess essandess force-pushed the py-tensorflow-macos branch 2 times, most recently from a5a68d9 to ad82e1b Compare Nov 1, 2021
@essandess
Copy link
Contributor Author

@essandess essandess commented Nov 2, 2021

I believe that all the CI errors are timeouts and that this PR is ready to merge.

@cjones051073
Copy link
Contributor

@cjones051073 cjones051073 commented Nov 2, 2021

Sorry to be late to the party here.. Having metal acceleration for TF is really nice. I don't see anything obvious here I object strongly to, but as @reneeotten needs to explicitly OK his requested changes lets wait until we hear back from him.

@essandess
Copy link
Contributor Author

@essandess essandess commented Nov 2, 2021

Sorry to be late to the party here.. Having metal acceleration for TF is really nice. I don't see anything obvious here I object strongly to, but as @reneeotten needs to explicitly OK his requested changes lets wait until we hear back from him.

👍🏻

@reneeotten reneeotten merged commit 529e8d8 into macports:master Nov 2, 2021
2 of 3 checks passed
@essandess essandess deleted the py-tensorflow-macos branch Nov 2, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment