Esempio n. 1
0
 def test_set_backend_invalid(self):
     try:
         ngraph_bridge.set_backend('POTATO')
         error_thrown = False
     except:
         error_thrown = True
     assert error_thrown
Esempio n. 2
0
    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)
Esempio n. 3
0
    def test_disable_3(self):
        old_backend = ngraph_bridge.get_backend()
        ngraph_bridge.set_backend('CPU')
        N = 1
        C = 4
        H = 10
        W = 10
        FW = 3
        FH = 3
        O = 6
        inp = tf.compat.v1.placeholder(tf.float32, shape=(N, C, H, W))
        filt = tf.constant(np.ones((FH, FW, C, O)), dtype=tf.float32)
        conv = nn_ops.conv2d(inp,
                             filt,
                             strides=[1, 1, 1, 2],
                             padding="SAME",
                             data_format='NCHW')

        def run_test(sess):
            return sess.run(conv, feed_dict={inp: np.ones((N, C, H, W))})

        # Ensure that NCHW does not run on TF CPU backend natively
        test_passed = True
        try:
            self.without_ngraph(run_test)
        except:
            test_passed = False
        if (test_passed):
            ngraph_bridge.set_backend(old_backend)
            assert False, 'Had expected test to raise error, since NCHW in conv2d is not supported'

        # We have asserted the above network would not run on TF natively.
        # Now ensure it runs with ngraph
        test_passed = True
        try:
            self.with_ngraph(run_test)
        except:
            test_passed = False
        if (not test_passed):
            ngraph_bridge.set_backend(old_backend)
            assert False, 'Had expected test to pass, since NCHW in conv2d is supported through ngraph'

        # Now disabling Conv2D. Expecting the test to fail, even when run through ngraph
        ngraph_bridge.set_disabled_ops('Conv2D')
        test_passed = True
        try:
            self.with_ngraph(run_test)
        except:
            test_passed = False
        if (test_passed):
            ngraph_bridge.set_backend(old_backend)
            assert False, 'Had expected test to raise error, since conv2D is disabled in ngraph'

        # Clean up
        ngraph_bridge.set_backend(old_backend)
        ngraph_bridge.set_disabled_ops('')
Esempio n. 4
0
    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])
Esempio n. 5
0
 def test_set_backend_invalid(self):
     env_var_map = self.store_env_variables(["NGRAPH_TF_BACKEND"])
     self.unset_env_variable("NGRAPH_TF_BACKEND")
     current_backend = ngraph_bridge.get_backend()
     error_thrown = False
     try:
         ngraph_bridge.set_backend('POTATO')
     except:
         error_thrown = True
     ngraph_bridge.set_backend(current_backend)
     self.restore_env_variables(env_var_map)
     assert error_thrown
Esempio n. 6
0
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 setup_ngraph_bridge(self, backend):
        # Enviornment variables
        os.environ['PLAIDML_USE_STRIPE'] = '1'

        if self.workers < 1:
            os.environ['OMP_NUM_THREADS'] = 1
        else:
            # Use default
            if os.getenv('OMP_NUM_THREADS') is not None:
                del os.environ['OMP_NUM_THREADS']

        import ngraph_bridge

        if backend == 'DISABLED' or backend == 'TF':
            ngraph_bridge.disable()
        elif backend == 'CPU':
            ngraph_bridge.set_backend('CPU')
            ngraph_bridge.enable()
        elif backend == 'PLAIDML':
            ngraph_bridge.set_backend('PLAIDML')
            ngraph_bridge.enable()
        else:
            print("ERROR: Unsupported backend " + backend + " selected.")
