Advertisement
crackanddie

keras_works_2

Jan 22nd, 2022
1,221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.34 KB | None | 0 0
  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=2, activation='relu'))  # out shape (None, 13, 13, 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.Flatten())
  29. cnn.add(tf.keras.layers.Dense(64, activation='relu'))
  30. cnn.add(tf.keras.layers.Dense(4, activation='softmax'))
  31.  
  32. cnn.compile(optimizer="adam", loss="categorical_crossentropy", metrics=["accuracy"])
  33. cnn.fit(x=train_generator, epochs=14)
  34.  
  35. cnn.save('D:\\Scripts\\OpenCV\\ShelfData\\model_saved_5.h5')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement