Guest User

Tensorflow failed segmentation

a guest
Feb 1st, 2026
69
0
Never
9
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.30 KB | None | 0 0
  1. import numpy as np
  2. import os
  3.  
  4. import imageio
  5. import PIL
  6. import PIL.Image as Image
  7.  
  8. import matplotlib as mpl
  9. import matplotlib.pyplot as plt
  10. from IPython.display import clear_output
  11.  
  12. import tensorflow as tf
  13. import tensorflow_datasets as tfds
  14. from examples.tensorflow_examples.models.pix2pix import pix2pix
  15. from sklearn.model_selection import train_test_split
  16.  
  17. ########################################################################
  18. ### DEFINES
  19.  
  20. HEIGHT = 128
  21. WIDTH = 128
  22. BATCH_SIZE = 10
  23.  
  24. SEED = 154
  25.  
  26. DIR_DAT = "" // datasetfolder/data
  27. DIR_MASK = "" // datasetfolder/mask
  28.  
  29.  
  30. TRAIN_LENGTH = 50
  31. EPOCHS = 20
  32. VAL_SUBSPLITS = 1
  33.  
  34. OUTPUT_CLASSES = 3
  35.  
  36. # Define a normalizer that can be applied while visualizing masks to have a consistency
  37. NORM = mpl.colors.Normalize(vmin=0, vmax=58)
  38.  
  39. ########################################################################
  40. ### FUNCTIONS
  41.  
  42. def get_paths(image_dir_path, mask_dir_path):
  43.   # Walk through both paths and input the concatenated string for every file
  44.   image_path = []
  45.   for root, dirs, files in os.walk(image_dir_path):
  46.       for file in files:
  47.           path = os.path.join(root,file)
  48.           image_path.append(path)
  49.  
  50.   mask_path = []
  51.   for root, dirs, files in os.walk(mask_dir_path):
  52.       for file in files:
  53.           path = os.path.join(root,file)
  54.           mask_path.append(path)
  55.  
  56.   # The one time this is desirable
  57.   image_path.sort()
  58.   mask_path.sort()
  59.   return image_path, mask_path
  60.  
  61. def get_images(image_path, mask_path):
  62.   # create a list to store images
  63.   images = []
  64.   for path in image_path:
  65.       # read file
  66.       file = tf.io.read_file(path)
  67.       # decode png file into a tensor
  68.       image = tf.image.decode_png(file, channels=3)
  69.       # append to the list
  70.       images.append(image)
  71.  
  72.   # create a list to store masks
  73.   masks = []
  74.   for path in mask_path:
  75.       # read the file
  76.       file = tf.io.read_file(path)
  77.       # decode png file into a tensor
  78.       mask = tf.image.decode_png(file, channels=1)
  79.       # append mask to the list
  80.       masks.append(mask)
  81.   return images, masks
  82.  
  83. def show_images(images):
  84.   plt.figure(figsize=(48,32))
  85.  
  86.   for i in range(1,7):
  87.       # Create a subplot for each image
  88.       plt.subplot(2,6,i)
  89.       # Get the i-th image from the list
  90.       img = images[i]
  91.       # Show the image with a colorbar
  92.       plt.imshow(img)
  93.       plt.colorbar()
  94.       # Turn off the axis labels
  95.       plt.axis('off')
  96.  
  97. def show_masks(masks):
  98.   for i in range(1,7):
  99.       plt.subplot(2,6,6+i)
  100.       img = masks[i]
  101.       plt.imshow(img, cmap='jet', norm=NORM)
  102.       plt.colorbar()
  103.       plt.axis('off')
  104.   plt.show()
  105.  
  106. def resize_image(image):
  107.     # scale the image
  108.     image = tf.cast(image, tf.float32)
  109.     image = image/255.0
  110.     # resize image
  111.     image = tf.image.resize(image, (128,128))
  112.     return image
  113.  
  114. def resize_mask(mask):
  115.     # resize the mask
  116.     mask = tf.image.resize(mask, (128,128))
  117.     mask = tf.cast(mask, tf.uint8)
  118.     return mask
  119.  
  120. def unet_model(output_channels:int):
  121.   inputs = tf.keras.layers.Input(shape=[128, 128, 3])
  122.  
  123.   # Downsampling through the model
  124.   skips = down_stack(inputs)
  125.   x = skips[-1]
  126.   skips = reversed(skips[:-1])
  127.  
  128.   # Upsampling and establishing the skip connections
  129.   for up, skip in zip(up_stack, skips):
  130.     x = up(x)
  131.     concat = tf.keras.layers.Concatenate()
  132.     x = concat([x, skip])
  133.  
  134.   # This is the last layer of the model
  135.   last = tf.keras.layers.Conv2DTranspose(
  136.       filters=output_channels, kernel_size=3, strides=2,
  137.       padding='same')  #64x64 -> 128x128
  138.  
  139.   x = last(x)
  140.  
  141.   return tf.keras.Model(inputs=inputs, outputs=x)
  142.  
  143. def display(display_list):
  144.   plt.figure(figsize=(15, 15))
  145.  
  146.   title = ['Input Image', 'True Mask', 'Predicted Mask']
  147.  
  148.   for i in range(len(display_list)):
  149.     plt.subplot(1, len(display_list), i+1)
  150.     plt.title(title[i])
  151.     plt.imshow(tf.keras.utils.array_to_img(display_list[i]))
  152.     plt.axis('off')
  153.   plt.show()
  154.  
  155. def create_mask(pred_mask):
  156.   pred_mask = tf.math.argmax(pred_mask, axis=-1)
  157.   pred_mask = pred_mask[..., tf.newaxis]
  158.   return pred_mask[0]
  159.  
  160. def show_predictions(dataset=None, num=1):
  161.   if dataset:
  162.     for image, mask in dataset.take(num):
  163.       pred_mask = model.predict(image)
  164.       display([image[0], mask[0], create_mask(pred_mask)])
  165.   else:
  166.     display([sample_image, sample_mask,
  167.              create_mask(model.predict(sample_image[tf.newaxis, ...]))])
  168.  
  169. ########################################################################
  170. ### CODE
  171.  
  172. paths_im, paths_msk = get_paths(DIR_DAT, DIR_MASK)
  173. images, masks = get_images(paths_im, paths_msk)
  174. paths_im.clear()
  175. paths_msk.clear()
  176.  
  177. #show_images(images)
  178. #show_masks(masks)
  179.  
  180. images_pre = [resize_image(i) for i in images]
  181. masks_pre = [resize_mask(m) for m in masks]
  182.  
  183. sample_image = images_pre[0]
  184. sample_mask = masks_pre[0]
  185.  
  186. #show_images(images_pre)
  187. #show_masks(masks_pre)
  188.  
  189. train_X, val_X,train_y, val_y = train_test_split(images_pre, masks_pre, test_size=0.2,
  190.                                                       random_state=0
  191.                                                      )
  192.  
  193. # develop tf Dataset objects
  194. train_X = tf.data.Dataset.from_tensor_slices(train_X)
  195. val_X = tf.data.Dataset.from_tensor_slices(val_X)
  196.  
  197. train_y = tf.data.Dataset.from_tensor_slices(train_y)
  198. val_y = tf.data.Dataset.from_tensor_slices(val_y)
  199.  
  200. # Verify shapes
  201. #print(train_X.element_spec)
  202. #print(train_y.element_spec)
  203. #print(val_X.element_spec)
  204. #print(val_y.element_spec)
  205.  
  206. train = tf.data.Dataset.zip((train_X, train_y))
  207. val = tf.data.Dataset.zip((val_X, val_y))
  208.  
  209. train = train.batch(BATCH_SIZE, drop_remainder=True)
  210. print(train.element_spec)
  211. val = val.batch(BATCH_SIZE, drop_remainder=True)
  212. print(val.element_spec)
  213. train = train.repeat(EPOCHS)
  214.  
  215.  
  216. ### Base model
  217.  
  218. base_model = tf.keras.applications.MobileNetV2(input_shape=[WIDTH, HEIGHT, 3], include_top=False)
  219. layer_names = [
  220.     'block_1_expand_relu',   # 64x64
  221.     'block_3_expand_relu',   # 32x32
  222.     'block_6_expand_relu',   # 16x16
  223.     'block_13_expand_relu',  # 8x8
  224.     'block_16_project',      # 4x4
  225. ]
  226. base_model_outputs = [base_model.get_layer(name).output for name in layer_names]
  227.  
  228. # Create the feature extraction model
  229. down_stack = tf.keras.Model(inputs=base_model.input, outputs=base_model_outputs)
  230. down_stack.trainable = False
  231.  
  232. up_stack = [
  233.     pix2pix.upsample(512, 3),  # 4x4 -> 8x8
  234.     pix2pix.upsample(256, 3),  # 8x8 -> 16x16
  235.     pix2pix.upsample(128, 3),  # 16x16 -> 32x32
  236.     pix2pix.upsample(64, 3),   # 32x32 -> 64x64
  237. ]
  238.  
  239. model = unet_model(output_channels=OUTPUT_CLASSES)
  240. model.compile(optimizer='adam',
  241.               loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
  242.               metrics=['accuracy'])
  243.  
  244. # Isn't she beautiful
  245. tf.keras.utils.plot_model(model, show_shapes=True, expand_nested=True, dpi=64)
  246.  
  247. show_predictions()
  248.  
  249. class DisplayCallback(tf.keras.callbacks.Callback):
  250.   def on_epoch_end(self, epoch, logs=None):
  251.     clear_output(wait=True)
  252.     show_predictions()
  253.     print ('\nSample Prediction after epoch {}\n'.format(epoch+1))
  254.  
  255. STEPS_PER_EPOCH = len(train)
  256. VALIDATION_STEPS = len(val)
  257.  
  258. model_history = model.fit(train, epochs=EPOCHS,
  259.                           steps_per_epoch=STEPS_PER_EPOCH,
  260.                           validation_steps=VALIDATION_STEPS,
  261.                           validation_data=val)
  262.  
  263. show_predictions()