Esempio n. 8
0
    def __init__(self,
                 gui=1,
                 training=0,
                 model='ResNet50',
                 backend='INTERPRETER',
                 verbose=1):
        self.gui = gui
        self.verbose = verbose
        self.backend = backend
        ngraph_bridge.set_backend(self.backend)

        if gui:
            self.gui = widgets.VBox(children=[
                self.tab, self.progress_box, self.out_initial, self.out,
                self.out_stats
            ])
            display(self.init_gui())

        # 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 = []

        self.train_button.disabled = False
        self.fine_tune_button.disabled = False

        self.init_model()

        if training:
            self.train_model(self.train_button, model=model)
    def test_command_line_api(self, inp_format, inp_loc, out_node_name,
                              save_ng_clusters, out_format, commandline,
                              ng_device, shape_hints, precompile):
        # Only run this test when grappler is enabled
        if not ngraph_bridge.is_grappler_enabled():
            return

        # Store and unset env variable NGRAPH_TF_BACKEND because the test
        # implicitly tests with different options
        env_var_map = self.store_env_variables(["NGRAPH_TF_BACKEND"])
        self.set_env_variable("NGRAPH_TF_BACKEND", ng_device)
        ngraph_bridge.set_backend("INTERPRETER")

        assert Testtf2ngraph.format_and_loc_match(inp_format, inp_loc)
        out_loc = inp_loc.split('.')[0] + '_modified' + ('' if out_format
                                                         == 'savedmodel' else
                                                         ('.' + out_format))
        try:
            (shutil.rmtree, os.remove)[os.path.isfile(out_loc)](out_loc)
        except:
            pass
        conversion_successful = False
        try:
            optional_backend_params = {
                'CPU': {
                    'device_config': '0'
                },
                'INTERPRETER': {
                    'test_echo': '1'
                }
            }[ng_device]
            config_file_name = 'temp_config_file.json'
            Tf2ngraphJson.dump_json(config_file_name, optional_backend_params,
                                    shape_hints)
            if commandline:
                # In CI this test is expected to be run out of artifacts/test/python
                # out_node_str is empty if out_node_name is None.
                # Automatic output node inference display diagnostic logs
                # But the tf2ngraph call will still fail
                command = [
                    'python', '../../tools/tf2ngraph.py',
                    '--input_' + inp_format, inp_loc, '--output_' + out_format,
                    out_loc, '--ng_backend', ng_device, '--config_file',
                    config_file_name
                ]
                if out_node_name is not None:
                    command.extend(['--output_nodes', out_node_name])
                if precompile:
                    command.append('--precompile')
                if save_ng_clusters:
                    command.append('--save_ng_clusters')
                p = Popen(command, stdin=PIPE, stdout=PIPE, stderr=PIPE)
                output, err = p.communicate()
                rc = p.returncode
                if out_node_name is None:
                    assert rc != 0, "Call to tf2ngraph should fail when no output name is provided"
                    return
            else:
                convert(inp_format, inp_loc, out_format, out_loc, ['out_node'],
                        ng_device, "", optional_backend_params, shape_hints,
                        precompile, save_ng_clusters)
            file_present = 'ngraph_cluster_0.pbtxt' in os.listdir()
            assert save_ng_clusters == file_present
            conversion_successful = True
        finally:
            if not conversion_successful:
                try:
                    (shutil.rmtree,
                     os.remove)[os.path.isfile(out_loc)](out_loc)
                    os.remove(config_file_name)
                except:
                    pass
            if save_ng_clusters and 'ngraph_cluster_0.pbtxt' in os.listdir():
                os.remove('ngraph_cluster_0.pbtxt')
        assert conversion_successful

        gdef = get_gdef(out_format, out_loc)
        (shutil.rmtree, os.remove)[os.path.isfile(out_loc)](out_loc)
        os.remove(config_file_name)

        with tf.Graph().as_default() as g:
            tf.import_graph_def(gdef, name='')
            # The graph should have exactly one encapsulate
            assert len([
                0 for i in g.get_operations() if i.type == 'NGraphEncapsulate'
            ]) == 1
            # TODO: check that the encapsulate op has correct backend and extra params attached to it
            x = self.get_tensor(g, "x:0", False)
            y = self.get_tensor(g, "y:0", False)
            out = self.get_tensor(g, "out_node:0", False)

            sess_fn = lambda sess: sess.run(
                [out], feed_dict={i: np.zeros((10, ))
                                  for i in [x, y]})

            res1 = self.with_ngraph(sess_fn)
            res2 = self.without_ngraph(sess_fn)

            exp = [0.5 * np.ones((10, ))]
            # Note both run on Host (because NgraphEncapsulate can only run on host)
            assert np.isclose(res1, res2).all()
            # Comparing with expected value
            assert np.isclose(res1, exp).all()

        # Restore env variable NGRAPH_TF_BACKEND that was stored
        self.restore_env_variables(env_var_map)
Esempio n. 10
0
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)
Esempio n. 11
0
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):
        self.show = show
Esempio n. 12
0
def attach_device_and_ng_backend(gdef, ng_backend):
    ngraph_bridge.set_backend(ng_backend)
    # Assumes that the whole graph runs on a single ng_backend
    for n in gdef.node:
        n.device = "/device:CPU:0"
Esempio n. 13
0
 def test_set_backend(self):
     ngraph_bridge.set_backend('CPU')
     assert ngraph_bridge.get_currently_set_backend_name() == "CPU"