def main(model_name, device): from tensorflow.python.compiler.mlcompute import mlcompute if device == "cpu": mlcompute.set_mlc_device(device_name='cpu') elif device == "gpu": mlcompute.set_mlc_device(device_name='gpu') else: raise Exception("Unknown devices") batch_size = 1 seq_len = 128 model = tf_utils.get_huggingface_model(model_name, batch_size, seq_len) def run_keras(args): assert len(args) == 2 model = args[0] np_input = args[1] _ = model(np_input) run_args = [ model, np.random.randint(0, 10000, size=[batch_size, seq_len]).astype(np.int32) ] mean, std = tf_utils.measure(run_keras, run_args) print( "[Keras] Mean Inference time (std dev) on {device}: {mean_time} ms ({std} ms)" .format(device=device, mean_time=mean, std=std))
def load_data_set_and_train(): data_set = load_data_set() PlotXY(data_set[0], data_set[1]) # 전처리에 쓰레드풀 사용하여 tensorflow gpu 설정은 그 이후에 해야 함. from tensorflow.python.compiler.mlcompute import mlcompute mlcompute.set_mlc_device(device_name='gpu') import network net = network.load_v3() train_only(net, data_set) return (net, data_set)
def main(graph_path, device): from tensorflow.python.compiler.mlcompute import mlcompute if device == "cpu": mlcompute.set_mlc_device(device_name='cpu') elif device == "gpu": mlcompute.set_mlc_device(device_name='gpu') else: raise Exception("Unknown devices") batch_size = 1 seq_len = 128 if os.path.exists(graph_path) is False: raise Exception("Graph doesn't exist. Please dump tf graph first.") with tf.io.gfile.GFile(graph_path, "rb") as fi: graph_def = tf.compat.v1.GraphDef() loaded = graph_def.ParseFromString(fi.read()) g = tf.graph_util.import_graph_def(graph_def) with tf.compat.v1.Session(graph=g) as sess: dummy_input = np.random.randint(0, 10000, size=[batch_size, seq_len]).astype(np.int32) x = tf.compat.v1.get_default_graph().get_tensor_by_name('x:0') fetches = ["tf_bert_for_sequence_classification/classifier/BiasAdd:0"] feed_dict = {x: dummy_input} # warm up _ = sess.run(fetches=["tf_bert_for_sequence_classification/classifier/BiasAdd:0"], feed_dict={x: dummy_input}) def run_graph(args): sess = args[0] fetches = args[1] feed_dict = args[2] _ = sess.run(fetches=["tf_bert_for_sequence_classification/classifier/BiasAdd:0"], feed_dict={x: dummy_input}) run_args = [ sess, fetches, feed_dict ] mean, std = tf_utils.measure(run_graph, run_args) print("[Graphdef] Mean Inference time (std dev) on {device}: {mean_time} ms ({std} ms)".format( device=device, mean_time=mean, std=std ))
import time import numpy as np import tensorflow as tf from tensorflow.python.compiler.mlcompute import mlcompute mlcompute.set_mlc_device(device_name='gpu') tf.config.run_functions_eagerly(False) # Model / data parameters num_classes = 10 input_shape = (28, 28, 1) # the data, split between train and test sets (x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data() # Scale images to the [0, 1] range x_train = x_train.astype("float32") / 255 x_test = x_test.astype("float32") / 255 # Make sure images have shape (28, 28, 1) x_train = np.expand_dims(x_train, -1) x_test = np.expand_dims(x_test, -1) print("x_train shape:", x_train.shape) print(x_train.shape[0], "train samples") print(x_test.shape[0], "test samples") # convert class vectors to binary class matrices y_train = tf.keras.utils.to_categorical(y_train, num_classes) y_test = tf.keras.utils.to_categorical(y_test, num_classes) """ ## Build the model """
from tensorflow.python.compiler.mlcompute import mlcompute # Select CPU device. mlcompute.set_mlc_device(device_name='cpu') # Available options are 'cpu', 'gpu', and ‘any'. import tensorflow as tf # Import mlcompute module to use the optional set_mlc_device API for device selection with ML Compute. from tensorflow.keras.layers import Dense, Flatten, Conv2D from tensorflow.keras import Model mnist = tf.keras.datasets.mnist (x_train, y_train), (x_test, y_test) = mnist.load_data() x_train, x_test = x_train / 255.0, x_test / 255.0 # Add a channels dimension x_train = x_train[..., tf.newaxis].astype("float32") x_test = x_test[..., tf.newaxis].astype("float32") train_ds = tf.data.Dataset.from_tensor_slices( (x_train, y_train)).shuffle(10000).batch(32) test_ds = tf.data.Dataset.from_tensor_slices((x_test, y_test)).batch(32) class MyModel(Model): def __init__(self): super(MyModel, self).__init__() self.conv1 = Conv2D(32, 3, activation='relu') self.flatten = Flatten()
# 训练神经网络,将参数(weight)存入HDF5文件 import numpy as np import tensorflow as tf from network import * from utils import * from tensorflow.python.compiler.mlcompute import mlcompute mlcompute.set_mlc_device(device_name='any') def train(): notes = get_notes() # 得到所有不重复的音调数目 num_pitch = len(set(notes)) network_input, network_output = prepare_sequences(notes, num_pitch) model = network_model( network_input, num_pitch, ) # 输入,音符的数量,训练后的参数文件(训练的时候不用写) filepath = "weights-{epoch:02d}-{loss:.4f}.hdf5" # 用checkpoint(检查点)文件在每一个Epoch结束时保存模型的参数 # 不怕训练过程中丢失模型参数,当对loss损失满意的时候可以随时停止训练 checkpoint = tf.keras.callbacks.ModelCheckpoint( filepath, # 保存参数文件的路径 monitor='loss', # 衡量的标准 verbose=0, # 不用冗余模式 save_best_only=True, # 最近出现的用monitor衡量的最好的参数不会被覆盖 mode='min' # 关注的是loss的最小值 )
import glob import numpy as np import tensorflow as tf from tensorflow.python.compiler.mlcompute import mlcompute mlcompute.set_mlc_device('gpu') #验证集和测试集结果生成 #加载图片 def load_img(path): image = tf.io.read_file(path) image = tf.image.decode_jpeg(image, channels=3) image = tf.image.resize(image, [32, 32]) image = tf.cast(image, tf.float32) image = image / 31 return image #本地测试集测试 imgs_path_t = glob.glob('val/*jpg') print(imgs_path_t) len_test = len(imgs_path_t) print("test lens:" + str(len_test)) #加载模型 model_test = tf.keras.models.load_model( '/Users/gutao/Desktop/p/cv21b/mds/m1.h5') a = 0 f = open("171250575-a.txt", 'w') for i in imgs_path_t: a = a + 1
# -*- coding: utf-8 -*- import glob import numpy as np import tensorflow as tf from tensorflow.python.compiler.mlcompute import mlcompute mlcompute.set_mlc_device(device_name="gpu") #测试集获取 dic = {} f = open("val_anno.txt") line = f.readline() while line: str1 = line.split(' ')[0] str2 = int(line.split(' ')[1]) dic[str1] = str2 line = f.readline() f.close() imgs_path_t = glob.glob('val/*jpg') labels_t = [dic[img_path_t.split('/')[1]] for img_path_t in imgs_path_t] #训练集获取 imgs_path = glob.glob('train/*/*jpg') all_labels_name = [img_path.split('/')[1] for img_path in imgs_path] labels_name = np.unique(all_labels_name) all_labels = [int(name) for name in all_labels_name] #乱序化 np.random.seed(2021) random_index = np.random.permutation(len(imgs_path)) imgs_path = np.array(imgs_path)[random_index] all_labels = np.array(all_labels)[random_index] imgs_path_t = np.array(imgs_path_t)
Gets to 99.25% test accuracy after 12 epochs (there is still a lot of margin for parameter tuning). 16 seconds per epoch on a GRID K520 GPU. ''' from __future__ import print_function import tensorflow as tf from tensorflow.keras.datasets import mnist from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Dense, Dropout, Flatten from tensorflow.keras.layers import Conv2D, MaxPooling2D from tensorflow.keras import backend as K from tensorflow.python.compiler.mlcompute import mlcompute mlcompute.set_mlc_device(device_name="any") batch_size = 128 num_classes = 10 epochs = 12 # input image dimensions img_rows, img_cols = 28, 28 # the data, split between train and test sets (x_train, y_train), (x_test, y_test) = mnist.load_data() if K.image_data_format() == 'channels_first': x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols) x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols) input_shape = (1, img_rows, img_cols)