def setup(): """ Very use full if you use tensorflow-gpu and your using your GPU at the ame time -> tensorflow want to use the whole Video Ram :return: None """ config = ConfigProto() config.gpu_options.allow_growth = True config.log_device_placement = True session = InteractiveSession(config=config) set_session(session) # check if directories for models and logs exists, if not there are created if not os.path.isdir("models"): os.mkdir("models") if not os.path.isdir("logs"): os.mkdir("logs")
#!/usr/bin/env python3 # if version.parse(tf.__version__).release[0] >= 2: # THIS SEEMS TO WORK IN BOTH tf 2 and 1 from packaging import version import tensorflow as tf from tensorflow.keras.backend import set_session from tensorflow.compat.v1 import ConfigProto from tensorflow.compat.v1 import InteractiveSession, Session config = ConfigProto() config.gpu_options.allow_growth = True config.log_device_placement = True # to log device placement (on which device the operation ran) # session = InteractiveSession(config=config) import tensorflow as tf session = Session(config=config) set_session( session) # set this TensorFlow session as the default session for Keras import tensorflow.keras as keras from tensorflow.keras.datasets import mnist from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Dense, Dropout from tensorflow.keras.layers import Flatten, MaxPooling2D, Conv2D from tensorflow.keras.callbacks import TensorBoard (X_train, y_train), (X_test, y_test) = mnist.load_data() X_train = X_train.reshape(60000, 28, 28, 1).astype('float32') X_test = X_test.reshape(10000, 28, 28, 1).astype('float32')
import tensorflow as tf from tensorflow.python.framework import ops ops.reset_default_graph() #tf.compat.v1.disable_eager_execution() #init = tf.compat.v1.global_variables_initializer() from model import build_model from tensorflow.compat.v1 import ConfigProto from tensorflow.compat.v1 import InteractiveSession config = ConfigProto() config.gpu_options.allow_growth = True config.allow_soft_placement = True config.log_device_placement = True #session = InteractiveSession(config=config) tensor_regex = re.compile('.*:\d*') # Get a tensor by name, convenience method def t(tensor_name): tensor_name = tensor_name + ":0" if not tensor_regex.match( tensor_name) else tensor_name return tf.compat.v1.get_default_graph().get_tensor_by_name(tensor_name) # Called from train_ann to perform a test of the train or test data, needs to separate pos/neg to get accurate #'s def train_ann_test_batch( sess,
from keras.backend.tensorflow_backend import set_session from tensorflow.compat.v1 import ConfigProto from tensorflow.compat.v1 import InteractiveSession, Session config = ConfigProto() config.gpu_options.allow_growth = True config.log_device_placement = True # to log device placement (on which device the operation ran) # session = InteractiveSession(config=config) import tensorflow as tf session = tf.Session(config=config) set_session(session) # set this TensorFlow session as the default session for Keras