<jupyter_start><jupyter_text>Import Data to TensorFlow<jupyter_code>import tensorflow as tf
import tensorflow_addons as tfa
img_files = []
labels    = []

for f in files:
    img_files.append(files[f]['image'])
    labels.append(files[f]['data']['Content'])
    
IMG_WIDTH = 150
IMG_HEIGHT = 150
BATCH_SIZE = 64

Map = {}
count = 0
Labels = []

for label in labels:
    if not Map.get(label):
        Map[label] = count
        count += 1
    Labels.append(Map[label])
    
def decode_img(img):
    # convert the compressed string to a 3D uint8 tensor
    img = tf.image.decode_jpeg(img, channels=3)
    # Use `convert_image_dtype` to convert to floats in the [0,1] range.
    img = tf.image.convert_image_dtype(img, tf.float32)
    # resize the image to the desired size.
    return tf.image.resize(img, [IMG_WIDTH, IMG_HEIGHT])
def process_path(file_path):
    # load the raw data from the file as a string
    img = tf.io.read_file(file_path)
    img = decode_img(img)
    return img
list_ds = tf.data.Dataset.from_tensor_slices(img_files)
image_ds = list_ds.map(process_path)
label_ds = tf.data.Dataset.from_tensor_slices(Labels)
train_ds = tf.data.Dataset.zip((image_ds, label_ds))

train_ds = train_ds.shuffle(1024).batch(BATCH_SIZE)<jupyter_output><empty_output><jupyter_text>Model Creation<jupyter_code>model = tf.keras.Sequential()

model.add(tf.keras.Input(shape=(IMG_WIDTH, IMG_HEIGHT, 3)))

model.add(tf.keras.layers.Conv2D(
    64, 
    (2,2), 
    strides=2,
    activation='relu',
    padding='same'
))

model.add(tf.keras.layers.Conv2D(
    64, 
    (2,2), 
    strides=3,
    activation='relu',
    padding='same'
))

model.add(tf.keras.layers.Conv2D(
    64, 
    (2,2), 
    strides=2,
    activation='relu',
    padding='same'
))

model.add(tf.keras.layers.Flatten())

model.add(tf.keras.layers.Dense(256, activation=None))

model.add(tf.keras.layers.Lambda(lambda x: tf.math.l2_normalize(x, axis=1)) # L2 normalize embeddings
)

model.summary()
model.compile(
    optimizer=tf.keras.optimizers.Adam(),
    loss=tfa.losses.TripletSemiHardLoss())
# Train the network
history = model.fit(
    train_ds,
    epochs=20)
model.save("plant_images.tf", save_format="tf")
# Train the network
history = model.fit(
    train_ds,
    epochs=10)
model.save("plant_images_v2.tf", save_format="tf")
import umap
import matplotlib.pyplot as plt

results = model.predict(train_ds)

reducer = umap.UMAP()
embedding = reducer.fit_transform(results)
plt.scatter(embedding[:,0], embedding[:,1])
batch = train_ds.take(1)
IMGS, LABS = [], []

for b in batch:
    imgs, labs = b[0], b[1]
    IMGS.extend(imgs)
    LABS.extend(labs)
    
print(IMGS)
res = model.predict([IMGS])
import seaborn as sns

res = model.predict([IMGS])
embedding = reducer.fit_transform(res)
plt.scatter(embedding[:,0], embedding[:,1], c=[sns.color_palette()[x] for x in LABS])
# Train the network
history = model.fit(
    train_ds,
    epochs=50
)

model.save("plant_images_v3.tf", save_format="tf")
res = model.predict([IMGS])
embedding = reducer.fit_transform(res)
plt.scatter(embedding[:,0], embedding[:,1], c=[sns.color_palette()[x] for x in LABS])
Map
Pam = {
    3: 'LeafScan',
    1: 'Flower',
    2: 'Entire',
    4: 'Fruit',
    5: 'Leaf',
    6: 'Branch',
    7: 'Stem'
}
C = {}
colors = [(x, sns.color_palette()[x]) for x in LABS]
for i in range(1,8):
    for idx, color in colors:
        if i == idx:
            print(idx.numpy())
            C[idx.numpy()] = color
            break
C
type(res)
import numpy as np

to_show = []
for idx, color in colors:
    if idx.numpy() in [2, 4, 6]:
        to_show.append(idx.numpy())
_LABS = np.array(LABS)
print(_LABS)
embedding = reducer.fit_transform(res[to_show])
plt.scatter(embedding[:,0], embedding[:,1], c=[sns.color_palette()[x] for x in _LABS[to_show]])<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1510866719036397, 'fraction_numerical': 0.020162346163917256, 'mean_word_length': 3.948186528497409, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '319296', 'license': 'MIT', 'n_tokens_mistral': 1516, 'n_tokens_neox': 1481, 'n_words': 300, 'path': 'notebooks/PlantImages.ipynb', 'repo_name': 'gengler1123/Triplet_Loss_Demo', 'size': '3819'}