def test_set_backend(self): # store env variables # when testing on backends like GPU the tests are run with NGRPAH_TF_BACKEND # by storing and restoring the env_variables we run the tests independent of the backend set # currently we store and restore only the NGRPAH_TF_BACKEND env_var_map = self.store_env_variables() # test ngraph_bridge.enable() backend_cpu = 'CPU' backend_interpreter = 'INTERPRETER' found_cpu = False found_interpreter = False # These will only print when running pytest with flag "-s" print("Number of supported backends ", ngraph_bridge.backends_len()) supported_backends = ngraph_bridge.list_backends() print(" ****** Supported Backends ****** ") for backend_name in supported_backends: print(backend_name) if backend_name == backend_cpu: found_cpu = True if backend_name == backend_interpreter: found_interpreter = True print(" ******************************** ") assert (found_cpu and found_interpreter) == True # Create Graph val = tf.placeholder(tf.float32) out1 = tf.abs(val) out2 = tf.abs(out1) # set INTERPRETER backend assert ngraph_bridge.is_supported_backend(backend_interpreter) == True ngraph_bridge.set_backend(backend_interpreter) currently_set_backend = ngraph_bridge.get_currently_set_backend_name() assert currently_set_backend == backend_interpreter # create new session to execute graph # If you want to re-confirm which backend the graph was executed # currently the only way is to enable NGRAPH_TF_VLOG_LEVEL=5 with tf.Session() as sess: sess.run((out2, ), feed_dict={val: ((1.4, -0.5, -1))}) currently_set_backend = ngraph_bridge.get_currently_set_backend_name() assert currently_set_backend == backend_interpreter # set CPU backend assert ngraph_bridge.is_supported_backend(backend_cpu) == True ngraph_bridge.set_backend(backend_cpu) currently_set_backend = ngraph_bridge.get_currently_set_backend_name() assert currently_set_backend == backend_cpu # create new session to execute graph with tf.Session() as sess: sess.run((out2, ), feed_dict={val: ((1.4, -0.5, -1))}) currently_set_backend = ngraph_bridge.get_currently_set_backend_name() assert currently_set_backend == backend_cpu # restore env_variables self.restore_env_variables(env_var_map)
def test_set_backend(): ngraph_bridge.enable() backend_cpu = 'CPU' backend_interpreter = 'INTERPRETER' found_cpu = False found_interpreter = False # These will only print when running pytest with flag "-s" print("Number of supported backends ", ngraph_bridge.backends_len()) supported_backends = ngraph_bridge.list_backends() print(" ****** Supported Backends ****** ") for backend_name in supported_backends: print(backend_name) if backend_name == backend_cpu: found_cpu = True if backend_name == backend_interpreter: found_interpreter = True print(" ******************************** ") assert (found_cpu and found_interpreter) == True # Create Graph val = tf.placeholder(tf.float32) out1 = tf.abs(val) out2 = tf.abs(out1) # set INTERPRETER backend assert ngraph_bridge.is_supported_backend(backend_interpreter) == True ngraph_bridge.set_backend(backend_interpreter) currently_set_backend = ngraph_bridge.get_currently_set_backend_name() assert currently_set_backend == backend_interpreter # create new session to execute graph # If you want to re-confirm which backend the graph was executed # currently the only way is to enable NGRAPH_TF_VLOG_LEVEL=5 with tf.Session() as sess: sess.run((out2, ), feed_dict={val: ((1.4, -0.5, -1))}) currently_set_backend = ngraph_bridge.get_currently_set_backend_name() assert currently_set_backend == backend_interpreter # set CPU backend assert ngraph_bridge.is_supported_backend(backend_cpu) == True ngraph_bridge.set_backend(backend_cpu) currently_set_backend = ngraph_bridge.get_currently_set_backend_name() assert currently_set_backend == backend_cpu # create new session to execute graph with tf.Session() as sess: sess.run((out2, ), feed_dict={val: ((1.4, -0.5, -1))}) currently_set_backend = ngraph_bridge.get_currently_set_backend_name() assert currently_set_backend == backend_cpu
def test_list_backends(self): assert len(ngraph_bridge.list_backends())
class Demo: # Cats & Dogs classes NUM_CLASSES = 2 # RGB CHANNELS = 3 RESNET50_POOLING_AVERAGE = 'avg' DENSE_LAYER_ACTIVATION = 'softmax' OBJECTIVE_FUNCTION = 'categorical_crossentropy' # Common accuracy metric for all outputs, but can use different metrics for different output LOSS_METRICS = ['accuracy'] IMAGE_SIZE = 160 # All images will be resized to 160x160 IMAGE_SHAPE = (IMAGE_SIZE, IMAGE_SIZE, 3) batch_size = 32 epochs = 2 fine_tune_at = 100 ngraph_backends = ngraph_bridge.list_backends() ngraph_backends.append('DISABLED') ngraph_bridge.set_backend(ngraph_backends[0]) base_model = None model = None # GUI Elements out = widgets.Output(layout={'border': '1px solid black'}) out_initial = widgets.Output(layout={'border': '1px solid black'}) out_stats = widgets.Output(layout={'border': '1px solid black'}) model_dropdown = widgets.Dropdown(options=['ResNet50', 'MobileNet v2'], value='ResNet50', description='Model:') ngraph_dropdown = widgets.Dropdown(options=ngraph_backends, value=ngraph_backends[0], description='nGraph:') progress_bar = widgets.IntProgress() progress_text = widgets.Label() epoch_text = widgets.Label() progress_box = widgets.HBox([progress_bar, progress_text, epoch_text]) batch_slider = widgets.IntSlider(min=1, max=100, value=batch_size, description='Batch Size:') epoch_slider = widgets.IntSlider(min=1, max=16, value=epochs, description='Epochs:') fine_tune_slider = widgets.IntSlider(min=1, max=500, value=fine_tune_at, description='Fine Tune at Layer:') train_button = widgets.Button(description='Train', disabled=True) classify_button = widgets.Button(description='Classify', disabled=True) fine_tune_button = widgets.Button(description='Fine Tune', disable=True) shuffle_button = widgets.Button(description='Shuffle') training_tab = widgets.VBox(children=[ model_dropdown, ngraph_dropdown, batch_slider, epoch_slider, train_button, classify_button, shuffle_button ]) fine_tuning_tab = widgets.VBox(children=[ batch_slider, epoch_slider, fine_tune_slider, fine_tune_button, classify_button, shuffle_button ]) tab = widgets.Tab(children=[training_tab, fine_tuning_tab]) tab.set_title(0, 'Training') tab.set_title(1, 'Fine Tuning') gui = widgets.VBox(children=[tab, progress_box, out_initial, out]) def __init__(self, verbose=0, test=0): self.verbose = verbose self.test = test if verbose: self.gui = widgets.VBox(children=[ self.tab, self.progress_box, self.out_initial, self.out, self.out_stats ]) # Images self.test_class_indices = [] self.test_dir = None self.init_images() self.sgd = optimizers.SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True) self.predicted_class_indices_init = [] self.wrong_guesses = [] if not test: display(self.init_gui()) self.train_button.disabled = False self.fine_tune_button.disabled = False self.init_model() def init_images(self): zip_file = tf.keras.utils.get_file( origin= "https://storage.googleapis.com/mledu-datasets/cats_and_dogs_filtered.zip", fname="cats_and_dogs_filtered.zip", extract=True) base_dir, _ = os.path.splitext(zip_file) train_dir = os.path.join(base_dir, 'train') validation_dir = os.path.join(base_dir, 'validation') self.test_dir = os.path.join(base_dir, 'test') if not os.path.exists(self.test_dir): os.makedirs(self.test_dir) # Directory with our training cat pictures train_cats_dir = os.path.join(train_dir, 'cats') # Directory with our training dog pictures train_dogs_dir = os.path.join(train_dir, 'dogs') # Directory with our validation cat pictures validation_cats_dir = os.path.join(validation_dir, 'cats') # Directory with our validation dog pictures validation_dogs_dir = os.path.join(validation_dir, 'dogs') # Directory with our test cat pictures test_cats_dir = os.path.join(self.test_dir, 'cats') if not os.path.exists(test_cats_dir): os.makedirs(test_cats_dir) for i in range(900, 1000): os.rename(train_cats_dir + '/cat.' + str(i) + '.jpg', test_cats_dir + '/cat.' + str(i) + '.jpg') # Directory with our test dog pictures test_dogs_dir = os.path.join(self.test_dir, 'dogs') if not os.path.exists(test_dogs_dir): os.makedirs(test_dogs_dir) for i in range(900, 1000): os.rename(train_dogs_dir + '/dog.' + str(i) + '.jpg', test_dogs_dir + '/dog.' + str(i) + '.jpg') # Preprocess images train_datagen = ImageDataGenerator( preprocessing_function=preprocess_input) validation_datagen = ImageDataGenerator( preprocessing_function=preprocess_input) with self.out_stats: # Flow training images in batches of 20 using train_datagen generator self.train_generator = train_datagen.flow_from_directory( train_dir, # Source directory for the training images target_size=(self.IMAGE_SIZE, self.IMAGE_SIZE), batch_size=self.batch_size, class_mode='categorical') # Flow validation images in batches of 20 using test_datagen generator self.validation_generator = validation_datagen.flow_from_directory( validation_dir, # Source directory for the validation images target_size=(self.IMAGE_SIZE, self.IMAGE_SIZE), batch_size=self.batch_size, class_mode='categorical') # Flow validation images in batches of 20 using test_datagen generator self.test_generator = validation_datagen.flow_from_directory( self.test_dir, # Source directory for the test images target_size=(self.IMAGE_SIZE, self.IMAGE_SIZE), batch_size=self.batch_size, class_mode=None, shuffle=False, seed=42) # Test Correct Values (0 Cat, 1 Dog) for file in self.test_generator.filenames: if "cat" in file: self.test_class_indices.append(0) elif "dog" in file: self.test_class_indices.append(1) else: print("Error, unclassifiable image " + file) def init_model(self, b=None): self.out_initial.clear_output() self.out.clear_output() self.out_stats.clear_output() # Initial prediction self.progress_bar.max = 13 self.progress_text.value = "Calculating Initital Predictions" with self.out: self.compile_model(self.model_dropdown.value) predictions_initial = self.model.predict_generator( self.test_generator, verbose=0, callbacks=[ProgressBar(self)]) self.predicted_class_indices_init = np.argmax(predictions_initial, axis=1) # ~ operator is equivalent to -x-1; so 0 becomes -1, 1 becomes -2; add 2 to implement xnor guesses = ~(self.predicted_class_indices_init ^ self.test_class_indices) + 2 # Randomly select 9 images that guessed incorrectly self.wrong_guesses = [] while len(self.wrong_guesses) < 9: randomIndex = random.randint( 0, len(self.predicted_class_indices_init) - 1) if not guesses[randomIndex]: self.wrong_guesses.append(randomIndex) with self.out_initial: f, ax = plt.subplots(3, 3, figsize=(15, 15)) for i in range(0, 9): test_image = os.path.join( self.test_dir, self.test_generator.filenames[self.wrong_guesses[i]]) imgRGB = mpimg.imread(test_image) predicted_class = "Dog" if self.predicted_class_indices_init[ self.wrong_guesses[i]] else "Cat" ax[i // 3, i % 3].imshow(imgRGB) ax[i // 3, i % 3].axis('off') ax[i // 3, i % 3].set_title("Predicted:{}".format(predicted_class), color='r') if predicted_class.lower() in self.test_generator.filenames[ self.wrong_guesses[i]]: ax[i // 3, i % 3].set_title("Predicted:{}".format(predicted_class), color='g') plt.show() def on_model_change(self, change): if change['type'] == 'change' and change['name'] == 'value': self.classify_button.disabled = True if change['new'] == 'ResNet50': self.epoch_slider.min = 1 self.epoch_slider.max = 16 self.epoch_slider.value = 2 if change['new'] == 'MobileNet v2': self.epoch_slider.min = 2 self.epoch_slider.max = 50 self.epoch_slider.value = 10 def on_ngraph_change(self, change): if change['type'] == 'change' and change['name'] == 'value': i = self.ngraph_backends.index(change['new']) if self.ngraph_backends[i] == 'DISABLED': self.use_ngraph = False ngraph_bridge.disable() else: self.use_ngraph = True ngraph_bridge.enable() ngraph_bridge.set_backend(self.ngraph_backends[i]) def init_gui(self): self.model_dropdown.observe(self.on_model_change) self.ngraph_dropdown.observe(self.on_ngraph_change) self.train_button.on_click(self.train_model) self.shuffle_button.on_click(self.init_model) self.classify_button.on_click(self.classify) self.fine_tune_button.on_click(self.train_model) return self.gui def train_model(self, b=None, epochs=1, batch_size=32, model='ResNet50', fine_tune_at=0): if not self.test: self.train_button.disabled = True self.epochs = self.epoch_slider.value self.batch_size = self.batch_slider.value model = self.model_dropdown.value self.progress_text.value = '' else: self.epochs = epochs self.batch_size = batch_size self.out.clear_output() if b == self.fine_tune_button: fine_tune_at = self.fine_tune_slider.value else: fine_tune_at = 0 self.compile_model(model, fine=fine_tune_at) steps_per_epoch = self.train_generator.n // self.batch_size validation_steps = self.validation_generator.n // self.batch_size with self.out_stats: history = self.model.fit_generator( self.train_generator, steps_per_epoch=steps_per_epoch, epochs=self.epochs, workers=8, validation_data=self.validation_generator, validation_steps=validation_steps, verbose=self.verbose, callbacks=[ProgressBar(self)]) self.classify_button.disabled = False # Test model immediately after training self.classify(b) self.train_button.disabled = False return history def compile_model(self, modelName, fine=0): if modelName == 'ResNet50': self.base_model = ResNet50(input_shape=self.IMAGE_SHAPE, include_top=False, weights='imagenet') elif modelName == 'MobileNet v2': self.base_model = MobileNetV2(input_shape=self.IMAGE_SHAPE, include_top=False, weights='imagenet') # GUI element update self.fine_tune_slider.max = len(self.base_model.layers) with self.out_stats: print('Setting base model to ' + modelName) # Fine Tuning if fine: self.base_model.trainable = True # Fine tune from this layer onwards self.fine_tune_at = fine # Freeze all the layers before the `fine_tune_at` layer for layer in self.base_model.layers[:self.fine_tune_at]: layer.trainable = False with self.out_stats: print('Fine tuning at layer ' + str(fine)) # Training else: self.base_model.trainable = False # Add layers self.model = tf.keras.Sequential([ self.base_model, keras.layers.GlobalAveragePooling2D(), keras.layers.Dense(self.NUM_CLASSES, activation=self.DENSE_LAYER_ACTIVATION) ]) self.model.compile(optimizer=self.sgd, loss=self.OBJECTIVE_FUNCTION, metrics=self.LOSS_METRICS) def classify(self, b): if b and b.description == 'Classify': out.clear_output() with self.out_stats: probabilities = self.model.predict_generator( self.test_generator, verbose=self.verbose, callbacks=[ProgressBar(self)]) predicted_class_indices = np.argmax(probabilities, axis=1) with self.out: f, ax = plt.subplots(3, 3, figsize=(15, 15)) for i in range(0, 9): test_image = os.path.join( self.test_dir, self.test_generator.filenames[self.wrong_guesses[i]]) imgRGB = mpimg.imread(test_image) predicted_class = "Dog" if predicted_class_indices[ self.wrong_guesses[i]] else "Cat" ax[i // 3, i % 3].imshow(imgRGB) ax[i // 3, i % 3].axis('off') ax[i // 3, i % 3].set_title("Predicted:{}".format(predicted_class), color='r') if predicted_class.lower() in self.test_generator.filenames[ self.wrong_guesses[i]]: ax[i // 3, i % 3].set_title("Predicted:{}".format(predicted_class), color='g') plt.show() with self.out_stats: wrong = ~(predicted_class_indices ^ self.test_class_indices) + 2 print("Correct Matrix") print(wrong)
if __name__ == "__main__": parser = argparse.ArgumentParser(prog='TransferLearningDemo') parser.add_argument('--gui', help='shows the GUI of the demo', action='store_true') parser.add_argument('--training', help='performs the training phase of the demo', action='store_true') parser.add_argument( '--network_type', help= 'selects the network used for training/classification [ResNet50]/MobileNet V2' ) parser.add_argument( '--backend', help= 'selects the backend used for training/classification (run ngraph_bridge.list_backends() for full list)' ) parser.add_argument('--quiet', help='disables most logging', action='store_false') args = parser.parse_args() nw = 'ResNet50' if args.network_type == 'ResNet50' or args.network_type == 'MobileNet V2': nw = args.network_type be = "PLAIDML" if args.backend != "" and args.backend in ngraph_bridge.list_backends(): be = args.backend Demo(args.gui, args.training, nw, be, args.quiet)
from rl.core import Processor from rl.callbacks import FileLogger, ModelIntervalCheckpoint INPUT_SHAPE = (84, 84) WINDOW_LENGTH = 4 try: import ngraph_bridge import tensorflow as tf config = tf.compat.v1.ConfigProto() config_ngraph_enabled = ngraph_bridge.update_config(config) sess = tf.compat.v1.Session(config=config_ngraph_enabled) print(ngraph_bridge.list_backends()) ngraph_bridge.set_backend('INTELGPU') except Exception as ex: print(ex) import tensorflow as tf config = tf.compat.v1.ConfigProto() config.gpu_options.allow_growth = True config.gpu_options.per_process_gpu_memory_fraction = 2 sess = tf.compat.v1.Session(config=config) class AtariProcessor(Processor): def __init__(self, show=False, output_dir=None):
def test_list_backends(self): backends_count = ngraph_bridge.backends_len() assert len(ngraph_bridge.list_backends()) == backends_count