crackanddie

keras_mb_works

Jan 22nd, 2022 (edited)
233
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # import statements
  2. import tensorflow as tf
  3. from keras.preprocessing.image import ImageDataGenerator
  4. import numpy as np
  5.  
  6.  
  7. # blue empty white yellow
  8.  
  9. # loading training data
  10. train_datagen = ImageDataGenerator(
  11.         rescale=1./255,
  12.         shear_range=0.1,
  13.         zoom_range=0.1,
  14.         brightness_range=[0.2, 1.0],
  15.         horizontal_flip=True)
  16. train_generator = train_datagen.flow_from_directory(
  17.         'D:\\Scripts\\OpenCV\\ShelfData\\TrainWithSub\\',
  18.         target_size=(30, 30),
  19.         batch_size=56,
  20.         class_mode='categorical')
  21.  
  22.  
  23. cnn = tf.keras.models.Sequential()
  24. cnn.add(tf.keras.layers.Conv2D(filters=48, kernel_size=3, activation='relu', input_shape=[30, 30, 3]))  # out shape (None, 28, 28, 48)
  25. cnn.add(tf.keras.layers.MaxPool2D(pool_size=2, strides=2))  # out shape (None, 14, 14, 48)
  26. cnn.add(tf.keras.layers.Conv2D(filters=48, kernel_size=3, activation='relu'))  # out shape (None, 12, 12, 48)
  27. cnn.add(tf.keras.layers.MaxPool2D(pool_size=2, strides=2))  # out shape (None, 6, 6, 48)
  28. cnn.add(tf.keras.layers.Conv2D(filters=32, kernel_size=3, activation='relu'))  # out shape (None, 4, 4, 32)
  29. cnn.add(tf.keras.layers.MaxPool2D(pool_size=2, strides=2))  # out shape (None, 2, 2, 32)
  30. cnn.add(tf.keras.layers.Flatten())
  31. cnn.add(tf.keras.layers.Dense(128, activation='relu'))
  32. cnn.add(tf.keras.layers.Dense(64, activation='relu'))
  33. cnn.add(tf.keras.layers.Dense(4, activation='softmax'))
  34.  
  35. cnn.compile(optimizer="adam", loss="categorical_crossentropy", metrics=["accuracy"])
  36. cnn.fit(x=train_generator, epochs=14)
  37.  
  38. cnn.save('D:\\Scripts\\OpenCV\\ShelfData\\model_saved_5.h5')
RAW Paste Data Copied