Exemplo n.º 1
0
def save_input_images(images):
    """
    Saves input images and expose them to the Flask server.

    :param images: Input images taken from Inspector
    """
    input_images = images_top_dir + "/input_images"

    if not os.path.exists(input_images):
        os.mkdir(input_images)

    for i in range(images.shape[0]):
        ts = datetime.datetime.now().timestamp()

        img_path = input_images + "/img_{}_{}.jpg".format(
            str(ts).replace(".", ""), i)
        img_relative = img_relative_path + "/input_images" + "/img_{}_{}.jpg".format(
            str(ts).replace(".", ""), i)

        image = images[i]
        image = smart_resize(image, size=(128, 128))

        save_img(img_path, image)

        if i in input_images_dict.keys():
            input_images_dict[i].append(img_relative)
        else:
            input_images_dict[i] = img_relative
Exemplo n.º 2
0
def read_images(pet_ids, target_width=224, target_height=224):

    # list with images and ids
    images = []
    processed_ids = []

    # loop for each pet id in the main dataframe
    for pet_id in tqdm(pet_ids):

        try:

            # reading image and putting it into machine format
            img = plt.imread(pet_id)
            img = image.smart_resize(img, (target_width, target_height),
                                     interpolation='nearest')
            img = image.img_to_array(img)
            img = preprocess_input(img)

            # saving
            images.append(img)
            processed_ids.append(pet_id)

        # do nothing if passes
        except:
            pass

    return np.array(images), np.array(processed_ids)
Exemplo n.º 3
0
def tfdata2h5(data_name, names, num_points, img_size):
    ds = tfds.load(data_name,
                   split='train',
                   shuffle_files=True,
                   as_supervised=True)
    h5file = data_name + str(img_size) + '.h5'
    images = np.zeros((num_points, img_size, img_size, 3), dtype='float32')
    labels = np.zeros((num_points, ), dtype='int32')
    i = 0
    for img, lbl in ds.take(num_points):
        img = tkimg.smart_resize(img, (img_size, img_size))
        images[i, :] = img.numpy() / 255
        labels[i] = lbl
        i += 1
    maxlen = max([len(n) for n in names])
    names = np.array([np.string_(name) for name in names])
    with h5py.File(h5file, 'w') as f:
        f.create_dataset('images', data=images, compression='gzip')
        f.create_dataset('labels', data=labels, compression='gzip')
        f.create_dataset('names',
                         data=names,
                         dtype='S%d' % maxlen,
                         compression='gzip')
        f.close()
    print('\nfile size: %s' % list(os.stat(h5file))[6])
    return h5file
Exemplo n.º 4
0
def images2array(files_path, img_size, preprocess=False, grayscale=False):
    files_list = sorted(os.listdir(files_path))
    n, img_array = len(files_list), []
    for i in range(n):
        if i % round(.1 * n) == 0:
            print('=>', end='', flush=True)
        img_path = files_path + files_list[i]
        if preprocess:
            img = tkimg.load_img(img_path, grayscale=grayscale)
            img = tkimg.img_to_array(img)
            img = tkimg.smart_resize(img, (img_size, img_size))
        else:
            img = tkimg.load_img(img_path, target_size=(img_size, img_size))
            img = tkimg.img_to_array(img)
        img = np.expand_dims(img, axis=0) / 255
        img_array.append(img)
    return np.array(np.vstack(img_array), dtype='float32')
Exemplo n.º 5
0
TEMPERATURE = 0.9
STRAIGHT_THROUGH = False

# Build the DiscreteVAE model
vae = DiscreteVAE(
    image_size=IMG_SIZE,  # Size of image
    num_tokens=
    NUM_VISUAL_TOKENS,  # Number of visual tokens: The paper used 8192, but could be smaller for downsized projects
    codebook_dim=CODEBOOK_DIM,  # Codebook dimension
    num_layers=
    NUM_LAYERS,  # Number of downsamples - ex. 256 / (2 ** 3) = (32 x 32 feature map)
    num_resblocks=NUM_RESBLOCKS,  # Number of resnet blocks
    hidden_dim=HIDDEN_DIM,  # Hidden dimension
    temperature=
    TEMPERATURE,  # Gumbel softmax temperature. The lower this is, the harder the discretization
    straight_through=
    STRAIGHT_THROUGH  # Straight-through for gumbel softmax. unclear if it is better one way or the other
)

vae.load_weights("./dalle_tensorflow/model_weights/vae/vae_weights")

image = load_img(image_path)
image = np.array(image)
image = smart_resize(image, size=[IMG_SIZE, IMG_SIZE])
image = normalize_img(image)
image = tf.expand_dims(image, axis=0)

output = vae(image)
output = tf.reshape(tensor=output, shape=[IMG_SIZE, IMG_SIZE, 3])
output = save_img(path="vae_out.jpg", x=output)
Exemplo n.º 6
0
def main(
    dataset_name,
    original_dataset_path,
    single_image_height,
    single_image_width,
    single_image_path,
    model_type,
):
    # Set paths
    exe_dir = os.path.dirname(os.path.abspath(__file__))
    model_dir = os.path.join(exe_dir, "..", "models")

    # Load test dataset
    if single_image_path:
        # Load and preprocess single image
        test_image = load_img(single_image_path)
        test_image = img_to_array(test_image) / 255
        test_image = smart_resize(
            test_image,
            size=(single_image_height, single_image_width),
        )
        test_image = test_image[None, ...]
    elif dataset_name:
        test_dataset, info = tfds.load(
            name=dataset_name,
            split="test",
            with_info=True,
        )
        hight_size, width_size, channel_size = info.features["image"].shape
        num_classes = info.features["label"].num_classes
    elif original_dataset_path:
        # Load original dataset from directory
        ds_loader = OriginalDatasetLoader(
            original_dataset_path,
            valid_per_train=0,
        )
        (test_dataset, _), info = ds_loader.load()
        hight_size = info["hight_size"]
        width_size = info["width_size"]
        channel_size = info["channel_size"]
        num_classes = info["num_classes"]
    else:
        raise AssertionError("The dataset is not specified correctly.")

    # Load model
    model_file_name = model_type + ".h5"
    model_path = os.path.join(model_dir, model_file_name)
    model = load_model(model_path)

    idx2label = None
    label_dict_file_name = model_type + "_label_dict.csv"
    label_dict_path_name = os.path.join(model_dir, label_dict_file_name)
    if os.path.isfile(label_dict_path_name):
        with open(label_dict_path_name) as file:
            reader = csv.reader(file)
            idx2label = {int(idx): label for label, idx in reader}

    # Evaluation
    if single_image_path:
        pred = model.predict(test_image, batch_size=1, verbose=0)
        if idx2label:
            pred_label = idx2label[np.argmax(pred[0])]
        else:
            pred_label = np.argmax(pred[0])
        score = np.max(pred)
        print(f"Predict label: {pred_label}")
        print(f"score: {score}")
    else:
        # Batch evaluation
        img_prep = TFImagePreprocessing(
            hight_size=hight_size,
            width_size=width_size,
            channel_size=channel_size,
            num_classes=num_classes,
        )  # Image preprocess
        if model_type == "SimpleCNN":
            test_dataset = test_dataset.map(img_prep.base_preprocess)
        elif model_type == "VGG16":
            test_dataset = test_dataset.map(img_prep.vgg_preprocess)
        elif model_type == "Xception":
            test_dataset = test_dataset.map(img_prep.xception_preprocess)
        else:
            raise ValueError(f"The model: {model_type} does not exist.")
        # Batch
        test_dataset = test_dataset.batch(1)
        test_loss, test_acc = model.evaluate(test_dataset)
        print(f"test_loss: {test_loss}, test_acc: {test_acc}")