Advertisement
korenizla

resnet1

Feb 10th, 2023
1,306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.47 KB | None | 0 0
  1. from tensorflow.keras import Sequential
  2. from tensorflow.keras.layers import Dense, GlobalAveragePooling2D
  3. from tensorflow.keras.optimizers import Adam
  4. from tensorflow.keras.preprocessing.image import ImageDataGenerator
  5. from tensorflow.keras.applications.resnet import ResNet50
  6. import numpy as np
  7.  
  8. def load_train(path):
  9.     datagen = ImageDataGenerator(rescale=1./255, horizontal_flip=True, vertical_flip=True)
  10.  
  11.     train_datagen_flow = datagen.flow_from_directory(
  12.     path,
  13.     target_size=(150, 150),
  14.     batch_size=16,
  15.     class_mode='sparse',
  16.     seed=12345)
  17.  
  18.     return train_datagen_flow
  19.  
  20. def create_model(input_shape):
  21.  
  22.     optimizer = Adam(lr=0.0001)
  23.  
  24.     backbone = ResNet50(input_shape=(150, 150, 3),
  25.                     weights='imagenet',
  26.                     include_top=False)
  27.  
  28.  
  29.     model = Sequential()
  30.     model.add(backbone)
  31.     model.add(GlobalAveragePololing2D())
  32.  
  33.     model.add(Dense(units=12, activation='softmax'))
  34.     model.compile(optimizer=optimizer, loss='sparse_categorical_crossentropy',
  35.               metrics=['acc'])
  36.  
  37.     return model
  38.  
  39. def train_model(model, train_data, test_data, batch_size=None, epochs=3,
  40.                steps_per_epoch=None, validation_steps=None):
  41.  
  42.     model.fit(train_data, validation_data=test_data,
  43.               batch_size=batch_size, epochs=epochs,
  44.               steps_per_epoch=steps_per_epoch,
  45.               validation_steps=validation_steps,
  46.               verbose=2)
  47.  
  48.     return model
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement