Advertisement
neonocanon

Keras_CV

Nov 20th, 2024 (edited)
310
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.70 KB | Source Code | 0 0
  1. import numpy as np
  2. import pandas as pd
  3. import tensorflow as tf
  4. import os
  5. from tensorflow.keras.preprocessing.image import ImageDataGenerator
  6. from tensorflow.keras.layers import Conv2D, MaxPool2D, Dense, GlobalAveragePooling2D, Flatten
  7. from tensorflow.keras.models import Sequential
  8. from tensorflow.keras.optimizers import Adam, SGD, RMSprop
  9. from tensorflow.keras.applications.resnet import ResNet50
  10. from tensorflow.keras.losses import MeanAbsoluteError
  11.  
  12. def load_train(path):
  13.  
  14.     dataframe = os.path.join(path, 'labels.csv')
  15.    
  16.     train_datagen = ImageDataGenerator(rescale = 1/255.,
  17.                                       horizontal_flip=True,
  18.                                       vertical_flip=True)
  19.    
  20.     train_flow = train_datagen.flow_from_dataframe(
  21.         dataframe = pd.read_csv(dataframe),
  22.         directory = os.path.join(path, 'final_files'),
  23.         x_col='file_name',
  24.         y_col='real_age',
  25.         target_size=(224, 224),
  26.         batch_size=32,
  27.         class_mode='raw',
  28.         seed=51,
  29.         shuffle = True)
  30.    
  31.     return train_flow
  32.  
  33.  
  34. def load_test(path):
  35.  
  36.     dataframe = os.path.join(path, 'labels.csv')
  37.     test_datagen = ImageDataGenerator(rescale = 1/255.)
  38.    
  39.     test_flow = train_datagen.flow_from_dataframe(
  40.         dataframe = pd.read_csv(dataframe),
  41.         directory = os.path.join(path, 'final_files'),
  42.         x_col='file_name',
  43.         y_col='real_age',
  44.         target_size=(224, 224),
  45.         batch_size=32,
  46.         class_mode='raw',
  47.         shuffle = False)
  48.    
  49.     return test_flow
  50.  
  51. def create_model(inp_shape=(224, 224, 3)):
  52.  
  53.     resnet_50 = ResNet50(input_shape = inp_shape,
  54.                         weights = '/datasets/keras_models/resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5',
  55.                         include_top = False)
  56.  
  57.     for layer in resnet_50.layers:
  58.         layer.trainable = False
  59.  
  60.     optimizer = Adam(learning_rate=0.001)
  61.    
  62.     model = Sequential()
  63.     #model.add(inception)
  64.     model.add(resnet_50)
  65.     model.add(GlobalAveragePooling2D())
  66.     model.add(Dense(128, activation = 'relu'))
  67.     model.add(Dense(1, activation = 'relu'))
  68.     model.compile(optimizer=optimizer, loss='mse',
  69.                   metrics=['mae'])
  70.    
  71.     return model
  72.  
  73. def train_model(model, train_data, test_data, batch_size = 32, epochs = 20,
  74.                 steps_per_epoch=None, validation_steps=None):
  75.    
  76.     model.fit(train_data,
  77.               validation_data=test_data,
  78.               steps_per_epoch=steps_per_epoch,
  79.               validation_steps=validation_steps,
  80.               batch_size = batch_size,
  81.               verbose=2,
  82.               shuffle=True,
  83.               epochs = epochs)
  84.        
  85.     return model
Tags: Cv
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement