-
Notifications
You must be signed in to change notification settings - Fork 0
/
negative test.py
81 lines (64 loc) · 2.37 KB
/
negative test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
"""
Gebaseerd op https://keras.io/examples/vision/siamese_network/
"""
import matplotlib.pyplot as plt
import numpy as np
import os
import random
import tensorflow as tf
from pathlib import Path
from keras import applications
from keras import layers
from keras import losses
from keras import optimizers
from keras import metrics
from keras import Model
from keras.applications import resnet
from keras.models import save_model
# Doel grootte van afbeeldingen
target_shape = (200, 200)
cache_dir = Path(Path.home()) / ".keras"
# Basis afbeeldingen/anker afbeelding, invoeren registraties van merken
anchor_images_path = cache_dir / "Anchor"
# Vergelijkbare afbeeldingen, invoeren afbeeldingen die lijken op de merken (zoals op auto etc.)
positive_images_path = cache_dir / "Positive"
def preprocess_image(filename):
# Afbeeldingen inlezen, decoden, en resizen
image_string = tf.io.read_file(filename)
image = tf.image.decode_jpeg(image_string, channels=3)
image = tf.image.convert_image_dtype(image, tf.float32)
image = tf.image.resize(image, target_shape)
return image
def preprocess_triplets(anchor, positive, negative):
# Drieling afbeeldingen retourneren
return (
preprocess_image(anchor),
preprocess_image(positive),
preprocess_image(negative),
)
# We need to make sure both the anchor and positive images are loaded in
# sorted order so we can match them together.
anchor_images = sorted(
[str(anchor_images_path / f) for f in os.listdir(anchor_images_path)]
)
print(anchor_images)
positive_images = sorted(
[str(positive_images_path / f) for f in os.listdir(positive_images_path)]
)
# Pak afbeeldinglocatie van input afbeelding
print(os.path.dirname(__file__))
input_folder_path = os.path.join(os.path.dirname(__file__), 'input')
for file in os.listdir(input_folder_path):
input_image = os.path.join(input_folder_path, file)
#input_data = [input_image]
image_count = len(anchor_images)
anchor_dataset = tf.data.Dataset.from_tensor_slices(anchor_images)
#input_dataset = tf.data.Dataset.from_tensor_slices(input_data)
rng = np.random.RandomState(seed=42)
rng.shuffle(anchor_images)
rng.shuffle(positive_images)
negative_images = anchor_images + positive_images
np.random.RandomState(seed=32).shuffle(negative_images)
print(positive_images)
print(negative_images)
negative_dataset = tf.data.Dataset.from_tensor_slices(negative_images)