Advertisement
makcimbx

Untitled

Aug 4th, 2021
691
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.51 KB | None | 0 0
  1. import random
  2.  
  3. import numpy as np # linear algebra
  4. import pathlib
  5. import tensorflow as tf
  6. from glob import glob
  7. import matplotlib.pyplot as plt
  8. import os
  9. import PIL
  10. from tensorflow import keras
  11. from tensorflow.keras import layers
  12. from tensorflow.keras.models import Sequential
  13. import pandas as pd
  14. import matplotlib.pyplot as plt
  15. from shutil import copyfile
  16. from tensorflow.keras import activations
  17. from keras import optimizers
  18.  
  19. trainpath = "C:/Users/makci/Desktop/rentgen/dataset_python/"
  20.  
  21. batch_size = 32
  22. img_height = 150
  23. img_width = 150
  24. image_size = (150, 150)
  25.  
  26.  
  27. def ds_gen(image_size, batch_size, type_ds):
  28.     return tf.keras.preprocessing.image_dataset_from_directory(
  29.         trainpath,
  30.         labels='inferred',
  31.         color_mode='rgb',
  32.         # label_mode='categorical',
  33.  
  34.         validation_split=0.2,
  35.         subset=type_ds,
  36.         seed=1488,
  37.         interpolation='bilinear',
  38.         image_size=image_size,
  39.         batch_size=batch_size,
  40.     )
  41.  
  42. train_ds = ds_gen(image_size ,batch_size , "training")
  43. val_ds = ds_gen(image_size, batch_size, "validation")
  44.  
  45. class_names = train_ds.class_names
  46. print(class_names)
  47.  
  48. AUTOTUNE = tf.data.experimental.AUTOTUNE
  49. train_ds = train_ds.cache().shuffle(30000).prefetch(buffer_size=AUTOTUNE)
  50. val_ds = val_ds.cache().prefetch(buffer_size=AUTOTUNE)
  51.  
  52. data_augmentation = keras.Sequential(
  53.   [
  54.     # layers.experimental.preprocessing.RandomFlip("horizontal_and_vertical",
  55.     #                                              input_shape=(img_height,
  56.     #                                                           img_width,
  57.     #                                                           3)),
  58.     layers.experimental.preprocessing.RandomRotation(0.1, input_shape=(img_height,
  59.                                                               img_width,
  60.                                                               3)),
  61.     layers.experimental.preprocessing.RandomZoom(0.1),
  62.   ]
  63. )
  64.  
  65.  
  66. def model_gen(num_classes, dense, drop):
  67.     return Sequential([
  68.         data_augmentation,
  69.         layers.experimental.preprocessing.Rescaling(1. / 255),
  70.         layers.Conv2D(8, 3, padding='same', activation='relu'),
  71.         layers.MaxPooling2D(),
  72.         layers.Conv2D(16, 3, padding='same', activation='relu'),
  73.         layers.MaxPooling2D(),
  74.  
  75.         layers.Conv2D(32, 3, padding='same', activation='relu'),
  76.         layers.MaxPooling2D(),
  77.         layers.Conv2D(64, 3, padding='same', activation='relu'),
  78.         layers.MaxPooling2D(),
  79.  
  80.         # layers.Dropout(0.2),
  81.         # layers.Conv2D(256, 3, padding='same', activation='relu'),
  82.         # layers.MaxPooling2D(),
  83.         layers.Dropout(drop),
  84.         layers.Flatten(),
  85.         layers.Dense(dense, activation=activations.relu),
  86.         layers.Dense(num_classes)
  87.     ])
  88.  
  89. neurons = 2048
  90. dropout = 0.2
  91. model = model_gen(len(class_names), neurons, dropout)
  92.  
  93. model.compile(optimizer='adam',
  94.                   loss=tf.keras.losses.SparseCategoricalCrossentropy(
  95.                     from_logits=True, reduction="auto", name="sparse_categorical_crossentropy"),
  96.                     metrics=['accuracy'])
  97.  
  98. model.summary()
  99.  
  100. history = model.fit(
  101.       train_ds,
  102.       validation_data=val_ds,
  103.       epochs=20
  104.     )
  105.  
  106. pd.DataFrame(model.history.history).plot()
  107. plt.show()
  108.  
  109. test_loss, test_acc = model.evaluate(val_ds)
  110.  
  111. print('\nTest accuracy:', test_acc)
  112.  
  113. # model.save("C:/Users/makci/Desktop/bestmodel")
  114.  
  115. # model = keras.models.load_model("C:/Users/makci/Desktop/bestmodel")
  116. # loss, acc = model.evaluate(train_ds, verbose=2)
  117. # print("Restored model, accuracy: {:5.2f}%".format(100 * acc))
  118. #
  119. #
  120. # def predicting(image_val):
  121. #     img = keras.preprocessing.image.load_img(
  122. #         image_val, target_size=(20, 20)
  123. #     )
  124. #     img_array = keras.preprocessing.image.img_to_array(img)
  125. #     img_array = tf.expand_dims(img_array, 0)  # Create a batch
  126. #
  127. #     predictions = model.predict(img_array)
  128. #     score = tf.nn.softmax(predictions[0])
  129. #
  130. #     return class_names[np.argmax(score)]
  131. #     # return (
  132. #     #     " :  {}  {:.2f} % confidence."
  133. #     #         .format(class_names[np.argmax(score)], 100 * np.max(score))
  134. #     # )
  135. #
  136. # directory = "C:/Users/makci/Desktop/dataset/test_set/avia-test/"
  137. # for filename in os.listdir(directory):
  138. #     if filename.endswith(".jpg") or filename.endswith(".png"):
  139. #         path = os.path.join(directory, filename)
  140. #         score = predicting(path)
  141. #         copyfile(path, "C:/Users/makci/Desktop/dataset/test_set/" + score + "/" + filename)
  142. #     else:
  143. #         continue
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement