Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # import statements
- import tensorflow as tf
- from keras.preprocessing.image import ImageDataGenerator
- import numpy as np
- # blue empty white yellow
- # loading training data
- train_datagen = ImageDataGenerator(
- rescale=1./255,
- shear_range=0.1,
- zoom_range=0.1,
- brightness_range=[0.2, 1.0],
- horizontal_flip=True)
- train_generator = train_datagen.flow_from_directory(
- 'D:\\Scripts\\OpenCV\\ShelfData\\TrainWithSub\\',
- target_size=(30, 30),
- batch_size=56,
- class_mode='categorical')
- cnn = tf.keras.models.Sequential()
- cnn.add(tf.keras.layers.Conv2D(filters=48, kernel_size=3, activation='relu', input_shape=[30, 30, 3])) # out shape (None, 28, 28, 48)
- cnn.add(tf.keras.layers.MaxPool2D(pool_size=2, strides=2)) # out shape (None, 14, 14, 48)
- cnn.add(tf.keras.layers.Conv2D(filters=48, kernel_size=3, activation='relu')) # out shape (None, 12, 12, 48)
- cnn.add(tf.keras.layers.MaxPool2D(pool_size=2, strides=2)) # out shape (None, 6, 6, 48)
- cnn.add(tf.keras.layers.Conv2D(filters=32, kernel_size=3, activation='relu')) # out shape (None, 4, 4, 32)
- cnn.add(tf.keras.layers.MaxPool2D(pool_size=2, strides=2)) # out shape (None, 2, 2, 32)
- cnn.add(tf.keras.layers.Flatten())
- cnn.add(tf.keras.layers.Dense(128, activation='relu'))
- cnn.add(tf.keras.layers.Dense(64, activation='relu'))
- cnn.add(tf.keras.layers.Dense(4, activation='softmax'))
- cnn.compile(optimizer="adam", loss="categorical_crossentropy", metrics=["accuracy"])
- cnn.fit(x=train_generator, epochs=14)
- cnn.save('D:\\Scripts\\OpenCV\\ShelfData\\model_saved_5.h5')
Add Comment
Please, Sign In to add comment