Advertisement
deniswhite77

овощи

Feb 24th, 2022
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.48 KB | None | 0 0
  1. from tensorflow.keras import Sequential
  2. from tensorflow.keras.layers import Conv2D, Flatten, Dense, AvgPool2D
  3. from tensorflow.keras.preprocessing.image import ImageDataGenerator
  4. import matplotlib.pyplot as plt
  5. import numpy as np
  6. from tensorflow.keras.optimizers import Adam
  7. optimizer = Adam()
  8.  
  9.  
  10. def load_train(path):
  11.     datagen = ImageDataGenerator(validation_split=0.25,
  12.                              rescale=1./255)
  13.     train_datagen = ImageDataGenerator(
  14.     validation_split=0.25,
  15.     rescale=1./255,
  16.     horizontal_flip=True,
  17.     vertical_flip= True)
  18.    
  19.     validation_datagen = ImageDataGenerator(
  20.     validation_split=0.25,
  21.     rescale=1./255)
  22.    
  23.     train_datagen_flow = datagen.flow_from_directory(path,
  24.     '/datasets/fruits_small/',
  25.     target_size=(150, 150),
  26.     batch_size=16,
  27.     class_mode='sparse',
  28.     subset='training',
  29.     seed=12345)
  30.    
  31.     val_datagen_flow = datagen.flow_from_directory(path,
  32.     '/datasets/fruits_small/',
  33.     target_size=(150, 150),
  34.     batch_size=16,
  35.     class_mode='sparse',
  36.     subset='validation',
  37.     seed=12345)
  38.    
  39.     features_train, target_train = next(train_datagen_flow)
  40.     features_test, target_test = next(val_datagen_flow)
  41.  
  42.     return features_train, target_train
  43.  
  44.  
  45. def create_model(input_shape):
  46.     model = Sequential()
  47.     model.add(Conv2D(filters=6, kernel_size=(5, 5), padding='same',
  48.                  activation='relu', input_shape=input_shape))
  49.     model.add(AvgPool2D(pool_size =(2,2)))
  50.     model.add(Conv2D(filters=16, kernel_size=(5, 5), padding='valid',strides = 1,
  51.                  activation='relu'))
  52.     model.add(AvgPool2D(pool_size =(2,2)))
  53.     model.add(Flatten())
  54.     model.add(Dense(units=120, activation='relu'))
  55.     model.add(Dense(units=84, activation='relu'))
  56.     model.add(Dense(units=10, activation='softmax'))
  57.     model.compile(optimizer=optimizer, loss='sparse_categorical_crossentropy',
  58.                   metrics=['acc'])
  59.  
  60.     return model
  61.  
  62.  
  63. def train_model(model, train_data, test_data, batch_size=16, epochs=5,
  64.                steps_per_epoch=None, validation_steps=None):
  65.  
  66.     features_train, target_train = train_data
  67.     features_test, target_test = test_data
  68.     model.fit(features_train, target_train,
  69.               validation_data=(features_test, target_test),
  70.               batch_size=batch_size, epochs=epochs,
  71.               steps_per_epoch=steps_per_epoch,
  72.               validation_steps=validation_steps,
  73.               verbose=2, shuffle=True)
  74.  
  75.     return model
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement