def trainModel( model = None): # Trains a model # model = optional parameter; creates new if not passed; otherwise keeps training # Returns: # model: trained Keras model crop_range = 1 # number of pixels to crop image (if size is 235, crops are 0-223, 1-224, ... 11-234) target_size = 224 datasrc = "ilsvrc14" #dataGen = dg_v1.prepDataGen(target_size = target_size, batch_size = 64 ) dataGen = as_v2.AugSequence ( target_size=target_size, crop_range=crop_range, batch_size=256, datasrc=datasrc, test=False, debug=True ) if model is None: input_shape = (224, 224, 3) model = m_v7.prepModel ( input_shape = input_shape, \ L1_size_stride_filters = (7, 2, 96), L1MaxPool_size_stride = (3, 2), \ L2_size_stride_filters = (5, 2, 256), L2MaxPool_size_stride = (3, 2), \ L3_size_stride_filters = (3, 1, 384), \ L4_size_stride_filters = (3, 1, 384), \ L5_size_stride_filters = (3, 1, 256), L5MaxPool_size_stride = (3, 2), \ D1_size = 4096, \ D2_size = 4096) full_epochs = 50 # 1 epoch is full pass of data over all variants of 16x16 shifts # 12x12 = 144 passes through original images in 1 full epoch # full epoch is 12x12 = 144 passes over data: 1 times for each subframe model.fit_generator ( dataGen, steps_per_epoch=len(dataGen), epochs=full_epochs, verbose=2 ) e_v2.eval(model, target_size=target_size, datasrc=datasrc) e_v2.eval(model, target_size=target_size, datasrc=datasrc, test=True) return model
def trainModel( model = None): # Trains a model # model = optional parameter; creates new if not passed; otherwise keeps training # Returns: # model: trained Keras model crop_range = 1 # number of pixels to crop image (if size is 235, crops are 0-223, 1-224, ... 11-234) target_size = 224 #dataGen = dg_v1.prepDataGen(target_size = target_size, batch_size = 64 ) dataGen = as_v1.AugSequence ( target_size=target_size, crop_range=crop_range, batch_size=256, test=False, debug=True ) if model is None: input_shape = (224, 224, 3) model = m_v7.prepModel ( input_shape = input_shape, \ L1_size_stride_filters = (7, 2, 96), L1MaxPool_size_stride = (3, 2), \ L2_size_stride_filters = (5, 2, 256), L2MaxPool_size_stride = (3, 2), \ L3_size_stride_filters = (3, 1, 384), \ L4_size_stride_filters = (3, 1, 384), \ L5_size_stride_filters = (3, 1, 256), L5MaxPool_size_stride = (3, 2), \ D1_size = 4096, \ D2_size = 4096) full_epochs = 50 # 1 epoch is full pass of data over all variants of 16x16 shifts # 12x12 = 144 passes through original images in 1 full epoch # full epoch is 12x12 = 144 passes over data: 1 times for each subframe model.fit_generator ( dataGen, steps_per_epoch=len(dataGen), epochs=full_epochs, verbose=2 ) #for full_epoch in range (full_epochs): # # for each subframe within a image... # for epoch_single_subframe in range ( crop_range * crop_range ): # #Pick a starting pixel # h_ind = int (epoch_single_subframe / crop_range) # w_ind = epoch_single_subframe % crop_range # size = target_size - crop_range + 1 # #shuffle data upon reset # dataGen.reset() # iter_in_epoch = 0 # for X,Y in dataGen: # #print ("len(dataGen):",str(len(dataGen))) # X_subframe = X [ :, h_ind:h_ind+size, w_ind:w_ind+size, : ] # #print ("X_subframe.shape,X.shape,h_ind,w_ind,size",X_subframe.shape,X.shape,h_ind,w_ind,size) # model.fit ( X_subframe, Y, verbose=0 ) # iter_in_epoch += 1 # if iter_in_epoch >= len(dataGen): # break # print ("full_epoch, epoch_single_subframe:",time.strftime("%H:%M:%S"), full_epoch, epoch_single_subframe ) #e_v1.eval(model) e_v2.eval(model, target_size=target_size) e_v2.eval(model, target_size=target_size, test=True) return model
def trainModel(model=None): # Trains a model # model = optional parameter; creates new if not passed; otherwise keeps training # Returns: # model: trained Keras model crop_range = 12 # number of pixels to crop image (if size is 235, crops are 0-223, 1-224, ... 11-234) crop_size = 224 target_size = crop_size + crop_range - 1 #235 dataGen = dg_v1.prepDataGen(target_size=target_size, batch_size=64) if model is None: input_shape = (224, 224, 3) model = m_v6.prepModel ( input_shape = input_shape, \ L1_size_stride_filters = (7, 2, 96), L1MaxPool_size_stride = (3, 2), \ L2_size_stride_filters = (5, 2, 256), L2MaxPool_size_stride = (3, 2), \ L3_size_stride_filters = (3, 1, 384), \ L4_size_stride_filters = (3, 1, 384), \ D1_size = 4096) full_epochs = 3 # 1 epoch is full pass of data over all variants of 16x16 shifts # 12x12 = 144 passes through original images in 1 full epoch # full epoch is 12x12 = 144 passes over data: 1 times for each subframe for full_epoch in range(full_epochs): # for each subframe within a image... for epoch_single_subframe in range(crop_range * crop_range): #Pick a starting pixel h_ind = int(epoch_single_subframe / crop_range) w_ind = epoch_single_subframe % crop_range size = target_size - crop_range + 1 #shuffle data upon reset dataGen.reset() iter_in_epoch = 0 for X, Y in dataGen: #print ("len(dataGen):",str(len(dataGen))) X_subframe = X[:, h_ind:h_ind + size, w_ind:w_ind + size, :] #print ("X_subframe.shape,X.shape,h_ind,w_ind,size",X_subframe.shape,X.shape,h_ind,w_ind,size) model.fit(X_subframe, Y, verbose=0) iter_in_epoch += 1 if iter_in_epoch >= len(dataGen): break print("full_epoch, epoch_single_subframe:", time.strftime("%H:%M:%S"), full_epoch, epoch_single_subframe) #e_v1.eval(model) e_v2.eval(model, target_size=crop_size) e_v2.eval(model, target_size=crop_size, test=True) return model
def loadModel(model=None, epochs=1): # Trains a model # model = optional parameter; creates new if not passed; otherwise keeps training # epochs - number of max epochs to train (subject to early stopping) # Returns: # model: trained Keras model crop_range = 32 # number of pixels to crop image (if size is 235, crops are 0-223, 1-224, ... 11-234) target_size = 224 datasrc = "ilsvrc14_full" #datasrc = "ilsvrc14" # "presumed mean" of X, subtract from all input #subtractMean=0.5 # Load pre-calculated RGB mean, PCA (Principal Component Analysis) eigenvectors and eigenvalues #subtractMean=np.array ( [ 0.4493, 0.4542, 0.3901 ] ) #subtractMean = np.load("..\\rgb_mean.npy") #pca_eigenvectors = np.load("..\\eigenvectors.npy") #pca_eigenvalues = np.load("..\\eigenvalues.npy") #dataGen = dg_v1.prepDataGen(target_size = target_size, batch_size = 64 ) #dataGen = as_v4.AugSequence ( target_size=target_size, crop_range=crop_range, allow_hor_flip=True, batch_size=128, \ # subtractMean=subtractMean, pca_eigenvectors=pca_eigenvectors, pca_eigenvalues=pca_eigenvalues, \ # datasrc=datasrc, test=False ) #Load pretrained model model = VGG16() #prepare a validation data generator, used for early stopping #vldDataGen = dg_v1.prepDataGen( target_size=target_size, test=True, batch_size=128, datasrc=datasrc ) #vldDataGen = as_v4.AugSequence ( target_size=target_size, crop_range=1, allow_hor_flip=False, batch_size=128, subtractMean=subtractMean, datasrc=datasrc, test=True ) #callback_earlystop = EarlyStopping ( monitor='val_acc', min_delta=0.001, patience=20, verbose=1, mode='max', restore_best_weights=True ) # full epoch is 12x12 = 144 passes over data: 1 times for each subframe #model.fit_generator ( dataGen, steps_per_epoch=len(dataGen), epochs=epochs, verbose=2, validation_data=vldDataGen, validation_steps=len(vldDataGen), callbacks=[callback_earlystop] ) #model.fit_generator ( dataGen, steps_per_epoch=1, epochs=epochs, verbose=2, validation_data=vldDataGen, validation_steps=len(vldDataGen), callbacks=[callback_earlystop] ) #model.fit_generator ( dataGen, steps_per_epoch=len(dataGen), epochs=epochs, verbose=2 ) #model.fit_generator ( dataGen, steps_per_epoch=1, epochs=epochs, verbose=2 ) #print ("Evaluation on train set (1 frame)") #e_v2.eval(model, target_size=target_size, datasrc=datasrc) print("Evaluation on validation set (1 frame)") e_v2.eval(model, target_size=target_size, datasrc=datasrc, preprocess="vgg", test=True) print("Evaluation on validation set (5 frames)") #e_v3.eval(model, target_size=target_size, datasrc=datasrc, preprocess="vgg", test=True) print("Evaluation on validation set (10 frames)") #e_v4.eval(model, target_size=target_size, datasrc=datasrc, preprocess="vgg", test=True) return model
def trainModel( model = None, epochs = 1): # Trains a model # model = optional parameter; creates new if not passed; otherwise keeps training # epochs - number of max epochs to train (subject to early stopping) # Returns: # model: trained Keras model crop_range = 32 # number of pixels to crop image (if size is 235, crops are 0-223, 1-224, ... 11-234) target_size = 224 datasrc = "ilsvrc14_50classes" #datasrc = "ilsvrc14" #dataGen = dg_v1.prepDataGen(target_size = target_size, batch_size = 64 ) dataGen = as_v3.AugSequence ( target_size=target_size, crop_range=crop_range, batch_size=128, datasrc=datasrc, test=False, debug=True ) if model is None: input_shape = (target_size, target_size, 3) model = m_v8.prepModel ( input_shape = input_shape, \ L1_size_stride_filters = (11, 4, 96), L1MaxPool_size_stride = (3, 2), \ L2_size_stride_filters = (5, 1, 256), L2MaxPool_size_stride = (3, 2), \ L3_size_stride_filters = (3, 1, 384), \ L4_size_stride_filters = (3, 1, 384), \ L5_size_stride_filters = (3, 1, 256), L5MaxPool_size_stride = (3, 2), \ D1_size = 4096, \ D2_size = 4096, \ Softmax_size = 50, \ Conv_padding = "same" ) #epochs = 0 #prepare a validation data generator, used for early stopping vldDataGen = dg_v1.prepDataGen( target_size=target_size, test=True, batch_size=128, datasrc=datasrc ) callback_earlystop = EarlyStopping ( monitor='val_acc', min_delta=0.001, patience=20, verbose=1, mode='max', restore_best_weights=True ) # full epoch is 12x12 = 144 passes over data: 1 times for each subframe model.fit_generator ( dataGen, steps_per_epoch=len(dataGen), epochs=epochs, verbose=2, validation_data=vldDataGen, validation_steps=len(vldDataGen), callbacks=[callback_earlystop] ) #model.fit_generator ( dataGen, steps_per_epoch=1, epochs=epochs, verbose=2, validation_data=vldDataGen, validation_steps=len(vldDataGen), callbacks=[callback_earlystop] ) #model.fit_generator ( dataGen, steps_per_epoch=len(dataGen), epochs=epochs, verbose=2 ) #model.fit_generator ( dataGen, steps_per_epoch=1, epochs=epochs, verbose=2 ) print ("Evaluation on train set (1 frame)") e_v2.eval(model, target_size=target_size, datasrc=datasrc) print ("Evaluation on validation set (1 frame)") e_v2.eval(model, target_size=target_size, datasrc=datasrc, test=True) print ("Evaluation on validation set (5 frames)") e_v3.eval(model, target_size=target_size, datasrc=datasrc, test=True) print ("Evaluation on validation set (10 frames)") e_v4.eval(model, target_size=target_size, datasrc=datasrc, test=True) return model
def trainModel(model=None): # Trains a model # model = optional parameter; creates new if not passed; otherwise keeps training # Returns: # model: trained Keras model target_size = 161 dataGen = dg_v1.prepDataGen(target_size=target_size, batch_size=64) if model is None: model = m_v2.prepModel() full_epochs = 10 # 1 epoch is full pass of data over all variants of 12x12 shifts # 12x12 = 144 passes through original images in 1 full epoch crop_range = 12 # number of pixels to crop image (if size is 161, crops are 0-149, 1-150, ... 11-160) # full epoch is 12x12 = 144 passes over data: 1 times for each subframe for full_epoch in range(full_epochs): # for each subframe within a image... for epoch_single_subframe in range(crop_range * crop_range): #Pick a starting pixel h_ind = int(epoch_single_subframe / crop_range) w_ind = epoch_single_subframe % crop_range size = target_size - crop_range + 1 #shuffle data upon reset dataGen.reset() iter_in_epoch = 0 for X, Y in dataGen: #print ("len(dataGen):",str(len(dataGen))) X_subframe = X[:, h_ind:h_ind + size, w_ind:w_ind + size, :] #print ("X_subframe.shape,X.shape,h_ind,w_ind,size",X_subframe.shape,X.shape,h_ind,w_ind,size) model.fit(X_subframe, Y, verbose=0) iter_in_epoch += 1 if iter_in_epoch >= len(dataGen): break print("full_epoch, epoch_single_subframe:", time.strftime("%H:%M:%S"), full_epoch, epoch_single_subframe) #e_v1.eval(model) e_v2.eval(model) e_v2.eval(model, test=True) return model
def trainModel(model=None, epochs=1): # Trains a model # model = optional parameter; creates new if not passed; otherwise keeps training # epochs - number of max epochs to train (subject to early stopping) # Returns: # model: trained Keras model crop_range = 32 # number of pixels to crop image (if size is 235, crops are 0-223, 1-224, ... 11-234) target_size = 224 datasrc = "ilsvrc14_50classes" #datasrc = "ilsvrc14" # "presumed mean" of X, subtract from all input #subtractMean=0.5 # Load pre-calculated RGB mean, PCA (Principal Component Analysis) eigenvectors and eigenvalues #subtractMean=np.array ( [ 0.4493, 0.4542, 0.3901 ] ) subtractMean = np.load("..\\rgb_mean.npy") pca_eigenvectors = np.load("..\\eigenvectors.npy") pca_eigenvalues = np.load("..\\eigenvalues.npy") #dataGen = dg_v1.prepDataGen(target_size = target_size, batch_size = 64 ) dataGen = as_v4.AugSequence ( target_size=target_size, crop_range=crop_range, allow_hor_flip=True, batch_size=128, \ subtractMean=subtractMean, pca_eigenvectors=pca_eigenvectors, pca_eigenvalues=pca_eigenvalues, \ datasrc=datasrc, test=False ) if model is None: input_shape = (target_size, target_size, 3) model = m_v8.prepModel ( input_shape = input_shape, \ L1_size_stride_filters = (11, 4, 96), L1MaxPool_size_stride = (3, 2), L1_dropout = 0.0, \ L2_size_stride_filters = (5, 1, 256), L2MaxPool_size_stride = (3, 2), L2_dropout = 0.0, \ L3_size_stride_filters = (3, 1, 384), L3_dropout = 0.0, \ L4_size_stride_filters = (3, 1, 384), L4_dropout = 0.0, \ L5_size_stride_filters = (3, 1, 256), L5MaxPool_size_stride = (3, 2), L5_dropout = 0.0, \ D1_size = 4096, D1_dropout = 0.5, \ D2_size = 4096, D2_dropout = 0.55, \ Softmax_size = 50, \ Conv_padding = "same" ) #prepare a validation data generator, used for early stopping #vldDataGen = dg_v1.prepDataGen( target_size=target_size, test=True, batch_size=128, datasrc=datasrc ) vldDataGen = as_v3.AugSequence(target_size=target_size, crop_range=1, allow_hor_flip=False, batch_size=128, subtractMean=subtractMean, datasrc=datasrc, test=True) callback_earlystop = EarlyStopping(monitor='val_acc', min_delta=0.001, patience=20, verbose=1, mode='max', restore_best_weights=True) # full epoch is 12x12 = 144 passes over data: 1 times for each subframe model.fit_generator(dataGen, steps_per_epoch=len(dataGen), epochs=epochs, verbose=2, validation_data=vldDataGen, validation_steps=len(vldDataGen), callbacks=[callback_earlystop]) #model.fit_generator ( dataGen, steps_per_epoch=1, epochs=epochs, verbose=2, validation_data=vldDataGen, validation_steps=len(vldDataGen), callbacks=[callback_earlystop] ) #model.fit_generator ( dataGen, steps_per_epoch=len(dataGen), epochs=epochs, verbose=2 ) #model.fit_generator ( dataGen, steps_per_epoch=1, epochs=epochs, verbose=2 ) print("Evaluation on train set (1 frame)") e_v2.eval(model, target_size=target_size, subtractMean=subtractMean, datasrc=datasrc) print("Evaluation on validation set (1 frame)") e_v2.eval(model, target_size=target_size, subtractMean=subtractMean, datasrc=datasrc, test=True) print("Evaluation on validation set (5 frames)") e_v3.eval(model, target_size=target_size, subtractMean=subtractMean, datasrc=datasrc, test=True) print("Evaluation on validation set (10 frames)") e_v4.eval(model, target_size=target_size, subtractMean=subtractMean, datasrc=datasrc, test=True) return model
def trainModel(model=None): # Trains a model # model = optional parameter; creates new if not passed; otherwise keeps training # Returns: # model: trained Keras model crop_range = 12 # number of pixels to crop image (if size is 235, crops are 0-223, 1-224, ... 11-234) crop_size = 224 target_size = crop_size + crop_range - 1 #235 input_shape = (224, 224, 3) ################### FIT GENERATOR ################# #dataGen = dg_v1.prepDataGen(target_size = 224, batch_size = 64 ) dataGen = as_v1.AugSequence(target_size=224, crop_range=2, batch_size=256, test=False) model = m_v7.prepModel ( input_shape = input_shape, \ L1_size_stride_filters = (7, 2, 96), L1MaxPool_size_stride = (3, 2), \ L2_size_stride_filters = (5, 2, 256), L2MaxPool_size_stride = (3, 2), \ L3_size_stride_filters = (3, 1, 384), \ L4_size_stride_filters = (3, 1, 384), \ L5_size_stride_filters = (3, 1, 256), L5MaxPool_size_stride = (3, 2), \ D1_size = 4096, \ D2_size = 128) print("FIT GENERATOR START ", time.strftime("%H:%M:%S")) model.fit_generator(dataGen, steps_per_epoch=len(dataGen), epochs=2, verbose=2) print("FIT GENERATOR END ", time.strftime("%H:%M:%S")) e_v2.eval(model, target_size=crop_size) e_v2.eval(model, target_size=crop_size, test=True) #################### FIT LOOP ################# #model = m_v7.prepModel ( input_shape = input_shape, \ # L1_size_stride_filters = (7, 2, 96), L1MaxPool_size_stride = (3, 2), \ # L2_size_stride_filters = (5, 2, 256), L2MaxPool_size_stride = (3, 2), \ # L3_size_stride_filters = (3, 1, 384), \ # L4_size_stride_filters = (3, 1, 384), \ # L5_size_stride_filters = (3, 1, 256), L5MaxPool_size_stride = (3, 2), \ # D1_size = 4096, \ # D2_size = 128) ##dataGen = dg_v1.prepDataGen(target_size = 224, batch_size = 64 ) #dataGen = as_v1.AugSequence ( target_size=224, batch_size=64, test=False ) ## full epoch is 12x12 = 144 passes over data: 1 times for each subframe ##print (time.strftime("%H:%M:%S")) #for full_epoch in range (20): # # for each subframe within a image... # for epoch_single_subframe in range ( 1 * 1 ): # #Pick a starting pixel # h_ind = 0 #int (epoch_single_subframe / crop_range) # w_ind = 0 #epoch_single_subframe % crop_range # size = 224 #target_size - crop_range + 1 # #shuffle data upon reset # #dataGen.reset() # iter_in_epoch = 0 # for X,Y in dataGen: # #print ("len(dataGen):",str(len(dataGen))) # X_subframe = X [ :, h_ind:h_ind+size, w_ind:w_ind+size, : ] # #print ("X_subframe.shape,X.shape,h_ind,w_ind,size",X_subframe.shape,X.shape,h_ind,w_ind,size) # model.fit ( X_subframe, Y, verbose=0 ) # iter_in_epoch += 1 # if iter_in_epoch >= len(dataGen): # break # #print ("full_epoch, epoch_single_subframe:",time.strftime("%H:%M:%S"), full_epoch, epoch_single_subframe ) # #e_v1.eval(model) # print ("FIT START ", time.strftime("%H:%M:%S")) # e_v2.eval(model, target_size=crop_size) # e_v2.eval(model, target_size=crop_size, test=True) # print ("FIT END ", time.strftime("%H:%M:%S")) ##print (time.strftime("%H:%M:%S")) return model
def trainModel(epochs=1): # Trains a model # model = optional parameter; creates new if not passed; otherwise keeps training # epochs - number of max epochs to train (subject to early stopping) # Returns: # model: trained Keras model crop_range = 1 # number of pixels to crop image (if size is 235, crops are 0-223, 1-224, ... 11-234) target_size = 256 datasrc = "sco_v3" #Softmax_size=33 # "presumed mean" of X, subtract from all input #subtractMean=0.5 #dataGen = dg_v1.prepDataGen(target_size = target_size, batch_size = 64 ) dataGen = as_v5.AugSequence ( target_size=target_size, crop_range=crop_range, allow_hor_flip=True, batch_size=32, \ #subtractMean=subtractMean, pca_eigenvectors=pca_eigenvectors, pca_eigenvalues=pca_eigenvalues, \ preprocess="div255", datasrc=datasrc, test=False, debug=True ) Softmax_size = len(dataGen.dataGen().class_indices) #model = m_v7.prepModel (Softmax_size=Softmax_size, L1MaxPool_size_stride=(2,2), L2MaxPool_size_stride=(2,2), L5MaxPool_size_stride=(2,2) ) model = m_v13.prepModel(target_size=target_size, Softmax_size=Softmax_size) model.summary() #prepare a validation data generator, used for early stopping vldDataGen = as_v5.AugSequence ( target_size=target_size, crop_range=1, allow_hor_flip=False, batch_size=32, \ preprocess="div255", datasrc=datasrc, test=True ) callback_earlystop = EarlyStopping( monitor='val_acc', min_delta=0.001, patience=20, verbose=1, mode='max') #, restore_best_weights=True ) lc_file_name = lc_file_pattern.format(date.today().strftime("%Y%m%d"), Softmax_size) callback_csv_logger = CSVLogger(lc_file_name + '.csv', separator=",", append=False) #callback_earlystop = EarlyStopping ( monitor='val_acc', min_delta=0., patience=0, verbose=2, mode='auto', restore_best_weights=True ) model_file_name = model_file_pattern.format( date.today().strftime("%Y%m%d"), Softmax_size) mcp_save = ModelCheckpoint(model_file_name, save_best_only=True, monitor='val_acc', mode='max') # full epoch is 12x12 = 144 passes over data: 1 times for each subframe model.fit_generator( dataGen, steps_per_epoch=len(dataGen), epochs=epochs, verbose=2, validation_data=vldDataGen, validation_steps=len(vldDataGen), callbacks=[callback_earlystop, mcp_save, callback_csv_logger]) #model.fit_generator ( dataGen, steps_per_epoch=len(dataGen), epochs=epochs, verbose=2 ) # Loading best saved model model = load_model(model_file_name) #print ("Evaluation on train set (1 frame)") #e_v2.eval(model, target_size=target_size, datasrc=datasrc) print("Evaluation on validation set (1 frame)") e_v2.eval(model, target_size=target_size, datasrc=datasrc, preprocess="div255", test=True) print("Evaluation on validation set (5 frames)") e_v3.eval(model, target_size=target_size, datasrc=datasrc, preprocess="div255", test=True) print("Evaluation on validation set (10 frames)") e_v4.eval(model, target_size=target_size, datasrc=datasrc, preprocess="div255", test=True) #print ("Evaluation on test set (1 frame)") e_v2.eval(model, target_size=target_size, datasrc=datasrc, preprocess="div255", testtest=True) return model