Advertisement
korenizla

resnet

Feb 10th, 2023
1,579
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.48 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.  
  7. def load_train(path):
  8.     datagen = ImageDataGenerator(rescale=1./255, horizontal_flip=True, vertical_flip=True)
  9.  
  10.     train_datagen_flow = datagen.flow_from_directory(
  11.     path,
  12.     target_size=(150, 150),
  13.     batch_size=16,
  14.     class_mode='sparse',
  15.     seed=12345)
  16.  
  17.     return train_datagen_flow
  18.  
  19. def create_model(input_shape):
  20.  
  21.     optimizer = Adam(lr=0.0001)
  22.  
  23.     backbone = ResNet50(input_shape=(150, 150, 3),
  24.                     weights='/datasets/keras_models/resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5',
  25.                     include_top=False)
  26.  
  27.  
  28.     model = Sequential()
  29.     model.add(backbone)
  30.     model.add(GlobalAveragePololing2D())
  31.  
  32.     model.add(Dense(units=12, activation='softmax'))
  33.     model.compile(optimizer=optimizer, loss='sparse_categorical_crossentropy',
  34.               metrics=['acc'])
  35.  
  36.     return model
  37.  
  38. def train_model(model, train_data, test_data, batch_size=None, epochs=3,
  39.                steps_per_epoch=None, validation_steps=None):
  40.  
  41.     model.fit(train_data, validation_data=test_data, batch_size=batch_size, epochs=epochs,
  42.               steps_per_epoch=steps_per_epoch, validation_steps=validation_steps, verbose=2, shuffle=True)
  43.  
  44.     return model
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement