Проекции Tensorboard не будут загружать спрайт изображения при использовании с керасом, ошибка выборки метаданных - PullRequest
0 голосов
/ 08 марта 2019

Я пытаюсь использовать проекцию тензорной доски для визуализации встраивания изображений. Код работает нормально, когда я пытаюсь передать файл .tsv.

Теперь я пытаюсь передать изображения спрайтов в соответствии с тензорным потоком (квадратные спрайты). 1) Сначала я создаю спрайт-изображение, используя этот код

import csv
# import custom_confusion_matrix
from keras.callbacks import History,EarlyStopping
from keras.layers import Dense, GlobalAveragePooling2D, Dropout, Activation
from keras.applications.inception_v3 import InceptionV3
from keras.optimizers import Adam, SGD
from sklearn.metrics import confusion_matrix
from keras.utils.data_utils import Sequence
from tensorflow import set_random_seed
# import present_confusion_matrix
from keras.models import Model
from keras import callbacks
import keras.backend as K
# import present_auroc
import numpy as np
import keras
import time
import math
import cv2
import os
from sklearn.metrics import roc_curve, auc
import matplotlib.pyplot as plt
from itertools import cycle
from keras.utils import to_categorical
from keras.layers import BatchNormalization
from sklearn.metrics import classification_report
# ======================================= DATA
from tensorflow.python.keras.applications import Xception
from os.path import exists, join

def vector_to_matrix_mnist(mnist_digits):
    """Reshapes normal mnist digit (batch,28*28) to matrix (batch,28,28)"""
    return np.reshape(mnist_digits,(-1,28,28))

def invert_grayscale(mnist_digits):
    """ Makes black white, and white black """
    return 1-mnist_digits
def one_hot(pos):
    a = np.zeros(num_classes)
    a[pos] = 1
    return a
num_classes = 4


data_root_path = "/home/sriharij/Desktop/B/data/"
custom_data_path= "BD2/data/"
im_ht = im_wid = 299

def load_test_data():
    # load_dat = "../data/phone/3-fold-cropped/test"

    # load_dat = "../data/reboot/phone/5-class/test"


    load_dat = data_root_path+ custom_data_path +"alldata"

    test_x = []
    test_y = []
    test_img_names = []

    for classes in sorted(os.listdir(load_dat)):
        files = sorted(os.listdir(load_dat + "/" + classes))
        train_data_class = files[int(len(files) * .8): int(len(files) )]


        print("classes",classes)

        print(len(train_data_class))

        # all_files = np.append(test_all_files, train_data_class, axis=0)

        for image in train_data_class:
            im = cv2.imread(load_dat + "/" + classes + "/" + image)

            im = cv2.resize(im, (299, 299))

            # # print(im.shape)
            # im = im / 255
            # test_x.append(im)
            # test_y.append(one_hot(int(classes) - 1))
            # test_img_names.append(image)

            # im = (im - np.min(im)) / (np.max(im) - np.min(im))
            for ang in [0, 45, 90, 135, 180, 225, 270, 315]:
                M = cv2.getRotationMatrix2D((im_wid / 2, im_ht / 2), ang, 1)
                dst = cv2.warpAffine(im, M, (im_wid, im_ht))

                # im = im / 127.5
                # im = im - 1.
                # dst = dst / 255
                test_x.append(dst)
                test_y.append(one_hot(int(classes) - 1))
                test_img_names.append(image)
    test_x = np.array(test_x)
    test_y = np.array(test_y)
    return test_x, test_y,test_img_names

test_x, test_y,test_img_names = load_test_data()


#!/usr/bin/python

from PIL import Image
import math

test_images = test_x
test_images = test_images[0:10]
grid = int(math.sqrt(len(test_images))) + 1
image_height = int(8192 / grid)         # tensorboard supports sprite images up to 8192 x 8192
image_width = int(8192 / grid)

big_image = Image.new(
    mode='RGBA',
    size=(image_width * grid, image_height * grid),
    color=(0,0,0,0))  # fully transparent

for i in range(len(test_images)):
    row = i // grid
    col = i % grid
    img = test_images[i]
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    img = Image.fromarray(img)
    img = img.resize((image_height, image_width), Image.ANTIALIAS)
    row_loc = row * image_height
    col_loc = col * image_width
    big_image.paste(img, (col_loc, row_loc)) # NOTE: the order is reverse due to PIL saving
    print(row_loc, col_loc)
big_image = big_image.convert('RGB')
big_image.save('sprite_image.jpg', transparency=0)

2) Теперь я передаю этот файл sprite_image.jpg в тенорборд в керасе, используя

tfboard = callbacks.TensorBoard(log_dir= log_dir_p, histogram_freq=0,
                                write_graph=True, write_grads=True, write_images=True,
                                batch_size=batch_size,
                                embeddings_freq=1,
                                embeddings_layer_names=['features256','features_512'],
                                embeddings_metadata='sprite_image.jpg',
                                embeddings_data=test_x)

Когда я загружаю вкладку проекции тензорной доски, я получаю эту ошибку Я пытаюсь использовать проекцию тензорной доски для визуализации встраивания изображений. Код работает нормально, когда я пытаюсь передать .tsv файл.

Теперь я пытаюсь передать изображения спрайтов в соответствии с тензорным потоком (квадратные спрайты). 1) Сначала я создаю спрайт-изображение, используя этот код

import csv
# import custom_confusion_matrix
from keras.callbacks import History,EarlyStopping
from keras.layers import Dense, GlobalAveragePooling2D, Dropout, Activation
from keras.applications.inception_v3 import InceptionV3
from keras.optimizers import Adam, SGD
from sklearn.metrics import confusion_matrix
from keras.utils.data_utils import Sequence
from tensorflow import set_random_seed
# import present_confusion_matrix
from keras.models import Model
from keras import callbacks
import keras.backend as K
# import present_auroc
import numpy as np
import keras
import time
import math
import cv2
import os
from sklearn.metrics import roc_curve, auc
import matplotlib.pyplot as plt
from itertools import cycle
from keras.utils import to_categorical
from keras.layers import BatchNormalization
from sklearn.metrics import classification_report
# ======================================= DATA
from tensorflow.python.keras.applications import Xception
from os.path import exists, join

def vector_to_matrix_mnist(mnist_digits):
    """Reshapes normal mnist digit (batch,28*28) to matrix (batch,28,28)"""
    return np.reshape(mnist_digits,(-1,28,28))

def invert_grayscale(mnist_digits):
    """ Makes black white, and white black """
    return 1-mnist_digits
def one_hot(pos):
    a = np.zeros(num_classes)
    a[pos] = 1
    return a
num_classes = 4


data_root_path = "/home/sriharij/Desktop/B/data/"
custom_data_path= "BD2/data/"
im_ht = im_wid = 299

def load_test_data():
    # load_dat = "../data/phone/3-fold-cropped/test"

    # load_dat = "../data/reboot/phone/5-class/test"


    load_dat = data_root_path+ custom_data_path +"alldata"

    test_x = []
    test_y = []
    test_img_names = []

    for classes in sorted(os.listdir(load_dat)):
        files = sorted(os.listdir(load_dat + "/" + classes))
        train_data_class = files[int(len(files) * .8): int(len(files) )]


        print("classes",classes)

        print(len(train_data_class))

        # all_files = np.append(test_all_files, train_data_class, axis=0)

        for image in train_data_class:
            im = cv2.imread(load_dat + "/" + classes + "/" + image)

            im = cv2.resize(im, (299, 299))

            # # print(im.shape)
            # im = im / 255
            # test_x.append(im)
            # test_y.append(one_hot(int(classes) - 1))
            # test_img_names.append(image)

            # im = (im - np.min(im)) / (np.max(im) - np.min(im))
            for ang in [0, 45, 90, 135, 180, 225, 270, 315]:
                M = cv2.getRotationMatrix2D((im_wid / 2, im_ht / 2), ang, 1)
                dst = cv2.warpAffine(im, M, (im_wid, im_ht))

                # im = im / 127.5
                # im = im - 1.
                # dst = dst / 255
                test_x.append(dst)
                test_y.append(one_hot(int(classes) - 1))
                test_img_names.append(image)
    test_x = np.array(test_x)
    test_y = np.array(test_y)
    return test_x, test_y,test_img_names

test_x, test_y,test_img_names = load_test_data()


#!/usr/bin/python

from PIL import Image
import math

test_images = test_x
test_images = test_images[0:10]
grid = int(math.sqrt(len(test_images))) + 1
image_height = int(8192 / grid)         # tensorboard supports sprite images up to 8192 x 8192
image_width = int(8192 / grid)

big_image = Image.new(
    mode='RGBA',
    size=(image_width * grid, image_height * grid),
    color=(0,0,0,0))  # fully transparent

for i in range(len(test_images)):
    row = i // grid
    col = i % grid
    img = test_images[i]
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    img = Image.fromarray(img)
    img = img.resize((image_height, image_width), Image.ANTIALIAS)
    row_loc = row * image_height
    col_loc = col * image_width
    big_image.paste(img, (col_loc, row_loc)) # NOTE: the order is reverse due to PIL saving
    print(row_loc, col_loc)
big_image = big_image.convert('RGB')
big_image.save('sprite_image.jpg', transparency=0)

2) Теперь я передаю этот файл sprite_image.jpg в тенорборд в керасе, используя

tfboard = callbacks.TensorBoard(log_dir= log_dir_p, histogram_freq=0,
                                write_graph=True, write_grads=True, write_images=True,
                                batch_size=batch_size,
                                embeddings_freq=1,
                                embeddings_layer_names=['features256','features_512'],
                                embeddings_metadata='sprite_image.jpg',
                                embeddings_data=test_x)

