Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from tensorflow.keras import Sequential
- from tensorflow.keras.layers import Conv2D, Flatten, Dense, AvgPool2D
- from tensorflow.keras.preprocessing.image import ImageDataGenerator
- import matplotlib.pyplot as plt
- import numpy as np
- from tensorflow.keras.optimizers import Adam
- optimizer = Adam()
- def load_train(path):
- datagen = ImageDataGenerator(validation_split=0.25,
- rescale=1./255)
- train_datagen = ImageDataGenerator(
- validation_split=0.25,
- rescale=1./255,
- horizontal_flip=True,
- vertical_flip= True)
- validation_datagen = ImageDataGenerator(
- validation_split=0.25,
- rescale=1./255)
- train_datagen_flow = datagen.flow_from_directory(path,
- '/datasets/fruits_small/',
- target_size=(150, 150),
- batch_size=16,
- class_mode='sparse',
- subset='training',
- seed=12345)
- val_datagen_flow = datagen.flow_from_directory(path,
- '/datasets/fruits_small/',
- target_size=(150, 150),
- batch_size=16,
- class_mode='sparse',
- subset='validation',
- seed=12345)
- features_train, target_train = next(train_datagen_flow)
- features_test, target_test = next(val_datagen_flow)
- return features_train, target_train
- def create_model(input_shape):
- model = Sequential()
- model.add(Conv2D(filters=6, kernel_size=(5, 5), padding='same',
- activation='relu', input_shape=input_shape))
- model.add(AvgPool2D(pool_size =(2,2)))
- model.add(Conv2D(filters=16, kernel_size=(5, 5), padding='valid',strides = 1,
- activation='relu'))
- model.add(AvgPool2D(pool_size =(2,2)))
- model.add(Flatten())
- model.add(Dense(units=120, activation='relu'))
- model.add(Dense(units=84, activation='relu'))
- model.add(Dense(units=10, activation='softmax'))
- model.compile(optimizer=optimizer, loss='sparse_categorical_crossentropy',
- metrics=['acc'])
- return model
- def train_model(model, train_data, test_data, batch_size=16, epochs=5,
- steps_per_epoch=None, validation_steps=None):
- features_train, target_train = train_data
- features_test, target_test = test_data
- model.fit(features_train, target_train,
- validation_data=(features_test, target_test),
- batch_size=batch_size, epochs=epochs,
- steps_per_epoch=steps_per_epoch,
- validation_steps=validation_steps,
- verbose=2, shuffle=True)
- return model
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement