Beispiel #1
0
    def get_debug_data(self, x, module, output):
        image = None
        activations = None
        if isinstance(output, tuple) and len(output) > 0:
            output = output[0]

        if hasattr(output, 'shape'):
            activations = self.get_histogram(x, output)

            if len(output.shape) > 1:
                # outputs come in batch usually, so pick first
                sample = output[0].cpu().detach().numpy()
                if len(sample.shape) == 3:
                    if sample.shape[0] == 3:
                        image = PIL.Image.fromarray(
                            get_layer_vis_square(sample))
                    else:
                        image = PIL.Image.fromarray(get_image_tales(sample))
                elif len(sample.shape) > 1:
                    image = PIL.Image.fromarray(get_layer_vis_square(sample))
                elif len(sample.shape) == 1:
                    if sample.shape[0] == 1:
                        # we got a single number
                        output = sample[0]
                    else:
                        image = make_image_from_dense(sample)
        # elif isinstance(output[0], (float, str, int)):
        #     image = output

        whistogram = None
        bhistogram = None

        if hasattr(module, 'weight') and module.weight is not None:
            whistogram = self.get_histogram(x, module.weight)

        if hasattr(module, 'bias') and module.bias is not None:
            bhistogram = self.get_histogram(x, module.bias)

        output_rep = None
        if isinstance(image, PIL.Image.Image):
            output_rep = image
        elif isinstance(output, (float, np.floating)):
            output_rep = float(output)
        elif isinstance(output, (int, np.integer)):
            output_rep = int(output)

        return output_rep, activations, whistogram, bhistogram
Beispiel #2
0
    def _image_and_histogram(self, x, output):
        image = None
        histogram = None
        if hasattr(output, 'shape'):
            # tf is not batch per default
            sample = np.copy(output)
            shape = output.shape

            if self.is_batch:
                # display only first item in batch
                sample = np.copy(output[0])
                shape = output.shape[1:]  # first is batch shizzle

            if len(shape) == 3:
                if keras.backend.image_data_format() == 'channels_last':
                    sample = np.transpose(sample, (2, 0, 1))

                if sample.shape[0] == 3:
                    image = PIL.Image.fromarray(get_layer_vis_square(sample))
                else:
                    image = PIL.Image.fromarray(get_image_tales(sample))
            elif len(shape) > 1:
                image = PIL.Image.fromarray(get_layer_vis_square(sample))
            elif len(shape) == 1:
                if shape[0] == 1:
                    # we got a single number
                    output = sample[0]
                else:
                    image = make_image_from_dense(sample)

            h = np.histogram(sample, bins=20)
            histogram = pack('<BIH', 1, int(x), h[0].size) + h[1].astype(
                '<f').tobytes() + h[0].astype('<I').tobytes()

        output_rep = None
        if isinstance(image, PIL.Image.Image):
            output_rep = image
        elif isinstance(output, (float, np.floating)):
            output_rep = float(output)
        elif isinstance(output, (int, np.integer)):
            output_rep = int(output)

        return output_rep, histogram
    def get_image_from_layer(self, layer_name: str):
        layer = self.model.get_layer(layer_name)
        model = Model(self.model.input, layer.output)
        y = model.predict(self.debug_x, steps=1)
        data = y[0]  # we pick only the first in the batch

        if isinstance(layer, (
                keras.layers.Conv2D,
                keras.layers.UpSampling2D,
                keras.layers.MaxPooling2D,
                keras.layers.AveragePooling2D,
                keras.layers.GlobalMaxPool2D,
        )):
            data = np.transpose(data, (2, 0, 1))
            image = PIL.Image.fromarray(get_image_tales(data))
        else:
            if len(data.shape) > 1:
                if len(data.shape) == 3:
                    data = np.transpose(data, (2, 0, 1))
                image = PIL.Image.fromarray(get_layer_vis_square(data))
            else:
                image = self.make_image_from_dense(data)

        return self.pil_image_to_jpeg(image)
Beispiel #4
0
    def log_insight(self,
                    *data,
                    name: str,
                    x=None,
                    image_convertion=True,
                    meta=None):
        if x is None:
            if self.job_steps > 0:
                x = self.job_iteration + (self.job_step / self.job_steps)
            elif self.job_iteration > 0:
                x = self.job_iteration
            else:
                if name not in self.auto_x_of_insight:
                    self.auto_x_of_insight[name] = 0
                self.auto_x_of_insight[name] += 1
                x = self.auto_x_of_insight[name]

        if not isinstance(x, (int, float)):
            raise Exception('x needs to be integer or float')

        if x not in self.created_insights:
            self.created_insights[x] = True
            self.client.job_action_threadsafe('addInsight', [
                x,
                time.time(),
                self.job_iteration,
                self.job_step,
            ])

        for i, d in enumerate(data):
            file_type = ''
            if isinstance(d, PIL.Image.Image):
                file_type = 'png'
                d = pil_image_to_jpeg(d)
            elif isinstance(d, np.ndarray):
                # tf is not batch per default

                if image_convertion:
                    sample = np.copy(d)
                    shape = d.shape
                    image = False
                    if len(shape) == 3:
                        try:
                            if 'keras' in sys.modules:
                                import keras
                                if keras.backend.image_data_format(
                                ) == 'channels_last':
                                    sample = np.transpose(sample, (2, 0, 1))
                            elif 'tensorflow.keras' in sys.modules:
                                import tensorflow.keras as keras
                                if keras.backend.image_data_format(
                                ) == 'channels_last':
                                    sample = np.transpose(sample, (2, 0, 1))
                        except:
                            pass

                        if sample.shape[0] == 3:
                            d = PIL.Image.fromarray(
                                get_layer_vis_square(sample))
                            image = True
                        else:
                            d = PIL.Image.fromarray(get_image_tales(sample))
                            image = True
                    elif len(shape) > 1:
                        d = PIL.Image.fromarray(get_layer_vis_square(sample))
                        image = True
                    elif len(shape) == 1:
                        if shape[0] != 1:
                            # we got a single number
                            d = sample[0]
                        else:
                            d = make_image_from_dense(sample)
                            image = True
                    if image:
                        file_type = 'png'
                        d = pil_image_to_jpeg(d)
                    else:
                        file_type = 'npy'
                        d = numpy_to_binary(d)
                else:
                    file_type = 'npy'
                    d = numpy_to_binary(d)
            else:
                file_type = 'json'
                d = bytes(json.dumps(d), encoding='utf-8')

            if len(data) > 1:
                file_name = name + '_' + str(i) + '.' + file_type
            else:
                file_name = name + '.' + file_type

            self.client.job_action_threadsafe('addInsightEntry', [
                x,
                file_name,
                datetime.utcnow().isoformat(),
                {
                    'type': file_type,
                    'meta': meta
                },
                base64.b64encode(d).decode(),
            ])