Когда я загружаю вкладку проекции тензорной доски, я получаю эту ошибку

Error fetching metadata
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>500 Internal Server Error</title>
<h1>Internal Server Error</h1>
<p>The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.</p>

А это ошибка с консоли

TensorBoard 1.10.0 at http://venom-X399-AORUS-XTREME:6006 (Press CTRL+C to quit)
E0307 18:22:37.597437 Thread-2 _internal.py:88] Error on request:
Traceback (most recent call last):
  File "/home/venom/anaconda3/envs/py36/lib/python3.6/site-packages/werkzeug/serving.py", line 270, in run_wsgi
    execute(self.server.app)
  File "/home/venom/anaconda3/envs/py36/lib/python3.6/site-packages/werkzeug/serving.py", line 258, in execute
    application_iter = app(environ, start_response)
  File "/home/venom/anaconda3/envs/py36/lib/python3.6/site-packages/tensorboard/backend/application.py", line 274, in __call__
    return self.data_applications[clean_path](environ, start_response)
  File "/home/venom/anaconda3/envs/py36/lib/python3.6/site-packages/werkzeug/wrappers.py", line 308, in application
    resp = f(*args[:-2] + (request,))
  File "/home/venom/anaconda3/envs/py36/lib/python3.6/site-packages/tensorboard/plugins/projector/projector_plugin.py", line 514, in _serve_metadata
    for line in f:
  File "/home/venom/anaconda3/envs/py36/lib/python3.6/site-packages/tensorflow/python/lib/io/file_io.py", line 220, in __next__
    return self.next()
  File "/home/venom/anaconda3/envs/py36/lib/python3.6/site-packages/tensorflow/python/lib/io/file_io.py", line 214, in next
    retval = self.readline()
  File "/home/venom/anaconda3/envs/py36/lib/python3.6/site-packages/tensorflow/python/lib/io/file_io.py", line 184, in readline
    return self._prepare_value(self._read_buf.ReadLineAsString())
  File "/home/venom/anaconda3/envs/py36/lib/python3.6/site-packages/tensorflow/python/lib/io/file_io.py", line 100, in _prepare_value
    return compat.as_str_any(val)
  File "/home/venom/anaconda3/envs/py36/lib/python3.6/site-packages/tensorflow/python/util/compat.py", line 107, in as_str_any
    return as_str(value)
  File "/home/venom/anaconda3/envs/py36/lib/python3.6/site-packages/tensorflow/python/util/compat.py", line 80, in as_text
    return bytes_or_text.decode(encoding)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte
E0307 18:22:37.597437 140030588733184 _internal.py:88] Error on request:
Traceback (most recent call last):
  File "/home/venom/anaconda3/envs/py36/lib/python3.6/site-packages/werkzeug/serving.py", line 270, in run_wsgi
    execute(self.server.app)
  File "/home/venom/anaconda3/envs/py36/lib/python3.6/site-packages/werkzeug/serving.py", line 258, in execute
    application_iter = app(environ, start_response)
  File "/home/venom/anaconda3/envs/py36/lib/python3.6/site-packages/tensorboard/backend/application.py", line 274, in __call__
    return self.data_applications[clean_path](environ, start_response)
  File "/home/venom/anaconda3/envs/py36/lib/python3.6/site-packages/werkzeug/wrappers.py", line 308, in application
    resp = f(*args[:-2] + (request,))
  File "/home/venom/anaconda3/envs/py36/lib/python3.6/site-packages/tensorboard/plugins/projector/projector_plugin.py", line 514, in _serve_metadata
    for line in f:
  File "/home/venom/anaconda3/envs/py36/lib/python3.6/site-packages/tensorflow/python/lib/io/file_io.py", line 220, in __next__
    return self.next()
  File "/home/venom/anaconda3/envs/py36/lib/python3.6/site-packages/tensorflow/python/lib/io/file_io.py", line 214, in next
    retval = self.readline()
  File "/home/venom/anaconda3/envs/py36/lib/python3.6/site-packages/tensorflow/python/lib/io/file_io.py", line 184, in readline
    return self._prepare_value(self._read_buf.ReadLineAsString())
  File "/home/venom/anaconda3/envs/py36/lib/python3.6/site-packages/tensorflow/python/lib/io/file_io.py", line 100, in _prepare_value
    return compat.as_str_any(val)
  File "/home/venom/anaconda3/envs/py36/lib/python3.6/site-packages/tensorflow/python/util/compat.py", line 107, in as_str_any
    return as_str(value)
  File "/home/venom/anaconda3/envs/py36/lib/python3.6/site-packages/tensorflow/python/util/compat.py", line 80, in as_text
    return bytes_or_text.decode(encoding)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte

Может кто-нибудь поделиться, где я иду не так? Использование изображений спрайтов для тензорной доски плохо документировано, и я не мог найти способ решить эту проблему

...