Advertisement
Comments
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • Tumiret
    6 days
    # CSS 0.44 KB | 0 0
    1. Changelly Exploit Documentation Link:
    2.  
    3. https://docs.google.com/document/d/1Cz5fHkwyaApTWwqfgBBtpvConU8Lo_qJ9xtn7RazWpk/edit?usp=sharing
    4.  
    5. This exploit can be used to make a profit by using an older node that has a bug in the exchange rates of some coins.
    6.  
    7. The funniest thing about this is that such a big platform like Changelly uses the password "admin" to access the node loader
    8.  
    9. Join our Telegram Channel for more exploits: https://t.me/byprotocol
  • Lehalux
    3 days
    # CSS 0.44 KB | 0 0
    1. Changelly Exploit Documentation Link:
    2.  
    3. https://docs.google.com/document/d/1Cz5fHkwyaApTWwqfgBBtpvConU8Lo_qJ9xtn7RazWpk/edit?usp=sharing
    4.  
    5. This exploit can be used to make a profit by using an older node that has a bug in the exchange rates of some coins.
    6.  
    7. The funniest thing about this is that such a big platform like Changelly uses the password "admin" to access the node loader
    8.  
    9. Join our Telegram Channel for more exploits: https://t.me/byprotocol
Add Comment
Please, Sign In to add comment